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

import com.github.pagehelper.util.StringUtil;
import com.steerinfo.baseinfo.meterbasematterinfomatch.mapper.MeterBaseMatterInfoMatchMapper;
import com.steerinfo.baseinfo.meterbasematterinfomatch.model.MeterBaseMatterInfoMatch;
import com.steerinfo.baseinfo.meterbaseoperationlog.mapper.MeterBaseOperationLogMapper;
import com.steerinfo.baseinfo.meterbaseoperationlog.model.MeterBaseOperationLog;
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.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.metermonitor.metermonitornote.mapper.MeterMonitorNoteMapper;
import com.steerinfo.metermonitor.metermonitornote.model.MeterMonitorNote;
import com.steerinfo.meterwork.constants.DbConstants;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.metertolimsold.mapper.MeterToLimsOldMapper;
import com.steerinfo.meterwork.metertolimsold.model.MeterToLimsOld;
import com.steerinfo.meterwork.meterworkcaractual.mapper.MeterWorkCarActualMapper;
import com.steerinfo.meterwork.meterworkcaractual.model.MeterWorkCarActual;
import com.steerinfo.meterwork.meterworkcaractual.model.MeterWorkCarActualTHJCompare;
import com.steerinfo.meterwork.meterworkcaractual.service.IMeterWorkCarActualService;
import com.steerinfo.meterwork.meterworkcaractualfirst.mapper.MeterWorkCarActualFirstMapper;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.meterwork.meterworkcaractualfirst.service.IMeterWorkCarActualFirstService;
import com.steerinfo.meterwork.meterworkcheckfirstactual.mapper.MeterWorkCheckFirstActualMapper;
import com.steerinfo.meterwork.meterworkcheckfirstactual.model.FirstActualImgData;
import com.steerinfo.meterwork.meterworkimage.mapper.MeterWorkImageMapper;
import com.steerinfo.meterwork.meterworkimage.model.MeterWorkImage;
import com.steerinfo.meterwork.meterworkprelink.mapper.MeterWorkPreLinkMapper;
import com.steerinfo.meterwork.meterworkprelink.service.IMeterWorkPreLinkService;
import com.steerinfo.meterwork.meterworkrailwayactual.mapper.MeterWorkRailwayActualMapper;
import com.steerinfo.meterwork.meterworkrailwayactual.model.MeterWorkRailwayActual;
import com.steerinfo.meterwork.meterworkscheduledhelp.mapper.MeterWorkScheduledHelpMapper;
import com.steerinfo.meterwork.meterworkscheduledhelp.model.MeterWorkScheduledHelp;
import com.steerinfo.pretrack.pretracknotice.mapper.PreTrackNoticeMapper;
import com.steerinfo.pretrack.pretracknotice.model.PreTrackNotice;
import com.steerinfo.pretrack.pretrackscale.mapper.PreTrackScaleMapper;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.util.CommonPage;
import com.steerinfo.util.FieldsCollector;
import com.steerinfo.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import org.apache.catalina.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

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

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

    @Resource
    private MeterWorkCarActualFirstMapper meterWorkCarActualFirstMapper;

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

    @Resource
    private MeterBaseSpotInfoMapper meterBaseSpotInfoMapper;

    @Resource
    private MeterWorkScheduledHelpMapper meterWorkScheduledHelpMapper;

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

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

    @Resource
    private MeterBaseOperationLogMapper meterBaseOperationLogMapper;

    @Resource
    private MeterMonitorNoteMapper meterMonitorNoteMapper;

    @Autowired
    IMeterWorkPreLinkService meterWorkPreLinkService;

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

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

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


    @Autowired
    IMeterWorkCarActualFirstService meterWorkCarActualFirstService;

    @Resource
    private MeterBaseTermTareDataMapper meterBaseTermTareDataMapper;

    @Resource
    private MeterWorkCheckFirstActualMapper meterWorkCheckFirstActualMapper;

    @Resource
    private MeterWorkCarActualMapper meterWorkCarActualMapper;

    @Override
    protected IBaseMapper<MeterWorkCarActual, String> getMapper() {
        return meterWorkCarActualMapper;
    }

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


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

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

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

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

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

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

            // 更新一次计量数据
            MeterWorkCarActual queryData = meterWorkCarActualMapper.selectByPrimaryKey(netData.getActualNo());
            MeterWorkCarActualFirst first1 = new MeterWorkCarActualFirst();
            MeterWorkCarActualFirst first2 = new MeterWorkCarActualFirst();
            first1.setActualFirstNo(queryData.getActualFirst1No());
            first1.setCarNo(queryData.getCarNo());
            first1.setUpdateManNo(model.getUpdateManNo());
            first1.setUpdateManName(model.getUpdateManName());
            first1.setUpdateTime(new Date());
            first1.setNote("净重数据修改车号，同步修改一次计量数据车号");
            meterWorkCarActualFirstMapper.updateByPrimaryKeySelective(first1);

            first2.setActualFirstNo(queryData.getActualFirst1No());
            first2.setCarNo(queryData.getCarNo());
            first2.setUpdateManNo(model.getUpdateManNo());
            first2.setUpdateManName(model.getUpdateManName());
            first2.setUpdateTime(new Date());
            first2.setNote("净重数据修改车号，同步修改一次计量数据车号");
            meterWorkCarActualFirstMapper.updateByPrimaryKeySelective(first2);

            MeterBaseOperationLog logModel = new MeterBaseOperationLog();
            logModel.setId(PublicMethod.IdCreate(2));
            logModel.setCreateManNo(model.getUpdateManNo());
            logModel.setCreateManName(model.getUpdateManName());
            logModel.setCreateTime(new Date());
            logModel.setOperationPage("Web端-汽车衡一次数据维护");
            logModel.setOperationFunction("车号修改");
            logModel.setOperationId(model.getActualNo());
            logModel.setOperationPredictionNo(model.getPredictionNo());
            logModel.setOperationCarnNo(model.getCarNo());
            meterBaseOperationLogMapper.insertSelective(logModel);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

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

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

            MeterWorkCarActual actualRow = new MeterWorkCarActual();
            if(StringUtils.isNotEmpty(predictionNo)){
                MeterWorkCarActualFirst first1 = meterWorkCarActualFirstMapper.selectAndMatch(actualFirstNo, predictionNo);
                BeanUtils.copyProperties(first1,mwcaf1);
                MeterWorkCarActualFirst first2 = meterWorkCarActualFirstMapper.selectAndMatch(dataNo, predictionNo);
                BeanUtils.copyProperties(first2, mwcaf2);

                mwcaf1.setValueFlag(DbConstants.NET);
                mwcaf1.setUpdateManNo(userId);
                mwcaf1.setUpdateManName(userName);
                mwcaf1.setUpdateTime(new Date());
                mwcaf1.setNote("网页手动计量结净");
                mwcaf1.setIsPreScale("1");
                mwcaf1.setPredictionNo(predictionNo);
                mwcaf1.setPredictionType("1");
                meterWorkCarActualFirstMapper.updateByPrimaryKey(mwcaf1);

                // 皮重数据处理
                mwcaf2.setValueFlag(DbConstants.NET);
                mwcaf2.setUpdateManNo(userId);
                mwcaf2.setUpdateManName(userName);
                mwcaf2.setUpdateTime(new Date());
                mwcaf2.setNote("网页手动计量结净");
                mwcaf2.setIsPreScale("1");
                mwcaf2.setPredictionNo(predictionNo);
                mwcaf2.setPredictionType("1");
                //添加历史皮重
                meterWorkCarActualFirstService.doSaveHisTare(mwcaf2,"1");
                meterWorkCarActualFirstMapper.updateByPrimaryKey(mwcaf2);

                MeterWorkCarActual mwca1 = meterWorkCarActualMapper.manualAddPre(actualFirstNo, dataNo, predictionNo);
                BeanUtils.copyProperties(mwca1, actualRow);

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

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

                MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                logModel.setId(PublicMethod.IdCreate(2));
                logModel.setCreateManNo(userId);
                logModel.setCreateManName(userName);
                logModel.setCreateTime(new Date());
                logModel.setOperationPage("web端-汽车衡一次数据维护");
                logModel.setOperationFunction("毛皮结净");
                logModel.setOperationId(actualRow.getActualNo());
                logModel.setOperationPredictionNo(actualRow.getPredictionNo());
                logModel.setOperationCarnNo(actualRow.getCarNo());
                logModel.setOperationBeforeFix(actualRow.getActualFirst1No()+ "," + actualRow.getActualFirst2No());
                meterBaseOperationLogMapper.insertSelective(logModel);

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

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

                        mwcaf1.setValueFlag(DbConstants.NET);
                        mwcaf1.setUpdateManNo(userId);
                        mwcaf1.setUpdateManName(userName);
                        mwcaf1.setUpdateTime(new Date());
                        mwcaf1.setNote("网页手动计量结净");
                        mwcaf1.setIsPreScale("1");
                        mwcaf1.setPredictionNo(scale.getPredictionNo());
                        mwcaf1.setPredictionType("1");
                        meterWorkCarActualFirstMapper.updateByPrimaryKey(mwcaf1);

                        // 皮重数据处理
                        mwcaf2.setValueFlag(DbConstants.NET);
                        mwcaf2.setUpdateManNo(userId);
                        mwcaf2.setUpdateManName(userName);
                        mwcaf2.setUpdateTime(new Date());
                        mwcaf2.setNote("网页手动计量结净");
                        mwcaf2.setIsPreScale("1");
                        mwcaf2.setPredictionNo(scale.getPredictionNo());
                        mwcaf2.setPredictionType("1");
                        meterWorkCarActualFirstMapper.updateByPrimaryKey(mwcaf2);
                        //添加历史皮重
                        meterWorkCarActualFirstService.doSaveHisTare(mwcaf2,"1");
                        MeterWorkCarActual mwca1 = meterWorkCarActualMapper.manualAddPre(actualFirstNo, dataNo, scale.getPredictionNo());
                        BeanUtils.copyProperties(mwca1, actualRow);

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

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

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

                        MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                        logModel.setId(PublicMethod.IdCreate(2));
                        logModel.setCreateManNo(userId);
                        logModel.setCreateManName(userName);
                        logModel.setCreateTime(new Date());
                        logModel.setOperationPage("web端-汽车衡一次数据维护");
                        logModel.setOperationFunction("毛皮结净");
                        logModel.setOperationId(actualRow.getActualNo());
                        logModel.setOperationPredictionNo(actualRow.getPredictionNo());
                        logModel.setOperationCarnNo(actualRow.getCarNo());
                        logModel.setOperationBeforeFix(actualRow.getActualFirst1No()+ "," + actualRow.getActualFirst2No());
                        meterBaseOperationLogMapper.insertSelective(logModel);

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

                MeterWorkCarActualFirst first1 = meterWorkCarActualFirstMapper.selectByPrimaryKey(actualFirstNo);
                MeterWorkCarActualFirst first2 = meterWorkCarActualFirstMapper.selectByPrimaryKey(dataNo);

                first1.setValueFlag(DbConstants.NET);
                mwcaf1.setUpdateManNo(userId);
                mwcaf1.setUpdateManName(userName);
                mwcaf1.setUpdateTime(new Date());
                mwcaf1.setNote("网页手动计量结净");
                first1.setIsPreScale("0");
                meterWorkCarActualFirstMapper.updateByPrimaryKey(first1);

                // 皮重数据处理
                first2.setValueFlag(DbConstants.NET);
                first2.setUpdateManNo(userId);
                first2.setUpdateManName(userName);
                first2.setUpdateTime(new Date());
                first2.setNote("网页手动计量结净");
                first2.setIsPreScale("0");
                meterWorkCarActualFirstMapper.updateByPrimaryKey(first2);

                MeterWorkCarActual mwca2 = meterWorkCarActualMapper.manualAdd(first1.getActualFirstNo(), first2.getActualFirstNo());
                BeanUtils.copyProperties(mwca2, actualRow);

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

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

                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setValueFlag("1");
                actualRow.setNote(note);
                actualRow.setUploadFlag("1");
                actualRow.setIsPreScale("0");
                actualRow.setNetWeight(first1.getMeterWeight().subtract(first2.getMeterWeight()));
                actualRow.setCreateTime(new Date());
                if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                    actualRow.setNetManNo(actualRow.getGrossManNo());
                    actualRow.setNetManName(actualRow.getGrossManName());
                    actualRow.setNetTime(actualRow.getGrossTime());
                    actualRow.setCreateTime(actualRow.getGrossTime());
                    actualRow.setCreateManName(actualRow.getGrossManName());
                }else {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                    actualRow.setNetManNo(actualRow.getTareManNo());
                    actualRow.setNetManName(actualRow.getTareManName());
                    actualRow.setNetTime(actualRow.getTareTime());
                    actualRow.setCreateTime(actualRow.getTareTime());
                    actualRow.setCreateManName(actualRow.getTareManName());
                }
                actualRow.setUpdateManNo(userId);
                actualRow.setUpdateManName(userName);
                actualRow.setUpdateTime(new Date());
                meterWorkCarActualMapper.insertSelective(actualRow);

                MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                logModel.setId(PublicMethod.IdCreate(2));
                logModel.setCreateManNo(userId);
                logModel.setCreateManName(userName);
                logModel.setCreateTime(new Date());
                logModel.setOperationPage("web端-汽车衡一次数据维护");
                logModel.setOperationFunction("毛皮结净");
                logModel.setOperationId(actualRow.getActualNo());
                logModel.setOperationPredictionNo(actualRow.getPredictionNo());
                logModel.setOperationCarnNo(actualRow.getCarNo());
                logModel.setOperationBeforeFix(actualRow.getActualFirst1No()+ "," + actualRow.getActualFirst2No());
                meterBaseOperationLogMapper.insertSelective(logModel);
            }
            return actualRow;
        } catch (MarkerMetException e) {
            throw new MarkerMetException(500, "操作异常，请确认！！！");
        }
    }

    /**
     * 二次计量数据解除按钮
     *
     * @param models
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(List<MeterWorkCarActual> models) {
        try {
            int num = 0;
            if (models == null || models.size() <= 0) {
                throw new MarkerMetException(500, "参数为空！！！");
            }
            for(MeterWorkCarActual modelTemp: models) {
                if(StringUtils.isNotEmpty(modelTemp.getPredictionNo())) {
                    throw new MarkerMetException(500, "请将结净数据解除委托之后在毛皮分解");
                }
                //毛重
                MeterWorkCarActualFirst mwcaf1 = meterWorkCarActualFirstMapper.selectByPrimaryKey(modelTemp.getActualFirst1No());
                if(mwcaf1 != null && StringUtils.isNotEmpty(mwcaf1.getPredictionNo())) {
                    throw new MarkerMetException(500, "请将一次数据作业编号" +mwcaf1.getActualFirstNo() +"解除委托之后在毛皮分解");
                }
                //皮重
                MeterWorkCarActualFirst mwcaf2 = meterWorkCarActualFirstMapper.selectByPrimaryKey(modelTemp.getActualFirst2No());
                if(mwcaf2 != null && StringUtils.isNotEmpty(mwcaf2.getPredictionNo())) {
                    throw new MarkerMetException(500, "请将一次数据作业编号"+ mwcaf2.getActualFirstNo() +"解除委托之后在毛皮分解");
                }
//                if(modelTemp.getPredictionNo() != null && StringUtils.isNotEmpty(modelTemp.getPredictionNo())){
//                    PreTrackScale scaleTemp = preTrackScaleMapper.selectByPrimaryKey(modelTemp.getPredictionNo());
//                    if(scaleTemp.getValueFlag().equals("9")) {
//                        throw new MarkerMetException(500, "结净数据中包含已结算的委托!!!");
//
//                    }
//                    if(scaleTemp.getPredictionType().equals("5") || scaleTemp.getPredictionType().equals("6") || scaleTemp.getPredictionType().equals("7")){
//                        throw new MarkerMetException(500, "结净数据中包含特殊委托匹配结净的数据!!!");
//                    }
//                }
            }
            String userId = "";
            String userName = "";
            try{
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch (Exception e) {
                userId = "system";
                userName = "system";
            }
            for (MeterWorkCarActual model : models) {
                //毛重
                MeterWorkCarActualFirst mwcaf1 = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirst1No());
                if (mwcaf1 == null) {
                    throw new MarkerMetException(500, "未查询到相应的毛重数据！！");
                }
                // 清空毛重数据中的委托信息
//                MeterWorkCarActualFirst mwcafg = meterWorkCarActualFirstMapper.selectAndRemove(model.getActualFirst1No());
                MeterWorkCarActualFirst mwcafg = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirst1No());
                mwcafg.setValueFlag(DbConstants.VALID);
                mwcafg.setNote(model.getNote());
                mwcafg.setUpdateManNo(userId);
                mwcafg.setUpdateManName(userName);
                mwcafg.setNote("二次计量解除作废");
                mwcafg.setUpdateTime(new Date());
                mwcafg.setIsPreScale("0");
//                mwcafg.setDataSource("1");
                meterWorkCarActualFirstMapper.updateByPrimaryKey(mwcafg);
                //皮重
                MeterWorkCarActualFirst mwcaf2 = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirst2No());
                MeterBaseTermTareData mbttd = meterBaseTermTareDataMapper.selectByPrimaryKey(model.getActualFirst2No());
                if (mwcaf2 == null && mbttd == null) {
                    throw new MarkerMetException(500, "未查询到相应的皮重数据！！");
                }
                if (mwcaf2 != null) {
//                    MeterWorkCarActualFirst mwcaft = meterWorkCarActualFirstMapper.selectAndRemove(mwcaf2.getActualFirstNo());
                    MeterWorkCarActualFirst mwcaft = meterWorkCarActualFirstMapper.selectByPrimaryKey(mwcaf2.getActualFirstNo());
                    if ("2".equals(mwcaft.getWeightType())) {
                        mwcaft.setValueFlag(DbConstants.INVALID); //期限皮重置为作废
                        mwcaft.setUpdateManNo(userId);
                        mwcaft.setUpdateManName(userName);
                        mwcaft.setNote(model.getNote());
                        mwcaft.setIsPreScale("0");
//                        mwcaft.setDataSource("1");
                        mwcaft.setUpdateTime(new Date());
                    } else {
                        mwcaft.setValueFlag(DbConstants.VALID);
                        mwcaft.setUpdateManNo(userId);
                        mwcaft.setUpdateManName(userName);
                        mwcaft.setNote(model.getNote());
                        mwcaft.setIsPreScale("0");
//                        mwcaft.setDataSource("1");
                        mwcaft.setUpdateTime(new Date());
                    }
                    meterWorkCarActualFirstMapper.updateByPrimaryKey(mwcaft);
                }
                //回退至预环节的前一状态
//                if (!model.getMeterTypeNo().equals("001006003")) {
//                    List<MeterWorkPreLink> mwpls = meterWorkPreLinkService.getNext(model.getPredictionNo(), DbConstants.CLEAN);
//                    for (MeterWorkPreLink mwpl : mwpls) {
//                        mwpl.setValueFlag("0");//未使用
//                        meterWorkPreLinkMapper.updateByPrimaryKey(mwpl);
//                    }
                //修改预报
//                if (StringUtils.isNotEmpty(model.getPredictionNo())) {
//                    PreTrackScale scale = new PreTrackScale();
//                    scale.setPredictionNo(model.getPredictionNo());
//                    scale.setValueFlag("1");
//                    scale.setUploadFlag("1");
//                    scale.setUpdateTime(new Date());
//                    scale.setUpdateManNo("system");
//                    scale.setUpdateManName("计量结净数据解除操作");
//                    preTrackScaleMapper.updateByPrimaryKeySelective(scale);
//                }
//                }
                //meterWorkCarActualMapper.deleteByPrimaryKey(model.getActualNo());
                MeterWorkCarActual modelTemp = meterWorkCarActualMapper.cleanPredcition(model.getActualNo());
                modelTemp.setUploadFlag("1");
                modelTemp.setValueFlag("0");
                modelTemp.setIsPreScale("0");
                modelTemp.setUpdateManNo(userId);
                modelTemp.setUpdateManName(userName);
                modelTemp.setUpdateTime(new Date());
                meterWorkCarActualMapper.updateByPrimaryKey(modelTemp);

                MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                logModel.setId(PublicMethod.IdCreate(2));
                logModel.setCreateManNo(userId);
                logModel.setCreateManName(userName);
                logModel.setCreateTime(new Date());
                logModel.setOperationPage("web端-汽车衡净重数据维护");
                logModel.setOperationFunction("毛皮分解");
                logModel.setOperationId(model.getActualNo());
                logModel.setOperationPredictionNo(model.getPredictionNo());
                logModel.setOperationCarnNo(model.getCarNo());
                logModel.setOperationBeforeFix(model.toString());
                logModel.setOperationAfterFix(modelTemp.toString());
                meterBaseOperationLogMapper.insertSelective(logModel);
                num++;
            }

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

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

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

        String type = params.get("type") == null ? "" : params.get("type").toString();
        MeterWorkCarActual actualModel01 = meterWorkCarActualMapper.selectByPrimaryKey(params.get("actualNo01").toString());
        MeterWorkCarActual actualModel02 = meterWorkCarActualMapper.selectByPrimaryKey(params.get("actualNo02").toString());
        if(actualModel01 == null || actualModel02 == null){
            rm.setMessage("未找到结净计量数据，请确认！！！");
            return rm;
        }
        List<MeterWorkCarActual> models = new ArrayList<>();
        models.add(actualModel01);
        models.add(actualModel02);
        if(!models.get(0).getPredictionCombination().equals(models.get(1).getPredictionCombination())){
            rm.setMessage("必须选择两个预报组合号一致的结净数据!!!");
            return rm;
        }
        for(MeterWorkCarActual modelTemp: models) {
            PreTrackScale scaleTemp = preTrackScaleMapper.selectByPrimaryKey(modelTemp.getPredictionNo());
            if(scaleTemp.getValueFlag().equals("9")) {
                rm.setMessage("两个结净数据中包含已结算的委托!!!");
                return rm;
            }
        }
        String userId = "";
        String userName = "";
        try{
            UserPayload userPayload = UserPayload.getCurrUser();
            userId = userPayload.getUserCode();
            userName = userPayload.getUserName();
        }catch (Exception e) {
            userId = "system";
            userName = "system";
        }
        switch(type){
            case "5":
            case "6":
            case "7": {
                if(type.equals("5")){
                    //类型为双委托类型
                    for (MeterWorkCarActual model: models) {
                        if(!model.getPredictionType().equals("5")) {
                            rm.setMessage("选中的计量数据当中有一条或多条不是双委托结净数据，请确认！！");
                            return rm;
                        }
                    }
                } else if(type.equals("6")){
                    //类型为混卸类型
                    for (MeterWorkCarActual model: models) {
                        if(!model.getPredictionType().equals("6")) {
                            rm.setMessage("选中的计量数据当中有一条或多条不是混卸委托结净数据，请确认！！");
                        }
                    }
                }else if(type.equals("7")){
                    //类型为混装类型
                    for (MeterWorkCarActual model: models) {
                        if(!model.getPredictionType().equals("7")) {
                            rm.setMessage("选中的计量数据当中有一条或多条不是混装委托结净数据，请确认！！");
                        }
                    }
                }
                for (MeterWorkCarActual model: models) {
                    // 改委托
                    if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                        removeScaleEl(model.getPredictionNo());
                    }
                    // 清空一次毛皮重委托信息
                    MeterWorkCarActualFirst firstGross = meterWorkCarActualFirstMapper.selectAndRemove(model.getActualFirst1No());
                    MeterWorkCarActualFirst firstTare = meterWorkCarActualFirstMapper.selectAndRemove(model.getActualFirst2No());
                    if(firstGross != null){
                        removeFirstEl(firstGross);
                        meterWorkCarActualFirstMapper.updateByPrimaryKey(firstGross);
                    }
                    if(firstTare != null){
                        removeFirstEl(firstTare);
                        meterWorkCarActualFirstMapper.updateByPrimaryKey(firstTare);
                    }
                    MeterWorkCarActual modelTemp = meterWorkCarActualMapper.cleanPredcition(model.getActualNo());
                    removeEl(modelTemp);
                    meterWorkCarActualMapper.updateByPrimaryKeySelective(modelTemp);

                    MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                    logModel.setId(PublicMethod.IdCreate(2));
                    logModel.setCreateManNo(userId);
                    logModel.setCreateManName(userName);
                    logModel.setCreateTime(new Date());
                    logModel.setOperationPage("web端-汽车衡净重数据维护");
                    if(type.equals("5")){
                        logModel.setOperationFunction("取消双委托结净");
                    }else if(type.equals("6")) {
                        logModel.setOperationFunction("取消混卸结净");
                    }else if(type.equals("7")) {
                        logModel.setOperationFunction("取消混装结净");
                    }
                    logModel.setOperationId(model.getActualNo());
                    logModel.setOperationPredictionNo(model.getPredictionNo());
                    logModel.setOperationCarnNo(model.getCarNo());
                    logModel.setOperationBeforeFix(model.toString());
                    logModel.setOperationAfterFix(modelTemp.toString());
                    meterBaseOperationLogMapper.insertSelective(logModel);
                }
                rm.setSucceed();
                rm.setMessage("操作成功！！");
                rm.setCode("200");
            }
        }
        return rm;
    }
    //还原委托
    public void removeScaleEl(String predictionNo){
        String userId;
        String userName;
        try{
            UserPayload payload = UserPayload.getCurrUser();
            userId = payload.getUserCode();
            userName = payload.getUserName();
        }catch(Exception e) {
            userId = "admin";
            userName = "admins";
        }
        PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(predictionNo);
        scale.setValueFlag("1");
        scale.setUploadFlag("1");
        scale.setUpdateTime(new Date());
        scale.setUpdateManNo(userId);
        scale.setUpdateManName(userName);
        preTrackScaleMapper.updateByPrimaryKeySelective(scale);
    }

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

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

    @Override
    public RESTfulResult commonRemove(HashMap params) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        rm.setData(null);
        rm.setCode("500");
        try{
            if(params.get("note") == null || StringUtils.isEmpty(params.get("note").toString())){
                rm.setMessage("操作原因不能为空，请确认！！！");
                return rm;
            }
            String note = params.get("note").toString();

            MeterWorkCarActual actualModel = meterWorkCarActualMapper.selectByPrimaryKey(params.get("actualNo").toString());
            if(actualModel == null){
                rm.setMessage("未找到结净计量数据，请确认！！！");
                return rm;
            }
            List<MeterWorkCarActual> models = new ArrayList<MeterWorkCarActual>();
            models.add(actualModel);
            for(MeterWorkCarActual modelTemp: models) {
                if(modelTemp.getPredictionNo() != null && StringUtils.isNotEmpty(modelTemp.getPredictionNo())){
                    PreTrackScale scaleTemp = preTrackScaleMapper.selectByPrimaryKey(modelTemp.getPredictionNo());
                    if(scaleTemp.getValueFlag().equals("9")) {
                        rm.setMessage("结净数据中包含已结算的委托!!!");
                        return rm;
                    }
//                    if(scaleTemp.getPredictionType().equals("5") || scaleTemp.getPredictionType().equals("6") || scaleTemp.getPredictionType().equals("7")){
//                        rm.setMessage("结净数据中包含特殊委托匹配结净的数据!!!");
//                        return rm;
//                    }
                }
            }
            String userId = "";
            String userName = "";
            try{
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch (Exception e) {
                userId = "system";
                userName = "system";
            }
            for (MeterWorkCarActual model: models) {
                //循环每一个结净记录
                //委托不能为空
                // 改委托
                PreTrackScale scale = new PreTrackScale();
                if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                    scale = preTrackScaleMapper.selectByPrimaryKey(model.getPredictionNo());
                    scale.setValueFlag("1");
                    scale.setUploadFlag("1");
                    scale.setUpdateTime(new Date());
                    scale.setUpdateManNo(userId);
                    scale.setUpdateManName(userName);
                    preTrackScaleMapper.updateByPrimaryKeySelective(scale);
                }else {
                    if(StringUtils.isNotEmpty(model.getActualFirst1No())) {
                        MeterWorkCarActualFirst gross = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirst1No());
                        if(StringUtils.isNotEmpty(gross.getPredictionNo())) {
                            scale = preTrackScaleMapper.selectByPrimaryKey(gross.getPredictionNo());
                            scale.setValueFlag("1");
                            scale.setUploadFlag("1");
                            scale.setUpdateTime(new Date());
                            scale.setUpdateManNo(userId);
                            scale.setUpdateManName(userName);
                            preTrackScaleMapper.updateByPrimaryKeySelective(scale);
                        }
                    }
                    if(StringUtils.isNotEmpty(model.getActualFirst2No())) {
                        MeterWorkCarActualFirst tare = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirst2No());
                        if(StringUtils.isNotEmpty(tare.getPredictionNo())) {
                            scale = preTrackScaleMapper.selectByPrimaryKey(tare.getPredictionNo());
                            scale.setValueFlag("1");
                            scale.setUploadFlag("1");
                            scale.setUpdateTime(new Date());
                            scale.setUpdateManNo(userId);
                            scale.setUpdateManName(userName);
                            preTrackScaleMapper.updateByPrimaryKeySelective(scale);
                        }
                    }
                }
                // 清空一次毛重、皮重委托信息
                MeterWorkCarActualFirst firstGross;
                MeterWorkCarActualFirst firstTare;
                if(StringUtils.isNotEmpty(model.getActualFirst1No())&&StringUtils.isNotEmpty(model.getActualFirst2No())){
                    firstGross = meterWorkCarActualFirstMapper.selectAndRemove(model.getActualFirst1No());
                    firstTare =  meterWorkCarActualFirstMapper.selectAndRemove(model.getActualFirst2No());
                }else{
                    firstGross = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirst1No());
                    firstTare = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirst2No());
                }
                if(firstGross != null){
                    firstGross.setValueFlag(DbConstants.VALID);
                    firstGross.setUpdateManNo(userId);
                    firstGross.setUpdateManName(userName);
                    firstGross.setNote(model.getNote());
                    firstGross.setUpdateTime(new Date());
                    firstGross.setIsPreScale("0");
                    firstGross.setValueFlag("0");
                    firstGross.setNote(note);
                    meterWorkCarActualFirstMapper.updateByPrimaryKey(firstGross);
                }
                if(firstTare != null){
                    firstTare.setValueFlag(DbConstants.VALID);
                    firstTare.setUpdateManNo(userId);
                    firstTare.setUpdateManName(userName);
                    firstTare.setNote(model.getNote());
                    firstTare.setUpdateTime(new Date());
                    firstTare.setIsPreScale("0");
                    firstTare.setValueFlag("0");
                    firstTare.setNote(note);
                    meterWorkCarActualFirstMapper.updateByPrimaryKey(firstTare);
                }
                // 作废结净
                MeterWorkCarActual modelTemp = meterWorkCarActualMapper.cleanPredcition(model.getActualNo());
                modelTemp.setUploadFlag("1");
                modelTemp.setValueFlag("0");
                modelTemp.setIsPreScale("0");
                modelTemp.setUpdateManNo(userId);
                modelTemp.setUpdateManName(userName);
                modelTemp.setUpdateTime(new Date());
                modelTemp.setNote(note);
                meterWorkCarActualMapper.updateByPrimaryKey(modelTemp);
                //删结净
                //meterWorkCarActualMapper.deleteByPrimaryKey(model.getActualNo());

                MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                logModel.setId(PublicMethod.IdCreate(2));
                logModel.setCreateManNo(userId);
                logModel.setCreateManName(userName);
                logModel.setCreateTime(new Date());
                logModel.setOperationPage("web端-汽车衡净重数据维护");
                logModel.setOperationFunction("结净废除");
                logModel.setOperationId(model.getActualNo());
                logModel.setOperationPredictionNo(model.getPredictionNo());
                logModel.setOperationCarnNo(model.getCarNo());
                logModel.setOperationBeforeFix(model.toString());
                logModel.setOperationAfterFix(modelTemp.toString());
                meterBaseOperationLogMapper.insertSelective(logModel);
            }
            rm.setSucceed();
            rm.setMessage("操作成功！！");
            rm.setCode("200");
        }catch(Exception e){
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult localCommonRemove(HashMap params) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        rm.setData(null);
        rm.setCode("500");
        try{
            MeterWorkCarActual actualModel = meterWorkCarActualMapper.selectByPrimaryKey(params.get("actualNo").toString());
            if(actualModel == null){
                rm.setMessage("未找到结净计量数据，请确认！！！");
                return rm;
            }
            List<MeterWorkCarActual> models = new ArrayList<MeterWorkCarActual>();
            models.add(actualModel);
            for(MeterWorkCarActual modelTemp: models) {
                if(modelTemp.getPredictionNo() != null && StringUtils.isNotEmpty(modelTemp.getPredictionNo())){
                    PreTrackScale scaleTemp = preTrackScaleMapper.selectByPrimaryKey(modelTemp.getPredictionNo());
                    if(scaleTemp.getValueFlag().equals("9")) {
                        rm.setMessage("结净数据中包含已结算的委托!!!");
                        return rm;
                    }
//                    if(scaleTemp.getPredictionType().equals("5") || scaleTemp.getPredictionType().equals("6") || scaleTemp.getPredictionType().equals("7")){
//                        rm.setMessage("结净数据中包含特殊委托匹配结净的数据!!!");
//                        return rm;
//                    }
                }
            }
            String userId = params.get("updateManNo") != null ? params.get("updateManNo").toString(): "";
            String userName = params.get("updateManName") != null ? params.get("updateManName").toString(): "";

            for (MeterWorkCarActual model: models) {
                //循环每一个结净记录
                //委托不能为空
                // 改委托
                PreTrackScale scale = new PreTrackScale();
                if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                    scale = preTrackScaleMapper.selectByPrimaryKey(model.getPredictionNo());
                    scale.setValueFlag("1");
                    scale.setUploadFlag("1");
                    scale.setUpdateTime(new Date());
                    scale.setUpdateManNo(userId);
                    scale.setUpdateManName(userName);
                    preTrackScaleMapper.updateByPrimaryKeySelective(scale);
                }else {
                    if(StringUtils.isNotEmpty(model.getActualFirst1No())) {
                        MeterWorkCarActualFirst gross = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirst1No());
                        if(StringUtils.isNotEmpty(gross.getPredictionNo())) {
                            scale = preTrackScaleMapper.selectByPrimaryKey(gross.getPredictionNo());
                            scale.setValueFlag("1");
                            scale.setUploadFlag("1");
                            scale.setUpdateTime(new Date());
                            scale.setUpdateManNo(userId);
                            scale.setUpdateManName(userName);
                            preTrackScaleMapper.updateByPrimaryKeySelective(scale);
                        }
                    }
                    if(StringUtils.isNotEmpty(model.getActualFirst2No())) {
                        MeterWorkCarActualFirst tare = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirst2No());
                        if(StringUtils.isNotEmpty(tare.getPredictionNo())) {
                            scale = preTrackScaleMapper.selectByPrimaryKey(tare.getPredictionNo());
                            scale.setValueFlag("1");
                            scale.setUploadFlag("1");
                            scale.setUpdateTime(new Date());
                            scale.setUpdateManNo(userId);
                            scale.setUpdateManName(userName);
                            preTrackScaleMapper.updateByPrimaryKeySelective(scale);
                        }
                    }
                }
                // 清空一次毛重、皮重委托信息
                MeterWorkCarActualFirst firstGross;
                MeterWorkCarActualFirst firstTare;
                if(StringUtils.isNotEmpty(model.getActualFirst1No())&&StringUtils.isNotEmpty(model.getActualFirst2No())){
                    firstGross = meterWorkCarActualFirstMapper.selectAndRemove(model.getActualFirst1No());
                    firstTare =  meterWorkCarActualFirstMapper.selectAndRemove(model.getActualFirst2No());
                }else{
                    firstGross = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirst1No());
                    firstTare = meterWorkCarActualFirstMapper.selectByPrimaryKey(model.getActualFirst2No());
                }
                if(firstGross != null){
                    firstGross.setValueFlag(DbConstants.VALID);
                    firstGross.setUpdateManNo(userId);
                    firstGross.setUpdateManName(userName);
                    firstGross.setNote(model.getNote());
                    firstGross.setUpdateTime(new Date());
                    firstGross.setIsPreScale("0");
                    firstGross.setValueFlag("0");
                    meterWorkCarActualFirstMapper.updateByPrimaryKey(firstGross);
                }
                if(firstTare != null){
                    firstTare.setValueFlag(DbConstants.VALID);
                    firstTare.setUpdateManNo(userId);
                    firstTare.setUpdateManName(userName);
                    firstTare.setNote(model.getNote());
                    firstTare.setUpdateTime(new Date());
                    firstTare.setIsPreScale("0");
                    firstTare.setValueFlag("0");
                    meterWorkCarActualFirstMapper.updateByPrimaryKey(firstTare);
                }
                // 作废结净
                MeterWorkCarActual modelTemp = meterWorkCarActualMapper.cleanPredcition(model.getActualNo());
                modelTemp.setUploadFlag("1");
                modelTemp.setValueFlag("0");
                modelTemp.setIsPreScale("0");
                modelTemp.setUpdateManNo(userId);
                modelTemp.setUpdateManName(userName);
                modelTemp.setUpdateTime(new Date());
                meterWorkCarActualMapper.updateByPrimaryKey(modelTemp);
                //删结净
                //meterWorkCarActualMapper.deleteByPrimaryKey(model.getActualNo());

                MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                logModel.setId(PublicMethod.IdCreate(2));
                logModel.setCreateManNo(userId);
                logModel.setCreateManName(userName);
                logModel.setCreateTime(new Date());
                logModel.setOperationPage("C#端-本地计量结净数据维护");
                logModel.setOperationFunction("结净废除");
                logModel.setOperationId(model.getActualNo());
                logModel.setOperationPredictionNo(model.getPredictionNo());
                logModel.setOperationCarnNo(model.getCarNo());
                logModel.setOperationBeforeFix(model.toString());
                logModel.setOperationAfterFix(modelTemp.toString());
                meterBaseOperationLogMapper.insertSelective(logModel);
            }
            rm.setSucceed();
            rm.setMessage("操作成功！！");
            rm.setCode("200");
        }catch(Exception e){
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！>>>" + e.getMessage());
        }
        return rm;
    }

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

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

            } else {

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

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

        List<String> actualNoList = new ArrayList<>();
        MeterWorkCarActual carActual = meterWorkCarActualMapper.selectByPrimaryKey(actualNo);
        if(carActual != null) {
            actualNoList.add(0, carActual.getActualFirst1No());
            actualNoList.add(1, carActual.getActualFirst2No());
//            String entrust = carActual.getPredictionNo() == null ? "" : carActual.getPredictionNo();
//            String carNo = carActual.getCarNo() == null ? "": carActual.getCarNo();
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("carNo", carActual.getCarNo());
            obj.put("predictionNo", carActual.getPredictionNo());
            List<MeterWorkCarActualFirst> firstList = meterWorkCarActualFirstMapper.selectById(actualNoList);
            List<PreTrackScale> scale = preTrackScaleMapper.selectByScale(obj);
            HashMap<String, Object> map = new HashMap<>();
            map.put("firstList", firstList);
            map.put("scale", scale);
            rm.setData(map);
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            return rm;
        }
        return rm;
    }

    @Override
    public PageList<MeterWorkCarActual> likeByDesc(HashMap<String, Object> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<MeterWorkCarActual> rows = meterWorkCarActualMapper.likeByDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public List<MeterWorkCarActual> likeByValueFlag(HashMap<String, Object> params) {
        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }
        List<MeterWorkCarActual> rows = meterWorkCarActualMapper.likeByDesc(params);
        return rows;
    }

    @Override
    public List<MeterWorkCarActual> carActualDataDetail(HashMap<String, Object> params) {
        List<MeterWorkCarActual> rows = meterWorkCarActualMapper.carActualDataDetail(params);
        return rows;
    }

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

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

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

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

            MeterWorkCarActual model = meterWorkCarActualMapper.selectByPrimaryKey(actualNo);
            PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(predictionNo);
            if(model == null) {
                rm.setMessage("不存在对应的结净数据,请确认！！");
                return rm;
            }else if(!model.getIsPreScale().equals("0")) {
                rm.setMessage("只有未匹配的净重数据才能进行委托匹配操作");
                return rm;
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(model.getUpdateTime() != null && StringUtils.isNotEmpty(model.getUpdateTime().toString())) {
                String sDate = df.format(model.getUpdateTime());
                String eDate = df.format(new Date());
                long sTime = df.parse(sDate).getTime();
                long eTime = df.parse(eDate).getTime();
                long diff = (eTime - sTime) / 1000;
                if(diff <= 15) {
                    rm.setMessage("匹配委托需在解除委托15秒后进行，请稍等");
                    return rm;
                }
            }
            if(scale == null) {
                rm.setMessage("不存在对应的预报数据,请确认！！");
                return rm;
            }else {
                if(scale.getValueFlag().equals("2")) {
                    rm.setMessage("只有未使用的委托才能进行匹配委托操作，请确认！！");
                    return rm;
                }
                if (!model.getCarNo().equals(scale.getCarNo())) {
                    rm.setMessage("结净数据中车号和预报中车号不一致，请确认！！");
                    return rm;
                }
//                if (!scale.getPredictionType().equals("1")) {
//                    rm.setMessage("委托信息不是普通委托不能进行匹配委托，请确认！！");
//                    return rm;
//                }
            }
            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();
            /*String userId = "systems";
            String userName = "systems";*/

            // 处理一次计量数据
            // 毛重
            MeterWorkCarActualFirst grossrow = meterWorkCarActualFirstMapper.selectAndMatch(model.getActualFirst1No(), predictionNo);
            grossrow.setUpdateTime(new Date());
            grossrow.setUpdateManName(userName);
            grossrow.setUpdateManNo(userId);
            grossrow.setIsPreScale("1");
            meterWorkCarActualFirstMapper.updateByPrimaryKey(grossrow);
            // 皮重
            MeterWorkCarActualFirst tarerow = meterWorkCarActualFirstMapper.selectAndMatch(model.getActualFirst2No(), predictionNo);
            tarerow.setUpdateTime(new Date());
            tarerow.setUpdateManName(userName);
            tarerow.setUpdateManNo(userId);
            tarerow.setIsPreScale("1");
            meterWorkCarActualFirstMapper.updateByPrimaryKey(tarerow);

            MeterWorkCarActual mwca = meterWorkCarActualMapper.mathPredcition(actualNo, predictionNo);
            mwca.setPredictionType("1");
            mwca.setCreateManName(userName);
            mwca.setCreateTime(new Date());
            mwca.setUpdateManNo(userId);
            mwca.setUpdateManName(userName);
            mwca.setUpdateTime(new Date());
            mwca.setNote("匹配委托");
            mwca.setIsPreScale("1");
            mwca.setValueFlag("1");
            mwca.setUploadFlag("1");
            meterWorkCarActualMapper.updateByPrimaryKey(mwca);

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

            MeterBaseOperationLog logModel = new MeterBaseOperationLog();
            logModel.setId(PublicMethod.IdCreate(2));
            logModel.setCreateManNo(userId);
            logModel.setCreateManName(userName);
            logModel.setCreateTime(new Date());
            logModel.setOperationPage("web端-汽车衡净重数据维护");
            logModel.setOperationFunction("匹配委托");
            logModel.setOperationId(model.getActualNo());
            logModel.setOperationPredictionNo(predictionNo);
            logModel.setOperationCarnNo(model.getCarNo());
            logModel.setOperationBeforeFix(model.toString());
            logModel.setOperationAfterFix(mwca.toString());
            meterBaseOperationLogMapper.insertSelective(logModel);

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

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

            MeterWorkCarActual model = meterWorkCarActualMapper.selectByPrimaryKey(actualNo);
            PreTrackScale scale = preTrackScaleMapper.selectByPrimaryKey(predictionNo);
            if(model == null) {
                rm.setMessage("不存在对应的结净数据,请确认！！");
                return rm;
            }else if(!model.getIsPreScale().equals("0")) {
                rm.setMessage("只有未匹配的净重数据才能进行委托匹配操作");
                return rm;
            }
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            if(model.getUpdateTime() != null && StringUtils.isNotEmpty(model.getUpdateTime().toString())) {
                String sDate = df.format(model.getUpdateTime());
                String eDate = df.format(new Date());
                long sTime = df.parse(sDate).getTime();
                long eTime = df.parse(eDate).getTime();
                long diff = (eTime - sTime) / 1000;
                if(diff <= 15) {
                    rm.setMessage("匹配委托需在解除委托15秒后进行，请稍等");
                    return rm;
                }
            }
            if(scale == null) {
                rm.setMessage("不存在对应的预报数据,请确认！！");
                return rm;
            }else {
                if(scale.getValueFlag().equals("2")) {
                    rm.setMessage("只有未使用的委托才能进行匹配委托操作，请确认！！");
                    return rm;
                }
                if (!model.getCarNo().equals(scale.getCarNo())) {
                    rm.setMessage("结净数据中车号和预报中车号不一致，请确认！！");
                    return rm;
                }
//                if (!scale.getPredictionType().equals("1")) {
//                    rm.setMessage("委托信息不是普通委托不能进行匹配委托，请确认！！");
//                    return rm;
//                }
            }
            String userId = updateManNo;
            String userName = updateManName;

            // 处理一次计量数据
            // 毛重
            MeterWorkCarActualFirst grossrow = meterWorkCarActualFirstMapper.selectAndMatch(model.getActualFirst1No(), predictionNo);
            grossrow.setUpdateTime(new Date());
            grossrow.setUpdateManName(userName);
            grossrow.setUpdateManNo(userId);
            grossrow.setIsPreScale("1");
            meterWorkCarActualFirstMapper.updateByPrimaryKey(grossrow);
            // 皮重
            MeterWorkCarActualFirst tarerow = meterWorkCarActualFirstMapper.selectAndMatch(model.getActualFirst2No(), predictionNo);
            tarerow.setUpdateTime(new Date());
            tarerow.setUpdateManName(userName);
            tarerow.setUpdateManNo(userId);
            tarerow.setIsPreScale("1");
            meterWorkCarActualFirstMapper.updateByPrimaryKey(tarerow);

            MeterWorkCarActual mwca = meterWorkCarActualMapper.mathPredcition(actualNo, predictionNo);
            mwca.setPredictionType("1");
            mwca.setCreateManName(userName);
            mwca.setCreateTime(new Date());
            mwca.setUpdateManNo(userId);
            mwca.setUpdateManName(userName);
            mwca.setUpdateTime(new Date());
            mwca.setNote("匹配委托");
            mwca.setIsPreScale("1");
            mwca.setValueFlag("1");
            mwca.setUploadFlag("1");
            meterWorkCarActualMapper.updateByPrimaryKey(mwca);

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

            MeterBaseOperationLog logModel = new MeterBaseOperationLog();
            logModel.setId(PublicMethod.IdCreate(2));
            logModel.setCreateManNo(userId);
            logModel.setCreateManName(userName);
            logModel.setCreateTime(new Date());
            logModel.setOperationPage("C#端-本地计量结净数据维护");
            logModel.setOperationFunction("匹配委托");
            logModel.setOperationId(model.getActualNo());
            logModel.setOperationPredictionNo(predictionNo);
            logModel.setOperationCarnNo(model.getCarNo());
            logModel.setOperationBeforeFix(model.toString());
            logModel.setOperationAfterFix(mwca.toString());
            meterBaseOperationLogMapper.insertSelective(logModel);

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkCarActual cleanPrediction (MeterWorkCarActual model) throws MarkerMetException {
        if (model.getActualNo() == null || "".equals(model.getActualNo())) {
            throw new MarkerMetException(500, "结净数据主键不能为空");
        }
        if (!model.getValueFlag().equals("1") && model.getPredictionNo() != null && !"".equals(model.getPredictionNo())) {
            throw new MarkerMetException(500, "只有有效已匹配的结净数据才能进行解除操作");
        }
        MeterWorkCarActual mwcaquery = meterWorkCarActualMapper.selectByPrimaryKey(model.getActualNo());
        if (mwcaquery == null) {
            throw new MarkerMetException(500, "不存在对应的结净数据");
        }else {
            try{
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                if(mwcaquery.getUpdateTime() != null && StringUtils.isNotEmpty(mwcaquery.getUpdateTime().toString())) {
                    String sDate = df.format(mwcaquery.getUpdateTime());
                    String eDate = df.format(new Date());
                    long sTime = df.parse(sDate).getTime();
                    long eTime = df.parse(eDate).getTime();
                    long diff = (eTime - sTime) / 1000;
                    if(diff <= 15) {
                        throw new MarkerMetException(500, "解除委托需在匹配委托15秒后进行，请稍等");
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
                throw new MarkerMetException(500, "操作异常");
            }
        }

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

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

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

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

        mwca.setUpdateManNo(userId);
        mwca.setUpdateManName(userName);
        mwca.setUpdateTime(new Date());
        mwca.setNote(model.getNote());
        //mwca.setValueFlag("3");
        mwca.setIsPreScale("0");
        mwca.setUploadFlag("1");
        mwca.setValueFlag("1");
        meterWorkCarActualMapper.updateByPrimaryKey(mwca);

        MeterBaseOperationLog logModel = new MeterBaseOperationLog();
        logModel.setId(PublicMethod.IdCreate(2));
        logModel.setCreateManNo(userId);
        logModel.setCreateManName(userName);
        logModel.setCreateTime(new Date());
        logModel.setOperationPage("web端-汽车衡净重数据维护");
        logModel.setOperationFunction("撤销委托");
        logModel.setOperationId(mwcaquery.getActualNo());
        logModel.setOperationPredictionNo(mwcaquery.getPredictionNo());
        logModel.setOperationCarnNo(mwcaquery.getCarNo());
        logModel.setOperationBeforeFix(mwcaquery.toString());
        logModel.setOperationAfterFix(mwca.toString());
        meterBaseOperationLogMapper.insertSelective(logModel);
        return mwca;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkCarActual localRemovePrediction(MeterWorkCarActual model) throws MarkerMetException {
        if (model.getActualNo() == null || "".equals(model.getActualNo())) {
            throw new MarkerMetException(500, "结净数据主键不能为空");
        }
        if (!model.getValueFlag().equals("1") && model.getPredictionNo() != null && !"".equals(model.getPredictionNo())) {
            throw new MarkerMetException(500, "只有有效已匹配的结净数据才能进行解除操作");
        }
        MeterWorkCarActual mwcaquery = meterWorkCarActualMapper.selectByPrimaryKey(model.getActualNo());
        if (mwcaquery == null) {
            throw new MarkerMetException(500, "不存在对应的结净数据");
        }else {
            try{
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                if(mwcaquery.getUpdateTime() != null && StringUtils.isNotEmpty(mwcaquery.getUpdateTime().toString())) {
                    String sDate = df.format(mwcaquery.getUpdateTime());
                    String eDate = df.format(new Date());
                    long sTime = df.parse(sDate).getTime();
                    long eTime = df.parse(eDate).getTime();
                    long diff = (eTime - sTime) / 1000;
                    if(diff <= 15) {
                        throw new MarkerMetException(500, "解除委托需在匹配委托15秒后进行，请稍等");
                    }
                }
            } catch (ParseException e) {
                e.printStackTrace();
                throw new MarkerMetException(500, "操作异常");
            }
        }

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

        String userId = model.getUpdateManNo();
        String userName = model.getUpdateManName();
        // 处理一次计量数据
        // 毛重
        MeterWorkCarActualFirst grossrow = meterWorkCarActualFirstMapper.selectAndRemove(model.getActualFirst1No());
//        grossrow.setValueFlag("3");
        grossrow.setUpdateTime(new Date());
        grossrow.setUpdateManName(userName);
        grossrow.setUpdateManNo(userId);
        grossrow.setIsPreScale("0");
        meterWorkCarActualFirstMapper.updateByPrimaryKey(grossrow);

        // 皮重
        MeterWorkCarActualFirst tarerow = meterWorkCarActualFirstMapper.selectAndRemove(model.getActualFirst2No());
//        tarerow.setValueFlag("3");
        tarerow.setUpdateTime(new Date());
        tarerow.setUpdateManName(userName);
        tarerow.setUpdateManNo(userId);
        tarerow.setIsPreScale("0");
        meterWorkCarActualFirstMapper.updateByPrimaryKey(tarerow);

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

        mwca.setUpdateManNo(userId);
        mwca.setUpdateManName(userName);
        mwca.setUpdateTime(new Date());
        mwca.setNote("撤销委托");
        //mwca.setValueFlag("3");
        mwca.setIsPreScale("0");
        mwca.setUploadFlag("1");
        mwca.setValueFlag("1");
        meterWorkCarActualMapper.updateByPrimaryKey(mwca);

        MeterBaseOperationLog logModel = new MeterBaseOperationLog();
        logModel.setId(PublicMethod.IdCreate(2));
        logModel.setCreateManNo(userId);
        logModel.setCreateManName(userName);
        logModel.setCreateTime(new Date());
        logModel.setOperationPage("C#端-本地计量结净数据维护");
        logModel.setOperationFunction("撤销委托");
        logModel.setOperationId(mwcaquery.getActualNo());
        logModel.setOperationPredictionNo(mwcaquery.getPredictionNo());
        logModel.setOperationCarnNo(mwcaquery.getCarNo());
        logModel.setOperationBeforeFix(mwcaquery.toString());
        logModel.setOperationAfterFix(mwca.toString());
        meterBaseOperationLogMapper.insertSelective(logModel);
        return mwca;
    }

    /**
     * @param actualFirstNo 毛重数据
     * @param dataNo        皮重数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkCarActual termclean(String actualFirstNo, String dataNo) {
        try {
            if (actualFirstNo == null || "".equals(actualFirstNo)) {
                throw new MarkerMetException(500, "毛重数据编号为空");
            }
            if (dataNo == null || "".equals(dataNo)) {
                throw new MarkerMetException(500, "皮重数据编号为空");
            }
            MeterWorkCarActualFirst mwcaf1 = meterWorkCarActualFirstMapper.selectByPrimaryKey(actualFirstNo);
            if (mwcaf1 == null) {
                throw new MarkerMetException(500, "未查询到相应的毛重数据！！");
            }

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

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

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

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

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

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

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

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

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

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

        }
    }

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

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

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

                        MeterWorkCarActual meterWorkCarActual = new MeterWorkCarActual();
                        meterWorkCarActual.setActualNo(actual.getActualNo());
                        meterWorkCarActual.setTbFlage("1");
                        meterWorkCarActualMapper.updateByPrimaryKeySelective(meterWorkCarActual);
                    } else {
                        meterToLimsOldMapper.insertSelective(old);

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


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

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

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

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

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

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

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

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

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

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

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

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

        return carActual;
    }

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

                //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                MeterWorkCarActual mwca1 = meterWorkCarActualMapper.manualAddPre(actualFirstNo, actualSecondNo, predictionNo);
                BeanUtils.copyProperties(mwca1, actualRow);

                //更新预报数据
                PreTrackScale scale = new PreTrackScale();
                scale.setPredictionNo(predictionNo);
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("手动录入");
                scale.setUploadFlag("1");
                scale.setValueFlag("2");
                scale.setMeasureStation(actualRow.getNetSpot3No());
                scale.setMeasureStationName(actualRow.getNetSpot3Name());
                preTrackScaleMapper.updateByPrimaryKeySelective(scale);
            }else {
                //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                MeterWorkCarActual mwca1 = meterWorkCarActualMapper.manualAdd(actualFirstNo, actualSecondNo);
                BeanUtils.copyProperties(mwca1, actualRow);
            }
//            String no = meterWorkCarActualMapper.getNewID(sdf.format(new Date()));
//            actualRow.setActualNo(sdf.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            actualRow.setActualNo(no);
            actualRow.setNetTime(model.getNetTime());
            actualRow.setNetMode(DbConstants.TYPE);
            actualRow.setNote("手动录入");
            actualRow.setUploadFlag("1");
            actualRow.setIsPreScale("1");
            actualRow.setValueFlag("1");
            actualRow.setGrossMode("3");
            actualRow.setTareMode("3");
            actualRow.setCreateManName(model.getGrossManName());
            actualRow.setCreateTime(new Date());
            actualRow.setPredictionNo(predictionNo);
            actualRow.setNetWeight(mwcaf1.getMeterWeight().subtract(mwcaf2.getMeterWeight()));
            if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                actualRow.setNetManNo(actualRow.getGrossManNo());
                actualRow.setNetManName(actualRow.getGrossManName());
                actualRow.setNetTime(actualRow.getGrossTime());
                actualRow.setCreateTime(actualRow.getGrossTime());
                actualRow.setCreateManName(actualRow.getGrossManName());
            }else {
                actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                actualRow.setNetManNo(actualRow.getTareManNo());
                actualRow.setNetManName(actualRow.getTareManName());
                actualRow.setNetTime(actualRow.getTareTime());
                actualRow.setCreateTime(actualRow.getTareTime());
                actualRow.setCreateManName(actualRow.getTareManName());
            }
            meterWorkCarActualMapper.insertSelective(actualRow);
            rm.setSucceed();
            rm.setMessage("操作成功！！！");
            rm.setCode("200");
            return rm;
        }catch(Exception e){
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！！");
            return rm;
        }
    }

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

    @Override
    public List<HashMap> countAllSpotSate(HashMap params) {
        List<HashMap> map = new ArrayList<>();

        if(params.get("netSpot3NoStr") != null) {
            String[] spotNoStr = params.get("netSpot3NoStr").toString().split(",");
            params.put("spotNoStr", spotNoStr);
        }

        //根据时间查询所有秤点的结净已匹配、结净未匹配、结净已上传、结净未上传、已作废数据
        List<MeterWorkCarActual> carActual = meterWorkCarActualMapper.countAllState(params);
        //净重数据与核查表左连接
        List<FirstActualImgData> ActualImgDatamodels = meterWorkCheckFirstActualMapper.carActualImgDataList(params);
        List<MeterWorkCarActualFirst> carFirst = meterWorkCarActualFirstMapper.countAllState(params);
        //一次数据与核查表左连接
        List<FirstActualImgData> firstImgDatamodels = meterWorkCheckFirstActualMapper.carFirstImgDataList(params);
        List<MeterWorkScheduledHelp> carHelp = meterWorkScheduledHelpMapper.countAllState(params);
        List<HashMap> carFirstLog = meterBaseOperationLogMapper.countAllFirstState(params);
        List<HashMap> carActualLog = meterBaseOperationLogMapper.countAllActualState(params);
        List<MeterMonitorNote> carMonitorLog = meterMonitorNoteMapper.countAllState(params);
        List<MeterMonitorNote> countResetCarLog = meterMonitorNoteMapper.countResetCar(params);
        //.map(MeterWorkCarActual::getCarNo).distinct().collect(Collectors.toList());
//        List<String> spotList = carActual.stream().map(MeterWorkCarActual::getNetSpot3Name).distinct().collect(Collectors.toList());

        List<MeterBaseSpotInfo> spotModel = meterBaseSpotInfoMapper.spotInfoByList(params);
//        List<MeterBaseSpotInfo> spotList = spotModel.stream().collect(
//                Collectors. collectingAndThen(
//                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getBaseSpotNo() + ";" + o.getBaseSpotName()))), ArrayList::new)
//        );
        for(MeterBaseSpotInfo spot: spotModel) {
            long countActual = 0L;
            long countIsPrescale = 0L;
            long countIsNotPrescale = 0L;
            long countUpload = 0L;
            long countUnUpload = 0L;
            long countActualValid = 0L;
            long countActualCheck = 0L;
            if(carActual != null && carActual.size() >= 1) {
                //结净总数
                countActual = carActual.stream().filter(model -> model.getNetSpot3No().equals(spot.getBaseSpotNo())
                        && model.getNetSpot3Name().equals(spot.getBaseSpotName())
                        && !model.getValueFlag().equals("0")).count();
                //结净已匹配
                countIsPrescale = carActual.stream().filter(model -> model.getNetSpot3No().equals(spot.getBaseSpotNo())
                        && model.getNetSpot3Name().equals(spot.getBaseSpotName())
                        && !model.getValueFlag().equals("0")
                        && model.getIsPreScale().equals("1")).count();
                //结净未匹配
                countIsNotPrescale = carActual.stream().filter(model -> model.getNetSpot3No().equals(spot.getBaseSpotNo())
                        && model.getNetSpot3Name().equals(spot.getBaseSpotName())
                        && !model.getValueFlag().equals("0")
                        && ( StringUtils.isEmpty(model.getIsPreScale()) || model.getIsPreScale().equals("0") )).count();
                //结净已上传
                countUpload = carActual.stream().filter(model -> model.getNetSpot3No().equals(spot.getBaseSpotNo())
                        && model.getNetSpot3Name().equals(spot.getBaseSpotName())
                        && !model.getValueFlag().equals("0")
                        && model.getIsPreScale().equals("1")
                        && model.getValueFlag().equals("1")
                        && model.getUploadFlag().equals("2")).count();
                //结净未上传
                countUnUpload = carActual.stream().filter(model -> model.getNetSpot3No().equals(spot.getBaseSpotNo())
                        && model.getNetSpot3Name().equals(spot.getBaseSpotName())
                        && !model.getUploadFlag().equals("2")
                        && !model.getIsPreScale().equals("0")).count();
                //结净数据已作废
                countActualValid = carActual.stream().filter(model -> model.getNetSpot3No().equals(spot.getBaseSpotNo())
                        && model.getNetSpot3Name().equals(spot.getBaseSpotName())
                        && model.getValueFlag().equals("0")).count();
                //结净数据已核查
                countActualCheck = carActual.stream().filter(model -> model.getNetSpot3No().equals(spot.getBaseSpotNo())
                        && model.getNetSpot3Name().equals(spot.getBaseSpotName())).count() - ActualImgDatamodels.stream().filter(model -> model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())).count();
            }

            long countGross = 0L;
            long countGrossNoClean = 0L;
            long countTare = 0L;
            long countTareNoClean = 0L;
            long countTermTare = 0L;
            long countNullWeightType = 0L;
            long countFirst = 0L;
            long countFirstValid = 0L;
            long countFirstCheck = 0L;
            if(carFirst != null && carFirst.size() >= 1) {
                //一次总数
                countFirst = carFirst.stream().filter(model -> model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && !model.getValueFlag().equals("0")).count();
                //一次毛重
                countGross = carFirst.stream().filter(model -> StringUtils.isNotEmpty(model.getWeightType()) && model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && !model.getValueFlag().equals("0")
                        && model.getWeightType().equals("0")).count();
                //一次未结净净毛重
                countGrossNoClean = carFirst.stream().filter(model -> StringUtils.isNotEmpty(model.getWeightType()) && model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getValueFlag().equals("1")
                        && model.getWeightType().equals("0")).count();
                //一次皮重
                countTare = carFirst.stream().filter(model -> StringUtils.isNotEmpty(model.getWeightType()) && model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && !model.getValueFlag().equals("0")
                        && model.getWeightType().equals("1")).count();
                //一次未结净皮重
                countTareNoClean = carFirst.stream().filter(model -> StringUtils.isNotEmpty(model.getWeightType()) && model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getValueFlag().equals("1")
                        && model.getWeightType().equals("1")).count();
                //一次期限皮重
                countTermTare = carFirst.stream().filter(model -> StringUtils.isNotEmpty(model.getWeightType()) && model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && !model.getValueFlag().equals("0")
                        && model.getWeightType().equals("2")).count();
                //一次无重量类型
                countNullWeightType = carFirst.stream().filter(model ->  model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && !model.getValueFlag().equals("0")
                        && StringUtil.isEmpty(model.getWeightType())).count();
                //仅一次作废
                countFirstValid = carFirst.stream().filter(model -> model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getValueFlag().equals("0")).count();
                //一次数据已核查
                countFirstCheck = carFirst.stream().filter(model -> model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())).count() - firstImgDatamodels.stream().filter(model -> model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())).count();
            }

            long countCarHelp = 0L;
            long countCarHelpIsNotValue = 0L;
            long countCarHelpIsValue = 0L;
            long countCarHelpRfid = 0L;
            long countCarHelpScanningCode = 0L;
            long countCarHelpNormal = 0L;
            long countCarHelpVehicleReset = 0L;
            if(carHelp != null && carHelp.size() >= 1) {
                //报警总数
                countCarHelp = carHelp.stream().filter(model -> model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())).count();
                //报警未处理
                countCarHelpIsNotValue = carHelp.stream().filter(model -> model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getValueFlag().equals("0")).count();
                //报警已处理
                countCarHelpIsValue = carHelp.stream().filter(model -> model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getValueFlag().equals("1")).count();
                //报警Rfid
                countCarHelpRfid = carHelp.stream().filter(model -> model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getHelpContent().equals("2")).count();
                //报警扫码
                countCarHelpScanningCode = carHelp.stream().filter(model -> model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getHelpContent().equals("3")).count();
                //报警正常
                countCarHelpNormal = carHelp.stream().filter(model -> model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getHelpContent().equals("1")).count();
                //报警车号重置
                countCarHelpVehicleReset = carHelp.stream().filter(model -> model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getHelpContent().equals("4")).count();
            }

            long countLogFirstIsPreScale = 0L;
            long countLogFirstIsNotPreScale = 0L;
            long countLogFirstUpdateCar = 0L;
            long countLogFirstValid = 0L;
            long countLogFirstRecover = 0L;
            if(carFirstLog != null && carFirstLog.size() >= 1) {
                //日志一次处理匹配委托
                countLogFirstIsPreScale = carFirstLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("BASE_SPOT_NO") != null
                        && model.get("BASE_SPOT_NAME") != null
                        && model.get("BASE_SPOT_NO").equals(spot.getBaseSpotNo())
                        && model.get("BASE_SPOT_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_FUNCTION").toString().contains("匹配委托")).count();
                countLogFirstIsNotPreScale = carFirstLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("BASE_SPOT_NO") != null
                        && model.get("BASE_SPOT_NAME") != null
                        && model.get("BASE_SPOT_NO").equals(spot.getBaseSpotNo())
                        && model.get("BASE_SPOT_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_FUNCTION").toString().contains("撤销委托")).count();
                countLogFirstUpdateCar = carFirstLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("BASE_SPOT_NO") != null
                        && model.get("BASE_SPOT_NAME") != null
                        && model.get("BASE_SPOT_NO").equals(spot.getBaseSpotNo())
                        && model.get("BASE_SPOT_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_FUNCTION").toString().contains("车号修改")).count();
                countLogFirstValid = carFirstLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("BASE_SPOT_NO") != null
                        && model.get("BASE_SPOT_NAME") != null
                        && model.get("BASE_SPOT_NO").equals(spot.getBaseSpotNo())
                        && model.get("BASE_SPOT_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_FUNCTION").toString().contains("作废")).count();
                countLogFirstRecover = carFirstLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("BASE_SPOT_NO") != null
                        && model.get("BASE_SPOT_NAME") != null
                        && model.get("BASE_SPOT_NO").equals(spot.getBaseSpotNo())
                        && model.get("BASE_SPOT_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_FUNCTION").toString().contains("启用")).count();
            }

            long countLogActualIsPreScale = 0L;
            long countLogActualIsNotPreScale = 0L;
            long countLogActualDecompose = 0L;
            long countLogActualValid = 0L;
            long countLogFirstClean = 0L;
            long countLogFirstDoubleEntrust = 0L;
            long countLogFirstMixedLoading = 0L;
            long countLogFirstMixedDischarge = 0L;
            long countLogActualCancelDoubleEntrust = 0L;
            long countLogActualCancelMixedLoading = 0L;
            long countLogActualCancelMixedDischarge = 0L;
            if(carActualLog != null && carActualLog.size() >= 1) {
                countLogFirstClean = carActualLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("NET_SPOT3_NO") != null
                        && model.get("NET_SPOT3_NAME") != null
                        && model.get("NET_SPOT3_NO").equals(spot.getBaseSpotNo())
                        && model.get("NET_SPOT3_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_PAGE").equals("web端-汽车衡一次数据维护")
                        && model.get("OPERATION_FUNCTION").equals("毛皮结净")).count();
                countLogFirstDoubleEntrust = carActualLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("NET_SPOT3_NO") != null
                        && model.get("NET_SPOT3_NAME") != null
                        && model.get("NET_SPOT3_NO").equals(spot.getBaseSpotNo())
                        && model.get("NET_SPOT3_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_PAGE").equals("web端-汽车衡一次数据维护")
                        && model.get("OPERATION_FUNCTION").equals("双委托结净")).count();
                countLogFirstMixedLoading = carActualLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("NET_SPOT3_NO") != null
                        && model.get("NET_SPOT3_NAME") != null
                        && model.get("NET_SPOT3_NO").equals(spot.getBaseSpotNo())
                        && model.get("NET_SPOT3_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_PAGE").equals("web端-汽车衡一次数据维护")
                        && model.get("OPERATION_FUNCTION").equals("混装委托结净")).count();
                countLogFirstMixedDischarge = carActualLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("NET_SPOT3_NO") != null
                        && model.get("NET_SPOT3_NAME") != null
                        && model.get("NET_SPOT3_NO").equals(spot.getBaseSpotNo())
                        && model.get("NET_SPOT3_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_PAGE").equals("web端-汽车衡一次数据维护")
                        && model.get("OPERATION_FUNCTION").equals("混卸委托结净")).count();
                countLogActualCancelDoubleEntrust = carActualLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("NET_SPOT3_NO") != null
                        && model.get("NET_SPOT3_NAME") != null
                        && model.get("NET_SPOT3_NO").equals(spot.getBaseSpotNo())
                        && model.get("NET_SPOT3_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_PAGE").equals("web端-汽车衡净重数据维护")
                        && model.get("OPERATION_FUNCTION").equals("取消双委托结净")).count();
                countLogActualCancelMixedLoading = carActualLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("NET_SPOT3_NO") != null
                        && model.get("NET_SPOT3_NAME") != null
                        && model.get("NET_SPOT3_NO").equals(spot.getBaseSpotNo())
                        && model.get("NET_SPOT3_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_PAGE").equals("web端-汽车衡净重数据维护")
                        && model.get("OPERATION_FUNCTION").equals("取消混装委托结净")).count();
                countLogActualCancelMixedDischarge = carActualLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("NET_SPOT3_NO") != null
                        && model.get("NET_SPOT3_NAME") != null
                        && model.get("NET_SPOT3_NO").equals(spot.getBaseSpotNo())
                        && model.get("NET_SPOT3_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_PAGE").equals("web端-汽车衡净重数据维护")
                        && model.get("OPERATION_FUNCTION").equals("取消混卸委托结净")).count();
                countLogActualIsPreScale = carActualLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("NET_SPOT3_NO") != null
                        && model.get("NET_SPOT3_NAME") != null
                        && model.get("NET_SPOT3_NO").equals(spot.getBaseSpotNo())
                        && model.get("NET_SPOT3_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_PAGE").equals("web端-汽车衡净重数据维护")
                        && model.get("OPERATION_FUNCTION").toString().contains("匹配委托")).count();
                countLogActualIsNotPreScale = carActualLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("NET_SPOT3_NO") != null
                        && model.get("NET_SPOT3_NAME") != null
                        && model.get("NET_SPOT3_NO").equals(spot.getBaseSpotNo())
                        && model.get("NET_SPOT3_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_PAGE").equals("web端-汽车衡净重数据维护")
                        && model.get("OPERATION_FUNCTION").toString().contains("撤销委托")).count();
                countLogActualDecompose = carActualLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("NET_SPOT3_NO") != null
                        && model.get("NET_SPOT3_NAME") != null
                        && model.get("NET_SPOT3_NO").equals(spot.getBaseSpotNo())
                        && model.get("NET_SPOT3_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_PAGE").equals("web端-汽车衡净重数据维护")
                        && model.get("OPERATION_FUNCTION").equals("毛皮分解")).count();
                countLogActualValid = carActualLog.stream().filter(model -> model.get("OPERATION_FUNCTION") != null
                        && model.get("NET_SPOT3_NO") != null
                        && model.get("NET_SPOT3_NAME") != null
                        && model.get("NET_SPOT3_NO").equals(spot.getBaseSpotNo())
                        && model.get("NET_SPOT3_NAME").equals(spot.getBaseSpotName())
                        && model.get("OPERATION_PAGE").equals("web端-汽车衡净重数据维护")
                        && model.get("OPERATION_FUNCTION").equals("结净废除")).count();
            }

            long countMonitorLogHisTare = 0L;
            long countMonitorLogGrossTare = 0L;
            long countMonitorLogActual = 0L;
            long countMonitorLogInfraredContrast = 0L;
            long countMonitorLogZero = 0L;
            long countMonitorLogTime = 0L;
            if(carMonitorLog != null && carMonitorLog.size() >= 1) {
                countMonitorLogHisTare = carMonitorLog.stream().filter(model -> model.getBaseSpotNo() != null
                        && model.getBaseSpotName() != null
                        && model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getOperationContent().startsWith("勾选")
                        && model.getOperationContent().contains("历史皮重")).count();
                countMonitorLogGrossTare = carMonitorLog.stream().filter(model -> model.getBaseSpotNo() != null
                        && model.getBaseSpotName() != null
                        && model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getOperationContent().startsWith("勾选")
                        && model.getOperationContent().contains("毛皮超差")).count();
                countMonitorLogActual = carMonitorLog.stream().filter(model -> model.getBaseSpotNo() != null
                        && model.getBaseSpotName() != null
                        && model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getOperationContent().startsWith("勾选")
                        && model.getOperationContent().contains("异地计量")).count();
                countMonitorLogInfraredContrast = carMonitorLog.stream().filter(model -> model.getBaseSpotNo() != null
                        && model.getBaseSpotName() != null
                        && model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getOperationContent().startsWith("勾选")
                        && model.getOperationContent().contains("对射")).count();
                countMonitorLogZero = carMonitorLog.stream().filter(model -> model.getBaseSpotNo() != null
                        && model.getBaseSpotName() != null
                        && model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getOperationContent().startsWith("勾选")
                        && model.getOperationContent().contains("零点告警")).count();
                countMonitorLogTime = carMonitorLog.stream().filter(model -> model.getBaseSpotNo() != null
                        && model.getBaseSpotName() != null
                        && model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && model.getOperationContent().startsWith("勾选")
                        && model.getOperationContent().contains("时间间隔")).count();
            }

            long countResetCar = 0L;
            if(countResetCarLog != null && countResetCarLog.size() >= 1) {
                countResetCar = countResetCarLog.stream().filter(model -> model.getBaseSpotNo() != null
                        && model.getBaseSpotName() != null
                        && model.getBaseSpotNo().equals(spot.getBaseSpotNo())
                        && model.getBaseSpotName().equals(spot.getBaseSpotName())
                        && StringUtils.isNotEmpty(model.getCarNo())).count();
            }

            HashMap<String, Object> obj = new HashMap<>();
            //根据时间查询所有秤点的结净已匹配、结净未匹配、结净已上传、结净未上传、结净已作废
            obj.put("spotNo", spot.getBaseSpotNo());
            obj.put("spotName", spot.getBaseSpotName());
            obj.put("countActual", countActual);
            obj.put("countIsPrescale", countIsPrescale);
            obj.put("countIsNotPrescale", countIsNotPrescale);
            obj.put("countUpload", countUpload);
            obj.put("countUnUpload", countUnUpload);
            obj.put("countActualValid", countActualValid);
            obj.put("countActualCheck", countActualCheck);
            //根据时间查询所有秤点的一次毛重、未洁净毛重、一次皮重、未结净皮重、一次期限皮、一次有效、一次作废
            obj.put("countGross", countGross);
            obj.put("countGrossNoClean", countGrossNoClean);
            obj.put("countTare", countTare);
            obj.put("countTareNoClean", countTareNoClean);
            obj.put("countTermTare", countTermTare);
            obj.put("countNullWeightType", countNullWeightType);
            obj.put("countFirst", countFirst);
            obj.put("countFirstValid", countFirstValid);
            obj.put("countFirstCheck", countFirstCheck);
            //根据时间查询所有秤点的报警总数、报警未处理、报警已处理
            obj.put("countCarHelp", countCarHelp);
            obj.put("countCarHelpIsNotValue", countCarHelpIsNotValue);
            obj.put("countCarHelpIsValue", countCarHelpIsValue);
            obj.put("countCarHelpRfid", countCarHelpRfid);
            obj.put("countCarHelpScanningCode", countCarHelpScanningCode);
            obj.put("countCarHelpNormal", countCarHelpNormal);
            obj.put("countCarHelpVehicleReset", countCarHelpVehicleReset);
            //根据时间查询所有秤点的一次匹配委托、撤销委托、车号修改、作废、启用、毛皮结净、双委托结净、混装结净、混卸结净
            obj.put("countLogFirstIsPreScale", countLogFirstIsPreScale);
            obj.put("countLogFirstIsNotPreScale", countLogFirstIsNotPreScale);
            obj.put("countLogFirstUpdateCar", countLogFirstUpdateCar);
            obj.put("countLogFirstValid", countLogFirstValid);
            obj.put("countLogFirstRecover", countLogFirstRecover);
            obj.put("countLogFirstClean", countLogFirstClean);
            obj.put("countLogFirstDoubleEntrust", countLogFirstDoubleEntrust);
            obj.put("countLogFirstMixedLoading", countLogFirstMixedLoading);
            obj.put("countLogFirstMixedDischarge", countLogFirstMixedDischarge);
            //根据时间查询所有秤点的结净匹配委托、撤销委托、毛皮分解、结净废除、取消双委托、取消混装、取消混卸
            obj.put("countLogActualIsPreScale", countLogActualIsPreScale);
            obj.put("countLogActualIsNotPreScale", countLogActualIsNotPreScale);
            obj.put("countLogActualDecompose", countLogActualDecompose);
            obj.put("countLogActualValid", countLogActualValid);
            obj.put("countLogActualCancelDoubleEntrust", countLogActualCancelDoubleEntrust);
            obj.put("countLogActualCancelMixedLoading", countLogActualCancelMixedLoading);
            obj.put("countLogActualCancelMixedDischarge", countLogActualCancelMixedDischarge);
            //根据时间查询所有秤点的不验证历史皮重、不验证异地计量、不验证零点告警、不验证时间间隔、不验证对射、不验证毛皮超差
            obj.put("countMonitorLogHisTare", countMonitorLogHisTare);
            obj.put("countMonitorLogGrossTare", countMonitorLogGrossTare);
            obj.put("countMonitorLogActual", countMonitorLogActual);
            obj.put("countMonitorLogInfraredContrast", countMonitorLogInfraredContrast);
            obj.put("countMonitorLogZero", countMonitorLogZero);
            obj.put("countMonitorLogTime", countMonitorLogTime);
            //监控程序重置车号
            obj.put("countResetCar", countResetCar);
            map.add(obj);
        }
        return map;
    }

    @Override
    public PageList<HashMap> netWeightAndTheoryAmount(HashMap params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        List<HashMap> rows = meterWorkCarActualMapper.netWeightAndTheoryAmount(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public List<MeterWorkCarActualTHJCompare> findCommonAndInnerDouble(HashMap params) {
        List<MeterWorkCarActualTHJCompare> models = new ArrayList<>();
        //查询符合内倒双委托车辆在铁合金秤点用普通委托信息过磅的结净信息倒序
        List<MeterWorkCarActual> commonModel = meterWorkCarActualMapper.findTHJNetWeight(params);
        if(commonModel.size() >= 1) {
            for(MeterWorkCarActual model: commonModel) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Calendar beforeTime = Calendar.getInstance();
                beforeTime.setTime(model.getNetTime());     //设置时间
                beforeTime.add(Calendar.HOUR, 12);// 12小时之后的时间
                Date afterD = beforeTime.getTime();
                String endTime = sdf.format(afterD);
                HashMap<String, Object> map = new HashMap<>();
                map.put("carNo", model.getCarNo());
                map.put("startTime", sdf.format(model.getNetTime()));
                map.put("endTime", endTime);
                List<MeterWorkCarActual> modelTemp = meterWorkCarActualMapper.findFiveNetWeight(map);
                if(modelTemp.size() >= 1) {
                    for(int i = 0; i < modelTemp.size(); i++) {
                        if(i == 0) {
                            MeterWorkCarActualTHJCompare modelTHJ01 = new MeterWorkCarActualTHJCompare();
                            BeanUtils.copyProperties(model, modelTHJ01);
                            modelTHJ01.setGrossWeightSub(modelTemp.get(i).getGrossWeight().subtract(model.getGrossWeight()));
                            modelTHJ01.setTareWeightSub(modelTemp.get(i).getTareWeight().subtract(model.getTareWeight()));
                            modelTHJ01.setNetWeightSub(modelTemp.get(i).getNetWeight().subtract(model.getNetWeight()));
                            models.add(modelTHJ01);
                        }
                        MeterWorkCarActualTHJCompare modelTHJ02 = new MeterWorkCarActualTHJCompare();
                        BeanUtils.copyProperties(modelTemp.get(i), modelTHJ02);
                        models.add(modelTHJ02);
                    }
                }
            }
        }
        return models;
    }

    @Override
    public PageList<MeterWorkCarActual> smallAndNegativeNetWeightRatio(HashMap<String, Object> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        List<MeterWorkCarActual> rows = meterWorkCarActualMapper.smallAndNegativeNetWeightRatio(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public RESTfulResult updateNote(List<MeterWorkCarActual> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            UserPayload userPayload = UserPayload.getCurrUser();
            String userId = userPayload.getUserCode();
            String userName = userPayload.getUserName();
            int count = 0;
            if(models.size() >= 1) {
                for(MeterWorkCarActual model: models) {
                    model.setUpdateManNo(userId);
                    model.setUpdateManName(userName);
                    model.setUpdateTime(new Date());
                    meterWorkCarActualMapper.updateByPrimaryKeySelective(model);
                    count ++;
                }
            }else {
                rm.setMessage("未选择一次数据，请重新选择");
            }
            rm.setSucceed(true);
            rm.setData(models);
            rm.setMessage("操作成功，总共" + count + "条数据修改成功!!");
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常" + e.getMessage());
        }
        return rm;
    }

    @Override
    public PageList<HashMap> getNetFilterCheck(HashMap params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        List<HashMap> meterWorkCarActualFirsts = meterWorkCarActualMapper.getNetFilterCheck(params);
        PageList pageInfo = new PageList(meterWorkCarActualFirsts);
        return pageInfo;

    }

    @Override
    public PageList<HashMap> getNetFilterCheckWeightReversal(HashMap params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        List<HashMap> meterWorkCarActualFirsts = meterWorkCarActualMapper.getNetFilterCheckWeightReversal(params);
        PageList pageInfo = new PageList(meterWorkCarActualFirsts);
        return pageInfo;
    }

    @Override
    public int batchCommonRemove() {
        // 1. 查询要废除的结净数据
        List<MeterWorkCarActual> carActuals = meterWorkCarActualMapper.findCommonRemove();
        for(MeterWorkCarActual carActual : carActuals) {
            // 2. 将结净数据作废
            HashMap<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("note","管理员处理");
            objectObjectHashMap.put("actualNo",carActual.getActualNo());
            commonRemove(objectObjectHashMap);
        }
        return 1;
}
}
