package com.steerinfo.pretrack.peraddweight.service.impl;

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.meterwork.constants.DbConstants;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.pretrack.peraddweight.model.PerAddWeight;
import com.steerinfo.pretrack.peraddweight.mapper.PerAddWeightMapper;
import com.steerinfo.pretrack.peraddweight.service.IPerAddWeightService;
import com.steerinfo.pretrack.pretrackscale.mapper.PreTrackScaleMapper;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * PerAddWeight服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2020-12-04 08:33
 * 类描述
 * 修订历史：
 * 日期：2020-12-04
 * 作者：generator
 * 参考：
 * 描述：PerAddWeight服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "perAddWeightService")
public class PerAddWeightServiceImpl extends BaseServiceImpl<PerAddWeight, String> implements IPerAddWeightService {

    @Autowired
    private PerAddWeightMapper perAddWeightMapper;

    @Resource
    private PreTrackScaleMapper preTrackScaleMapper;

    @Override
    protected IBaseMapper<PerAddWeight, String> getMapper() {
        return perAddWeightMapper;
    }

    @Override
    @Transactional
    public String checkAndAdd(PerAddWeight model){
        check(model);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(model.getCreateTime());
        String no = perAddWeightMapper.getNewID(afl);
        model.setPerdictionAddNo(afl+no);
        //修改预报中的附加重量总和
        PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(model.getPredictionNo());
        if (pts.getAddWeightSum() != null){
            BigDecimal b = pts.getAddWeightSum().add(new BigDecimal(model.getUtensilWeight())) ;
            pts.setAddWeightSum(b);
        }else {
            pts.setAddWeightSum(new BigDecimal(model.getUtensilWeight()));
        }
        preTrackScaleMapper.updateByPrimaryKeySelective(pts);
        //新增数据
        perAddWeightMapper.insertSelective(model);
        return no;
    }


    @Override
    @Transactional
    public int checkAndDelete(List<PerAddWeight> models){
        //修改预报中的附加重量总和
        if (models.size() == 0){
            throw new MarkerMetException(500, "参数不能为空");
        }
        List<String>  ids = new ArrayList<>();
        for (PerAddWeight model : models){
            PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(model.getPredictionNo());
            if (pts.getAddWeightSum() == null || pts.getAddWeightSum().compareTo(new BigDecimal(0)) <= 0){
                throw new MarkerMetException(500, "附加重量已为0！！");
            }
            Double sumd = pts.getAddWeightSum().doubleValue() - model.getUtensilWeight();
            pts.setAddWeightSum(new BigDecimal(sumd));
            preTrackScaleMapper.updateByPrimaryKeySelective(pts);
            //删除数据
            ids.add(model.getPerdictionAddNo());
        }
        int num =  perAddWeightMapper.batchDelete(ids);
        return num;
    }

    private void check(PerAddWeight model){
        if (model == null){
            throw new MarkerMetException(500, "参数不能为空");
        }
        if (model.getAddWeightNo() == null || model.getAddWeightNo().equals("")) {
            throw new MarkerMetException(500, "附加重量编号不能为空");
        }
        if (model.getUtensilNo() == null || model.getUtensilNo().equals("")) {
            throw new MarkerMetException(500, "器具编号不能为空");
        }
        if (model.getUtensilName() == null || model.getUtensilName().equals("")) {
            throw new MarkerMetException(500, "器具名称不能为空");
        }
        if (model.getUtensilWeight() == null || model.getUtensilWeight().equals("")) {
            throw new MarkerMetException(500, "器具重量不能为空");
        }
        if (model.getUtensilTypeNo() == null || model.getUtensilTypeNo().equals("")) {
            throw new MarkerMetException(500, "器具属性编号不能为空");
        }
        if (model.getUtensilUtensilName() == null || model.getUtensilUtensilName().equals("")) {
            throw new MarkerMetException(500, "器具属性名称不能为空");
        }
        if (model.getPredictionNo() == null || model.getPredictionNo().equals("")) {
            throw new MarkerMetException(500, "预报编号不能为空");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        if (StringUtils.isEmpty(model.getPerdictionAddNo())) {
            model.setCreateManNo(userId);
            model.setCreateManName(userName);
            model.setCreateTime(new Date());
        }
    }
}
