package com.steerinfo.meterwork.meterworkaddweight.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.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkaddweight.mapper.MeterWorkAddWeightMapper;
import com.steerinfo.meterwork.meterworkaddweight.model.MeterWorkAddWeight;
import com.steerinfo.meterwork.meterworkaddweight.service.IMeterWorkAddWeightService;
import com.steerinfo.meterwork.meterworkcaractualfirst.mapper.MeterWorkCarActualFirstMapper;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.meterwork.meterworkcaractualfirst.service.IMeterWorkCarActualFirstService;
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.List;

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

    @Autowired
    private MeterWorkAddWeightMapper meterWorkAddWeightMapper;


    @Autowired
    IMeterWorkCarActualFirstService meterWorkCarActualFirstService;

    @Resource
    private MeterWorkCarActualFirstMapper meterWorkCarActualFirstMapper;

    @Override
    protected IBaseMapper<MeterWorkAddWeight, String> getMapper() {
        return meterWorkAddWeightMapper;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public String checkhandadd(MeterWorkAddWeight model)
    {
        check(model);
        String no=meterWorkAddWeightMapper.getNewID();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        model.setAddWeightActNo(sdf.format(new Date())+no);
        //修改实绩中的附加重量总和
        MeterWorkCarActualFirst mwcaf = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirstNo());
        if (mwcaf.getAddWeight() != null){
            BigDecimal b = mwcaf.getAddWeight().add(new BigDecimal(model.getUtensilWeight())) ;
            mwcaf.setAddWeight(b);
        }else {
            mwcaf.setAddWeight(new BigDecimal(model.getUtensilWeight()));
        }
        meterWorkCarActualFirstMapper.updateByPrimaryKeySelective(mwcaf);
        //新增数据
        meterWorkAddWeightMapper.insertSelective(model);
        return model.getAddWeightActNo();
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int deleteList(List<MeterWorkAddWeight> models)
    {
       if (models==null || models.size() <= 0) {
           throw new MarkerMetException(500,"参数为空！！");
       }
        List<String>  ids = new ArrayList<>();
        for (MeterWorkAddWeight model : models){
            MeterWorkCarActualFirst mwcaf = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirstNo());
            if (mwcaf.getAddWeight() == null || mwcaf.getAddWeight().compareTo(new BigDecimal(0)) <= 0){
                throw new MarkerMetException(500, "附加重量已为0！！");
            }
            Double sumd = mwcaf.getAddWeight().doubleValue() - model.getUtensilWeight();
            mwcaf.setAddWeight(new BigDecimal(sumd));
            meterWorkCarActualFirstMapper.updateByPrimaryKeySelective(mwcaf);
            //删除数据
            ids.add(model.getAddWeightActNo());
        }
        int num =  meterWorkCarActualFirstMapper.batchDelete(ids);
        return num;
    }

    private void  check(MeterWorkAddWeight model)throws MarkerMetException
    {
        if(model.getActualFirstNo()==null || model.getActualFirstNo().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,"器具属性名称不能为空");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        if(model.getAddWeightActNo() == null){
            model.setCreateTime(new Date());
            model.setCreateManNo(userId);
            model.setCreateManName(userName);
        }
    }
}
