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

import com.alibaba.fastjson.JSONObject;
import com.jisco.dynamicweight.util.UserUtil;
import com.steerinfo.baseinfo.meterbaseoperationlog.mapper.MeterBaseOperationLogMapper;
import com.steerinfo.baseinfo.meterbaseoperationlog.model.MeterBaseOperationLog;
import com.steerinfo.meterwork.constants.DjPbModel;
import com.steerinfo.meterwork.meterworkcaractualbak.mapper.MeterWorkCarActualBakMapper;
import com.steerinfo.meterwork.meterworkcaractualbak.model.MeterWorkCarActualBak;
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.baseinfo.meterbaseaddweight.mapper.MeterBaseAddWeightMapper;
import com.steerinfo.baseinfo.meterbaseaddweight.model.MeterBaseAddWeight;
import com.steerinfo.baseinfo.meterbasehistaredata.mapper.MeterBaseHisTareDataMapper;
import com.steerinfo.baseinfo.meterbasehistaredata.model.MeterBaseHisTareData;
import com.steerinfo.baseinfo.meterbasehistaredetail.mapper.MeterBaseHisTareDetailMapper;
import com.steerinfo.baseinfo.meterbasehistaredetail.model.MeterBaseHisTareDetail;
import com.steerinfo.baseinfo.meterbasematterinfo.mapper.MeterBaseMatterInfoMapper;
import com.steerinfo.baseinfo.meterbasematterinfo.model.MeterBaseMatterInfo;
import com.steerinfo.baseinfo.meterbasespotinfo.mapper.MeterBaseSpotInfoMapper;
import com.steerinfo.baseinfo.meterbasespotinfo.model.MeterBaseSpotInfo;
import com.steerinfo.baseinfo.meterbasetermtaredata.mapper.MeterBaseTermTareDataMapper;
import com.steerinfo.baseinfo.meterbasetermtaredata.model.MeterBaseTermTareData;
import com.steerinfo.baseinfo.meterbaseunloadtime.mapper.MeterBaseUnloadTimeMapper;
import com.steerinfo.baseinfo.meterbaseunloadtime.model.MeterBaseUnloadTime;
import com.steerinfo.baseinfo.meterbasevideoinfo.mapper.MeterBaseVideoinfoMapper;
import com.steerinfo.baseinfo.meterbasevideoinfo.model.MeterBaseVideoinfo;
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.framework.utils.collection.ListUtils;
import com.steerinfo.metermonitor.metermonitornote.mapper.MeterMonitorNoteMapper;
import com.steerinfo.metermonitor.metermonitornote.model.MeterMonitorNote;
import com.steerinfo.meterwork.constants.DbConstants;
import com.steerinfo.meterwork.dto.*;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkaddweight.mapper.MeterWorkAddWeightMapper;
import com.steerinfo.meterwork.meterworkaddweight.model.MeterWorkAddWeight;
import com.steerinfo.meterwork.meterworkcraneactual.mapper.MeterWorkCraneActualMapper;
import com.steerinfo.meterwork.meterworkimage.mapper.MeterWorkImageMapper;
import com.steerinfo.meterwork.meterworkimage.model.MeterWorkImage;
import com.steerinfo.meterwork.meterworkmonitor.mapper.MeterWorkMonitorMapper;
import com.steerinfo.meterwork.meterworkmonitor.model.MeterWorkMonitor;
import com.steerinfo.meterwork.meterworkonline.mapper.MeterWorkOnlineMapper;
import com.steerinfo.meterwork.meterworkonline.model.MeterWorkOnline;
import com.steerinfo.meterwork.meterworkprelink.mapper.MeterWorkPreLinkMapper;
import com.steerinfo.meterwork.meterworkprelink.model.MeterWorkPreLink;
import com.steerinfo.meterwork.meterworkprelink.service.IMeterWorkPreLinkService;
import com.steerinfo.meterwork.meterworkpreprocess.mapper.MeterWorkPreProcessMapper;
import com.steerinfo.meterwork.meterworkpreprocess.model.MeterWorkPreProcess;
import com.steerinfo.meterwork.meterworkprewgttype.mapper.MeterWorkPreWgttypeMapper;
import com.steerinfo.meterwork.meterworkrailwayactfirst.mapper.MeterWorkRailwayActFirstMapper;
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.pretrackscale.mapper.PreTrackScaleMapper;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.util.*;
import org.apache.http.util.TextUtils;
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 com.steerinfo.framework.controller.RESTfulResult;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


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

    @Resource
    private MeterWorkCraneActualMapper craneActualMapper;

    @Resource
    private MeterBaseSpotInfoMapper meterBaseSpotInfoMapper;

    @Resource
    private MeterWorkCarActualFirstBakMapper meterWorkCarActualFirstBakMapper;

    @Resource
    private MeterWorkRailwayActualMapper meterWorkRailwayActualMapper;

    @Resource
    private MeterBaseHisTareDataMapper meterBaseHisTareDataMapper;

    @Resource
    private PreTrackScaleMapper preTrackScaleMapper;

    @Resource
    private MeterWorkPreLinkMapper meterWorkPreLinkMapper;

    @Autowired
    IMeterWorkPreLinkService meterWorkPreLinkService;

    @Resource
    private MeterBaseHisTareDetailMapper meterBaseHisTareDetailMapper;

    @Resource
    private MeterWorkPreWgttypeMapper meterWorkPreWgttypeMapper;

    @Resource
    private MeterWorkImageMapper meterWorkImageMapper;

    @Resource
    private MeterBaseVideoinfoMapper meterBaseVideoinfoMapper;

    @Resource
    private MeterWorkCarActualBakMapper meterWorkCarActualBakMapper;

    @Resource
    private MeterWorkPreProcessMapper meterWorkPreProcessMapper;

    @Resource
    private MeterWorkMonitorMapper meterWorkMonitorMapper;

    @Resource
    private MeterWorkOnlineMapper meterWorkOnlineMapper;

    @Resource
    private MeterWorkRailwayActFirstMapper meterWorkRailwayActFirstMapper;

    @Resource
    private MeterMonitorNoteMapper meterMonitorNoteMapper;

    @Resource
    private MeterBaseMatterInfoMapper meterBaseMatterInfoMapper;

    @Resource
    private MeterBaseTermTareDataMapper meterBaseTermTareDataMapper;

    @Resource
    private MeterBaseUnloadTimeMapper meterBaseUnloadTimeMapper;

    @Resource
    private MeterBaseAddWeightMapper meterBaseAddWeightMapper;

    @Resource
    private MeterWorkAddWeightMapper meterWorkAddWeightMapper;

    @Resource
    private MeterWorkCarActualFirstMapper meterWorkCarActualFirstMapper;

    @Resource
    private MeterBaseOperationLogMapper meterBaseOperationLogMapper;

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

    @Override
    protected IBaseMapper<MeterWorkCarActualFirstBak, String> getMapper() {
        return meterWorkCarActualFirstBakMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult checkandadd(MeterWorkCarActualFirstBak model, MeterMonitorNote note) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            ckeck1(model);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
//            String no = meterWorkCarActualFirstMapper.getNewID(afl);
//            model.setActualFirstNo(sdf.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            model.setActualFirstNo(no);

            if(StringUtils.isNotEmpty(model.getPredictionNo())){
                PreTrackScale pre = new PreTrackScale();
                pre.setPredictionNo(model.getPredictionNo());
                pre.setLineDesc("0");
                preTrackScaleMapper.updateByPrimaryKeySelective(pre);
            }

            //6、存储图片
            //SaveImg(model);

            //7、修改计量监控
            updateMonitor(model.getBaseSpotNo());

            //8、修改监控的计量作业编号
            updateMonitorOver(model.getActualFirstNo(), model.getBaseSpotNo());

            //信息加密
            String str = model.getActualFirstNo() + "," + model.getMeterWeight() + "," + "1";
            String enStr = encryption(str);

            //保存日志
            saveNote(note);

            //当存储一次数据或者结净时，将本车辆的预处理重量改为已使用
            updatePreWeight(model.getCarNo());
            if(StringUtils.isEmpty(model.getValueFlag())){
                model.setValueFlag("1");
            }
            if(StringUtils.isNotEmpty(model.getPredictionNo())){
                model.setIsPreScale("1");
            }
            model.setCreateTime(new Date());
            int num = meterWorkCarActualFirstBakMapper.insertSelective(model);
            rm.setSucceed();
            rm.setCode("200");
            rm.setData(model.getActualFirstNo());
            rm.setMessage(enStr);
            return rm;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int checkandupdate(MeterWorkCarActualFirstBak model) throws MarkerMetException {
        try {
            if (model.getValueFlag().equals(DbConstants.NET)) {
                throw new MarkerMetException(500, "已经结净的数据无法修改！！");
            }
            ckeck(model);
            MeterWorkCarActualFirstBak first = new MeterWorkCarActualFirstBak();
            first.setActualFirstNo(model.getActualFirstNo());
            first.setCarNo(model.getCarNo());
            int num = meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(first);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int checkcarandupdate(MeterWorkCarActualFirstBak model) throws MarkerMetException {
        try {
            ckeckcar(model);
            int num = meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    public RESTfulResult firstDataSupplement(MeterWorkCarActualFirstBak model) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            String afl = sdf.format(new Date());
//            String no = meterWorkCarActualFirstBakMapper.getNewID(afl);
//            model.setActualFirstNo(sdf.format(new Date()) + no);
//            String userId = "";
//            String userName = "";
//            try{
//                UserPayload payload = UserPayload.getCurrUser();
//                userId = payload.getUserCode();
//                userName = payload.getUserName();
//            }catch(Exception e) {
//                userId = "admin";
//                userName = "admins";
//            }
            Assert.isTrue(model.getCreateTime() != null && StringUtils.isNotEmpty(model.getCreateTime().toString()), "计量时间不能为空");
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("baseSpotNo", model.getBaseSpotNo());
            List<MeterBaseSpotInfo> spotModel = meterBaseSpotInfoMapper.selectByParameters(obj);
            Assert.isTrue(spotModel.size() >= 1, "计量点配置表未找到相应的计量点");
            model.setBaseSpotName(spotModel.get(0).getBaseSpotName());
            if(model.getWeightType().equals("毛重")){
                model.setWeightType("0");
            }else if(model.getWeightType().equals("皮重")){
                model.setWeightType("1");
            }else if(model.getWeightType().equals("期限皮重")) {
                model.setWeightType("2");
            }
            String no = PublicMethod.IdCreate(3);
            model.setActualFirstNo(no);
            model.setAddWeight(model.getAddWeight()==null ? new BigDecimal(0): model.getAddWeight());
            BigDecimal meterWeight = model.getMeterWeight().multiply(new BigDecimal(1000));
            model.setMeterWeight(meterWeight);
            model.setValueFlag("1");
            model.setCheckFlag("1");
//            model.setMeterMode("3");
            model.setDataSource("4");
            model.setIsPreScale("0");
            meterWorkCarActualFirstBakMapper.insertSelective(model);
            rm.setSucceed();
            rm.setCode("200");
            rm.setData(model);
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！！>>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int abolish(List<MeterWorkCarActualFirstBak> models) throws MarkerMetException {
        try {
            int num = 0;
            if (models.size() <= 0 || models == null) {
                throw new MarkerMetException(500, "参数为空!!");
            }
            for (MeterWorkCarActualFirstBak model : models) {
                if (model.getValueFlag().equals(DbConstants.NET)) {
                    throw new MarkerMetException(500, "已结净的数据不可再废除!!");
                }
                ckeck(model);
                model.setValueFlag(DbConstants.INVALID);
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model);
                num++;
            }
            for (MeterWorkCarActualFirstBak model : models) {
                if (StringUtils.isNotEmpty(model.getPredictionNo())
                        && model.getPredictionNo().length() > 10) {
                    PreTrackScale ps = new PreTrackScale();
                    ps.setPredictionNo(model.getPredictionNo());
                    ps.setValueFlag(DbConstants.Abolish); //没有有效的一次计量记录的时候，预报直接作废，这样检化验那边才能不影响下一车采样 2021年4月18日
                    ps.setUploadFlag("1");
                    this.preTrackScaleMapper.updateByPrimaryKeySelective(ps);
                }
            }
            /*
            // 2021年5月24日 不需要搞得这么复杂，直接干掉就可以了
            //杨秀东新增逻辑，若预报只有一条有效的一次计量数据，则作废一次计量数据时，将预报置为未使用的状态
            for (MeterWorkCarActualFirstBak model : models) {
                //必须判断预报是否存在，因为厂内倒运没有预报数据
                if (StringUtils.isNotEmpty(model.getPredictionNo())
                        && model.getPredictionNo().length() > 10) {
                    MeterWorkCarActualFirstBak myFirt = new MeterWorkCarActualFirstBak();
                    myFirt.setPredictionNo(model.getPredictionNo());
                    myFirt.setValueFlag(DbConstants.VALID);
                    Map<String, Object> map = FieldsCollector.beanToMap(myFirt);
                    //一次计量数据信息
                    List<MeterWorkCarActualFirstBak> listFirst = this.meterWorkCarActualFirstBakMapper.selectByParameters(map);
                    //计量流程表数据
                    List<MeterWorkPreProcess> listPreProcess = this.meterWorkPreProcessMapper.selectByParameters(map);
                    //环节表数据
                    List<MeterWorkPreLink> listPreLink = this.meterWorkPreLinkMapper.selectByParameters(map);
                    if (listFirst != null && listFirst.size() == 0) {
                        //处理预报信息；回退到未使用状态
                        PreTrackScale ps = new PreTrackScale();
                        ps.setPredictionNo(model.getPredictionNo());
//                        ps.setValueFlag(DbConstants.VALID); //没有有效的一次计量记录的时候，预报回退到未使用的状态
                        ps.setValueFlag(DbConstants.Abolish); //没有有效的一次计量记录的时候，预报直接作废，这样检化验那边才能采样 2021年4月18日
                        this.preTrackScaleMapper.updateByPrimaryKeySelective(ps);

                        //处理流程表信息
                        if (listPreProcess != null && listPreProcess.size() > 0) {
                            for (MeterWorkPreProcess myProcess : listPreProcess) {
                                myProcess.setValueFlag(DbConstants.N);
                                this.meterWorkPreProcessMapper.updateByPrimaryKeySelective(myProcess);
                            }
                        }
                        //处理环节信息
                        int mySeqNo = 0;
                        if (listPreLink != null && listPreLink.size() > 0) {
                            for (MeterWorkPreLink myLink : listPreLink) {
                                if ("一次计量".equals(myLink.getLinkName())) {
                                    mySeqNo = myLink.getSeqNo();
                                }
                            }
                            //流程大于“一次计量”的全部都给置位 0
                            for (MeterWorkPreLink myLink : listPreLink) {
                                if (myLink.getSeqNo() > mySeqNo || myLink.getSeqNo() == mySeqNo) {
                                    myLink.setValueFlag(DbConstants.N);
                                    this.meterWorkPreLinkMapper.updateByPrimaryKeySelective(myLink);
                                }
                            }
                        }
                    }
                    if (listFirst != null && listFirst.size() == 1) {
                        //处理预报信息；回退到已使用状态
                        PreTrackScale ps = new PreTrackScale();
                        ps.setPredictionNo(model.getPredictionNo());
                        ps.setValueFlag(DbConstants.Y); //只有一条有效的一次计量记录的时候，预报回退到已使用的状态
                        this.preTrackScaleMapper.updateByPrimaryKeySelective(ps);

                        //处理流程表信息
                        if (listPreProcess != null && listPreProcess.size() > 0) {
                            for (MeterWorkPreProcess myProcess : listPreProcess) {
                                myProcess.setValueFlag(DbConstants.Y);
                                this.meterWorkPreProcessMapper.updateByPrimaryKeySelective(myProcess);
                            }
                        }
                        //处理环节信息
                        int mySeqNo = 0;
                        if (listPreLink != null && listPreLink.size() > 0) {
                            for (MeterWorkPreLink myLink : listPreLink) {
                                if ("一次计量".equals(myLink.getLinkName())) {
                                    mySeqNo = myLink.getSeqNo();
                                }
                            }
                            //流程大于“一次计量”的全部都给置位 0
                            for (MeterWorkPreLink myLink : listPreLink) {
                                if (myLink.getSeqNo() > mySeqNo) {
                                    myLink.setValueFlag(DbConstants.N);
                                    this.meterWorkPreLinkMapper.updateByPrimaryKeySelective(myLink);
                                }
                            }
                        }
                    }
                }

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


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int recover(List<MeterWorkCarActualFirstBak> models) throws MarkerMetException {
        try {
            int num = 0;
            if (models.size() <= 0 || models == null) {
                throw new MarkerMetException(500, "请选择需要启用的数据!!");
            }
            for (MeterWorkCarActualFirstBak model : models) {
                if (model.getValueFlag().equals(DbConstants.NET)) {
                    throw new MarkerMetException(500, "计量数据已经结净，无法启用");
                }
                model.setValueFlag(DbConstants.VALID);
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model);

                if (StringUtils.isNotEmpty(model.getPredictionNo())
                        && model.getPredictionNo().length() > 10) {
                    PreTrackScale ps = new PreTrackScale();
                    ps.setPredictionNo(model.getPredictionNo());
                    ps.setValueFlag(DbConstants.IN); //预报调整为使用中
                    ps.setUploadFlag("1");
                    ps.setMeasureStation(model.getBaseSpotNo());
                    ps.setMeasureStationName(model.getBaseSpotName());
                    this.preTrackScaleMapper.updateByPrimaryKeySelective(ps);
                }

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


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int audit(MeterWorkCarActualFirstBak model) throws MarkerMetException {
        try {
            ckeck(model);
            if (model.getCheckFlag().equals("0")) {
                model.setCheckFlag(DbConstants.VALID);
            } else {
                throw new MarkerMetException(500, "当前状态不可审核");
            }
            int num = meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int unaudit(MeterWorkCarActualFirstBak model) throws MarkerMetException {
        try {
            ckeck(model);
            if (model.getCheckFlag().equals("1")) {
                model.setCheckFlag(DbConstants.INVALID);
            } else {
                throw new MarkerMetException(500, "该数据未审核");
            }
            int num = meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }


    @Override
    @Transactional
    public PageList<MeterWorkCarActualFirstBak> selectView(Map<Objects, String> parmas, Integer pageNum, Integer pageSize) {
        List<MeterWorkCarActualFirstBak> rowsCount = meterWorkCarActualFirstBakMapper.selectView(parmas);
        //system.out.println("数据总条数：" + rowsCount.size());
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkCarActualFirstBak> rows = meterWorkCarActualFirstBakMapper.selectView(parmas);
        //system.out.println("数据分页条数：" + rows.size());
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    /**
     * 一次计量解除结净
     *
     * @param models
     * @return
     * @throws MarkerMetException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(List<MeterWorkCarActualFirstBak> models) throws MarkerMetException {
        try {
            int num = 0;
            if (models.size() <= 0 || models == null) {
                throw new MarkerMetException(500, "参数为空!!");
            }

            for(MeterWorkCarActualFirstBak model: models) {
                MeterWorkCarActualFirstBak modelTemp = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(model.getActualFirstNo());
                if(modelTemp != null) {
                    if(modelTemp.getValueFlag().equals("0") || modelTemp.getValueFlag().equals("2")) {
                        throw new MarkerMetException(500, "当前一次数据已结净或者已作废，不允许匹配委托!!");
                    }
                }
                PreTrackScale pls = preTrackScaleMapper.selectByPrimaryKey(model.getPredictionNo());
                if(pls.getValueFlag().equals("9")) {
                    throw new MarkerMetException(500, "一次数据中包含已结算的委托!!");
                }
                if(modelTemp.getPredictionType().equals("5") || modelTemp.getPredictionType().equals("6") || modelTemp.getPredictionType().equals("7")){
                    throw new MarkerMetException(500, "结净数据中包含特殊委托匹配结净的数据!!!");
                }
            }

            for (MeterWorkCarActualFirstBak model : models) {
                if (model.getPredictionNo() == null && "".equals(model.getPredictionNo())) {
                    throw new MarkerMetException(500, "该条数据还未匹配！！");
                }
                PreTrackScale pls = preTrackScaleMapper.selectByPrimaryKey(model.getPredictionNo());

                /*
                //2021年5月24日 杨秀东注释，没有预报有效次数这个概念了，预报次数检疫的时候，自动生成新的预报编号；
                pls.setPredictionNo(model.getPredictionNo());
                pls.setEffectiveNum(pls.getEffectiveNum() + 1);
                if (pls.getEffectiveNum() == 0) {
                    pls.setValueFlag(DbConstants.UP);
                }
                if (pls.getEffectiveNum() > 0) {
                    pls.setValueFlag(DbConstants.UP);
                }
                 */
                if (pls == null) {
                    throw new MarkerMetException(500, "数据对应的委托不存在！！");
                }
                pls.setUpdateTime(new Date());
                pls.setUpdateManNo("system");
                pls.setUpdateManName("一次计量数据解除操作");
                pls.setValueFlag(DbConstants.VALID);
                pls.setUploadFlag("1");
                preTrackScaleMapper.updateByPrimaryKeySelective(pls);

                //回退至预环节的前一状态; 001021003 = 一次计量
                List<MeterWorkPreLink> mwpls = meterWorkPreLinkService.getNext(model.getPredictionNo(), DbConstants.CALCULATE);
                for (MeterWorkPreLink mwpl : mwpls) {
                    mwpl.setValueFlag("0");//未使用
                    meterWorkPreLinkMapper.updateByPrimaryKey(mwpl);
                }

                MeterWorkCarActualFirstBak mwcaf = meterWorkCarActualFirstBakMapper.selectAndRemove(model.getActualFirstNo());

                String actualFirstNo = mwcaf.getActualFirstNo();
                //当一次计量数据是已结净的状态时，同时将该结净数据的另一条一次计量数据自动匹配该委托，并将该结净记录也进行委托匹配
                if (mwcaf.getValueFlag() == "2" || "2".equals(mwcaf.getValueFlag())){
                    MeterWorkCarActualBak meterWorkCarActual = meterWorkCarActualBakMapper.selectActual(actualFirstNo);
                    String getActualFirstNo = "";
                    //判断传入的是哪条一次记录
                    if (meterWorkCarActual.getActualFirst1No().equals(actualFirstNo)){
                        getActualFirstNo = meterWorkCarActual.getActualFirst2No();
                    }
                    if (meterWorkCarActual.getActualFirst2No().equals(actualFirstNo)){
                        getActualFirstNo = meterWorkCarActual.getActualFirst1No();
                    }
                    //另一条一次数据进行委托解除
                    MeterWorkCarActualFirstBak meterWorkCarActualFirstBak1 = meterWorkCarActualFirstBakMapper.selectAndRemove(getActualFirstNo);
                    meterWorkCarActualFirstBak1.setIsPreScale("0");
                    meterWorkCarActualFirstBakMapper.updateByPrimaryKey(meterWorkCarActualFirstBak1);
                    //将该一次记录的结净数据委托解除
                    MeterWorkCarActualBak mwca = meterWorkCarActualBakMapper.cleanPredcition(meterWorkCarActual.getActualNo());
                    mwca.setIsPreScale("0");
                    meterWorkCarActualBakMapper.updateByPrimaryKey(mwca);
                }

                // 将一次计量数据设置为未匹配状态
                //mwcaf.setValueFlag("2");
                mwcaf.setIsPreScale("0");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf);
                num++;
            }
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }


    /**
     * 一次计量数据匹配
     *
     * @param actualFirstNo
     * @param predictionNo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult match(String actualFirstNo, String predictionNo) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            UserPayload userPayload = UserPayload.getCurrUser();
            String userId = userPayload.getUserCode();
            String userName = userPayload.getUserName();

            Assert.isTrue(StringUtils.isNotEmpty(actualFirstNo), "作业编号为空");
            Assert.isTrue(StringUtils.isNotEmpty(predictionNo), "预报编号为空");
            PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(predictionNo);
            Assert.isTrue(pts != null, "委托编号：" + predictionNo + "未查询到有效委托信息");
            MeterWorkCarActualFirstBak model = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualFirstNo);
            Assert.isTrue(model != null, "一次数据不存在");
            Assert.isTrue(!(model.getValueFlag().equals("0") || model.getValueFlag().equals("2")), "当前一次数据已结净或者已作废，不允许匹配委托");
            Assert.isTrue(model.getPredictionNo() == null || StringUtils.isEmpty(model.getPredictionNo()), "一次数据已匹配,不允许计量");
            MeterWorkCarActualFirstBak modelTemp = meterWorkCarActualFirstBakMapper.selectAndMatch(actualFirstNo, pts.getPredictionNo());
            BeanUtils.copyProperties(modelTemp, model);
            model.setIsPreScale("1");
            model.setUpdateManNo("system");
            model.setUpdateManName("system");
            model.setUpdateTime(new Date());
            model.setNote("一次数据匹配委托");
            model.setPredictionNo(pts.getPredictionNo());
            meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model);

            pts.setUpdateTime(new Date());
            pts.setUpdateManNo(userId);
            pts.setUpdateManName(userName);
            pts.setValueFlag(DbConstants.IN); //2021年5月24日  预报直接就写成【使用中】
            pts.setUploadFlag("1");
            pts.setMeasureStation(model.getBaseSpotNo());
            pts.setMeasureStationName(model.getBaseSpotName());
            preTrackScaleMapper.updateByPrimaryKey(pts);

            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("匹配委托成功");
            rm.setData(model);
        }catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常：>>>" + e.getMessage());
        }
        return rm;

//            if (StringUtils.isEmpty(actualFirstNo)) {
//                throw new MarkerMetException(500, "计量作业编号为空!!");
//            }
//            if (predictionNo == null || "".equals(predictionNo)) {
//                throw new MarkerMetException(500, "预报编号为空!!");
//            }
//            PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(predictionNo);
//            if (pts == null) {
//                throw new MarkerMetException(500, "该预报信息不存在");
//            }
//            MeterWorkCarActualFirstBak meterWorkCarActualFirst = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualFirstNo);
//            if (meterWorkCarActualFirst == null) {
//                throw new MarkerMetException(500, "该计量作业信息不存在");
//            }
//            if (meterWorkCarActualFirst.getPredictionNo() != null && !"".equals(meterWorkCarActualFirst.getPredictionNo())) {
//                throw new MarkerMetException(500, "该计量作业已匹配！！");
//            }
//            MeterWorkCarActualFirst model = meterWorkCarActualFirstMapper.selectAndMatch(actualFirstNo, predictionNo);
//            //区分预报的类型对状态进行修改
//            /*
//            //逻辑异常，此处预报的有效次数默认都是1，在匹配完成后，预报状态应当变为4【使用中】
//            pts.setEffectiveNum(pts.getEffectiveNum() - 1);
//            if (pts.getEffectiveNum() == 0) {
//                pts.setValueFlag(DbConstants.UP);
//            } else {
//                pts.setValueFlag(DbConstants.IN);
//            }
//             */
//            //将预环节的前置包括本环节都使用；001021003 = 一次计量
////            List<MeterWorkPreLink> mwpls = meterWorkPreLinkService.getPer(predictionNo, DbConstants.CALCULATE);
////            for (MeterWorkPreLink mwpl : mwpls) {
////                mwpl.setValueFlag("2");//已使用
////                meterWorkPreLinkMapper.updateByPrimaryKey(mwpl);
////            }
//            //当一次计量数据是已结净的状态时，同时将该结净数据的另一条一次计量数据自动匹配该委托，并将该结净记录也进行委托匹配
//            if ("2".equals(meterWorkCarActualFirst.getValueFlag()) || "0".equals(meterWorkCarActualFirst.getValueFlag())) {
//                throw new MarkerMetException(500, "该计量作业信息已结净或已作废");
//            }
//            if(pts != null) {
//                MeterWorkCarActualBak meterWorkCarActual = meterWorkCarActualBakMapper.selectActual(actualFirstNo);
//                String getActualFirstNo = "";
//                //判断传入的是哪条一次记录
//                if (meterWorkCarActual.getActualFirst1No().equals(actualFirstNo)){
//                    getActualFirstNo = meterWorkCarActual.getActualFirst2No();
//                }
//                if (meterWorkCarActual.getActualFirst2No().equals(actualFirstNo)){
//                    getActualFirstNo = meterWorkCarActual.getActualFirst1No();
//                }
//                //另一条一次数据进行委托解除
//                MeterWorkCarActualFirstBak meterWorkCarActualFirst1 = meterWorkCarActualFirstBakMapper.selectAndMatch(getActualFirstNo,predictionNo);
//                meterWorkCarActualFirst1.setIsPreScale("1");
//                meterWorkCarActualFirstMapper.updateByPrimaryKey(meterWorkCarActualFirst1);
//                //将该一次记录的结净数据委托解除
//                MeterWorkCarActualBak mwca = meterWorkCarActualBakMapper.mathPredcition(meterWorkCarActual.getActualNo(),predictionNo);
//                mwca.setIsPreScale("1");
//                mwca.setUploadFlag("1");
//                meterWorkCarActualBakMapper.updateByPrimaryKey(mwca);
//
//                pts.setUpdateTime(new Date());
//                pts.setUpdateManNo("system");
//                pts.setUpdateManName("一次计量数据匹配操作");
//                pts.setValueFlag(DbConstants.IN); //2021年5月24日  预报直接就写成【使用中】
//                pts.setUploadFlag("1");
//                pts.setMeasureStation(mwca.getNetSpot3No());
//                pts.setMeasureStationName(mwca.getNetSpot3Name());
//                preTrackScaleMapper.updateByPrimaryKey(pts);
//            }
//            // 设置为已匹配状态
//            //model.setValueFlag("1");
//            model.setIsPreScale("1");
//            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(model);
//            return model;
//        } catch (MarkerMetException e) {
//            throw e;
//        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageList<MeterWorkCarActualFirstBak> selectTa(Map<String, Object> parmas, String weightType, Integer pageNum, Integer pageSize) {
        parmas.put("weightType", weightType);
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkCarActualFirstBak> mwcafs = meterWorkCarActualFirstBakMapper.selectTa(parmas);
        PageList pageInfo = new PageList(mwcafs);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageList<MeterWorkCarActualFirstBak> selectNet(String weightType, String carNo, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkCarActualFirstBak> mwcafs = meterWorkCarActualFirstBakMapper.selectNet(weightType, carNo);
        PageList pageInfo = new PageList(mwcafs);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageList<StaffPerformanceAnalysis> analysis(String startTime, String endTime, Integer pageNum, Integer pageSize) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        PageHelper.startPage(pageNum, pageSize);
        List<StaffPerformanceAnalysis> list = meterWorkCarActualFirstBakMapper.analysis(obj);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageList<UtensilDto> utensil(String startTime, String endTime, Integer pageNum, Integer pageSize) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        PageHelper.startPage(pageNum, pageSize);
        List<UtensilDto> list = meterWorkCarActualFirstBakMapper.utensil(obj);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MeterTypeDto> meterType(String baseSpotNo, String startTime, String endTime) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("baseSpotNo", baseSpotNo);
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        List<MeterTypeDto> list = meterWorkCarActualFirstBakMapper.meterType(obj);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<TimesDto> daily(String baseSpotNo, String startTime, String endTime) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("baseSpotNo", baseSpotNo);
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        List<TimesDto> list = meterWorkCarActualFirstBakMapper.daily(obj);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<TimesDto> period(String baseSpotNo, String startTime, String endTime) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("baseSpotNo", baseSpotNo);
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        List<TimesDto> list = meterWorkCarActualFirstBakMapper.period(obj);
        List<TimesDto> tds = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            TimesDto td = new TimesDto();
            String times1 = String.format("%02d" + ":00:00", i);
            td.setTimes(times1);
            td.setNum(0.0);
            for (TimesDto dto : list) {
                if (td.getTimes().equals(dto.getTimes())) {
                    td.setNum(dto.getNum());
                }
            }
            tds.add(td);
        }
        return tds;
    }

    @Override
    @Transactional
    public List<MeterWorkCarActualFirstBak> detailed(String times, String num) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("spotTypeNo", DbConstants.CAR);
        obj.put("times", times);
        obj.put("num", num);
        List<MeterWorkCarActualFirstBak> list = meterWorkCarActualFirstBakMapper.detailed(obj);
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageList<AbnormalDto> abnormal(String baseSpotNo, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("baseSpotNo", baseSpotNo);
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        PageHelper.startPage(pageNum, pageSize);
        List<AbnormalDto> list = meterWorkCarActualFirstBakMapper.abnormal(obj);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageList<MeterWorkCarActualFirstBak> getByAbnormal(String meterReasonNo, String meterReasonName, Integer pageNum, Integer pageSize) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("meterReasonNo", meterReasonNo);
        obj.put("meterReasonName", meterReasonName);
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkCarActualFirstBak> list = meterWorkCarActualFirstBakMapper.getByAbnormal(obj);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }

    private void ckeck1(MeterWorkCarActualFirstBak model) throws MarkerMetException {
        if (model.getCarNo() == null || model.getCarNo().equals("")) {
            throw new MarkerMetException(500, "车号不能为空");
        }
        if (model.getBaseSpotNo() == null || model.getBaseSpotNo().equals("")) {
            throw new MarkerMetException(500, "计量点编号不能为空");
        }
        if (model.getBaseSpotName() == null || model.getBaseSpotName().equals("")) {
            throw new MarkerMetException(500, "计量点名称不能为空");
        }

        // 判断是否带有委托
        if (model.getPredictionNo() != null && !"".equals(model.getPredictionNo())) {
            PreTrackScale pre = new PreTrackScale();
            pre.setPredictionNo(model.getPredictionNo());
            pre.setValueFlag("4");
            pre.setUploadFlag("1");
            pre.setMeasureStation(model.getBaseSpotNo());
            pre.setMeasureStationName(model.getBaseSpotName());
            preTrackScaleMapper.updateByPrimaryKeySelective(pre);
        }

        if (StringUtils.isEmpty(model.getActualFirstNo())) {
            model.setValueFlag(DbConstants.VALID);
            model.setCheckFlag(DbConstants.INVALID);
        } else {
            model.setUpdateTime(new Date());
        }
    }


    private void ckeck(MeterWorkCarActualFirstBak model) throws MarkerMetException {
        if (model.getCarNo() == null || model.getCarNo().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.getBaseSpotNo() == null || model.getBaseSpotNo().equals("")) {
            throw new MarkerMetException(500, "计量点编号不能为空");
        }
        if (model.getBaseSpotName() == null || model.getBaseSpotName().equals("")) {
            throw new MarkerMetException(500, "计量点名称不能为空");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        if (StringUtils.isEmpty(model.getActualFirstNo())) {
            model.setCreateManNo(userId);
            model.setCreateManName(userName);
            model.setValueFlag(DbConstants.VALID);
            model.setCheckFlag(DbConstants.INVALID);
        } else {
            model.setUpdateTime(new Date());
            model.setUpdateManNo(userId);
            model.setUpdateManName(userName);
        }
    }

    private void ckeckcar(MeterWorkCarActualFirstBak model) throws MarkerMetException {
        if (model.getActualFirstNo() == null || "".equals(model.getActualFirstNo())) {
            throw new MarkerMetException(500, "操作数据主键不能为空");
        }
        if (model.getValueFlag() == "2") {
            throw new MarkerMetException(500, "已经结净的数据不能进行修改");
        }
        if (model.getPredictionNo() != null || "".equals(model.getPredictionNo())) {
            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.setUpdateTime(new Date());
        model.setUpdateManNo(userId);
        model.setUpdateManName(userName);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doQueryWf(MeterWorkCarActualFirstBak oci) {
        RESTfulResult rm = new RESTfulResult();
        try {
            rm.setCode("200");
            oci.setMeterWeight(null);
            oci.setAddWeight(null);
            oci.setMeterNum(null);
            oci.setShipmentGrossWeight(null);
            oci.setShipmentNetWeight(null);
            oci.setShipmentNum(null);
            Map<String, Object> map = FieldsCollector.beanToMap(oci);
            rm.setData(meterWorkCarActualFirstBakMapper.selectByParameters(map));
        } catch (Exception ex) {
            rm.setCode("500");
            rm.setMessage("查询失败：" + ex.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult queryFirst(String carNo) {
        RESTfulResult rm = new RESTfulResult();
        try {
            rm.setCode("200");
            rm.setData(meterWorkCarActualFirstBakMapper.selectByCarNo(carNo));
        } catch (Exception ex) {
            rm.setCode("500");
            rm.setMessage("查询失败：" + ex.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doUpdateWf(MeterWorkCarActualFirstBak oci) {
        RESTfulResult rm = new RESTfulResult();
        try {
            rm.setCode("200");
            meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(oci);
        } catch (Exception ex) {
            rm.setCode("500");
            rm.setMessage("修改数据失败：" + ex.getMessage());
        }
        return rm;
    }

    /**
     * 公共的存储历史皮重
     *
     * @param oci
     */
    @Transactional(rollbackFor = Exception.class)
    public void doSaveHisTare(MeterWorkCarActualFirstBak oci) {
        //获取ID
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String afl = formatter.format(oci.getCreateTime());
        //查找相应车辆有效的历史皮重信息
        MeterBaseHisTareData mbtd = new MeterBaseHisTareData();
        mbtd.setCarNo(oci.getCarNo());
        mbtd.setValueFlag("0");
        Map<String, Object> map = FieldsCollector.beanToMap(mbtd);
        List<MeterBaseHisTareData> lmOne = meterBaseHisTareDataMapper.selectByParameters(map);
        if (lmOne != null && lmOne.size() > 0) {
            //说明存在历史皮重，则执行修改操作
            mbtd.setDataNo(lmOne.get(0).getDataNo());
            mbtd.setBaseSpotNo(oci.getBaseSpotNo());
            mbtd.setBaseSpotName(oci.getBaseSpotName());
            mbtd.setMeterWeight(Math.round(oci.getMeterWeight().doubleValue()));
            mbtd.setUpWeight(oci.getMeterWeight().longValue()); //上次皮重
            //  (之前的重量*次数)/(次数+1)
            double db = (lmOne.get(0).getAvgWeight() * lmOne.get(0).getMeterNum() + lmOne.get(0).getMeterWeight()) / (lmOne.get(0).getMeterNum() + 1);
            mbtd.setAvgWeight(Math.round(db));//平均皮重
            mbtd.setMeterNum(lmOne.get(0).getMeterNum() + 1);
            mbtd.setCreateTime(new Date());
            meterBaseHisTareDataMapper.updateByPrimaryKeySelective(mbtd);
        } else {
            //新增操作
            String no = meterBaseHisTareDataMapper.getNewID(afl);
            mbtd.setDataNo(afl + no);
            mbtd.setBaseSpotNo(oci.getBaseSpotNo());
            mbtd.setBaseSpotName(oci.getBaseSpotName());
            mbtd.setMeterWeight(Math.round(oci.getMeterWeight().doubleValue()));
            mbtd.setUpWeight(oci.getMeterWeight().longValue()); //上次皮重
            mbtd.setAvgWeight(Math.round(oci.getMeterWeight().doubleValue()));//平均皮重
            mbtd.setMeterNum(1l);
            mbtd.setMeterTime(oci.getCreateTime());
            mbtd.setCreateTime(oci.getCreateTime());
            meterBaseHisTareDataMapper.insert(mbtd);
        }
        MeterBaseHisTareDetail mbhtd = new MeterBaseHisTareDetail();
        String no = meterBaseHisTareDetailMapper.getNewID(afl);
        mbhtd.setDetailNo(PublicMethod.IdCreate(3));
        mbhtd.setActualFirstNo(oci.getActualFirstNo());
        mbhtd.setBaseSpotNo(oci.getBaseSpotNo());
        mbhtd.setBaseSpotName(oci.getBaseSpotName());
        mbhtd.setCarNo(oci.getCarNo());
        mbhtd.setMeterOrder("1");
        mbhtd.setMeterTime(oci.getCreateTime());
        mbhtd.setMeterWeight(oci.getMeterWeight().longValue());
        meterBaseHisTareDetailMapper.insert(mbhtd);
    }


    /**
     * 图片保存信息
     *
     * @param oci
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public void SaveImg(MeterWorkCarActualFirstBak oci) throws Exception {
        //查下当前配置的摄像头有多少个
        MeterBaseVideoinfo mbv = new MeterBaseVideoinfo();
        mbv.setPointid(oci.getBaseSpotNo());
        Map<String, Object> map = FieldsCollector.beanToMap(mbv);
        List<MeterBaseVideoinfo> lmb = meterBaseVideoinfoMapper.selectByParameters(map);
        if (ListUtils.isNotEmpty(lmb) && lmb.size() > 0) {
            MeterBaseVideoinfo mb = lmb.get(0);

            long i = 0;
            //https://www.cnblogs.com/alice-cj/p/10363356.html
            Class clazz = mb.getClass();
            Object obj = clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();
            for (Field field : fields) {
                if (field.getName().contains("pointname")) {
                    field.setAccessible(true); // 私有属性必须设置访问权限
                    Object resultValue = field.get(obj);
                    if (resultValue != null && resultValue.toString().trim().length() > 0) {
                        i++;
                    }
                }
            }
            MeterWorkImage mw= meterWorkImageMapper.selectByPrimaryKey("C" + oci.getActualFirstNo());
            //METER_WORK_IMAGE
            MeterWorkImage mwi = new MeterWorkImage();
            mwi.setActualFirstNo("C" + oci.getActualFirstNo());
            mwi.setImageTime(oci.getCreateTime());
            mwi.setImageNum(++i);//因为界面上会存储一个界面截图，所以此处比摄像头要多1条
            for (int k = 1; k <= i; k++) {
                Field fName = mwi.getClass().getDeclaredField("imageFile" + i);
                fName.setAccessible(true);
                fName.set(mwi, oci.getBaseSpotNo() + "_" + oci.getActualFirstNo() + "_" + i);
            }
            if(mw != null){
                meterWorkImageMapper.updateByPrimaryKeySelective(mwi);
            }else{
                meterWorkImageMapper.insertSelective(mwi);
            }
        }
    }

    /**
     * 内倒车辆存储一次计量数据
     *
     * @param oci
     * @param saveTare 0 不保存历史皮，1保存历史皮
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(value = "tm", rollbackFor = Exception.class)
    public RESTfulResult doAddInnerWf(MeterWorkCarActualFirstBak oci, String saveTare, MeterMonitorNote note) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            //1、存储一次计量信息
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String afl = formatter.format(oci.getCreateTime());
//        String no = meterWorkCarActualFirstBakMapper.getNewID(afl);
//        oci.setActualFirstNo(formatter.format(new Date()) + no);

            String no = PublicMethod.IdCreate(3);
            oci.setActualFirstNo(no);
            oci.setValueFlag("1");
        /*//
        if(oci.getWeightType().equals("0")){

        }
        if(oci.getWeightType().equals("1")){

        }*/
            oci.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insert(oci);

            //2、存储历史皮重及历史皮重明细信息
            if ("1".equals(saveTare)) {
                doSaveHisTare(oci);
            }
            //3、保存图片
            //SaveImg(oci);
            //4、修改计量监控
            updateMonitor(oci.getBaseSpotNo());

            //保存日志
            saveNote(note);

            updateMonitorOver(oci.getActualFirstNo(), oci.getBaseSpotNo());

            //补充附加重量的数据
            if (StringUtils.isNotEmpty(oci.getAddWgtPk())) {
                insertAddWgtNo(oci.getActualFirstNo(), oci.getAddWgtPk(), oci.getCreateManNo(), oci.getCreateManName());
            }

            //重量类型为毛重则调用接口为
            String flag = "";
        /*
        if("0".equals(oci.getWeightType())){
            flag = transferParamForLims(oci.getCarNo(),oci.getMatterNo(),oci.getReceivingUintName(),oci.getForwardingUnitName(),"",oci.getCreateTime(),oci.getMeterWeight().toString(),oci.getActualFirstNo());
        }
        //*/
            rm.setSucceed();
            rm.setCode("200");
            rm.setData(oci.getActualFirstNo());
            rm.setMessage(flag);
        }catch(Exception e) {
            e.printStackTrace();
            rm.setFailed();
            rm.setMessage("操作异常，请确认！！>>>" + e.getMessage());
        }
        return rm;
    }

    /**
     * 内倒车辆结净
     *
     * @param secondDb 二次计量数据
     * @param netDb    净重数据
     * @param saveTare 0 不保存历史皮，1保存历史皮
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(value = "tm", rollbackFor = Exception.class)
    public RESTfulResult doAddNetInnerWf(MeterWorkCarActualFirstBak secondDb, MeterWorkCarActualBak netDb, String saveTare, MeterMonitorNote note) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        rm.setCode("500");
        try{
            //1、存储当前计量的数据
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String afl = formatter.format(new Date());
//            String no = meterWorkCarActualFirstMapper.getNewID(afl);
//            secondDb.setActualFirstNo(formatter.format(new Date()) + no);

            String no = PublicMethod.IdCreate(3);
            secondDb.setActualFirstNo(no);

            secondDb.setValueFlag("2");//结净
            secondDb.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insertSelective(secondDb);

            //修改一次计量数据为结净
            MeterWorkCarActualFirstBak firstUpdate = new MeterWorkCarActualFirstBak();
            //2、存储结净数据
            if (secondDb.getMeterWeight().equals(netDb.getTareWeight())) {
                //二次重量如果跟皮重一样
                netDb.setActualFirst2No(secondDb.getActualFirstNo());
                firstUpdate.setActualFirstNo(netDb.getActualFirst1No());
                firstUpdate.setWeightType("0");
            } else {
                netDb.setActualFirst1No(secondDb.getActualFirstNo());
                firstUpdate.setActualFirstNo(netDb.getActualFirst2No());
                firstUpdate.setWeightType("1");
            }

            //3、修改第一次存储的计量数据状态为结净
            if (firstUpdate.getActualFirstNo() != null) {
                firstUpdate.setValueFlag("2");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(firstUpdate);
            }

            //判断结净数据是否匹配委托
            if (StringUtils.isNotEmpty(netDb.getPredictionNo())) {
                MeterWorkCarActualFirstBak meca1 = meterWorkCarActualFirstBakMapper.selectAndMatch(firstUpdate.getActualFirstNo(), netDb.getPredictionNo());
                meca1.setIsPreScale("1");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(meca1);
                PreTrackScale preTrackScale = new PreTrackScale();
                preTrackScale.setPredictionNo(netDb.getPredictionNo());
                preTrackScale.setValueFlag("2");
                preTrackScale.setUploadFlag("1");
                preTrackScale.setMeasureStation(netDb.getNetSpot3No());
                preTrackScale.setMeasureStationName(netDb.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(preTrackScale);
                PreTrackScale pres = preTrackScaleMapper.selectBypredictionNo(netDb.getPredictionNo());
                if(pres != null && pres.getReceiverRemark() != null && (pres.getReceiverRemark().equals("H01") || pres.getReceiverRemark().equals("H02"))){
                    netDb.setPredictionType("7");
                }
            }
            // 判断当前数据是否存在委托
//            if (netDb.getPredictionNo() != null && "".equals(netDb.getPredictionNo())) {
//                MeterWorkCarActualFirst gross = meterWorkCarActualFirstMapper.selectByPrimaryKey(netDb.getActualFirst1No());
//                if ((gross.getPredictionNo() == null || "".equals(gross.getPredictionNo()))) {
//                    // 如果毛重数据还未匹配委托，则匹配委托操作
//                    MeterWorkCarActualFirstBak meca1 = meterWorkCarActualFirstBakMapper.selectAndMatch(gross.getActualFirstNo(), netDb.getPredictionNo());
//                    meterWorkCarActualFirstMapper.updateByPrimaryKeySelective(meca1);
//                    PreTrackScale preTrackScale = new PreTrackScale();
//                    preTrackScale.setPredictionNo(netDb.getPredictionNo());
//                    preTrackScale.setValueFlag("2");
//                    preTrackScale.setUploadFlag("1");
//                    preTrackScaleMapper.updateByPrimaryKeySelective(preTrackScale);
//                }
//            }
//            String no1 = meterWorkCarActualBakMapper.getNewID(afl);
//            netDb.setActualNo(afl + no1);
            String no1 = PublicMethod.IdCreate(3);
            netDb.setActualNo(no1);
            if (netDb != null && netDb.getNetWeight() != null) {
                //2021年3月7日 净重保留两位小数；
                BigDecimal net;
                net = new BigDecimal(String.valueOf(netDb.getNetWeight())).setScale(2, BigDecimal.ROUND_HALF_UP);
                netDb.setNetWeight(net);
            }
            netDb.setUploadFlag("1");
            netDb.setCreateTime(new Date());
            if(netDb.getGrossTime().compareTo(netDb.getTareTime()) >= 0) {
                netDb.setNetSpot3No(netDb.getBaseSpot1No());
                netDb.setNetSpot3Name(netDb.getBaseSpot1Name());
                netDb.setNetManNo(netDb.getGrossManNo());
                netDb.setNetManName(netDb.getGrossManName());
                netDb.setNetTime(netDb.getGrossTime());
                netDb.setCreateTime(netDb.getGrossTime());
                netDb.setCreateManName(netDb.getGrossManName());
            }else {
                netDb.setNetSpot3No(netDb.getBaseSpot2No());
                netDb.setNetSpot3Name(netDb.getBaseSpot2Name());
                netDb.setNetManNo(netDb.getTareManNo());
                netDb.setNetManName(netDb.getTareManName());
                netDb.setNetTime(netDb.getTareTime());
                netDb.setCreateTime(netDb.getTareTime());
                netDb.setCreateManName(netDb.getTareManName());
            }
            meterWorkCarActualBakMapper.insert(netDb);

            //4、如果为期限皮重，则将期限皮重转化为一条皮重计量数据保存
            MeterWorkCarActualFirstBak mwraf = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(netDb.getActualFirst2No());
            if (mwraf == null) {
                MeterBaseTermTareData mbttd = new MeterBaseTermTareData();
                HashMap mbMap = new HashMap();
                SimpleDateFormat mbsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                mbMap.put("endTime",mbsdf.format(new Date()));
                if(StringUtils.isNotEmpty(netDb.getActualFirst2No())){
                    mbMap.put("dataNo",netDb.getActualFirst2No());
                    mbttd = meterBaseTermTareDataMapper.selectByDataNo(mbMap);
                }
                else{
                    mbMap.put("carNo",netDb.getCarNo());
                    mbttd = meterBaseTermTareDataMapper.selectByCarNo(mbMap);
                }
                MeterWorkCarActualFirstBak imTare = new MeterWorkCarActualFirstBak();
                //补充数据
                String afl1 = formatter.format(new Date());
//                String imTareNO = meterWorkCarActualFirstMapper.getNewID(afl1);
//                imTare.setActualFirstNo(afl1 + imTareNO);
                String imTareNO = PublicMethod.IdCreate(3);
                imTare.setActualFirstNo(imTareNO);
                imTare.setMeterTypeNo(secondDb.getMeterTypeNo());
                imTare.setMeterTypeName(secondDb.getMeterTypeName());
                imTare.setBaseSpotNo(mbttd.getBaseSpotNo());
                imTare.setBaseSpotName(mbttd.getBaseSpotName());
                imTare.setCarNo(mbttd.getCarNo());
                imTare.setCreateTime(mbttd.getCreateTime());
                imTare.setCreateManNo(mbttd.getCreateManNo());
                imTare.setCreateManName(mbttd.getCreateManName());
                imTare.setMeterWeight(netDb.getTareWeight());
                imTare.setWeightType("2");//期限皮重
                imTare.setValueFlag("2");//结净
                meterWorkCarActualFirstBakMapper.insertSelective(imTare);
                //将结净数据中的皮重编号修改
                MeterWorkCarActualBak mwca = new MeterWorkCarActualBak();
                mwca.setActualNo(netDb.getActualNo());
                mwca.setActualFirst2No(imTare.getActualFirstNo());
                meterWorkCarActualBakMapper.updateByPrimaryKeySelective(mwca);
            }
            //4、存储历史皮重及历史皮重明细信息
            if ("1".equals(saveTare)) {
                MeterWorkCarActualFirstBak oci = new MeterWorkCarActualFirstBak();
                oci.setCarNo(netDb.getCarNo());
                oci.setCreateTime(netDb.getTareTime());
                oci.setBaseSpotNo(netDb.getBaseSpot2No());
                oci.setBaseSpotName(netDb.getBaseSpot2Name());
                oci.setMeterWeight(netDb.getTareWeight());
                oci.setActualFirstNo(netDb.getActualFirst2No());
                doSaveHisTare(oci);
            }
            //6、修改计量监控
            updateMonitor(secondDb.getBaseSpotNo());
            //保存日志
            saveNote(note);
            //当存储一次数据或者结净时，将本车辆的预处理重量改为已使用
            updatePreWeight(secondDb.getCarNo());
            updateMonitorOver(secondDb.getActualFirstNo(), secondDb.getBaseSpotNo());
            //重量类型为毛重则调用接口为
            String flag = "";
            //补充附加重量的数据
            if (StringUtils.isNotEmpty(secondDb.getAddWgtPk())) {
                insertAddWgtNo(secondDb.getActualFirstNo(), secondDb.getAddWgtPk(), secondDb.getCreateManNo(), secondDb.getCreateManName());
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setData(secondDb.getActualFirstNo());
            rm.setMessage(flag);
            return rm;
        }catch (Exception e){
            e.printStackTrace();
            rm.setFailed();
            rm.setMessage(e.getMessage());
            return rm;
        }
    }

    /**
     * 内倒车辆结净
     *
     * @param secondDb 二次计量数据
     * @param netDb    净重数据
     * @param saveTare 0 不保存历史皮，1保存历史皮
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(value = "tm", rollbackFor = Exception.class)
    public RESTfulResult doAddNetNewHZHX(MeterWorkCarActualFirstBak secondDb, MeterWorkCarActualBak netDb, MeterWorkCarActualFirstBak once, String saveTare, MeterMonitorNote note) throws Exception {
        try{
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("200");

            //1a.查询上一个结净，如果receive_remark不是H01，直接报错
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("carNo", netDb.getCarNo());
            obj.put("receiverRemark", "H01");
            List<MeterWorkCarActualBak> models = meterWorkCarActualBakMapper.likeByDesc(obj);
            if(models.size() < 1) {
                rm.setCode("500");
                rm.setData(null);
                rm.setMessage("不存在混装结净数据，请确认！！");
                return rm;
            }
            MeterWorkCarActualBak upModel = models.get(0);
            MeterWorkCarActualFirstBak upFirstGross = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(upModel.getActualFirst1No());
            MeterWorkCarActualFirstBak upFirstTare = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(upModel.getActualFirst2No());
            PreTrackScale upScale = preTrackScaleMapper.selectByPrimaryKey(upModel.getPredictionNo());
            upFirstGross.setPredictionCombination(upModel.getPredictionNo() + "," + netDb.getPredictionNo());
            upFirstGross.setPredictionType("7");
            meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(upFirstGross);
            upFirstTare.setPredictionCombination(upModel.getPredictionNo() + "," + netDb.getPredictionNo());
            upFirstTare.setPredictionType("7");
            meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(upFirstTare);
            upModel.setPredictionCombination(upModel.getPredictionNo() + "," + netDb.getPredictionNo());
            upModel.setPredictionType("7");
            meterWorkCarActualBakMapper.updateByPrimaryKeySelective(upModel);
            upScale.setPredictionType("7");
            upScale.setPredictionCombination(upModel.getPredictionNo() + "," + netDb.getPredictionNo());
            preTrackScaleMapper.updateByPrimaryKeySelective(upScale);

            //1、存储当前计量的数据
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String afl = formatter.format(new Date());
//            String no = meterWorkCarActualFirstMapper.getNewID(afl);
//            secondDb.setActualFirstNo(formatter.format(new Date()) + no);

            String no = PublicMethod.IdCreate(3);
            secondDb.setActualFirstNo(no);

            secondDb.setValueFlag("2");//结净
            secondDb.setUpdateManNo("system");
            secondDb.setUpdateManName("混装结净更新");
            secondDb.setPredictionType("7");
            secondDb.setCreateTime(new Date());
            secondDb.setPredictionCombination(upModel.getPredictionNo() + "," + netDb.getPredictionNo());
            meterWorkCarActualFirstBakMapper.insertSelective(secondDb);

            //1、存储当前计量的数据
            SimpleDateFormat formatterHZ = new SimpleDateFormat("yyyyMMdd");
            String aflHZ = formatterHZ.format(new Date());
//            String noHZ = meterWorkCarActualFirstMapper.getNewID(aflHZ);
//            once.setActualFirstNo(formatter.format(new Date()) + noHZ);

            String noHZ = PublicMethod.IdCreate(3);
            once.setActualFirstNo(noHZ);
            once.setValueFlag("2");//结净
            once.setUpdateManNo("system");
            once.setUpdateManName("混装结净更新");
            once.setPredictionType("7");
            once.setCreateTime(new Date());
            once.setPredictionCombination(upModel.getPredictionNo() + "," + netDb.getPredictionNo());
            meterWorkCarActualFirstBakMapper.insertSelective(once);

//            String no1 = meterWorkCarActualBakMapper.getNewID(afl);
//            netDb.setActualNo(afl + no1);

            String no1 = PublicMethod.IdCreate(3);
            netDb.setActualNo(no1);

            netDb.setActualFirst2No(no);
            netDb.setActualFirst1No(noHZ);
            if (netDb != null && netDb.getNetWeight() != null) {
                //2021年3月7日 净重保留两位小数；
                BigDecimal net;
                net = new BigDecimal(String.valueOf(netDb.getNetWeight())).setScale(2, BigDecimal.ROUND_HALF_UP);
                netDb.setNetWeight(net);
            }
            netDb.setPredictionType("7");
            netDb.setUploadFlag("1");
            // 拿到结净编号B
            netDb.setPredictionCombination(upModel.getPredictionNo() + "," + netDb.getPredictionNo());
            netDb.setCreateTime(new Date());
            if(netDb.getGrossTime().compareTo(netDb.getTareTime()) >= 0) {
                netDb.setNetSpot3No(netDb.getBaseSpot1No());
                netDb.setNetSpot3Name(netDb.getBaseSpot1Name());
                netDb.setNetManNo(netDb.getGrossManNo());
                netDb.setNetManName(netDb.getGrossManName());
                netDb.setNetTime(netDb.getGrossTime());
                netDb.setCreateTime(netDb.getGrossTime());
                netDb.setCreateManName(netDb.getGrossManName());
            }else {
                netDb.setNetSpot3No(netDb.getBaseSpot2No());
                netDb.setNetSpot3Name(netDb.getBaseSpot2Name());
                netDb.setNetManNo(netDb.getTareManNo());
                netDb.setNetManName(netDb.getTareManName());
                netDb.setNetTime(netDb.getTareTime());
                netDb.setCreateTime(netDb.getTareTime());
                netDb.setCreateManName(netDb.getTareManName());
            }
            meterWorkCarActualBakMapper.insert(netDb);

            //1b.如果receive_remark是H01，拿到委托单号，拿到上一个结净编号A
            //修改上一个委托单

            //修改委托编号
            PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(netDb.getPredictionNo());
            scale.setValueFlag(DbConstants.NET);
            scale.setUpdateTime(new Date());
            scale.setUpdateManNo("system");
            scale.setUpdateManName("混装结净更新");
            scale.setUploadFlag("1");
            scale.setPredictionType("7");
            scale.setPredictionCombination(upModel.getPredictionNo() + "," + netDb.getPredictionNo());
            scale.setMeasureStation(netDb.getNetSpot3No());
            scale.setMeasureStationName(netDb.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(scale);

            //4、存储历史皮重及历史皮重明细信息
            if ("1".equals(saveTare)) {
                MeterWorkCarActualFirstBak oci = new MeterWorkCarActualFirstBak();
                oci.setCarNo(netDb.getCarNo());
                oci.setCreateTime(netDb.getTareTime());
                oci.setBaseSpotNo(netDb.getBaseSpot2No());
                oci.setBaseSpotName(netDb.getBaseSpot2Name());
                oci.setMeterWeight(netDb.getTareWeight());
                oci.setActualFirstNo(netDb.getActualFirst2No());
                doSaveHisTare(oci);
            }



            //5、保存图片
            //SaveImg(secondDb);

            //6、修改计量监控
            updateMonitor(secondDb.getBaseSpotNo());

            //保存日志
            saveNote(note);

            updateMonitorOver(secondDb.getActualFirstNo(), secondDb.getBaseSpotNo());

            //重量类型为毛重则调用接口为
            String flag = "";
            /*
            if("0".equals(secondDb.getWeightType())){
                flag = transferParamForLims(netDb.getCarNo(),netDb.getMatterNo(),netDb.getReceivingUintName(),netDb.getForwardingUnitName(),netDb.getNetWeight().toString(),netDb.getNetTime(),"",netDb.getActualNo());
            }
            //*/

            //补充附加重量的数据
            if (StringUtils.isNotEmpty(secondDb.getAddWgtPk())) {
                insertAddWgtNo(secondDb.getActualFirstNo(), secondDb.getAddWgtPk(), secondDb.getCreateManNo(), secondDb.getCreateManName());
            }



            rm.setData(secondDb.getActualFirstNo());
            rm.setMessage(flag);
            return rm;
        }
        catch (Exception e){
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("500");
            rm.setData(null);
            rm.setMessage(e.getMessage());
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return rm;
        }
    }


    /**
     * 非内倒车辆存储一次计量数据
     *
     * @param oci
     * @param link
     * @param isLock   1 互锁   其它为非互锁  如果是非互锁，且还有预报未使用，则需要留二次计量的信息
     * @param saveTare 0 不保存历史皮，1保存历史皮
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doAddWf(MeterWorkCarActualFirstBak oci, MeterWorkPreLink link, String isLock, String saveTare, MeterMonitorNote note) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setCode("200");
        if (oci == null || link == null) {
            throw new MarkerMetException(500, "参数！！");
        }
        //0、查询预报信息；判断预报车号的车号与前端传进来的车号是不是一样的；2021年5月16日
        PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(oci.getPredictionNo());
        if (StringUtils.isEmpty(pts.getCarNo()) || !pts.getCarNo().equals(oci.getCarNo())) {
            throw new MarkerMetException(500, "预报中的车号【" + pts.getCarNo() + "】与界面车号不一致【" + oci.getCarNo() + "】");
        }

        //1、存储一次计量信息
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String afl = formatter.format(oci.getCreateTime());
//        String no = meterWorkCarActualFirstMapper.getNewID(afl);
//        oci.setActualFirstNo(afl + no);

        String no = PublicMethod.IdCreate(3);
        oci.setActualFirstNo(no);

        oci.setValueFlag("1");
        //计算卸货截止时间
        Map<String, Object> obj = new HashMap<>();
        if (StringUtils.isNotEmpty(oci.getBaseSpotNo())) {
            obj.put("baseSpotNo", oci.getBaseSpotNo());
        }
        if (StringUtils.isNotEmpty(oci.getLoadPointNo())) {
            obj.put("loadPointNo", oci.getLoadPointNo());
        }
        if (oci.getWeightType().equals("0")) {
            List<MeterBaseUnloadTime> list1 = meterBaseUnloadTimeMapper.selectByParameters(obj);
            if (list1.size() == 1) {
                Calendar gc = new GregorianCalendar();
                gc.setTime(oci.getCreateTime());
                gc.add(GregorianCalendar.MINUTE, Integer.parseInt(list1.get(0).getLoadTime().toString()));
                oci.setLoadEndTime(gc.getTime());
            }
        }
        oci.setCreateTime(new Date());
        meterWorkCarActualFirstBakMapper.insertSelective(oci);

        //2、修改预环节为使用
        MeterWorkPreLink mpl = new MeterWorkPreLink();
        mpl.setPreLinkNo(link.getPreLinkNo());
        mpl.setValueFlag("1");
        meterWorkPreLinkMapper.updateByPrimaryKeySelectivePart(mpl);

        //3、获取预流程是否还有未使用的环节，若没有了则修改其状态
        MeterWorkPreLink mplq = new MeterWorkPreLink();
        mplq.setPreProcessNo(link.getPreProcessNo());
        mplq.setValueFlag("0");
        Map<String, Object> map = FieldsCollector.beanToMap(mplq);
        List<MeterWorkPreLink> list = meterWorkPreLinkMapper.selectByParameters(map);
        if (list.size() == 0) {
            MeterWorkPreProcess mpp = new MeterWorkPreProcess();
            mpp.setPreProcessNo(link.getPreProcessNo());
            mpp.setValueFlag("2");
            meterWorkPreProcessMapper.updateByPrimaryKeySelective(mpp);
        }

        //4、修改预报状态  Value_flag 0：未配车；1：未使用；2：已使用；3：已作废；4：正使用
        PreTrackScale ptsU = new PreTrackScale();
        ptsU.setPredictionNo(oci.getPredictionNo());
        if (pts.getPredictionType().equals("2")) {//联运预报
            if ("1".equals(isLock)) {
                ptsU.setValueFlag("4");//一车联运的话，互锁扫码时会将预报改为正使用 下次扫后面的码
            } else {
                ptsU.setValueFlag("2");//一车联运的话，非互锁扫码时预报改为已使用  下次还是扫相同码
            }
        } else {
            //5、存储历史皮重及历史皮重明细信息
            if ("1".equals(saveTare)) {
                doSaveHisTare(oci);
            }
            ptsU.setValueFlag("4");//非一车联运的话，扫码时预报改为正使用  下次还是扫相同码
        }
        ptsU.setUploadFlag("1");
        ptsU.setMeasureStation(oci.getBaseSpotNo());
        ptsU.setMeasureStationName(oci.getBaseSpotName());
        preTrackScaleMapper.updateByPrimaryKeySelective(ptsU);

        //6、存储图片
        //SaveImg(oci);

        //7、修改计量监控
        updateMonitor(oci.getBaseSpotNo());

        //8、修改监控的计量作业编号
        updateMonitorOver(oci.getActualFirstNo(), oci.getBaseSpotNo());

        //9、批次预报重新生成一套预环节   若为批次预报且还未用完
        if (pts.getPredictionType().equals("0") || pts.getPredictionType().equals("3")) {
            meterWorkPreLinkService.batchReset(oci.getPredictionNo());
        }
        //信息加密
        String str = oci.getActualFirstNo() + "," + oci.getMeterWeight() + "," + "1";
        String enStr = encryption(str);

        //保存日志
        saveNote(note);

        //补充附加重量的数据
        if (StringUtils.isNotEmpty(oci.getAddWgtPk())) {
            insertAddWgtNo(oci.getActualFirstNo(), oci.getAddWgtPk(), oci.getCreateManNo(), oci.getCreateManName());
        }

        // eason add 2021-11-25 若进入到这里则要作废期限皮重数据，因为这边如果可以留期限皮则不会留1次皮重计量数据
        if(!oci.getWeightType().equals("0")){
            cancelTermTare(oci.getCarNo());
        }

        rm.setData(oci.getActualFirstNo());
        rm.setMessage(enStr);
        return rm;
    }

    /**
     * 作废期限皮重数据
     */
    private void cancelTermTare(String carNo){
        HashMap<String,Object> mp=new HashMap<>();
        mp.put("carNo",carNo);
        mp.put("valueFlag","0");
        meterBaseTermTareDataMapper.cancelTermTare(mp);
    }

    /**
     * 非内倒车辆结净
     *
     * @param secondDb 二次计量数据
     * @param netDb    净重数据
     * @param link     预报数据
     * @param isLock   1 互锁   其它为非互锁  如果是非互锁，且还有预报未使用，则需要留二次计量的信息
     * @param saveTare 0 不保存历史皮，1保存历史皮
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(value = "tm", rollbackFor = Exception.class)
    public RESTfulResult doAddNetWf(MeterWorkCarActualFirstBak secondDb, MeterWorkCarActualBak netDb, MeterMonitorNote note, MeterWorkPreLink link, String isLock, String saveTare) throws Exception {
        String lyFirstNo = "";//联运自动生成的一次计量编号
        RESTfulResult rm = new RESTfulResult();
        rm.setCode("200");
        //先格式化时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

        //0、查询预报信息；判断预报车号的车号与前端传进来的车号是不是一样的；2021年5月16日
        PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(secondDb.getPredictionNo());
        if (StringUtils.isEmpty(pts.getCarNo()) || !pts.getCarNo().equals(secondDb.getCarNo())) {
            throw new MarkerMetException(500, "预报中的车号【" + pts.getCarNo() + "】与界面车号不一致【" + secondDb.getCarNo() + "】");
        }

        //1、存储当前计量的数据  1.1先给一个初始状态
        secondDb.setValueFlag("2");//结净

        //2、修改预环节为使用 2.1修改预环节
        MeterWorkPreLink mpl = new MeterWorkPreLink();
        mpl.setPreLinkNo(link.getPreLinkNo());
        mpl.setValueFlag("1");
        meterWorkPreLinkMapper.updateByPrimaryKeySelectivePart(mpl);

        //2、获取预流程是否还有未使用的环节，若没有了则修改其状态  2.2修改预流程
        MeterWorkPreLink mplq = new MeterWorkPreLink();
        mplq.setPreProcessNo(link.getPreProcessNo());
        mplq.setValueFlag("0");
        Map<String, Object> map = FieldsCollector.beanToMap(mplq);
        List<MeterWorkPreLink> list = meterWorkPreLinkMapper.selectByParameters(map);
        if (list.size() == 0) {
            MeterWorkPreProcess mpp = new MeterWorkPreProcess();
            mpp.setPreProcessNo(link.getPreProcessNo());
            mpp.setValueFlag("2");
            meterWorkPreProcessMapper.updateByPrimaryKeySelective(mpp);
        }

        //3、存储历史皮重及历史皮重明细信息  3.1判断皮重信息
        if (secondDb.getMeterWeight().equals(netDb.getTareWeight())) {
            //二次重量如果跟皮重一样
            netDb.setActualFirst2No(secondDb.getActualFirstNo());
        } else {
            netDb.setActualFirst1No(secondDb.getActualFirstNo());
        }
        //3、存储历史皮重及历史皮重明细信息  3.2判断需要保存皮重且不是一车联运预报
        if (!pts.getPredictionType().equals("2")) {//不是一车联运预报
            if ("1".equals(saveTare)) {
                MeterWorkCarActualFirstBak oci = new MeterWorkCarActualFirstBak();
                oci.setCarNo(netDb.getCarNo());
                oci.setCreateTime(netDb.getTareTime());
                oci.setBaseSpotNo(netDb.getBaseSpot2No());
                oci.setBaseSpotName(netDb.getBaseSpot2Name());
                oci.setMeterWeight(netDb.getTareWeight());
                oci.setActualFirstNo(netDb.getActualFirst2No());
                doSaveHisTare(oci);
            }
        }

        //4、修改预报信息  包括处理各类预报类型（一车联运多新增一条作为皮重的信息）
        PreTrackScale ptsU = new PreTrackScale();
        ptsU.setPredictionNo(pts.getPredictionNo());
        if (pts.getPredictionType().equals("0")) {
            ptsU.setValueFlag("4");//正使用
            //批次预报 有时间，次数，重量限制
            if (pts.getUsedType().equals("0")) {  //0次数  1时间  2总量
                //次数
                ptsU.setMeterNum(pts.getMeterNum() == null ? 1 : (pts.getMeterNum() + 1));
                if (pts.getEffectiveNum().equals(ptsU.getMeterNum())) {//如果两个值相等
                    ptsU.setValueFlag("2");//用完了则状态改为2
                }
            } else if (pts.getUsedType().equals("1")) {
                //1时间
                if (!"2".equals(pts.getValueFlag()) && pts.getEffectiveEndTime().compareTo(new Date()) <= 0) {
                    ptsU.setValueFlag("2");//当前状态不为2，且结束时间小于当时间
                }
            } else if (pts.getUsedType().equals("2")) {
                //总量
                ptsU.setMeterWeight(pts.getMeterWeight() == null ? netDb.getNetWeight() : (pts.getMeterWeight().add(netDb.getNetWeight())));
                if (pts.getEffectiveWeight().doubleValue() <= ptsU.getMeterWeight().doubleValue()) {//如果总重量小于等于检斤重量则用完了
                    ptsU.setValueFlag("2");//用完了则状态改为2
                }
            }
            ptsU.setUploadFlag("1");
            ptsU.setMeasureStation(netDb.getNetSpot3No());
            ptsU.setMeasureStationName(netDb.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(ptsU);
        } else if (pts.getPredictionType().equals("1")) {
            //单次预报 使用1次后将状态设置为已使用
            ptsU.setValueFlag("2");
            ptsU.setUploadFlag("1");
            ptsU.setMeasureStation(netDb.getNetSpot3No());
            ptsU.setMeasureStationName(netDb.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(ptsU);
        } else if (pts.getPredictionType().equals("2")) {
            //联运预报 使用1次，但是根据车辆的环节中若是 互锁式则直接结净，否则则看是否还有其它的预报未使用
            //若存在其它未使用的预报则需要留一个二次计量的数据作为下一车的一次计量信息
            if ("1".equals(isLock)) { //互锁搞法：与单次的先皮后毛流程一样
                ptsU.setValueFlag("2"); //修改预报状态为正使用
                ptsU.setUploadFlag("1");
                ptsU.setMeasureStation(netDb.getNetSpot3No());
                ptsU.setMeasureStationName(netDb.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(ptsU);
            } else {
                MeterWorkCarActualFirstBak mwaf = new MeterWorkCarActualFirstBak();
                String two = formatter.format(secondDb.getCreateTime());
//                String twoNo = meterWorkCarActualFirstMapper.getNewID(two);
//                mwaf.setActualFirstNo(two + twoNo);

                String twoNo = PublicMethod.IdCreate(3);
                mwaf.setActualFirstNo(twoNo);

                //非互锁式 value_flag：1未使用 2已使用状态
                PreTrackScale ptsQ = new PreTrackScale();
                ptsQ.setValueFlag("1");
                ptsQ.setPredictionCombination(secondDb.getPredictionCombination());
                Map<String, Object> map1 = FieldsCollector.beanToMap(ptsQ);
                List<PreTrackScale> lscale = preTrackScaleMapper.selectByParameters(map1);

                int iCount = 0;
                for (PreTrackScale pt : lscale) {
                    //由于预报都已使用的话，不可能进入到服务端而应该在客户端就被抛出异常了，所以这里只可能是还存在未使用的预报
                    //存在未使用，且当前计量编号与未使用编号不一致的情况，那么表示后面还有其他的预报，否则则说明这已经是最后一次计量了
                    if (!secondDb.getPredictionNo().equals(pt.getPredictionNo())) {
                        ++iCount;
                    }
                }
                if (iCount == 0) {
                    //存在未使用预报，但是预报就是当前预报自身，说明已经是最后一车计量了
                    //最后一个预报了
                    //结净的编号与当前计量的编号不一致则说明当前计量扫的是另外一个码需给结净的数据补充一条计量数据，否则则不用补
                    if (!netDb.getPredictionNo().equals(secondDb.getPredictionNo())) {
                        MeterWorkCarActualFirstBak mwafQ = new MeterWorkCarActualFirstBak();
                        mwafQ.setPredictionNo(netDb.getPredictionNo());
                        mwafQ.setValueFlag("1");
                        Map<String, Object> map2 = FieldsCollector.beanToMap(mwafQ);
                        List<MeterWorkCarActualFirstBak> lmwaf = meterWorkCarActualFirstBakMapper.selectByParameters(map2);
                        if (lmwaf.size() <= 0) {
                            throw new MarkerMetException(500, "操作失败！！");
                        }
                        //给结净数据补充完整二次计量数据(皮重数据)
                        mwaf.setPredictionNo(lmwaf.get(0).getPredictionNo());
                        mwaf.setNoticeNo(lmwaf.get(0).getNoticeNo());
                        mwaf.setCarNo(lmwaf.get(0).getCarNo());
                        mwaf.setTrailerNo(lmwaf.get(0).getTrailerNo());
                        mwaf.setMatterNo(lmwaf.get(0).getMatterNo());
                        mwaf.setMatterName(lmwaf.get(0).getMatterName());
                        mwaf.setContractNo(lmwaf.get(0).getContractNo());
                        mwaf.setBatchNo(lmwaf.get(0).getBatchNo());
                        mwaf.setCustomerSupplierNo(lmwaf.get(0).getCustomerSupplierNo());
                        mwaf.setCustomerSupplierName(lmwaf.get(0).getCustomerSupplierName());
                        mwaf.setForwardingUnitNo(lmwaf.get(0).getForwardingUnitNo());
                        mwaf.setForwardingUnitName(lmwaf.get(0).getForwardingUnitName());
                        mwaf.setReceivingUintNo(lmwaf.get(0).getReceivingUintNo());
                        mwaf.setReceivingUintName(lmwaf.get(0).getReceivingUintName());
                        mwaf.setMaterialNo(lmwaf.get(0).getMaterialNo());
                        mwaf.setMaterialName(lmwaf.get(0).getMaterialName());
                        mwaf.setSpecNo(lmwaf.get(0).getSpecNo());
                        mwaf.setSpecName(lmwaf.get(0).getSpecName());
                        mwaf.setShipmentGrossWeight(lmwaf.get(0).getShipmentGrossWeight());
                        mwaf.setShipmentNetWeight(lmwaf.get(0).getShipmentNetWeight());
                        mwaf.setShipmentNum(lmwaf.get(0).getShipmentNum());
                        mwaf.setBindCardNo(lmwaf.get(0).getBindCardNo());
                        mwaf.setLoadPointNo(lmwaf.get(0).getLoadPointNo());
                        mwaf.setLoadPointName(lmwaf.get(0).getLoadPointName());
                        mwaf.setSampleNo(lmwaf.get(0).getSampleNo());
                        mwaf.setSampleVoucher(lmwaf.get(0).getSampleVoucher());
                        mwaf.setCarrierUnitNo(lmwaf.get(0).getCarrierUnitNo());
                        mwaf.setCarrierUnitName(lmwaf.get(0).getCarrierUnitName());
                        mwaf.setPredictionType(lmwaf.get(0).getPredictionType());
                        mwaf.setPredictionCombination(lmwaf.get(0).getPredictionCombination());
                        mwaf.setMeterTypeNo(lmwaf.get(0).getMeterTypeNo());
                        mwaf.setRailwayNo(lmwaf.get(0).getRailwayNo());
                        mwaf.setMeterTypeName(lmwaf.get(0).getMeterTypeName());
                        mwaf.setMeterProcessEditionNo(lmwaf.get(0).getMeterProcessEditionNo());
                        mwaf.setMeterWeight(secondDb.getMeterWeight());//当前重量
                        mwaf.setAddWeight(new BigDecimal(0));//皮重补充附加重量为0
                        mwaf.setMeterNum(lmwaf.get(0).getMeterNum());
                        mwaf.setWeightType(lmwaf.get(0).getWeightType().equals("0") ? "1" : "0"); //0=毛重；1=常规皮重；2=期限皮重；3=历史皮重；4=标识皮重；5=自重皮重
                        mwaf.setCreateManNo(secondDb.getCreateManNo());
                        mwaf.setCreateManName(secondDb.getCreateManName());
                        mwaf.setCreateTime(secondDb.getCreateTime());
                        mwaf.setMeterClass(secondDb.getMeterClass());
                        mwaf.setMeterGroup(secondDb.getMeterGroup());
                        //mwaf.setWeight_type(secondDb.getWeight_type().equals("0")?"1":"0");//0=毛重；1=常规皮重；2=期限皮重；3=历史皮重；4=标识皮重；5=自重皮重
                        mwaf.setBaseSpotNo(secondDb.getBaseSpotNo());
                        mwaf.setBaseSpotName(secondDb.getBaseSpotName());
                        mwaf.setValueFlag("2");//0=有效，1=作废，2=结净
                        mwaf.setCheckFlag("0");
                        mwaf.setMeterMode(secondDb.getMeterMode());//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                        mwaf.setMemo("一车联运自动生成一次计量信息1");
                        //存储补充的需结净的数据
                        meterWorkCarActualFirstBakMapper.insertSelective(mwaf);
                        lyFirstNo = mwaf.getActualFirstNo();

                        //2021年3月20日；杨秀东对前端传来的实体进行处理;处理state；处理预报
                        secondDb.setWeightType(mwaf.getWeightType().equals("0") ? "1" : "0");
                        PreTrackScale lyScale = new PreTrackScale();
                        lyScale.setPredictionNo(secondDb.getPredictionNo());
                        lyScale.setValueFlag("4");
                        lyScale.setUploadFlag("1");
                        lyScale.setMeasureStation(secondDb.getBaseSpotNo());
                        lyScale.setMeasureStationName(secondDb.getBaseSpotName());
                        preTrackScaleMapper.updateByPrimaryKeySelective(lyScale);

                        //查找到的上个毛重写为结净
                        lmwaf.get(0).setValueFlag("2");//结净
                        meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(lmwaf.get(0));

                        //设置毛重数据数据为有效而非结净
                        secondDb.setValueFlag("1");//有效

                        //将结净数据中的毛重编号修改
                        if (mwaf.getMeterWeight().doubleValue() == netDb.getTareWeight().doubleValue()) {
                            //若此次补充的一次数据重量与结净中的皮重相同,则将这个编号赋值给结净数据的皮重编号
                            netDb.setActualFirst2No(mwaf.getActualFirstNo());
                        } else {
                            netDb.setActualFirst1No(mwaf.getActualFirstNo());
                        }
                    } else {
                        Map<String, Object> hm = new HashMap<>();
                        hm.put("predictionNo", netDb.getPredictionNo());
                        hm.put("valueFlag", "1");
                        List<MeterWorkCarActualFirstBak> lmwaf = meterWorkCarActualFirstBakMapper.selectByParameters(hm);
                        if (lmwaf.size() <= 0) {
                            throw new MarkerMetException(500, "操作失败！！");
                        }
                        //查找到的上个毛重写为结净
                        lmwaf.get(0).setValueFlag("2");//结净
                        meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(lmwaf.get(0));
                        //已经是最后一个预报的最后一次结净了
                        ptsU.setValueFlag("2"); //修改预报状态
                        ptsU.setUploadFlag("1");
                        ptsU.setMeasureStation(lmwaf.get(0).getBaseSpotNo());
                        ptsU.setMeasureStationName(lmwaf.get(0).getBaseSpotName());
                        preTrackScaleMapper.updateByPrimaryKeySelective(ptsU);
                    }
                } else {
                    //说明后续还有预报，此时直接将这个预报状态修改为2，同时为当前结净补充一条计量皮重数据
                    Map<String, Object> map3 = new HashMap<>();
                    map3.put("predictionNo", netDb.getPredictionNo());
                    map3.put("valueFlag", "1");
                    List<MeterWorkCarActualFirstBak> lmwaf = meterWorkCarActualFirstBakMapper.selectByParameters(map3);
                    if (lmwaf.size() <= 0) {
                        throw new MarkerMetException(500, "操作失败！！");
                    }
                    //给结净数据补充完整二次计量数据
                    mwaf.setPredictionNo(lmwaf.get(0).getPredictionNo());
                    mwaf.setNoticeNo(lmwaf.get(0).getNoticeNo());
                    mwaf.setCarNo(lmwaf.get(0).getCarNo());
                    mwaf.setTrailerNo(lmwaf.get(0).getTrailerNo());
                    mwaf.setMatterNo(lmwaf.get(0).getMatterNo());
                    mwaf.setMatterName(lmwaf.get(0).getMatterName());
                    mwaf.setContractNo(lmwaf.get(0).getContractNo());
                    mwaf.setBatchNo(lmwaf.get(0).getBatchNo());
                    mwaf.setCustomerSupplierNo(lmwaf.get(0).getCustomerSupplierNo());
                    mwaf.setCustomerSupplierName(lmwaf.get(0).getCustomerSupplierName());
                    mwaf.setForwardingUnitNo(lmwaf.get(0).getForwardingUnitNo());
                    mwaf.setForwardingUnitName(lmwaf.get(0).getForwardingUnitName());
                    mwaf.setReceivingUintNo(lmwaf.get(0).getReceivingUintNo());
                    mwaf.setReceivingUintName(lmwaf.get(0).getReceivingUintName());
                    mwaf.setMaterialNo(lmwaf.get(0).getMaterialNo());
                    mwaf.setMaterialName(lmwaf.get(0).getMaterialName());
                    mwaf.setSpecNo(lmwaf.get(0).getSpecNo());
                    mwaf.setSpecName(lmwaf.get(0).getSpecName());
                    mwaf.setShipmentGrossWeight(lmwaf.get(0).getShipmentGrossWeight());
                    mwaf.setShipmentNetWeight(lmwaf.get(0).getShipmentNetWeight());
                    mwaf.setShipmentNum(lmwaf.get(0).getShipmentNum());
                    mwaf.setBindCardNo(lmwaf.get(0).getBindCardNo());
                    mwaf.setLoadPointNo(lmwaf.get(0).getLoadPointNo());
                    mwaf.setLoadPointName(lmwaf.get(0).getLoadPointName());
                    mwaf.setSampleNo(lmwaf.get(0).getSampleNo());
                    mwaf.setSampleVoucher(lmwaf.get(0).getSampleVoucher());
                    mwaf.setCarrierUnitNo(lmwaf.get(0).getCarrierUnitNo());
                    mwaf.setCarrierUnitName(lmwaf.get(0).getCarrierUnitName());
                    mwaf.setPredictionType(lmwaf.get(0).getPredictionType());
                    mwaf.setPredictionCombination(lmwaf.get(0).getPredictionCombination());
                    mwaf.setMeterTypeNo(lmwaf.get(0).getMeterTypeNo());
                    mwaf.setRailwayNo(lmwaf.get(0).getRailwayNo());
                    mwaf.setMeterTypeName(lmwaf.get(0).getMeterTypeName());
                    mwaf.setMeterProcessEditionNo(lmwaf.get(0).getMeterProcessEditionNo());
                    mwaf.setMeterWeight(secondDb.getMeterWeight());//当前重量
                    mwaf.setAddWeight(new BigDecimal(0));//皮重补充附加重量为0
                    mwaf.setMeterNum(lmwaf.get(0).getMeterNum());
                    mwaf.setWeightType(lmwaf.get(0).getWeightType().equals("0") ? "1" : "0");
                    mwaf.setCreateManNo(secondDb.getCreateManNo());
                    mwaf.setCreateManName(secondDb.getCreateManName());
                    mwaf.setCreateTime(secondDb.getCreateTime());
                    mwaf.setMeterClass(secondDb.getMeterClass());
                    mwaf.setMeterGroup(secondDb.getMeterGroup());
                    //mwaf.setWeight_type(secondDb.getWeight_type().equals("0")?"1":"0");//0=毛重；1=常规皮重；2=期限皮重；3=历史皮重；4=标识皮重；5=自重皮重
                    mwaf.setBaseSpotNo(secondDb.getBaseSpotNo());
                    mwaf.setBaseSpotName(secondDb.getBaseSpotName());
                    mwaf.setValueFlag("2");//0=有效，1=作废，2=结净
                    mwaf.setCheckFlag("0");
                    mwaf.setMeterMode(secondDb.getMeterMode());
                    mwaf.setMemo("一车联运自动生成一次计量信息2");
                    //存储补充第二次计量的需结净的皮重数据
                    meterWorkCarActualFirstBakMapper.insertSelective(mwaf);
                    lyFirstNo = mwaf.getActualFirstNo();

                    ptsU.setValueFlag("2"); //修改当前预报状态
                    ptsU.setUploadFlag("1");
                    ptsU.setMeasureStation(mwaf.getBaseSpotNo());
                    ptsU.setMeasureStationName(mwaf.getBaseSpotName());
                    preTrackScaleMapper.updateByPrimaryKeySelective(ptsU);

                    //查找到的上个毛重写为结净
                    lmwaf.get(0).setValueFlag("2");//结净
                    meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(lmwaf.get(0));

                    //将本次的这一个毛重写为有效
                    secondDb.setValueFlag("1");//有效
                    //=========eason add 2021-03-30==============
                    secondDb.setWeightType(mwaf.getWeightType().equals("0") ? "1" : "0");
                    //===========================================

                    //将结净数据中的毛重编号修改
                    if (mwaf.getMeterWeight().doubleValue() == netDb.getTareWeight().doubleValue()) {
                        //若此次补充的一次数据重量与结净中的皮重相同,则将这个编号赋值给结净数据的皮重编号
                        netDb.setActualFirst2No(mwaf.getActualFirstNo());
                    } else {
                        netDb.setActualFirst1No(mwaf.getActualFirstNo());
                    }
                }
            }
        } else if (pts.getPredictionType().equals("3") && StringUtils.isNotEmpty(pts.getRailwayNo())) {
            //===================火车分检预报处理开始==========================
            //分检预报 有效次数为1；每次计量需要更新火车的重量,前提是火车车号不允许为空***************
            //1.找到轨道衡的同一车的所有数据；重量类型weightType为毛重；状态位为（0,2）的数据(xml中默认)
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("railwayNo", pts.getRailwayNo());
            obj.put("weightType", "0");
            List<MeterWorkRailwayActFirst> mwrafs = meterWorkRailwayActFirstMapper.selectBy(obj);
            if (mwrafs != null && mwrafs.size() > 0) {
                MeterWorkRailwayActFirst meterWorkRailwayActFirst = mwrafs.get(0);
                countForRail(meterWorkRailwayActFirst, netDb.getNetWeight()); //一次计量数据，净重数据
            }

            ptsU.setValueFlag("2");//预报完结
            ptsU.setUploadFlag("1");
            ptsU.setMeasureStation(mwrafs.get(0).getBaseSpotNo());
            ptsU.setMeasureStationName(mwrafs.get(0).getBaseSpotName());
            preTrackScaleMapper.updateByPrimaryKeySelective(ptsU);
            //===================火车分检预报处理结束==========================
        }

        //5、存储当前计量的数据
        String afl = formatter.format(secondDb.getCreateTime());
//        String iActualNo = meterWorkCarActualFirstMapper.getNewID(afl);
//        secondDb.setActualFirstNo(afl + iActualNo);

        String iActualNo = PublicMethod.IdCreate(3);
        secondDb.setActualFirstNo(iActualNo);

        secondDb.setCreateTime(new Date());
        meterWorkCarActualFirstBakMapper.insertSelective(secondDb);
        if (!secondDb.getPredictionNo().equals(netDb.getPredictionNo())) {
            //杨秀东2021-3-22；一车联运检化验处理;
            // 若一次计量中的预报编号与二次计量的编号不一致，则说明是一车联运；直接新增一条检化验接口数据
        }

        //一次计量的数据修改状态
        MeterWorkCarActualFirstBak firstUpdate = new MeterWorkCarActualFirstBak();

        //6、存储结净数据
//        String afl1 = formatter.format(netDb.getNetTime());
//        String no = meterWorkCarActualBakMapper.getNewID(afl1);
//        netDb.setActualNo(afl + no); //注意：此处生成的编号是下一次的编号；如果是一车联运不能直接返回给客户端这个编号

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

        if (secondDb.getMeterWeight().equals(netDb.getTareWeight())) {
            //二次指哪个了如果跟皮重一样
            if (StringUtils.isEmpty(netDb.getActualFirst2No()) || netDb.getActualFirst2No().length() < 10)
                netDb.setActualFirst2No(secondDb.getActualFirstNo()); //皮重编号
            firstUpdate.setActualFirstNo(netDb.getActualFirst1No());
        } else {
            if (StringUtils.isEmpty(netDb.getActualFirst1No()) || netDb.getActualFirst1No().length() < 10)
                netDb.setActualFirst1No(secondDb.getActualFirstNo()); //毛重编号
            firstUpdate.setActualFirstNo(netDb.getActualFirst2No());
        }
        if (netDb != null && netDb.getNetWeight() != null) {
            //2021年3月7日 净重保留两位小数；
            BigDecimal net;
            net = new BigDecimal(String.valueOf(netDb.getNetWeight())).setScale(2, BigDecimal.ROUND_HALF_UP);
            netDb.setNetWeight(net);
        }
        netDb.setUploadFlag("1");
        netDb.setCreateTime(new Date());
        if(netDb.getGrossTime().compareTo(netDb.getTareTime()) >= 0) {
            netDb.setNetSpot3No(netDb.getBaseSpot1No());
            netDb.setNetSpot3Name(netDb.getBaseSpot1Name());
            netDb.setNetManNo(netDb.getGrossManNo());
            netDb.setNetManName(netDb.getGrossManName());
            netDb.setNetTime(netDb.getGrossTime());
            netDb.setCreateTime(netDb.getGrossTime());
            netDb.setCreateManName(netDb.getGrossManName());
        }else {
            netDb.setNetSpot3No(netDb.getBaseSpot2No());
            netDb.setNetSpot3Name(netDb.getBaseSpot2Name());
            netDb.setNetManNo(netDb.getTareManNo());
            netDb.setNetManName(netDb.getTareManName());
            netDb.setNetTime(netDb.getTareTime());
            netDb.setCreateTime(netDb.getTareTime());
            netDb.setCreateManName(netDb.getTareManName());
        }
        meterWorkCarActualBakMapper.insertSelective(netDb);

        //7、修改第一次存储的计量数据状态为结净;预报编号相同时才处理（不是一车联运非互锁式）
        if (firstUpdate.getActualFirstNo() != null
                && netDb.getPredictionNo() != null && secondDb.getPredictionNo() != null
                && netDb.getPredictionNo().equals(secondDb.getPredictionNo())) {
            firstUpdate.setValueFlag("2");
            if (secondDb.getWeightType().equals("0"))//第二次为毛重
            {
                firstUpdate.setWeightType("1");//第一次则为皮重
            } else {
                firstUpdate.setWeightType("0");//否则第一次为毛重
            }
            meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(firstUpdate);
        }


        //8、修改结净的预环节为已使用
        Map<String, Object> updateLink = new HashMap<>();
        updateLink.put("predictionNo", netDb.getPredictionNo());
        updateLink.put("linkCode", "001021005");
        List<MeterWorkPreLink> mwpls = meterWorkPreLinkMapper.selectByParameters(updateLink);
        if (mwpls != null && mwpls.size() > 0) {
            mwpls.get(0).setValueFlag("1");//已使用
            meterWorkPreLinkMapper.updateByPrimaryKeySelective(mwpls.get(0));
        }


        //9、批次预报重新生成一套预环节   若为批次预报且还未用完
        if (pts.getPredictionType().equals("0")) {
            meterWorkPreLinkService.batchReset(netDb.getPredictionNo());
        }
        //yxd 2021年7月6日 对一车联运返回给客户端的的二次编号做处理
        if (pts.getPredictionType().equals("2") && StringUtils.isNotEmpty(lyFirstNo)) {
            secondDb.setActualFirstNo(lyFirstNo);
        }

        //10、存储图片
        //SaveImg(secondDb);

        //11、修改计量监控
        updateMonitor(secondDb.getBaseSpotNo());

        //12、修改监控的计量作业编号
        updateMonitorOver(secondDb.getActualFirstNo(), secondDb.getBaseSpotNo());


        //信息加密
        String str = netDb.getActualNo() + "," + netDb.getNetWeight() + "," + "2";
        String enStr = encryption(str);

        //保存日志
        saveNote(note);

        rm.setData(secondDb.getActualFirstNo());
        rm.setMessage(enStr);
        return rm;
    }


    public void updateMonitor(String baseSpotNo) {
        MeterWorkMonitor mwm = meterWorkMonitorMapper.selectByPrimaryKey(baseSpotNo);
        MeterWorkOnline mwo = meterWorkOnlineMapper.selectByPrimaryKey(mwm.getUserNo());
        //修改计量监控
        /*mwm.setUserNo("");
        meterWorkMonitorMapper.updateByPrimaryKeyNotUserNo(mwm);
        //修改在线状态
        if (mwo != null) {
            mwo.setWorkFlag("0");//未接管
            meterWorkOnlineMapper.updateByPrimaryKey(mwo);
        }*/
    }

    public void updateMonitorOver(String actualFirstNo, String baseSpotNo) {
        if (StringUtils.isEmpty(actualFirstNo)) {
            throw new MarkerMetException(500, "修改监控状态时，计量作业编号为空");
        }
        if (StringUtils.isEmpty(baseSpotNo)) {
            throw new MarkerMetException(500, "修改监控状态时，计量点编号为空");
        }
        MeterWorkMonitor mwm = meterWorkMonitorMapper.selectByPrimaryKey(baseSpotNo);
        mwm.setIsOverWgt(actualFirstNo);
        meterWorkMonitorMapper.updateByPrimaryKeySelective(mwm);
    }

    @Override
    public String encryption(String str) throws Exception {
        String deStr = AESUtil.encrypt(str, "abcdefghijklmnop", "0000000000000000");
        return deStr;
    }

    @Override
    public String encryptionTest(String carNo) throws Exception {
//        String deStr = AESUtil.encrypt(str, "abcdefghijklmnop", "0000000000000000");
//        return deStr;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmm");
        String afl = sdf.format(new Date());
        String message = carNo + "jisco2022" + afl;
        String key = "jiscosec";
        String deStr = DESUtil.toHexString(DESUtil.encrypt(message, key));
        //byte[] deStr = DESUtil.encrypt(message, key);
//        return deStr.toString();s
//        return new String(deStr);
        return deStr;
    }

    @Override
    public RESTfulResult decryptionTest(String str) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            String key = "jiscosec";
            String destr;
            try{
                destr = DESUtil.decrypt(str, key);
            }catch(Exception e) {
                rm.setCode("9");
                rm.setMessage("密文错误");
                return rm;
            }
            String carNo = destr.substring(0, 7);
            String time = destr.substring(16);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            SimpleDateFormat sdf01 = new SimpleDateFormat("yyyyMMddHHmm");
            Date date = sdf01.parse(time);
            Calendar beforeTime = Calendar.getInstance();
            beforeTime.add(Calendar.MINUTE, -5);// 5分钟之前的时间
            Date startTime = sdf.parse(sdf.format(beforeTime.getTime()));
            Date endTime = sdf.parse(sdf.format(new Date()));

            if(!(date.compareTo(startTime) >= 0 && date.compareTo(endTime) <= 0)) {
                rm.setMessage("二维码已过期");
                rm.setCode("1");
                return rm;
            }

            String carnumRegex = "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领 A-Z]{1}[A-Z]{1}(([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领 A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9 挂学警港澳]{1})";
            if (!TextUtils.isEmpty(carNo)) {
                if(carNo.matches(carnumRegex)) {
                    rm.setSucceed();
                    rm.setCode("0");
                    rm.setMessage("操作成功");
                    rm.setData(carNo);
                }else {
                    rm.setMessage("车号不正确");
                    rm.setCode("2");
                }
            }else {
                rm.setMessage("车号为空");
                rm.setCode("2");
            }
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常>>>" + e.getMessage());
        }
        return rm;
    }

    //两条轨道衡计量实绩的替换
    private MeterWorkRailwayActFirst change(MeterWorkRailwayActFirst one, MeterWorkRailwayActFirst two) {
        //1、为有值的数据，2为0的值  进行替换
        //先判断是否需要修改结净
        if (one.getValueFlag().equals("2")) {
            //已经结净
            //有值的结净数据
            Map<String, Object> map1 = new HashMap<>();
            map1.put("predictionNo", one.getPredictionNo());
            map1.put("valueFlag", "0");
            map1.put("uploadFlag", "0");
            List<MeterWorkRailwayActual> mwra1 = meterWorkRailwayActualMapper.selectByParameters(map1);
            //为0的结净
            Map<String, Object> map2 = new HashMap<>();
            map2.put("predictionNo", two.getPredictionNo());
            map2.put("valueFlag", "0");
            map2.put("uploadFlag", "0");
            List<MeterWorkRailwayActual> mwra2 = meterWorkRailwayActualMapper.selectByParameters(map2);
            mwra2.get(0).setNetWeight(mwra1.get(0).getNetWeight());
            mwra2.get(0).setTareWeight(mwra1.get(0).getTareWeight());
            mwra2.get(0).setGrossWeight(mwra1.get(0).getGrossWeight());
            mwra1.get(0).setNetWeight(new BigDecimal(0));
            mwra1.get(0).setTareWeight(new BigDecimal(0));
            mwra1.get(0).setGrossWeight(new BigDecimal(0));
            //修改数据
            meterWorkRailwayActualMapper.updateByPrimaryKeySelective(mwra1.get(0));
            meterWorkRailwayActualMapper.updateByPrimaryKeySelective(mwra2.get(0));
        }
        two.setMeterBalanceWeight(one.getMeterBalanceWeight());
        two.setMeterWeight(one.getMeterWeight());
        two.setMeterSortWeight(one.getMeterSortWeight());
        meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(two);
        one.setMeterSortWeight(new BigDecimal(0));
        one.setMeterWeight(new BigDecimal(0));
        one.setMeterBalanceWeight(new BigDecimal(0));
        meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(one);
        return two;
    }

    /**
     * 改写轨道衡实绩的值（若结净，还要改写结净的值）
     *
     * @param one
     * @param netWeight
     */
    private void countForRail(MeterWorkRailwayActFirst one, BigDecimal netWeight) {
        //有值的数据
        one.setMeterSortWeight(one.getMeterSortWeight().add(netWeight));
        one.setMeterBalanceWeight(one.getMeterWeight().subtract(one.getMeterSortWeight()));
        one.setMemo(one.getMemo() + "分检重量：" + netWeight + ";");
        meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(one);
        if (one.getValueFlag().equals("2")) {
            //修改结净
            Map<String, Object> map = new HashMap<>();
            map.put("predictionNo", one.getPredictionNo());
            map.put("valueFlag", "0");
            map.put("uploadFlag", "0");
            List<MeterWorkRailwayActual> mwra1 = meterWorkRailwayActualMapper.selectByParameters(map);
            MeterWorkRailwayActual meterWorkRailwayActual = new MeterWorkRailwayActual();
            meterWorkRailwayActual.setActualNo(mwra1.get(0).getActualNo());
            meterWorkRailwayActual.setGrossWeight(one.getMeterBalanceWeight());
            meterWorkRailwayActual.setNetWeight(one.getMeterBalanceWeight().subtract(mwra1.get(0).getTareWeight()));
            meterWorkRailwayActual.setMemo(meterWorkRailwayActual.getMemo() + "分检重量：" + netWeight + ";");
            meterWorkRailwayActualMapper.updateByPrimaryKeySelective(meterWorkRailwayActual);
        }
    }

    private String saveNote(MeterMonitorNote note) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String afl = formatter.format(new Date());
        String no = meterMonitorNoteMapper.getNewID(afl);
        note.setNoteNo(PublicMethod.IdCreate(3));
        meterMonitorNoteMapper.insertSelective(note);
        return note.getNoteNo();
    }

    //将附加重量的编号取出，并新增进附加中的表中
    private void insertAddWgtNo(String actualFirstNo, String addWgtPk, String createManNo, String createManName) {
        if (StringUtils.isEmpty(actualFirstNo) || StringUtils.isEmpty(addWgtPk)) {
            throw new MarkerMetException(500, "附加重量异常");
        }
        String[] str = addWgtPk.split(",");
        for (int i = 0; i < str.length; i++) {
            MeterBaseAddWeight mbaw = meterBaseAddWeightMapper.selectByPrimaryKey(str[i]);
            MeterWorkAddWeight mkaw = new MeterWorkAddWeight();
            String no = meterWorkAddWeightMapper.getNewID();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            mkaw.setAddWeightActNo(sdf.format(new Date()) + no);
            mkaw.setActualFirstNo(actualFirstNo);
            mkaw.setAddWeightNo(mbaw.getAddWeightNo());
            mkaw.setUtensilTypeNo(mbaw.getUtensilTypeNo());
            mkaw.setUtensilName(mbaw.getUtensilName());
            mkaw.setUtensilNo(mbaw.getUtensilNo());
            mkaw.setUtensilUtensilName(mbaw.getUtensilUtensilName());
            mkaw.setUtensilWeight(mbaw.getUtensilWeight());
            mkaw.setCreateManNo(createManNo);
            mkaw.setCreateManName(createManName);
            mkaw.setCreateTime(new Date());
            meterWorkAddWeightMapper.insertSelective(mkaw);
        }
    }

    /**
     * 非内倒车辆使用期限皮重结净
     *
     * @param oci
     * @param netDb
     * @param note
     * @return
     */
    @Override
    @Transactional
    public RESTfulResult doAddNetByTermWf(MeterWorkCarActualFirstBak oci, MeterWorkCarActualBak netDb, MeterMonitorNote note) {
        RESTfulResult rm = new RESTfulResult();
        rm.setCode("200");

        //0、查询预报信息；判断预报车号的车号与前端传进来的车号是不是一样的；2021年5月16日
        PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(oci.getPredictionNo());
        if (StringUtils.isEmpty(pts.getCarNo()) || !pts.getCarNo().equals(oci.getCarNo())) {
            throw new MarkerMetException(500, "预报中的车号【" + pts.getCarNo() + "】与界面车号不一致【" + oci.getCarNo() + "】");
        }

        //先格式化时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        //1、新增一次数据
        //存储当前计量的数据
        String afl = formatter.format(oci.getCreateTime());
//        String iActualNo = meterWorkCarActualFirstMapper.getNewID(afl);
//        oci.setActualFirstNo(afl + iActualNo);

        String iActualNo = PublicMethod.IdCreate(3);
        oci.setActualFirstNo(iActualNo);

        oci.setValueFlag("2");
        oci.setCreateTime(new Date());
        meterWorkCarActualFirstBakMapper.insertSelective(oci);
        //2、补充期限皮重的数据
        MeterBaseTermTareData mbttd = meterBaseTermTareDataMapper.selectByPrimaryKey(netDb.getActualFirst2No());
        MeterWorkCarActualFirstBak imTare = new MeterWorkCarActualFirstBak();
        //补充数据
        String afl1 = formatter.format(new Date());
//        String imTareNO = meterWorkCarActualFirstMapper.getNewID(afl1);
//        imTare.setActualFirstNo(afl1 + imTareNO);

        String imTareNO = PublicMethod.IdCreate(3);
        imTare.setActualFirstNo(imTareNO);

        imTare.setMeterTypeNo(oci.getMeterTypeNo());
        imTare.setMeterTypeName(oci.getMeterTypeName());
        imTare.setBaseSpotNo(mbttd.getBaseSpotNo());
        imTare.setBaseSpotName(mbttd.getBaseSpotName());
        imTare.setCarNo(mbttd.getCarNo());
        imTare.setCreateTime(mbttd.getCreateTime());
        imTare.setCreateManNo(mbttd.getCreateManNo());
        imTare.setCreateManName(mbttd.getCreateManName());
        imTare.setMeterWeight(netDb.getTareWeight());
        imTare.setWeightType("2");//期限皮重
        imTare.setValueFlag("2");//结净
        meterWorkCarActualFirstBakMapper.insertSelective(imTare);

        //3、新增结净数据
//        String afl2 = formatter.format(netDb.getNetTime());
//        String no = meterWorkCarActualBakMapper.getNewID(afl2);
//        netDb.setActualNo(afl + no);

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

        netDb.setActualFirst2No(imTare.getActualFirstNo());
        netDb.setActualFirst1No(oci.getActualFirstNo());
        if (netDb != null && netDb.getNetWeight() != null) {
            //2021年3月7日 净重保留两位小数；
            BigDecimal net;
            net = new BigDecimal(String.valueOf(netDb.getNetWeight())).setScale(2, BigDecimal.ROUND_HALF_UP);
            netDb.setNetWeight(net);
        }
        netDb.setCreateTime(new Date());
        if(netDb.getGrossTime().compareTo(netDb.getTareTime()) >= 0) {
            netDb.setNetSpot3No(netDb.getBaseSpot1No());
            netDb.setNetSpot3Name(netDb.getBaseSpot1Name());
            netDb.setNetManNo(netDb.getGrossManNo());
            netDb.setNetManName(netDb.getGrossManName());
            netDb.setNetTime(netDb.getGrossTime());
            netDb.setCreateTime(netDb.getGrossTime());
            netDb.setCreateManName(netDb.getGrossManName());
        }else {
            netDb.setNetSpot3No(netDb.getBaseSpot2No());
            netDb.setNetSpot3Name(netDb.getBaseSpot2Name());
            netDb.setNetManNo(netDb.getTareManNo());
            netDb.setNetManName(netDb.getTareManName());
            netDb.setNetTime(netDb.getTareTime());
            netDb.setCreateTime(netDb.getTareTime());
            netDb.setCreateManName(netDb.getTareManName());
        }
        meterWorkCarActualBakMapper.insertSelective(netDb);

        //4、修改预报
        if (pts.getPredictionType().equals("0")) {
            pts.setValueFlag("4");//正使用
            //批次预报 有时间，次数，重量限制
            if (pts.getUsedType().equals("0")) {  //0次数  1时间  2总量
                //次数
                pts.setMeterNum(pts.getMeterNum() == null ? 1 : (pts.getMeterNum() + 1));
                if (pts.getEffectiveNum().equals(pts.getMeterNum())) {//如果两个值相等
                    pts.setValueFlag("2");//用完了则状态改为2
                }
            } else if (pts.getUsedType().equals("1")) {
                //1时间
                if (!"2".equals(pts.getValueFlag()) && pts.getEffectiveEndTime().compareTo(new Date()) <= 0) {
                    pts.setValueFlag("2");//当前状态不为2，且结束时间小于当时间
                }
            } else if (pts.getUsedType().equals("2")) {
                //总量
                pts.setMeterWeight(pts.getMeterWeight() == null ? netDb.getNetWeight() : (pts.getMeterWeight().add(netDb.getNetWeight())));
                if (pts.getEffectiveWeight().doubleValue() <= pts.getMeterWeight().doubleValue()) {//如果总重量小于等于检斤重量则用完了
                    pts.setValueFlag("2");//用完了则状态改为2
                }
            }
            pts.setUploadFlag("1");
            pts.setMeasureStation(netDb.getNetSpot3No());
            pts.setMeasureStationName(netDb.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(pts);
        } else if (pts.getPredictionType().equals("1")) {
            //单次预报 使用1次后将状态设置为已使用
            pts.setValueFlag("2");
            pts.setUploadFlag("1");
            pts.setMeasureStation(netDb.getNetSpot3No());
            pts.setMeasureStationName(netDb.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(pts);
        } else if (pts.getPredictionType().equals("3") && StringUtils.isNotEmpty(pts.getRailwayNo())) {
            //===================火车分检预报处理开始==========================
            //分检预报 有效次数为1；每次计量需要更新火车的重量,前提是火车车号不允许为空***************
            //1.找到轨道衡的同一车的所有数据；重量类型weightType为毛重；状态位为（0,2）的数据(xml中默认)
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("railwayNo", pts.getRailwayNo());
            obj.put("weightType", "0");
            List<MeterWorkRailwayActFirst> mwrafs = meterWorkRailwayActFirstMapper.selectBy(obj);
            if (mwrafs != null && mwrafs.size() > 0) {
                MeterWorkRailwayActFirst meterWorkRailwayActFirst = mwrafs.get(0);
                countForRail(meterWorkRailwayActFirst, netDb.getNetWeight()); //一次计量数据，净重数据
            }
            pts.setValueFlag("2");//预报完结
            pts.setUploadFlag("1");
            pts.setMeasureStation(mwrafs.get(0).getBaseSpotNo());
            pts.setMeasureStationName(mwrafs.get(0).getBaseSpotName());
            preTrackScaleMapper.updateByPrimaryKeySelective(pts);
            //===================火车分检预报处理结束==========================
        }
        //5、修改预环节
        //将结净计量以及之前的所有环节都置为已经使用
        List<MeterWorkPreLink> links = meterWorkPreLinkService.getPer(oci.getPredictionNo(), DbConstants.CLEAN); //001021005
        //2021年3月8日杨秀东新增逻辑，对卸货环节[001021004]进行特殊处理;
        if (links != null && links.size() > 1) {
            for (int i = links.size() - 1; i >= 0; i--) {
                //001021004
                if (links.get(i).getLinkCode() != null && DbConstants.unload.equals(links.get(i).getLinkCode())) {
                    links.remove(i);
                }
            }
        }
        meterWorkPreLinkMapper.batchUpdateForUse(links);
        //6、修改预流程
        //获取预流程是否还有未使用的环节，若没有了则修改其状态  2.2修改预流程
        MeterWorkPreLink mplq = new MeterWorkPreLink();
        mplq.setPreProcessNo(links.get(0).getPreProcessNo());
        mplq.setValueFlag("0");
        Map<String, Object> map = FieldsCollector.beanToMap(mplq);
        List<MeterWorkPreLink> list = meterWorkPreLinkMapper.selectByParameters(map);
        if (list.size() == 0) {
            MeterWorkPreProcess mpp = new MeterWorkPreProcess();
            mpp.setPreProcessNo(links.get(0).getPreProcessNo());
            mpp.setValueFlag("2");
            meterWorkPreProcessMapper.updateByPrimaryKeySelective(mpp);
        }
        //7、保存日志
        saveNote(note);

        //8、批次预报重新生成一套预环节  若为批次预报且还未用完
        if (pts.getPredictionType().equals("0")) {
            meterWorkPreLinkService.batchReset(netDb.getPredictionNo());
        }

        //处理检化验委托信息；非内倒车辆使用期限皮重结净
        rm.setData(oci.getActualFirstNo());
        return rm;
    }


    private String transferParamForLims(String carNo, String matterNo, String unloadPlace, String sendPlace, String netWgt,
                                        Date wgtTime, String grossWgt, String cardId) {
        //获取数据索引码（主数据编码）
        MeterBaseMatterInfo mbmi = meterBaseMatterInfoMapper.selectByPrimaryKey(matterNo);
        String matNo = mbmi.getIndexCode();
        //获取账登录deaccessTokens
        Map jsons = new HashMap();
        jsons.put("loginName", DbConstants.loginName);
        jsons.put("password", DbConstants.password);
        String logins = HttpRequest.postJsonMethodParamsEncode(DbConstants.IPFROLOGIN + "/icore.icp.web/pass/auth/login", jsons);
        String accessTokens = JSONObject.parseObject(JSONObject.parseObject(logins).getString("data")).getString("accessToken");
        //system.out.println(accessTokens);
        //调用接口为
        JSONObject json = new JSONObject();
        json.put("carNo", carNo);
        json.put("matNo", matNo);
        json.put("unloadPlace", unloadPlace);
        json.put("sendPlace", sendPlace);
        json.put("netWgt", netWgt);
        json.put("wgtTime", wgtTime);
        json.put("grossWgt", grossWgt);
        json.put("cardId", cardId);
        String result = HttpRequest.postmanAccessToken(DbConstants.IPFORTRAN + "/v1/limstestplans/replacementdata", JSONObject.toJSONString(json), accessTokens);
        String results = JSONObject.parseObject(result).getString("data");
        return results;
    }


    /**
     * 按照时间查询数据
     *
     * @param parmas
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageList<MeterWorkCarActualFirstBak> queryOrderByTime(HashMap parmas, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkCarActualFirstBak> firstList = meterWorkCarActualFirstBakMapper.queryOrderByTime(parmas);
        PageList pageInfo = new PageList(firstList);
        return pageInfo;
    }

    /**
     * WGT_TIME
     * 更新预报中的卸货截止时间
     *
     * @param actualFirst
     */
    public void updateTrastUnloadEndTime(MeterWorkCarActualFirstBak actualFirst) {
        if (actualFirst != null && StringUtils.isNotEmpty(actualFirst.getPredictionNo()) && StringUtils.isNotEmpty(actualFirst.getMatterNo())
                && StringUtils.isNotEmpty(actualFirst.getBaseSpotNo()) && StringUtils.isNotEmpty(actualFirst.getLoadPointNo())) {
            MeterBaseUnloadTime unloadTime = new MeterBaseUnloadTime();
            unloadTime.setBaseSpotNo(actualFirst.getBaseSpotNo());
            unloadTime.setLoadPointNo(actualFirst.getLoadPointNo());
            unloadTime.setMatterNo(actualFirst.getMatterNo());
            Map<String, Object> unloadTimeMap = FieldsCollector.beanToMap(unloadTime);
            List<MeterBaseUnloadTime> unloadTimeList = meterBaseUnloadTimeMapper.selectByParameters(unloadTimeMap);
            if (unloadTimeList != null && unloadTimeList.size() > 0) {
                PreTrackScale scale = new PreTrackScale();
                scale.setPredictionNo(actualFirst.getPredictionNo());
            }
        }
    }

    /**
     * 定期作废【销售出厂】未使用的氢氧化钠的皮重
     */
//    @Scheduled(cron = " 0 */60 * * * ?") //定时任务(每天凌晨两点执行一次)/作废皮重数据；定时服务打包时启用 @Scheduled(cron = " 0 0 2 * * ?")
    public void doInvalidNaOHData() {
        List<MeterWorkCarActualFirstBak> actFirstList = meterWorkCarActualFirstBakMapper.selectInvalidNaOHData();
        if (actFirstList != null && actFirstList.size() > 0) {
            for (MeterWorkCarActualFirstBak firstTare : actFirstList) {
                firstTare.setValueFlag("0");
                firstTare.setUpdateTime(new Date());
                firstTare.setUpdateManNo("system");
                firstTare.setUpdateManName("系统定时作废过期皮重信息");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKey(firstTare);

                if (StringUtils.isNotEmpty(firstTare.getPredictionNo()) && firstTare.getPredictionNo().length() > 10) {
                    PreTrackScale trackScale = new PreTrackScale();
                    trackScale.setPredictionNo(firstTare.getPredictionNo());
                    trackScale.setValueFlag("3");
                    trackScale.setUpdateTime(new Date());
                    trackScale.setUpdateManNo("system");
                    trackScale.setUploadFlag("1");
                    trackScale.setUpdateManName("系统定时作废过期皮重信息");
                    preTrackScaleMapper.updateByPrimaryKey(trackScale);
                }
            }
        }
    }


    /**
     * 非内倒车辆存储一次计量数据
     *
     * @param oci
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doAddLianDaWf(MeterWorkCarActualFirstBak oci, MeterMonitorNote note) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setCode("200");
        if (oci == null) {
            throw new MarkerMetException(500, "参数！！");
        }
        //0、查询预报信息；判断预报车号的车号与前端传进来的车号是不是一样的；2021年5月16日
//        PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(oci.getPredictionNo().replace("_",""));
//        if (StringUtils.isEmpty(pts.getCarNo()) || !pts.getCarNo().equals(oci.getCarNo())) {
//            throw new MarkerMetException(500, "预报中的车号【" + pts.getCarNo() + "】与界面车号不一致【" + oci.getCarNo() + "】");
//        }

        //1、存储一次计量信息
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String afl = formatter.format(oci.getCreateTime());
//        String no = meterWorkCarActualFirstMapper.getNewID(afl);
//        oci.setActualFirstNo(afl + no);

        String no = PublicMethod.IdCreate(3);
        oci.setActualFirstNo(no);

        oci.setValueFlag("1");
        oci.setCreateTime(new Date());
        meterWorkCarActualFirstBakMapper.insertSelective(oci);

        //6、存储图片
        //SaveImg(oci);

        //7、修改计量监控
        updateMonitor(oci.getBaseSpotNo());

        //8、修改监控的计量作业编号
        updateMonitorOver(oci.getActualFirstNo(), oci.getBaseSpotNo());

        //信息加密
        String str = oci.getActualFirstNo() + "," + oci.getMeterWeight() + "," + "1";
        String enStr = encryption(str);

        //保存日志
        saveNote(note);

        rm.setData(oci.getActualFirstNo());
        rm.setMessage(enStr);
        return rm;
    }

    /**
     * 非内倒车辆结净
     *
     * @param secondDb 二次计量数据
     * @param netDb    净重数据
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(value = "tm", rollbackFor = Exception.class)
    public RESTfulResult doAddNetLianDaWf(MeterWorkCarActualFirstBak secondDb, MeterWorkCarActualBak netDb, MeterMonitorNote note) throws Exception {
        String lyFirstNo = "";//联运自动生成的一次计量编号
        RESTfulResult rm = new RESTfulResult();
        rm.setCode("200");
        //先格式化时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

        //0、查询预报信息；判断预报车号的车号与前端传进来的车号是不是一样的；2021年5月16日
        PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(secondDb.getPredictionNo().replace("_",""));
        if (StringUtils.isEmpty(pts.getCarNo()) || !pts.getCarNo().equals(secondDb.getCarNo())) {
            throw new MarkerMetException(500, "预报中的车号【" + pts.getCarNo() + "】与界面车号不一致【" + secondDb.getCarNo() + "】");
        }

        //1、存储当前计量的数据  1.1先给一个初始状态
        secondDb.setValueFlag("2");//结净

        //3.1判断皮重信息
        if (secondDb.getMeterWeight().equals(netDb.getTareWeight())) {
            //二次重量如果跟皮重一样
            netDb.setActualFirst2No(secondDb.getActualFirstNo());
        } else {
            netDb.setActualFirst1No(secondDb.getActualFirstNo());
        }



        //5、存储当前计量的数据
        String afl = formatter.format(secondDb.getCreateTime());
//        String iActualNo = meterWorkCarActualFirstMapper.getNewID(afl);
//        secondDb.setActualFirstNo(afl + iActualNo);

        String iActualNo = PublicMethod.IdCreate(3);
        secondDb.setActualFirstNo(iActualNo);

        secondDb.setCreateTime(new Date());
        meterWorkCarActualFirstBakMapper.insertSelective(secondDb);


        //一次计量的数据修改状态
        MeterWorkCarActualFirstBak firstUpdate = new MeterWorkCarActualFirstBak();

        //6、存储结净数据
//        String afl1 = formatter.format(netDb.getNetTime());
//        String no = meterWorkCarActualBakMapper.getNewID(afl1);
//        netDb.setActualNo(afl + no); //注意：此处生成的编号是下一次的编号；如果是一车联运不能直接返回给客户端这个编号

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

        if (secondDb.getMeterWeight().equals(netDb.getTareWeight())) {
            //二次指哪个了如果跟皮重一样
            if (StringUtils.isEmpty(netDb.getActualFirst2No()) || netDb.getActualFirst2No().length() < 10)
                netDb.setActualFirst2No(secondDb.getActualFirstNo()); //皮重编号
            firstUpdate.setActualFirstNo(netDb.getActualFirst1No());
        } else {
            if (StringUtils.isEmpty(netDb.getActualFirst1No()) || netDb.getActualFirst1No().length() < 10)
                netDb.setActualFirst1No(secondDb.getActualFirstNo()); //毛重编号
            firstUpdate.setActualFirstNo(netDb.getActualFirst2No());
        }
        if (netDb != null && netDb.getNetWeight() != null) {
            //2021年3月7日 净重保留两位小数；
            BigDecimal net;
            net = new BigDecimal(String.valueOf(netDb.getNetWeight())).setScale(2, BigDecimal.ROUND_HALF_UP);
            netDb.setNetWeight(net);
        }
        netDb.setUploadFlag("1");
        netDb.setCreateTime(new Date());
        if(netDb.getGrossTime().compareTo(netDb.getTareTime()) >= 0) {
            netDb.setNetSpot3No(netDb.getBaseSpot1No());
            netDb.setNetSpot3Name(netDb.getBaseSpot1Name());
            netDb.setNetManNo(netDb.getGrossManNo());
            netDb.setNetManName(netDb.getGrossManName());
            netDb.setNetTime(netDb.getGrossTime());
            netDb.setCreateTime(netDb.getGrossTime());
            netDb.setCreateManName(netDb.getGrossManName());
        }else {
            netDb.setNetSpot3No(netDb.getBaseSpot2No());
            netDb.setNetSpot3Name(netDb.getBaseSpot2Name());
            netDb.setNetManNo(netDb.getTareManNo());
            netDb.setNetManName(netDb.getTareManName());
            netDb.setNetTime(netDb.getTareTime());
            netDb.setCreateTime(netDb.getTareTime());
            netDb.setCreateManName(netDb.getTareManName());
        }
        meterWorkCarActualBakMapper.insertSelective(netDb);

        //7、修改第一次存储的计量数据状态为结净;预报编号相同时才处理（不是一车联运非互锁式）
        if (firstUpdate.getActualFirstNo() != null
                && netDb.getPredictionNo() != null && secondDb.getPredictionNo() != null
                && netDb.getPredictionNo().equals(secondDb.getPredictionNo())) {
            firstUpdate.setValueFlag("2");
            if (secondDb.getWeightType().equals("0"))//第二次为毛重
            {
                firstUpdate.setWeightType("1");//第一次则为皮重
            } else {
                firstUpdate.setWeightType("0");//否则第一次为毛重
            }
            meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(firstUpdate);
        }

        //10、存储图片
        //SaveImg(secondDb);

        //11、修改计量监控
        updateMonitor(secondDb.getBaseSpotNo());

        //12、修改监控的计量作业编号
        updateMonitorOver(secondDb.getActualFirstNo(), secondDb.getBaseSpotNo());


        //信息加密
        String str = netDb.getActualNo() + "," + netDb.getNetWeight() + "," + "2";
        String enStr = encryption(str);

        //保存日志
        saveNote(note);

        rm.setData(secondDb.getActualFirstNo());
        rm.setMessage(enStr);
        return rm;
    }

    /**
     * 双委托数据结净
     *
     * @param secondDb 二次计量数据
     * @param netDb1    A-B净重数据
     * @param netDb2    B-C净重数据
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(value = "tm", rollbackFor = Exception.class)
    public RESTfulResult doAddNetDouble(MeterWorkCarActualFirstBak secondDb, MeterWorkCarActualBak netDb1, MeterMonitorNote note, MeterWorkCarActualBak netDb2) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setCode("200");
        //先格式化时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

        //1、存储当前计量的数据  1.1先给一个初始状态
        secondDb.setValueFlag("2");//结净

        //2、存储三条一次计量数据
        // 第一条结净数据的二次重量数据
        String afl = formatter.format(secondDb.getCreateTime());
//        String iActualNo = meterWorkCarActualFirstMapper.getNewID(afl);
//        secondDb.setActualFirstNo(afl + iActualNo);

        String iActualNo = PublicMethod.IdCreate(3);
        secondDb.setActualFirstNo(iActualNo);
        if (netDb1.getActualFirst1No() == null || "".equals(netDb1.getActualFirst1No())) {
            netDb1.setActualFirst1No(secondDb.getActualFirstNo());
        } else {
            netDb1.setActualFirst2No(secondDb.getActualFirstNo());
        }
        secondDb.setCreateTime(new Date());
        meterWorkCarActualFirstBakMapper.insertSelective(secondDb);

        // 第二条结净数据的一次重量数据
        MeterWorkCarActualFirstBak first1 = new MeterWorkCarActualFirstBak();
        MeterWorkCarActualFirstBak first2 = new MeterWorkCarActualFirstBak();

        //一次计量的数据修改状态
        MeterWorkCarActualFirstBak firstUpdate = new MeterWorkCarActualFirstBak();

        //6、存储结净数据
//        String afl1 = formatter.format(netDb1.getNetTime());
//        String no = meterWorkCarActualBakMapper.getNewID(afl1);
//        netDb1.setActualNo(afl + no); //注意：此处生成的编号是下一次的编号；如果是一车联运不能直接返回给客户端这个编号

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

        if (secondDb.getMeterWeight().equals(netDb1.getTareWeight())) {
            //二次指哪个了如果跟皮重一样
            if (StringUtils.isEmpty(netDb1.getActualFirst2No()) || netDb1.getActualFirst2No().length() < 10)
                netDb1.setActualFirst2No(secondDb.getActualFirstNo()); //皮重编号
            firstUpdate.setActualFirstNo(netDb1.getActualFirst1No());
        } else {
            if (StringUtils.isEmpty(netDb1.getActualFirst1No()) || netDb1.getActualFirst1No().length() < 10)
                netDb1.setActualFirst1No(secondDb.getActualFirstNo()); //毛重编号
            firstUpdate.setActualFirstNo(netDb1.getActualFirst2No());
        }
        if (netDb1 != null && netDb1.getNetWeight() != null) {
            //2021年3月7日 净重保留两位小数；
            BigDecimal net;
            net = new BigDecimal(String.valueOf(netDb1.getNetWeight())).setScale(2, BigDecimal.ROUND_HALF_UP);
            netDb1.setNetWeight(net);
        }
        netDb1.setCreateTime(new Date());
        if(netDb1.getGrossTime().compareTo(netDb1.getTareTime()) >= 0) {
            netDb1.setNetSpot3No(netDb1.getBaseSpot1No());
            netDb1.setNetSpot3Name(netDb1.getBaseSpot1Name());
            netDb1.setNetManNo(netDb1.getGrossManNo());
            netDb1.setNetManName(netDb1.getGrossManName());
            netDb1.setNetTime(netDb1.getGrossTime());
            netDb1.setCreateTime(netDb1.getGrossTime());
            netDb1.setCreateManName(netDb1.getGrossManName());
        }else {
            netDb1.setNetSpot3No(netDb1.getBaseSpot2No());
            netDb1.setNetSpot3Name(netDb1.getBaseSpot2Name());
            netDb1.setNetManNo(netDb1.getTareManNo());
            netDb1.setNetManName(netDb1.getTareManName());
            netDb1.setNetTime(netDb1.getTareTime());
            netDb1.setCreateTime(netDb1.getTareTime());
            netDb1.setCreateManName(netDb1.getTareManName());
        }
        meterWorkCarActualBakMapper.insertSelective(netDb1);



        //10、存储图片
        //(secondDb);

        //11、修改计量监控
        updateMonitor(secondDb.getBaseSpotNo());

        //12、修改监控的计量作业编号
        updateMonitorOver(secondDb.getActualFirstNo(), secondDb.getBaseSpotNo());


        //信息加密
        String str = netDb1.getActualNo() + "," + netDb1.getNetWeight() + "," + "2";
        String enStr = encryption(str);

        //保存日志
        saveNote(note);

        rm.setData(secondDb.getActualFirstNo());
        rm.setMessage(enStr);
        return rm;
    }


    /**
     * 吊钩秤存储一次计量数据
     * @param oci
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doCraneDbWf(MeterWorkCarActualFirstBak oci, MeterMonitorNote note) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setCode("200");
        if (oci == null) {
            throw new MarkerMetException(500, "参数！！");
        }
        //0、查询预报信息；判断预报车号的车号与前端传进来的车号是不是一样的；2021年5月16日
        PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(oci.getPredictionNo());
        if (StringUtils.isEmpty(pts.getCarNo()) || !pts.getCarNo().equals(oci.getCarNo())) {
            throw new MarkerMetException(500, "预报中的车号【" + pts.getCarNo() + "】与界面车号不一致【" + oci.getCarNo() + "】");
        }

        //1、存储一次计量信息
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String afl = formatter.format(oci.getCreateTime());
//        String no = meterWorkCarActualFirstMapper.getNewID(afl);
//        oci.setActualFirstNo(afl + no);
        String no = PublicMethod.IdCreate(3);
        oci.setActualFirstNo(no);
        oci.setValueFlag("1");
        oci.setCreateTime(new Date());
        meterWorkCarActualFirstBakMapper.insertSelective(oci);

        //根据车号加预报编号修改 craneActualMapper
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("predictionNo", oci.getPredictionNo());
        obj.put("carNo", oci.getCarNo());
        obj.put("valueFlag", "2");
        obj.put("weightType","0");
        craneActualMapper.doUpdateFlag(obj);

        //修改预报为已使用，因为车子可能过几天才来过皮，所以预报不能处于正使用状态，否则会使车辆无法继续过磅
        PreTrackScale pre=new PreTrackScale();
        pre.setPredictionNo(oci.getPredictionNo());
        pre.setValueFlag("2");
        pre.setUploadFlag("1");
        pre.setMeasureStation(oci.getBaseSpotNo());
        pre.setMeasureStationName(oci.getBaseSpotName());
        preTrackScaleMapper.updateByPrimaryKeySelective(pre);

        //若有环节信息，还得将环节信息给设置为已使用，不然会影响后续计量的流程
        meterWorkPreLinkMapper.doUpdateFlag(obj);

        //信息加密
        String str = oci.getActualFirstNo() + "," + oci.getMeterWeight() + "," + "1";
        String enStr = encryption(str);

        //保存日志
        saveNote(note);

        rm.setData(oci.getActualFirstNo());
        rm.setMessage(enStr);
        return rm;
    }

    @Override
    @Transactional
    public RESTfulResult doCraneNetDbWf(MeterWorkCarActualFirstBak oci, MeterWorkCarActualBak netDB, MeterMonitorNote note) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setCode("200");
        if (oci == null) {
            throw new MarkerMetException(500, "参数！！");
        }
        //0、查询预报信息；判断预报车号的车号与前端传进来的车号是不是一样的；2021年5月16日
        PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(oci.getPredictionNo());
        if (StringUtils.isEmpty(pts.getCarNo()) || !pts.getCarNo().equals(oci.getCarNo())) {
            throw new MarkerMetException(500, "预报中的车号【" + pts.getCarNo() + "】与界面车号不一致【" + oci.getCarNo() + "】");
        }

        //1、存储一次计量信息
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String afl = formatter.format(oci.getCreateTime());
//        String no = meterWorkCarActualFirstMapper.getNewID(afl);
//        oci.setActualFirstNo(afl + no);

        String no = PublicMethod.IdCreate(3);
        oci.setActualFirstNo(no);

        oci.setValueFlag("2");
        oci.setCreateTime(new Date());
        meterWorkCarActualFirstBakMapper.insertSelective(oci);

        netDB.setActualFirst2No(oci.getActualFirstNo());
//        String no1 = meterWorkCarActualBakMapper.getNewID(afl);
//        netDB.setActualNo(afl + no1);

        String no1 = PublicMethod.IdCreate(3);
        netDB.setActualNo(no1);

        //2、将原有的一次数据结净
        MeterWorkCarActualFirstBak firstDb=new MeterWorkCarActualFirstBak();
        firstDb.setActualFirstNo(netDB.getActualFirst1No());
        firstDb.setValueFlag("2");
        meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(firstDb);

        //3、存储数据到结净表
        netDB.setCreateTime(new Date());
        if(netDB.getGrossTime().compareTo(netDB.getTareTime()) >= 0) {
            netDB.setNetSpot3No(netDB.getBaseSpot1No());
            netDB.setNetSpot3Name(netDB.getBaseSpot1Name());
            netDB.setNetManNo(netDB.getGrossManNo());
            netDB.setNetManName(netDB.getGrossManName());
            netDB.setNetTime(netDB.getGrossTime());
            netDB.setCreateTime(netDB.getGrossTime());
            netDB.setCreateManName(netDB.getGrossManName());
        }else {
            netDB.setNetSpot3No(netDB.getBaseSpot2No());
            netDB.setNetSpot3Name(netDB.getBaseSpot2Name());
            netDB.setNetManNo(netDB.getTareManNo());
            netDB.setNetManName(netDB.getTareManName());
            netDB.setNetTime(netDB.getTareTime());
            netDB.setCreateTime(netDB.getTareTime());
            netDB.setCreateManName(netDB.getTareManName());
        }
        meterWorkCarActualBakMapper.insert(netDB);

        //4、根据车号加预报编号修改 craneActualMapper
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("predictionNo", oci.getPredictionNo());
        obj.put("carNo", oci.getCarNo());
        obj.put("weightType","1");
        obj.put("valueFlag", "2");
        craneActualMapper.doUpdateFlag(obj);

        //信息加密
        String str = oci.getActualFirstNo() + "," + oci.getMeterWeight() + "," + "1";
        String enStr = encryption(str);

        //5、保存日志
        saveNote(note);

        rm.setData(oci.getActualFirstNo());
        rm.setMessage(enStr);
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> distinctCarNo(HashMap<String, Object> obj) {
//        HashMap<String, Object> obj = new HashMap<>();
//        obj.put("carNo", carNo);
//        obj.put("startTime", startTime);
//        obj.put("endTime", endTime);
//        obj.put("baseSpotNo", baseSpotNo);
//        obj.put("matterName", matterName);
//        obj.put("predictionNo", predictionNo);
//        obj.put("forwardingUnitName", forwardingUnitName);
//        obj.put("receivingUintName", receivingUintName);
//        obj.put("valueFlag", valueFlag);
        List<String> list = meterWorkCarActualFirstBakMapper.distinctCarNo(obj).stream().map(MeterWorkCarActualFirstBak::getCarNo).collect(Collectors.toList());
        return list;
    }

    @Override
    public RESTfulResult doAddNetHZHX(MeterWorkCarActualFirstBak first02, MeterWorkCarActualBak carModel, MeterWorkCarActualFirstBak first03, MeterMonitorNote note) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            String scaleNo = null;
            if(first02.getMeterProcessNo()!=null && StringUtils.isNotEmpty(first02.getMeterProcessNo())) {
                String[] strList = first02.getMeterProcessNo().split(",");
                if(strList.length >= 1) {
                    if(strList[0].equals("混料")) {
                        scaleNo = strList[1];
                    }
                }
            }
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            String afl = sdf.format(new Date());

            //存储当前计量的数据
//            String firstNo01 = sdf.format(new Date()) + meterWorkCarActualFirstMapper.getNewID(afl);
//            first02.setActualFirstNo(firstNo01);
            String firstNo01 = PublicMethod.IdCreate(3);
            first02.setActualFirstNo(firstNo01);

            first02.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insertSelective(first02);

            MeterWorkCarActualBak actualRow = new MeterWorkCarActualBak();
            //毛重一次
            MeterWorkCarActualFirstBak model01 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(carModel.getActualFirst1No());
            //皮重一次
            MeterWorkCarActualFirstBak model02 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(carModel.getActualFirst2No());
            PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(carModel.getPredictionNo());
            //判断当前一次重量类型、0：毛重，则为混装操作、1：常规皮重，则为混卸操作
            if (first02.getWeightType().equals("0")) {       //混装
                //修改委托状态
                //将委托中的编号和收发货的等数据拷贝进一次数据中
                MeterWorkCarActualFirstBak first1 = meterWorkCarActualFirstBakMapper.selectAndMatch(model02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first1, model02);
                MeterWorkCarActualFirstBak first2 = meterWorkCarActualFirstBakMapper.selectAndMatch(first02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first2, first02);



                //修改毛重一次数据状态
                first02.setValueFlag(DbConstants.NET);
                first02.setUpdateManNo("system");
                first02.setUpdateManName("混装结净更新");
                first02.setUpdateTime(new Date());
                first02.setWeightType("0");
                first02.setIsPreScale("1");
                first02.setPredictionNo(scale.getPredictionNo());
                first02.setPredictionType("7");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(first02);
                //修改皮重一次数据状态
                model02.setValueFlag(DbConstants.NET);
                model02.setUpdateManNo("system");
                model02.setUpdateManName("混装结净更新");
                model02.setUpdateTime(new Date());
                model02.setWeightType("1");
                model02.setIsPreScale("1");
                model02.setPredictionNo(scale.getPredictionNo());
                model02.setPredictionType("7");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model02);

                MeterWorkCarActualBak mwca1 = meterWorkCarActualBakMapper.manualAddPre(first02.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
//                String no = meterWorkCarActualBakMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);

                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);
                actualRow.setNetTime(new Date());
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setNote("混装匹配结净");
                actualRow.setNetMode("4");
                actualRow.setUploadFlag("1");
                actualRow.setValueFlag("1");
                actualRow.setIsPreScale("1");
                actualRow.setNetManNo(carModel.getNetManNo());
                actualRow.setNetManName(carModel.getNetManName());
                actualRow.setNetSpot3No(carModel.getNetSpot3No());
                actualRow.setNetSpot3Name(carModel.getNetSpot3Name());
                actualRow.setCreateManName(carModel.getCreateManName());
                actualRow.setCreateTime(carModel.getCreateTime());
                actualRow.setUpdateManNo(carModel.getUpdateManNo());
                actualRow.setUpdateManName(carModel.getUpdateManName());
                actualRow.setUpdateTime(carModel.getUpdateTime());
                actualRow.setPredictionNo(scale.getPredictionNo());
                actualRow.setPredictionType("7");
                actualRow.setNetWeight(first02.getMeterWeight().subtract(model02.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);

                //修改委托编号
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("混装结净更新");
                scale.setUploadFlag("1");
                scale.setPredictionType("7");
                scale.setMeasureStation(actualRow.getNetSpot3No());
                scale.setMeasureStationName(actualRow.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(scale);
            } else if (first02.getWeightType().equals("1")) {         //混卸
                //修改委托状态
                //将委托中的编号和收发货的等数据拷贝进一次数据中
                MeterWorkCarActualFirstBak first1 = meterWorkCarActualFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first1, model01);
                MeterWorkCarActualFirstBak first2 = meterWorkCarActualFirstBakMapper.selectAndMatch(first02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first2, first02);

                //修改毛重一次数据状态
                first02.setValueFlag(DbConstants.NET);
                first02.setUpdateManNo("system");
                first02.setUpdateManName("混卸结净更新");
                first02.setUpdateTime(new Date());
                first02.setWeightType("0");
                first02.setIsPreScale("1");
                first02.setPredictionNo(scale.getPredictionNo());
                first02.setPredictionType("6");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(first02);
                //修改皮重一次数据状态
                model01.setValueFlag(DbConstants.NET);
                model01.setUpdateManNo("system");
                model01.setUpdateManName("混卸结净更新");
                model01.setUpdateTime(new Date());
                model01.setWeightType("1");
                model01.setIsPreScale("1");
                model01.setPredictionNo(scale.getPredictionNo());
                model01.setPredictionType("6");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model01);

                MeterWorkCarActualBak mwca1 = meterWorkCarActualBakMapper.manualAddPre(model01.getActualFirstNo(), first02.getActualFirstNo(),  scale.getPredictionNo());
                BeanUtils.copyProperties(mwca1, actualRow);

                //保存结净
//                String no = meterWorkCarActualBakMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);

                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);
                actualRow.setNetTime(new Date());
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setNote("混卸匹配结净");
                actualRow.setNetMode("5");
                actualRow.setUploadFlag("1");
                actualRow.setValueFlag("1");
                actualRow.setIsPreScale("1");
                actualRow.setNetManNo(carModel.getNetManNo());
                actualRow.setNetManName(carModel.getNetManName());
                actualRow.setNetSpot3No(carModel.getNetSpot3No());
                actualRow.setNetSpot3Name(carModel.getNetSpot3Name());
                actualRow.setCreateManName(carModel.getCreateManName());
                actualRow.setCreateTime(carModel.getCreateTime());
                actualRow.setUpdateManNo(carModel.getUpdateManNo());
                actualRow.setUpdateManName(carModel.getUpdateManName());
                actualRow.setUpdateTime(carModel.getUpdateTime());
                actualRow.setPredictionNo(scale.getPredictionNo());
                actualRow.setPredictionType("6");
                actualRow.setNetWeight(model01.getMeterWeight().subtract(first02.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);

                //修改委托编号
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("混卸结净更新");
                scale.setUploadFlag("1");
                scale.setPredictionType("6");
                scale.setMeasureStation(actualRow.getNetSpot3No());
                scale.setMeasureStationName(actualRow.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(scale);
            }

            //存储当前计量的数据
            if(first03 != null) {
//                String fristNo03 = sdf.format(new Date()) + meterWorkCarActualFirstMapper.getNewID(afl);
//                first03.setActualFirstNo(fristNo03);
                String fristNo03 = PublicMethod.IdCreate(3);
                first03.setActualFirstNo(fristNo03);

                first03.setIsPreScale("0");
                first03.setCreateTime(new Date());
                meterWorkCarActualFirstBakMapper.insertSelective(first03);
            }else {
                if(scaleNo != null && StringUtils.isNotEmpty(scaleNo)) {
                    //获取
                    MeterWorkCarActualFirstBak grossModel = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualRow.getActualFirst1No());
                    MeterWorkCarActualFirstBak tareModel = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualRow.getActualFirst2No());
                    MeterWorkCarActualBak actualModel = meterWorkCarActualBakMapper.selectByPrimaryKey(actualRow.getActualNo());
                    PreTrackScale scaleTemp = preTrackScaleMapper.selectByPrimaryKey(scaleNo);

                    grossModel.setPredictionCombination(carModel.getPredictionNo() + "," + scaleTemp.getPredictionNo());
                    tareModel.setPredictionCombination(carModel.getPredictionNo() + "," + scaleTemp.getPredictionNo());
                    actualModel.setPredictionCombination(carModel.getPredictionNo() + "," + scaleTemp.getPredictionNo());

                    meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(grossModel);
                    meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(tareModel);
                    meterWorkCarActualBakMapper.updateByPrimaryKeySelective(actualModel);

                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("predictionNo", scaleNo);
                    List<MeterWorkCarActualBak> modelList = meterWorkCarActualBakMapper.selectByParameters(obj);
                    if(modelList.size() >= 1) {
                        MeterWorkCarActualBak preCarActual = modelList.get(0);
                        MeterWorkCarActualFirstBak preGrossModel = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(preCarActual.getActualFirst1No());
                        MeterWorkCarActualFirstBak preTareModel = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(preCarActual.getActualFirst2No());

                        preGrossModel.setPredictionCombination(carModel.getPredictionNo() + "," + scaleTemp.getPredictionNo());
                        preTareModel.setPredictionCombination(carModel.getPredictionNo() + "," + scaleTemp.getPredictionNo());
                        preCarActual.setPredictionCombination(carModel.getPredictionNo() + "," + scaleTemp.getPredictionNo());

                        meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(preGrossModel);
                        meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(preTareModel);
                        meterWorkCarActualBakMapper.updateByPrimaryKeySelective(preCarActual);
                    }
                }
            }
            //6、修改计量监控
            updateMonitor(first02.getBaseSpotNo());
            //保存日志
            saveNote(note);
            updateMonitorOver(first02.getActualFirstNo(), first02.getBaseSpotNo());
            //补充附加重量的数据
            if (StringUtils.isNotEmpty(first02.getAddWgtPk())) {
                insertAddWgtNo(first02.getActualFirstNo(), first02.getAddWgtPk(), first02.getCreateManNo(), first02.getCreateManName());
            }
            rm.setSucceed();
            rm.setData(first02.getActualFirstNo());
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            return rm;
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！！>>>>" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return rm;
        }
    }

    @Override
    public RESTfulResult doNetAddTwo(MeterWorkCarActualFirstBak one, MeterWorkCarActualBak two, PreTrackScale three, PreTrackScale four) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());

            //存储当前计量的数据
//            String firstNo01 = sdf.format(new Date()) + meterWorkCarActualFirstMapper.getNewID(afl);
//            one.setActualFirstNo(firstNo01);

            String firstNo01 = PublicMethod.IdCreate(3);
            one.setActualFirstNo(firstNo01);
            one.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insertSelective(one);

            MeterWorkCarActualFirstBak model01 = new MeterWorkCarActualFirstBak();
            MeterWorkCarActualFirstBak model02 = new MeterWorkCarActualFirstBak();

            MeterWorkCarActualBak actualRow1 = new MeterWorkCarActualBak();
            MeterWorkCarActualBak actualRow2 = new MeterWorkCarActualBak();
            if (one.getWeightType().equals("0")) {
                //毛重一次
                model01 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(one.getActualFirstNo());
                //皮重一次
                model02 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(two.getActualFirst2No());
            } else if (one.getWeightType().equals("1")) {
                //毛重一次
                model01 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(two.getActualFirst1No());
                //皮重一次
                model02 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(one.getActualFirstNo());
            }
            MeterWorkCarActualFirstBak model03 = new MeterWorkCarActualFirstBak();
            MeterWorkCarActualFirstBak model04 = new MeterWorkCarActualFirstBak();

            PreTrackScale scale01 = preTrackScaleMapper.selectByPrimaryKey(three.getPredictionNo());
            PreTrackScale scale02 = preTrackScaleMapper.selectByPrimaryKey(four.getPredictionNo());

            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkCarActualFirstBak first1 = meterWorkCarActualFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale01.getPredictionNo());
            BeanUtils.copyProperties(first1, model01);
            MeterWorkCarActualFirstBak first2 = meterWorkCarActualFirstBakMapper.selectAndMatch(model02.getActualFirstNo(), scale01.getPredictionNo());
            BeanUtils.copyProperties(first2, model02);
            MeterWorkCarActualFirstBak first3 = meterWorkCarActualFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale02.getPredictionNo());
            BeanUtils.copyProperties(first3, model03);
            MeterWorkCarActualFirstBak first4 = meterWorkCarActualFirstBakMapper.selectAndMatch(model02.getActualFirstNo(), scale02.getPredictionNo());
            BeanUtils.copyProperties(first4, model04);

            //更新一次数据
            //更新第一次毛重数据库
            model01.setValueFlag(DbConstants.NET);
            model01.setUpdateManNo("system");
            model01.setUpdateManName("双委托结净更新");
            model01.setUpdateTime(new Date());
            model01.setWeightType("0");
            model01.setIsPreScale("1");
            model01.setPredictionNo(scale01.getPredictionNo());
            model01.setPredictionType("5");
            model01.setPredictionCombination(scale01.getPredictionNo() + "," + scale02.getPredictionNo());
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(model01);
            //更新第一次皮重数据库
            model02.setValueFlag(DbConstants.NET);
            model02.setUpdateManNo("system");
            model02.setUpdateManName("双委托结净更新");
            model02.setUpdateTime(new Date());
            model02.setWeightType("1");
            model02.setIsPreScale("1");
            model02.setPredictionNo(scale01.getPredictionNo());
            model02.setPredictionType("5");
            model02.setPredictionCombination(scale01.getPredictionNo() + "," + scale02.getPredictionNo());
            meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model02);
            //添加第一次毛重数据库
//            String actualFirstNo = sdf.format(new Date()) + meterWorkCarActualFirstMapper.getNewID(afl);
//            model03.setActualFirstNo(actualFirstNo);
            String actualFirstNo = PublicMethod.IdCreate(3);
            model03.setActualFirstNo(actualFirstNo);

            model03.setValueFlag(DbConstants.NET);
            model03.setUpdateManNo("system");
            model03.setUpdateManName("双委托结净更新");
            model03.setWeightType("0");
            model03.setUpdateTime(new Date());
            model03.setIsPreScale("1");
            model03.setPredictionNo(scale02.getPredictionNo());
            model03.setPredictionType("5");
            model03.setPredictionCombination(scale01.getPredictionNo() + "," + scale02.getPredictionNo());
            meterWorkCarActualFirstBakMapper.insertSelective(model03);
            //添加第二次皮重数据库
//            String actualSecondNo = sdf.format(new Date()) + meterWorkCarActualFirstMapper.getNewID(afl);
//            model04.setActualFirstNo(actualSecondNo);
            String actualSecondNo = PublicMethod.IdCreate(3);
            model04.setActualFirstNo(actualSecondNo);

            model04.setValueFlag(DbConstants.NET);
            model04.setUpdateManNo("system");
            model04.setUpdateManName("双委托结净更新");
            model04.setWeightType("1");
            model04.setUpdateTime(new Date());
            model04.setIsPreScale("1");
            model04.setPredictionNo(scale02.getPredictionNo());
            model04.setPredictionType("5");
            model04.setPredictionCombination(scale01.getPredictionNo() + "," + scale02.getPredictionNo());
            meterWorkCarActualFirstBakMapper.insertSelective(model04);

            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkCarActualBak mwca1 = meterWorkCarActualBakMapper.manualAddPre(model01.getActualFirstNo(), model02.getActualFirstNo(), scale01.getPredictionNo());
            BeanUtils.copyProperties(mwca1, actualRow1);
            MeterWorkCarActualBak mwca2 = meterWorkCarActualBakMapper.manualAddPre(model03.getActualFirstNo(), model04.getActualFirstNo(), scale02.getPredictionNo());
            BeanUtils.copyProperties(mwca2, actualRow2);

            //添加结净数据
//            String no = meterWorkCarActualBakMapper.getNewID(sdf.format(new Date()));
//            actualRow1.setActualNo(sdf.format(new Date()) + no);

            String no = PublicMethod.IdCreate(3);
            actualRow1.setActualNo(no);
            actualRow1.setNetTime(new Date());
            actualRow1.setNetMode(DbConstants.TYPE);
            actualRow1.setNote("双委托匹配结净");
            actualRow1.setNetMode("3");
            actualRow1.setUploadFlag("1");
            actualRow1.setValueFlag("1");
            actualRow1.setIsPreScale("1");
            actualRow1.setNetManNo(two.getNetManNo());
            actualRow1.setNetManName(two.getNetManName());
            actualRow1.setNetSpot3No(two.getNetSpot3No());
            actualRow1.setNetSpot3Name(two.getNetSpot3Name());
            actualRow1.setCreateManName(two.getCreateManName());
            actualRow1.setUpdateManNo(two.getUpdateManNo());
            actualRow1.setUpdateManName(two.getUpdateManName());
            actualRow1.setUpdateTime(two.getUpdateTime());
            actualRow1.setPredictionNo(scale01.getPredictionNo());
            actualRow1.setPredictionType("5");
            actualRow1.setPredictionCombination(scale01.getPredictionNo() + "," + scale02.getPredictionNo());
            actualRow1.setNetWeight(model01.getMeterWeight().subtract(model02.getMeterWeight()));
            if(actualRow1.getGrossTime().compareTo(actualRow1.getTareTime()) >= 0) {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot1No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot1Name());
                actualRow1.setNetManNo(actualRow1.getGrossManNo());
                actualRow1.setNetManName(actualRow1.getGrossManName());
                actualRow1.setNetTime(actualRow1.getGrossTime());
                actualRow1.setCreateTime(actualRow1.getGrossTime());
                actualRow1.setCreateManName(actualRow1.getGrossManName());
            }else {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot2No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot2Name());
                actualRow1.setNetManNo(actualRow1.getTareManNo());
                actualRow1.setNetManName(actualRow1.getTareManName());
                actualRow1.setNetTime(actualRow1.getTareTime());
                actualRow1.setCreateTime(actualRow1.getTareTime());
                actualRow1.setCreateManName(actualRow1.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(actualRow1);

//            String no1 = meterWorkCarActualBakMapper.getNewID(sdf.format(new Date()));
//            actualRow2.setActualNo(sdf.format(new Date()) + no1);

            String no1 = PublicMethod.IdCreate(3);
            actualRow2.setActualNo(no1);
            actualRow2.setNetTime(new Date());
            actualRow2.setNetMode(DbConstants.TYPE);
            actualRow2.setNote("双委托匹配结净");
            actualRow2.setNetMode("3");
            actualRow2.setValueFlag("1");
            actualRow2.setUploadFlag("1");
            actualRow2.setIsPreScale("1");
            actualRow2.setNetManNo(two.getNetManNo());
            actualRow2.setNetManName(two.getNetManName());
            actualRow2.setNetSpot3No(two.getNetSpot3No());
            actualRow2.setNetSpot3Name(two.getNetSpot3Name());
            actualRow2.setCreateManName(two.getCreateManName());
            actualRow2.setUpdateManNo(two.getUpdateManNo());
            actualRow2.setUpdateManName(two.getUpdateManName());
            actualRow2.setUpdateTime(two.getUpdateTime());
            actualRow2.setPredictionNo(scale02.getPredictionNo());
            actualRow2.setPredictionType("5");
            actualRow2.setPredictionCombination(scale01.getPredictionNo() + "," + scale02.getPredictionNo());
            actualRow2.setNetWeight(model03.getMeterWeight().subtract(model04.getMeterWeight()));
            if(actualRow2.getGrossTime().compareTo(actualRow2.getTareTime()) >= 0) {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot1No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot1Name());
                actualRow2.setNetManNo(actualRow2.getGrossManNo());
                actualRow2.setNetManName(actualRow2.getGrossManName());
                actualRow2.setNetTime(actualRow2.getGrossTime());
                actualRow2.setCreateTime(actualRow2.getGrossTime());
                actualRow2.setCreateManName(actualRow2.getGrossManName());
            }else {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot2No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot2Name());
                actualRow2.setNetManNo(actualRow2.getTareManNo());
                actualRow2.setNetManName(actualRow2.getTareManName());
                actualRow2.setNetTime(actualRow2.getTareTime());
                actualRow2.setCreateTime(actualRow2.getTareTime());
                actualRow2.setCreateManName(actualRow2.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(actualRow2);


            //更新预报数据
            scale01.setValueFlag(DbConstants.NET);
            scale01.setUpdateTime(new Date());
            scale01.setUpdateManNo("system");
            scale01.setUpdateManName("双委托结净更新");
            scale01.setUploadFlag("1");
            scale01.setPredictionType("5");
            scale01.setPredictionCombination(scale01.getPredictionNo() + "," + scale02.getPredictionNo());
            scale01.setMeasureStation(actualRow1.getNetSpot3No());
            scale01.setMeasureStationName(actualRow1.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(scale01);

            scale02.setValueFlag(DbConstants.NET);
            scale02.setUpdateTime(new Date());
            scale02.setUpdateManNo("system");
            scale02.setUpdateManName("双委托结净更新");
            scale02.setUploadFlag("1");
            scale02.setPredictionType("5");
            scale02.setPredictionCombination(scale01.getPredictionNo() + "," + scale02.getPredictionNo());
            scale02.setMeasureStation(actualRow2.getNetSpot3No());
            scale02.setMeasureStationName(actualRow2.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(scale02);
            //11、修改计量监控
            updateMonitor(one.getBaseSpotNo());
            //12、当存储一次数据或者结净时，将本车辆的预处理重量改为已使用
            updatePreWeight(one.getCarNo());
            updateMonitorOver(one.getActualFirstNo(), one.getBaseSpotNo());
            //补充附加重量的数据
            if (StringUtils.isNotEmpty(one.getAddWgtPk())) {
                insertAddWgtNo(one.getActualFirstNo(), one.getAddWgtPk(), one.getCreateManNo(), one.getCreateManName());
            }

            rm.setSucceed();
            rm.setData(one.getActualFirstNo());
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            return rm;
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！！>>>>" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return rm;
    }


    private void updatePreWeight(String CarNo) throws MarkerMetException{
        meterWorkPreWgttypeMapper.updatePreWeight(CarNo);
    }

    @Override
    public String mixedLoading(String actualNo1, String actualNo2, String actualNo3, String predictionNo1, String predictionNo2) {
        try{
            //查询所有的一次数据
            MeterWorkCarActualFirstBak mwcaf1 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualNo1);
            MeterWorkCarActualFirstBak mwcaf2 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualNo2);
            MeterWorkCarActualFirstBak mwcaf3 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualNo2);
            MeterWorkCarActualFirstBak mwcaf4 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualNo3);
            //查询所有的委托数据
            PreTrackScale pts1 = preTrackScaleMapper.selectByPrimaryKey(predictionNo1);
            PreTrackScale pts2 = preTrackScaleMapper.selectByPrimaryKey(predictionNo2);
            //定义新的结净数据实体
            MeterWorkCarActualBak actualRow1 = new MeterWorkCarActualBak();
            MeterWorkCarActualBak actualRow2 = new MeterWorkCarActualBak();

            //判断是否为混装逻辑
            if(mwcaf1.getMeterWeight().compareTo(mwcaf2.getMeterWeight()) == 1 ||
                    mwcaf1.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == 1 ||
                    mwcaf2.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == 1){
                return "请根据一次数据从小到大的规则，依次选择三条一次数据！！！！";
            }
            /*
            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
            String fromDate = simpleFormat.format(pts1.getCreateTime());
            String toDate = simpleFormat.format(pts2.getCreateTime());
            long from = simpleFormat.parse(fromDate).getTime();
            long to = simpleFormat.parse(toDate).getTime();
            int hours = (int) ((to - from) / (1000 * 60 * 60));
            if ( pts1.getMatterNo().equals(pts2.getMatterNo()) &&
                    pts1.getForwardingUnitNo().equals(pts2.getForwardingUnitNo()) &&
                    hours <= 4) {
                return "委托信息不符合混装委托规则，请确认!!";
            }
             */

            //判断一次数据是否存在
            if (mwcaf1 == null || mwcaf2 == null || mwcaf3 == null || mwcaf4 == null) {
                return "未查询到相应的一次数据！！";
            }
            //判断一次数据是否已结净
            if (mwcaf1.getValueFlag().equals("2") || mwcaf2.getValueFlag().equals("2") ||
                    mwcaf3.getValueFlag().equals("2") || mwcaf4.getValueFlag().equals("2")) {
                return "存在已结净的一次数据！！";
            }
            //判断委托数据是否存在
            if (pts1 == null || pts2 == null) {
                return "未查询到相应的委托数据！！";
            }
            //判断委托数据是否已使用
            if (!pts1.getValueFlag().equals("1") || !pts1.getValueFlag().equals("1")) {
                return "委托不是未使用状态！！";
            }

            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkCarActualFirstBak first1 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo1, predictionNo1);
            BeanUtils.copyProperties(first1,mwcaf1);
            MeterWorkCarActualFirstBak first2 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo2, predictionNo1);
            BeanUtils.copyProperties(first2, mwcaf2);
            MeterWorkCarActualFirstBak first3 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo2, predictionNo2);
            BeanUtils.copyProperties(first3,mwcaf3);
            MeterWorkCarActualFirstBak first4 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo3, predictionNo2);
            BeanUtils.copyProperties(first4, mwcaf4);


            //更新一次数据
            //更新第一次皮重数据
            mwcaf1.setValueFlag(DbConstants.NET);
            mwcaf1.setUpdateManNo("system");
            mwcaf1.setUpdateManName("混装结净更新");
            mwcaf1.setUpdateTime(new Date());
            mwcaf1.setIsPreScale("1");
            mwcaf1.setPredictionNo(predictionNo1);
            mwcaf1.setPredictionType("7");
            mwcaf1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf1);
            //更新第一次毛重数据
            mwcaf2.setValueFlag(DbConstants.NET);
            mwcaf2.setUpdateManNo("system");
            mwcaf2.setUpdateManName("混装结净更新");
            mwcaf2.setUpdateTime(new Date());
            mwcaf2.setIsPreScale("1");
            mwcaf2.setPredictionNo(predictionNo1);
            mwcaf2.setPredictionType("7");
            mwcaf2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf2);
            //添加第二次皮重数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
