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

import com.jisco.dynamicweight.util.UserUtil;
import com.steerinfo.baseinfo.meterbaseoperationlog.mapper.MeterBaseOperationLogMapper;
import com.steerinfo.baseinfo.meterbaseoperationlog.model.MeterBaseOperationLog;
import com.steerinfo.meterwork.meterworkcaractual.mapper.MeterWorkCarActualMapper;
import com.steerinfo.meterwork.meterworkcaractual.model.MeterWorkCarActual;
import com.steerinfo.meterwork.meterworkcaractualbak.mapper.MeterWorkCarActualBakMapper;
import com.steerinfo.meterwork.meterworkcaractualbak.model.MeterWorkCarActualBak;
import com.steerinfo.meterwork.meterworkcaractualbak.service.IMeterWorkCarActualBakService;
import com.steerinfo.baseinfo.meterbasematterinfomatch.mapper.MeterBaseMatterInfoMatchMapper;
import com.steerinfo.baseinfo.meterbasematterinfomatch.model.MeterBaseMatterInfoMatch;
import com.steerinfo.baseinfo.meterbasetermtaredata.mapper.MeterBaseTermTareDataMapper;
import com.steerinfo.baseinfo.meterbasetermtaredata.model.MeterBaseTermTareData;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.meterwork.constants.DbConstants;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.metertolimsold.mapper.MeterToLimsOldMapper;
import com.steerinfo.meterwork.metertolimsold.model.MeterToLimsOld;
import com.steerinfo.meterwork.meterworkcaractualfirst.mapper.MeterWorkCarActualFirstMapper;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.meterwork.meterworkcaractualfirstbak.controller.MeterWorkCarActualFirstBakController;
import com.steerinfo.meterwork.meterworkcaractualfirstbak.mapper.MeterWorkCarActualFirstBakMapper;
import com.steerinfo.meterwork.meterworkcaractualfirstbak.model.MeterWorkCarActualFirstBak;
import com.steerinfo.meterwork.meterworkcaractualfirstbak.service.IMeterWorkCarActualFirstBakService;
import com.steerinfo.meterwork.meterworkimage.mapper.MeterWorkImageMapper;
import com.steerinfo.meterwork.meterworkimage.model.MeterWorkImage;
import com.steerinfo.meterwork.meterworkprelink.mapper.MeterWorkPreLinkMapper;
import com.steerinfo.meterwork.meterworkprelink.service.IMeterWorkPreLinkService;
import com.steerinfo.meterwork.meterworkrailwayactfirst.model.MeterWorkRailwayActFirst;
import com.steerinfo.meterwork.meterworkrailwayactual.mapper.MeterWorkRailwayActualMapper;
import com.steerinfo.meterwork.meterworkrailwayactual.model.MeterWorkRailwayActual;
import com.steerinfo.pretrack.pretracknotice.mapper.PreTrackNoticeMapper;
import com.steerinfo.pretrack.pretracknotice.model.PreTrackNotice;
import com.steerinfo.pretrack.pretrackscale.mapper.PreTrackScaleMapper;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.util.CommonPage;
import com.steerinfo.util.FieldsCollector;
import com.steerinfo.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

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

    @Resource
    private MeterWorkCarActualFirstBakMapper meterWorkCarActualFirstBakMapper;

    //新旧系统计量数据转换表
    @Resource
    private MeterToLimsOldMapper meterToLimsOldMapper;

    //物资匹配表
    @Resource
    private MeterBaseMatterInfoMatchMapper meterBaseMatterInfoMatchMapper;

    @Resource
    private MeterWorkImageMapper meterWorkImageMapper;
    //预环节数据
    @Resource
    private MeterWorkPreLinkMapper meterWorkPreLinkMapper;

    @Autowired
    IMeterWorkPreLinkService meterWorkPreLinkService;

    //预报数据
    @Resource
    private PreTrackScaleMapper preTrackScaleMapper;

    //合同数据
    @Resource
    private PreTrackNoticeMapper preTrackNoticeMapper;

    //火车实绩数据
    @Resource
    private MeterWorkRailwayActualMapper meterWorkRailwayActualMapper;


    @Autowired
    IMeterWorkCarActualFirstBakService meterWorkCarActualFirstBakService;

    @Resource
    private MeterBaseTermTareDataMapper meterBaseTermTareDataMapper;

    @Resource
    private MeterWorkCarActualBakMapper meterWorkCarActualBakMapper;

    @Resource
    private MeterWorkCarActualMapper meterWorkCarActualMapper;

    @Resource
    private MeterWorkCarActualFirstMapper meterWorkCarActualFirstMapper;

    @Resource
    private MeterBaseOperationLogMapper meterBaseOperationLogMapper;

    private static Logger logger	= LoggerFactory.getLogger( MeterWorkCarActualFirstBakController.class );

    @Override
    protected IBaseMapper<MeterWorkCarActualBak, String> getMapper() {
        return meterWorkCarActualBakMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int checkandadd(MeterWorkCarActualBak model) {
        try {
            check(model);
            model.setUploadFlag("1");
            model.setCreateTime(new Date());
            if(model.getGrossTime().compareTo(model.getTareTime()) >= 0) {
                model.setNetSpot3No(model.getBaseSpot1No());
                model.setNetSpot3Name(model.getBaseSpot1Name());
                model.setNetManNo(model.getGrossManNo());
                model.setNetManName(model.getGrossManName());
                model.setNetTime(model.getGrossTime());
                model.setCreateTime(model.getGrossTime());
                model.setCreateManName(model.getGrossManName());
            }else {
                model.setNetSpot3No(model.getBaseSpot2No());
                model.setNetSpot3Name(model.getBaseSpot2Name());
                model.setNetManNo(model.getTareManNo());
                model.setNetManName(model.getTareManName());
                model.setNetTime(model.getTareTime());
                model.setCreateTime(model.getTareTime());
                model.setCreateManName(model.getTareManName());
            }
            int num = meterWorkCarActualBakMapper.insertSelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int audit(MeterWorkCarActualBak model) {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数为空！！");
            }
            check(model);
            model.setUploadFlag(DbConstants.VALID);
            int num = meterWorkCarActualBakMapper.updateByPrimaryKeySelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int unaudit(MeterWorkCarActualBak model) {
        try {
            check(model);
            model.setUploadFlag(DbConstants.INVALID);
            int num = meterWorkCarActualBakMapper.updateByPrimaryKeySelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int publish(MeterWorkCarActualBak model) {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数为空！！");
            }
            if (!model.getValueFlag().equals("1")) {
                throw new MarkerMetException(500, "只有未发布状态的数据才能进行发布数据操作");
            }
            if (model.getIsPreScale().equals("0")) {
                throw new MarkerMetException(500, "只有已匹配委托的数据才能进行发布数据操作");
            }
            check(model);
            model.setValueFlag("2"); // 状态改为发布
            model.setUploadFlag("1");
            int num = meterWorkCarActualBakMapper.updateByPrimaryKeySelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int revocation(MeterWorkCarActualBak model) {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数为空！！");
            }
            if (!model.getValueFlag().equals("2")) {
                throw new MarkerMetException(500, "只有发布状态的数据才能进行撤销发布操作");
            }
            check(model);
            model.setValueFlag("1"); // 状态改为未发布
            int num = meterWorkCarActualBakMapper.updateByPrimaryKeySelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int cancel(MeterWorkCarActualBak model) {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数为空！！");
            }
            check(model);
            model.setValueFlag("1"); // 状态改为发布
            int num = meterWorkCarActualBakMapper.updateByPrimaryKeySelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCar (MeterWorkCarActualBak model) {
        try {
            careditcheck(model);
            // 更新净重数据
            MeterWorkCarActualBak netData = new MeterWorkCarActualBak();
            netData.setActualNo(model.getActualNo());
            netData.setCarNo(model.getCarNo());
            netData.setNote(model.getNote());
            netData.setUpdateTime(new Date());
            netData.setUpdateManNo(model.getUpdateManNo());
            netData.setUpdateManName(model.getUpdateManName());
            netData.setUploadFlag("1");
            int num = meterWorkCarActualBakMapper.updateByPrimaryKeySelective(netData);

            // 更新一次计量数据
            MeterWorkCarActualBak queryData = meterWorkCarActualBakMapper.selectByPrimaryKey(netData.getActualNo());
            MeterWorkCarActualFirstBak first1 = new MeterWorkCarActualFirstBak();
            MeterWorkCarActualFirstBak first2 = new MeterWorkCarActualFirstBak();
            first1.setActualFirstNo(queryData.getActualFirst1No());
            first1.setCarNo(queryData.getCarNo());
            first1.setUpdateManName("system");
            first1.setUpdateTime(new Date());
            first1.setNote("未匹配委托净重数据修改车号，同步修改一次计量数据车号");
            meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(first1);

            first2.setActualFirstNo(queryData.getActualFirst1No());
            first2.setCarNo(queryData.getCarNo());
            first2.setUpdateManName("system");
            first2.setUpdateTime(new Date());
            first2.setNote("未匹配委托净重数据修改车号，同步修改一次计量数据车号");
            meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(first2);

            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    private void careditcheck (MeterWorkCarActualBak model) throws MarkerMetException {
        if (model.getActualNo() == null || "".equals(model.getActualNo())) {
            throw new MarkerMetException(500, "操作数据主键不能为空！！！");
        }
        if (!model.getValueFlag().equals("3")) {
            throw new MarkerMetException(500, "只有未匹配委托的数据才能修改车号！！！");
        }
        if (model.getNote() == null || "".equals(model.getNote())) {
            throw new MarkerMetException(500, "车号修改原因不能为空");
        }
        if (model.getCarNo() == null || model.getCarNo().equals("")) {
            throw new MarkerMetException(500, "车号不能为空");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        model.setUpdateManName(userName);
        model.setUpdateManNo(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkCarActualBak clean(String actualFirstNo, String dataNo, String predictionNo, String note) {
        try {
            if (actualFirstNo == null || "".equals(actualFirstNo)) {
                throw new MarkerMetException(500, "毛重数据编号为空");
            }
            if (dataNo == null || "".equals(dataNo)) {
                throw new MarkerMetException(500, "皮重数据编号为空");
            }
            MeterWorkCarActualFirstBak mwcaf1 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualFirstNo);
            if (mwcaf1 == null) {
                throw new MarkerMetException(500, "未查询到相应的毛重数据！！");
            }
            MeterWorkCarActualFirstBak mwcaf2 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(dataNo);
            if (mwcaf2 == null) {
                throw new MarkerMetException(500, "未查询到相应的皮重数据！！");
            }
            //判断皮毛重量是否符合要求
            if (mwcaf2.getMeterWeight().compareTo(mwcaf1.getMeterWeight()) == 1) {
                throw new MarkerMetException(500, "皮重重量大于毛重重量！！");
            }
            // 选中的是否为同一车的数据
            if (!mwcaf1.getCarNo().equals(mwcaf2.getCarNo())) {
                throw new MarkerMetException(500, "请选择同一车号的一次计量数据进行结净");
            }
            // 一次计量数据是否已经完成委托匹配
            if ((mwcaf1.getPredictionNo() != null && "".equals(mwcaf1.getPredictionNo())) || (mwcaf2.getPredictionNo() != null && "".equals(mwcaf2.getPredictionNo()))) {
                if (!mwcaf1.getPredictionNo().equals(mwcaf2.getPredictionNo())) {
                    throw new MarkerMetException(500, "选中的两条一次计量数据匹配的委托不一致，不允许进行结净操作！！！");
                } else if (predictionNo != null && !"".equals(predictionNo)) {
                    throw new MarkerMetException(500, "选中的两条一次计量数据已经匹配委托，请正确选择数据！！！");
                }
            }

            MeterWorkCarActualBak actualRow = new MeterWorkCarActualBak();
            if(StringUtils.isNotEmpty(predictionNo)){
                MeterWorkCarActualFirstBak first1 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualFirstNo, predictionNo);
                BeanUtils.copyProperties(first1,mwcaf1);
                MeterWorkCarActualFirstBak first2 = meterWorkCarActualFirstBakMapper.selectAndMatch(dataNo, predictionNo);
                BeanUtils.copyProperties(first2, mwcaf2);

                mwcaf1.setValueFlag(DbConstants.NET);
                mwcaf1.setUpdateManNo("system");
                mwcaf1.setUpdateManName("二次计量结净更新");
                mwcaf1.setUpdateTime(new Date());
                mwcaf1.setIsPreScale("1");
                mwcaf1.setPredictionNo(predictionNo);
                mwcaf1.setPredictionType("1");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf1);

                // 皮重数据处理
                mwcaf2.setValueFlag(DbConstants.NET);
                mwcaf2.setUpdateManNo("system");
                mwcaf2.setUpdateManName("二次计量结净更新");
                mwcaf2.setUpdateTime(new Date());
                mwcaf2.setPredictionNo(predictionNo);
                mwcaf2.setPredictionType("1");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf2);

                MeterWorkCarActualBak mwca1 = meterWorkCarActualBakMapper.manualAddPre(actualFirstNo, dataNo, predictionNo);
                BeanUtils.copyProperties(mwca1, actualRow);

//                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//                String no = meterWorkCarActualMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);

                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setValueFlag("1");
                actualRow.setNote(note);
                actualRow.setUploadFlag("1");
                actualRow.setIsPreScale("1");
                actualRow.setNetWeight(mwcaf1.getMeterWeight().subtract(mwcaf2.getMeterWeight()));
                actualRow.setCreateTime(new Date());
                if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                    actualRow.setNetManNo(actualRow.getGrossManNo());
                    actualRow.setNetManName(actualRow.getGrossManName());
                    actualRow.setNetTime(actualRow.getGrossTime());
                    actualRow.setCreateTime(actualRow.getGrossTime());
                    actualRow.setCreateManName(actualRow.getGrossManName());
                }else {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                    actualRow.setNetManNo(actualRow.getTareManNo());
                    actualRow.setNetManName(actualRow.getTareManName());
                    actualRow.setNetTime(actualRow.getTareTime());
                    actualRow.setCreateTime(actualRow.getTareTime());
                    actualRow.setCreateManName(actualRow.getTareManName());
                }
                meterWorkCarActualBakMapper.insertSelective(actualRow);

                PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(predictionNo);
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("二次计量结净更新");
                scale.setUploadFlag("1");
                scale.setMeasureStation(actualRow.getNetSpot3No());
                scale.setMeasureStationName(actualRow.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(scale);
            }else if(StringUtils.isNotEmpty(mwcaf1.getPredictionNo()) || StringUtils.isNotEmpty(mwcaf2.getPredictionNo())){
                String preNo = null;
                if (StringUtils.isEmpty(mwcaf1.getPredictionNo()) && StringUtils.isNotEmpty(mwcaf2.getPredictionNo())){
                    preNo = mwcaf2.getPredictionNo();
                }else if (StringUtils.isNotEmpty(mwcaf1.getPredictionNo())&& StringUtils.isEmpty(mwcaf2.getPredictionNo())){
                    preNo = mwcaf1.getPredictionNo();
                }else if(StringUtils.isNotEmpty(mwcaf1.getPredictionNo())&& StringUtils.isNotEmpty(mwcaf2.getPredictionNo())){
                    preNo = mwcaf1.getPredictionNo();
                }
                if(StringUtils.isNotEmpty(preNo)){
                    PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(preNo);
                    if(scale != null) {

                        MeterWorkCarActualFirstBak first1 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualFirstNo, scale.getPredictionNo());
                        BeanUtils.copyProperties(first1,mwcaf1);
                        MeterWorkCarActualFirstBak first2 = meterWorkCarActualFirstBakMapper.selectAndMatch(dataNo, scale.getPredictionNo());
                        BeanUtils.copyProperties(first2, mwcaf2);

                        mwcaf1.setValueFlag(DbConstants.NET);
                        mwcaf1.setUpdateManNo("system");
                        mwcaf1.setUpdateManName("二次计量结净更新");
                        mwcaf1.setUpdateTime(new Date());
                        mwcaf1.setIsPreScale("1");
                        mwcaf1.setPredictionNo(scale.getPredictionNo());
                        mwcaf1.setPredictionType("1");
                        meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf1);

                        // 皮重数据处理
                        mwcaf2.setValueFlag(DbConstants.NET);
                        mwcaf2.setUpdateManNo("system");
                        mwcaf2.setUpdateManName("二次计量结净更新");
                        mwcaf2.setUpdateTime(new Date());
                        mwcaf2.setIsPreScale("1");
                        mwcaf2.setPredictionNo(scale.getPredictionNo());
                        mwcaf2.setPredictionType("1");
                        meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf2);

                        MeterWorkCarActualBak mwca1 = meterWorkCarActualBakMapper.manualAddPre(actualFirstNo, dataNo, scale.getPredictionNo());
                        BeanUtils.copyProperties(mwca1, actualRow);

//                        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//                        String no = meterWorkCarActualMapper.getNewID(sdf.format(new Date()));
//                        actualRow.setActualNo(sdf.format(new Date()) + no);

                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);

                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setValueFlag("1");
                        actualRow.setNote(note);
                        actualRow.setUploadFlag("1");
                        actualRow.setPredictionNo(scale.getPredictionNo());
                        actualRow.setIsPreScale("1");
                        actualRow.setNetWeight(mwcaf1.getMeterWeight().subtract(mwcaf2.getMeterWeight()));
                        actualRow.setCreateTime(new Date());
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                        }else {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                            actualRow.setNetManNo(actualRow.getTareManNo());
                            actualRow.setNetManName(actualRow.getTareManName());
                            actualRow.setNetTime(actualRow.getTareTime());
                            actualRow.setCreateTime(actualRow.getTareTime());
                            actualRow.setCreateManName(actualRow.getTareManName());
                        }
                        meterWorkCarActualBakMapper.insertSelective(actualRow);

                        scale.setValueFlag(DbConstants.NET);
                        scale.setUpdateTime(new Date());
                        scale.setUpdateManNo("system");
                        scale.setUpdateManName("二次计量结净更新");
                        scale.setUploadFlag("1");
                        scale.setMeasureStation(actualRow.getNetSpot3No());
                        scale.setMeasureStationName(actualRow.getNetSpot3Name());
                        preTrackScaleMapper.updateByPrimaryKeySelective(scale);
                    }else {
                        throw new MarkerMetException(500, "委托表不存在该"+ preNo + "委托编号信息！！");
                    }
                }
            }else {

                MeterWorkCarActualFirstBak first1 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualFirstNo);
                MeterWorkCarActualFirstBak first2 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(dataNo);

                first1.setValueFlag(DbConstants.NET);
                first1.setUpdateManNo("system");
                first1.setUpdateManName("二次计量结净更新");
                first1.setUpdateTime(new Date());
                first1.setIsPreScale("0");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKey(first1);

                // 皮重数据处理
                first2.setValueFlag(DbConstants.NET);
                first2.setUpdateManNo("system");
                first2.setUpdateManName("二次计量结净更新");
                first2.setUpdateTime(new Date());
                first2.setIsPreScale("0");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKey(first2);

                MeterWorkCarActualBak mwca2 = meterWorkCarActualBakMapper.manualAdd(first1.getActualFirstNo(), first2.getActualFirstNo());
                BeanUtils.copyProperties(mwca2, actualRow);

//                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//                String no = meterWorkCarActualMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);

                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);

                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setValueFlag("1");
                actualRow.setNote(note);
                actualRow.setUploadFlag("1");
                actualRow.setIsPreScale("0");
                actualRow.setNetWeight(first1.getMeterWeight().subtract(first2.getMeterWeight()));
                actualRow.setCreateTime(new Date());
                if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                    actualRow.setNetManNo(actualRow.getGrossManNo());
                    actualRow.setNetManName(actualRow.getGrossManName());
                    actualRow.setNetTime(actualRow.getGrossTime());
                    actualRow.setCreateTime(actualRow.getGrossTime());
                    actualRow.setCreateManName(actualRow.getGrossManName());
                }else {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                    actualRow.setNetManNo(actualRow.getTareManNo());
                    actualRow.setNetManName(actualRow.getTareManName());
                    actualRow.setNetTime(actualRow.getTareTime());
                    actualRow.setCreateTime(actualRow.getTareTime());
                    actualRow.setCreateManName(actualRow.getTareManName());
                }
                meterWorkCarActualBakMapper.insertSelective(actualRow);
            }
            return actualRow;
        } catch (MarkerMetException e) {
            throw new MarkerMetException(500, "操作异常，请确认！！！");
        }
    }

    /**
     * 二次计量数据解除按钮
     *
     * @param models
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(List<MeterWorkCarActualBak> models) {
        try {
            int num = 0;
            if (models == null || models.size() <= 0) {
                throw new MarkerMetException(500, "参数为空！！！");
            }
            for(MeterWorkCarActualBak modelTemp: models) {
                if(modelTemp.getIsPreScale().equals("1")) {
                    throw new MarkerMetException(500, "请将结净数据解除委托之后在毛皮分解");
                }
                if(modelTemp.getPredictionNo() != null && StringUtils.isNotEmpty(modelTemp.getPredictionNo())){
                    PreTrackScale scaleTemp = preTrackScaleMapper.selectByPrimaryKey(modelTemp.getPredictionNo());
                    if(scaleTemp.getValueFlag().equals("9")) {
                        throw new MarkerMetException(500, "结净数据中包含已结算的委托!!!");

                    }
                    if(scaleTemp.getPredictionType().equals("5") || scaleTemp.getPredictionType().equals("6") || scaleTemp.getPredictionType().equals("7")){
                        throw new MarkerMetException(500, "结净数据中包含特殊委托匹配结净的数据!!!");
                    }
                }
            }

            for (MeterWorkCarActualBak model : models) {
                //毛重
                MeterWorkCarActualFirstBak mwcaf1 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(model.getActualFirst1No());
                if (mwcaf1 == null) {
                    throw new MarkerMetException(500, "未查询到相应的毛重数据！！");
                }
                // 清空毛重数据中的委托信息
                MeterWorkCarActualFirstBak mwcafg = meterWorkCarActualFirstBakMapper.selectAndRemove(model.getActualFirst1No());
                mwcafg.setValueFlag(DbConstants.VALID);
                mwcafg.setNote(model.getNote());
                mwcafg.setUpdateManNo("system");
                mwcafg.setUpdateManName("二次计量解除更新");
                mwcafg.setUpdateTime(new Date());
                mwcafg.setIsPreScale("0");
//                mwcafg.setDataSource("1");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcafg);
                //皮重
                MeterWorkCarActualFirstBak mwcaf2 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(model.getActualFirst2No());
                MeterBaseTermTareData mbttd = meterBaseTermTareDataMapper.selectByPrimaryKey(model.getActualFirst2No());
                if (mwcaf2 == null && mbttd == null) {
                    throw new MarkerMetException(500, "未查询到相应的皮重数据！！");
                }
                if (mwcaf2 != null) {
                    MeterWorkCarActualFirstBak mwcaft = meterWorkCarActualFirstBakMapper.selectAndRemove(mwcaf2.getActualFirstNo());
                    if ("2".equals(mwcaft.getWeightType())) {
                        mwcaft.setValueFlag(DbConstants.INVALID); //期限皮重置为作废
                        mwcaft.setUpdateManNo("system");
                        mwcaft.setUpdateManName("二次计量解除作废");
                        mwcaft.setIsPreScale("0");
//                        mwcaft.setDataSource("1");
                        mwcaft.setUpdateTime(new Date());
                    } else {
                        mwcaft.setValueFlag(DbConstants.VALID);
                        mwcaft.setUpdateManNo("system");
                        mwcaft.setUpdateManName("二次计量解除更新");
                        mwcaft.setNote(model.getNote());
                        mwcaft.setIsPreScale("0");
//                        mwcaft.setDataSource("1");
                        mwcaft.setUpdateTime(new Date());
                    }
                    meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaft);
                }
                //回退至预环节的前一状态
//                if (!model.getMeterTypeNo().equals("001006003")) {
//                    List<MeterWorkPreLink> mwpls = meterWorkPreLinkService.getNext(model.getPredictionNo(), DbConstants.CLEAN);
//                    for (MeterWorkPreLink mwpl : mwpls) {
//                        mwpl.setValueFlag("0");//未使用
//                        meterWorkPreLinkMapper.updateByPrimaryKey(mwpl);
//                    }
                //修改预报
                if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                    PreTrackScale scale = new PreTrackScale();
                    scale.setPredictionNo(model.getPredictionNo());
                    scale.setValueFlag("1");
                    scale.setUploadFlag("1");
                    scale.setUpdateTime(new Date());
                    scale.setUpdateManNo("system");
                    scale.setUpdateManName("计量结净数据解除操作");
                    preTrackScaleMapper.updateByPrimaryKeySelective(scale);
                }
//                }
                //meterWorkCarActualMapper.deleteByPrimaryKey(model.getActualNo());
                model.setUploadFlag("1");
                model.setValueFlag("0");
                model.setIsPreScale("0");
                meterWorkCarActualBakMapper.updateByPrimaryKey(model);
                num++;
            }

            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeButNotEntrust(List<MeterWorkCarActualBak> models) {
        try {
            int num = 0;
            if (models == null || models.size() <= 0) {
                throw new MarkerMetException(500, "参数为空！！！");
            }
            for (MeterWorkCarActualBak model : models) {
                //毛重
                MeterWorkCarActualFirstBak mwcaf1 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(model.getActualFirst1No());
                if (mwcaf1 == null) {
                    throw new MarkerMetException(500, "未查询到相应的毛重数据！！");
                }
                // 清空毛重数据中的委托信息
                mwcaf1.setValueFlag(DbConstants.VALID);
                mwcaf1.setNote(model.getNote());
                mwcaf1.setUpdateManNo("system");
                mwcaf1.setUpdateManName("二次计量解除更新");
                mwcaf1.setUpdateTime(new Date());
                meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf1);
                //皮重
                MeterWorkCarActualFirstBak mwcaf2 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(model.getActualFirst2No());
                MeterBaseTermTareData mbttd = meterBaseTermTareDataMapper.selectByPrimaryKey(model.getActualFirst2No());
                if (mwcaf2 == null && mbttd == null) {
                    throw new MarkerMetException(500, "未查询到相应的皮重数据！！");
                }
                if (mwcaf2 != null) {
                    if ("2".equals(mwcaf2.getWeightType())) {
                        mwcaf2.setValueFlag(DbConstants.INVALID); //期限皮重置为作废
                        mwcaf2.setUpdateManNo("system");
                        mwcaf2.setUpdateManName("二次计量解除作废");
                        mwcaf2.setUpdateTime(new Date());
                    } else {
                        mwcaf2.setValueFlag(DbConstants.VALID);
                        mwcaf2.setUpdateManNo("system");
                        mwcaf2.setUpdateManName("二次计量解除更新");
                        mwcaf2.setNote(model.getNote());
                        mwcaf2.setUpdateTime(new Date());
                    }
                    meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf2);
                }
                meterWorkCarActualBakMapper.deleteByPrimaryKey(model.getActualNo());
                //model.setUploadFlag("1");
                //model.setValueFlag("0");
                //meterWorkCarActualMapper.updateByPrimaryKey(model);
                num++;
            }
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    public RESTfulResult specialRemove(HashMap params) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        rm.setData(null);
        rm.setCode("500");

        String type = params.get("type") == null ? "" : params.get("type").toString();
        MeterWorkCarActualBak actualModel01 = meterWorkCarActualBakMapper.selectByPrimaryKey(params.get("actualNo01").toString());
        MeterWorkCarActualBak actualModel02 = meterWorkCarActualBakMapper.selectByPrimaryKey(params.get("actualNo02").toString());
        if(actualModel01 == null || actualModel02 == null){
            rm.setMessage("未找到结净计量数据，请确认！！！");
            return rm;
        }
        List<MeterWorkCarActualBak> models = new ArrayList<>();
        models.add(actualModel01);
        models.add(actualModel02);
        if(!models.get(0).getPredictionCombination().equals(models.get(1).getPredictionCombination())){
            rm.setMessage("必须选择两个预报组合号一致的结净数据!!!");
            return rm;
        }
        for(MeterWorkCarActualBak modelTemp: models) {
            PreTrackScale scaleTemp = preTrackScaleMapper.selectByPrimaryKey(modelTemp.getPredictionNo());
            if(scaleTemp.getValueFlag().equals("9")) {
                rm.setMessage("两个结净数据中包含已结算的委托!!!");
                return rm;
            }
        }
        switch(type){
            case "5":
            case "6":
            case "7": {
                if(type.equals("5")){
                    //类型为双委托类型
                    for (MeterWorkCarActualBak model: models) {
                        if(!model.getPredictionType().equals("5")) {
                            rm.setMessage("选中的计量数据当中有一条或多条不是双委托结净数据，请确认！！");
                            return rm;
                        }
                    }
                } else if(type.equals("6")){
                    //类型为混卸类型
                    for (MeterWorkCarActualBak model: models) {
                        if(!model.getPredictionType().equals("6")) {
                            rm.setMessage("选中的计量数据当中有一条或多条不是混卸委托结净数据，请确认！！");
                        }
                    }
                }else if(type.equals("7")){
                    //类型为混装类型
                    for (MeterWorkCarActualBak model: models) {
                        if(!model.getPredictionType().equals("7")) {
                            rm.setMessage("选中的计量数据当中有一条或多条不是混装委托结净数据，请确认！！");
                        }
                    }
                }
                for (MeterWorkCarActualBak model: models) {
                    // 改委托
                    if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                        removeScaleEl(model.getPredictionNo());
                    }
                    // 清空一次毛皮重委托信息
                    MeterWorkCarActualFirstBak firstGross = meterWorkCarActualFirstBakMapper.selectAndRemove(model.getActualFirst1No());
                    MeterWorkCarActualFirstBak firstTare = meterWorkCarActualFirstBakMapper.selectAndRemove(model.getActualFirst2No());
                    if(firstGross != null){
                        removeFirstEl(firstGross);
                        meterWorkCarActualFirstBakMapper.updateByPrimaryKey(firstGross);
                    }
                    if(firstTare != null){
                        removeFirstEl(firstTare);
                        meterWorkCarActualFirstBakMapper.updateByPrimaryKey(firstTare);
                    }
                    removeEl(model);
                    meterWorkCarActualBakMapper.updateByPrimaryKeySelective(model);
                }
                rm.setSucceed();
                rm.setMessage("操作成功！！");
                rm.setCode("200");
            }
        }
        return rm;
    }
    //还原委托
    public void removeScaleEl(String predictionNo){
        String userId;
        String userName;
        try{
            UserPayload payload = UserPayload.getCurrUser();
            userId = payload.getUserCode();
            userName = payload.getUserName();
        }catch(Exception e) {
            userId = "admin";
            userName = "admins";
        }
        PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(predictionNo);
        scale.setValueFlag("1");
        scale.setUploadFlag("1");
        scale.setUpdateTime(new Date());
        scale.setUpdateManNo(userId);
        scale.setUpdateManName(userName);
        preTrackScaleMapper.updateByPrimaryKeySelective(scale);
    }

    //还原一次
    public void removeFirstEl(MeterWorkCarActualFirstBak model) {
        String userId;
        String userName;
        try{
            UserPayload payload = UserPayload.getCurrUser();
            userId = payload.getUserCode();
            userName = payload.getUserName();
        }catch(Exception e) {
            userId = "admin";
            userName = "admins";
        }
        model.setValueFlag("0");
        model.setUpdateManNo(userId);
        model.setUpdateManName(userName);
        model.setUpdateTime(new Date());
        model.setIsPreScale("0");
    }

    //还原结净
    public void removeEl(MeterWorkCarActualBak model) {
        String userId;
        String userName;
        try{
            UserPayload payload = UserPayload.getCurrUser();
            userId = payload.getUserCode();
            userName = payload.getUserName();
        }catch(Exception e) {
            userId = "admin";
            userName = "admins";
        }
        model.setUpdateManNo(userId);
        model.setUpdateManName(userName);
        model.setUpdateTime(new Date());
        model.setUploadFlag("1");
        model.setIsPreScale("0");
        model.setValueFlag("0");
        model.setIsBoundList("0");
        model.setBoundListUrl("");
        model.setContractNo("");
        model.setIsBoundListDownloaded("0");
        model.setDocumentId("");
    }

    @Override
    public RESTfulResult commonRemove(HashMap params) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        rm.setData(null);
        rm.setCode("500");

        try{


            MeterWorkCarActualBak actualModel = meterWorkCarActualBakMapper.selectByPrimaryKey(params.get("actualNo").toString());
            if(actualModel == null){
                rm.setMessage("未找到结净计量数据，请确认！！！");
                return rm;
            }
            List<MeterWorkCarActualBak> models = new ArrayList<MeterWorkCarActualBak>();
            models.add(actualModel);

            for(MeterWorkCarActualBak modelTemp: models) {
                if(modelTemp.getPredictionNo() != null && StringUtils.isNotEmpty(modelTemp.getPredictionNo())){
                    PreTrackScale scaleTemp = preTrackScaleMapper.selectByPrimaryKey(modelTemp.getPredictionNo());
                    if(scaleTemp.getValueFlag().equals("9")) {
                        rm.setMessage("结净数据中包含已结算的委托!!!");
                        return rm;
                    }
//                    if(scaleTemp.getPredictionType().equals("5") || scaleTemp.getPredictionType().equals("6") || scaleTemp.getPredictionType().equals("7")){
//                        rm.setMessage("结净数据中包含特殊委托匹配结净的数据!!!");
//                        return rm;
//                    }
                }
            }
            for (MeterWorkCarActualBak model: models) {
                //循环每一个结净记录
                //委托不能为空
                if(StringUtils.isNotEmpty(model.getPredictionNo())){
                    // 改委托
                    if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                        PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(model.getPredictionNo());
                        scale.setValueFlag("1");
                        scale.setUploadFlag("1");
                        scale.setUpdateTime(new Date());
                        scale.setUpdateManNo("system");
                        scale.setUpdateManName("结净废除操作");
                        preTrackScaleMapper.updateByPrimaryKeySelective(scale);
                    }
                }
                // 清空一次毛重、皮重委托信息
                MeterWorkCarActualFirstBak firstGross;
                MeterWorkCarActualFirstBak firstTare;
                if(StringUtils.isNotEmpty(model.getActualFirst1No())&&StringUtils.isNotEmpty(model.getActualFirst2No())){
                    firstGross = meterWorkCarActualFirstBakMapper.selectAndRemove(model.getActualFirst1No());
                    firstTare =  meterWorkCarActualFirstBakMapper.selectAndRemove(model.getActualFirst2No());
                }else{
                    firstGross = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(model.getActualFirst1No());
                    firstTare = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(model.getActualFirst2No());
                }
                if(firstGross != null){
                    firstGross.setValueFlag(DbConstants.VALID);
                    firstGross.setUpdateManNo("system");
                    firstGross.setUpdateManName("结净废除更新");
                    firstGross.setNote(model.getNote());
                    firstGross.setUpdateTime(new Date());
                    firstGross.setIsPreScale("0");
                    firstGross.setValueFlag("0");
                    meterWorkCarActualFirstBakMapper.updateByPrimaryKey(firstGross);
                }
                if(firstTare != null){
                    firstTare.setValueFlag(DbConstants.VALID);
                    firstTare.setUpdateManNo("system");
                    firstTare.setUpdateManName("结净废除更新");
                    firstTare.setNote(model.getNote());
                    firstTare.setUpdateTime(new Date());
                    firstTare.setIsPreScale("0");
                    firstTare.setValueFlag("0");
                    meterWorkCarActualFirstBakMapper.updateByPrimaryKey(firstTare);
                }
                // 作废结净
                model.setUploadFlag("1");
                model.setValueFlag("0");
                meterWorkCarActualBakMapper.updateByPrimaryKeySelective(model);
                //删结净
                //meterWorkCarActualMapper.deleteByPrimaryKey(model.getActualNo());
            }
            rm.setSucceed();
            rm.setMessage("操作成功！！");
            rm.setCode("200");
        }catch(Exception e){
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int manualInput (MeterWorkCarActualBak model) {
        try {
            // 校验传入数据是否完整
            manualCheck(model);
            model.setCreateTime(new Date());
            if(model.getGrossTime().compareTo(model.getTareTime()) >= 0) {
                model.setNetSpot3No(model.getBaseSpot1No());
                model.setNetSpot3Name(model.getBaseSpot1Name());
                model.setNetManNo(model.getGrossManNo());
                model.setNetManName(model.getGrossManName());
                model.setNetTime(model.getGrossTime());
                model.setCreateTime(model.getGrossTime());
                model.setCreateManName(model.getGrossManName());
            }else {
                model.setNetSpot3No(model.getBaseSpot2No());
                model.setNetSpot3Name(model.getBaseSpot2Name());
                model.setNetManNo(model.getTareManNo());
                model.setNetManName(model.getTareManName());
                model.setNetTime(model.getTareTime());
                model.setCreateTime(model.getTareTime());
                model.setCreateManName(model.getTareManName());
            }
            int num = meterWorkCarActualBakMapper.insertSelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkCarActualBak clientClean (MeterWorkCarActualFirstBak first, MeterWorkCarActualBak net) {
        try {
            MeterWorkCarActualBak mwca = new MeterWorkCarActualBak();
            if ((net.getActualFirst1No() == null || "".equals(net.getActualFirst1No())) && (net.getActualFirst2No() == null || "".equals(net.getActualFirst2No()))) {
                throw new MarkerMetException(500, "一次计量数据编号为空!!");
            }
            // 判断传入净重数据中是否存在委托编号
            if (net.getPredictionNo() == null || "".equals(net.getPredictionNo())) {

            } else {

            }
            return mwca;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    public RESTfulResult firstAndEntrustById(String actualNo) {
        RESTfulResult rm = new RESTfulResult();
        rm.setMessage("查询失败！");
        rm.setFailed();

        List<String> actualNoList = new ArrayList<>();
        MeterWorkCarActualBak carActual = meterWorkCarActualBakMapper.selectByPrimaryKey(actualNo);
        if(carActual != null) {
            actualNoList.add(0, carActual.getActualFirst1No());
            actualNoList.add(1, carActual.getActualFirst2No());
            String entrust = carActual.getPredictionNo() == null ? "" : carActual.getPredictionNo();
            List<MeterWorkCarActualFirstBak> firstList = meterWorkCarActualFirstBakMapper.selectById(actualNoList);
            PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(entrust);
            HashMap<String, Object> map = new HashMap<>();
            map.put("firstList", firstList);
            map.put("scale", scale);
            rm.setData(map);
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            return rm;
        }
        return rm;
    }

    @Override
    public PageList<MeterWorkCarActualBak> likeByDesc(HashMap<String, Object> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkCarActualBak> rows = meterWorkCarActualBakMapper.likeByDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public RESTfulResult abolish(List<MeterWorkCarActualBak> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        int sum = 0;
        for(MeterWorkCarActualBak model: models){
            model.setValueFlag("0");
            int i = meterWorkCarActualBakMapper.updateByPrimaryKeySelective(model);
            if(i >= 1){
                sum++;
            }else{
                return rm;
            }
        }
        rm.setSucceed();
        rm.setMessage("操作成功！！");
        rm.setData(sum);
        rm.setCode("200");
        return rm;
    }

    @Override
    public RESTfulResult selectByMap(HashMap<String, Object> params) {
        RESTfulResult rm = new RESTfulResult();
        rm.setSucceed();
        rm.setData(null);
        rm.setMessage("");
        rm.setCode("200");
        params.remove("createTime");
        List<MeterWorkCarActualBak> models = meterWorkCarActualBakMapper.selectByMap(params);
        if(models.size() >= 1) {
            rm.setSucceed();
            rm.setMessage("操作成功！！");
            rm.setCode("200");
            rm.setData(models);
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult matchPrediction (String actualNo, String predictionNo, String note){
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        rm.setCode("500");
        try{
            if (StringUtils.isEmpty(actualNo)) {
                rm.setMessage("结净数据主键不能为空,请确认！！");
                return rm;
            }
            if (StringUtils.isEmpty(predictionNo)) {
                rm.setMessage("预报编号不能为空,请确认！！");
                return rm;
            }

            MeterWorkCarActualBak model = meterWorkCarActualBakMapper.selectByPrimaryKey(actualNo);
            PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(predictionNo);
            if(model == null) {
                rm.setMessage("不存在对应的结净数据,请确认！！");
                return rm;
            }else if(!model.getIsPreScale().equals("0")) {
                rm.setMessage("只有未匹配的净重数据才能进行委托匹配操作");
                return rm;
            }

            if(scale == null) {
                rm.setMessage("不存在对应的预报数据,请确认！！");
                return rm;
            }else {
                if(scale.getValueFlag().equals("2")) {
                    rm.setMessage("只有未使用的委托才能进行匹配委托操作，请确认！！");
                    return rm;
                }
                if (!model.getCarNo().equals(scale.getCarNo())) {
                    rm.setMessage("结净数据中车号和预报中车号不一致，请确认！！");
                    return rm;
                }
                if (!scale.getPredictionType().equals("1")) {
                    rm.setMessage("委托信息不是普通委托不能进行匹配委托，请确认！！");
                    return rm;
                }
            }
            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();
            /*String userId = "systems";
            String userName = "systems";*/

            // 处理一次计量数据
            // 毛重
            MeterWorkCarActualFirstBak grossrow = meterWorkCarActualFirstBakMapper.selectAndMatch(model.getActualFirst1No(), predictionNo);
            grossrow.setUpdateTime(new Date());
            grossrow.setUpdateManName(userName + ": 结净数据匹配委托操作");
            grossrow.setUpdateManNo(userId);
            grossrow.setIsPreScale("1");
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(grossrow);
            // 皮重
            MeterWorkCarActualFirstBak tarerow = meterWorkCarActualFirstBakMapper.selectAndMatch(model.getActualFirst2No(), predictionNo);
            tarerow.setUpdateTime(new Date());
            tarerow.setUpdateManName(userName + ": 结净数据匹配委托操作");
            tarerow.setUpdateManNo(userId);
            tarerow.setIsPreScale("1");
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(tarerow);

            MeterWorkCarActualBak mwca = meterWorkCarActualBakMapper.mathPredcition(actualNo, predictionNo);
            mwca.setIsPreScale("1");
            mwca.setPredictionType("1");
            mwca.setCreateManName(userName);
            mwca.setCreateTime(new Date());
            mwca.setValueFlag("1");
            mwca.setUploadFlag("1");
            mwca.setNote(note);
            meterWorkCarActualBakMapper.updateByPrimaryKey(mwca);

            // 处理预报
            if (scale != null) {
                scale.setValueFlag("2");
                scale.setUploadFlag("1");
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("计量结净数据匹配委托操作");
                scale.setMeasureStation(mwca.getNetSpot3No());
                scale.setMeasureStationName(mwca.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(scale);
            }

            rm.setSucceed();
            rm.setCode("200");
            rm.setData(mwca);
            rm.setMessage("操作成功！！");
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常！！");
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkCarActualBak cleanPrediction (MeterWorkCarActualBak model) throws MarkerMetException {
        if (model.getActualNo() == null || "".equals(model.getActualNo())) {
            throw new MarkerMetException(500, "结净数据主键不能为空");
        }
        if (!model.getValueFlag().equals("1") && model.getPredictionNo() != null && !"".equals(model.getPredictionNo())) {
            throw new MarkerMetException(500, "只有有效已匹配的结净数据才能进行解除操作");
        }
        MeterWorkCarActualBak mwcaquery = meterWorkCarActualBakMapper.selectByPrimaryKey(model.getActualNo());
        if (mwcaquery == null) {
            throw new MarkerMetException(500, "不存在对应的结净数据");
        }

        PreTrackScale scaleTemp = preTrackScaleMapper.selectByPrimaryKey(model.getPredictionNo());
        if(scaleTemp.getPredictionNo() != null && StringUtils.isNotEmpty(scaleTemp.getPredictionNo())) {
            if(scaleTemp.getValueFlag().equals("9")) {
                throw new MarkerMetException(500, "结净数据包含已结算的委托!!!");
            }
//            if(scaleTemp.getPredictionType().equals("5") || scaleTemp.getPredictionType().equals("6") || scaleTemp.getPredictionType().equals("7")){
//                throw new MarkerMetException(500, "结净数据中包含特殊委托匹配结净的数据!!!");
//            }
        }

        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();

        // 处理一次计量数据
        // 毛重
        MeterWorkCarActualFirstBak grossrow = meterWorkCarActualFirstBakMapper.selectAndRemove(model.getActualFirst1No());
//        grossrow.setValueFlag("3");
        grossrow.setUpdateTime(new Date());
        grossrow.setUpdateManName(userName + ": 结净数据解除委托操作");
        grossrow.setUpdateManNo(userId);
        grossrow.setIsPreScale("0");
        meterWorkCarActualFirstBakMapper.updateByPrimaryKey(grossrow);

        // 皮重
        MeterWorkCarActualFirstBak tarerow = meterWorkCarActualFirstBakMapper.selectAndRemove(model.getActualFirst2No());
//        tarerow.setValueFlag("3");
        tarerow.setUpdateTime(new Date());
        tarerow.setUpdateManName(userName + ": 结净数据解除委托操作");
        tarerow.setUpdateManNo(userId);
        tarerow.setIsPreScale("0");
        meterWorkCarActualFirstBakMapper.updateByPrimaryKey(tarerow);

        // 处理预报
        if (StringUtils.isNotEmpty(model.getPredictionNo())) {
            PreTrackScale scale = new PreTrackScale();
            scale.setPredictionNo(model.getPredictionNo());
            scale.setValueFlag("1");
            scale.setUploadFlag("1");
            scale.setUpdateTime(new Date());
            scale.setUpdateManNo("system");
            scale.setUpdateManName("计量结净数据解除委托操作");
            preTrackScaleMapper.updateByPrimaryKeySelective(scale);
        }
        MeterWorkCarActualBak mwca = meterWorkCarActualBakMapper.cleanPredcition(model.getActualNo());

        mwca.setUpdateManNo(userId);
        mwca.setUpdateManName(userName);
        mwca.setUpdateTime(new Date());
        //mwca.setValueFlag("3");
        mwca.setIsPreScale("0");
        mwca.setUploadFlag("1");
        meterWorkCarActualBakMapper.updateByPrimaryKey(mwca);
        return mwca;
    }
    /**
     * @param actualFirstNo 毛重数据
     * @param dataNo        皮重数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkCarActualBak termclean(String actualFirstNo, String dataNo) {
        try {
            if (actualFirstNo == null || "".equals(actualFirstNo)) {
                throw new MarkerMetException(500, "毛重数据编号为空");
            }
            if (dataNo == null || "".equals(dataNo)) {
                throw new MarkerMetException(500, "皮重数据编号为空");
            }
            MeterWorkCarActualFirstBak mwcaf1 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualFirstNo);
            if (mwcaf1 == null) {
                throw new MarkerMetException(500, "未查询到相应的毛重数据！！");
            }

            MeterBaseTermTareData mbttd = meterBaseTermTareDataMapper.selectByPrimaryKey(dataNo);
            if (mbttd == null) {
                throw new MarkerMetException(500, "未查询到相应的皮重数据！！");
            }
            if (mbttd.getMeterWeight().compareTo(mwcaf1.getMeterWeight()) == 1) {
                throw new MarkerMetException(500, "皮重重量大于毛重重量！！");
            }
            mwcaf1.setValueFlag(DbConstants.NET);
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf1);

            MeterWorkCarActualBak mwca = meterWorkCarActualBakMapper.tareAdd(actualFirstNo, dataNo);
            check(mwca);
            if (mwca.getGrossTime().compareTo(mwca.getTareTime()) == 1) {
                mwca.setNetSpot3No(mwca.getBaseSpot1No());
                mwca.setNetSpot3Name(mwca.getBaseSpot1Name());
                mwca.setNetTime(mwca.getGrossTime());
            } else {
                mwca.setNetSpot3No(mwca.getBaseSpot2No());
                mwca.setNetSpot3Name(mwca.getBaseSpot2Name());
                mwca.setNetTime(mwca.getTareTime());
            }
            mwca.setNetMode(DbConstants.TYPE);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            String no = meterWorkCarActualMapper.getNewID(sdf.format(new Date()));
//            mwca.setActualNo(sdf.format(new Date()) + no);

            String no = PublicMethod.IdCreate(3);
            mwca.setActualNo(no);

            mwca.setUploadFlag("1");
            mwca.setNetWeight(mwcaf1.getMeterWeight().subtract(mbttd.getMeterWeight()));
            //mwca.setCreateTime(new Date());
            if(mwca.getGrossTime().compareTo(mwca.getTareTime()) >= 0) {
                mwca.setNetSpot3No(mwca.getBaseSpot1No());
                mwca.setNetSpot3Name(mwca.getBaseSpot1Name());
                mwca.setNetManNo(mwca.getGrossManNo());
                mwca.setNetManName(mwca.getGrossManName());
                mwca.setNetTime(mwca.getGrossTime());
                mwca.setCreateTime(mwca.getGrossTime());
                mwca.setCreateManName(mwca.getGrossManName());
            }else {
                mwca.setNetSpot3No(mwca.getBaseSpot2No());
                mwca.setNetSpot3Name(mwca.getBaseSpot2Name());
                mwca.setNetManNo(mwca.getTareManNo());
                mwca.setNetManName(mwca.getTareManName());
                mwca.setNetTime(mwca.getTareTime());
                mwca.setCreateTime(mwca.getTareTime());
                mwca.setCreateManName(mwca.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(mwca);

            //修改预报
            if (StringUtils.isNotEmpty(mwca.getPredictionNo())
                    && mwca.getPredictionNo().length() > 10) {
                PreTrackScale scale = new PreTrackScale();
                scale.setPredictionNo(mwca.getPredictionNo());
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUploadFlag("1");
                scale.setUpdateManNo("system");
                scale.setUpdateManName("计量结净数据结净操作");
                scale.setMeasureStation(mwca.getNetSpot3No());
                scale.setMeasureStationName(mwca.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(scale);
            }
            return mwca;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageList<MeterWorkCarActualBak> selectView(Map<Objects, String> parmas, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkCarActualBak> rows = meterWorkCarActualBakMapper.selectView(parmas);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    private void check(MeterWorkCarActualBak model) throws MarkerMetException {
        if (model.getCarNo() == null || model.getCarNo().equals("")) {
            throw new MarkerMetException(500, "车号不能为空" + model.getCarNo());
        }
//        if (model.getMatterNo() == null || model.getMatterNo().equals("")) {
//            throw new MarkerMetException(500, "物料编号不能为空");
//        }
//        if (model.getMatterName() == null || model.getMatterName().equals("")) {
//            throw new MarkerMetException(500, "物料名称不能为空");
//        }
//        if (model.getMeterTypeNo() == null || model.getMeterTypeNo().equals("")) {
//            throw new MarkerMetException(500, "计量类型编号不能为空");
//        }
//        if (model.getMeterTypeName() == null || model.getMeterTypeName().equals("")) {
//            throw new MarkerMetException(500, "计量类型名称不能为空");
//        }
        if (model.getActualFirst1No() == null || model.getActualFirst1No().equals("")) {
            throw new MarkerMetException(500, "毛重计量作业编号不能为空");
        }
        if (model.getGrossTime() == null || model.getGrossTime().equals("")) {
            throw new MarkerMetException(500, "毛重计量时间不能为空");
        }
        if (model.getBaseSpot1No() == null || model.getBaseSpot1No().equals("")) {
            throw new MarkerMetException(500, "毛重计量点编号不能为空");
        }
        if (model.getBaseSpot1Name() == null || model.getBaseSpot1Name().equals("")) {
            throw new MarkerMetException(500, "毛重计量点名称不能为空");
        }
        if (model.getActualFirst2No() == null || "".equals(model.getActualFirst2No())) {
            throw new MarkerMetException(500, "皮重计量作业编号不能为空");
        }
        if (model.getTareTime() == null || model.getTareTime().equals("")) {
            throw new MarkerMetException(500, "皮重计量时间不能为空");
        }
        if (model.getBaseSpot2No() == null || model.getBaseSpot2No().equals("")) {
            throw new MarkerMetException(500, "皮重计量点编号不能为空");
        }
        if (model.getBaseSpot2Name() == null || model.getBaseSpot2Name().equals("")) {
            throw new MarkerMetException(500, "皮重计量点名称不能为空");
        }
        if (model.getActualNo() == null && model.getValueFlag() == null) {
            model.setValueFlag("1");//有效已匹配
        }
        if (model.getActualNo() == null && model.getPredictionNo() == null){
            model.setIsPreScale("0");
        }
        else {
            model.setIsPreScale("1");
        }

        if (model.getActualNo() == null && model.getUploadFlag() == null)
            model.setUploadFlag(DbConstants.INVALID);
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        model.setUploadFlag("1");
        if (model.getActualNo() == null) {
            model.setNetTime(new Date());
            model.setNetManNo(userId);
            model.setNetManName(userName);
        }
    }

    private void manualCheck(MeterWorkCarActualBak model) throws MarkerMetException {
        if (model.getCarNo() == null || model.getCarNo().equals("")) {
            throw new MarkerMetException(500, "车号不能为空" + model.getCarNo());
        }
        if (model.getMatterNo() == null || model.getMatterNo().equals("")) {
            throw new MarkerMetException(500, "物资编码不能为空" + model.getCarNo());
        }
        if (model.getMatterName() == null || model.getMatterName().equals("")) {
            throw new MarkerMetException(500, "物资名称不能为空" + model.getCarNo());
        }
        if (model.getForwardingUnitNo() == null || model.getForwardingUnitNo().equals("")) {
            throw new MarkerMetException(500, "发货单位编码不能为空" + model.getCarNo());
        }
        if (model.getForwardingUnitName() == null || model.getForwardingUnitName().equals("")) {
            throw new MarkerMetException(500, "发货单位名称不能为空" + model.getCarNo());
        }
        if (model.getReceivingUintNo() == null || model.getReceivingUintNo().equals("")) {
            throw new MarkerMetException(500, "收货单位编码不能为空" + model.getCarNo());
        }
        if (model.getReceivingUintName() == null || model.getReceivingUintName().equals("")) {
            throw new MarkerMetException(500, "收货单位名称不能为空" + model.getCarNo());
        }
        if (model.getGrossTime() == null || model.getGrossTime().equals("")) {
            throw new MarkerMetException(500, "毛重计量时间不能为空");
        }
        if (model.getTareTime() == null || model.getTareTime().equals("")) {
            throw new MarkerMetException(500, "皮重计量时间不能为空");
        }
        if (model.getActualNo() == null && model.getValueFlag() == null) {
            model.setValueFlag("4");//手工录入
        }

        if (model.getActualNo() == null && model.getUploadFlag() == null)
            model.setUploadFlag("2");
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        model.setUploadFlag("1");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        if (model.getActualNo() == null) {
            model.setNetTime(new Date());
            model.setNetManNo(userId);
            model.setNetManName(userName);
            model.setNetWeight(model.getGrossWeight().subtract(model.getTareWeight()));
//            model.setActualNo(sdf.format(new Date()) + meterWorkCarActualMapper.getNewID(sdf.format(new Date())));
            String no = PublicMethod.IdCreate(3);
            model.setActualNo(no);

        }
    }

    @Override
    public RESTfulResult doQueryWf(CommonPage<MeterWorkCarActualBak> oci) {
        RESTfulResult rm = new RESTfulResult();
        try {
            rm.setCode("200");
            if (oci.getParam() != null) {
                int iStart = oci.getPageSize() > 0 ? oci.getPageSize() : 9999;
                Map<String, Object> map = FieldsCollector.beanToMap(oci.getParam());
                PageHelper.startPage(oci.getPageNum(), iStart);
                List<MeterWorkCarActualBak> rows = meterWorkCarActualBakMapper.selectByParameters(map);
                PageList pageInfo = new PageList(rows);
                rm.setData(pageInfo);
            }
        } catch (Exception ex) {
            rm.setCode("500");
            rm.setMessage("查询失败：" + ex.getMessage());
        }
        return rm;
    }

    /***
     * 定期将汽车结净的数据转换到计量转换表中（已废弃）
     * @return
     */
    @Autowired
    DataSource dataSource;

    @Transactional
    public RESTfulResult dataConversionOfCar() {
        RESTfulResult rm = new RESTfulResult();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        String date = sdf.format(new Date()).substring(8, 10);
        try {
            Map<String, Object> map = new HashMap<>();
            List<MeterWorkCarActualBak> actualList = meterWorkCarActualBakMapper.selectDataConversionOfCar(map);
            if (actualList != null && actualList.size() > 0) {
                for (MeterWorkCarActualBak actual : actualList) {
                    //第一步判断是否已经作废，若已经作废则作废中间表的数据；
                    //第二步判断是否已经处理过了；处理了则进行update；
                    MeterToLimsOld old = new MeterToLimsOld();
                    old.setId(actual.getActualNo()); //主键
                    old.setXh(actual.getActualNo().substring(7)); //序号
                    old.setCpmc(actual.getMatterName() + "&&待处理"); //先默认一个值，下面if进行更新操作；
                    if (StringUtils.isNotEmpty(actual.getMatterName())) {
                        //物资名称进行转换1；老物资
                        MeterBaseMatterInfoMatch matterInfoMatch = meterBaseMatterInfoMatchMapper.selectByPrimaryKey(actual.getMatterName());
                        if (matterInfoMatch != null && matterInfoMatch.getMatterName() != null) {
                            old.setCpfl(matterInfoMatch.getMatterNameType()); //产品大类；此处需要进行转换
                            old.setCpmc(matterInfoMatch.getMatterName());
                        }
                        //物资名称进行转换2；新物资
                        if (matterInfoMatch == null) {
                            MeterBaseMatterInfoMatch infoMatch = new MeterBaseMatterInfoMatch();
                            infoMatch.setMatterNameJhy(actual.getMatterName());
                            infoMatch.setSpecName(actual.getSpecName());
                            Map<String, Object> mapMach = FieldsCollector.beanToMap(infoMatch);
                            List<MeterBaseMatterInfoMatch> matchList = meterBaseMatterInfoMatchMapper.selectByParameters(mapMach);
                            if (matchList != null && matchList.size() > 0 && matchList.get(0).getMatterName() != null) {
                                old.setCpfl(matchList.get(0).getMatterNameType()); //产品大类；此处需要进行转换
                                old.setCpmc(matchList.get(0).getMatterName());
                            }
                        }
                    }
                    old.setLy(actual.getForwardingUnitName()); //来源
                    old.setQx(actual.getReceivingUintName()); //去向
                    String strCarNo = actual.getCarNo().substring(0, 2) + "-" + actual.getCarNo().substring(2, actual.getCarNo().length());
                    old.setChh(strCarNo);//车号格式转换
                    map.put("carNo", actual.getCarNo());
                    map.put("startTime", sdf2.format(new Date()));
                    map.put("netTime", actual.getNetTime());
                    //判断车号在今天是否重复过磅，若未重复则将车号修改为 甘A-27592-17-1;车号17号第N次出现
                    List<MeterWorkCarActualBak> carList = meterWorkCarActualBakMapper.selectRepeatCarNO(map);
                    if (carList != null && carList.size() > 1) {
                        for (int i = 1; i <= carList.size(); i++) {
                            if (i == 1 && carList.size() == 2) {
                                //第二次出现这个车号的时候，把第一个车号进行更新
                                String strCarNo1 = carList.get(i - 1).getCarNo().substring(0, 2)
                                        + "-" + carList.get(i - 1).getCarNo().substring(2, carList.get(i - 1).getCarNo().length())
                                        + "-" + date + "-" + i;
                                MeterToLimsOld first = new MeterToLimsOld();
                                first.setId(carList.get(i - 1).getActualNo());
                                first.setChh(strCarNo1);
                                meterToLimsOldMapper.updateByPrimaryKeySelective(first);
                            }
                            if (i == carList.size()) {
                                old.setChh(strCarNo + "-" + date + "-" + i); //处理当前的车号
                            }
                        }
                    }
                    old.setRq(actual.getNetTime()); //过磅日期
                    old.setSj(actual.getNetTime()); //过磅时间
                    old.setPh(actual.getBatchNo()); //批次号
                    old.setMz(actual.getGrossWeight()); //毛重
                    old.setPz(actual.getTareWeight()); //皮重
                    old.setJz(actual.getNetWeight()); //净重
                    old.setJly(actual.getNetManName()); //计量员
                    old.setFjpz(actual.getAddWeight()); //附加重量
                    old.setNetSpot3Name(actual.getNetSpot3Name()); //净重计量点
                    old.setBaseSpot2Name(actual.getBaseSpot2Name()); //皮重计量点名称
                    old.setBaseSpot1Name(actual.getBaseSpot1Name()); //毛重计量点
                    old.setDeleteManName("正常");//用作标志位（0=有效；1=作废）
                    if (!actual.getValueFlag().equals("0")) {
                        old.setDeleteManName("作废");
                        old.setDeleteTime(new Date());
                    }
                    MeterToLimsOld limsOld = meterToLimsOldMapper.selectByPrimaryKey(actual.getActualNo());
                    if (limsOld != null && StringUtils.isNotEmpty(limsOld.getId())) {
                        meterToLimsOldMapper.updateByPrimaryKeySelective(old);

                        MeterWorkCarActualBak meterWorkCarActualBak = new MeterWorkCarActualBak();
                        meterWorkCarActualBak.setActualNo(actual.getActualNo());
                        meterWorkCarActualBak.setTbFlage("1");
                        meterWorkCarActualBakMapper.updateByPrimaryKeySelective(meterWorkCarActualBak);
                    } else {
                        meterToLimsOldMapper.insertSelective(old);

                        MeterWorkCarActualBak meterWorkCarActualBak = new MeterWorkCarActualBak();
                        meterWorkCarActualBak.setActualNo(actual.getActualNo());
                        meterWorkCarActualBak.setTbFlage("1");
                        meterWorkCarActualBakMapper.updateByPrimaryKeySelective(meterWorkCarActualBak);
                    }
                }
            }
            rm.setCode("200");
        } catch (Exception ex) {
            rm.setCode("500");
            rm.setMessage("数据处理失败：" + ex.getMessage());
        }
        //system.out.println("DATASOURCE = " + dataSource);
        return rm;
    }


    /**
     * 转换火车实绩到汽车实绩表中
     *
     * @param predictionNo 汽车预报编号
     * @param actualNo     火车实绩编号
     * @return
     */
    @Override
    public MeterWorkCarActualBak conversionTrainToCar(String predictionNo, String actualNo) {
        if (StringUtils.isEmpty(predictionNo) || StringUtils.isEmpty(actualNo)) {
            throw new MarkerMetException(500, "汽车预报编号、火车实绩号均不允许为空！");
        }
        //预报数据处理；
        PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(predictionNo);
        if (scale == null || StringUtils.isEmpty(scale.getPredictionNo())) {
            throw new MarkerMetException(500, "汽车预报编号[" + predictionNo + "]没有查询到预报数据！");
        }
        if (!"0".equals(scale.getValueFlag()) && !"1".equals(scale.getValueFlag())) {
            throw new MarkerMetException(500, "汽车预报编号不是未配车，或未使用状态，不允许进行转换操作！");
        }
        if (!"3".equals(scale.getPredictionType())) {
            throw new MarkerMetException(500, "汽车预报的类型不是【分检预报】，不允许进行转换操作！");
        }
        if (StringUtils.isEmpty(scale.getRailwayNo())) {
            throw new MarkerMetException(500, "汽车预报中的【火车车号】为空，不允许进行转换操作！");
        }

        //火车衡实绩数据
        MeterWorkRailwayActual railwayActual = meterWorkRailwayActualMapper.selectByPrimaryKey(actualNo);
        if (railwayActual == null || StringUtils.isEmpty(railwayActual.getActualNo())) {
            throw new MarkerMetException(500, "火车实绩编号[" + actualNo + "]没有查询到实绩数据！");
        }
        //判断火车重量是否小于等于0
        if (railwayActual.getNetWeight().compareTo(new BigDecimal(0)) < 1) {
            throw new MarkerMetException(500, "火车实绩的净重[" + railwayActual.getNetWeight() + "]小于等于0，不允许进行转换操作！");
        }
        if (!scale.getRailwayNo().equals(railwayActual.getRailwayNo())) {
            throw new MarkerMetException(500, "汽车预报中的火车车号【" + scale.getRailwayNo() + "】与火车实绩的车号【" + railwayActual.getRailwayNo() + "】不一致，不允许进行转换操作！");
        }

        MeterWorkCarActualBak carActual = new MeterWorkCarActualBak();
        //火车实绩转汽车实绩
        BeanUtils.copyProperties(railwayActual, carActual);

        //预报数据转汽车实绩
        BeanUtils.copyProperties(scale, carActual);

        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        if (StringUtils.isNotEmpty(userName)) {
            scale.setCreateManNo(userId);
            scale.setCreateManName(userName);

            carActual.setNetManNo(userId);
            carActual.setNetManName(userName);

            railwayActual.setUpdateManNo(userId);
            railwayActual.setUpdateManName(userName);
        }
        scale.setCreateTime(new Date());
        carActual.setNetTime(new Date());
        railwayActual.setUpdateTime(new Date());

        scale.setValueFlag(DbConstants.NET);
        scale.setUploadFlag("1");
        preTrackScaleMapper.updateByPrimaryKeySelective(scale);

//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//        String no = meterWorkCarActualMapper.getNewID(sdf.format(new Date()));
//        carActual.setActualNo(sdf.format(new Date()) + no);

        String no = PublicMethod.IdCreate(3);
        carActual.setActualNo(no);

        carActual.setActualFirst1No(railwayActual.getActualNo()); //汽车毛重编号，存火车实绩z编号
        carActual.setActualFirst2No(""); //皮重编号
        carActual.setUploadFlag("1");
        carActual.setCreateTime(new Date());
        if(carActual.getGrossTime().compareTo(carActual.getTareTime()) >= 0) {
            carActual.setNetSpot3No(carActual.getBaseSpot1No());
            carActual.setNetSpot3Name(carActual.getBaseSpot1Name());
            carActual.setNetManNo(carActual.getGrossManNo());
            carActual.setNetManName(carActual.getGrossManName());
            carActual.setNetTime(carActual.getGrossTime());
            carActual.setCreateTime(carActual.getGrossTime());
            carActual.setCreateManName(carActual.getGrossManName());
        }else {
            carActual.setNetSpot3No(carActual.getBaseSpot2No());
            carActual.setNetSpot3Name(carActual.getBaseSpot2Name());
            carActual.setNetManNo(carActual.getTareManNo());
            carActual.setNetManName(carActual.getTareManName());
            carActual.setNetTime(carActual.getTareTime());
            carActual.setCreateTime(carActual.getTareTime());
            carActual.setCreateManName(carActual.getTareManName());
        }
        meterWorkCarActualBakMapper.insert(carActual);

        railwayActual.setMemo(railwayActual.getMemo() + "汽车实绩转换，转换净重" + railwayActual.getNetWeight() + "");
        railwayActual.setNetWeight(new BigDecimal(0));
        railwayActual.setGrossWeight(railwayActual.getTareWeight());
        railwayActual.setUploadFlag("1");
        meterWorkRailwayActualMapper.updateByPrimaryKeySelective(railwayActual);

        return carActual;
    }

    /**
     * 按照合同号新增汽车结净数据：用于驻外检测
     *
     * @param actual
     * @return
     */
    @Override
    public MeterWorkCarActualBak insertByNotice(MeterWorkCarActualBak actual) {
        try {
            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();

            if (actual == null || StringUtils.isEmpty(actual.getNoticeNo())) {
                throw new MarkerMetException(500, "合同通知单号[" + actual.getNoticeNo() + "]不允许为空！");
            }
            if (actual == null || StringUtils.isEmpty(actual.getCarNo())) {
                throw new MarkerMetException(500, "驻外检测批次号[" + actual.getCarNo() + "]不允许为空！");
            }
            if (StringUtils.isEmpty(actual.getMeterTypeName()) || !actual.getMeterTypeName().contains("检测")) {
                throw new MarkerMetException(500, "业务类型[" + actual.getMatterName() + "]必须包含[驻外检测]或[到厂检测]！");
            }
            if (StringUtils.isEmpty(actual.getMeterTypeNo())) {
                throw new MarkerMetException(500, "业务类型编码[" + actual.getMatterNo() + "]不允许为空！");
            }
            if (actual.getNetWeight() == null || actual.getGrossWeight() == null || actual.getTareWeight() == null) {
                throw new MarkerMetException(500, "毛重、皮重、净重均不允许为空！");
            }
            if (actual.getGrossWeight().subtract(actual.getTareWeight()).compareTo(actual.getNetWeight()) != 0) {
                throw new MarkerMetException(500, "净重不等于毛重减皮重，请重新录入数据！");
            }

            PreTrackNotice trackNotice = preTrackNoticeMapper.selectByPrimaryKey(actual.getNoticeNo());
            if (trackNotice == null || StringUtils.isEmpty(trackNotice.getNoticeNo())) {
                throw new MarkerMetException(500, "合同通知单号[" + actual.getNoticeNo() + "]未查询到数据，请重新选择合同号！");
            }
            trackNotice.setMeterTypeNo(actual.getMeterTypeNo());//重新对业务类型赋值，因为合同中本来就带有业务类型；
            trackNotice.setMeterTypeName(actual.getMeterTypeName());

            PreTrackScale trackScale = new PreTrackScale();
            BeanUtils.copyProperties(trackNotice, trackScale);

            BeanUtils.copyProperties(trackNotice, actual); //相同的实体类进行转换
            actual.setNetTime(new Date());

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String predectionNo = preTrackScaleMapper.getNewID(sdf.format(new Date()));
            trackScale.setPredictionNo(sdf.format(new Date()) + predectionNo);
            trackScale.setCreateManNo(userId);
            trackScale.setCreateManName(userName);
            trackScale.setCreateTime(new Date());
            trackScale.setValueFlag(DbConstants.NET);
            trackScale.setMemo("驻外检测自动新增一条结净的预报");
            trackScale.setCarNo(actual.getCarNo());
            trackScale.setUploadFlag("1");
            preTrackScaleMapper.insertSelective(trackScale);

//            String no = meterWorkCarActualMapper.getNewID(sdf.format(new Date()));
//            actual.setActualNo(sdf.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            actual.setActualNo(no);

            actual.setActualFirst1No("Z" + actual.getActualNo());
            actual.setNetManNo(userId);
            actual.setNetManName(userName);
            actual.setPredictionNo(trackScale.getPredictionNo());
            actual.setValueFlag(DbConstants.INVALID);
            actual.setUploadFlag("1");
            actual.setCreateTime(new Date());
            if(actual.getGrossTime().compareTo(actual.getTareTime()) >= 0) {
                actual.setNetSpot3No(actual.getBaseSpot1No());
                actual.setNetSpot3Name(actual.getBaseSpot1Name());
                actual.setNetManNo(actual.getGrossManNo());
                actual.setNetManName(actual.getGrossManName());
                actual.setNetTime(actual.getGrossTime());
                actual.setCreateTime(actual.getGrossTime());
                actual.setCreateManName(actual.getGrossManName());
            }else {
                actual.setNetSpot3No(actual.getBaseSpot2No());
                actual.setNetSpot3Name(actual.getBaseSpot2Name());
                actual.setNetManNo(actual.getTareManNo());
                actual.setNetManName(actual.getTareManName());
                actual.setNetTime(actual.getTareTime());
                actual.setCreateTime(actual.getTareTime());
                actual.setCreateManName(actual.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(actual);
            return actual;
        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return null;
        }
    }


    /**
     * 按照合同号新增汽车结净数据：单包计量
     *
     * @param actual
     * @return
     */
    @Override
    public MeterWorkCarActualBak insertByScale(MeterWorkCarActualBak actual) {
        try {
            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();

            if (actual == null || StringUtils.isEmpty(actual.getPredictionNo())) {
                throw new MarkerMetException(500, "预报编号[" + actual.getPredictionNo() + "]不允许为空！");
            }
            if (actual == null || StringUtils.isEmpty(actual.getCarNo())) {
                throw new MarkerMetException(500, "单包计量批次号[" + actual.getCarNo() + "]不允许为空！");
            }
            if (StringUtils.isEmpty(actual.getMeterTypeName()) || !actual.getMeterTypeName().contains("单包计量")) {
                throw new MarkerMetException(500, "业务类型[" + actual.getMatterName() + "]必须包含[单包计量]！");
            }
            if (actual.getNetWeight() == null || actual.getGrossWeight() == null || actual.getTareWeight() == null) {
                throw new MarkerMetException(500, "毛重、皮重、净重均不允许为空！");
            }
            if (actual.getGrossWeight().subtract(actual.getTareWeight()).compareTo(actual.getNetWeight()) != 0) {
                throw new MarkerMetException(500, "净重不等于毛重减皮重，请重新录入数据！");
            }

            PreTrackScale trackScale = preTrackScaleMapper.selectByPrimaryKey(actual.getPredictionNo());
            if (trackScale == null || StringUtils.isEmpty(trackScale.getNoticeNo())) {
                throw new MarkerMetException(500, "预报编号[" + actual.getPredictionNo() + "]未查询到数据，请重新选择预报编号！");
            }
            if (!"0".equals(trackScale.getValueFlag()) && !"1".equals(trackScale.getValueFlag())) {
                throw new MarkerMetException(500, "汽车预报编号不是未配车，或未使用状态，不允许进行转换操作！");
            }
            if (StringUtils.isEmpty(trackScale.getMeterTypeName()) || !trackScale.getMeterTypeName().contains("单包计量")) {
                throw new MarkerMetException(500, "业务类型[" + trackScale.getMatterName() + "]必须包含[单包计量]！");
            }

            BeanUtils.copyProperties(trackScale, actual); //相同的实体类进行转换
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            String no = meterWorkCarActualMapper.getNewID(sdf.format(new Date()));
//            actual.setActualNo(sdf.format(new Date()) + no);

            String no = PublicMethod.IdCreate(3);
            actual.setActualNo(no);
            actual.setActualFirst1No("D" + actual.getActualNo());
            actual.setNetManNo(userId);
            actual.setNetManName(userName);
            actual.setNetTime(new Date());
            actual.setValueFlag(DbConstants.INVALID);
            actual.setUploadFlag("1");
            actual.setCreateTime(new Date());
            if(actual.getGrossTime().compareTo(actual.getTareTime()) >= 0) {
                actual.setNetSpot3No(actual.getBaseSpot1No());
                actual.setNetSpot3Name(actual.getBaseSpot1Name());
                actual.setNetManNo(actual.getGrossManNo());
                actual.setNetManName(actual.getGrossManName());
                actual.setNetTime(actual.getGrossTime());
                actual.setCreateTime(actual.getGrossTime());
                actual.setCreateManName(actual.getGrossManName());
            }else {
                actual.setNetSpot3No(actual.getBaseSpot2No());
                actual.setNetSpot3Name(actual.getBaseSpot2Name());
                actual.setNetManNo(actual.getTareManNo());
                actual.setNetManName(actual.getTareManName());
                actual.setNetTime(actual.getTareTime());
                actual.setCreateTime(actual.getTareTime());
                actual.setCreateManName(actual.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(actual);

            trackScale.setUpdateManNo(userId);
            trackScale.setUpdateManName(userName);
            trackScale.setUpdateTime(new Date());
            trackScale.setValueFlag(DbConstants.NET);
            trackScale.setUploadFlag("1");
            trackScale.setMeasureStation(actual.getNetSpot3No());
            trackScale.setMeasureStationName(actual.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(trackScale);

            return actual;
        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return null;
    }


    /**
     * 单包计量修改接口（全校验通过后，才进行修改）
     *
     * @param models
     * @return
     */
    @Override
    public int updateByDanBao(List<MeterWorkCarActualBak> models) {
        int num = 0;
        try {
            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();
            List<PreTrackScale> scaleList = new ArrayList<PreTrackScale>();
            for (MeterWorkCarActualBak model : models) {
                if (model == null || StringUtils.isEmpty(model.getPredictionNo())) {
                    throw new MarkerMetException(500, "[" + model.getCarNo() + "]的预报编号[" + model.getPredictionNo() + "]不允许为空！");
                }
                if (model == null || StringUtils.isEmpty(model.getActualNo())) {
                    throw new MarkerMetException(500, "[" + model.getCarNo() + "]的单包计量主键[" + model.getCarNo() + "]不允许为空！");
                }
                if (model.getNetWeight() == null || model.getGrossWeight() == null || model.getTareWeight() == null) {
                    throw new MarkerMetException(500, "[" + model.getCarNo() + "]的毛重、皮重、净重均不允许为空！");
                }
                if (model.getGrossWeight().subtract(model.getTareWeight()).compareTo(model.getNetWeight()) != 0) {
                    throw new MarkerMetException(500, "[" + model.getCarNo() + "]的净重不等于毛重减皮重，请重新录入数据！");
                }
                model.setUpdateManName(userName);
                model.setUpdateManNo(userId);
                model.setUpdateTime(new Date());

                PreTrackScale trackScale = new PreTrackScale();
                BeanUtils.copyProperties(model, trackScale);
                trackScale.setCreateTime(null);
                trackScale.setCreateManNo(null);
                trackScale.setCreateManName(null);
                if (StringUtils.isNotEmpty(model.getValueFlag()) && "1".equals(model.getValueFlag())) {
                    trackScale.setpValueFlag(trackScale.getValueFlag());
                    trackScale.setValueFlag("3");
                } else {
                    trackScale.setValueFlag("2");
                }
                trackScale.setUploadFlag("1");
                scaleList.add(trackScale);
                num++;
            }
            meterWorkCarActualBakMapper.batchUpdate(models);
            preTrackScaleMapper.batchUpdateSelective(scaleList);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            num=0;
        }
        return num;
    }

    /**
     * 驻外检测修改接口（全校验通过后，才进行修改）
     *
     * @param models
     * @return
     */
    @Override
    public int updateByZhuWai(List<MeterWorkCarActualBak> models) {
        int num = 0;
        try {
            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();
            for (MeterWorkCarActualBak model : models) {
                if (model == null || StringUtils.isEmpty(model.getPredictionNo())) {
                    throw new MarkerMetException(500, "[" + model.getCarNo() + "]的预报编号[" + model.getPredictionNo() + "]不允许为空！");
                }
                if (model == null || StringUtils.isEmpty(model.getActualNo())) {
                    throw new MarkerMetException(500, "[" + model.getCarNo() + "]的单包计量主键[" + model.getCarNo() + "]不允许为空！");
                }
                if (model.getNetWeight() == null || model.getGrossWeight() == null || model.getTareWeight() == null) {
                    throw new MarkerMetException(500, "[" + model.getCarNo() + "]的毛重、皮重、净重均不允许为空！");
                }
                if (model.getGrossWeight().subtract(model.getTareWeight()).compareTo(model.getNetWeight()) != 0) {
                    throw new MarkerMetException(500, "[" + model.getCarNo() + "]的净重不等于毛重减皮重，请重新录入数据！");
                }
                model.setUpdateManName(userName);
                model.setUpdateManNo(userId);
                model.setUpdateTime(new Date());

                num++;
            }
            meterWorkCarActualBakMapper.batchUpdate(models);

            return num;
        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            num=0;
        }
        return num;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> distinctCarNo(HashMap<String, Object> params) {
//        HashMap<String, Object> obj = new HashMap<>();
//        obj.put("carNo", carNo);
//        obj.put("startTime", startTime);
//        obj.put("endTime", endTime);
//        obj.put("dataSource", dataSource);
        List<String> list = meterWorkCarActualBakMapper.distinctCarNo(params).stream().map(MeterWorkCarActualBak::getCarNo).collect(Collectors.toList());
        return list;
    }

    @Override
    @Transactional
    public int checkandadditional(MeterWorkCarActualBak model) {
        try{
            int num = 0;
            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();
            if(model.getGrossWeight() == null || model.getGrossWeight().equals(""))
            {
                throw new MarkerMetException(500, "毛重数据不能为空");
            }
            if(model.getTareWeight() == null || model.getTareWeight().equals(""))
            {
                throw new MarkerMetException(500,"皮重数据不能为空");
            }
            if(model.getCarNo() == null || model.getCarNo().equals(""))
            {
                throw new MarkerMetException(500, "车号不能为空");
            }
            if(new BigDecimal(model.getGrossWeight().toString()).compareTo(new BigDecimal(model.getTareWeight().toString())) == -1)
            {
                throw new MarkerMetException(500, "毛重重量小于皮重");
            }
            if(model.getNetWeight() == null || model.getNetWeight().equals(""))
            {
                throw new MarkerMetException(500, "净重数据不能为空");
            }
            String predictionNo = model.getPredictionNo()==null?"":model.getPredictionNo();
            MeterWorkCarActualFirstBak mwcaf1 = new MeterWorkCarActualFirstBak();
            MeterWorkCarActualFirstBak mwcaf2 = new MeterWorkCarActualFirstBak();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            if(predictionNo == "" || "".equals(predictionNo)){
                //添加毛重一次计量数据
//                String actualFirstNo = sdf.format(new Date()) + meterWorkCarActualFirstBakMapper.getNewID(afl);
                String actualFirstNo = PublicMethod.IdCreate(3);
                mwcaf1.setActualFirstNo(actualFirstNo);
                mwcaf1.setValueFlag("2");
                mwcaf1.setCreateTime(new Date());
                mwcaf1.setCreateManNo(userId);
                mwcaf1.setCreateManName(userName);
                mwcaf1.setCarNo(model.getCarNo());
                mwcaf1.setMeterWeight(model.getGrossWeight());
                mwcaf1.setDataSource("4");
                mwcaf1.setMeterMode("3");
                mwcaf1.setIsPreScale("0");
                meterWorkCarActualFirstBakMapper.insertSelective(mwcaf1);
                //添加皮重一次计量数据
//                String dataNo = sdf.format(new Date()) + meterWorkCarActualFirstBakMapper.getNewID(afl);
//                mwcaf2.setActualFirstNo(dataNo);
                String dataNo = PublicMethod.IdCreate(3);
                mwcaf2.setActualFirstNo(dataNo);
                mwcaf2.setValueFlag("2");
                mwcaf2.setCreateTime(new Date());
                mwcaf2.setCreateManNo(userId);
                mwcaf2.setCreateManName(userName);
                mwcaf2.setCarNo(model.getCarNo());
                mwcaf2.setMeterWeight(model.getTareWeight());
                mwcaf2.setDataSource("4");
                mwcaf2.setMeterMode("3");
                mwcaf2.setIsPreScale("0");
                meterWorkCarActualFirstBakMapper.insertSelective(mwcaf2);
                //添加净重重一次计量数据
//                String ActualNo = sdf.format(new Date()) + meterWorkCarActualMapper.getNewID(afl);
//                model.setActualNo(ActualNo);

                String ActualNo = PublicMethod.IdCreate(3);
                model.setActualNo(ActualNo);
                model.setValueFlag("2");
                model.setActualFirst1No(actualFirstNo);
                model.setGrossManNo(userId);
                model.setGrossManName(userName);
                model.setGrossTime(mwcaf1.getCreateTime());
                model.setGrossMode("3");
                model.setActualFirst2No(dataNo);
                model.setTareManNo(userId);
                model.setTareManName(userName);
                model.setTareTime(mwcaf2.getCreateTime());
                model.setTareMode("3");
                model.setNetManNo(userId);
                model.setNetManName(userName);
                model.setNetTime(new Date());
                model.setNetMode("3");
                model.setDataSource("4");
                model.setIsPreScale("0");
                model.setUploadFlag("1");
                model.setCreateTime(new Date());
                if(model.getGrossTime().compareTo(model.getTareTime()) >= 0) {
                    model.setNetSpot3No(model.getBaseSpot1No());
                    model.setNetSpot3Name(model.getBaseSpot1Name());
                    model.setNetManNo(model.getGrossManNo());
                    model.setNetManName(model.getGrossManName());
                    model.setNetTime(model.getGrossTime());
                    model.setCreateTime(model.getGrossTime());
                    model.setCreateManName(model.getGrossManName());
                }else {
                    model.setNetSpot3No(model.getBaseSpot2No());
                    model.setNetSpot3Name(model.getBaseSpot2Name());
                    model.setNetManNo(model.getTareManNo());
                    model.setNetManName(model.getTareManName());
                    model.setNetTime(model.getTareTime());
                    model.setCreateTime(model.getTareTime());
                    model.setCreateManName(model.getTareManName());
                }
                meterWorkCarActualBakMapper.insertSelective(model);
                num++;
            }
            else {
                PreTrackScale preTrackScale = preTrackScaleMapper.selectBypredictionNo(predictionNo);
                //添加毛重一次计量数据
//                String actualFirstNo = sdf.format(new Date()) + meterWorkCarActualFirstBakMapper.getNewID(afl);
                String actualFirstNo = PublicMethod.IdCreate(3);
                BeanUtils.copyProperties(preTrackScale,mwcaf1);
                mwcaf1.setActualFirstNo(actualFirstNo);
                mwcaf1.setIsPreScale("1");
                mwcaf1.setValueFlag("2");
                mwcaf1.setCreateTime(new Date());
                mwcaf1.setCreateManNo(userId);
                mwcaf1.setCreateManName(userName);
                mwcaf1.setCarNo(model.getCarNo());
                mwcaf1.setMeterWeight(model.getGrossWeight());
                mwcaf1.setDataSource("4");
                mwcaf1.setMeterMode("3");
                meterWorkCarActualFirstBakMapper.insertSelective(mwcaf1);
                //添加皮重一次计量数据
//                String dataNo = sdf.format(new Date()) + meterWorkCarActualFirstBakMapper.getNewID(afl);
                String dataNo = PublicMethod.IdCreate(3);
                BeanUtils.copyProperties(preTrackScale, mwcaf2,predictionNo);
                mwcaf2.setActualFirstNo(dataNo);
                mwcaf2.setIsPreScale("1");
                mwcaf2.setValueFlag("2");
                mwcaf2.setCreateTime(new Date());
                mwcaf2.setCreateManNo(userId);
                mwcaf2.setCreateManName(userName);
                mwcaf2.setCarNo(model.getCarNo());
                mwcaf2.setMeterWeight(model.getTareWeight());
                mwcaf2.setDataSource("4");
                mwcaf2.setMeterMode("3");
                meterWorkCarActualFirstBakMapper.insertSelective(mwcaf2);
                //添加净重重一次计量数据
//                String ActualNo = sdf.format(new Date()) + meterWorkCarActualMapper.getNewID(afl);
                String ActualNo = PublicMethod.IdCreate(3);
                BeanUtils.copyProperties(preTrackScale, model);
                model.setActualNo(ActualNo);
                model.setIsPreScale("1");
                model.setValueFlag("2");
                model.setActualFirst1No(actualFirstNo);
                model.setGrossManNo(userId);
                model.setGrossManName(userName);
                model.setGrossTime(mwcaf1.getCreateTime());
                model.setGrossMode("3");
                model.setActualFirst2No(dataNo);
                model.setTareManNo(userId);
                model.setTareManName(userName);
                model.setTareTime(mwcaf2.getCreateTime());
                model.setTareMode("3");
                model.setNetManNo(userId);
                model.setNetManName(userName);
                model.setNetTime(new Date());
                model.setNetMode("3");
                model.setDataSource("4");
                model.setUploadFlag("1");
                model.setCreateTime(new Date());
                if(model.getGrossTime().compareTo(model.getTareTime()) >= 0) {
                    model.setNetSpot3No(model.getBaseSpot1No());
                    model.setNetSpot3Name(model.getBaseSpot1Name());
                    model.setNetManNo(model.getGrossManNo());
                    model.setNetManName(model.getGrossManName());
                    model.setNetTime(model.getGrossTime());
                    model.setCreateTime(model.getGrossTime());
                    model.setCreateManName(model.getGrossManName());
                }else {
                    model.setNetSpot3No(model.getBaseSpot2No());
                    model.setNetSpot3Name(model.getBaseSpot2Name());
                    model.setNetManNo(model.getTareManNo());
                    model.setNetManName(model.getTareManName());
                    model.setNetTime(model.getTareTime());
                    model.setCreateTime(model.getTareTime());
                    model.setCreateManName(model.getTareManName());
                }
                meterWorkCarActualBakMapper.insertSelective(model);

                preTrackScale.setValueFlag("2");
                preTrackScale.setUploadFlag("1");
                preTrackScale.setUpdateTime(new Date());
                preTrackScale.setUpdateManNo("system");
                preTrackScale.setUpdateManName("计量结净数据手动录入");
                preTrackScale.setMeasureStation(model.getNetSpot3No());
                preTrackScale.setMeasureStationName(model.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(preTrackScale);
                num++;
            }
            return num;
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return -1;
        }
    }

    @Override
    public RESTfulResult supplementaryData(MeterWorkCarActualBak model) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            MeterWorkCarActualFirstBak mwcaf1 = new MeterWorkCarActualFirstBak();
            MeterWorkCarActualFirstBak mwcaf2 = new MeterWorkCarActualFirstBak();
            MeterWorkCarActualBak actualRow = new MeterWorkCarActualBak();
            String predictionNo = model.getPredictionNo();

            /*UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();*/

            //毛重
//            String actualFirstNo = sdf.format(new Date()) + meterWorkCarActualFirstBakMapper.getNewID(afl);
            String actualFirstNo = PublicMethod.IdCreate(3);
            mwcaf1.setActualFirstNo(actualFirstNo);
            mwcaf1.setCarNo(model.getCarNo());
            mwcaf1.setMeterWeight(model.getGrossWeight());
            mwcaf1.setWeightType("0");
            mwcaf1.setCreateManNo(model.getGrossManNo());
            mwcaf1.setCreateManName(model.getGrossManName());
            mwcaf1.setCreateTime(model.getGrossTime());
            mwcaf1.setBaseSpotNo(model.getBaseSpot1No());
            mwcaf1.setBaseSpotName(model.getBaseSpot1Name());
            mwcaf1.setValueFlag("2");
            mwcaf1.setCheckFlag("0");
            mwcaf1.setDataSource("4");
            mwcaf1.setMeterMode("3");
            mwcaf1.setIsPreScale("0");
            meterWorkCarActualFirstBakMapper.insertSelective(mwcaf1);
            //皮重
//            String actualSecondNo = sdf.format(new Date()) + meterWorkCarActualFirstBakMapper.getNewID(afl);
            String actualSecondNo = PublicMethod.IdCreate(3);
            mwcaf2.setActualFirstNo(actualSecondNo);
            mwcaf2.setCarNo(model.getCarNo());
            mwcaf2.setMeterWeight(model.getTareWeight());
            mwcaf2.setWeightType("1");
            mwcaf2.setCreateManNo(model.getGrossManNo());
            mwcaf2.setCreateManName(model.getGrossManName());
            mwcaf2.setCreateTime(model.getTareTime());
            mwcaf2.setBaseSpotNo(model.getBaseSpot2No());
            mwcaf2.setBaseSpotName(model.getBaseSpot2Name());
            mwcaf2.setValueFlag("2");
            mwcaf2.setCheckFlag("0");
            mwcaf2.setDataSource("4");
            mwcaf2.setMeterMode("3");
            mwcaf2.setIsPreScale("0");
            meterWorkCarActualFirstBakMapper.insertSelective(mwcaf2);
            //净重
            if(StringUtils.isNotEmpty(predictionNo)){
                //将委托中的编号和收发货的等数据拷贝进一次数据中
                MeterWorkCarActualFirstBak first1 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualFirstNo, predictionNo);
                BeanUtils.copyProperties(first1,mwcaf1);
                mwcaf1.setIsPreScale("1");
                mwcaf1.setPredictionNo(predictionNo);
                meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf1);
                MeterWorkCarActualFirstBak first2 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualSecondNo, predictionNo);
                BeanUtils.copyProperties(first2, mwcaf2);
                mwcaf2.setIsPreScale("1");
                mwcaf2.setPredictionNo(predictionNo);
                meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf2);

                //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                MeterWorkCarActualBak mwca1 = meterWorkCarActualBakMapper.manualAddPre(actualFirstNo, actualSecondNo, predictionNo);
                BeanUtils.copyProperties(mwca1, actualRow);
                //更新预报数据
                PreTrackScale scale = new PreTrackScale();
                scale.setPredictionNo(predictionNo);
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("手动录入");
                scale.setUploadFlag("1");
                scale.setValueFlag("2");
                scale.setMeasureStation(actualRow.getNetSpot3No());
                scale.setMeasureStationName(actualRow.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(scale);

                actualRow.setIsPreScale("1");
                actualRow.setPredictionNo(predictionNo);
            }else {
                //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                MeterWorkCarActualBak mwca1 = meterWorkCarActualBakMapper.manualAdd(actualFirstNo, actualSecondNo);
                BeanUtils.copyProperties(mwca1, actualRow);

                mwcaf1.setIsPreScale("0");
                mwcaf2.setIsPreScale("0");
                actualRow.setIsPreScale("0");
            }
//            String no = meterWorkCarActualMapper.getNewID(sdf.format(new Date()));
//            actualRow.setActualNo(sdf.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            actualRow.setActualNo(no);
            actualRow.setNetTime(model.getNetTime());
            actualRow.setNetMode(DbConstants.TYPE);
            actualRow.setNote("手动录入");
            actualRow.setUploadFlag("1");
            actualRow.setValueFlag("1");
            actualRow.setGrossMode("3");
            actualRow.setTareMode("3");
            actualRow.setCreateManName(model.getGrossManName());
            actualRow.setCreateTime(new Date());
            actualRow.setNetWeight(mwcaf1.getMeterWeight().subtract(mwcaf2.getMeterWeight()));
            if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                actualRow.setNetManNo(actualRow.getGrossManNo());
                actualRow.setNetManName(actualRow.getGrossManName());
                actualRow.setNetTime(actualRow.getGrossTime());
                actualRow.setCreateTime(actualRow.getGrossTime());
                actualRow.setCreateManName(actualRow.getGrossManName());
            }else {
                actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                actualRow.setNetManNo(actualRow.getTareManNo());
                actualRow.setNetManName(actualRow.getTareManName());
                actualRow.setNetTime(actualRow.getTareTime());
                actualRow.setCreateTime(actualRow.getTareTime());
                actualRow.setCreateManName(actualRow.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(actualRow);
            rm.setSucceed();
            rm.setMessage("操作成功！！！");
            rm.setCode("200");
            return rm;
        }catch(Exception e){
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！！");
            return rm;
        }
    }

    @Override
    public RESTfulResult findPictureByActualNo(String actualNo) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            Assert.isTrue(!(actualNo == null || StringUtils.isEmpty(actualNo)), "结净编号不能为空");
            MeterWorkCarActualBak model = meterWorkCarActualBakMapper.selectByPrimaryKey(actualNo);
            Assert.isTrue(!Objects.isNull(model), "未查询到结净数据");
            List<String> firstNoList = new ArrayList<>();
            firstNoList.add(model.getActualFirst1No());
            firstNoList.add(model.getActualFirst2No());
            List<String> imageList = new ArrayList<>();
            for (String s : firstNoList) {
                MeterWorkImage imageModel = meterWorkImageMapper.selectByPrimaryKey("C" + s);
                if(Objects.isNull(imageModel)) continue;
                imageList.add(imageModel.getImageFile1());
                imageList.add(imageModel.getImageFile2());
                imageList.add(imageModel.getImageFile3());
                imageList.add(imageModel.getImageFile4());
                imageList.add(imageModel.getImageFile5());
                imageList.add(imageModel.getImageFile6());
                imageList.add(imageModel.getImageFile7());
                imageList.add(imageModel.getImageFile8());
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功");
            rm.setData(imageList);
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常:>>>" + e.getMessage());
        }
        return rm;
    }


    //内倒
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void ndCopyToActual(List<String> ids,String carNo) throws Exception {

        for (String id : ids){
            //根据id获取对应的一次数据
            MeterWorkCarActualBak meterWorkCarActualBak = meterWorkCarActualBakMapper.selectByPrimaryKey(id);

            //一次数据为空
            if (meterWorkCarActualBak == null) {
                //抛出异常
                //事务回滚
                throw new Exception("结净数据为空");
            }

            if (!carNo.equals(meterWorkCarActualBak.getCarNo())){
                //抛出异常
                //事务回滚
                throw new Exception(id+"的车号不一致");
            }


            //判断是否符合要求
            String note = isSuit(meterWorkCarActualBak,0);
            if (note.equals("1")){
                //复制到First正式表里面
                MeterWorkCarActual actual = new MeterWorkCarActual();
                //复制一份
                BeanUtils.copyProperties(meterWorkCarActualBak,actual);
                //修改主键
                actual.setActualFirst1No(actual.getActualFirst1No() + "bak");
                actual.setActualFirst2No(actual.getActualFirst2No() + "bak");
                actual.setActualNo(actual.getActualNo()+"bak");

                MeterWorkCarActual tempActual = meterWorkCarActualMapper.selectByPrimaryKey(actual.getActualNo());
                if(tempActual != null) {
                    throw new Exception("结净数据已转成正式数据，不能重复转正式数据");
                }
                MeterWorkCarActualFirst tempGrossFirst = meterWorkCarActualFirstMapper.selectByPrimaryKey(actual.getActualFirst1No());
                if(tempActual != null) {
                    throw new Exception("一次毛重数据已转成正式数据，不能重复转正式数据");
                }
                MeterWorkCarActualFirst tempTareFirst = meterWorkCarActualFirstMapper.selectByPrimaryKey(actual.getActualFirst2No());
                if(tempActual != null) {
                    throw new Exception("一次皮重数据已转成正式数据，不能重复转正式数据");
                }

                meterWorkCarActualMapper.insert(actual);

                try {
                    //新增日志
                    MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                    logModel.setId(PublicMethod.IdCreate(2));
                    logModel.setOperationCarnNo(actual.getCarNo());
                    logModel.setCreateManNo(UserUtil.getCurrentUserId());
                    logModel.setCreateManName(UserUtil.getCurrentUserName());
                    logModel.setCreateTime(new Date());
                    logModel.setOperationPage("web端-汽车衡净重数据维护");
                    logModel.setOperationFunction("新增");
                    logModel.setOperationId(actual.getActualNo());
                    logModel.setOperationPredictionNo(actual.getPredictionNo());
                    meterBaseOperationLogMapper.insertSelective(logModel);
                }catch (Exception e){
                    logger.error("web端-汽车衡净重数据维护日志新增失败");
                    e.printStackTrace();
                }

                //同时将两个一次也复制一下
                MeterWorkCarActualFirstBak firstBak1 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(meterWorkCarActualBak.getActualFirst1No());
                MeterWorkCarActualFirstBak firstBak2 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(meterWorkCarActualBak.getActualFirst2No());

                MeterWorkCarActualFirst first1 = new MeterWorkCarActualFirst();
                MeterWorkCarActualFirst first2 = new MeterWorkCarActualFirst();
                BeanUtils.copyProperties(firstBak1,first1);
                BeanUtils.copyProperties(firstBak2,first2);
                first1.setActualFirstNo(first1.getActualFirstNo()+"bak");
                first2.setActualFirstNo(first2.getActualFirstNo()+"bak");
                meterWorkCarActualFirstMapper.insert(first1);
                meterWorkCarActualFirstMapper.insert(first2);

                firstBak1.setValueFlag("0");
                firstBak2.setValueFlag("0");
                meterWorkCarActualBak.setValueFlag("0");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(firstBak1);
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(firstBak2);
                meterWorkCarActualBakMapper.updateByPrimaryKeySelective(meterWorkCarActualBak);
            }else{
                //抛出异常
                //事务回滚
                throw new Exception(note);
            }

        }


    }

    //外进外发
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void wwCopyToActual(List<String> ids,String carNo) throws Exception {
        for (String id : ids){
            //根据id获取对应的一次数据
            MeterWorkCarActualBak meterWorkCarActualBak = meterWorkCarActualBakMapper.selectByPrimaryKey(id);

            //一次数据为空
            if (meterWorkCarActualBak == null) {
                //抛出异常
                //事务回滚
                throw new Exception("一次数据为空");
            }

            if (!carNo.equals(meterWorkCarActualBak.getCarNo())){
                throw new Exception(id+"的车号不一致");
            }

            //判断是否符合要求
            String note = isSuit(meterWorkCarActualBak,1);
            if (note.equals("1")){
                //复制到First正式表里面
                MeterWorkCarActual actual = new MeterWorkCarActual();
                //复制一份
                BeanUtils.copyProperties(meterWorkCarActualBak,actual);
                //修改主键
                actual.setActualFirst1No(actual.getActualFirst1No() + "bak");
                actual.setActualFirst2No(actual.getActualFirst2No() + "bak");
                actual.setActualNo(actual.getActualNo()+"bak");

                MeterWorkCarActual tempActual = meterWorkCarActualMapper.selectByPrimaryKey(actual.getActualNo());
                if(tempActual != null) {
                    throw new Exception("结净数据已转成正式数据，不能重复转正式数据");
                }
                MeterWorkCarActualFirst tempGrossFirst = meterWorkCarActualFirstMapper.selectByPrimaryKey(actual.getActualFirst1No());
                if(tempActual != null) {
                    throw new Exception("一次毛重数据已转成正式数据，不能重复转正式数据");
                }
                MeterWorkCarActualFirst tempTareFirst = meterWorkCarActualFirstMapper.selectByPrimaryKey(actual.getActualFirst2No());
                if(tempActual != null) {
                    throw new Exception("一次皮重数据已转成正式数据，不能重复转正式数据");
                }

                meterWorkCarActualMapper.insert(actual);

                try {
                    //新增日志
                    MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                    logModel.setId(PublicMethod.IdCreate(2));
                    logModel.setOperationCarnNo(actual.getCarNo());
                    logModel.setCreateManNo(UserUtil.getCurrentUserId());
                    logModel.setCreateManName(UserUtil.getCurrentUserName());
                    logModel.setCreateTime(new Date());
                    logModel.setOperationPage("web端-汽车衡净重数据维护");
                    logModel.setOperationFunction("新增");
                    logModel.setOperationId(actual.getActualNo());
                    logModel.setOperationPredictionNo(actual.getPredictionNo());
                    meterBaseOperationLogMapper.insertSelective(logModel);
                }catch (Exception e){
                    logger.error("web端-汽车衡净重数据维护日志新增失败");
                    e.printStackTrace();
                }

                //同时将两个一次也复制一下
                MeterWorkCarActualFirstBak firstBak1 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(meterWorkCarActualBak.getActualFirst1No());
                MeterWorkCarActualFirstBak firstBak2 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(meterWorkCarActualBak.getActualFirst2No());

                MeterWorkCarActualFirst first1 = new MeterWorkCarActualFirst();
                MeterWorkCarActualFirst first2 = new MeterWorkCarActualFirst();
                BeanUtils.copyProperties(firstBak1,first1);
                BeanUtils.copyProperties(firstBak2,first2);
                first1.setActualFirstNo(first1.getActualFirstNo()+"bak");
                first2.setActualFirstNo(first2.getActualFirstNo()+"bak");
                meterWorkCarActualFirstMapper.insert(first1);
                meterWorkCarActualFirstMapper.insert(first2);

                firstBak1.setValueFlag("0");
                firstBak2.setValueFlag("0");
                meterWorkCarActualBak.setValueFlag("0");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(firstBak1);
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(firstBak2);
                meterWorkCarActualBakMapper.updateByPrimaryKeySelective(meterWorkCarActualBak);
            }else{
                throw new Exception(note);
            }

        }
    }

    @Override
    public List<MeterWorkCarActualBak> likeByDescNoPage(HashMap params) {
        List<MeterWorkCarActualBak> rows = meterWorkCarActualBakMapper.likeByDesc(params);
        return rows;
    }

    //判断 MeterWorkCarActualBak 是否符合要求
    //true -> 符合
    //false -> 不符合

    //type == 0 ->内倒
    //type == 1 ->外进外发

    /**  要求：
     * 站点、站点名、创建人、创建人编号、创建时间、数据来源、重量类型不能为空
     * 内倒车辆毛重必须有委托
     *
     */

    private String isSuit(MeterWorkCarActualBak model,int type){

        //毛重信息
        if (model.getBaseSpot1No() == null || StringUtils.isEmpty(model.getBaseSpot1No())) {
            return "毛重计量编号为空";
        }
        if (model.getBaseSpot1Name() == null || StringUtils.isEmpty(model.getBaseSpot1Name())) {
            return "毛重计量名称为空";
        }
        if(model.getGrossManNo() == null || StringUtils.isEmpty(model.getGrossManNo())) {
            return  "毛重计量员编号为空";
        }
        if(model.getGrossManName() == null || StringUtils.isEmpty(model.getGrossManName())) {
            return  "毛重计量员为空";
        }
        if (model.getGrossTime() == null || StringUtils.isEmpty(model.getGrossTime().toString())) {
            return "毛重时间为空";
        }
        if (model.getGrossWeight() == null || StringUtils.isEmpty(model.getGrossWeight().toString())) {
            return "毛重为空";
        }
        //皮重信息
        if (model.getBaseSpot2No() == null || StringUtils.isEmpty(model.getBaseSpot2No())) {
            return "皮重计量点编号为空";
        }
        if (model.getBaseSpot2Name() == null || StringUtils.isEmpty(model.getBaseSpot2Name())) {
            return "皮重计量点为空";
        }
        if(model.getTareManNo() == null || StringUtils.isEmpty(model.getTareManNo())) {
            return  "皮重计量员编号为空";
        }
        if(model.getTareManName() == null || StringUtils.isEmpty(model.getTareManName())) {
            return "皮重计量员名称为空";
        }
        if (model.getTareTime() == null || StringUtils.isEmpty(model.getTareTime().toString())) {
            return "皮重时间为空";
        }
        if (model.getTareWeight() == null || StringUtils.isEmpty(model.getTareWeight().toString())) {
            return "皮重为空";
        }

        //结净信息
        if (model.getNetSpot3No() == null || StringUtils.isEmpty(model.getNetSpot3No())) {
            return "净重计量点编号为空";
        }
        if (model.getNetSpot3Name() == null || StringUtils.isEmpty(model.getNetSpot3Name())) {
            return "净重计量点名称为空";
        }
        if(model.getNetManNo() == null || StringUtils.isEmpty(model.getNetManNo())) {
            return "净重计量员编号为空";
        }
        if(model.getNetManName() == null || StringUtils.isEmpty(model.getNetManName())) {
            return  "净重计量员为空";
        }
        if (model.getNetTime() == null || StringUtils.isEmpty(model.getNetTime().toString())) {
            return "净重时间为空";
        }
        if (model.getNetWeight() == null || StringUtils.isEmpty(model.getNetWeight().toString())) {
            return "净重为空";
        }
        //创建人、创建时间
        if(model.getCreateManName() == null || StringUtils.isEmpty(model.getCreateManName())) {
            return  "创建人为空";
        }
        if (model.getCreateTime() == null || StringUtils.isEmpty(model.getCreateTime().toString())) {
            return "创建时间为空";
        }
        if (model.getDataSource() == null || StringUtils.isEmpty(model.getDataSource())) {
            return "计量数据来源为空";
        }
        if (model.getUploadFlag() == null || StringUtils.isEmpty(model.getUploadFlag())){
            return "同步标志为空";
        }
        if (model.getValueFlag() == null || StringUtils.isEmpty(model.getValueFlag())){
            return "数据状态为空";
        }


        //内倒车结净必须有委托
        //type == 0 -> 内倒
        if (type == 0 ) {
            //委托不能为空
            if(model.getPredictionNo() == null){
                return "委托编号为空";
            }
        }

        return "1";
    }
}
