package com.steerinfo.meterwork.meterworkstoragecalibration.service.impl;

import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.meterwork.constants.DbConstants;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkstorageactual.mapper.MeterWorkStorageActualMapper;
import com.steerinfo.meterwork.meterworkstorageactual.model.MeterWorkStorageActual;
import com.steerinfo.meterwork.meterworkstoragecalibration.model.MeterWorkStorageCalibration;
import com.steerinfo.meterwork.meterworkstoragecalibration.mapper.MeterWorkStorageCalibrationMapper;
import com.steerinfo.meterwork.meterworkstoragecalibration.service.IMeterWorkStorageCalibrationService;
import com.steerinfo.pretrack.prestoragescale.model.PreStorageScale;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

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

    @Autowired
    private MeterWorkStorageCalibrationMapper meterWorkStorageCalibrationMapper;

    @Override
    protected IBaseMapper<MeterWorkStorageCalibration, String> getMapper() {
        return meterWorkStorageCalibrationMapper;
    }

    @Resource
    private ComBaseInfoMapper comBaseInfoMapper;

    @Resource
    private MeterWorkStorageActualMapper meterWorkStorageActualMapper;

    /**
     * 新增热送磅数据
     *
     * @param models
     * @return
     */
    @Override
    public String doAddWf(List<MeterWorkStorageCalibration> models) {
        String str = "";
        try {
            for (MeterWorkStorageCalibration calibration : models) {
                if (StringUtils.isEmpty(calibration.getCreateManName()) || StringUtils.isEmpty(calibration.getCreateManNo())) {
                    return "创建人及创建人编码不允许为空！";
                }
                if (StringUtils.isEmpty(calibration.getWgtNo())) {
                    return "计量实绩编号不允许为空！";
                }
            }
            for (MeterWorkStorageCalibration calibration : models) {
                //更新实绩表的数据
                MeterWorkStorageActual actual = new MeterWorkStorageActual();
                actual.setActualNo(calibration.getWgtNo());
                actual.setValueFlag(DbConstants.STORAGE_ACTUAL_VALUE_INVALID);
                actual.setHeatNo("888");
                actual.setPackageNo("888");
                actual.setBlockNum("888");
                actual.setGradeNo("1");
                actual.setGradeName("校秤");
                actual.setSpecNo("1");
                actual.setSpecName("校秤");
                meterWorkStorageActualMapper.updateByPrimaryKeySelective(actual);

                //新增较秤表的数据
                String maxID = comBaseInfoMapper.getConfigID("METER_WORK_STORAGE_CALIBRATION", "WGT_NO");
                calibration.setWgtNo(maxID);
                calibration.setCreateTime(new Date());
                meterWorkStorageCalibrationMapper.insert(calibration);
            }
        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return str;
    }

    /**
     * 修改热送磅数据
     *
     * @param models
     * @return
     */
    @Override
    public String doUpdateWf(List<MeterWorkStorageCalibration> models) {
        String str = "";
        try {
            for (MeterWorkStorageCalibration calibration : models) {
                if (StringUtils.isEmpty(calibration.getUpdateManName()) || StringUtils.isEmpty(calibration.getUpdateManNo())) {
                    return "修改人及修改人编码不允许为空！";
                }
            }
            for (MeterWorkStorageCalibration calibration : models) {
                calibration.setUpdateTime(new Date());
                meterWorkStorageCalibrationMapper.updateByPrimaryKeySelective(calibration);
            }
        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return str;
    }

    /**
     * 新增未生产的数据
     *
     * @param models
     * @return
     */
    @Override
    public String doAddNotProd(List<MeterWorkStorageCalibration> models) {
        String str = "";
        try {
            for (MeterWorkStorageCalibration calibration : models) {
                if (StringUtils.isEmpty(calibration.getCreateManName()) || StringUtils.isEmpty(calibration.getCreateManNo())) {
                    return "创建人及创建人编码不允许为空！";
                }
            }
            for (MeterWorkStorageCalibration calibration : models) {
                //新增较秤表的数据
                String maxID = comBaseInfoMapper.getConfigID("METER_WORK_STORAGE_CALIBRATION", "WGT_NO");
                calibration.setWgtNo(maxID);
                calibration.setCreateTime(new Date());
                meterWorkStorageCalibrationMapper.insertSelective(calibration);
            }
        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return str;
    }

    /**
     * 手动添加校秤的数据
     * @param models
     * @return
     */
    @Override
    public String doAddManual(List<MeterWorkStorageCalibration> models) {
        String str = "";
        try {
            for (MeterWorkStorageCalibration calibration : models) {
                if (StringUtils.isEmpty(calibration.getCreateManName()) || StringUtils.isEmpty(calibration.getCreateManNo())) {
                    return "创建人及创建人编码不允许为空！";
                }
            }
            for (MeterWorkStorageCalibration calibration : models) {
                String maxID = comBaseInfoMapper.getConfigID("METER_WORK_STORAGE_CALIBRATION", "WGT_NO");
                calibration.setWgtNo(maxID);
                calibration.setCreateTime(new Date());
                meterWorkStorageCalibrationMapper.insert(calibration);
            }
        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return str;
    }
}