//            String actualFirstNo = sdf.format(new Date()) + meterWorkCarActualFirstMapper.getNewID(afl);
//            mwcaf3.setActualFirstNo(actualFirstNo);
            String actualFirstNo = PublicMethod.IdCreate(3);
            mwcaf3.setActualFirstNo(actualFirstNo);


            mwcaf3.setValueFlag(DbConstants.NET);
            mwcaf3.setUpdateManNo("system");
            mwcaf3.setUpdateManName("混装结净更新");
            mwcaf3.setUpdateTime(new Date());
            mwcaf3.setIsPreScale("1");
            mwcaf3.setPredictionNo(predictionNo2);
            mwcaf3.setPredictionType("7");
            mwcaf3.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            mwcaf3.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insertSelective(mwcaf3);
            //更新第二次毛重数据
            mwcaf4.setValueFlag(DbConstants.NET);
            mwcaf4.setUpdateManNo("system");
            mwcaf4.setUpdateManName("混装结净更新");
            mwcaf4.setUpdateTime(new Date());
            mwcaf4.setIsPreScale("1");
            mwcaf4.setPredictionNo(predictionNo2);
            mwcaf4.setPredictionType("7");
            mwcaf4.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf4);
            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkCarActualBak mwca1 = meterWorkCarActualBakMapper.manualAddPre(actualNo2, actualNo1, predictionNo1);
            BeanUtils.copyProperties(mwca1, actualRow1);
            MeterWorkCarActualBak mwca2 = meterWorkCarActualBakMapper.manualAddPre(actualNo3, actualFirstNo, predictionNo2);
            BeanUtils.copyProperties(mwca2,actualRow2);

            //添加结净数据
//            String no = meterWorkCarActualBakMapper.getNewID(sdf.format(new Date()));
//            actualRow1.setActualNo(sdf.format(new Date()) + no);

            String no = PublicMethod.IdCreate(3);
            actualRow1.setActualNo(no);
            actualRow1.setNetTime(new Date());
            actualRow1.setNetMode(DbConstants.TYPE);
            actualRow1.setNote("混装匹配结净");
            actualRow1.setNetMode("4");
            actualRow1.setUploadFlag("1");
            actualRow1.setIsPreScale("1");
            actualRow1.setValueFlag("1");
            actualRow1.setCreateTime(new Date());
            actualRow1.setCreateManName("混装匹配结净");
            actualRow1.setPredictionType("7");
            actualRow1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow1.setPredictionNo(predictionNo1);
            actualRow1.setNetWeight(mwcaf2.getMeterWeight().subtract(mwcaf1.getMeterWeight()));
            if(actualRow1.getGrossTime().compareTo(actualRow1.getTareTime()) >= 0) {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot1No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot1Name());
                actualRow1.setNetManNo(actualRow1.getGrossManNo());
                actualRow1.setNetManName(actualRow1.getGrossManName());
                actualRow1.setNetTime(actualRow1.getGrossTime());
                actualRow1.setCreateTime(actualRow1.getGrossTime());
                actualRow1.setCreateManName(actualRow1.getGrossManName());
            }else {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot2No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot2Name());
                actualRow1.setNetManNo(actualRow1.getTareManNo());
                actualRow1.setNetManName(actualRow1.getTareManName());
                actualRow1.setNetTime(actualRow1.getTareTime());
                actualRow1.setCreateTime(actualRow1.getTareTime());
                actualRow1.setCreateManName(actualRow1.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(actualRow1);

//            String no1 = meterWorkCarActualBakMapper.getNewID(sdf.format(new Date()));
//            actualRow2.setActualNo(sdf.format(new Date()) + no1);

            String no1 = PublicMethod.IdCreate(3);
            actualRow2.setActualNo(no1);
            actualRow2.setNetTime(new Date());
            actualRow2.setNetMode(DbConstants.TYPE);
            actualRow2.setNote("混装匹配结净");
            actualRow2.setNetMode("4");
            actualRow2.setUploadFlag("1");
            actualRow2.setIsPreScale("1");
            actualRow2.setValueFlag("1");
            actualRow2.setCreateTime(new Date());
            actualRow2.setCreateManName("混装匹配结净");
            actualRow2.setPredictionType("7");
            actualRow2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow2.setPredictionNo(predictionNo2);
            actualRow2.setNetWeight(mwcaf4.getMeterWeight().subtract(mwcaf3.getMeterWeight()));
            if(actualRow2.getGrossTime().compareTo(actualRow2.getTareTime()) >= 0) {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot1No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot1Name());
                actualRow2.setNetManNo(actualRow2.getGrossManNo());
                actualRow2.setNetManName(actualRow2.getGrossManName());
                actualRow2.setNetTime(actualRow2.getGrossTime());
                actualRow2.setCreateTime(actualRow2.getGrossTime());
                actualRow2.setCreateManName(actualRow2.getGrossManName());
            }else {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot2No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot2Name());
                actualRow2.setNetManNo(actualRow2.getTareManNo());
                actualRow2.setNetManName(actualRow2.getTareManName());
                actualRow2.setNetTime(actualRow2.getTareTime());
                actualRow2.setCreateTime(actualRow2.getTareTime());
                actualRow2.setCreateManName(actualRow2.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(actualRow2);

            //更新预报数据
            PreTrackScale scale = new PreTrackScale();
            scale.setPredictionNo(predictionNo1);
            scale.setValueFlag(DbConstants.NET);
            scale.setUpdateTime(new Date());
            scale.setUpdateManNo("system");
            scale.setUpdateManName("混装结净更新");
            scale.setPredictionType("7");
            scale.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale.setUploadFlag("1");
            scale.setMeasureStation(actualRow1.getNetSpot3No());
            scale.setMeasureStationName(actualRow1.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(scale);

            PreTrackScale scale1 = new PreTrackScale();
            scale1.setPredictionNo(predictionNo2);
            scale1.setValueFlag(DbConstants.NET);
            scale1.setUpdateTime(new Date());
            scale1.setUpdateManNo("system");
            scale1.setUpdateManName("混装结净更新");
            scale1.setPredictionType("7");
            scale1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale1.setUploadFlag("1");
            scale1.setMeasureStation(actualRow2.getNetSpot3No());
            scale1.setMeasureStationName(actualRow2.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(scale1);
        }
        catch (Exception e){
            e.printStackTrace();
            return "操作异常，请确认";
        }
        return "";
    }

    @Override
    public String mixedDischarge(String actualNo1, String actualNo2, String actualNo3, String predictionNo1, String predictionNo2) {
        try{
            //查询所有的一次数据
            MeterWorkCarActualFirstBak mwcaf1 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualNo1);
            MeterWorkCarActualFirstBak mwcaf2 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualNo2);
            MeterWorkCarActualFirstBak mwcaf3 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualNo2);
            MeterWorkCarActualFirstBak mwcaf4 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualNo3);
            //查询所有的委托数据
            PreTrackScale pts1 = preTrackScaleMapper.selectByPrimaryKey(predictionNo1);
            PreTrackScale pts2 = preTrackScaleMapper.selectByPrimaryKey(predictionNo2);
            //定义新的结净数据实体
            MeterWorkCarActualBak actualRow1 = new MeterWorkCarActualBak();
            MeterWorkCarActualBak actualRow2 = new MeterWorkCarActualBak();
            //判断是否为混装逻辑
            if(mwcaf1.getMeterWeight().compareTo(mwcaf2.getMeterWeight()) == -1 ||
                    mwcaf1.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == -1 ||
                    mwcaf2.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == -1){
                return "请根据一次数据从大到小的规则，依次选择三条一次数据！！";
            }
            /*
            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
            String fromDate = simpleFormat.format(pts1.getCreateTime());
            String toDate = simpleFormat.format(pts2.getCreateTime());
            long from = simpleFormat.parse(fromDate).getTime();
            long to = simpleFormat.parse(toDate).getTime();
            int minutes = (int) ((to - from) / (1000 * 60));
            //判断混卸 物料相同、AB发货单位相同、收货单位一定不一致：签发时间10分钟内
            if (pts1.getMatterNo().equals(pts2.getMatterNo()) &&
                    pts1.getForwardingUnitNo().equals(pts2.getForwardingUnitNo()) &&
                    !pts1.getReceivingUintNo().equals(pts2.getReceivingUintNo()) &&
                    minutes <= 10) {
                return "委托信息不符合混卸委托规则，请确认!!";
            }
             */
            //判断一次数据是否存在
            if (mwcaf1 == null || mwcaf2 == null || mwcaf3 == null || mwcaf4 == null) {
                return "未查询到相应的一次数据！！";
            }
            //判断一次数据是否已结净
            if (mwcaf1.getValueFlag().equals("2") || mwcaf2.getValueFlag().equals("2") ||
                    mwcaf3.getValueFlag().equals("2") || mwcaf4.getValueFlag().equals("2")) {
                return "存在已结净的一次数据！！";
            }
            //判断委托数据是否存在
            if (pts1 == null || pts2 == null) {
                return "未查询到相应的委托数据！！";
            }
            //判断委托数据是否已使用
            if (!pts1.getValueFlag().equals("1") || !pts1.getValueFlag().equals("1")) {
                return "委托不是未使用状态！！";
            }

            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkCarActualFirstBak first1 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo1, predictionNo1);
            BeanUtils.copyProperties(first1,mwcaf1);
            MeterWorkCarActualFirstBak first2 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo2, predictionNo1);
            BeanUtils.copyProperties(first2, mwcaf2);
            MeterWorkCarActualFirstBak first3 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo2, predictionNo2);
            BeanUtils.copyProperties(first3,mwcaf3);
            MeterWorkCarActualFirstBak first4 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo3, predictionNo2);
            BeanUtils.copyProperties(first4, mwcaf4);

            //更新一次数据
            //更新第一次毛重数据
            mwcaf1.setValueFlag(DbConstants.NET);
            mwcaf1.setUpdateManNo("system");
            mwcaf1.setUpdateManName("混卸结净更新");
            mwcaf1.setUpdateTime(new Date());
            mwcaf1.setIsPreScale("1");
            mwcaf1.setPredictionNo(predictionNo1);
            mwcaf1.setPredictionType("6");
            mwcaf1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf1);
            //更新第一次皮重数据
            mwcaf2.setValueFlag(DbConstants.NET);
            mwcaf2.setUpdateManNo("system");
            mwcaf2.setUpdateManName("混卸结净更新");
            mwcaf2.setUpdateTime(new Date());
            mwcaf2.setIsPreScale("1");
            mwcaf2.setPredictionNo(predictionNo1);
            mwcaf2.setPredictionType("6");
            mwcaf2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf2);
            //添加第二次毛重数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
//            String actualFirstNo = sdf.format(new Date()) + meterWorkCarActualFirstMapper.getNewID(afl);
//            mwcaf3.setActualFirstNo(actualFirstNo);
            String actualFirstNo = PublicMethod.IdCreate(3);
            mwcaf3.setActualFirstNo(actualFirstNo);

            mwcaf3.setValueFlag(DbConstants.NET);
            mwcaf3.setUpdateManNo("system");
            mwcaf3.setUpdateManName("混卸结净更新");
            mwcaf3.setUpdateTime(new Date());
            mwcaf3.setIsPreScale("1");
            mwcaf3.setPredictionNo(predictionNo2);
            mwcaf3.setPredictionType("6");
            mwcaf3.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            mwcaf3.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insertSelective(mwcaf3);
            //更新第二次皮重数据
            mwcaf4.setValueFlag(DbConstants.NET);
            mwcaf4.setUpdateManNo("system");
            mwcaf4.setUpdateManName("混卸结净更新");
            mwcaf4.setUpdateTime(new Date());
            mwcaf4.setIsPreScale("1");
            mwcaf4.setPredictionNo(predictionNo2);
            mwcaf4.setPredictionType("6");
            mwcaf4.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf4);
            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkCarActualBak mwca1 = meterWorkCarActualBakMapper.manualAddPre(actualNo1, actualNo2, predictionNo1);
            BeanUtils.copyProperties(mwca1, actualRow1);
            MeterWorkCarActualBak mwca2 = meterWorkCarActualBakMapper.manualAddPre(actualFirstNo, actualNo3, predictionNo2);
            BeanUtils.copyProperties(mwca2,actualRow2);

            //添加结净数据
//            String no = meterWorkCarActualBakMapper.getNewID(sdf.format(new Date()));
//            actualRow1.setActualNo(sdf.format(new Date()) + no);

            String no = PublicMethod.IdCreate(3);
            actualRow1.setActualNo(no);
            actualRow1.setNetTime(new Date());
            actualRow1.setNetMode(DbConstants.TYPE);
            actualRow1.setNote("混卸匹配结净");
            actualRow1.setNetMode("5");
            actualRow1.setUploadFlag("1");
            actualRow1.setIsPreScale("1");
            actualRow1.setValueFlag("1");
            actualRow1.setCreateManName("混卸匹配结净");
            actualRow1.setCreateTime(new Date());
            actualRow1.setPredictionType("6");
            actualRow1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow1.setPredictionNo(predictionNo1);
            actualRow1.setNetWeight(mwcaf1.getMeterWeight().subtract(mwcaf2.getMeterWeight()));
            if(actualRow1.getGrossTime().compareTo(actualRow1.getTareTime()) >= 0) {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot1No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot1Name());
                actualRow1.setNetManNo(actualRow1.getGrossManNo());
                actualRow1.setNetManName(actualRow1.getGrossManName());
                actualRow1.setNetTime(actualRow1.getGrossTime());
                actualRow1.setCreateTime(actualRow1.getGrossTime());
                actualRow1.setCreateManName(actualRow1.getGrossManName());
            }else {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot2No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot2Name());
                actualRow1.setNetManNo(actualRow1.getTareManNo());
                actualRow1.setNetManName(actualRow1.getTareManName());
                actualRow1.setNetTime(actualRow1.getTareTime());
                actualRow1.setCreateTime(actualRow1.getTareTime());
                actualRow1.setCreateManName(actualRow1.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(actualRow1);
//            String no1 = meterWorkCarActualBakMapper.getNewID(sdf.format(new Date()));
//            actualRow2.setActualNo(sdf.format(new Date()) + no1);

            String no1 = PublicMethod.IdCreate(3);
            actualRow2.setActualNo(no1);
            actualRow2.setNetTime(new Date());
            actualRow2.setNetMode(DbConstants.TYPE);
            actualRow2.setNote("混卸匹配结净");
            actualRow2.setNetMode("5");
            actualRow2.setUploadFlag("1");
            actualRow2.setIsPreScale("1");
            actualRow2.setValueFlag("1");
            actualRow2.setCreateManName("混卸匹配结净");
            actualRow2.setCreateTime(new Date());
            actualRow2.setPredictionType("6");
            actualRow2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow2.setPredictionNo(predictionNo2);
            actualRow2.setNetWeight(mwcaf3.getMeterWeight().subtract(mwcaf4.getMeterWeight()));
            if(actualRow2.getGrossTime().compareTo(actualRow2.getTareTime()) >= 0) {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot1No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot1Name());
                actualRow2.setNetManNo(actualRow2.getGrossManNo());
                actualRow2.setNetManName(actualRow2.getGrossManName());
                actualRow2.setNetTime(actualRow2.getGrossTime());
                actualRow2.setCreateTime(actualRow2.getGrossTime());
                actualRow2.setCreateManName(actualRow2.getGrossManName());
            }else {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot2No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot2Name());
                actualRow2.setNetManNo(actualRow2.getTareManNo());
                actualRow2.setNetManName(actualRow2.getTareManName());
                actualRow2.setNetTime(actualRow2.getTareTime());
                actualRow2.setCreateTime(actualRow2.getTareTime());
                actualRow2.setCreateManName(actualRow2.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(actualRow2);

            //更新预报数据
            PreTrackScale scale = new PreTrackScale();
            scale.setPredictionNo(predictionNo1);
            scale.setValueFlag(DbConstants.NET);
            scale.setUpdateTime(new Date());
            scale.setUpdateManNo("system");
            scale.setUpdateManName("混卸结净更新");
            scale.setUploadFlag("1");
            scale.setValueFlag("2");
            //预报类型：双委托（5）、预报组合：A预报，B预报
            scale.setPredictionType("6");
            scale.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale.setMeasureStation(actualRow1.getNetSpot3No());
            scale.setMeasureStationName(actualRow1.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(scale);

            PreTrackScale scale1 = new PreTrackScale();
            scale1.setPredictionNo(predictionNo2);
            scale1.setValueFlag(DbConstants.NET);
            scale1.setUpdateTime(new Date());
            scale1.setUpdateManNo("system");
            scale1.setUpdateManName("混卸结净更新");
            scale1.setUploadFlag("1");
            scale1.setValueFlag("2");
            scale1.setPredictionType("6");
            scale1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale1.setMeasureStation(actualRow2.getNetSpot3No());
            scale1.setMeasureStationName(actualRow2.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(scale1);
        }
        catch (Exception e){
            e.printStackTrace();
            return "操作异常，请确认";
        }
        return "";
    }

    @Override
    public RESTfulResult doubleEntrustmentMatch(String actualNo1, String actualNo2, String predictionNo1, String predictionNo2) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        try{
            //查询出一次计量数据
            MeterWorkCarActualFirstBak mwcaf01 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualNo1);
            MeterWorkCarActualFirstBak mwcaf02 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualNo1);
            MeterWorkCarActualFirstBak mwcaf03 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualNo2);
            MeterWorkCarActualFirstBak mwcaf04 = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualNo2);
            //查询出所有委托信息
            PreTrackScale pts01 = preTrackScaleMapper.selectByPrimaryKey(predictionNo1);
            PreTrackScale pts02 = preTrackScaleMapper.selectByPrimaryKey(predictionNo2);
            //定义两个结净类
            MeterWorkCarActualBak actualRow1 = new MeterWorkCarActualBak();
            MeterWorkCarActualBak actualRow2 = new MeterWorkCarActualBak();

            BigDecimal num01 = mwcaf01.getMeterWeight().subtract(mwcaf03.getMeterWeight());
            //判断一次数据和委托信息是否存存在
            if(mwcaf01 == null || mwcaf03 == null ){
                rm.setMessage("未找到相应的一次计量信息,请确认！!");
                return rm;
            }
            if(pts01 == null || pts02 == null){
                rm.setMessage("未找到相应的委托信息,请确认！！");
                return rm;
            }
            //A一次数据重量>B一次数据重量
            if(num01.compareTo(BigDecimal.valueOf(0)) == -1){
                rm.setMessage("请根据一次数据从大到小的规则，依次选择两条一次数据！！");
                return rm;
            }
            //判断双委托 物料相同、A委托的收货单位是B委托的发货单位 或者 B委托的收货单位是A委托的发货单位
            if(!(pts01.getMatterNo().equals(pts02.getMatterNo()) &&
                    (pts01.getReceivingUintNo().equals(pts02.getForwardingUnitNo()) ||
                            pts01.getForwardingUnitNo().equals(pts02.getReceivingUintNo())))) {
                rm.setMessage("委托信息不符合双委托规则，请确认!!");
                return rm;
            }
            if(!mwcaf01.getCarNo().equals(mwcaf03.getCarNo())){
                rm.setMessage("两条一次计量数据车号不一致,请确认！！");
                return rm;
            }
            //结净数据不能小于400
            if(num01.compareTo(BigDecimal.valueOf(400)) == -1){
                rm.setMessage("选择的两条一次计量数据不能小于400，请确认！！");
                return rm;
            }
            //委托信息是否为有效数据
            if(pts01.getValueFlag().equals("2") || pts02.getValueFlag().equals("2")){
                rm.setMessage("委托信息已使用，请确认！！");
                return rm;
            }
            if(pts01.getValueFlag().equals("3") || pts02.getValueFlag().equals("3")){
                rm.setMessage("委托信息已作废，请确认！！");
                return rm;
            }
            //一次数据是否为有效
            if(mwcaf01.getValueFlag().equals("2") || mwcaf03.getValueFlag().equals("2")){
                rm.setMessage("一次计量数据已结净，请确认！！");
                return rm;
            }
            if(mwcaf01.getValueFlag().equals("0") || mwcaf03.getValueFlag().equals("0")){
                rm.setMessage("一次计量数据已作废，请确认！！");
                return rm;
            }


            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkCarActualFirstBak first1 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo1, predictionNo1);
            BeanUtils.copyProperties(first1,mwcaf01);
            MeterWorkCarActualFirstBak first2 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo2, predictionNo1);
            BeanUtils.copyProperties(first2, mwcaf02);
            MeterWorkCarActualFirstBak first3 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo1, predictionNo2);
            BeanUtils.copyProperties(first3,mwcaf03);
            MeterWorkCarActualFirstBak first4 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo2, predictionNo2);
            BeanUtils.copyProperties(first4, mwcaf04);

            //更新一次数据
            //更新第一次毛重数据库
            mwcaf01.setValueFlag(DbConstants.NET);
            mwcaf01.setUpdateManNo("system");
            mwcaf01.setUpdateManName("双委托结净更新");
            mwcaf01.setUpdateTime(new Date());
            mwcaf01.setIsPreScale("1");
            mwcaf01.setPredictionNo(predictionNo1);
            mwcaf01.setPredictionType("5");
            mwcaf01.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf01);
            //更新第一次皮重数据库
            mwcaf02.setValueFlag(DbConstants.NET);
            mwcaf02.setUpdateManNo("system");
            mwcaf02.setUpdateManName("双委托结净更新");
            mwcaf02.setUpdateTime(new Date());
            mwcaf02.setIsPreScale("1");
            mwcaf02.setPredictionNo(predictionNo1);
            mwcaf02.setPredictionType("5");
            mwcaf02.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(mwcaf02);
            //添加第一次毛重数据库
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
//            String actualFirstNo = sdf.format(new Date()) + meterWorkCarActualFirstMapper.getNewID(afl);
//            mwcaf03.setActualFirstNo(actualFirstNo);
            String actualFirstNo = PublicMethod.IdCreate(3);
            mwcaf03.setActualFirstNo(actualFirstNo);

            mwcaf03.setValueFlag(DbConstants.NET);
            mwcaf03.setUpdateManNo("system");
            mwcaf03.setUpdateManName("双委托结净更新");
            mwcaf03.setUpdateTime(new Date());
            mwcaf03.setIsPreScale("1");
            mwcaf03.setPredictionNo(predictionNo2);
            mwcaf03.setPredictionType("5");
            mwcaf03.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            mwcaf03.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insertSelective(mwcaf03);
            //添加第二次皮重数据库
            SimpleDateFormat sdf01 = new SimpleDateFormat("yyyyMMdd");
            String afl01 = sdf01.format(new Date());
//            String actualSecondNo = sdf01.format(new Date()) + meterWorkCarActualFirstMapper.getNewID(afl01);
//            mwcaf04.setActualFirstNo(actualSecondNo);
            String actualSecondNo = PublicMethod.IdCreate(3);
            mwcaf04.setActualFirstNo(actualSecondNo);

            mwcaf04.setValueFlag(DbConstants.NET);
            mwcaf04.setUpdateManNo("system");
            mwcaf04.setUpdateManName("双委托结净更新");
            mwcaf04.setUpdateTime(new Date());
            mwcaf04.setIsPreScale("1");
            mwcaf04.setPredictionNo(predictionNo2);
            mwcaf04.setPredictionType("5");
            mwcaf04.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            mwcaf04.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insertSelective(mwcaf04);

            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkCarActualBak mwca1 = meterWorkCarActualBakMapper.manualAddPre(actualNo1, actualNo2, predictionNo1);
            BeanUtils.copyProperties(mwca1, actualRow1);
            MeterWorkCarActualBak mwca2 = meterWorkCarActualBakMapper.manualAddPre(actualFirstNo, actualSecondNo, predictionNo2);
            BeanUtils.copyProperties(mwca2,actualRow2);

            //添加结净数据
//            String no = meterWorkCarActualBakMapper.getNewID(sdf.format(new Date()));
//            actualRow1.setActualNo(sdf.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            actualRow1.setActualNo(no);
            actualRow1.setNetTime(new Date());
            actualRow1.setNetMode(DbConstants.TYPE);
            actualRow1.setNote("双委托匹配结净");
            actualRow1.setNetMode("3");
            actualRow1.setUploadFlag("1");
            actualRow1.setIsPreScale("1");
            actualRow1.setValueFlag("1");
            actualRow1.setPredictionNo(predictionNo1);
            actualRow1.setPredictionType("5");
            actualRow1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow1.setNetWeight(mwcaf01.getMeterWeight().subtract(mwcaf02.getMeterWeight()));
            actualRow1.setCreateTime(new Date());
            if(actualRow1.getGrossTime().compareTo(actualRow1.getTareTime()) >= 0) {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot1No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot1Name());
                actualRow1.setNetManNo(actualRow1.getGrossManNo());
                actualRow1.setNetManName(actualRow1.getGrossManName());
                actualRow1.setNetTime(actualRow1.getGrossTime());
                actualRow1.setCreateTime(actualRow1.getGrossTime());
                actualRow1.setCreateManName(actualRow1.getGrossManName());
            }else {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot2No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot2Name());
                actualRow1.setNetManNo(actualRow1.getTareManNo());
                actualRow1.setNetManName(actualRow1.getTareManName());
                actualRow1.setNetTime(actualRow1.getTareTime());
                actualRow1.setCreateTime(actualRow1.getTareTime());
                actualRow1.setCreateManName(actualRow1.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(actualRow1);

//            String no1 = meterWorkCarActualBakMapper.getNewID(sdf.format(new Date()));
//            actualRow2.setActualNo(sdf.format(new Date()) + no1);
            String no1 = PublicMethod.IdCreate(3);
            actualRow2.setActualNo(no1);
            actualRow2.setNetTime(new Date());
            actualRow2.setNetMode(DbConstants.TYPE);
            actualRow2.setNote("双委托匹配结净");
            actualRow2.setNetMode("3");
            actualRow2.setUploadFlag("1");
            actualRow2.setIsPreScale("1");
            actualRow2.setValueFlag("1");
            actualRow2.setPredictionNo(predictionNo2);
            actualRow2.setPredictionType("5");
            actualRow2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow2.setNetWeight(mwcaf03.getMeterWeight().subtract(mwcaf04.getMeterWeight()));
            actualRow2.setCreateTime(new Date());
            if(actualRow2.getGrossTime().compareTo(actualRow2.getTareTime()) >= 0) {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot1No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot1Name());
                actualRow2.setNetManNo(actualRow2.getGrossManNo());
                actualRow2.setNetManName(actualRow2.getGrossManName());
                actualRow2.setNetTime(actualRow2.getGrossTime());
                actualRow2.setCreateTime(actualRow2.getGrossTime());
                actualRow2.setCreateManName(actualRow2.getGrossManName());
            }else {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot2No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot2Name());
                actualRow2.setNetManNo(actualRow2.getTareManNo());
                actualRow2.setNetManName(actualRow2.getTareManName());
                actualRow2.setNetTime(actualRow2.getTareTime());
                actualRow2.setCreateTime(actualRow2.getTareTime());
                actualRow2.setCreateManName(actualRow2.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(actualRow2);

            //更新预报数据
            PreTrackScale scale = new PreTrackScale();
            scale.setPredictionNo(predictionNo1);
            scale.setValueFlag(DbConstants.NET);
            scale.setUpdateTime(new Date());
            scale.setUpdateManNo("system");
            scale.setUpdateManName("双委托结净更新");
            scale.setUploadFlag("1");
            scale.setPredictionType("5");
            scale.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale.setMeasureStation(actualRow1.getNetSpot3No());
            scale.setMeasureStationName(actualRow1.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(scale);

            PreTrackScale scale1 = new PreTrackScale();
            scale1.setPredictionNo(predictionNo2);
            scale1.setValueFlag(DbConstants.NET);
            scale1.setUpdateTime(new Date());
            scale1.setUpdateManNo("system");
            scale1.setUpdateManName("双委托结净更新");
            scale1.setUploadFlag("1");
            scale1.setPredictionType("5");
            scale1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale1.setMeasureStation(actualRow2.getNetSpot3No());
            scale1.setMeasureStationName(actualRow2.getNetSpot3Name());
            preTrackScaleMapper.updateByPrimaryKeySelective(scale1);

            rm.setSucceed();
            rm.setMessage("操作成功!!");
            rm.setCode("200");
            return rm;
        }catch(Exception e){
            e.printStackTrace();
            rm.setMessage("操作异常，请确认");
            return rm;
        }
    }



    /**
     * 内倒车辆结净
     *
     * @param secondDb 二次计量数据
     * @param netDb    净重数据
     * @param saveTare 0 不保存历史皮，1保存历史皮
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(value = "tm", rollbackFor = Exception.class)
    public RESTfulResult doNetTermTare(MeterWorkCarActualFirstBak secondDb, MeterWorkCarActualBak netDb, String saveTare) throws Exception {
        try{
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("200");

            //1、存储当前计量的数据
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String afl = formatter.format(new Date());
//            String no = meterWorkCarActualFirstMapper.getNewID(afl);
//            secondDb.setActualFirstNo(formatter.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            secondDb.setActualFirstNo(no);

            secondDb.setValueFlag("2");//结净
            secondDb.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insertSelective(secondDb);
//            String no1 = meterWorkCarActualBakMapper.getNewID(afl);
//            netDb.setActualNo(afl + no1);
            String no1 = PublicMethod.IdCreate(3);
            netDb.setActualNo(no1);
            if (netDb != null && netDb.getNetWeight() != null) {
                //2021年3月7日 净重保留两位小数；
                BigDecimal net;
                net = new BigDecimal(String.valueOf(netDb.getNetWeight())).setScale(2, BigDecimal.ROUND_HALF_UP);
                netDb.setNetWeight(net);
            }
            netDb.setGrossTime(secondDb.getCreateTime());
            netDb.setNetTime(new Date());
            netDb.setUploadFlag("1");
            netDb.setActualFirst1No(secondDb.getActualFirstNo());
            netDb.setCreateTime(new Date());
            if(netDb.getGrossTime().compareTo(netDb.getTareTime()) >= 0) {
                netDb.setNetSpot3No(netDb.getBaseSpot1No());
                netDb.setNetSpot3Name(netDb.getBaseSpot1Name());
                netDb.setNetManNo(netDb.getGrossManNo());
                netDb.setNetManName(netDb.getGrossManName());
                netDb.setNetTime(netDb.getGrossTime());
                netDb.setCreateTime(netDb.getGrossTime());
                netDb.setCreateManName(netDb.getGrossManName());
            }else {
                netDb.setNetSpot3No(netDb.getBaseSpot2No());
                netDb.setNetSpot3Name(netDb.getBaseSpot2Name());
                netDb.setNetManNo(netDb.getTareManNo());
                netDb.setNetManName(netDb.getTareManName());
                netDb.setNetTime(netDb.getTareTime());
                netDb.setCreateTime(netDb.getTareTime());
                netDb.setCreateManName(netDb.getTareManName());
            }
            meterWorkCarActualBakMapper.insert(netDb);

            //4、将期限皮重转化为一条皮重计量数据保存
            MeterBaseTermTareData mbttd = new MeterBaseTermTareData();
            HashMap mbMap = new HashMap();
            SimpleDateFormat mbsdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            mbMap.put("endTime",mbsdf.format(new Date()));
            if(StringUtils.isNotEmpty(netDb.getActualFirst2No())){
                mbMap.put("dataNo",netDb.getActualFirst2No());
                mbttd = meterBaseTermTareDataMapper.selectByDataNo(mbMap);
            }
            else{
                mbMap.put("carNo",netDb.getCarNo());
                mbttd = meterBaseTermTareDataMapper.selectByCarNo(mbMap);
            }
            MeterWorkCarActualFirstBak imTare = new MeterWorkCarActualFirstBak();
            //补充数据
            String afl1 = formatter.format(new Date());
//            String imTareNO = meterWorkCarActualFirstMapper.getNewID(afl1);
//            String actualNo = afl1 + imTareNO;
            String actualNo = PublicMethod.IdCreate(3);
            imTare.setActualFirstNo(no);
            imTare.setActualFirstNo(actualNo);
            imTare.setMeterTypeNo(secondDb.getMeterTypeNo());
            imTare.setMeterTypeName(secondDb.getMeterTypeName());
            imTare.setBaseSpotNo(mbttd.getBaseSpotNo());
            imTare.setBaseSpotName(mbttd.getBaseSpotName());
            imTare.setCarNo(mbttd.getCarNo());
            imTare.setCreateTime(mbttd.getMeterTime());
            imTare.setCreateManNo(mbttd.getCreateManNo());
            imTare.setCreateManName(mbttd.getCreateManName());
            imTare.setMeterWeight(mbttd.getMeterWeight());
            imTare.setWeightType("2");//期限皮重
            imTare.setValueFlag("2");//结净
            imTare.setDataSource("1");
            meterWorkCarActualFirstBakMapper.insertSelective(imTare);
            //将结净数据中的皮重编号修改
            MeterWorkCarActualBak mwca = new MeterWorkCarActualBak();
            mwca.setActualNo(netDb.getActualNo());
            mwca.setActualFirst2No(actualNo);
            mwca.setTareWeight(mbttd.getMeterWeight());
            //新增修改结净数据汇总皮重计量点和时间   --20220724杜勇
            mwca.setTareTime(mbttd.getMeterTime());
            mwca.setBaseSpot2No(mbttd.getBaseSpotNo());
            mwca.setBaseSpot2Name(mbttd.getBaseSpotName());
            mwca.setNetWeight(secondDb.getMeterWeight().subtract(imTare.getMeterWeight()));
            meterWorkCarActualBakMapper.updateByPrimaryKeySelective(mwca);

            //皮重数据匹配委托
            MeterWorkCarActualFirstBak mwcaf2 = meterWorkCarActualFirstBakMapper.selectAndMatch(actualNo, netDb.getPredictionNo());
//            String nomwcaf2 = meterWorkCarActualFirstMapper.getNewID(afl);
            String nomwcaf2 = PublicMethod.IdCreate(3);
            mwcaf2.setIsPreScale("1");
            meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(mwcaf2);

            //判断结净数据是否匹配委托
            if (StringUtils.isNotEmpty(netDb.getPredictionNo())) {
                PreTrackScale preTrackScale = new PreTrackScale();
                preTrackScale.setPredictionNo(netDb.getPredictionNo());
                preTrackScale.setValueFlag("2");
                preTrackScale.setUploadFlag("1");
                preTrackScale.setMeasureStation(netDb.getNetSpot3No());
                preTrackScale.setMeasureStationName(netDb.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(preTrackScale);
            }

            //4、存储历史皮重及历史皮重明细信息
            if ("1".equals(saveTare)) {
                MeterWorkCarActualFirstBak oci = new MeterWorkCarActualFirstBak();
                oci.setCarNo(netDb.getCarNo());
                oci.setCreateTime(mbttd.getCreateTime());
                oci.setBaseSpotNo(mbttd.getBaseSpotNo());
                oci.setBaseSpotName(mbttd.getBaseSpotName());
                oci.setMeterWeight(mbttd.getMeterWeight());
                oci.setActualFirstNo(actualNo);
                doSaveHisTare(oci);
            }

            //5、保存图片
            //SaveImg(secondDb);

            //6、修改计量监控
            updateMonitor(secondDb.getBaseSpotNo());

            //当存储一次数据或者结净时，将本车辆的预处理重量改为已使用
            updatePreWeight(secondDb.getCarNo());

            updateMonitorOver(secondDb.getActualFirstNo(), secondDb.getBaseSpotNo());

            //重量类型为毛重则调用接口为
            String flag = "";
            /*
            if("0".equals(secondDb.getWeightType())){
                flag = transferParamForLims(netDb.getCarNo(),netDb.getMatterNo(),netDb.getReceivingUintName(),netDb.getForwardingUnitName(),netDb.getNetWeight().toString(),netDb.getNetTime(),"",netDb.getActualNo());
            }
            //*/

            //补充附加重量的数据
            if (StringUtils.isNotEmpty(secondDb.getAddWgtPk())) {
                insertAddWgtNo(secondDb.getActualFirstNo(), secondDb.getAddWgtPk(), secondDb.getCreateManNo(), secondDb.getCreateManName());
            }


            rm.setData(secondDb.getActualFirstNo());
            rm.setMessage(flag);
            return rm;
        }
        catch (Exception e){
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("500");
            rm.setData(null);
            rm.setMessage(e.getMessage());
            e.printStackTrace();
            return rm;
        }
    }

    @Override
    public RESTfulResult addEmergency(List<MeterWorkCarActualFirstBak> meterWorkCarActualFirstBaks) throws Exception {
        try{
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("200");
            int num = 0;
            for(MeterWorkCarActualFirstBak model: meterWorkCarActualFirstBaks){
                if(StringUtils.isNotEmpty(model.getActualFirstNo())){
                    MeterWorkCarActualFirstBak list = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(model.getActualFirstNo());
                    if(list != null){
                        continue;
                    }
                }
                SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//                String afl = formatter.format(new Date());
//                String no = meterWorkCarActualFirstMapper.getNewID(afl);
                String no = PublicMethod.IdCreate(3);
                if(StringUtils.isEmpty(model.getActualFirstNo())){
                    model.setActualFirstNo(formatter.format(new Date()) + no);
                }
                model.setValueFlag("1");
                model.setIsPreScale("0");
                model.setDataSource("3");
                model.setCreateTime(new Date());
                meterWorkCarActualFirstBakMapper.insertSelective(model);
                num++;
            }
            rm.setData(num);
            rm.setMessage("成功");
            return rm;
        }
        catch (Exception e){
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("500");
            rm.setData(null);
            rm.setMessage(e.getMessage());
            e.printStackTrace();
            return rm;
        }
    }

    @Override
    public RESTfulResult addFirstEmergency(MeterWorkCarActualFirstBak model) throws Exception {
        try{
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("200");
            int num = 0;
            if(StringUtils.isNotEmpty(model.getActualFirstNo())){
                MeterWorkCarActualFirstBak list = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(model.getActualFirstNo());
            }
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//                String afl = formatter.format(new Date());
//                String no = meterWorkCarActualFirstMapper.getNewID(afl);
            String no = PublicMethod.IdCreate(3);
            if(StringUtils.isEmpty(model.getActualFirstNo())){
                //邓义晶 一次主键多了一个时间
//                model.setActualFirstNo(formatter.format(new Date()) + no);
                model.setActualFirstNo(no);
            }
            model.setValueFlag("1");
            model.setIsPreScale("0");
            model.setDataSource("3");
            model.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insertSelective(model);
            rm.setData(num);
            rm.setMessage("成功");
            return rm;
        }
        catch (Exception e){
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("500");
            rm.setData(null);
            rm.setMessage(e.getMessage());
            e.printStackTrace();
            return rm;
        }
    }

    @Override
    public RESTfulResult doNetEmergency(MeterWorkCarActualBak model) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            Assert.isTrue(!Objects.isNull(model), "结净数据不能为空");
            Assert.isTrue(!(model.getActualFirst1No() == null || model.getActualFirst2No()==null
                    || StringUtils.isEmpty(model.getActualFirst1No()) || StringUtils.isEmpty(model.getActualFirst2No())), "一次数据编号不能为空");
            MeterWorkCarActualFirstBak firstTemp = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(model.getActualFirst1No());
            MeterWorkCarActualFirstBak secondTemp = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(model.getActualFirst2No());
            Assert.isTrue(!(Objects.isNull(firstTemp) || Objects.isNull(secondTemp)), "一次数据未查到有效的数据");

            MeterWorkCarActualBak actualModel = new MeterWorkCarActualBak();
            BigDecimal meterWeight;
            if(firstTemp.getMeterWeight().compareTo(secondTemp.getMeterWeight()) > 0) {
                MeterWorkCarActualBak mwca = meterWorkCarActualBakMapper.manualAdd(firstTemp.getActualFirstNo(), secondTemp.getActualFirstNo());
                BeanUtils.copyProperties(mwca, actualModel);
                firstTemp.setWeightType("0");
                secondTemp.setWeightType("1");
                meterWeight = firstTemp.getMeterWeight().subtract(secondTemp.getMeterWeight());
            }else {
                MeterWorkCarActualBak mwca = meterWorkCarActualBakMapper.manualAdd(secondTemp.getActualFirstNo(), firstTemp.getActualFirstNo());
                BeanUtils.copyProperties(mwca, actualModel);
                firstTemp.setWeightType("1");
                secondTemp.setWeightType("0");
                meterWeight = secondTemp.getMeterWeight().subtract(firstTemp.getMeterWeight());
            }
            firstTemp.setValueFlag(DbConstants.NET);
            firstTemp.setUpdateManNo("system");
            firstTemp.setUpdateManName("紧急结净更新");
            firstTemp.setUpdateTime(new Date());
            firstTemp.setIsPreScale("0");
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(firstTemp);
            //更新第一次皮重数据库
            secondTemp.setValueFlag(DbConstants.NET);
            secondTemp.setUpdateManNo("system");
            secondTemp.setUpdateManName("紧急结净更新");
            secondTemp.setUpdateTime(new Date());
            secondTemp.setIsPreScale("0");
            meterWorkCarActualFirstBakMapper.updateByPrimaryKey(secondTemp);

            String no = PublicMethod.IdCreate(3);
            actualModel.setActualNo(no);
            actualModel.setNetTime(new Date());
            actualModel.setNetMode("1");
            actualModel.setNote("紧急结净保存");
            actualModel.setUploadFlag("1");
            actualModel.setIsPreScale("0");
            actualModel.setValueFlag("1");
            actualModel.setNetWeight(meterWeight);
            actualModel.setCreateTime(new Date());
            if(actualModel.getGrossTime().compareTo(actualModel.getTareTime()) >= 0) {
                actualModel.setNetSpot3No(actualModel.getBaseSpot1No());
                actualModel.setNetSpot3Name(actualModel.getBaseSpot1Name());
                actualModel.setNetManNo(actualModel.getGrossManNo());
                actualModel.setNetManName(actualModel.getGrossManName());
                actualModel.setNetTime(actualModel.getGrossTime());
                actualModel.setCreateTime(actualModel.getGrossTime());
                actualModel.setCreateManName(actualModel.getGrossManName());
            }else {
                actualModel.setNetSpot3No(actualModel.getBaseSpot2No());
                actualModel.setNetSpot3Name(actualModel.getBaseSpot2Name());
                actualModel.setNetManNo(actualModel.getTareManNo());
                actualModel.setNetManName(actualModel.getTareManName());
                actualModel.setNetTime(actualModel.getTareTime());
                actualModel.setCreateTime(actualModel.getTareTime());
                actualModel.setCreateManName(actualModel.getTareManName());
            }
            meterWorkCarActualBakMapper.insertSelective(actualModel);

            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功");
            rm.setData(actualModel.getActualNo());
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常：>>>" + e.getMessage());
        }
        return rm;
    }

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

    @Override
    public PageList<MeterWorkCarActualFirstBak> ApplikeByDesc(Map<String, Object> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkCarActualFirstBak> rows = meterWorkCarActualFirstBakMapper.likeByDesc(params);
        //移除所有期限皮数据
        if(rows.size() >= 1) {
            for(int i = 0; i < rows.size(); i++){
                if(rows.get(0).getWeightType() != null) {
                    if(rows.get(0).getWeightType().equals("2")) {
                        rows.remove(i);
                    }
                }
            }
            //添加一条期限皮信息
            params.put("weightType", "2");
            List<MeterWorkCarActualFirstBak> firstList = meterWorkCarActualFirstBakMapper.likeByDesc(params);
            if(firstList.size() >= 1) {
                rows.add(firstList.get(0));
            }

        /*Collections.sort(rows, new Comparator<MeterWorkCarActualFirst>() {
            @Override
            public int compare(MeterWorkCarActualFirst o1, MeterWorkCarActualFirst o2) {
                //这里使用时间字段排序 如果要用其他字段直接从bean实体中取
                return o2.getCreateTime().compareTo(o1.getCreateTime());
                //上面使用的是降序排序，如果正序 直接使用o1-o2即可
            }
        });*/
        }
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public List<MeterWorkCarActualFirstBak> queryForTimeInterval(Map<String, Object> params) {
        List<MeterWorkCarActualFirstBak> rows = meterWorkCarActualFirstBakMapper.queryForTimeInterval(params);
        return rows;
    }

    @Override
    @Transactional(value = "tm", rollbackFor = Exception.class)
    public RESTfulResult doNetAddMuch(MeterWorkCarActualFirstBak actualFirst) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setCode("200");

        try {
            HashMap map = new HashMap();
            map.put("valueFlag","1");
            map.put("carNo",actualFirst.getCarNo());
            List<MeterWorkCarActualFirstBak> list = meterWorkCarActualFirstBakMapper.selectByParameters(map);
            int num = 0;
            for (MeterWorkCarActualFirstBak model:list){
                //存储当前皮重的数据
                SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
                String afl = formatter.format(new Date());
//                String no = meterWorkCarActualFirstMapper.getNewID(afl);
//                actualFirst.setActualFirstNo(formatter.format(new Date()) + no);
                String no = PublicMethod.IdCreate(3);
                actualFirst.setActualFirstNo(no);

                actualFirst.setValueFlag("2");//未结净
                actualFirst.setCreateTime(new Date());
                int i = meterWorkCarActualFirstBakMapper.insertSelective(actualFirst);

                //将毛重委托匹配到皮重中
                MeterWorkCarActualFirstBak meterWorkCarActualFirstBak = meterWorkCarActualFirstBakMapper.selectAndMatch(actualFirst.getActualFirstNo(), model.getPredictionNo());
                meterWorkCarActualFirstBak.setIsPreScale("1");
                meterWorkCarActualFirstBak.setWeightType("1");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(meterWorkCarActualFirstBak);


                //修改毛重数据状态为结净
                model.setValueFlag("2");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model);

                MeterWorkCarActualBak meterWorkCarActual = new MeterWorkCarActualBak();

                MeterWorkCarActualBak mwca1 = meterWorkCarActualBakMapper.manualAddPre(model.getActualFirstNo(), actualFirst.getActualFirstNo(), model.getPredictionNo());
                BeanUtils.copyProperties(mwca1, meterWorkCarActual);

//                String noActual = meterWorkCarActualFirstMapper.getNewID(afl);
//                meterWorkCarActual.setActualNo(formatter.format(new Date()) + noActual);
                String noActual = PublicMethod.IdCreate(3);
                meterWorkCarActual.setActualNo(noActual);
                meterWorkCarActual.setNetTime(new Date());
                meterWorkCarActual.setNetMode(DbConstants.TYPE);
                meterWorkCarActual.setNote("计量数据结净");
                meterWorkCarActual.setUploadFlag("1");
                meterWorkCarActual.setIsPreScale("1");
                meterWorkCarActual.setValueFlag("1");
                meterWorkCarActual.setNetWeight(model.getMeterWeight().subtract(actualFirst.getMeterWeight()));
                meterWorkCarActual.setCreateTime(new Date());
                if(meterWorkCarActual.getGrossTime().compareTo(meterWorkCarActual.getTareTime()) >= 0) {
                    meterWorkCarActual.setNetSpot3No(meterWorkCarActual.getBaseSpot1No());
                    meterWorkCarActual.setNetSpot3Name(meterWorkCarActual.getBaseSpot1Name());
                    meterWorkCarActual.setNetManNo(meterWorkCarActual.getGrossManNo());
                    meterWorkCarActual.setNetManName(meterWorkCarActual.getGrossManName());
                    meterWorkCarActual.setNetTime(meterWorkCarActual.getGrossTime());
                    meterWorkCarActual.setCreateTime(meterWorkCarActual.getGrossTime());
                    meterWorkCarActual.setCreateManName(meterWorkCarActual.getGrossManName());
                }else {
                    meterWorkCarActual.setNetSpot3No(meterWorkCarActual.getBaseSpot2No());
                    meterWorkCarActual.setNetSpot3Name(meterWorkCarActual.getBaseSpot2Name());
                    meterWorkCarActual.setNetManNo(meterWorkCarActual.getTareManNo());
                    meterWorkCarActual.setNetManName(meterWorkCarActual.getTareManName());
                    meterWorkCarActual.setNetTime(meterWorkCarActual.getTareTime());
                    meterWorkCarActual.setCreateTime(meterWorkCarActual.getTareTime());
                    meterWorkCarActual.setCreateManName(meterWorkCarActual.getTareManName());
                }
                meterWorkCarActualBakMapper.insertSelective(meterWorkCarActual);

                PreTrackScale preTrackScale = new PreTrackScale();
                preTrackScale.setPredictionNo(meterWorkCarActual.getPredictionNo());
                preTrackScale.setValueFlag("2");
                preTrackScale.setUploadFlag("1");
                preTrackScale.setMeasureStation(meterWorkCarActual.getNetSpot3No());
                preTrackScale.setMeasureStationName(meterWorkCarActual.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(preTrackScale);
                num++;
            }
            //当存储一次数据或者结净时，将本车辆的预处理重量改为已使用
            updatePreWeight(actualFirst.getCarNo());

            rm.setData(num);
            rm.setMessage("成功");
            return rm;
        }
        catch (Exception e){
            rm.setCode("500");
            rm.setData(null);
            rm.setMessage(e.getMessage());
            e.printStackTrace();
            return rm;
        }
    }


    /**
     * 内倒车辆结净
     *
     * @param secondDb 二次计量数据
     * @param netDb    净重数据
     * @param saveTare 0 不保存历史皮，1保存历史皮
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(value = "tm", rollbackFor = Exception.class)
    public RESTfulResult doAddNetInnerWfError(MeterWorkCarActualFirstBak secondDb, MeterWorkCarActualBak netDb, String saveTare, MeterMonitorNote note) throws Exception {
        try{

            RESTfulResult rm = new RESTfulResult();
            rm.setCode("200");

            //1、存储当前计量的数据
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String afl = formatter.format(new Date());
//            String no = meterWorkCarActualFirstMapper.getNewID(afl);
//            secondDb.setActualFirstNo(formatter.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            secondDb.setActualFirstNo(no);
            secondDb.setValueFlag("2");//结净
            secondDb.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insertSelective(secondDb);

            //修改一次计量数据为结净
            MeterWorkCarActualFirstBak firstUpdate = new MeterWorkCarActualFirstBak();
            //2、存储结净数据
            if (StringUtils.isNotEmpty(netDb.getActualFirst1No())) {
                //二次重量如果跟皮重一样
                netDb.setActualFirst2No(secondDb.getActualFirstNo());
                firstUpdate.setActualFirstNo(netDb.getActualFirst1No());
            } else {
                netDb.setActualFirst1No(secondDb.getActualFirstNo());
                firstUpdate.setActualFirstNo(netDb.getActualFirst2No());
            }

            //3、修改第一次存储的计量数据状态为结净
            if (firstUpdate.getActualFirstNo() != null) {
                firstUpdate.setValueFlag("2");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(firstUpdate);
            }

            //判断结净数据是否匹配委托
            if (StringUtils.isNotEmpty(netDb.getPredictionNo())) {
                MeterWorkCarActualFirstBak meca1 = meterWorkCarActualFirstBakMapper.selectAndMatch(firstUpdate.getActualFirstNo(), netDb.getPredictionNo());
                meca1.setIsPreScale("1");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(meca1);
                PreTrackScale preTrackScale = new PreTrackScale();
                preTrackScale.setPredictionNo(netDb.getPredictionNo());
                preTrackScale.setValueFlag("2");
                preTrackScale.setUploadFlag("1");
                preTrackScale.setMeasureStation(firstUpdate.getBaseSpotNo());
                preTrackScale.setMeasureStationName(firstUpdate.getBaseSpotName());
                preTrackScaleMapper.updateByPrimaryKeySelective(preTrackScale);
            }

//            String no1 = meterWorkCarActualBakMapper.getNewID(afl);
//            netDb.setActualNo(afl + no1);
            String no1 = PublicMethod.IdCreate(3);
            netDb.setActualNo(no1);
            if (netDb != null && netDb.getNetWeight() != null) {
                //2021年3月7日 净重保留两位小数；
                BigDecimal net;
                net = new BigDecimal(String.valueOf(netDb.getNetWeight())).setScale(2, BigDecimal.ROUND_HALF_UP);
                netDb.setNetWeight(net);
            }
            netDb.setUploadFlag("1");
            netDb.setCreateTime(new Date());
            if(netDb.getGrossTime().compareTo(netDb.getTareTime()) >= 0) {
                netDb.setNetSpot3No(netDb.getBaseSpot1No());
                netDb.setNetSpot3Name(netDb.getBaseSpot1Name());
                netDb.setNetManNo(netDb.getGrossManNo());
                netDb.setNetManName(netDb.getGrossManName());
                netDb.setNetTime(netDb.getGrossTime());
                netDb.setCreateTime(netDb.getGrossTime());
                netDb.setCreateManName(netDb.getGrossManName());
            }else {
                netDb.setNetSpot3No(netDb.getBaseSpot2No());
                netDb.setNetSpot3Name(netDb.getBaseSpot2Name());
                netDb.setNetManNo(netDb.getTareManNo());
                netDb.setNetManName(netDb.getTareManName());
                netDb.setNetTime(netDb.getTareTime());
                netDb.setCreateTime(netDb.getTareTime());
                netDb.setCreateManName(netDb.getTareManName());
            }
            meterWorkCarActualBakMapper.insert(netDb);

            //4、如果为期限皮重，则将期限皮重转化为一条皮重计量数据保存
            MeterWorkCarActualFirstBak mwraf = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(netDb.getActualFirst2No());
            if (mwraf == null) {
                MeterBaseTermTareData mbttd = new MeterBaseTermTareData();
                HashMap mbMap = new HashMap();
                SimpleDateFormat mbsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                mbMap.put("endTime",mbsdf.format(new Date()));
                if(StringUtils.isNotEmpty(netDb.getActualFirst2No())){
                    mbMap.put("dataNo",netDb.getActualFirst2No());
                    mbttd = meterBaseTermTareDataMapper.selectByDataNo(mbMap);
                }
                else{
                    mbMap.put("carNo",netDb.getCarNo());
                    mbttd = meterBaseTermTareDataMapper.selectByCarNo(mbMap);
                }
                MeterWorkCarActualFirstBak imTare = new MeterWorkCarActualFirstBak();
                //补充数据
                String afl1 = formatter.format(new Date());
//                String imTareNO = meterWorkCarActualFirstMapper.getNewID(afl1);
//                imTare.setActualFirstNo(afl1 + imTareNO);
                String imTareNO = PublicMethod.IdCreate(3);
                imTare.setActualFirstNo(no);
                imTare.setMeterTypeNo(secondDb.getMeterTypeNo());
                imTare.setMeterTypeName(secondDb.getMeterTypeName());
                imTare.setBaseSpotNo(mbttd.getBaseSpotNo());
                imTare.setBaseSpotName(mbttd.getBaseSpotName());
                imTare.setCarNo(mbttd.getCarNo());
                imTare.setCreateTime(mbttd.getCreateTime());
                imTare.setCreateManNo(mbttd.getCreateManNo());
                imTare.setCreateManName(mbttd.getCreateManName());
                imTare.setMeterWeight(netDb.getTareWeight());
                imTare.setWeightType("2");//期限皮重
                imTare.setValueFlag("2");//结净
                meterWorkCarActualFirstBakMapper.insertSelective(imTare);
                //将结净数据中的皮重编号修改
                MeterWorkCarActualBak mwca = new MeterWorkCarActualBak();
                mwca.setActualNo(netDb.getActualNo());
                mwca.setActualFirst2No(imTare.getActualFirstNo());
                meterWorkCarActualBakMapper.updateByPrimaryKeySelective(mwca);
            }

            //4、存储历史皮重及历史皮重明细信息
            if ("1".equals(saveTare)) {
                MeterWorkCarActualFirstBak oci = new MeterWorkCarActualFirstBak();
                oci.setCarNo(netDb.getCarNo());
                oci.setCreateTime(netDb.getTareTime());
                oci.setBaseSpotNo(netDb.getBaseSpot2No());
                oci.setBaseSpotName(netDb.getBaseSpot2Name());
                oci.setMeterWeight(netDb.getTareWeight());
                oci.setActualFirstNo(netDb.getActualFirst2No());
                doSaveHisTare(oci);
            }

            //5、保存图片
            //SaveImg(secondDb);

            //6、修改计量监控
            updateMonitor(secondDb.getBaseSpotNo());

            //保存日志
            saveNote(note);

            //当存储一次数据或者结净时，将本车辆的预处理重量改为已使用
            updatePreWeight(secondDb.getCarNo());

            updateMonitorOver(secondDb.getActualFirstNo(), secondDb.getBaseSpotNo());

            //重量类型为毛重则调用接口为
            String flag = "";
            /*
            if("0".equals(secondDb.getWeightType())){
                flag = transferParamForLims(netDb.getCarNo(),netDb.getMatterNo(),netDb.getReceivingUintName(),netDb.getForwardingUnitName(),netDb.getNetWeight().toString(),netDb.getNetTime(),"",netDb.getActualNo());
            }
            //*/

            //补充附加重量的数据
            if (StringUtils.isNotEmpty(secondDb.getAddWgtPk())) {
                insertAddWgtNo(secondDb.getActualFirstNo(), secondDb.getAddWgtPk(), secondDb.getCreateManNo(), secondDb.getCreateManName());
            }


            rm.setData(afl + no1);
            rm.setMessage(flag);
            return rm;
        }
        catch (Exception e){
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("500");
            rm.setData(null);
            rm.setMessage(e.getMessage());
            e.printStackTrace();
            return rm;
        }
    }

    @Override
    public RESTfulResult doNetByFirst(MeterWorkCarActualFirstBak grossModel, MeterWorkCarActualFirstBak tareModel, PreTrackScale scale) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        try {
            String returnNo = "";
            MeterWorkCarActualBak model = new MeterWorkCarActualBak();
            if (StringUtils.isNotEmpty(scale.getPredictionNo())) {
                MeterWorkCarActualFirstBak mg = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(grossModel.getActualFirstNo());
                if(mg == null){
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
                    String afl1 = formatter.format(new Date());
//                    String no1 = meterWorkCarActualFirstMapper.getNewID(afl1);
//                    grossModel.setActualFirstNo(formatter.format(new Date()) + no1);
                    String no1 = PublicMethod.IdCreate(3);
                    grossModel.setActualFirstNo(no1);
                    grossModel.setValueFlag("2");//结净
                    grossModel.setCreateTime(new Date());
                    meterWorkCarActualFirstBakMapper.insertSelective(grossModel);
                    returnNo = grossModel.getActualFirstNo();
                }
                else {
                    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
                    String afl1 = formatter.format(new Date());
//                    String no1 = meterWorkCarActualFirstMapper.getNewID(afl1);
//                    tareModel.setActualFirstNo(formatter.format(new Date()) + no1);
                    String no1 = PublicMethod.IdCreate(3);
                    tareModel.setActualFirstNo(no1);
                    tareModel.setValueFlag("2");//结净
                    tareModel.setCreateTime(new Date());
                    meterWorkCarActualFirstBakMapper.insertSelective(tareModel);
                    returnNo = tareModel.getActualFirstNo();
                }


                //将委托信息插入一次数据
                MeterWorkCarActualFirstBak first1 = meterWorkCarActualFirstBakMapper.selectAndMatch(grossModel.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first1, grossModel);
                MeterWorkCarActualFirstBak first2 = meterWorkCarActualFirstBakMapper.selectAndMatch(tareModel.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first2, tareModel);

                //更新第一次毛重数据库
                grossModel.setValueFlag(DbConstants.NET);
                grossModel.setUpdateManNo("system");
                grossModel.setUpdateManName("期限皮结净更新");
                grossModel.setUpdateTime(new Date());
                grossModel.setIsPreScale("1");
                grossModel.setPredictionType("1");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKey(grossModel);
                //更新第一次皮重数据库
                tareModel.setValueFlag(DbConstants.NET);
                tareModel.setUpdateManNo("system");
                tareModel.setUpdateManName("期限皮结净更新");
                tareModel.setUpdateTime(new Date());
                tareModel.setIsPreScale("1");
                tareModel.setWeightType("1");
                tareModel.setPredictionType("1");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKey(tareModel);
                //将委托信息插入结净数据
                MeterWorkCarActualBak mwca1 = meterWorkCarActualBakMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(mwca1, model);
                //更新结净数据状态
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//                String afl = sdf.format(new Date());
//                String no = meterWorkCarActualBakMapper.getNewID(sdf.format(new Date()));
//                model.setActualNo(sdf.format(new Date()) + no);

                String no = PublicMethod.IdCreate(3);
                model.setActualNo(no);
                model.setNetTime(new Date());
                model.setNetMode(DbConstants.TYPE);
                model.setNote("期限皮匹配结净");
                model.setUploadFlag("1");
                model.setIsPreScale("1");
                model.setValueFlag("1");
                model.setNetManNo("admin");
                model.setNetManName("admin");
                model.setNetSpot3No(tareModel.getBaseSpotNo());
                model.setNetSpot3Name(tareModel.getBaseSpotName());
                model.setPredictionNo(scale.getPredictionNo());
                model.setPredictionType("1");
                model.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                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);

                //更新委托信息状态
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("期限皮结净更新");
                scale.setUploadFlag("1");
                scale.setPredictionType("1");
                scale.setMeasureStation(model.getNetSpot3No());
                scale.setMeasureStationName(model.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(scale);
            } else {
                rm.setMessage("委托信息编号不能为空，请确认！！");
                return rm;
            }

            //新增期限皮重信息
            HashMap<String, Object> map = new HashMap<>();
            map.put("carNo", tareModel.getCarNo());
            List<MeterBaseTermTareData> upModels = meterBaseTermTareDataMapper.selectByParameters(map);
            MeterBaseTermTareData termData = new MeterBaseTermTareData();
            if (upModels.size() >= 1) {
                termData = upModels.get(0);
            }
            if (termData.getDataNo() != null) {
                termData.setBaseSpotNo(tareModel.getBaseSpotNo());
                termData.setBaseSpotName(tareModel.getBaseSpotName());
                termData.setCarNo(tareModel.getCarNo());
                termData.setMeterName(tareModel.getCreateManName());
                termData.setMeterTime(tareModel.getCreateTime());
                termData.setStartTime(tareModel.getCreateTime());
                termData.setEndTime(new Date(tareModel.getCreateTime().getTime() + 24 * 60 * 60 * 1000));
                termData.setWarnTime(new Date(tareModel.getCreateTime().getTime() + 12 * 60 * 60 * 1000));
                termData.setValueFlag("0");
                termData.setCreateManNo(tareModel.getCreateManNo());
                termData.setCreateManName(tareModel.getCreateManName());
                termData.setCreateTime(new Date());
                termData.setMeterWeight(tareModel.getMeterWeight());
                meterBaseTermTareDataMapper.updateByPrimaryKeySelective(termData);
                /*
                termData.setUpWeight(termData.getMeterWeight().longValue());
                double db = (termData.getAvgWeight() * termData.getMeterNum() + termData.getMeterWeight().longValue()) / (termData.getMeterNum() + 1);
                termData.setAvgWeight(Math.round(db));
                termData.setMeterNum(termData.getMeterNum()+1);
                */
            } else {
                //新增操作
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String no = meterBaseTermTareDataMapper.getNewID(sdf.format(new Date()));
                termData.setDataNo(sdf.format(new Date()) + no);
                termData.setBaseSpotNo(tareModel.getBaseSpotNo());
                termData.setBaseSpotName(tareModel.getBaseSpotName());
                termData.setCarNo(tareModel.getCarNo());
                termData.setMeterName(tareModel.getCreateManName());
                termData.setMeterTime(tareModel.getCreateTime());
                termData.setStartTime(tareModel.getCreateTime());
                termData.setEndTime(new Date(tareModel.getCreateTime().getTime() + 24 * 60 * 60 * 1000));
                termData.setWarnTime(new Date(tareModel.getCreateTime().getTime() + 12 * 60 * 60 * 1000));
                termData.setValueFlag("0");
                termData.setCreateManNo(tareModel.getCreateManNo());
                termData.setCreateManName(tareModel.getCreateManName());
                termData.setCreateTime(new Date());
                termData.setMeterWeight(tareModel.getMeterWeight());
                meterBaseTermTareDataMapper.insertSelective(termData);
            }
            //当存储一次数据或者结净时，将本车辆的预处理重量改为已使用
            updatePreWeight(grossModel.getCarNo());

            MeterWorkCarActualFirstBak imTare = new MeterWorkCarActualFirstBak();
            imTare = meterWorkCarActualFirstBakMapper.selectAndMatch(tareModel.getActualFirstNo(), scale.getPredictionNo());
            SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd");
            //补充数据
            String afl1 = formatter1.format(new Date());
//            String imTareNO = meterWorkCarActualFirstMapper.getNewID(afl1);
//            String actualNo = afl1 + imTareNO;
            String actualNo = PublicMethod.IdCreate(3);
            imTare.setActualFirstNo(actualNo);
            imTare.setMeterTypeNo(tareModel.getMeterTypeNo());
            imTare.setMeterTypeName(tareModel.getMeterTypeName());
            imTare.setBaseSpotNo(termData.getBaseSpotNo());
            imTare.setBaseSpotName(termData.getBaseSpotName());
            imTare.setCarNo(termData.getCarNo());
            imTare.setCreateTime(termData.getMeterTime());
            imTare.setCreateManNo(termData.getCreateManNo());
            imTare.setCreateManName(termData.getCreateManName());
            imTare.setMeterWeight(termData.getMeterWeight());
            imTare.setWeightType("2");//期限皮重
            imTare.setValueFlag("2");//结净
            imTare.setDataSource("1");
            //期限皮重转为一次数据的时，创建时间应为期限皮开始时间
            //imTare.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insertSelective(imTare);

            //修改结净皮重编号
            MeterWorkCarActualBak mca = new MeterWorkCarActualBak();
            mca.setActualNo(model.getActualNo());
            mca.setActualFirst2No(actualNo);
            mca.setTareTime(termData.getMeterTime());
            mca.setBaseSpot2No(termData.getBaseSpotNo());
            mca.setBaseSpot2Name(termData.getBaseSpotName());
            meterWorkCarActualBakMapper.updateByPrimaryKeySelective(mca);

            //作废一次计量皮重信息
            tareModel.setValueFlag("0");
            meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(tareModel);
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(returnNo);
            return rm;
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！" + e.getMessage());
            return rm;
        }
    }

    @Override
    public RESTfulResult furConversion(HashMap<String, Object> params) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        try{
            if(StringUtils.isNotEmpty(params.get("actualFirstNo").toString())){
                MeterWorkCarActualFirstBak model = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(params.get("actualFirstNo").toString());
                if(model != null) {
                    if(model.getValueFlag().equals("2")){
                        rm.setMessage("毛皮转换失败：数据已结净，请确认！！");
                        return rm;
                    }
                    if(model.getWeightType() == null || StringUtils.isEmpty(model.getWeightType())) {
//                            rm.setMessage("毛皮转换失败：重量类型为空，请确认！！");
//                            return rm;
                        model.setWeightType("1");
                        meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model);
                        rm.setSucceed();
                        rm.setMessage("重量类型为空，毛皮转换成皮重!!");
                        rm.setData(model);
                        rm.setCode("200");
                        return rm;
                    } else {
                        if(model.getValueFlag().equals("1")) {
                            if(model.getWeightType().equals("0")){
                                model.setWeightType("1");
                            }else if (model.getWeightType().equals("1")){
                                model.setWeightType("0");
                            }else{
                                rm.setMessage("毛皮转换失败：毛皮转换只针对毛重和常规皮重，请确认！！");
                                return rm;
                            }
                            meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model);
                            rm.setSucceed();
                            rm.setMessage("毛皮转换成功!!");
                            rm.setData(model);
                            rm.setCode("200");
                            return rm;
                        }else{
                            rm.setMessage("毛皮转换失败：毛皮转换只针对有效的毛重和常规皮重，请确认！！");
                        }
                    }
                }else {
                    rm.setMessage("未找到相应的一次计量数据，不能毛皮转换！！");
                }
            }else {
                rm.setMessage("实绩编号不能为空！！");
            }
            return rm;
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！>>>" + e.getMessage());
            return rm;
        }
    }

    @Override
    public RESTfulResult doNetByTermData(HashMap<String, Object> params) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            UserPayload userPayload = UserPayload.getCurrUser();
            String userId;
            String userName;
            try{
                userId  = userPayload.getUserCode();
                userName  = userPayload.getUserName();
            }catch (Exception e) {
                userId = "admin";
                userName = "admins";
            }

            String actualFirstNo = params.get("actualFirstNo") == null ? "": params.get("actualFirstNo").toString();
            Assert.isTrue(StringUtils.isNotEmpty(actualFirstNo), "未选择一次数据");
            MeterWorkCarActualFirstBak grossModel = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(actualFirstNo);
            Assert.isTrue(!grossModel.getValueFlag().equals("2"), "一次数据已结净，不允许期限皮结净");
            Assert.isTrue(!grossModel.getValueFlag().equals("0"), "一次数据已作废，不允许期限皮结净");
            String predictionNo = params.get("predictionNo") == null ? "": params.get("predictionNo").toString();
            if(StringUtils.isEmpty(predictionNo)) {
                Assert.isTrue(StringUtils.isNotEmpty(grossModel.getPredictionNo()), "毛重未匹配委托");
                predictionNo = grossModel.getPredictionNo();
            }
            if(grossModel != null) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("carNo", grossModel.getCarNo());
                MeterBaseTermTareData termTareModel = meterBaseTermTareDataMapper.getTermTareData(map);
                Assert.isTrue(termTareModel != null, "此车号未查询到有效的期限皮重，不允许期限皮结净");
                if(StringUtils.isNotEmpty(predictionNo)) {
                    PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(predictionNo);
                    Assert.isTrue(scale != null, "根据委托编号：" + predictionNo + "为查询到有效的委托信息");

                    MeterWorkCarActualFirstBak tareModel = new MeterWorkCarActualFirstBak();
                    BeanUtils.copyProperties(grossModel, tareModel);
                    String no = PublicMethod.IdCreate(3);
                    tareModel.setActualFirstNo(no);
                    tareModel.setCreateTime(termTareModel.getCreateTime());
                    tareModel.setMeterWeight(termTareModel.getMeterWeight());
                    tareModel.setBaseSpotNo(termTareModel.getBaseSpotNo());
                    tareModel.setBaseSpotName(termTareModel.getBaseSpotName());
                    tareModel.setCreateManNo(userId);
                    tareModel.setCreateManName(userName);
                    meterWorkCarActualFirstBakMapper.insertSelective(tareModel);

                    MeterWorkCarActualFirstBak grossFirst = meterWorkCarActualFirstBakMapper.selectAndMatch(grossModel.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(grossFirst, grossModel);
                    MeterWorkCarActualFirstBak tareFirst = meterWorkCarActualFirstBakMapper.selectAndMatch(tareModel.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(tareFirst, tareModel);

                    grossModel.setUpdateManNo(userId);
                    grossModel.setUpdateManName(userName);
                    grossModel.setUpdateTime(new Date());
                    grossModel.setWeightType("0");
                    grossModel.setValueFlag("2");
                    grossModel.setIsPreScale("1");
                    grossModel.setPredictionType("1");
                    grossModel.setPredictionNo(scale.getPredictionNo());
                    grossModel.setNote("期限皮结净");
                    meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(grossModel);
                    tareModel.setUpdateManNo(userId);
                    tareModel.setUpdateManName(userName);
                    tareModel.setUpdateTime(new Date());
                    tareModel.setValueFlag("2");
                    tareModel.setWeightType("2");
                    tareModel.setIsPreScale("1");
                    tareModel.setPredictionType("1");
                    tareModel.setPredictionNo(scale.getPredictionNo());
                    tareModel.setNote("期限皮结净");
                    meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(tareModel);

                    MeterWorkCarActualBak actualRow = meterWorkCarActualBakMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), scale.getPredictionNo());
                    String noActual = PublicMethod.IdCreate(3);
                    actualRow.setActualNo(noActual);
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setUploadFlag("1");
                    actualRow.setIsPreScale("1");
                    actualRow.setValueFlag("1");
                    actualRow.setPredictionNo(scale.getPredictionNo());
                    actualRow.setPredictionType("1");
                    actualRow.setNote("期限皮匹配结净");
                    actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.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);

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

                    rm.setSucceed();
                    rm.setData(actualRow);
                    rm.setMessage("有委托期限皮结净成功");
                    rm.setCode("200");
                }else {
                    MeterWorkCarActualFirstBak tareModel = new MeterWorkCarActualFirstBak();
                    BeanUtils.copyProperties(grossModel, tareModel);
                    String no = PublicMethod.IdCreate(3);
                    tareModel.setActualFirstNo(no);
                    tareModel.setCreateTime(termTareModel.getCreateTime());
                    tareModel.setMeterWeight(termTareModel.getMeterWeight());
                    tareModel.setBaseSpotNo(termTareModel.getBaseSpotNo());
                    tareModel.setBaseSpotName(termTareModel.getBaseSpotName());
                    tareModel.setCreateManNo(userId);
                    tareModel.setCreateManName(userName);
                    meterWorkCarActualFirstBakMapper.insertSelective(tareModel);

                    grossModel.setUpdateManNo(userId);
                    grossModel.setUpdateManName(userName);
                    grossModel.setUpdateTime(new Date());
                    grossModel.setWeightType("0");
                    grossModel.setValueFlag("2");
                    grossModel.setIsPreScale("0");
                    grossModel.setNote("期限皮结净");
                    meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(grossModel);

                    tareModel.setUpdateManNo(userId);
                    tareModel.setUpdateManName(userName);
                    tareModel.setUpdateTime(new Date());
                    tareModel.setWeightType("2");
                    tareModel.setValueFlag("2");
                    tareModel.setIsPreScale("0");
                    tareModel.setNote("期限皮结净");
                    meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(tareModel);

                    MeterWorkCarActualBak actualRow = meterWorkCarActualBakMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                    String noActual = PublicMethod.IdCreate(3);
                    actualRow.setActualNo(noActual);
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setUploadFlag("1");
                    actualRow.setIsPreScale("0");
                    actualRow.setValueFlag("1");
                    actualRow.setNote("期限皮匹配结净");
                    actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.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.setData(actualRow);
                    rm.setMessage("无委托期限皮结净成功");
                    rm.setCode("200");
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult doNetAddMuchGross(List<MeterWorkCarActualFirstBak> gross, MeterWorkCarActualFirstBak tare, MeterMonitorNote note) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            //将皮重数据新增
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String afl = formatter.format(new Date());
//            String no = meterWorkCarActualFirstMapper.getNewID(afl);
//            tare.setActualFirstNo(formatter.format(new Date()) + no);

            String no = PublicMethod.IdCreate(3);
            tare.setActualFirstNo(no);

            tare.setValueFlag("0");//结净
            tare.setWeightType("1");
            tare.setCreateTime(new Date());
            meterWorkCarActualFirstBakMapper.insertSelective(tare);

            //将皮重数据保存或更新到期限皮中
            HashMap<String, Object> map = new HashMap<>();
            map.put("carNo", tare.getCarNo());
            List<MeterBaseTermTareData> upModels = meterBaseTermTareDataMapper.selectByParameters(map);
            MeterBaseTermTareData termData = new MeterBaseTermTareData();
            if (upModels.size() >= 1) {
                termData = upModels.get(0);
            }
            if (termData.getDataNo() != null) {
                termData.setBaseSpotNo(tare.getBaseSpotNo());
                termData.setBaseSpotName(tare.getBaseSpotName());
                termData.setCarNo(tare.getCarNo());
                termData.setMeterName(tare.getCreateManName());
                termData.setMeterTime(tare.getCreateTime());
                termData.setStartTime(tare.getCreateTime());
                termData.setEndTime(new Date(tare.getCreateTime().getTime() + 24 * 60 * 60 * 1000));
                termData.setWarnTime(new Date(tare.getCreateTime().getTime() + 12 * 60 * 60 * 1000));
                termData.setValueFlag("0");
                termData.setCreateManNo(tare.getCreateManNo());
                termData.setCreateManName(tare.getCreateManName());
                termData.setCreateTime(new Date());
                termData.setMeterWeight(tare.getMeterWeight());
                meterBaseTermTareDataMapper.updateByPrimaryKeySelective(termData);
            } else {
                //新增操作
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String no1 = meterBaseTermTareDataMapper.getNewID(sdf.format(new Date()));
                termData.setDataNo(sdf.format(new Date()) + no1);
                termData.setBaseSpotNo(tare.getBaseSpotNo());
                termData.setBaseSpotName(tare.getBaseSpotName());
                termData.setCarNo(tare.getCarNo());
                termData.setMeterName(tare.getCreateManName());
                termData.setMeterTime(tare.getCreateTime());
                termData.setStartTime(tare.getCreateTime());
                termData.setEndTime(new Date(tare.getCreateTime().getTime() + 24 * 60 * 60 * 1000));
                termData.setWarnTime(new Date(tare.getCreateTime().getTime() + 12 * 60 * 60 * 1000));
                termData.setValueFlag("0");
                termData.setCreateManNo(tare.getCreateManNo());
                termData.setCreateManName(tare.getCreateManName());
                termData.setCreateTime(new Date());
                termData.setMeterWeight(tare.getMeterWeight());
                meterBaseTermTareDataMapper.insertSelective(termData);
            }

            for (MeterWorkCarActualFirstBak model:gross){
                //将皮重数据新增
                model.setValueFlag("2");//结净
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(model);

                MeterWorkCarActualFirstBak imTare = new MeterWorkCarActualFirstBak();
                imTare = meterWorkCarActualFirstBakMapper.selectAndMatch(tare.getActualFirstNo(), model.getPredictionNo());
                SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd");
                //补充数据
                String afl1 = formatter1.format(new Date());
//                String imTareNO = meterWorkCarActualFirstMapper.getNewID(afl1);
//                String actualNo = afl1 + imTareNO;
                String actualNo = PublicMethod.IdCreate(3);

                imTare.setActualFirstNo(actualNo);
                imTare.setMeterTypeNo(tare.getMeterTypeNo());
                imTare.setMeterTypeName(tare.getMeterTypeName());
                imTare.setBaseSpotNo(termData.getBaseSpotNo());
                imTare.setBaseSpotName(termData.getBaseSpotName());
                imTare.setCarNo(termData.getCarNo());
                //杜勇 20220724修改
                imTare.setCreateTime(termData.getMeterTime());
                imTare.setCreateManNo(termData.getCreateManNo());
                imTare.setCreateManName(termData.getCreateManName());
                imTare.setMeterWeight(termData.getMeterWeight());
                imTare.setWeightType("2");//期限皮重
                imTare.setValueFlag("2");//结净
                imTare.setDataSource("1");
                imTare.setIsPreScale("1");
                imTare.setMeterTypeNo(model.getMeterTypeNo());
                imTare.setMeterTypeName(model.getMeterTypeName());
                imTare.setCreateTime(new Date());
                meterWorkCarActualFirstBakMapper.insertSelective(imTare);

                //将委托信息插入结净数据
                MeterWorkCarActualBak mwca = meterWorkCarActualBakMapper.manualAddPre(model.getActualFirstNo(), imTare.getActualFirstNo(), model.getPredictionNo());
                //更新结净数据状态
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//                String aflActual = sdf.format(new Date());
//                String noActual = meterWorkCarActualBakMapper.getNewID(sdf.format(new Date()));
//                mwca.setActualNo(aflActual + noActual);
                String noActual = PublicMethod.IdCreate(3);
                mwca.setActualNo(noActual);
                mwca.setNetTime(new Date());
                mwca.setNetMode(DbConstants.TYPE);
                mwca.setNote("期限皮匹配结净");
                mwca.setUploadFlag("1");
                mwca.setIsPreScale("1");
                mwca.setValueFlag("1");
                mwca.setNetManNo("admin");
                mwca.setNetManName("admin");
                mwca.setNetSpot3No(model.getBaseSpotNo());
                mwca.setNetSpot3Name(model.getBaseSpotName());
                mwca.setPredictionNo(model.getPredictionNo());
                mwca.setPredictionType("1");
                mwca.setNetWeight(model.getMeterWeight().subtract(imTare.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);

                //修改委托
                PreTrackScale scale = preTrackScaleMapper.selectBypredictionNo(model.getPredictionNo());
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("期限皮结净更新");
                scale.setUploadFlag("1");
                scale.setPredictionType("1");
                scale.setMeasureStation(mwca.getNetSpot3No());
                scale.setMeasureStationName(mwca.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(scale);
            }

            //当存储一次数据或者结净时，将本车辆的预处理重量改为已使用
            updatePreWeight(tare.getCarNo());

            //保存或更新历史皮重
            MeterWorkCarActualFirstBak oci = new MeterWorkCarActualFirstBak();
            oci.setCarNo(tare.getCarNo());
            oci.setCreateTime(new Date());
            oci.setBaseSpotNo(tare.getBaseSpotNo());
            oci.setBaseSpotName(tare.getBaseSpotName());
            oci.setMeterWeight(tare.getMeterWeight());
            oci.setActualFirstNo(tare.getActualFirstNo());
            doSaveHisTare(oci);

            rm.setSucceed();
            rm.setMessage("多毛结净完成!!");
            rm.setData(tare.getActualFirstNo());
            rm.setCode("200");
            return rm;
        }
        catch (Exception e){
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！>>>" + e.getMessage());
            return rm;
        }
    }

    @Override
    public RESTfulResult batchCheckAdd(List<MeterWorkCarActualFirstBak> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            cn.hutool.core.lang.Assert.isTrue(models.size() > 0, "数据为空，不能进行批量保存");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            for (MeterWorkCarActualFirstBak model: models) {
                String no = PublicMethod.IdCreate(3);
                model.setActualFirstNo(no + "bak");
                meterWorkCarActualFirstBakMapper.insertSelective(model);
            }
            rm.setSucceed();
            rm.setData(models);
            rm.setCode("200");
            rm.setMessage("操作成功");
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常>>>" + e.getMessage());
        }
        return rm;
    }

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

        for (String id : ids){
            //根据id获取对应的一次数据
            MeterWorkCarActualFirstBak meterWorkCarActualFirstBak = meterWorkCarActualFirstBakMapper.selectByPrimaryKey(id);

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

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

            //判断是否符合要求
            String note = isSuit(meterWorkCarActualFirstBak,0);
            if (note.equals("1")){
                //复制到First正式表里面

                MeterWorkCarActualFirst first = new MeterWorkCarActualFirst();
                //复制一份
                BeanUtils.copyProperties(meterWorkCarActualFirstBak,first);
                //修改主键
                first.setActualFirstNo(first.getActualFirstNo()+"bak");
                MeterWorkCarActualFirst tempFirst = meterWorkCarActualFirstMapper.selectByPrimaryKey(first.getActualFirstNo());
                if(tempFirst != null) {
                    throw new Exception("一次数据已转成正式数据，不能重复转正式数据");
                }
                meterWorkCarActualFirstMapper.insert(first);

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

                meterWorkCarActualFirstBak.setValueFlag("0");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(meterWorkCarActualFirstBak);
            }else{
                throw new Exception(note);
            }

        }


    }

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

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

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

            //判断是否符合要求
            String note = isSuit(meterWorkCarActualFirstBak,1);
            if (note.equals("1")){

                //复制到First正式表里面
                MeterWorkCarActualFirst first = new MeterWorkCarActualFirst();
                //复制一份
                BeanUtils.copyProperties(meterWorkCarActualFirstBak,first);
                //修改主键
                first.setActualFirstNo(first.getActualFirstNo()+"bak");

                MeterWorkCarActualFirst tempFirst = meterWorkCarActualFirstMapper.selectByPrimaryKey(first.getActualFirstNo());
                if(tempFirst != null) {
                    throw new Exception("一次数据已转成正式数据，不能重复转正式数据");
                }
                meterWorkCarActualFirstMapper.insert(first);


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

                meterWorkCarActualFirstBak.setValueFlag("0");
                meterWorkCarActualFirstBakMapper.updateByPrimaryKeySelective(meterWorkCarActualFirstBak);
            }else{
                throw new Exception(note);
            }

        }
    }

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

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

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

    private String isSuit(MeterWorkCarActualFirstBak model,int type){

        //不能是已结净的数据
        if ( !"1".equals(model.getValueFlag()) ){
            return "不能是结净数据";
        }

        //站点、站点名、创建人、创建人编号、创建时间、数据来源、重量类型、重量不能为空
        if (model.getBaseSpotNo() == null || StringUtils.isEmpty(model.getBaseSpotNo())) {
            return "站点编号为空";
        }
        if (model.getBaseSpotName() == null || StringUtils.isEmpty(model.getBaseSpotName())) {
            return "站点名称为空";
        }
        if (model.getCreateManNo() == null || StringUtils.isEmpty(model.getCreateManNo())) {
            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.getWeightType() == null || StringUtils.isEmpty(model.getWeightType())){
            return "计量重量类型不能为空";
        }
        if (model.getMeterWeight() == null){
            return "计量重量不能为空";
        }
        //内倒车毛重必须有委托
        //毛重 -> 0
        //type == 0 ->内倒
        if (type == 0 && "0".equals(model.getWeightType()) ){
            //委托不能为空
            if(model.getPredictionNo() == null){
                return "委托不能为空";
            }
        }
        return "1";
    }


    @Override
    public RESTfulResult addFirstEmergencyList(List<MeterWorkCarActualFirstBak> model) throws Exception {
        List<DjPbModel> lst=new ArrayList<>();
        RESTfulResult rm = new RESTfulResult();
        rm.setCode("200");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        int num = 0;

        for(MeterWorkCarActualFirstBak fbk:model){
            String str=fbk.getActualFirstNo();
            String s = time+(String.format("%03d",++num));

            DjPbModel dm=new DjPbModel();
            dm.setKey(str);
            dm.setValue(s);
            fbk.setActualFirstNo(s);

            try {
                meterWorkCarActualFirstBakMapper.insertSelective(fbk);
                lst.add(dm);
            }catch (Exception e){ }
        }

        rm.setData(lst);
        rm.setMessage("成功");
        return rm;
    }

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

