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

import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.baseinfo.meterbasehistaredetail.model.MeterBaseHisTareDetail;
import com.steerinfo.baseinfo.meterbasematterunitmixedloadunloadtime.mapper.MeterBaseMatterUnitMixedLoadUnloadTimeMapper;
import com.steerinfo.baseinfo.meterbasematterunitmixedloadunloadtime.model.MeterBaseMatterUnitMixedLoadUnloadTime;
import com.steerinfo.baseinfo.meterbaseoperationlog.mapper.MeterBaseOperationLogMapper;
import com.steerinfo.baseinfo.meterbaseoperationlog.model.MeterBaseOperationLog;
import com.steerinfo.baseinfo.meterbaserailwayaiweight.mapper.MeterBaseRailwayAiweightMapper;
import com.steerinfo.baseinfo.meterbaserailwayaiweight.model.MeterBaseRailwayAiweight;
import com.steerinfo.baseinfo.meterbaserailwaycarriageweight.mapper.MeterBaseRailwayCarriageWeightMapper;
import com.steerinfo.baseinfo.meterbaserailwaycarriageweight.model.MeterBaseRailwayCarriageWeight;
import com.steerinfo.baseinfo.meterbaserailwayhistaredata.mapper.MeterBaseRailwayHisTareDataMapper;
import com.steerinfo.baseinfo.meterbaserailwayhistaredata.model.MeterBaseRailwayHisTareData;
import com.steerinfo.baseinfo.meterbaserailwayhistaredetail.mapper.MeterBaseRailwayHisTareDetailMapper;
import com.steerinfo.baseinfo.meterbaserailwayhistaredetail.model.MeterBaseRailwayHisTareDetail;
import com.steerinfo.baseinfo.meterbaserailwayweight.mapper.MeterBaseRailwayWeightMapper;
import com.steerinfo.baseinfo.meterbaserailwayweight.model.MeterBaseRailwayWeight;
import com.steerinfo.baseinfo.meterbasevideoinfo.mapper.MeterBaseVideoinfoMapper;
import com.steerinfo.baseinfo.meterbasevideoinfo.model.MeterBaseVideoinfo;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.metermonitor.metermonitornote.model.MeterMonitorNote;
import com.steerinfo.meterwork.constants.DbConstants;
import com.steerinfo.meterwork.dto.CommonCombination;
import com.steerinfo.meterwork.dto.StaffPerformanceAnalysis;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.meterwork.meterworkcaractualfirst.service.impl.MeterWorkCarActualFirstServiceImpl;
import com.steerinfo.meterwork.meterworkimage.mapper.MeterWorkImageMapper;
import com.steerinfo.meterwork.meterworkimage.model.MeterWorkImage;
import com.steerinfo.meterwork.meterworkrailwayactfirst.mapper.MeterWorkRailwayActFirstMapper;
import com.steerinfo.meterwork.meterworkrailwayactfirst.model.MeterWorkRailwayActFirst;
import com.steerinfo.meterwork.meterworkrailwayactfirst.service.IMeterWorkRailwayActFirstService;
import com.steerinfo.meterwork.meterworkrailwayactual.mapper.MeterWorkRailwayActualMapper;
import com.steerinfo.meterwork.meterworkrailwayactual.model.MeterWorkRailwayActual;
import com.steerinfo.meterwork.meterworkrailwaymotorialreceived.model.MeterWorkRailwayMotorialReceived;
import com.steerinfo.meterwork.meterworkrailwayreceived.mapper.MeterWorkRailwayReceivedMapper;
import com.steerinfo.meterwork.meterworkrailwayreceived.model.MeterWorkRailwayReceived;
import com.steerinfo.meterwork.meterworkrailwayrecmain.mapper.MeterWorkRailwayRecMainMapper;
import com.steerinfo.meterwork.meterworkrailwayrecmain.model.MeterWorkRailwayRecMain;
import com.steerinfo.meterwork.meterworkrailwayscheduledhelp.mapper.MeterWorkRailwayScheduledHelpMapper;
import com.steerinfo.meterwork.meterworkrailwayscheduledhelp.model.MeterWorkRailwayScheduledHelp;
import com.steerinfo.meterwork.meterworkrailwayscheduledhelp.service.IMeterWorkRailwayScheduledHelpService;
import com.steerinfo.pretrack.prerailwayscale.mapper.PreRailwayScaleMapper;
import com.steerinfo.pretrack.prerailwayscale.model.PreRailwayScale;
import com.steerinfo.util.FieldsCollector;
import com.steerinfo.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

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

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

    @Resource
    private MeterWorkRailwayActFirstMapper meterWorkRailwayActFirstMapper;

    @Resource
    MeterBaseMatterUnitMixedLoadUnloadTimeMapper meterBaseMatterUnitMixedLoadUnloadTimeMapper;
    @Resource
    private MeterWorkRailwayRecMainMapper meterWorkRailwayRecMainMapper;

    @Resource
    private IMeterWorkRailwayScheduledHelpService meterWorkRailwayScheduledHelpService;

    @Resource
    private MeterBaseRailwayWeightMapper meterBaseRailwayWeightMapper;

    @Resource
    private MeterWorkRailwayScheduledHelpMapper meterWorkRailwayScheduledHelpMapper;

    @Resource
    private ComBaseInfoMapper comBaseInfoMapper;

    @Resource
    private MeterBaseRailwayCarriageWeightMapper meterBaseRailwayCarriageWeightMapper;

    @Resource
    private MeterBaseRailwayAiweightMapper meterBaseRailwayAiweightMapper;

    @Resource
    private PreRailwayScaleMapper preRailwayScaleMapper;

    @Resource
    private MeterWorkRailwayReceivedMapper meterWorkRailwayReceivedMapper;

    @Resource
    private MeterWorkRailwayActualMapper meterWorkRailwayActualMapper;

    @Resource
    private MeterBaseRailwayHisTareDataMapper meterBaseRailwayHisTareDataMapper;

    @Resource
    private MeterBaseVideoinfoMapper meterBaseVideoinfoMapper;

    @Resource
    private MeterWorkImageMapper meterWorkImageMapper;

    @Resource
    private MeterBaseOperationLogMapper meterBaseOperationLogMapper;

    @Resource
    private MeterBaseRailwayHisTareDetailMapper meterBaseRailwayHisTareDetailMapper;

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


    @Override
    protected IBaseMapper<MeterWorkRailwayActFirst, String> getMapper() {
        return meterWorkRailwayActFirstMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult checkandadd(MeterWorkRailwayActFirst model) {
        RESTfulResult rm = new RESTfulResult();
        log.info("请求到达, predictionNo={}, trainNo={}, time={}",
                model.getPredictionNo(), model.getRailwayNo(), model.getCreateTime());
        rm.setFailed();
        try {
            List<MeterWorkRailwayActFirst> firsts = new ArrayList<>();
            HashMap<String, Object> map = new HashMap<>();
            check1(model);
            model.setDataSource("1");
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            String no = meterWorkRailwayActFirstMapper.getNewID(sdf.format(new Date()));
//            model.setActualFirstNo(sdf.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            log.info("生成单号: {}", no);
            model.setActualFirstNo(no);
            model.setCreateTime(new Date());
            meterWorkRailwayActFirstMapper.insertSelective(model);
            log.info("插入记录成功, 主键={}", model.getId());
            if(model.getPredictionNo() != null && StringUtils.isNotEmpty(model.getPredictionNo())) {
                MeterWorkRailwayActFirst modelTemp = meterWorkRailwayActFirstMapper.selectAndMatch(model.getActualFirstNo(), model.getPredictionNo());
                BeanUtils.copyProperties(modelTemp, model);
                model.setPredictionType("1");
                model.setIsPreSacale("1");

                PreRailwayScale scale = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
                scale.setValueFlag("4");
                scale.setUploadFlag("1");
                scale.setMeasureStation(model.getBaseSpotNo());
                scale.setMeasureStationName(model.getBaseSpotName());
                scale.setPredictionType("1");
                scale.setUpdateManNo(model.getCreateManNo());
                scale.setUpdateManName(model.getCreateManName());
                scale.setUpdateTime(new Date());
                preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
            }
            int i = meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model);
            if (i >= 1) {
                firsts.add(model);
            }
            map.put("firsts", firsts);
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(map);
        } catch (Exception e) {
            log.error("操作异常，actualFirstNo={}, predictionNo={}",
                    model.getActualFirstNo(), model.getPredictionNo(), e);
            rm.setMessage("操作异常，请稍后重试");
            // 必须抛出去让事务回滚
            throw new RuntimeException(e);
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult dynamicAdd(MeterWorkRailwayActFirst model) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            String userId, userName;
            try{
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch(Exception e) {
                userId = "admin";
                userName = "admins";
            }

            ///判断这个火车号是不是存在未结净的一次皮重数据，存在的话就把传的一次数据当作作废的存进去，不存在就直接存进去
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("weightType", "1");
            obj.put("railwayNo", model.getRailwayNo());
            obj.put("valueFlag", "1");
            List<MeterWorkRailwayActFirst> modelList = meterWorkRailwayActFirstMapper.selectByParameters(obj);
            if(modelList.size() >= 1) {
                for (MeterWorkRailwayActFirst model_i:
                     modelList) {
                    model_i.setValueFlag("0");
                    model_i.setNote("一次新增，作废皮重");
                    model_i.setUpdateManNo(userId);
                    model_i.setUpdateManName(userName);
                    model_i.setUpdateTime(new Date());
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model_i);
                }
            }
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            model.setActualFirstNo(sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(sdf.format(new Date())));
            String no = PublicMethod.IdCreate(3);
            model.setActualFirstNo(no);
            model.setCreateManNo(userId);
            model.setCreateManName(userName);
            model.setCreateTime(new Date());
            model.setValueFlag("1");
            model.setCheckFlag("0");
            model.setMeterMode("3");
            model.setDataSource("1");
            model.setIsPreSacale("0");
            model.setHc15("0");
            model.setNote("一次新增");
            meterWorkRailwayActFirstMapper.insertSelective(model);
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(model.getActualFirstNo());
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！>>>" + e.getMessage());
        }
        return rm;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int checkandupdate(MeterWorkRailwayActFirst model) {
        try {
            check(model);
            if (model.getValueFlag().equals(DbConstants.NET)) {
                throw new MarkerMetException(500, "已经结净的数据无法修改！！");
            }
            int num = meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model);

            PreRailwayScale scale = firstToScale(model);
            if (scale != null && scale.getPredictionNo() != null) {
                scale.setUpdateTime(new Date());
                scale.setUpdateManName(model.getUpdateManName());
                scale.setUpdateManNo(model.getUpdateManNo());
                scale.setMeasureStation(model.getBaseSpotNo());
                scale.setMeasureStationName(model.getBaseSpotName());
                preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
            }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult updateList(List<MeterWorkRailwayActFirst> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            for (MeterWorkRailwayActFirst model: models) {
                check(model);
                if (model.getValueFlag().equals(DbConstants.NET)) {
                    throw new MarkerMetException(500, "已经结净的数据无法修改！！");
                }
            }
            int count = 0;
            for (MeterWorkRailwayActFirst model: models) {
                MeterWorkRailwayActFirst afterModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(model.getActualFirstNo());
                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model);
                count ++;

                MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                logModel.setId(PublicMethod.IdCreate(2));
                logModel.setCreateManNo(model.getUpdateManNo());
                logModel.setCreateManName(model.getUpdateManName());
                logModel.setCreateTime(new Date());
                logModel.setOperationPage("一次数据维护");
                logModel.setOperationFunction("修改");
                logModel.setOperationId(model.getActualFirstNo());
                logModel.setOperationPredictionNo(model.getPredictionNo());
                logModel.setOperationCarnNo(model.getRailwayNo());
                logModel.setOperationBeforeFix(afterModel.toString());
                logModel.setOperationAfterFix(model.toString());
                meterBaseOperationLogMapper.insertSelective(logModel);
            }
            rm.setSucceed();
            rm.setMessage("操作成功，共有" + count + "条数据更新成功");
            rm.setCode("200");
            rm.setData(count);
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常");
        }
        return rm;
    }

    /**
     * 一次计量实体，转换为预报实体
     *
     * @param first
     * @return
     */
    public PreRailwayScale firstToScale(MeterWorkRailwayActFirst first) {
        PreRailwayScale scale = new PreRailwayScale();
        scale.setPredictionNo(first.getPredictionNo());
        scale.setNoticeNo(first.getNoticeNo());
        scale.setMatterNo(first.getMatterNo());
        scale.setMatterName(first.getMatterName());
        scale.setContractNo(first.getContractNo());
        scale.setBatchNo(first.getBatchNo());
        scale.setCustomerSupplierNo(first.getCustomerSupplierNo());
        scale.setCustomerSupplierName(first.getCustomerSupplierName());
        scale.setForwardingUnitNo(first.getForwardingUnitNo());
        scale.setForwardingUnitName(first.getForwardingUnitName());
        scale.setReceivingUintNo(first.getReceivingUintNo());
        scale.setReceivingUintName(first.getReceivingUintName());
        scale.setMaterialNo(first.getMaterialNo());
        scale.setMaterialName(first.getMaterialName());
        scale.setSpecNo(first.getSpecNo());
        scale.setSpecName(first.getSpecName());
        scale.setLoadPointNo(first.getLoadPointNo());
        scale.setLoadPointName(first.getLoadPointName());
        scale.setSampleNo(first.getSampleNo());
        scale.setSampleVoucher(first.getSampleVoucher());
        scale.setCarrierUnitNo(first.getCarrierUnitNo());
        scale.setCarrierUnitName(first.getCarrierUnitName());
        scale.setMeterTypeNo(first.getMeterTypeNo());
        scale.setMeterTypeName(first.getMeterTypeName());
        scale.setMemo(first.getMemo());
        scale.setArrival(first.getArrival());
        scale.setStation(first.getStation());
        scale.setSourceArea(first.getSourceArea());
        return scale;
    }

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

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int abolish(MeterWorkRailwayActFirst model) {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数为空!!");
            }
            check(model);
            if (model.getValueFlag().equals(DbConstants.NET)) {
                throw new MarkerMetException(500, "已结净的数据不可再废除!!");
            }
            if (model.getPredictionNo() != null && !model.getPredictionNo().equals("")) {
                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";
            }

            model.setValueFlag("0");
            int num = meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model);

            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.getActualFirstNo());
            logModel.setOperationPredictionNo(model.getPredictionNo());
            logModel.setOperationCarnNo(model.getRailwayNo());
            meterBaseOperationLogMapper.insertSelective(logModel);

//            if (model.getPredictionNo() != null && StringUtils.isNotEmpty(model.getPredictionNo())) {
//                PreRailwayScale scale = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
//                if (scale != null && StringUtils.isNotEmpty(scale.getPredictionNo())) {
//                    scale.setPredictionNo(model.getPredictionNo());
//                    scale.setUpdateTime(new Date());
//                    scale.setUpdateManName(model.getUpdateManName());
//                    scale.setUpdateManNo(model.getUpdateManNo());
//                    scale.setpValueFlag(scale.getValueFlag());
//                    scale.setValueFlag("3");
//                    scale.setUploadFlag("1");
//                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
//                }
//            }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int recover(MeterWorkRailwayActFirst model) {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数为空!!");
            }
            check(model);
            if (model.getValueFlag().equals(DbConstants.NET)) {
                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";
            }
            HashMap map = new HashMap();
            map.put("actualFirstNo", model.getActualFirstNo());
            List<MeterWorkRailwayActual> rows = meterWorkRailwayActualMapper.selectByAcatualFristNo(map);
            if (rows.size() >= 1) {
                model.setValueFlag("1");
            } else {
                if(model.getValueFlag().equals("0")) {
                    model.setValueFlag("1");
                }else{
                    model.setValueFlag("0");
                }
            }
            /*if(model.getWeightType().equals("0")) {
                map.put("actualFirst1No", model.getActualFirstNo());
                List<MeterWorkRailwayActual> rows = meterWorkRailwayActualMapper.selectByParameter(map);
                if(rows.size() >= 1) {
                    model.setValueFlag("2");
                }else{
                    model.setValueFlag("1");
                }
            }else {
                map.put("actualFirst2No", model.getActualFirstNo());
                List<MeterWorkRailwayActual> rows = meterWorkRailwayActualMapper.selectByParameter(map);
                if(rows.size() >= 1) {
                    model.setValueFlag("2");
                }else{
                    model.setValueFlag("1");
                }
            }*/
            int num = meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model);

            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.getActualFirstNo());
            logModel.setOperationPredictionNo(model.getPredictionNo());
            logModel.setOperationCarnNo(model.getRailwayNo());
            meterBaseOperationLogMapper.insertSelective(logModel);

//            if (StringUtils.isNotEmpty(model.getPredictionNo())) {
//                PreRailwayScale scale = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
//                if (scale != null && StringUtils.isNotEmpty(scale.getPredictionNo())) {
//                    scale.setPredictionNo(model.getPredictionNo());
//                    scale.setUpdateTime(new Date());
//                    scale.setUpdateManName(model.getUpdateManName());
//                    scale.setUpdateManNo(model.getUpdateManNo());
//                    scale.setpValueFlag(scale.getValueFlag());
//                    scale.setValueFlag("4");
//                    scale.setUploadFlag("1");
//                    scale.setMeasureStation(model.getBaseSpotNo());
//                    scale.setMeasureStationName(model.getBaseSpotName());
//                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
//                }
//            }
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCar(MeterWorkRailwayActFirst model) {
        try {
            ckeckcar(model);

            String userId = "";
            String userName = "";
            try{
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch (Exception e) {
                userId = "system";
                userName = "system";
            }
            MeterWorkRailwayActFirst modelTemp = meterWorkRailwayActFirstMapper.selectByPrimaryKey(model.getActualFirstNo());
            int num = meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model);

            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.getActualFirstNo());
            logModel.setOperationPredictionNo(model.getPredictionNo());
            logModel.setOperationCarnNo(model.getRailwayNo());
            logModel.setOperationBeforeFix(modelTemp.getRailwayNo());
            logModel.setOperationAfterFix(model.getRailwayNo());
            meterBaseOperationLogMapper.insertSelective(logModel);

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

    private void ckeckcar(MeterWorkRailwayActFirst model) throws MarkerMetException {
        if (StringUtils.isEmpty(model.getActualFirstNo())) {
            throw new MarkerMetException(500, "操作数据主键不能为空");
        }
        if (model.getValueFlag() == "2") {
            throw new MarkerMetException(500, "已经结净的数据不能进行修改");
        }
        if (StringUtils.isNotEmpty(model.getPredictionNo())) {
            throw new MarkerMetException(500, "已经匹配委托的数据无法修改车号！！");
        }
        if (model.getRailwayNo() == null || model.getRailwayNo().equals("")) {
            throw new MarkerMetException(500, "火车号不能为空");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        model.setUpdateTime(new Date());
        model.setUpdateManNo(userId);
        model.setUpdateManName(userName);
    }

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

    @Override
    public List<MeterWorkRailwayActFirst> carFirstDataDetail(Map<String, Object> params) {
        if(params.get("railwayTypeNoStr") != null) {
            String[] typeNoStr = params.get("railwayTypeNoStr").toString().split(",");
            params.put("typeNoStr", typeNoStr);
        }
        List<MeterWorkRailwayActFirst> models = meterWorkRailwayActFirstMapper.carFirstDataDetail(params);
        return models;
    }

    @Override
    public List<MeterWorkRailwayActFirst> likeByDescNoPage(HashMap<String, Object> parameters) {
        List<MeterWorkRailwayActFirst> rows = meterWorkRailwayActFirstMapper.likeByDesc(parameters);
        return rows;
    }

    @Override
    public List<MeterWorkRailwayActFirst> webByLikeDesc(HashMap<String, Object> parameters) {
        String[] lcNoTemp = parameters.get("lcNoTemp") == null? "".split(",") :parameters.get("lcNoTemp").toString().split(",");
        parameters.remove("lcNoTemp");
        parameters.put("lcNoTemp", lcNoTemp);
        for (String str:
             lcNoTemp) {
            if(str.equals("")) {
                parameters.put("lcNoNull", "1");
                break;
            }else {
                parameters.put("lcNoNull", "");
            }
        }
        String[] railwayNoList = parameters.get("railwayNoList") == null? "".split(",") :parameters.get("railwayNoList").toString().split(",");
        parameters.remove("railwayNoList");
        parameters.put("railwayNoList", railwayNoList);
        for (String str:
                railwayNoList) {
            if(str.equals("")) {
                parameters.put("railwayNoNull", "1");
                break;
            }else {
                parameters.put("railwayNoNull", "");
            }
        }
        List<MeterWorkRailwayActFirst> rows = meterWorkRailwayActFirstMapper.likeByDesc(parameters);
        return rows;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> distinctRailwayNo(HashMap<String, Object> param) {
        if(param==null){
            param = new HashMap<>();
        }
        String[] lcNoTemp = param.get("lcNo") == null ? "".split(","): param.get("lcNo").toString().split(",");
        param.put("lcNoTemp", lcNoTemp);
        for (String str: lcNoTemp) {
            if(str.equals("")) {
                param.put("lcNoNull", "1");
                break;
            }else {
                param.put("lcNoNull", "");
            }
        }
        List<MeterWorkRailwayActFirst> templist = meterWorkRailwayActFirstMapper.distinctRailwayNo(param);
        List<String> list = new ArrayList<>();
        if(templist != null && !templist.isEmpty() ) {
            try{
                list = templist.stream().map(MeterWorkRailwayActFirst::getRailwayNo ).distinct().collect(Collectors.toList());
                if(list==null && list.isEmpty()) {
                    list.add("");
                }
            }catch (Exception e) {
                e.printStackTrace();
                list.add("");
            }
        }
        return list;
    }

    @Override
    public List<String> distinctLcNo(HashMap<String, Object> param) {
        List<MeterWorkRailwayActFirst> templist = meterWorkRailwayActFirstMapper.distinctLcNo(param);
        List<String> list = new ArrayList<>();
        if (templist != null && !templist.isEmpty() ) {
            try{
                list = templist.stream().map(MeterWorkRailwayActFirst::getLcNo).distinct().collect(Collectors.toList());
                if(list==null && list.isEmpty()) {
                    list.add("");
                }
            }catch (Exception e) {
                list.add("");
            }
        }
        return list;
    }

    @Override
    public RESTfulResult distinctLcNoAndRailwayNo(HashMap<String, Object> param) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            HashMap<String, Object> obj = new HashMap<>();
            List<MeterWorkRailwayActFirst> tempList = meterWorkRailwayActFirstMapper.distinctLcNo(param);
            List<String> list = null;
            List<String> railwayNum = null;
            if (tempList.size() != 0) {
                list = tempList.stream().map(MeterWorkRailwayActFirst::getLcNo).distinct().collect(Collectors.toList());
                railwayNum = tempList.stream().map(MeterWorkRailwayActFirst::getRailwayNo).distinct().collect(Collectors.toList());
            }
            obj.put("LcList", list);
            obj.put("railwayNoList", railwayNum);
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(obj);
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常！！>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MeterWorkRailwayActFirst> selectAct(HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            parmas.put("railwayTypeName", "动态轨道衡");
            parmas.put("railwayTypeNo", DbConstants.ACT);
            List<MeterWorkRailwayActFirst> mwrafs = meterWorkRailwayActFirstMapper.selectAct(parmas);
            return mwrafs;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    /**
     * 静态轨道衡复磅保存
     *
     * @param model
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String doAddWfRecover(MeterWorkRailwayActFirst model) {
        MeterWorkRailwayActFirst actFirst = meterWorkRailwayActFirstMapper.selectByPrimaryKey(model.getActualFirstNo());
        if (actFirst != null && StringUtils.isNotEmpty(actFirst.getActualFirstNo())) {
            //=================一次计量数据处理：开始=====================
            BigDecimal oldWeight = new BigDecimal(0);
            oldWeight = actFirst.getMeterWeight();
            //subtract add
            actFirst.setMeterWeight(model.getMeterWeight());
            actFirst.setUpdateManName("静态衡复磅更新");
            actFirst.setUpdateManNo("system");
            actFirst.setUpdateTime(new Date());
            actFirst.setMeterBalanceWeight(model.getMeterWeight().subtract(actFirst.getMeterSortWeight()));
            actFirst.setMemo(actFirst.getMemo() + ";" + "复磅保存：历史重量[" + oldWeight + "]，" +
                    "新的重量[" + actFirst.getMeterWeight() + "]");
            meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(actFirst);
            //=================一次计量数据处理：结束=====================

            //=================已结净的数据处理：开始=====================
            MeterWorkRailwayActual actual = new MeterWorkRailwayActual();
            if ("0".equals(actFirst.getWeightType())) {
                actual.setActualFirst1No(actFirst.getActualFirstNo()); //二次计量的毛重作业编号
            } else {
                actual.setActualFirst2No(actFirst.getActualFirstNo());//二次计量的皮重作业编号
            }
            List<MeterWorkRailwayActual> actualList = meterWorkRailwayActualMapper.selectByParameters(FieldsCollector.beanToMap(actual));
            if (actualList != null && actualList.size() > 0) {
                actual = actualList.get(0);
                if ("0".equals(actFirst.getWeightType())) {
                    actual.setUpdateManName("静态衡复磅更新");
                    actual.setUpdateManNo("system");
                    actual.setUpdateTime(new Date());
                    actual.setGrossWeight(actFirst.getMeterBalanceWeight());
                    actual.setNetWeight(actual.getGrossWeight().subtract(actual.getTareWeight()));
                    actual.setMemo(actual.getMemo() + ";" + "复磅保存：历史毛重[" + oldWeight + "]，" +
                            "新的毛重[" + actFirst.getMeterWeight() + "]");
                } else {
                    actual.setUpdateManName("静态衡复磅更新");
                    actual.setUpdateManNo("system");
                    actual.setUpdateTime(new Date());
                    actual.setTareWeight(actFirst.getMeterBalanceWeight());
                    actual.setNetWeight(actual.getGrossWeight().subtract(actual.getTareWeight()));
                    actual.setMemo(actual.getMemo() + ";" + "复磅保存：历史皮重[" + oldWeight + "]，" +
                            "新的皮重[" + actFirst.getMeterWeight() + "]");
                }
                actual.setUploadFlag("1");
                meterWorkRailwayActualMapper.updateByPrimaryKeySelective(actual);
            }
            //=================已结净的数据处理：结束=====================
        }
        return model.getActualFirstNo();
    }

    /**
     * C#端轨道衡实绩静轨-复磅-更新自重
     *
     * @param model
     * @return
     */
    @Override
    public String doUpdateSelfWeight(MeterWorkRailwayActFirst model) {
        MeterWorkRailwayActFirst actFirst = meterWorkRailwayActFirstMapper.selectByPrimaryKey(model.getActualFirstNo());
        if (actFirst != null && StringUtils.isNotEmpty(actFirst.getActualFirstNo())) {
            //=================一次计量数据处理：开始=====================
            BigDecimal oldWeight = new BigDecimal(0);
            oldWeight = actFirst.getMeterWeight();
            //subtract add
            actFirst.setMeterWeight(model.getMeterWeight());
            actFirst.setUpdateManName("静态衡[更新自重]更新");
            actFirst.setUpdateManNo("system");
            actFirst.setUpdateTime(new Date());
            actFirst.setMeterBalanceWeight(model.getMeterWeight().subtract(actFirst.getMeterSortWeight()));
            actFirst.setMemo(actFirst.getMemo() + ";" + "[更新自重]：历史重量[" + oldWeight + "]，" +
                    "新的重量[" + actFirst.getMeterWeight() + "]");
            meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(actFirst);
            //=================一次计量数据处理：结束=====================

            //=================已结净的数据处理：开始=====================
            MeterWorkRailwayActual actual = new MeterWorkRailwayActual();
            if ("0".equals(actFirst.getWeightType())) {
                actual.setActualFirst1No(actFirst.getActualFirstNo()); //二次计量的毛重作业编号
            } else {
                actual.setActualFirst2No(actFirst.getActualFirstNo());//二次计量的皮重作业编号
            }
            List<MeterWorkRailwayActual> actualList = meterWorkRailwayActualMapper.selectByParameters(FieldsCollector.beanToMap(actual));
            if (actualList != null && actualList.size() > 0) {
                actual = actualList.get(0);
                if ("0".equals(actFirst.getWeightType())) {
                    actual.setUpdateManName("静态衡[更新自重]更新");
                    actual.setUpdateManNo("system");
                    actual.setUpdateTime(new Date());
                    actual.setGrossWeight(actFirst.getMeterBalanceWeight());
                    actual.setNetWeight(actual.getGrossWeight().subtract(actual.getTareWeight()));
                    actual.setMemo(actual.getMemo() + ";" + "[更新自重]：历史毛重[" + oldWeight + "]，" +
                            "新的毛重[" + actFirst.getMeterWeight() + "]");
                } else {
                    actual.setUpdateManName("静态衡[更新自重]更新");
                    actual.setUpdateManNo("system");
                    actual.setUpdateTime(new Date());
                    actual.setTareWeight(actFirst.getMeterBalanceWeight());
                    actual.setNetWeight(actual.getGrossWeight().subtract(actual.getTareWeight()));
                    actual.setMemo(actual.getMemo() + ";" + "[更新自重]：历史皮重[" + oldWeight + "]，" +
                            "新的皮重[" + actFirst.getMeterWeight() + "]");
                }
                actual.setUploadFlag("1");
                meterWorkRailwayActualMapper.updateByPrimaryKeySelective(actual);
            }
            //=================已结净的数据处理：结束=====================
        }
        return model.getActualFirstNo();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doAddNetHZHX(MeterWorkRailwayActFirst first02, MeterWorkRailwayActual carModel, MeterWorkRailwayActFirst first03, MeterMonitorNote note) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

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

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

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

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

                MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(first02.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
//                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);
                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);
                actualRow.setNetTime(new Date());
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setNote("混装匹配结净");
                actualRow.setNetMode("4");
                actualRow.setUploadFlag("1");
                actualRow.setValueFlag("1");
                actualRow.setIsPreSacale("1");
                actualRow.setNetManNo(carModel.getNetManNo());
                actualRow.setNetManName(carModel.getNetManName());
                actualRow.setNetSpot3No(carModel.getNetSpot3No());
                actualRow.setNetSpot3Name(carModel.getNetSpot3Name());
                actualRow.setCreateManName(carModel.getCreateManName());
                actualRow.setCreateTime(carModel.getCreateTime());
                actualRow.setUpdateManNo(carModel.getUpdateManNo());
                actualRow.setUpdateManName(carModel.getUpdateManName());
                actualRow.setUpdateTime(carModel.getUpdateTime());
                actualRow.setPredictionNo(scale.getPredictionNo());
                actualRow.setPredictionType("7");
                actualRow.setNetWeight(first02.getMeterWeight().subtract(model02.getMeterWeight()));
                if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                    actualRow.setNetManNo(actualRow.getGrossManNo());
                    actualRow.setNetManName(actualRow.getGrossManName());
                    actualRow.setNetTime(actualRow.getGrossTime());
                    actualRow.setCreateTime(actualRow.getGrossTime());
                    actualRow.setCreateManName(actualRow.getGrossManName());
                    actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                }
                MeterWorkRailwayActFirst grossModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualRow.getActualFirst1No());
                MeterWorkRailwayActFirst tareModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualRow.getActualFirst2No());
                if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                    actualRow.setLcNo(grossModel.getLcNo());
                }else {
                    actualRow.setLcNo(tareModel.getLcNo());
                }
                meterWorkRailwayActualMapper.insertSelective(actualRow);

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

            } else if (first02.getWeightType().equals("1")) {         //混卸
                //修改委托状态
                //将委托中的编号和收发货的等数据拷贝进一次数据中
                MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first1, model01);
                MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(first02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first2, first02);

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

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

                //保存结净
//                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);
                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);
                actualRow.setNetTime(new Date());
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setNote("混卸匹配结净");
                actualRow.setNetMode("5");
                actualRow.setUploadFlag("1");
                actualRow.setValueFlag("1");
                actualRow.setIsPreSacale("1");
                actualRow.setNetManNo(carModel.getNetManNo());
                actualRow.setNetManName(carModel.getNetManName());
                actualRow.setNetSpot3No(carModel.getNetSpot3No());
                actualRow.setNetSpot3Name(carModel.getNetSpot3Name());
                actualRow.setCreateManName(carModel.getCreateManName());
                actualRow.setCreateTime(carModel.getCreateTime());
                actualRow.setUpdateManNo(carModel.getUpdateManNo());
                actualRow.setUpdateManName(carModel.getUpdateManName());
                actualRow.setUpdateTime(carModel.getUpdateTime());
                actualRow.setPredictionNo(scale.getPredictionNo());
                actualRow.setPredictionType("6");
                actualRow.setNetWeight(model01.getMeterWeight().subtract(first02.getMeterWeight()));
                if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                    actualRow.setNetManNo(actualRow.getGrossManNo());
                    actualRow.setNetManName(actualRow.getGrossManName());
                    actualRow.setNetTime(actualRow.getGrossTime());
                    actualRow.setCreateTime(actualRow.getGrossTime());
                    actualRow.setCreateManName(actualRow.getGrossManName());
                    actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                }
                meterWorkRailwayActualMapper.insertSelective(actualRow);

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

            //存储当前计量的数据
            if (first03 != null) {
//                String fristNo03 = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//                first03.setActualFirstNo(fristNo03);
                String fristNo03 = PublicMethod.IdCreate(3);
                first03.setActualFirstNo(fristNo03);
                first03.setCreateTime(new Date());
                meterWorkRailwayActFirstMapper.insertSelective(first03);
            }
            rm.setSucceed();
            rm.setData(first02.getActualFirstNo());
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            return rm;
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！！>>>>" + e.getMessage());
            return rm;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doNetAddTwo(MeterWorkRailwayActFirst one, MeterWorkRailwayActual two, PreRailwayScale three, PreRailwayScale four) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

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

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

            MeterWorkRailwayActFirst model01 = new MeterWorkRailwayActFirst();
            MeterWorkRailwayActFirst model02 = new MeterWorkRailwayActFirst();

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

            PreRailwayScale scale01 = preRailwayScaleMapper.selectByPrimaryKey(three.getPredictionNo());
            PreRailwayScale scale02 = preRailwayScaleMapper.selectByPrimaryKey(four.getPredictionNo());

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

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

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

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

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

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

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

            rm.setSucceed();
            rm.setData(one.getActualFirstNo());
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            return rm;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage(e.getMessage());
            return rm;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doNetAddTwo02(List<MeterWorkRailwayActFirst> selectedFirsts, List<MeterWorkRailwayActFirst> addFirsts, List<PreRailwayScale> scales, String note,Boolean needUpload){
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());

            HashMap map = new HashMap();
            List<MeterWorkRailwayActFirst> newFirsts = new ArrayList<>();
            List<MeterWorkRailwayActual> newActuals = new ArrayList<>();

            //如果计量员选择一次记录是空就是第一次 真，不是空就是第二次 假
            boolean isFirst = CollectionUtils.isEmpty(selectedFirsts);
            Assert.isTrue(!CollectionUtils.isEmpty(addFirsts) && addFirsts.size() >= 1, "新增一次记录必须不能为空");
            if(!isFirst) {
                if(CollectionUtils.isEmpty(scales)){
                    List<PreRailwayScale> scaleTemp = new ArrayList<>();
                    selectedFirsts.forEach(item->{
                        if(item.getPredictionNo() != null && StringUtils.isNotEmpty(item.getPredictionNo())){
                            scaleTemp.add(preRailwayScaleMapper.selectBypredictionNo(item.getPredictionNo()));
                        }
                    });
                    scales = scaleTemp;
                }

                if(!CollectionUtils.isEmpty(scales) && scales.size() != 2) {
                    Assert.isTrue(!CollectionUtils.isEmpty(scales) && scales.size() == 2, "双委托结净必须有两条委托");
                }
                //判断是否是双委托
                if (isDoubleEntrust(rm, scales.get(0), scales.get(1))) return rm;
            }
            if(!CollectionUtils.isEmpty(scales)) {
                Assert.isTrue(  scales.size() <= 2, "委托最多只能选择两条");
            }

            if(addFirsts.get(0).getRailwayFactoryWeight() != null && addFirsts.get(0).getRailwayFactoryWeight().toString() != ""
                && addFirsts.get(1).getRailwayFactoryWeight() != null && addFirsts.get(1).getRailwayFactoryWeight().toString() != "") {
                BigDecimal tareWeight01 = addFirsts.get(0).getRailwayFactoryWeight();
                BigDecimal tareWeight02 = addFirsts.get(1).getRailwayFactoryWeight();
                Assert.isTrue(tareWeight01.compareTo(new BigDecimal(0)) > 0
                        && tareWeight02.compareTo(new BigDecimal(0)) > 0, "新增两条一次数据的标识皮不能小于等于零");
                String userId = addFirsts.get(0).getCreateManNo();
                String userName = addFirsts.get(0).getCreateManName();
                //一次数据匹配委托
                for (int i = 0; i < scales.size(); i++) {
                    firstMatchPreScale(addFirsts.get(i), scales.get(i));
                    scales.get(i).setValueFlag("2");
                    scales.get(i).setUpdateManName("双委托更新");
                    scales.get(i).setPredictionType("5");
                }

                List<MeterWorkRailwayActFirst> addFirstsTare = new ArrayList<>();
                //插入一次添加数据
                for (int i = 0; i < addFirsts.size(); i++) {
//                    String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//                    addFirsts.get(i).setActualFirstNo(actualFirstNo);
                    String actualFirstNo = PublicMethod.IdCreate(3);
                    addFirsts.get(i).setActualFirstNo(actualFirstNo);
                    addFirsts.get(i).setValueFlag(DbConstants.NET);
                    addFirsts.get(i).setUpdateManNo("system");
                    addFirsts.get(i).setUpdateManName("双委托更新");
                    addFirsts.get(i).setUpdateTime(new Date());
                    addFirsts.get(i).setCreateManNo(userId);
                    addFirsts.get(i).setCreateManName(userName);
                    addFirsts.get(i).setCreateTime(new Date());
                    addFirsts.get(i).setPredictionType("5");
                    addFirsts.get(i).setUpdateManName("双委托更新");
                    meterWorkRailwayActFirstMapper.insertSelective(addFirsts.get(i));
                    MeterWorkRailwayActFirst tareFirst = new MeterWorkRailwayActFirst();
                    BeanUtils.copyProperties(addFirsts.get(i),tareFirst);
                    addFirstsTare.add(tareFirst);
                }
                //新增标识皮重数据


                for (int i = 0; i < addFirstsTare.size(); i++) {
//                    String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//                    addFirsts.get(i).setActualFirstNo(actualFirstNo);
//                    String actualFirstNo = PublicMethod.IdCreate(3);
//                    addFirstsTare.get(i).setActualFirstNo(actualFirstNo);
                    addFirstsTare.get(i).setWeightType("4");
                    addFirstsTare.get(i).setMeterWeight(addFirstsTare.get(i).getRailwayFactoryWeight());
                    addFirstsTare.get(i).setValueFlag(DbConstants.NET);
                    addFirstsTare.get(i).setUpdateManNo("system");
                    addFirstsTare.get(i).setUpdateManName("双委托更新");
                    addFirstsTare.get(i).setUpdateTime(new Date());
                    addFirstsTare.get(i).setCreateManNo(userId);
                    addFirstsTare.get(i).setCreateManName(userName);
                    addFirstsTare.get(i).setCreateTime(new Date());
                    addFirstsTare.get(i).setPredictionType("5");
                    addFirstsTare.get(i).setUpdateManName("双委托更新");
                    //meterWorkRailwayActFirstMapper.insertSelective(addFirstsTare.get(i));
                    doSaveTare(addFirstsTare.get(i),"2");
                }
                meterWorkRailwayActFirstMapper.batchUpdate(addFirsts);

                MeterWorkRailwayActFirst firstTemp = meterWorkRailwayActFirstMapper.selectAndMatch(addFirsts.get(0).getActualFirstNo(), scales.get(0).getPredictionNo());
                BeanUtils.copyProperties(firstTemp, addFirsts.get(0));
                addFirsts.get(0).setValueFlag(DbConstants.NET);
                addFirsts.get(0).setUpdateManNo("system");
                addFirsts.get(0).setUpdateManName("双委托结净更新");
                addFirsts.get(0).setCreateManNo(userId);
                addFirsts.get(0).setCreateManName(userName);
                addFirsts.get(0).setCreateTime(new Date());
                addFirsts.get(0).setWeightType("0");
                addFirsts.get(0).setUpdateTime(new Date());
                addFirsts.get(0).setIsPreSacale("1");
                addFirsts.get(0).setPredictionNo(scales.get(0).getPredictionNo());
                addFirsts.get(0).setPredictionType("5");
                addFirsts.get(0).setPredictionCombination(scales.get(0).getPredictionNo() + "," + scales.get(1).getPredictionNo());
                addFirsts.get(0).setCreateTime(new Date());
                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(addFirsts.get(0));

                //添加第二次皮重数据库
                addFirsts.get(1).setValueFlag(DbConstants.NET);
                addFirsts.get(1).setUpdateManNo("system");
                addFirsts.get(1).setUpdateManName("双委托结净更新");
                addFirsts.get(1).setWeightType("0");
                addFirsts.get(1).setUpdateTime(new Date());
                addFirsts.get(1).setIsPreSacale("1");
                addFirsts.get(1).setPredictionNo(scales.get(1).getPredictionNo());
                addFirsts.get(1).setPredictionType("5");
                addFirsts.get(1).setPredictionCombination(scales.get(0).getPredictionNo() + "," + scales.get(1).getPredictionNo());
                addFirsts.get(1).setCreateTime(new Date());
                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(addFirsts.get(1));
                if(!CollectionUtils.isEmpty(scales)) {
                    preRailwayScaleMapper.batchUpdate(scales);
                }
                newFirsts.addAll(addFirsts);
                MeterWorkRailwayActual actualRow01 = meterWorkRailwayActualMapper.manualAddPre(addFirsts.get(0).getActualFirstNo(), addFirsts.get(0).getActualFirstNo(), scales.get(0).getPredictionNo());
                MeterWorkRailwayActual actualRow02 = meterWorkRailwayActualMapper.manualAddPre(addFirsts.get(1).getActualFirstNo(), addFirsts.get(1).getActualFirstNo(), scales.get(1).getPredictionNo());
                actualRow01.setActualNo(PublicMethod.IdCreate(3));
                actualRow01.setIsPreSacale("1");
                actualRow01.setTareWeight(tareWeight01);
                actualRow01.setNetMode("2");
                actualRow01.setValueFlag("1");
                actualRow01.setUploadFlag("1");
                actualRow01.setNetWeight(actualRow01.getGrossWeight().subtract(tareWeight01));
                actualRow01.setPredictionType("5");
                actualRow01.setPredictionCombination(scales.get(0).getPredictionNo() + "," + scales.get(1).getPredictionNo());
                actualRow01.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow01.getActualFirst1No()));
                //是否发布 ture发布，false不发布
                if(needUpload) {
                    if(actualRow01.getIsPreSacale() != null && actualRow01.getIsPreSacale().equals("1")) {
                        actualRow01.setValueFlag("2");
                        actualRow01.setUploadFlag("1");
                    }
                }
                meterWorkRailwayActualMapper.insertSelective(actualRow01);

                actualRow02.setActualNo(PublicMethod.IdCreate(3));
                actualRow02.setIsPreSacale("1");
                actualRow02.setTareWeight(tareWeight02);
                actualRow02.setNetWeight(actualRow02.getGrossWeight().subtract(tareWeight02));
                actualRow02.setPredictionType("5");
                actualRow02.setNetMode("2");
                actualRow02.setValueFlag("1");
                actualRow02.setUploadFlag("1");
                actualRow02.setPredictionCombination(scales.get(0).getPredictionNo() + "," + scales.get(1).getPredictionNo());
                actualRow02.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow02.getActualFirst1No()));
                //是否发布 ture发布，false不发布
                if(needUpload) {
                    if(actualRow02.getIsPreSacale() != null && actualRow02.getIsPreSacale().equals("1")) {
                        actualRow02.setValueFlag("2");
                        actualRow02.setUploadFlag("1");
                    }
                }
                meterWorkRailwayActualMapper.insertSelective(actualRow02);

                newActuals.add(actualRow01);
                newActuals.add(actualRow02);

            }else{
                //第一次：所选择的数据为空
                if (isFirst) {
                    String userId = addFirsts.get(0).getCreateManNo();
                    String userName = addFirsts.get(0).getCreateManName();
                    if(scales != null && scales.size() >= 0) {
                        for (int i = 0; i < scales.size(); i++) {
                            firstMatchPreScale(addFirsts.get(i), scales.get(i));
                            scales.get(i).setValueFlag("1");
                            scales.get(i).setUpdateManName("双委托更新");
                            scales.get(i).setPredictionType("5");
                        }

                        for (int i = 0; i < addFirsts.size(); i++) {
//                    String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//                    addFirsts.get(i).setActualFirstNo(actualFirstNo);
                            String actualFirstNo = PublicMethod.IdCreate(3);
                            addFirsts.get(i).setActualFirstNo(actualFirstNo);
                            addFirsts.get(i).setValueFlag("1");
                            addFirsts.get(i).setUpdateManNo("system");
                            addFirsts.get(i).setUpdateManName("双委托更新");
                            addFirsts.get(i).setUpdateTime(new Date());
                            addFirsts.get(i).setCreateManNo(userId);
                            addFirsts.get(i).setCreateManName(userName);
                            addFirsts.get(i).setCreateTime(new Date());
                            addFirsts.get(i).setPredictionType("5");
                            addFirsts.get(i).setUpdateManName("双委托更新");
                            meterWorkRailwayActFirstMapper.insertSelective(addFirsts.get(i));
                        }
                        meterWorkRailwayActFirstMapper.batchUpdate(addFirsts);
                        if(!CollectionUtils.isEmpty(scales)) {
                            preRailwayScaleMapper.batchUpdate(scales);
                        }
                    }else {
                        for (int i = 0; i < addFirsts.size(); i++) {
//                    String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//                    addFirsts.get(i).setActualFirstNo(actualFirstNo);
                            String actualFirstNo = PublicMethod.IdCreate(3);
                            addFirsts.get(i).setActualFirstNo(actualFirstNo);
                            addFirsts.get(i).setValueFlag("1");
                            addFirsts.get(i).setUpdateManNo("system");
                            addFirsts.get(i).setUpdateManName("双委托更新");
                            addFirsts.get(i).setUpdateTime(new Date());
                            addFirsts.get(i).setCreateManNo(userId);
                            addFirsts.get(i).setCreateManName(userName);
                            addFirsts.get(i).setCreateTime(new Date());
                            meterWorkRailwayActFirstMapper.insertSelective(addFirsts.get(i));
                        }
                        meterWorkRailwayActFirstMapper.batchUpdate(addFirsts);
                    }
                    newFirsts.addAll(addFirsts);
                } else {//第二次，所选择数据一条或者两条
                    String userId = selectedFirsts.get(0).getCreateManNo();
                    String userName = selectedFirsts.get(0).getCreateManName();
                    //如果只选择了一条一次记录，就复制一条
                    if (selectedFirsts.size() == 1) {
                        MeterWorkRailwayActFirst firstCopy = new MeterWorkRailwayActFirst();
                        BeanUtils.copyProperties(selectedFirsts.get(0), firstCopy);
//                    String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//                    firstCopy.setActualFirstNo(actualFirstNo);
                        String actualFirstNo = PublicMethod.IdCreate(3);
                        firstCopy.setActualFirstNo(actualFirstNo);
                        firstCopy.setCreateManNo(userId);
                        firstCopy.setCreateManName(userName);
                        firstCopy.setCreateTime(new Date());
                        meterWorkRailwayActFirstMapper.insertSelective(firstCopy);
                        selectedFirsts.add(firstCopy);
                        newFirsts.add(firstCopy);
                    }

                    if(selectedFirsts.size() == 2) {
                        for (int i = 0; i < scales.size(); i++) {
//                    String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//                    addFirsts.get(i).setActualFirstNo(actualFirstNo);
                            String actualFirstNo = PublicMethod.IdCreate(3);
                            addFirsts.get(i).setActualFirstNo(actualFirstNo);
                            addFirsts.get(i).setUpdateManNo("system");
                            addFirsts.get(i).setUpdateManName("双委托结净更新");
                            addFirsts.get(i).setUpdateTime(new Date());
                            addFirsts.get(i).setCreateManNo(userId);
                            addFirsts.get(i).setCreateManName(userName);
                            addFirsts.get(i).setCreateTime(new Date());
                            addFirsts.get(i).setValueFlag(DbConstants.NET);
                            addFirsts.get(i).setIsPreSacale("1");
                            addFirsts.get(i).setPredictionType("5");
                            addFirsts.get(i).setPredictionNo(scales.get(i).getPredictionNo());
                            addFirsts.get(i).setPredictionCombination(selectedFirsts.get(i).getPredictionNo() + "," + addFirsts.get(i).getPredictionNo());
                            addFirsts.get(i).setCreateTime(new Date());
                            addFirsts.get(i).setRailwayTypeNo("001019001");
                            addFirsts.get(i).setRailwayTypeName("静态轨道衡");
                            meterWorkRailwayActFirstMapper.insertSelective(addFirsts.get(i));

                            //添加第二次皮重数据库
                            if(StringUtils.isEmpty(selectedFirsts.get(i).getActualFirstNo())) {
                                selectedFirsts.get(i).setActualFirstNo(PublicMethod.IdCreate(3));
                                selectedFirsts.get(i).setCreateTime(new Date());
                                selectedFirsts.get(i).setUpdateManNo("system");
                                selectedFirsts.get(i).setUpdateManName("双委托结净更新");
                                selectedFirsts.get(i).setUpdateTime(new Date());
                                selectedFirsts.get(i).setValueFlag(DbConstants.NET);
                                selectedFirsts.get(i).setIsPreSacale("1");
                                selectedFirsts.get(i).setPredictionType("5");
                                selectedFirsts.get(i).setPredictionNo(scales.get(i).getPredictionNo());
                                selectedFirsts.get(i).setPredictionCombination(selectedFirsts.get(i).getPredictionNo() + "," + addFirsts.get(i).getPredictionNo());
                                meterWorkRailwayActFirstMapper.insertSelective(selectedFirsts.get(i));
                            }else{
                                selectedFirsts.get(i).setUpdateManNo("system");
                                selectedFirsts.get(i).setUpdateManName("双委托结净更新");
                                selectedFirsts.get(i).setUpdateTime(new Date());
                                selectedFirsts.get(i).setValueFlag(DbConstants.NET);
                                selectedFirsts.get(i).setIsPreSacale("1");
                                selectedFirsts.get(i).setPredictionType("5");
                                selectedFirsts.get(i).setPredictionNo(scales.get(i).getPredictionNo());
                                selectedFirsts.get(i).setPredictionCombination(selectedFirsts.get(i).getPredictionNo() + "," + addFirsts.get(i).getPredictionNo());
                                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(selectedFirsts.get(i));

                            }

                            scales.get(i).setValueFlag("2");
                            scales.get(i).setUploadFlag("1");
                            scales.get(i).setUpdateManName("双委托结净更新");
                            scales.get(i).setPredictionType("5");
                            scales.get(i).setMeasureStation(selectedFirsts.get(i).getBaseSpotNo());
                            scales.get(i).setMeasureStationName(selectedFirsts.get(i).getBaseSpotName());
                            preRailwayScaleMapper.updateByPrimaryKeySelective(scales.get(i));
                        }

                        for (int i = 0; i < addFirsts.size(); i++) {
                            MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();
                            if(selectedFirsts.get(i).getMeterWeight().compareTo(addFirsts.get(i).getMeterWeight()) > 0) {
                                doNet(actualRow, selectedFirsts.get(i), addFirsts.get(i), selectedFirsts.get(i).getPredictionNo());

                                addFirsts.get(i).setWeightType("1");
                                selectedFirsts.get(i).setWeightType("0");
                                doSaveTare(addFirsts.get(i));
                            }else {
                                doNet(actualRow, addFirsts.get(i), selectedFirsts.get(i),  addFirsts.get(i).getPredictionNo());
                                addFirsts.get(i).setWeightType("0");
                                selectedFirsts.get(i).setWeightType("1");
                                doSaveTare(selectedFirsts.get(i));
                            }
//                    String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                    actualRow.setActualNo(sdf.format(new Date()) + no);
                            String no = PublicMethod.IdCreate(3);
                            actualRow.setActualNo(no);
                            actualRow.setNote("双委托匹配结净");
                            actualRow.setNetMode("2");
                            actualRow.setValueFlag("1");
                            actualRow.setUploadFlag("1");
                            actualRow.setPredictionType("5");
                            actualRow.setPredictionCombination(selectedFirsts.get(0).getPredictionNo() + "," + addFirsts.get(1).getPredictionNo());
                            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());
                                actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                            }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                            }
                            //是否发布 ture发布，false不发布
                            if(needUpload) {
                                if(actualRow.getIsPreSacale() != null && actualRow.getIsPreSacale().equals("1")) {
                                    actualRow.setValueFlag("2");
                                    actualRow.setUploadFlag("1");
                                }
                            }
                            meterWorkRailwayActualMapper.insertSelective(actualRow);
                            newActuals.add(actualRow);
                        }
                        meterWorkRailwayActFirstMapper.batchUpdate(addFirsts);
                        meterWorkRailwayActFirstMapper.batchUpdate(selectedFirsts);
                        newFirsts.addAll(addFirsts);
                    }
                }
            }
            map.put("firsts", newFirsts);
            map.put("actuals", newActuals);
            rm.setSucceed();
            rm.setMessage("操作成功");
            rm.setData(map);
            return rm;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage(e.getMessage());
            return rm;
        }
    }

    private boolean isDoubleEntrust(RESTfulResult rm, PreRailwayScale scale, PreRailwayScale scale2) {
        //判断双委托 物料相同、A委托的收货单位是B委托的发货单位 或者 B委托的收货单位是A委托的发货单位
        if (!(scale.getMatterNo().equals(scale2.getMatterNo()) &&
                (scale.getReceivingUintNo().equals(scale2.getForwardingUnitNo()) ||
                        scale.getForwardingUnitNo().equals(scale2.getReceivingUintNo())))) {
            rm.setMessage("委托信息不符合双委托规则，请确认!!");
            return true;
        }
        return false;
    }

    //一次记录双委托结净匹配
    private void doNet(MeterWorkRailwayActual actualRow, MeterWorkRailwayActFirst grossFirst, MeterWorkRailwayActFirst tareFirst, String predictionNo) {
        if(StringUtils.isEmpty(grossFirst.getActualFirstNo())) {
            grossFirst.setActualFirstNo(PublicMethod.IdCreate(3));
            grossFirst.setCreateTime(new Date());
            meterWorkRailwayActFirstMapper.insertSelective(grossFirst);
        }
        MeterWorkRailwayActFirst grossFirstTemp = meterWorkRailwayActFirstMapper.selectAndMatch(grossFirst.getActualFirstNo(), predictionNo);
        BeanUtils.copyProperties(grossFirstTemp, grossFirst);
        grossFirst.setIsPreSacale("1");
        grossFirst.setValueFlag("2");
        grossFirst.setPredictionType("5");
        grossFirst.setNote("双委托结净");

        if(StringUtils.isEmpty(tareFirst.getActualFirstNo())) {
            tareFirst.setActualFirstNo(PublicMethod.IdCreate(3));
            tareFirst.setCreateTime(new Date());
            meterWorkRailwayActFirstMapper.insertSelective(tareFirst);
        }
        MeterWorkRailwayActFirst tareFirstTemp = meterWorkRailwayActFirstMapper.selectAndMatch(tareFirst.getActualFirstNo(), predictionNo);
        BeanUtils.copyProperties(tareFirstTemp, tareFirst);
        tareFirst.setIsPreSacale("1");
        tareFirst.setValueFlag("2");
        tareFirst.setPredictionType("5");
        tareFirst.setNote("双委托结净");

        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAddPre(grossFirst.getActualFirstNo(), tareFirst.getActualFirstNo(), predictionNo);
        BeanUtils.copyProperties(mwca, actualRow);

        actualRow.setNetTime(new Date());
        actualRow.setNetMode(DbConstants.TYPE);
        actualRow.setUploadFlag("1");
        actualRow.setValueFlag("1");
        actualRow.setIsPreSacale("1");
        actualRow.setPredictionType("5");
        actualRow.setNote("双委托结净");
        actualRow.setNetWeight(grossFirst.getMeterWeight().subtract(tareFirst.getMeterWeight()));
    }

    //一次记录匹配委托
    private void firstMatchPreScale(MeterWorkRailwayActFirst first, PreRailwayScale scale) {
        BeanUtils.copyProperties(scale, first);
        first.setIsPreSacale("1");
        first.setPredictionNo(scale.getPredictionNo());

        scale.setUpdateTime(new Date());
        scale.setUpdateManNo("system");
        scale.setUploadFlag("1");
        scale.setValueFlag("1");
        scale.setMeasureStation(first.getBaseSpotNo());
        scale.setMeasureStationName(first.getBaseSpotName());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doAddNetHZHXTwo(PreRailwayScale scale, MeterWorkRailwayActFirst
            first01, MeterWorkRailwayActFirst first02, MeterMonitorNote note, String flag) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        HashMap<String, Object> map = new HashMap<>();
        List<MeterWorkRailwayActFirst> firsts = new ArrayList<>();
        List<MeterWorkRailwayActual> actuals = new ArrayList<>();
        try {
            String scaleNo = "";
            //从勾选的委托、两个一次数据中赋值委托号
            if(scale != null && scale.getPredictionNo() != null && StringUtils.isNotEmpty(scale.getPredictionNo())){
                if(first01.getPredictionNo() != null && StringUtils.isNotEmpty(first01.getPredictionNo()) && !first01.getPredictionNo().equals("混装混卸复制")) {
                    if(!scale.getPredictionNo().equals(first01.getPredictionNo())){
                        rm.setMessage("一次计量数据与勾选中的委托号不一致,请确认！！");
                        return rm;
                    }
                }
                if(first02.getPredictionNo() != null && StringUtils.isNotEmpty(first02.getPredictionNo()) &&!first02.getPredictionNo().equals("混装混卸复制")) {
                    if(!scale.getPredictionNo().equals(first02.getPredictionNo())){
                        rm.setMessage("一次计量数据与勾选中的委托号不一致,请确认！！");
                        return rm;
                    }
                }
                scaleNo = scale.getPredictionNo();
            }else if((first01.getPredictionNo() != null && StringUtils.isNotEmpty(first01.getPredictionNo()) && !first01.getPredictionNo().equals("混装混卸复制"))){
                scaleNo = first01.getPredictionNo();
            }else if(first02.getPredictionNo() != null && StringUtils.isNotEmpty(first02.getPredictionNo()) && !first02.getPredictionNo().equals("混装混卸复制")){
                scaleNo = first02.getPredictionNo();
            }
            scale = preRailwayScaleMapper.selectByPrimaryKey(scaleNo);


            if (StringUtils.isEmpty(scale.getPredictionNo())) {
                //获取两个一次重量的差值
                BigDecimal num01 = first01.getMeterWeight().subtract(first02.getMeterWeight()).abs();
                //判断两个一次车辆是否一致
                if (!first01.getRailwayNo().equals(first02.getRailwayNo())) {
                    rm.setMessage("两条一次计量数据车号不一致,请确认！！");
                    return rm;
                }
                //结净数据不能小于400
                /*if(num01.compareTo(BigDecimal.valueOf(400)) == -1){
                    rm.setMessage("选择的两条一次计量数据不能小于400kg，请确认！！");
                    return rm;
                }*/
                //一次数据是否为有效
                if (first01.getValueFlag().equals("2") || first01.getValueFlag().equals("0")) {
                    rm.setMessage("选择的一次计量数据已作废或者已结净，请确认！！");
                    return rm;
                }

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

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

                MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();
                MeterWorkRailwayActFirst model01 = new MeterWorkRailwayActFirst();
                MeterWorkRailwayActFirst model02 = new MeterWorkRailwayActFirst();

                //判断当前一次重量类型、0：毛重，则为混装操作、1：常规皮重，则为混卸操作
                if (first01.getWeightType().equals("1")) {      //混装
                    //毛重一次
                    model01 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first02.getActualFirstNo());
                    //皮重一次
                    model02 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first01.getActualFirstNo());

                    //修改委托状态
                    //将委托中的编号和收发货的等数据拷贝进一次数据中
                    /*MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first1, model01);
                    MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(model02.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first2, model02);*/

                    //修改毛重一次数据状态
                    model01.setValueFlag(DbConstants.NET);
                    model01.setUpdateManNo("system");
                    model01.setUpdateManName("智能计量混装结净更新");
                    model01.setUpdateTime(new Date());
                    model01.setWeightType("0");
                    model01.setIsPreSacale("0");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model01);
                    //修改皮重一次数据状态
                    model02.setValueFlag(DbConstants.NET);
                    model02.setUpdateManNo("system");
                    model02.setUpdateManName("智能计量混装结净更新");
                    model02.setUpdateTime(new Date());
                    model02.setWeightType("1");
                    model02.setIsPreSacale("0");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model02);

                    MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAdd(model01.getActualFirstNo(), model02.getActualFirstNo());
                    BeanUtils.copyProperties(mwca1, actualRow);
                    //保存结净
//                    String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    String no = PublicMethod.IdCreate(3);
                    actualRow.setActualNo(no);
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("智能计量混装匹配结净");
                    actualRow.setNetMode("4");
                    actualRow.setUploadFlag("1");
                    actualRow.setValueFlag("1");
                    actualRow.setIsPreSacale("0");
                    actualRow.setNetWeight(actualRow.getGrossWeight().subtract(actualRow.getTareWeight()));
                    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());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                    }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                    }
                    meterWorkRailwayActualMapper.insertSelective(actualRow);
                    //修改委托编号
                    scale.setValueFlag(DbConstants.NET);
                    scale.setUpdateTime(new Date());
                    scale.setUpdateManNo("system");
                    scale.setUpdateManName("智能计量混装结净更新");
                    scale.setUploadFlag("1");
                    scale.setPredictionType("7");
                    scale.setMeasureStation(actualRow.getNetSpot3No());
                    scale.setMeasureStationName(actualRow.getNetSpot3Name());
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
                } else if (first01.getWeightType().equals("0")) {         //混卸
                    //毛重一次
                    model01 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first01.getActualFirstNo());
                    //皮重一次
                    model02 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first02.getActualFirstNo());

                    //修改委托状态
                    //将委托中的编号和收发货的等数据拷贝进一次数据中
                    /*MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first1, model01);
                    MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(model02.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first2, model02);*/

                    //修改毛重一次数据状态
                    model01.setValueFlag(DbConstants.NET);
                    model01.setUpdateManNo("system");
                    model01.setUpdateManName("智能计量混卸结净更新");
                    model01.setUpdateTime(new Date());
                    model01.setWeightType("0");
                    model01.setIsPreSacale("0");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model01);
                    //修改皮重一次数据状态
                    model02.setValueFlag(DbConstants.NET);
                    model02.setUpdateManNo("system");
                    model02.setUpdateManName("智能计量混卸结净更新");
                    model02.setUpdateTime(new Date());
                    model02.setWeightType("1");
                    model02.setIsPreSacale("0");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model02);

                    MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAdd(model01.getActualFirstNo(), model02.getActualFirstNo());
                    mwca1.setPredictionNo("");
                    BeanUtils.copyProperties(mwca1, actualRow);

                    //保存结净
//                    String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    String no = PublicMethod.IdCreate(3);
                    actualRow.setActualNo(no);
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("智能计量混卸匹配结净");
                    actualRow.setNetMode("6");
                    actualRow.setUploadFlag("1");
                    actualRow.setValueFlag("1");
                    actualRow.setIsPreSacale("0");
                    actualRow.setNetWeight(actualRow.getGrossWeight().subtract(actualRow.getTareWeight()));
                    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());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                    }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                    }
                    meterWorkRailwayActualMapper.insertSelective(actualRow);
                    //修改委托编号
                    scale.setValueFlag(DbConstants.NET);
                    scale.setUpdateTime(new Date());
                    scale.setUpdateManNo("system");
                    scale.setUpdateManName("智能计量混卸结净更新");
                    scale.setUploadFlag("1");
                    scale.setPredictionType("6");
                    scale.setMeasureStation(actualRow.getNetSpot3No());
                    scale.setMeasureStationName(actualRow.getNetSpot3Name());
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
                }

                //存储当前计量的数据
                if (flag.equals("0")) {
                    if (first01.getWeightType().equals("0")) {
                        first02.setUpdateManNo("system");
                        first02.setUpdateManName("智能计量混卸一次复制");
                        firsts.add(model02);
                    } else {
                        first02.setUpdateManNo("system");
                        first02.setUpdateManName("智能计量混装一次复制");
                        firsts.add(model01);
                        doSaveTare(model02);
                    }
//                    String firstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//                    first02.setActualFirstNo(firstNo);
                    String firstNo = PublicMethod.IdCreate(3);
                    first02.setActualFirstNo(firstNo);
                    first02.setPredictionNo("混装混卸复制");
                    first02.setValueFlag("1");
                    //first02.setCreateTime(new Date());
                    meterWorkRailwayActFirstMapper.insertSelective(first02);
                } else {
                    model01.setPredictionNo("");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model01);
                    model02.setPredictionNo("");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model02);
                    if(first01.getWeightType().equals("1")){
                    }else {
                        doSaveTare(model02);
                    }
                }

                firsts.add(first02);
                actuals.add(actualRow);
                map.put("firsts", firsts);
                map.put("actuals", actuals);

                rm.setSucceed();
                rm.setData(map);
                rm.setCode("200");
                rm.setMessage("操作成功！！");
                return rm;
            } else {
                if (scale == null) {
                    rm.setMessage("未找到相应的委托信息,请确认！！");
                    return rm;
                } else {
                    //委托信息是否为有效数据
                    if (scale.getValueFlag().equals("2") || scale.getValueFlag().equals("3")) {
                        rm.setMessage("委托信息已使用或者已作废！！");
                        return rm;
                    }
                }
                //获取两个一次重量的差值
                BigDecimal num01 = first01.getMeterWeight().subtract(first02.getMeterWeight()).abs();
                //判断两个一次车辆是否一致
                if (!first01.getRailwayNo().equals(first02.getRailwayNo())) {
                    rm.setMessage("两条一次计量数据车号不一致,请确认！！");
                    return rm;
                }
                //结净数据不能小于400
                /*if(num01.compareTo(BigDecimal.valueOf(400)) == -1){
                    rm.setMessage("选择的两条一次计量数据不能小于400kg，请确认！！");
                    return rm;
                }*/
                //一次数据是否为有效
                if (first01.getValueFlag().equals("2") || first01.getValueFlag().equals("0")) {
                    rm.setMessage("选择的一次计量数据已作废或者已结净，请确认！！");
                    return rm;
                }

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

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

                MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();
                MeterWorkRailwayActFirst model01 = new MeterWorkRailwayActFirst();
                MeterWorkRailwayActFirst model02 = new MeterWorkRailwayActFirst();

                //判断当前一次重量类型、0：毛重，则为混装操作、1：常规皮重，则为混卸操作
                if (first01.getWeightType().equals("1")) {      //混装
                    //毛重一次
                    model01 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first02.getActualFirstNo());
                    //皮重一次
                    model02 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first01.getActualFirstNo());

                    //修改委托状态
                    //将委托中的编号和收发货的等数据拷贝进一次数据中
                    MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first1, model01);
                    MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(model02.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first2, model02);

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

                    MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(model01.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(mwca1, actualRow);
                    //保存结净
//                    String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    String no = PublicMethod.IdCreate(3);
                    actualRow.setActualNo(no);
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("智能计量混装匹配结净");
                    actualRow.setNetMode("4");
                    actualRow.setUploadFlag("1");
                    actualRow.setValueFlag("1");
                    actualRow.setIsPreSacale("1");
                    actualRow.setPredictionNo(scale.getPredictionNo());
                    actualRow.setPredictionType("7");
                    actualRow.setNetWeight(model01.getMeterWeight().subtract(model02.getMeterWeight()));
                    if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                        actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                        actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                        actualRow.setNetManNo(actualRow.getGrossManNo());
                        actualRow.setNetManName(actualRow.getGrossManName());
                        actualRow.setNetTime(actualRow.getGrossTime());
                        actualRow.setCreateTime(actualRow.getGrossTime());
                        actualRow.setCreateManName(actualRow.getGrossManName());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                    }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                    }
                    meterWorkRailwayActualMapper.insertSelective(actualRow);

                    //修改委托编号
                    scale.setValueFlag(DbConstants.NET);
                    scale.setUpdateTime(new Date());
                    scale.setUpdateManNo("system");
                    scale.setUpdateManName("智能计量混装结净更新");
                    scale.setUploadFlag("1");
                    scale.setPredictionType("7");
                    scale.setMeasureStation(actualRow.getNetSpot3No());
                    scale.setMeasureStationName(actualRow.getNetSpot3Name());
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);

                } else if (first01.getWeightType().equals("0")) {         //混卸
                    //毛重一次
                    model01 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first01.getActualFirstNo());
                    //皮重一次
                    model02 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first02.getActualFirstNo());

                    //修改委托状态
                    //将委托中的编号和收发货的等数据拷贝进一次数据中
                    MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first1, model01);
                    MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(model02.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first2, model02);

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

                    MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(model01.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(mwca1, actualRow);

                    //保存结净
//                    String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    String no = PublicMethod.IdCreate(3);
                    actualRow.setActualNo(no);
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("智能计量混卸匹配结净");
                    actualRow.setNetMode("5");
                    actualRow.setUploadFlag("1");
                    actualRow.setValueFlag("1");
                    actualRow.setIsPreSacale("1");
                    actualRow.setPredictionNo(scale.getPredictionNo());
                    actualRow.setPredictionType("6");
                    actualRow.setNetWeight(model01.getMeterWeight().subtract(model02.getMeterWeight()));
                    if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                        actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                        actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                        actualRow.setNetManNo(actualRow.getGrossManNo());
                        actualRow.setNetManName(actualRow.getGrossManName());
                        actualRow.setNetTime(actualRow.getGrossTime());
                        actualRow.setCreateTime(actualRow.getGrossTime());
                        actualRow.setCreateManName(actualRow.getGrossManName());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                    }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                    }
                    meterWorkRailwayActualMapper.insertSelective(actualRow);

                    //修改委托编号
                    scale.setValueFlag(DbConstants.NET);
                    scale.setUpdateTime(new Date());
                    scale.setUpdateManNo("system");
                    scale.setUpdateManName("智能计量混卸结净更新");
                    scale.setUploadFlag("1");
                    scale.setPredictionType("6");
                    scale.setMeasureStation(actualRow.getNetSpot3No());
                    scale.setMeasureStationName(actualRow.getNetSpot3Name());
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
                }
                //存储当前计量的数据
                if (flag.equals("0")) {
                    if (first01.getWeightType().equals("0")) {
                        first02.setUpdateManNo("system");
                        first02.setUpdateManName("智能计量混卸一次复制");
                        firsts.add(model02);
                    } else {
                        first02.setUpdateManNo("system");
                        first02.setUpdateManName("智能计量混装一次复制");
                        firsts.add(model01);
                        doSaveTare(model02);
                    }
//                    String firstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//                    first02.setActualFirstNo(firstNo);
                    String firstNo = PublicMethod.IdCreate(3);
                    first02.setActualFirstNo(firstNo);
                    first02.setPredictionNo("混装混卸复制");
                    first02.setValueFlag("1");
                    //first02.setCreateTime(new Date());
                    meterWorkRailwayActFirstMapper.insertSelective(first02);
                    firsts.add(first02);
                }else {
                    if(first01.getWeightType().equals("1")){
                        firsts.add(model01);
                    }else {
                        firsts.add(model02);
                        doSaveTare(model02);
                    }
                }
                actuals.add(actualRow);
                map.put("firsts", firsts);
                map.put("actuals", actuals);

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doNetAddTwoDouble(PreRailwayScale scale01, PreRailwayScale
            scale02, MeterWorkRailwayActFirst first01, MeterWorkRailwayActFirst first02) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        HashMap<String, Object> map = new HashMap<>();
        List<MeterWorkRailwayActFirst> firsts = new ArrayList<>();
        List<MeterWorkRailwayActual> actuals = new ArrayList<>();
        try {
            if (scale01 == null || scale02 == null) {
                rm.setMessage("未找到相应的委托信息,请确认！！");
                return rm;
            } else {
                //委托信息是否为有效数据
                if (scale01.getValueFlag().equals("2") || scale02.getValueFlag().equals("2")) {
                    rm.setMessage("委托信息已使用！！");
                    return rm;
                }
                if (scale01.getValueFlag().equals("3") || scale02.getValueFlag().equals("3")) {
                    rm.setMessage("委托信息已作废！！");
                    return rm;
                }
                //判断双委托 物料相同、A委托的收货单位是B委托的发货单位 或者 B委托的收货单位是A委托的发货货单位
                if (isDoubleEntrust(rm, scale01, scale02)) return rm;
            }
            //获取两个一次重量的差值
            BigDecimal num01 = first01.getMeterWeight().subtract(first02.getMeterWeight()).abs();
            //判断两个一次车辆是否一致
            if (!first01.getRailwayNo().equals(first02.getRailwayNo())) {
                rm.setMessage("两条一次计量数据车号不一致,请确认！！");
                return rm;
            }
            //结净数据不能小于400
            /*if(num01.compareTo(BigDecimal.valueOf(400)) == -1){
                rm.setMessage("选择的两条一次计量数据不能小于400kg，请确认！！");
                return rm;
            }*/
            //一次数据是否为有效
            if (first01.getValueFlag().equals("2") || first01.getValueFlag().equals("0")) {
                rm.setMessage("选择的一次计量数据已作废或者已结净，请确认！！");
                return rm;
            }

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

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

            MeterWorkRailwayActFirst model01 = new MeterWorkRailwayActFirst();
            MeterWorkRailwayActFirst model02 = new MeterWorkRailwayActFirst();

            MeterWorkRailwayActual actualRow1 = new MeterWorkRailwayActual();
            MeterWorkRailwayActual actualRow2 = new MeterWorkRailwayActual();
            if (first01.getWeightType().equals("0")) {
                //毛重一次
                model01 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first01.getActualFirstNo());
                //皮重一次
                model02 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first02.getActualFirstNo());
            } else if (first01.getWeightType().equals("1")) {
                //毛重一次
                model01 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first02.getActualFirstNo());
                //皮重一次
                model02 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first01.getActualFirstNo());
            }

            MeterWorkRailwayActFirst model03 = new MeterWorkRailwayActFirst();
            MeterWorkRailwayActFirst model04 = new MeterWorkRailwayActFirst();

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

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

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

            //添加结净数据
//            String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//            actualRow1.setActualNo(sdf.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            actualRow1.setActualNo(no);
            actualRow1.setNetTime(new Date());
            actualRow1.setNetMode(DbConstants.TYPE);
            actualRow1.setNote("智能计量双委托匹配结净");
            actualRow1.setNetMode("3");
            actualRow1.setUploadFlag("1");
            actualRow1.setValueFlag("1");
            actualRow1.setIsPreSacale("1");
            actualRow1.setPredictionNo(scale01.getPredictionNo());
            actualRow1.setPredictionNo(scale01.getPredictionNo());
            actualRow1.setPredictionType("5");
            actualRow1.setPredictionCombination(scale01.getPredictionNo() + "," + scale02.getPredictionNo());
            actualRow1.setNetWeight(model01.getMeterWeight().subtract(model02.getMeterWeight()));
            if(actualRow1.getGrossTime().compareTo(actualRow1.getTareTime()) >= 0) {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot1No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot1Name());
                actualRow1.setNetManNo(actualRow1.getGrossManNo());
                actualRow1.setNetManName(actualRow1.getGrossManName());
                actualRow1.setNetTime(actualRow1.getGrossTime());
                actualRow1.setCreateTime(actualRow1.getGrossTime());
                actualRow1.setCreateManName(actualRow1.getGrossManName());
                actualRow1.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow1.getActualFirst1No()));
            }else {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot2No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot2Name());
                actualRow1.setNetManNo(actualRow1.getTareManNo());
                actualRow1.setNetManName(actualRow1.getTareManName());
                actualRow1.setNetTime(actualRow1.getTareTime());
                actualRow1.setCreateTime(actualRow1.getTareTime());
                actualRow1.setCreateManName(actualRow1.getTareManName());
                actualRow1.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow1.getActualFirst2No()));
            }
            meterWorkRailwayActualMapper.insertSelective(actualRow1);

//            String no1 = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//            actualRow2.setActualNo(sdf.format(new Date()) + no1);
            String no1 = PublicMethod.IdCreate(3);
            actualRow2.setActualNo(no1);
            actualRow2.setNetTime(new Date());
            actualRow2.setNetMode(DbConstants.TYPE);
            actualRow2.setNote("智能计量双委托匹配结净");
            actualRow2.setNetMode("3");
            actualRow2.setValueFlag("1");
            actualRow2.setUploadFlag("1");
            actualRow2.setIsPreSacale("1");
            actualRow2.setPredictionNo(scale02.getPredictionNo());
            actualRow2.setPredictionType("5");
            actualRow2.setPredictionCombination(scale01.getPredictionNo() + "," + scale02.getPredictionNo());
            actualRow2.setNetWeight(model03.getMeterWeight().subtract(model04.getMeterWeight()));
            if(actualRow2.getGrossTime().compareTo(actualRow2.getTareTime()) >= 0) {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot1No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot1Name());
                actualRow2.setNetManNo(actualRow2.getGrossManNo());
                actualRow2.setNetManName(actualRow2.getGrossManName());
                actualRow2.setNetTime(actualRow2.getGrossTime());
                actualRow2.setCreateTime(actualRow2.getGrossTime());
                actualRow2.setCreateManName(actualRow2.getGrossManName());
                actualRow2.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow2.getActualFirst1No()));
            }else {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot2No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot2Name());
                actualRow2.setNetManNo(actualRow2.getTareManNo());
                actualRow2.setNetManName(actualRow2.getTareManName());
                actualRow2.setNetTime(actualRow2.getTareTime());
                actualRow2.setCreateTime(actualRow2.getTareTime());
                actualRow2.setCreateManName(actualRow2.getTareManName());
                actualRow2.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow2.getActualFirst2No()));
            }
            meterWorkRailwayActualMapper.insertSelective(actualRow2);

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

            scale02.setValueFlag(DbConstants.NET);
            scale02.setUpdateTime(new Date());
            scale02.setUpdateManNo("system");
            scale02.setUpdateManName("智能计量双委托结净更新");
            scale02.setUploadFlag("1");
            scale02.setPredictionType("5");
            scale02.setPredictionCombination(scale01.getPredictionNo() + "," + scale02.getPredictionNo());
            scale01.setMeasureStation(actualRow2.getNetSpot3No());
            scale01.setMeasureStationName(actualRow2.getNetSpot3Name());
            preRailwayScaleMapper.updateByPrimaryKeySelective(scale02);

            /*//11、修改计量监控
            updateMonitor(one.getBaseSpotNo());
            //12、当存储一次数据或者结净时，将本车辆的预处理重量改为已使用
            updatePreWeight(one.getCarNo());
            updateMonitorOver(one.getActualFirstNo(), one.getBaseSpotNo());
            //补充附加重量的数据
            if (StringUtils.isNotEmpty(one.getAddWgtPk())) {
                insertAddWgtNo(one.getActualFirstNo(), one.getAddWgtPk(), one.getCreateManNo(), one.getCreateManName());
            }*/
            firsts.add(model01);
            firsts.add(model02);
            actuals.add(actualRow1);
            actuals.add(actualRow2);
            map.put("firsts", firsts);
            map.put("actuals", actuals);

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String doubleEntrustMatch(String actualNo1, String actualNo2, String predictionNo1, String predictionNo2) {

        try {
            String userId = "";
            String userName = "";
            try{
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch (Exception e) {
                userId = "admin";
                userName = "admins";
            }
            //查询出一次计量数据
            MeterWorkRailwayActFirst mwcaf01 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirst mwcaf02 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirst mwcaf03 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirst mwcaf04 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo2);
            //查询出所有委托信息
            PreRailwayScale pts01 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo1);
            PreRailwayScale pts02 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo2);
            //定义两个结净类
            MeterWorkRailwayActual actualRow1 = new MeterWorkRailwayActual();
            MeterWorkRailwayActual actualRow2 = new MeterWorkRailwayActual();
            //获取两个一次重量的差值
            BigDecimal num01 = mwcaf01.getMeterWeight().subtract(mwcaf03.getMeterWeight());
            if (mwcaf01 == null || mwcaf03 == null) {
                return "未找到相应的一次计量信息,请确认！!";
            }
            if (pts01 == null || pts02 == null) {
                return "未找到相应的委托信息,请确认！！";
            }
            //判断毛重必须大于皮重
            if (num01.compareTo(BigDecimal.valueOf(0)) == -1) {
                return "请根据一次数据从大到小的规则，依次选择两条一次数据！！";
            }
            //判断双委托 物料相同、A委托的收货单位是B委托的发货单位 或者 B委托的收货单位是A委托的发货单位
//            if (!(pts01.getMatterNo().equals(pts02.getMatterNo()) &&
//                    (pts01.getReceivingUintNo().equals(pts02.getForwardingUnitNo()) ||
//                            pts01.getForwardingUnitNo().equals(pts02.getReceivingUintNo())))) {
//                return "委托信息不符合双委托规则，请确认!!";
//            }
            if (!(pts01.getReceivingUintNo().equals(pts02.getForwardingUnitNo()) ||
                            pts01.getForwardingUnitNo().equals(pts02.getReceivingUintNo()))) {
                return "委托信息不符合双委托规则，请确认!!";
            }

            if (!mwcaf01.getRailwayNo().equals(mwcaf03.getRailwayNo())) {
                return "两条一次计量数据车号不一致,请确认！！";
            }
            //委托信息是否为有效数据
            if (pts01.getValueFlag().equals("2") || pts02.getValueFlag().equals("2")) {
                return "委托信息已使用！！";
            }
            if (pts01.getValueFlag().equals("3") || pts02.getValueFlag().equals("3")) {
                return "委托信息已作废！！";
            }
            //一次数据是否为有效
            if (mwcaf01.getValueFlag().equals("2") || mwcaf03.getValueFlag().equals("2")) {
                return "一次计量数据已结净，请确认！！";
            }
            if (mwcaf01.getValueFlag().equals("0") || mwcaf03.getValueFlag().equals("0")) {
                return "一次计量数据已作废，请确认！！";
            }
            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo1, predictionNo1);
//            BeanUtils.copyProperties(first1, mwcaf01);
            MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo2, predictionNo1);
//            BeanUtils.copyProperties(first2, mwcaf02);
            MeterWorkRailwayActFirst first3 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo1, predictionNo2);
//            BeanUtils.copyProperties(first3, mwcaf03);
            MeterWorkRailwayActFirst first4 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo2, predictionNo2);
//            BeanUtils.copyProperties(first4, mwcaf04);

            //更新一次数据
            //更新第一次毛重数据库
            first1.setValueFlag(DbConstants.NET);
            first1.setUpdateManNo(userId);
            first1.setUpdateManName(userName);
            first1.setUpdateTime(new Date());
            first1.setIsPreSacale("1");
            first1.setWeightType("0");
            first1.setPredictionNo(predictionNo1);
            first1.setPredictionType("5");
            first1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(first1);
            //更新第一次皮重数据库
            first2.setValueFlag(DbConstants.NET);
            first2.setUpdateManNo(userId);
            first2.setUpdateManName(userName);
            first2.setUpdateTime(new Date());
            first2.setIsPreSacale("1");
            first2.setWeightType("1");
            first2.setPredictionNo(predictionNo1);
            first2.setPredictionType("5");
            first2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(first2);
            doSaveTare(first2);
            //添加第一次毛重数据库
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
//            String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//            mwcaf03.setActualFirstNo(actualFirstNo);
            String actualFirstNo = PublicMethod.IdCreate(3);
            first3.setActualFirstNo(actualFirstNo);
            first3.setValueFlag(DbConstants.NET);
            first3.setUpdateManNo(userId);
            first3.setUpdateManName(userName);
            first3.setUpdateTime(new Date());
            first3.setIsPreSacale("1");
            first3.setWeightType("0");
            first3.setPredictionNo(predictionNo2);
            first3.setPredictionType("5");
            first3.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.insertSelective(first3);
            //添加第二次皮重数据库
            SimpleDateFormat sdf01 = new SimpleDateFormat("yyyyMMdd");
            String afl01 = sdf01.format(new Date());
//            String actualSecondNo = sdf01.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl01);
//            mwcaf04.setActualFirstNo(actualSecondNo);
            String actualSecondNo = PublicMethod.IdCreate(3);
            first4.setActualFirstNo(actualSecondNo);
            first4.setValueFlag(DbConstants.NET);
            first4.setUpdateManNo(userId);
            first4.setUpdateManName(userName);
            first4.setUpdateTime(new Date());
            first4.setIsPreSacale("1");
            first4.setWeightType("1");
            first4.setPredictionNo(predictionNo2);
            first4.setPredictionType("5");
            first4.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.insertSelective(first4);
            doSaveTare(first4);
            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(actualNo1, actualNo2, predictionNo1);
            BeanUtils.copyProperties(mwca1, actualRow1);
            MeterWorkRailwayActual mwca2 = meterWorkRailwayActualMapper.manualAddPre(actualFirstNo, actualSecondNo, predictionNo2);
            BeanUtils.copyProperties(mwca2, actualRow2);

            //添加结净数据
//            String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//            actualRow1.setActualNo(sdf.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            actualRow1.setActualNo(no);
            actualRow1.setNetTime(new Date());
            actualRow1.setNetMode(DbConstants.TYPE);
            actualRow1.setNote("双委托匹配结净");
            actualRow1.setMemo("双委托匹配结净");
            actualRow1.setNetMode("3");
            actualRow1.setUploadFlag("1");
            actualRow1.setValueFlag("1");
            actualRow1.setIsPreSacale("1");
            actualRow1.setPredictionNo(predictionNo1);
            actualRow1.setPredictionType("5");
            actualRow1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow1.setNetWeight(first1.getMeterWeight().subtract(first2.getMeterWeight()));
            if(actualRow1.getGrossTime().compareTo(actualRow1.getTareTime()) >= 0) {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot1No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot1Name());
                actualRow1.setNetManNo(actualRow1.getGrossManNo());
                actualRow1.setNetManName(actualRow1.getGrossManName());
                actualRow1.setNetTime(actualRow1.getGrossTime());
                actualRow1.setCreateTime(actualRow1.getGrossTime());
                actualRow1.setCreateManName(actualRow1.getGrossManName());
                actualRow1.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow1.getActualFirst1No()));
            }else {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot2No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot2Name());
                actualRow1.setNetManNo(actualRow1.getTareManNo());
                actualRow1.setNetManName(actualRow1.getTareManName());
                actualRow1.setNetTime(actualRow1.getTareTime());
                actualRow1.setCreateTime(actualRow1.getTareTime());
                actualRow1.setCreateManName(actualRow1.getTareManName());
                actualRow1.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow1.getActualFirst2No()));
            }
            actualRow1.setUpdateManNo(userId);
            actualRow1.setUpdateManName(userName);
            actualRow1.setUpdateTime(new Date());
            meterWorkRailwayActualMapper.insertSelective(actualRow1);

//            String no1 = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//            actualRow2.setActualNo(sdf.format(new Date()) + no1);
            String no1 = PublicMethod.IdCreate(3);
            actualRow2.setActualNo(no1);
            actualRow2.setNetTime(new Date());
            actualRow2.setNetMode(DbConstants.TYPE);
            actualRow2.setNote("双委托匹配结净");
            actualRow2.setMemo("双委托匹配结净");
            actualRow2.setNetMode("3");
            actualRow2.setValueFlag("1");
            actualRow2.setUploadFlag("1");
            actualRow2.setIsPreSacale("1");
            actualRow2.setPredictionNo(predictionNo2);
            actualRow2.setPredictionType("5");
            actualRow2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow2.setNetWeight(first3.getMeterWeight().subtract(first4.getMeterWeight()));
            if(actualRow2.getGrossTime().compareTo(actualRow2.getTareTime()) >= 0) {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot1No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot1Name());
                actualRow2.setNetManNo(actualRow2.getGrossManNo());
                actualRow2.setNetManName(actualRow2.getGrossManName());
                actualRow2.setNetTime(actualRow2.getGrossTime());
                actualRow2.setCreateTime(actualRow2.getGrossTime());
                actualRow2.setCreateManName(actualRow2.getGrossManName());
                actualRow2.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow2.getActualFirst1No()));
            }else {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot2No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot2Name());
                actualRow2.setNetManNo(actualRow2.getTareManNo());
                actualRow2.setNetManName(actualRow2.getTareManName());
                actualRow2.setNetTime(actualRow2.getTareTime());
                actualRow2.setCreateTime(actualRow2.getTareTime());
                actualRow2.setCreateManName(actualRow2.getTareManName());
                actualRow2.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow2.getActualFirst2No()));
            }
            actualRow2.setUpdateManNo(userId);
            actualRow2.setUpdateManName(userName);
            actualRow2.setUpdateTime(new Date());
            meterWorkRailwayActualMapper.insertSelective(actualRow2);

            //更新预报数据
            PreRailwayScale scale = new PreRailwayScale();
            scale.setPredictionNo(predictionNo1);
            scale.setValueFlag(DbConstants.NET);
            scale.setUpdateTime(new Date());
            scale.setUpdateManNo(userId);
            scale.setUpdateManName(userName);
            scale.setUploadFlag("1");
            scale.setPredictionType("5");
            scale.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale.setMeasureStation(actualRow1.getNetSpot3No());
            scale.setMeasureStationName(actualRow1.getNetSpot3Name());
            preRailwayScaleMapper.updateByPrimaryKeySelective(scale);

            PreRailwayScale scale1 = new PreRailwayScale();
            scale1.setPredictionNo(predictionNo2);
            scale1.setValueFlag(DbConstants.NET);
            scale1.setUpdateTime(new Date());
            scale1.setUpdateManNo(userId);
            scale1.setUpdateManName(userName);
            scale1.setUploadFlag("1");
            scale1.setPredictionType("5");
            scale1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale1.setMeasureStation(actualRow2.getNetSpot3No());
            scale1.setMeasureStationName(actualRow2.getNetSpot3Name());
            preRailwayScaleMapper.updateByPrimaryKeySelective(scale1);

            MeterBaseOperationLog logModel01 = new MeterBaseOperationLog();
            logModel01.setId(PublicMethod.IdCreate(2));
            logModel01.setCreateManNo(userId);
            logModel01.setCreateManName(userName);
            logModel01.setCreateTime(new Date());
            logModel01.setOperationPage("web端-轨道衡一次数据维护");
            logModel01.setOperationFunction("双委托结净");
            logModel01.setOperationId(actualRow1.getActualNo());
            logModel01.setOperationPredictionNo(predictionNo1 + "+" + predictionNo2);
            logModel01.setOperationBeforeFix(actualRow1.getActualFirst1No() + "," + actualRow1.getActualFirst2No());
            logModel01.setOperationCarnNo(actualRow1.getRailwayNo());
            meterBaseOperationLogMapper.insertSelective(logModel01);

            MeterBaseOperationLog logModel02 = new MeterBaseOperationLog();
            logModel02.setId(PublicMethod.IdCreate(2));
            logModel02.setCreateManNo(userId);
            logModel02.setCreateManName(userName);
            logModel02.setCreateTime(new Date());
            logModel02.setOperationPage("web端-轨道衡一次数据维护");
            logModel02.setOperationFunction("双委托结净");
            logModel02.setOperationId(actualRow2.getActualNo());
            logModel02.setOperationPredictionNo(predictionNo1 + "+" + predictionNo2);
            logModel02.setOperationBeforeFix(actualRow2.getActualFirst1No() + "," + actualRow2.getActualFirst2No());
            logModel02.setOperationCarnNo(actualRow2.getRailwayNo());
            meterBaseOperationLogMapper.insertSelective(logModel02);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请确认";
        }
        return "";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String doubleEntrustAiWeightMatch(String actualNo1, BigDecimal tareWeight, String predictionNo1, String predictionNo2) {
        try {

            String userId = "";
            String userName = "";
            try{
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch (Exception e) {
                userId = "admin";
                userName = "admins";
            }
            //查询出一次计量数据

            //查询出一次计量数据
            MeterWorkRailwayActFirst mwcaf01 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirst mwcaf02 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirst mwcaf03 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirst mwcaf04 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo1);
            //查询出所有委托信息
            PreRailwayScale pts01 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo1);
            PreRailwayScale pts02 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo2);
            //定义两个结净类
            MeterWorkRailwayActual actualRow1 = new MeterWorkRailwayActual();
            MeterWorkRailwayActual actualRow2 = new MeterWorkRailwayActual();
            if (mwcaf01 == null) {
                return "未找到相应的一次计量信息,请确认！!";
            }
            if (pts01 == null || pts02 == null) {
                return "未找到相应的委托信息,请确认！！";
            }

            //获取两个一次重量的差值
            BigDecimal num01 = mwcaf01.getMeterWeight().subtract(tareWeight);
            BigDecimal num02 = mwcaf01.getMeterWeight();
            //判断毛重必须大于皮重
            if (num01.compareTo(BigDecimal.valueOf(0)) == -1) {
                return "输入的标识皮重大于所选择的一次计量数据，请重新操作！！";
            }
            //一次数据是否为有效
            if (mwcaf01.getValueFlag().equals("2")) {
                return "一次计量数据已结净，请确认！！";
            }
            if (mwcaf01.getValueFlag().equals("0")) {
                return "一次计量数据已作废，请确认！！";
            }
            //判断双委托 物料相同、A委托的收货单位是B委托的发货单位 或者 B委托的收货单位是A委托的发货单位
//            if (!(pts01.getMatterNo().equals(pts02.getMatterNo()) &&
//                    (pts01.getReceivingUintNo().equals(pts02.getForwardingUnitNo()) ||
//                            pts01.getForwardingUnitNo().equals(pts02.getReceivingUintNo())))) {
//                return "委托信息不符合双委托规则，请确认!!";
//            }
            if (!(pts01.getReceivingUintNo().equals(pts02.getForwardingUnitNo()) ||
                    pts01.getForwardingUnitNo().equals(pts02.getReceivingUintNo()))) {
                return "委托信息不符合双委托规则，请确认!!";
            }
            //委托信息是否为有效数据
            if (pts01.getValueFlag().equals("2") || pts02.getValueFlag().equals("2")) {
                return "委托信息已使用！！";
            }
            if (pts01.getValueFlag().equals("3") || pts02.getValueFlag().equals("3")) {
                return "委托信息已作废！！";
            }
            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo1, predictionNo1);
            BeanUtils.copyProperties(first1, mwcaf01);
            MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo1, predictionNo1);
            BeanUtils.copyProperties(first2, mwcaf02);
            MeterWorkRailwayActFirst first3 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo1, predictionNo2);
            BeanUtils.copyProperties(first3, mwcaf03);
            MeterWorkRailwayActFirst first4 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo1, predictionNo2);
            BeanUtils.copyProperties(first4, mwcaf04);

            //更新一次数据
            //更新第一次毛重数据库
            mwcaf01.setValueFlag(DbConstants.NET);
            mwcaf01.setUpdateManNo(userId);
            mwcaf01.setUpdateManName(userName);
            mwcaf01.setUpdateTime(new Date());
            mwcaf01.setIsPreSacale("1");
            mwcaf01.setWeightType("0");
            mwcaf01.setPredictionNo(predictionNo1);
            mwcaf01.setPredictionType("5");
            mwcaf01.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(mwcaf01);
            //更新第一次皮重数据库
            String tareNo1 = PublicMethod.IdCreate(3);
            mwcaf02.setActualFirstNo(tareNo1);
            mwcaf02.setValueFlag(DbConstants.NET);
            mwcaf02.setMeterWeight(tareWeight);
            mwcaf02.setCreateManNo(userId);
            mwcaf02.setCreateManName(userName);
            mwcaf02.setCreateTime(new Date());
            mwcaf02.setUpdateManNo(userId);
            mwcaf02.setUpdateManName(userName);
            mwcaf02.setUpdateTime(new Date());
            mwcaf02.setIsPreSacale("1");
            mwcaf02.setWeightType("1");
            mwcaf02.setPredictionNo(predictionNo1);
            mwcaf02.setPredictionType("5");
            mwcaf02.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.insertSelective(mwcaf02);
            doSaveTare(mwcaf02,"2");

            //添加第一次毛重数据库
            String grossNo1 = PublicMethod.IdCreate(3);
            mwcaf03.setActualFirstNo(grossNo1);
            mwcaf03.setValueFlag(DbConstants.NET);
            mwcaf03.setMeterWeight(num02);
            mwcaf03.setCreateManNo(userId);
            mwcaf03.setCreateManName(userName);
            mwcaf03.setCreateTime(new Date());
            mwcaf03.setUpdateManNo(userId);
            mwcaf03.setUpdateManName(userName);
            mwcaf03.setUpdateTime(new Date());
            mwcaf03.setIsPreSacale("1");
            mwcaf03.setWeightType("0");
            mwcaf03.setPredictionNo(predictionNo2);
            mwcaf03.setPredictionType("5");
            mwcaf03.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.insertSelective(mwcaf03);
            //添加第二次皮重数据库
            String tareNo2 = PublicMethod.IdCreate(3);
            mwcaf04.setActualFirstNo(tareNo2);
            mwcaf04.setValueFlag(DbConstants.NET);
            mwcaf04.setMeterWeight(tareWeight);
            mwcaf04.setCreateManNo(userId);
            mwcaf04.setCreateManName(userName);
            mwcaf04.setCreateTime(new Date());
            mwcaf04.setUpdateManNo(userId);
            mwcaf04.setUpdateManName(userName);
            mwcaf04.setUpdateTime(new Date());
            mwcaf04.setIsPreSacale("1");
            mwcaf04.setWeightType("1");
            mwcaf04.setPredictionNo(predictionNo2);
            mwcaf04.setPredictionType("5");
            mwcaf04.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.insertSelective(mwcaf04);
            doSaveTare(mwcaf04,"2");
            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(actualNo1, tareNo1, predictionNo1);
            BeanUtils.copyProperties(mwca1, actualRow1);
            MeterWorkRailwayActual mwca2 = meterWorkRailwayActualMapper.manualAddPre(grossNo1, tareNo2, predictionNo2);
            BeanUtils.copyProperties(mwca2, actualRow2);
            //添加结净数据
            actualRow1.setActualNo(PublicMethod.IdCreate(3));
            actualRow1.setNetTime(new Date());
            actualRow1.setNetMode(DbConstants.TYPE);
            actualRow1.setNote("标识皮双委托结净");
            actualRow1.setMemo("标识皮双委托结净");
            actualRow1.setNetMode("2");
            actualRow1.setUploadFlag("1");
            actualRow1.setValueFlag("1");
            actualRow1.setIsPreSacale("1");
            actualRow1.setPredictionNo(predictionNo1);
            actualRow1.setPredictionType("5");
            actualRow1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow1.setNetWeight(num01);
            if(actualRow1.getGrossTime().compareTo(actualRow1.getTareTime()) >= 0) {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot1No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot1Name());
                actualRow1.setNetManNo(actualRow1.getGrossManNo());
                actualRow1.setNetManName(actualRow1.getGrossManName());
                actualRow1.setNetTime(actualRow1.getGrossTime());
                actualRow1.setCreateTime(actualRow1.getGrossTime());
                actualRow1.setCreateManName(actualRow1.getGrossManName());
                actualRow1.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow1.getActualFirst1No()));
            }else {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot2No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot2Name());
                actualRow1.setNetManNo(actualRow1.getTareManNo());
                actualRow1.setNetManName(actualRow1.getTareManName());
                actualRow1.setNetTime(actualRow1.getTareTime());
                actualRow1.setCreateTime(actualRow1.getTareTime());
                actualRow1.setCreateManName(actualRow1.getTareManName());
                actualRow1.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow1.getActualFirst2No()));
            }
            actualRow1.setUpdateManNo(userId);
            actualRow1.setUpdateManName(userName);
            actualRow1.setUpdateTime(new Date());
            meterWorkRailwayActualMapper.insertSelective(actualRow1);

            actualRow2.setActualNo(PublicMethod.IdCreate(3));
            actualRow2.setNetTime(new Date());
            actualRow2.setNetMode(DbConstants.TYPE);
            actualRow2.setNote("标识皮双委托结净");
            actualRow2.setMemo("标识皮双委托结净");
            actualRow2.setNetMode("2");
            actualRow2.setValueFlag("1");
            actualRow2.setUploadFlag("1");
            actualRow2.setIsPreSacale("1");
            actualRow2.setPredictionNo(predictionNo2);
            actualRow2.setPredictionType("5");
            actualRow2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow2.setNetWeight(num01);
            if(actualRow2.getGrossTime().compareTo(actualRow2.getTareTime()) >= 0) {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot1No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot1Name());
                actualRow2.setNetManNo(actualRow2.getGrossManNo());
                actualRow2.setNetManName(actualRow2.getGrossManName());
                actualRow2.setNetTime(actualRow2.getGrossTime());
                actualRow2.setCreateTime(actualRow2.getGrossTime());
                actualRow2.setCreateManName(actualRow2.getGrossManName());
                actualRow2.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow2.getActualFirst1No()));
            }else {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot2No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot2Name());
                actualRow2.setNetManNo(actualRow2.getTareManNo());
                actualRow2.setNetManName(actualRow2.getTareManName());
                actualRow2.setNetTime(actualRow2.getTareTime());
                actualRow2.setCreateTime(actualRow2.getTareTime());
                actualRow2.setCreateManName(actualRow2.getTareManName());
                actualRow2.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow2.getActualFirst2No()));
            }
            actualRow2.setUpdateManNo(userId);
            actualRow2.setUpdateManName(userName);
            actualRow2.setUpdateTime(new Date());
            meterWorkRailwayActualMapper.insertSelective(actualRow2);

            meterWorkRailwayActFirstMapper.deleteByPrimaryKey(tareNo1);
            meterWorkRailwayActFirstMapper.deleteByPrimaryKey(tareNo2);

            //更新预报数据
            PreRailwayScale scale1 = new PreRailwayScale();
            scale1.setPredictionNo(predictionNo1);
            scale1.setValueFlag(DbConstants.NET);
            scale1.setUpdateTime(new Date());
            scale1.setUpdateManNo(userId);
            scale1.setUpdateManName(userName);
            scale1.setUploadFlag("1");
            scale1.setPredictionType("5");
            scale1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale1.setMeasureStation(actualRow1.getNetSpot3No());
            scale1.setMeasureStationName(actualRow1.getNetSpot3Name());
            preRailwayScaleMapper.updateByPrimaryKeySelective(scale1);

            PreRailwayScale scale2 = new PreRailwayScale();
            scale2.setPredictionNo(predictionNo2);
            scale2.setValueFlag(DbConstants.NET);
            scale2.setUpdateTime(new Date());
            scale2.setUpdateManNo(userId);
            scale2.setUpdateManName(userName);
            scale2.setUploadFlag("1");
            scale2.setPredictionType("5");
            scale2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale2.setMeasureStation(actualRow2.getNetSpot3No());
            scale2.setMeasureStationName(actualRow2.getNetSpot3Name());
            preRailwayScaleMapper.updateByPrimaryKeySelective(scale2);

            MeterBaseOperationLog logModel01 = new MeterBaseOperationLog();
            logModel01.setId(PublicMethod.IdCreate(2));
            logModel01.setCreateManNo(userId);
            logModel01.setCreateManName(userName);
            logModel01.setCreateTime(new Date());
            logModel01.setOperationPage("web端-轨道衡一次数据维护");
            logModel01.setOperationFunction("标识皮双委托结净");
            logModel01.setOperationId(actualRow1.getActualNo());
            logModel01.setOperationPredictionNo(predictionNo1 + "+" + predictionNo2);
            logModel01.setOperationBeforeFix(actualRow1.getActualFirst1No() + "," + actualRow1.getActualFirst2No());
            logModel01.setOperationCarnNo(actualRow1.getRailwayNo());
            meterBaseOperationLogMapper.insertSelective(logModel01);

            MeterBaseOperationLog logModel02 = new MeterBaseOperationLog();
            logModel02.setId(PublicMethod.IdCreate(2));
            logModel02.setCreateManNo(userId);
            logModel02.setCreateManName(userName);
            logModel02.setCreateTime(new Date());
            logModel02.setOperationPage("web端-轨道衡一次数据维护");
            logModel02.setOperationFunction("标识皮双委托结净");
            logModel02.setOperationId(actualRow2.getActualNo());
            logModel02.setOperationPredictionNo(predictionNo1 + "+" + predictionNo2);
            logModel02.setOperationBeforeFix(actualRow2.getActualFirst1No() + "," + actualRow2.getActualFirst2No());
            logModel02.setOperationCarnNo(actualRow2.getRailwayNo());
            meterBaseOperationLogMapper.insertSelective(logModel02);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请确认";
        }
        return "";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String mixedLoading(String actualNo1, String actualNo2, String actualNo3, String predictionNo1, String
            predictionNo2) {
        try {
            String userId = "";
            String userName = "";
            try{
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch (Exception e) {
                userId = "system";
                userName = "system";
            }
            //查询所有的一次数据
            MeterWorkRailwayActFirst mwcaf1 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirst mwcaf2 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirst mwcaf3 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirst mwcaf4 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo3);
            //查询所有的委托数据
            PreRailwayScale pts1 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo1);
            PreRailwayScale pts2 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo2);
            //定义新的结净数据实体
            MeterWorkRailwayActual actualRow1 = new MeterWorkRailwayActual();
            MeterWorkRailwayActual actualRow2 = new MeterWorkRailwayActual();
            //判断是否为混装逻辑
            if (mwcaf1.getMeterWeight().compareTo(mwcaf2.getMeterWeight()) == 1 ||
                    mwcaf1.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == 1 ||
                    mwcaf2.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == 1) {
                return "请根据一次数据从小到大的规则，依次选择三条一次数据！！";
            }

            /**
             * 2023年10月11日zn
             * 混装混卸配置查询
              */
            HashMap <String,Object> queryCon = new HashMap<>();
            queryCon.put("matterNo",pts2.getMatterNo());
            queryCon.put("matterName",pts2.getMatterName());
            queryCon.put("customerSupplierName",pts2.getForwardingUnitName());
            queryCon.put("customerSupplierNo",pts2.getForwardingUnitNo());
            queryCon.put("validFlag","1");
            queryCon.put("isloadUnload","0");
            List<MeterBaseMatterUnitMixedLoadUnloadTime> lists = meterBaseMatterUnitMixedLoadUnloadTimeMapper.selectByParameters(queryCon);
            if(lists!=null&&lists.size()!=0){
                SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String fromDate = simpleFormat.format(pts1.getTrustDateTime());
                String toDate = simpleFormat.format(pts2.getTrustDateTime());
                long from = simpleFormat.parse(fromDate).getTime();
                long to = simpleFormat.parse(toDate).getTime();
                int hours = (int) ((to - from) / (1000 * 60 * 60));
                if (pts1.getMatterNo().equals(pts2.getMatterNo()) &&
                        pts1.getForwardingUnitNo().equals(pts2.getForwardingUnitNo()) &&
                        hours <= lists.get(0).getTimeValue().doubleValue()&&
                        lists.get(0).getTimeValue().doubleValue()>0) {
                    return "委托信息不符合混装委托规则(同物料同发货单位委托创建时间不能小于"+lists.get(0)
                            .getTimeValue().doubleValue()+"小时)，请确认!!";
                }
            }
            //判断一次数据是否存在
            if (mwcaf1 == null || mwcaf2 == null || mwcaf3 == null || mwcaf4 == null) {
                return "未查询到相应的一次数据！！";
            }
            //判断一次数据是否已结净
            if (mwcaf1.getValueFlag().equals("2") || mwcaf2.getValueFlag().equals("2") ||
                    mwcaf3.getValueFlag().equals("2") || mwcaf4.getValueFlag().equals("2")) {
                return "存在已结净的一次数据！！";
            }
            //判断委托数据是否存在
            if (pts1 == null || pts2 == null) {
                return "未查询到相应的委托数据！！";
            }
            //判断委托数据是否已使用
            if (!pts1.getValueFlag().equals("1") || !pts1.getValueFlag().equals("1")) {
                return "委托不是未使用状态！！";
            }
            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo1, predictionNo1);
            BeanUtils.copyProperties(first1, mwcaf1);
            MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo2, predictionNo1);
            BeanUtils.copyProperties(first2, mwcaf2);
            MeterWorkRailwayActFirst first3 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo2, predictionNo2);
            BeanUtils.copyProperties(first3, mwcaf3);
            MeterWorkRailwayActFirst first4 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo3, predictionNo2);
            BeanUtils.copyProperties(first4, mwcaf4);

            //更新一次数据
            //更新第一次皮重数据
            mwcaf1.setValueFlag(DbConstants.NET);
            mwcaf1.setUpdateManNo(userId);
            mwcaf1.setUpdateManName(userName);
            mwcaf1.setUpdateTime(new Date());
            mwcaf1.setIsPreSacale("1");
            mwcaf1.setWeightType("1");
            mwcaf1.setPredictionNo(predictionNo1);
            mwcaf1.setPredictionType("7");
            mwcaf1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(mwcaf1);
            doSaveTare(mwcaf1);
            //更新第一次毛重数据
            mwcaf2.setValueFlag(DbConstants.NET);
            mwcaf2.setUpdateManNo(userId);
            mwcaf2.setUpdateManName(userName);
            mwcaf2.setUpdateTime(new Date());
            mwcaf2.setIsPreSacale("1");
            mwcaf2.setWeightType("0");
            mwcaf2.setPredictionNo(predictionNo1);
            mwcaf2.setPredictionType("7");
            mwcaf2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(mwcaf2);
            //添加第二次皮重数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
//            String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//            mwcaf3.setActualFirstNo(actualFirstNo);
            String actualFirstNo = PublicMethod.IdCreate(3);
            mwcaf3.setActualFirstNo(actualFirstNo);
            mwcaf3.setValueFlag(DbConstants.NET);
            mwcaf3.setUpdateManNo(userId);
            mwcaf3.setUpdateManName(userName);
            mwcaf3.setUpdateTime(new Date());
            mwcaf3.setIsPreSacale("1");
            mwcaf3.setWeightType("1");
            mwcaf3.setPredictionNo(predictionNo2);
            mwcaf3.setPredictionType("7");
            mwcaf3.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.insertSelective(mwcaf3);
            //更新第二次毛重数据
            mwcaf4.setValueFlag(DbConstants.NET);
            mwcaf4.setUpdateManNo(userId);
            mwcaf4.setUpdateManName(userName);
            mwcaf4.setUpdateTime(new Date());
            mwcaf4.setIsPreSacale("1");
            mwcaf4.setWeightType("0");
            mwcaf4.setPredictionNo(predictionNo2);
            mwcaf4.setPredictionType("7");
            mwcaf4.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(mwcaf4);

            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(actualNo2, actualNo1, predictionNo1);
            BeanUtils.copyProperties(mwca1, actualRow1);
            MeterWorkRailwayActual mwca2 = meterWorkRailwayActualMapper.manualAddPre(actualNo3, actualFirstNo, predictionNo2);
            BeanUtils.copyProperties(mwca2, actualRow2);
            //添加结净数据
//            String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//            actualRow1.setActualNo(sdf.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            actualRow1.setActualNo(no);
            actualRow1.setNetTime(new Date());
            actualRow1.setNetMode(DbConstants.TYPE);
            actualRow1.setNote("混装匹配结净");
            actualRow1.setNetMode("4");
            actualRow1.setUploadFlag("1");
            actualRow1.setIsPreSacale("1");
            actualRow1.setValueFlag("1");
            actualRow1.setCreateTime(new Date());
            actualRow1.setCreateManName(userName);
            actualRow1.setPredictionType("7");
            actualRow1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow1.setPredictionNo(predictionNo1);
            actualRow1.setNetWeight(mwcaf2.getMeterWeight().subtract(mwcaf1.getMeterWeight()));
            if(actualRow1.getGrossTime().compareTo(actualRow1.getTareTime()) >= 0) {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot1No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot1Name());
                actualRow1.setNetManNo(actualRow1.getGrossManNo());
                actualRow1.setNetManName(actualRow1.getGrossManName());
                actualRow1.setNetTime(actualRow1.getGrossTime());
                actualRow1.setCreateTime(actualRow1.getGrossTime());
                actualRow1.setCreateManName(actualRow1.getGrossManName());
                actualRow1.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow1.getActualFirst1No()));
            }else {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot2No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot2Name());
                actualRow1.setNetManNo(actualRow1.getTareManNo());
                actualRow1.setNetManName(actualRow1.getTareManName());
                actualRow1.setNetTime(actualRow1.getTareTime());
                actualRow1.setCreateTime(actualRow1.getTareTime());
                actualRow1.setCreateManName(actualRow1.getTareManName());
                actualRow1.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow1.getActualFirst2No()));
            }
            meterWorkRailwayActualMapper.insertSelective(actualRow1);

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

            //更新预报数据
            PreRailwayScale scale = new PreRailwayScale();
            scale.setPredictionNo(predictionNo1);
            scale.setValueFlag(DbConstants.NET);
            scale.setUpdateTime(new Date());
            scale.setUpdateManNo(userId);
            scale.setUpdateManName(userName);
            scale.setPredictionType("7");
            scale.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale.setUploadFlag("1");
            scale.setMeasureStation(actualRow1.getNetSpot3No());
            scale.setMeasureStationName(actualRow1.getNetSpot3Name());
            preRailwayScaleMapper.updateByPrimaryKeySelective(scale);

            PreRailwayScale scale1 = new PreRailwayScale();
            scale1.setPredictionNo(predictionNo2);
            scale1.setValueFlag(DbConstants.NET);
            scale1.setUpdateTime(new Date());
            scale1.setUpdateManNo(userId);
            scale1.setUpdateManName(userName);
            scale1.setPredictionType("7");
            scale1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale1.setUploadFlag("1");
            scale.setMeasureStation(actualRow2.getNetSpot3No());
            scale.setMeasureStationName(actualRow2.getNetSpot3Name());
            preRailwayScaleMapper.updateByPrimaryKeySelective(scale1);

            MeterBaseOperationLog logModel01 = new MeterBaseOperationLog();
            logModel01.setId(PublicMethod.IdCreate(2));
            logModel01.setCreateManNo(userId);
            logModel01.setCreateManName(userName);
            logModel01.setCreateTime(new Date());
            logModel01.setOperationPage("web端-轨道衡一次数据维护");
            logModel01.setOperationFunction("混装委托结净");
            logModel01.setOperationId(actualRow1.getActualNo());
            logModel01.setOperationPredictionNo(predictionNo1 + "+" + predictionNo2);
            logModel01.setOperationBeforeFix(actualNo1 + "+" + actualNo2 + "+" + actualNo3);
            logModel01.setOperationCarnNo(actualRow1.getRailwayNo());
            meterBaseOperationLogMapper.insertSelective(logModel01);

            MeterBaseOperationLog logModel02 = new MeterBaseOperationLog();
            logModel02.setId(PublicMethod.IdCreate(2));
            logModel02.setCreateManNo(userId);
            logModel02.setCreateManName(userName);
            logModel02.setCreateTime(new Date());
            logModel02.setOperationPage("web端-轨道衡一次数据维护");
            logModel02.setOperationFunction("混装委托结净");
            logModel02.setOperationId(actualRow2.getActualNo());
            logModel02.setOperationPredictionNo(predictionNo1 + "+" + predictionNo2);
            logModel02.setOperationBeforeFix(actualNo1 + "+" + actualNo2 + "+" + actualNo3);
            logModel02.setOperationCarnNo(actualRow2.getRailwayNo());
            meterBaseOperationLogMapper.insertSelective(logModel02);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请确认";
        }
        return "";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String mixedDischarge(String actualNo1, String actualNo2, String actualNo3, String predictionNo1, String
            predictionNo2) {
        try {
            String userId = "";
            String userName = "";
            try{
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch (Exception e) {
                userId = "system";
                userName = "system";
            }
            //查询所有的一次数据
            MeterWorkRailwayActFirst mwcaf1 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirst mwcaf2 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirst mwcaf3 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirst mwcaf4 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo3);
            //查询所有的委托数据
            PreRailwayScale pts1 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo1);
            PreRailwayScale pts2 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo2);
            //定义新的结净数据实体
            MeterWorkRailwayActual actualRow1 = new MeterWorkRailwayActual();
            MeterWorkRailwayActual actualRow2 = new MeterWorkRailwayActual();
            //判断是否为混装逻辑
            if (mwcaf1.getMeterWeight().compareTo(mwcaf2.getMeterWeight()) == -1 ||
                    mwcaf1.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == -1 ||
                    mwcaf2.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == -1) {
                return "请根据一次数据从大到小的规则，依次选择三条一次数据！！！！";
            }
            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            String fromDate = simpleFormat.format(pts1.getTrustDateTime());
            String toDate = simpleFormat.format(pts2.getTrustDateTime());
            long from = simpleFormat.parse(fromDate).getTime();
            long to = simpleFormat.parse(toDate).getTime();
            int minutes = (int) ((to - from) / (1000 * 60));
            //判断混卸 物料相同、AB发货单位相同、收货单位一定不一致：签发时间10分钟内
            if (pts1.getMatterNo().equals(pts2.getMatterNo()) &&
                    pts1.getForwardingUnitNo().equals(pts2.getForwardingUnitNo()) &&
                    !pts1.getReceivingUintNo().equals(pts2.getReceivingUintNo()) &&
                    minutes <= 10) {
                return "委托信息不符合混卸委托规则，请确认!!";
            }
            /**
             * 2023年10月11日zn
             * 混装混卸配置查询
             */
            HashMap <String,Object> queryCon = new HashMap<>();
            queryCon.put("matterNo",pts2.getMatterNo());
            queryCon.put("matterName",pts2.getMatterName());
            queryCon.put("customerSupplierName",pts2.getReceivingUintName());
            queryCon.put("customerSupplierNo",pts2.getReceivingUintNo());
            queryCon.put("validFlag","1");
            queryCon.put("isloadUnload","1");
            List<MeterBaseMatterUnitMixedLoadUnloadTime> lists = meterBaseMatterUnitMixedLoadUnloadTimeMapper.selectByParameters(queryCon);
            if(lists!=null&&lists.size()!=0){
                int hours = (int) ((to - from) / (1000 * 60 * 60));
                if (pts1.getMatterNo().equals(pts2.getMatterNo()) &&
                        pts1.getForwardingUnitNo().equals(pts2.getForwardingUnitNo()) &&
                        hours <= lists.get(0).getTimeValue().doubleValue()&&
                        lists.get(0).getTimeValue().doubleValue()>0) {
                    return "委托信息不符合混装委托规则(同物料同收货单位委托创建时间不能小于"+lists.get(0)
                            .getTimeValue().doubleValue()+"小时)，请确认!!";
                }
            }
            //判断一次数据是否存在
            if (mwcaf1 == null || mwcaf2 == null || mwcaf3 == null || mwcaf4 == null) {
                return "未查询到相应的一次数据！！";
            }
            //判断一次数据是否已结净
            if (mwcaf1.getValueFlag().equals("2") || mwcaf2.getValueFlag().equals("2") ||
                    mwcaf3.getValueFlag().equals("2") || mwcaf4.getValueFlag().equals("2")) {
                return "存在已结净的一次数据！！";
            }
            //判断委托数据是否存在
            if (pts1 == null || pts2 == null) {
                return "未查询到相应的委托数据！！";
            }
            //判断委托数据是否已使用
            if (!pts1.getValueFlag().equals("1") || !pts1.getValueFlag().equals("1")) {
                return "委托不是未使用状态！！";
            }
            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo1, predictionNo1);
            BeanUtils.copyProperties(first1, mwcaf1);
            MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo2, predictionNo1);
            BeanUtils.copyProperties(first2, mwcaf2);
            MeterWorkRailwayActFirst first3 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo2, predictionNo2);
            BeanUtils.copyProperties(first3, mwcaf3);
            MeterWorkRailwayActFirst first4 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo3, predictionNo2);
            BeanUtils.copyProperties(first4, mwcaf4);

            //更新一次数据
            //更新第一次毛重数据
            mwcaf1.setValueFlag(DbConstants.NET);
            mwcaf1.setUpdateManNo(userId);
            mwcaf1.setUpdateManName(userName);
            mwcaf1.setUpdateTime(new Date());
            mwcaf1.setIsPreSacale("1");
            mwcaf1.setWeightType("0");
            mwcaf1.setPredictionNo(predictionNo1);
            mwcaf1.setPredictionType("6");
            mwcaf1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(mwcaf1);
            //更新第一次皮重数据
            mwcaf2.setValueFlag(DbConstants.NET);
            mwcaf2.setUpdateManNo(userId);
            mwcaf2.setUpdateManName(userName);
            mwcaf2.setUpdateTime(new Date());
            mwcaf2.setIsPreSacale("1");
            mwcaf2.setWeightType("1");
            mwcaf2.setPredictionNo(predictionNo1);
            mwcaf2.setPredictionType("6");
            mwcaf2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(mwcaf2);
            //添加第二次毛重数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
//            String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//            mwcaf3.setActualFirstNo(actualFirstNo);
            String actualFirstNo = PublicMethod.IdCreate(3);
            mwcaf3.setActualFirstNo(actualFirstNo);
            mwcaf3.setValueFlag(DbConstants.NET);
            mwcaf3.setUpdateManNo(userId);
            mwcaf3.setUpdateManName(userName);
            mwcaf3.setUpdateTime(new Date());
            mwcaf3.setIsPreSacale("1");
            mwcaf3.setWeightType("0");
            mwcaf3.setPredictionNo(predictionNo2);
            mwcaf3.setPredictionType("6");
            mwcaf3.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.insertSelective(mwcaf3);
            //更新第二次皮重数据
            mwcaf4.setValueFlag(DbConstants.NET);
            mwcaf4.setUpdateManNo(userId);
            mwcaf4.setUpdateManName(userName);
            mwcaf4.setUpdateTime(new Date());
            mwcaf4.setIsPreSacale("1");
            mwcaf4.setWeightType("1");
            mwcaf4.setPredictionNo(predictionNo2);
            mwcaf4.setPredictionType("6");
            mwcaf4.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(mwcaf4);
            doSaveTare(mwcaf4);

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

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

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

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

            PreRailwayScale scale1 = new PreRailwayScale();
            scale1.setPredictionNo(predictionNo2);
            scale1.setValueFlag(DbConstants.NET);
            scale1.setUpdateTime(new Date());
            scale1.setUpdateManNo(userId);
            scale1.setUpdateManName(userName);
            scale1.setUploadFlag("1");
            scale1.setValueFlag("2");
            scale1.setPredictionType("6");
            scale1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            scale1.setMeasureStation(actualRow2.getNetSpot3No());
            scale1.setMeasureStationName(actualRow2.getNetSpot3Name());
            preRailwayScaleMapper.updateByPrimaryKeySelective(scale1);

            MeterBaseOperationLog logModel01 = new MeterBaseOperationLog();
            logModel01.setId(PublicMethod.IdCreate(2));
            logModel01.setCreateManNo(userId);
            logModel01.setCreateManName(userName);
            logModel01.setCreateTime(new Date());
            logModel01.setOperationPage("web端-轨道衡一次数据维护");
            logModel01.setOperationFunction("混卸委托结净");
            logModel01.setOperationId(actualRow1.getActualNo());
            logModel01.setOperationPredictionNo(predictionNo1 + "+" + predictionNo2);
            logModel01.setOperationBeforeFix(actualNo1 + "," + actualNo2 + "," + actualNo3);
            logModel01.setOperationCarnNo(actualRow1.getRailwayNo());
            meterBaseOperationLogMapper.insertSelective(logModel01);

            MeterBaseOperationLog logModel02 = new MeterBaseOperationLog();
            logModel02.setId(PublicMethod.IdCreate(2));
            logModel02.setCreateManNo(userId);
            logModel02.setCreateManName(userName);
            logModel02.setCreateTime(new Date());
            logModel02.setOperationPage("web端-轨道衡一次数据维护");
            logModel02.setOperationFunction("混卸委托结净");
            logModel02.setOperationId(actualRow2.getActualNo());
            logModel02.setOperationPredictionNo(predictionNo1 + "+" + predictionNo2);
            logModel02.setOperationBeforeFix(actualNo1 + "," + actualNo2 + "," + actualNo3);
            logModel02.setOperationCarnNo(actualRow2.getRailwayNo());
            meterBaseOperationLogMapper.insertSelective(logModel02);
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请确认";
        }
        return "";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String fixedTare(String actualNo, String tareWeight, String predictionNo) {

        if (com.steerinfo.util.StringUtils.isEmpty(actualNo)) {
            return "实绩编号不存在，请确认！！";
        }
        MeterWorkRailwayActFirst grossModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo);
        if (!grossModel.getValueFlag().equals("1")) {
            if (grossModel.getValueFlag().equals("0")) {
                return "当前数据已作废，请确认！！";
            } else if (grossModel.getValueFlag().equals("2")) {
                return "当前计量数据已结净，请确认！！";
            }else {
                return "当前计量数据状态不允许结净，状态为"+grossModel.getValueFlag()+"，请确认！！";
            }
        }

        String scaleNo = null;
        if(predictionNo != null && StringUtils.isNotEmpty(predictionNo)) {
            if(grossModel.getPredictionNo() != null && StringUtils.isNotEmpty(grossModel.getPredictionNo())) {
                if (!grossModel.getPredictionNo().equals(predictionNo)) {
                    return "当前计量数据已匹配委托不允许再次勾选其他委托";
                }
                scaleNo = grossModel.getPredictionNo();
            }
            scaleNo = predictionNo;
        }else {
            if(grossModel.getPredictionNo() != null && StringUtils.isNotEmpty(grossModel.getPredictionNo())) {
                scaleNo = grossModel.getPredictionNo();
            }
        }
        if(Double.parseDouble(tareWeight)*1000>grossModel.getMeterWeight().intValue()){
            return "输入的标识皮重不能大于毛重："+grossModel.getMeterWeight().intValue()/1000.0+"吨";
        }

        MeterWorkRailwayActFirst tareModel = new MeterWorkRailwayActFirst();
        BeanUtils.copyProperties(grossModel,tareModel);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(new Date());
//        String firstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//        tareModel.setActualFirstNo(firstNo);
        String firstNo = PublicMethod.IdCreate(3);
        tareModel.setActualFirstNo(firstNo);
        BigDecimal weight = (new BigDecimal(tareWeight)).multiply(new BigDecimal(1000));
        tareModel.setMeterWeight(weight);
        tareModel.setWeightType("4");
        tareModel.setCreateTime(new Date());
        int i = meterWorkRailwayActFirstMapper.insert(tareModel);
        doSaveTare(tareModel,"2");
        if (i >= 1) {
            tareModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(tareModel.getActualFirstNo());
            MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();

            PreRailwayScale scale = new PreRailwayScale();
            if (!com.steerinfo.util.StringUtils.isEmpty(scaleNo)) {
                scale = preRailwayScaleMapper.selectByPrimaryKey(scaleNo);
            }
            if (!com.steerinfo.util.StringUtils.isEmpty(scaleNo) && scale != null) {  //有预报编号进行预报更新
                //将委托中的编号和收发货的等数据拷贝进一次数据中
                MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(grossModel.getActualFirstNo(), scaleNo);
                BeanUtils.copyProperties(first1, grossModel);
                MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(tareModel.getActualFirstNo(), scaleNo);
                BeanUtils.copyProperties(first2, tareModel);
                grossModel.setIsPreSacale("1");
                grossModel.setPredictionType("1");
                tareModel.setIsPreSacale("1");
                tareModel.setPredictionType("1");
                //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), scaleNo);
                BeanUtils.copyProperties(mwca1, actualRow);
                actualRow.setIsPreSacale("1");
                actualRow.setPredictionType("1");

                //更新预报数据
                scale.setPredictionNo(scaleNo);
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("二次计量结净更新");
                scale.setUploadFlag("1");
                scale.setMeasureStation(actualRow.getNetSpot3No());
                scale.setMeasureStationName(actualRow.getNetSpot3Name());
                preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
            } else {
                //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                BeanUtils.copyProperties(mwca1, actualRow);
            }
            grossModel.setValueFlag(DbConstants.NET);
            grossModel.setUpdateManNo("system");
            grossModel.setUpdateManName("二次计量结净更新");
            grossModel.setUpdateTime(new Date());
            grossModel.setWeightType("0");
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);
            //更新第一次皮重数据
            tareModel.setValueFlag(DbConstants.NET);
            tareModel.setUpdateManNo("system");
            tareModel.setUpdateManName("二次计量结净更新");
            tareModel.setUpdateTime(new Date());
            tareModel.setWeightType("1");
//            meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);
            meterWorkRailwayActFirstMapper.deleteByPrimaryKey(tareModel.getActualFirstNo());

//            String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//            actualRow.setActualNo(sdf.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            actualRow.setActualNo(no);
            actualRow.setNetTime(new Date());
            actualRow.setNetMode(DbConstants.TYPE);
            actualRow.setNote("标识皮重结净");
            actualRow.setUploadFlag("1");
            actualRow.setValueFlag("1");
            actualRow.setCreateManName("结净");
            actualRow.setCreateTime(new Date());
            actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
            if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                actualRow.setNetManNo(actualRow.getGrossManNo());
                actualRow.setNetManName(actualRow.getGrossManName());
                actualRow.setNetTime(actualRow.getGrossTime());
                actualRow.setCreateTime(actualRow.getGrossTime());
                actualRow.setCreateManName(actualRow.getGrossManName());
            }else {
                actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                actualRow.setNetManNo(actualRow.getTareManNo());
                actualRow.setNetManName(actualRow.getTareManName());
                actualRow.setNetTime(actualRow.getTareTime());
                actualRow.setCreateTime(actualRow.getTareTime());
                actualRow.setCreateManName(actualRow.getTareManName());
            }
            actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
            meterWorkRailwayActualMapper.insertSelective(actualRow);
            return "";
        }
        return "保存失败";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult HistoricalTare(String actualNo, String predictionNo) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        if (com.steerinfo.util.StringUtils.isEmpty(actualNo)) {
            rm.setMessage("实绩编号不存在，请确认！！");
            return rm;
        }
        MeterWorkRailwayActFirst grossModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo);
        if (grossModel == null) {
            rm.setMessage("未找到一次计量数据，请确认！！");
            return rm;
        }
        if (!grossModel.getValueFlag().equals("1")) {
            rm.setMessage("当前计量数据已结净，请确认！！");
            return rm;
        }
        HashMap<String, Object> map = new HashMap<>();
        String railwayNo = grossModel.getRailwayNo();
        map.put("railwayNo", railwayNo);
        map.put("valueFlag", "0");
        List<MeterBaseRailwayHisTareData> hisTareModel = meterBaseRailwayHisTareDataMapper.selectByParameters(map);
        if (hisTareModel.size() >= 1) {
            Long tareWeight = hisTareModel.get(0).getMeterWeight();
            MeterWorkRailwayActFirst tareModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
//            String firstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//            tareModel.setActualFirstNo(firstNo);
            String firstNo = PublicMethod.IdCreate(3);
            tareModel.setActualFirstNo(firstNo);
            tareModel.setMeterWeight(new BigDecimal(tareWeight));
            tareModel.setWeightType("3");
            tareModel.setCreateTime(new Date());
            int i = meterWorkRailwayActFirstMapper.insert(tareModel);

            if (i >= 1) {
                tareModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(tareModel.getActualFirstNo());
                MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();

                PreRailwayScale scale = new PreRailwayScale();
                if (!com.steerinfo.util.StringUtils.isEmpty(predictionNo)) {
                    scale = preRailwayScaleMapper.selectByPrimaryKey(predictionNo);
                }
                if (!com.steerinfo.util.StringUtils.isEmpty(predictionNo) && scale != null) {  //有预报编号进行预报更新
                    //将委托中的编号和收发货的等数据拷贝进一次数据中
                    MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(grossModel.getActualFirstNo(), predictionNo);
                    BeanUtils.copyProperties(first1, grossModel);
                    MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(tareModel.getActualFirstNo(), predictionNo);
                    BeanUtils.copyProperties(first2, tareModel);
                    grossModel.setIsPreSacale("1");
                    tareModel.setIsPreSacale("1");

                    //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                    MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), predictionNo);
                    BeanUtils.copyProperties(mwca1, actualRow);
                    actualRow.setIsPreSacale("1");

                    //更新预报数据
                    scale.setPredictionNo(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());
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);

                } else {
                    //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                    MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                    BeanUtils.copyProperties(mwca1, actualRow);
                }
                grossModel.setValueFlag(DbConstants.NET);
                grossModel.setUpdateManNo("system");
                grossModel.setUpdateManName("二次计量结净更新");
                grossModel.setUpdateTime(new Date());
                meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);
                //更新第一次皮重数据
                tareModel.setValueFlag(DbConstants.NET);
                tareModel.setUpdateManNo("system");
                tareModel.setUpdateManName("二次计量结净更新");
                tareModel.setUpdateTime(new Date());
                meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);

//                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);
                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);
                actualRow.setNetTime(new Date());
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setNote("历史皮重结净");
                actualRow.setUploadFlag("1");
                actualRow.setValueFlag("2");
                actualRow.setCreateManName("结净");
                actualRow.setCreateTime(new Date());
                actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                    actualRow.setNetManNo(actualRow.getGrossManNo());
                    actualRow.setNetManName(actualRow.getGrossManName());
                    actualRow.setNetTime(actualRow.getGrossTime());
                    actualRow.setCreateTime(actualRow.getGrossTime());
                    actualRow.setCreateManName(actualRow.getGrossManName());
                    actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                }
                meterWorkRailwayActualMapper.insertSelective(actualRow);
                rm.setSucceed();
                rm.setMessage("操作成功");
                return rm;
            }
            return rm;
        } else {
            rm.setMessage("当前火车车号在历史皮重表里未找到历史皮重信息，不能历史皮重结净，请确认！！");
            return rm;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult supplementaryData(MeterWorkRailwayActFirst model) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

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

            /*String predictionNo = model.getPredictionNo();
            PreRailwayScale scale = preRailwayScaleMapper.selectByPrimaryKey(predictionNo);
            if(scale.getValueFlag().equals("2")) {
                rm.setMessage("委托已匹配，请确认！！");
                return rm;
            }*/

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

            //毛重
//            String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
//            mwcaf1.setActualFirstNo(actualFirstNo);
            String actualFirstNo = PublicMethod.IdCreate(3) + "bak";
//            mwcaf1.setActualFirstNo(actualFirstNo);
//            mwcaf1.setRailwayNo(model.getRailwayNo());
//            mwcaf1.setMeterWeight(model.getMeterWeight());
//            mwcaf1.setRailwayTypeNo(model.getRailwayTypeNo());
//            mwcaf1.setRailwayTypeName(model.getRailwayTypeName());
//            mwcaf1.setWeightType(model.getWeightType());
//            mwcaf1.setCreateManNo(model.getCreateManNo());
//            mwcaf1.setCreateManName(model.getCreateManName());
//            mwcaf1.setCreateTime(new Date());
//            mwcaf1.setBaseSpotNo(model.getBaseSpotNo());
//            mwcaf1.setBaseSpotName(model.getBaseSpotName());
//            mwcaf1.setRailwayModelNo(model.getRailwayModelNo());
//            mwcaf1.setRailwayModelName(model.getRailwayModelName());
            model.setActualFirstNo(actualFirstNo);
            model.setMeterWeight(model.getMeterWeight().multiply(new BigDecimal(1000)));
            model.setValueFlag("1");
            model.setCheckFlag("0");
            model.setDataSource("4");
            model.setMeterMode("3");
            model.setIsPreSacale("0");
            int i = meterWorkRailwayActFirstMapper.insertSelective(model);

            if (i < 1) {
                rm.setMessage("插入失败，请确认！！");
                return rm;
            }
            //净重
            /*if(com.steerinfo.util.StringUtils.isNotEmpty(predictionNo)){
                //将委托中的编号和收发货的等数据拷贝进一次数据中
                MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(actualFirstNo, predictionNo);
                BeanUtils.copyProperties(first1,mwcaf1);
                mwcaf1.setIsPreSacale("1");
                meterWorkRailwayActFirstMapper.updateByPrimaryKey(mwcaf1);
                //更新预报数据
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("手动录入");
                scale.setUploadFlag("1");
                scale.setValueFlag("2");
                preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
            }*/
            rm.setSucceed();

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MeterWorkRailwayActFirst> selectSta(HashMap parmas) {
        try {
            //parmas.put("railwayTypeName", "静态轨道衡");
            parmas.put("railwayTypeNo", DbConstants.STATIC);
            List<MeterWorkRailwayActFirst> mwrafs = meterWorkRailwayActFirstMapper.selectSta(parmas);
            return mwrafs;
        } catch (MarkerMetException e) {
            throw e;
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageList<MeterWorkRailwayActFirst> selectView(HashMap parmas, Integer pageNum, Integer pageSize) {
        String flag = parmas.get("flag").toString();
        if (flag == null || "".equals(flag)) {
            throw new MarkerMetException(500, "标识符为空!!");
        }
        if (!flag.equals("1") && !flag.equals("0")) {
            throw new MarkerMetException(500, "标识符不正确!!");
        }
        if (flag.equals("1")) {
            parmas.put("railwayTypeName", "动态轨道衡");
            parmas.put("railwayTypeNo", DbConstants.ACT);
        }
        if (flag.equals("0")) {
            parmas.put("railwayTypeName", "静态轨道衡");
            parmas.put("railwayTypeNo", DbConstants.STATIC);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkRailwayActFirst> mwrafs = meterWorkRailwayActFirstMapper.selectView(parmas);
        PageList pageInfo = new PageList(mwrafs);
        return pageInfo;
    }


    @Override
    @Transactional
    public PageList<MeterWorkRailwayActFirst> selectDiff(String flag, Integer pageNum, Integer pageSize) {
        String railwayTypeName = "";
        String railwayTypeNo = "";
        if (flag == null || "".equals(flag)) {
            throw new MarkerMetException(500, "标识符为空!!");
        }
        if (!flag.equals("1") && !flag.equals("0")) {
            throw new MarkerMetException(500, "标识符不正确!!");
        }
        if (flag.equals("1")) {
            railwayTypeName = "动态轨道衡";
            railwayTypeNo = DbConstants.ACT;
        }
        if (flag.equals("0")) {
            railwayTypeName = "静态轨道衡";
            railwayTypeNo = DbConstants.STATIC;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkRailwayActFirst> mwrafs = meterWorkRailwayActFirstMapper.selectDiff(railwayTypeNo, railwayTypeName);
        PageList pageInfo = new PageList(mwrafs);
        return pageInfo;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(MeterWorkRailwayActFirst model) {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数为空!!");
            }
            if (model.getValueFlag().equals("2")) {
                throw new MarkerMetException(500, "该条数据已结净不能解除委托");
            }
            if (model.getPredictionNo() == null || "".equals(model.getPredictionNo())) {
                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";
            }

            PreRailwayScale pls = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
            if (pls.getValueFlag().equals("9")) {
                throw new MarkerMetException(500, "该条数据匹配的委托已结算不能解除委托！！");
            }
            pls.setPredictionNo(model.getPredictionNo());
            pls.setUpdateManName("轨道衡一次计量解除委托操作");
            pls.setUpdateTime(new Date());
            pls.setValueFlag("1");
            pls.setUploadFlag("1");
            preRailwayScaleMapper.updateByPrimaryKeySelective(pls);
            MeterWorkRailwayActFirst mwcaf = meterWorkRailwayActFirstMapper.selectAndRemove(model.getActualFirstNo());
            mwcaf.setValueFlag("1");
            mwcaf.setIsPreSacale("0");
            int num = meterWorkRailwayActFirstMapper.updateByPrimaryKey(mwcaf);

            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.getActualFirstNo());
            logModel.setOperationPredictionNo(model.getPredictionNo());
            logModel.setOperationCarnNo(model.getRailwayNo());
            logModel.setOperationBeforeFix(model.toString());
            logModel.setOperationAfterFix(mwcaf.toString());
            meterBaseOperationLogMapper.insertSelective(logModel);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    /**
     * 一次计量信息与预报信息进行匹配
     *
     * @param actualFirstNo
     * @param predictionNo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkRailwayActFirst match(String actualFirstNo, String predictionNo) {
        try {
            UserPayload userPayload = UserPayload.getCurrUser();
            String userId = userPayload.getUserCode();
            String userName = userPayload.getUserName();

            if (actualFirstNo == null || "".equals(actualFirstNo)) {
                throw new MarkerMetException(500, "计量作业编号为空!!");
            }
            if (predictionNo == null || "".equals(predictionNo)) {
                throw new MarkerMetException(500, "预报编号为空!!");
            }

            PreRailwayScale prs = preRailwayScaleMapper.selectByPrimaryKey(predictionNo);
            if (prs == null) {
                throw new MarkerMetException(500, "该预报信息不存在");
            }
            if (prs.getValueFlag().equals("2")) {
                throw new MarkerMetException(500, "该预报信息已匹配");
            }
            MeterWorkRailwayActFirst meterWorkRailwayActFirst = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualFirstNo);
            if (meterWorkRailwayActFirst == null) {
                throw new MarkerMetException(500, "该计量作业信息不存在");
            }
            if (meterWorkRailwayActFirst.getPredictionNo() != null && !"".equals(meterWorkRailwayActFirst.getPredictionNo())) {
                throw new MarkerMetException(500, "该计量作业已匹配！！");
            }
            if (!meterWorkRailwayActFirst.getRailwayNo().equals(prs.getRailwayNo())) {
                throw new MarkerMetException(500, "结净数据中车号和预报中车号不一致，不允许进行匹配委托操作");
            }
            MeterWorkRailwayActFirst model = meterWorkRailwayActFirstMapper.selectAndMatch(actualFirstNo, predictionNo);
            model.setValueFlag("1");
            model.setUpdateManNo(userId);
            model.setUpdateManName(userName);
            model.setUpdateTime(new Date());
            model.setNote("一次计量匹配委托操作");
            model.setIsPreSacale("1");

            prs.setValueFlag(DbConstants.IN); //使用中
//            prs.setValueFlag("2"); //已使用
            prs.setUploadFlag("1");
            prs.setPredictionType("1");
            prs.setUpdateManNo(userId);
            prs.setUpdateManName(userName);
            prs.setUpdateTime(new Date());
            prs.setMeasureStation(model.getBaseSpotNo());
            prs.setMeasureStationName(model.getBaseSpotName());

            //当一次计量数据是已结净的状态时，同时将该结净数据的另一条一次计量数据自动匹配该委托，并将该结净记录也进行委托匹配
            if (model.getValueFlag() == "2" || "2".equals(model.getValueFlag())) {
                MeterWorkRailwayActual meterWorkRailwayActual = meterWorkRailwayActualMapper.selectActual(actualFirstNo);
                String getActualFirstNo = "";
                //判断传入的是哪条一次记录
                if (meterWorkRailwayActual.getActualFirst1No().equals(actualFirstNo)) {
                    getActualFirstNo = meterWorkRailwayActual.getActualFirst2No();
                }
                if (meterWorkRailwayActual.getActualFirst2No().equals(actualFirstNo)) {
                    getActualFirstNo = meterWorkRailwayActual.getActualFirst1No();
                }
                //另一条一次数据进行委托解除
                MeterWorkRailwayActFirst meterWorkCarActualFirst1 = meterWorkRailwayActFirstMapper.selectAndMatch(getActualFirstNo, predictionNo);
                meterWorkCarActualFirst1.setIsPreSacale("1");
                meterWorkRailwayActFirstMapper.updateByPrimaryKey(meterWorkCarActualFirst1);
                //将该一次记录的结净数据委托解除
                MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.mathPredcition(meterWorkRailwayActual.getActualNo(), predictionNo);
                mwca.setIsPreSacale("1");
                mwca.setUploadFlag("1");
                meterWorkRailwayActualMapper.updateByPrimaryKey(mwca);
            }


//            prs.setNote(); 需要添加修改原因写入
            preRailwayScaleMapper.updateByPrimaryKeySelective(prs);
            meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model);

            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.getActualFirstNo());
            logModel.setOperationPredictionNo(predictionNo);
            logModel.setOperationCarnNo(model.getRailwayNo());
            logModel.setOperationBeforeFix(meterWorkRailwayActFirst.toString());
            logModel.setOperationAfterFix(model.toString());
            meterBaseOperationLogMapper.insertSelective(logModel);
            return model;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageList<MeterWorkRailwayActFirst> selectTa(Map<String, Object> parmas, Integer pageNum, Integer
            pageSize) {
        try {
            String weightType = parmas.get("weightType").toString();
            String flag = parmas.get("flag").toString();
            String railwayTypeNo = "";
            if (StringUtils.isEmpty(weightType)) {
                throw new MarkerMetException(500, "重量编号为空!!");
            }
            if (StringUtils.isEmpty(flag)) {
                throw new MarkerMetException(500, "标识符为空!!");
            }
            if (!flag.equals("1") && !flag.equals("0")) {
                throw new MarkerMetException(500, "标识符不正确!!");
            }
            if (flag.equals("1")) {
                railwayTypeNo = DbConstants.ACT;
            }
            if (flag.equals("0")) {
                railwayTypeNo = DbConstants.STATIC;
            }
            parmas.put("railwayTypeNo", railwayTypeNo);
            PageHelper.startPage(pageNum, pageSize);
            List<MeterWorkRailwayActFirst> mwrafs = meterWorkRailwayActFirstMapper.selectTa(parmas);
            PageList pageInfo = new PageList(mwrafs);
            return pageInfo;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MeterWorkRailwayActFirst> selectNet(String weightType, String railwayNo, String flag) {
        try {
            String railwayTypeNo = "";
            if (weightType == null || "".equals(weightType)) {
                throw new MarkerMetException(500, "重量类型为空!!");
            }
            if (railwayNo == null || "".equals(railwayNo)) {
                throw new MarkerMetException(500, "车号为空!!");
            }
            if (flag == null || "".equals(flag)) {
                throw new MarkerMetException(500, "标识符为空!!");
            }
            if (!flag.equals("1") && !flag.equals("0")) {
                throw new MarkerMetException(500, "标识符不正确!!");
            }
            if (flag.equals("1")) {
                railwayTypeNo = DbConstants.ACT;
            }
            if (flag.equals("0")) {
                railwayTypeNo = DbConstants.STATIC;
            }
            List<MeterWorkRailwayActFirst> mwrafs = meterWorkRailwayActFirstMapper.selectNet(railwayNo, railwayTypeNo);
            return mwrafs;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<StaffPerformanceAnalysis> analysis(String startTime, String endTime) {
        try {
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("startTime", startTime);
            obj.put("endTime", endTime);
            List<StaffPerformanceAnalysis> list = meterWorkRailwayActFirstMapper.analysis(obj);
            return list;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    private void check1(MeterWorkRailwayActFirst model) throws MarkerMetException {
        if ("001019001".equals(model.getRailwayTypeNo())) {
            if (model.getRailwayNo() == null || model.getRailwayNo().equals("")) {
                throw new MarkerMetException(500, "火车车号不能为空");
            }
        }

        if (model.getBaseSpotNo() == null || model.getBaseSpotNo().equals("")) {
            throw new MarkerMetException(500, "计量点编号不能为空");
        }
        if (model.getBaseSpotName() == null || model.getBaseSpotName().equals("")) {
            throw new MarkerMetException(500, "计量点名称不能为空");
        }
        if (StringUtils.isEmpty(model.getActualFirstNo())) {
            model.setValueFlag(DbConstants.VALID);
            model.setCheckFlag(DbConstants.INVALID);
        } else {
            model.setUpdateTime(new Date());
        }
    }

    private void check(MeterWorkRailwayActFirst model) throws MarkerMetException {
        if ("001019001".equals(model.getRailwayTypeNo())) {
            if (model.getRailwayNo() == null || model.getRailwayNo().equals("")) {
                throw new MarkerMetException(500, "火车车号不能为空");
            }
        }
        if (model.getBaseSpotNo() == null || model.getBaseSpotNo().equals("")) {
            throw new MarkerMetException(500, "计量点编号不能为空");
        }
        if (model.getBaseSpotName() == null || model.getBaseSpotName().equals("")) {
            throw new MarkerMetException(500, "计量点名称不能为空");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        if (StringUtils.isEmpty(model.getActualFirstNo())) {
            model.setCreateManNo(userId);
            model.setCreateManName(userName);
            model.setValueFlag(DbConstants.VALID);
            model.setCheckFlag(DbConstants.INVALID);
        } else {
            model.setUpdateTime(new Date());
            model.setUpdateManNo(userId);
            model.setUpdateManName(userName);
        }
    }

    /**
     * C#端入库秤实绩静轨使用固重结净
     *
     * @param firstRail
     * @param netRail
     * @return
     */
    @Override
    @Transactional
    public String doAddWf(MeterWorkRailwayActFirst firstRail, MeterWorkRailwayActual netRail) {
        //1、将计量预报分割出
        String[] preSclas = firstRail.getPredictionCombination().split(",");
        ArrayList<String> ids = new ArrayList<>();
        Collections.addAll(ids, preSclas);
        String no = "";
        int num = 1;
        if (ids.size() <= 1) {
            //非分拣预报（非混装）
            no = addFirstAndNet(firstRail, netRail);
        } else {
            //分拣预报（混装）
            List<PreRailwayScale> prss = preRailwayScaleMapper.batchSelect(ids);
            if (prss.size() >= 1) {
                for (PreRailwayScale prs : prss) {
                    //补充一次数据
                    MeterWorkRailwayActFirst first = improveDataFirst(firstRail, prs);
                    first.setMeterBalanceWeight(first.getMeterWeight());
                    //补充结净数据
                    MeterWorkRailwayActual net = improveDataActual(netRail, prs);
                    net.setNetWeight(first.getMeterBalanceWeight());
                    //新增数据
                    if (num > 1) {
                        //不是第一条的将重量数据写0；
                        first.setMeterWeight(new BigDecimal(0));
                        first.setMeterSortWeight(new BigDecimal(0));
                        first.setMeterBalanceWeight(new BigDecimal(0));
                        //将结净的数据写为0
                        net.setGrossWeight(new BigDecimal(0));//毛重
                        net.setNetWeight(new BigDecimal(0));//净重
                    }
                    no = addFirstAndNet(firstRail, netRail);
                    num++;
                }
            }
        }
        return no;
    }

    //保存以及修改预报状态
    @Transactional
    public String addFirstAndNet(MeterWorkRailwayActFirst firstRail, MeterWorkRailwayActual netRail) {
        //补充编号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(firstRail.getCreateTime());
        //一次数据
//        String firstNo = meterWorkRailwayActFirstMapper.getNewID(afl);
//        firstRail.setActualFirstNo(afl + firstNo);
        String firstNo = PublicMethod.IdCreate(3);
        firstRail.setActualFirstNo(firstNo);

//        String firstNoNew= PublicMethod.IdCreate(2);
//        firstRail.setActualFirstNo(firstNoNew);
        //结净数据
//        String netNo = meterWorkRailwayActualMapper.getNewID(afl);
//        netRail.setActualNo(afl + netNo);
        String netNo = PublicMethod.IdCreate(3);
        netRail.setActualNo(netNo);
        netRail.setUploadFlag("1");
//        String netNoNew= PublicMethod.IdCreate(2);
//        netRail.setActualNo(netNoNew);
        firstRail.setCreateTime(new Date());
        meterWorkRailwayActFirstMapper.insertSelective(firstRail);
        if(netRail.getGrossTime().compareTo(netRail.getTareTime()) >= 0) {
            netRail.setNetSpot3No(netRail.getBaseSpot1No());
            netRail.setNetSpot3Name(netRail.getBaseSpot1Name());
            netRail.setNetManNo(netRail.getGrossManNo());
            netRail.setNetManName(netRail.getGrossManName());
            netRail.setNetTime(netRail.getGrossTime());
            netRail.setCreateTime(netRail.getGrossTime());
            netRail.setCreateManName(netRail.getGrossManName());
            netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst1No()));
        }else{
            netRail.setNetSpot3No(netRail.getBaseSpot2No());
            netRail.setNetSpot3Name(netRail.getBaseSpot2Name());
            netRail.setNetManNo(netRail.getTareManNo());
            netRail.setNetManName(netRail.getTareManName());
            netRail.setNetTime(netRail.getTareTime());
            netRail.setCreateTime(netRail.getTareTime());
            netRail.setCreateManName(netRail.getTareManName());
            netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst2No()));
        }
        meterWorkRailwayActualMapper.insertSelective(netRail);
        //修改预报状态
        return firstRail.getActualFirstNo();
    }

    //结净补充数据
    private MeterWorkRailwayActual improveDataActual(MeterWorkRailwayActual netRail, PreRailwayScale prs) {
        if (prs == null) {
            throw new MarkerMetException(500, "参数为空!!");
        }
        //补充数据
        netRail.setPredictionNo(prs.getPredictionNo());
        netRail.setNoticeNo(prs.getNoticeNo());
        netRail.setMatterNo(prs.getMatterNo());
        netRail.setMatterName(prs.getMatterName());
        netRail.setContractNo(prs.getContractNo());
        netRail.setBatchNo(prs.getBatchNo());
        netRail.setCustomerSupplierNo(prs.getCustomerSupplierNo());
        netRail.setCustomerSupplierName(prs.getCustomerSupplierName());
        netRail.setForwardingUnitNo(prs.getForwardingUnitNo());
        netRail.setForwardingUnitName(prs.getForwardingUnitName());
        netRail.setReceivingUintNo(prs.getReceivingUintNo());
        netRail.setReceivingUintName(prs.getReceivingUintName());
        netRail.setMaterialNo(prs.getMaterialNo());
        netRail.setMaterialName(prs.getMaterialName());
        netRail.setSpecNo(prs.getSpecNo());
        netRail.setSpecName(prs.getSpecName());
        netRail.setLoadPointNo(prs.getLoadPointNo());
        netRail.setLoadPointName(prs.getLoadPointName());
        netRail.setSampleNo(prs.getSampleNo());
        netRail.setSampleVoucher(prs.getSampleVoucher());
        netRail.setCarrierUnitNo(prs.getCarrierUnitNo());
        netRail.setCarrierUnitName(prs.getCarrierUnitName());
        netRail.setMeterTypeNo(prs.getMeterTypeNo());
        netRail.setMeterTypeName(prs.getMeterTypeName());
        return netRail;
    }

    //一次补充数据
    private MeterWorkRailwayActFirst improveDataFirst(MeterWorkRailwayActFirst firstRail, PreRailwayScale prs) {
        if (prs == null) {
            throw new MarkerMetException(500, "参数为空!!");
        }
        //补充数据
        firstRail.setPredictionNo(prs.getPredictionNo());
        firstRail.setNoticeNo(prs.getNoticeNo());
        firstRail.setMatterNo(prs.getMatterNo());
        firstRail.setMatterName(prs.getMatterName());
        firstRail.setContractNo(prs.getContractNo());
        firstRail.setBatchNo(prs.getBatchNo());
        firstRail.setCustomerSupplierNo(prs.getCustomerSupplierNo());
        firstRail.setCustomerSupplierName(prs.getCustomerSupplierName());
        firstRail.setForwardingUnitNo(prs.getForwardingUnitNo());
        firstRail.setForwardingUnitName(prs.getForwardingUnitName());
        firstRail.setReceivingUintNo(prs.getReceivingUintNo());
        firstRail.setReceivingUintName(prs.getReceivingUintName());
        firstRail.setMaterialNo(prs.getMaterialNo());
        firstRail.setMaterialName(prs.getMaterialName());
        firstRail.setSpecNo(prs.getSpecNo());
        firstRail.setSpecName(prs.getSpecName());
        firstRail.setShipmentGrossWeight(prs.getShipmentGrossWeight());
        firstRail.setShipmentNetWeight(prs.getShipmentNetWeight());
        firstRail.setShipmentNum(prs.getShipmentNum());
        firstRail.setLoadPointNo(prs.getLoadPointNo());
        firstRail.setLoadPointName(prs.getLoadPointName());
        firstRail.setSampleNo(prs.getSampleNo());
        firstRail.setSampleVoucher(prs.getSampleVoucher());
        firstRail.setCarrierUnitNo(prs.getCarrierUnitNo());
        firstRail.setCarrierUnitName(prs.getCarrierUnitName());
        firstRail.setPredictionType(prs.getPredictionType());
        firstRail.setPredictionCombination(prs.getPredictionCombination());
        firstRail.setMeterTypeNo(prs.getMeterTypeNo());
        firstRail.setMeterTypeName(prs.getMeterTypeName());
        firstRail.setIsChemical(prs.getIsChemical());
        firstRail.setLimitWeight(prs.getLimitWeight());
        return firstRail;
    }

    /**
     * 静态轨道衡计量保存逻辑
     *
     * @param model
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doAddWfStatic(MeterWorkRailwayActFirst model) throws Exception {
        BigDecimal zero = new BigDecimal(0);
        //String id = "";//需要返回的作业编号
        HashMap<String, Object> map = new HashMap<>();
        List<MeterWorkRailwayActFirst> firsts = new ArrayList<>();
        List<MeterWorkRailwayActual> actuals = new ArrayList<>();
        RESTfulResult rm = new RESTfulResult();
        rm.setSucceed();
        rm.setCode("0");
        rm.setMessage("操作成功！！");

        /*if (model == null || StringUtils.isEmpty(model.getPredictionNo())) {
            throw new MarkerMetException(500, "预报编号为空不允许进行保存操作!");
        }*/
        if (model.getMeterWeight() == null || model.getMeterWeight().compareTo(zero) <= 0) {
            throw new MarkerMetException(500, "采集重量为空或小于0不允许进行保存操作!");
        }

        if (StringUtils.isNotEmpty(model.getPredictionNo())) {
            //验证预报编号与预报的车号是否一致
            PreRailwayScale railwayScale = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
            if (railwayScale == null || StringUtils.isEmpty(railwayScale.getPredictionNo())) {
                throw new MarkerMetException(500, "未查询到预报编号为【" + model.getPredictionNo() + "】的数据，请刷新后重试!");
            } else {
                if (StringUtils.isEmpty(railwayScale.getRailwayNo())) {
                    throw new MarkerMetException(500, "预报【" + model.getPredictionNo() + "】中的车号为空，不允许进行保存操作，请刷新后重试!");
                } else {
                    if (!railwayScale.getRailwayNo().equals(model.getRailwayNo())) {
                        throw new MarkerMetException(500, "预报【" + model.getPredictionNo() + "】中的车号【" + railwayScale.getRailwayNo() + "】" +
                                "与界面车号【" + model.getRailwayNo() + "】不一致，不允许进行保存操作，请刷新后重试!");
                    }
                    if ("3".equals(railwayScale.getValueFlag())) {
                        throw new MarkerMetException(500, "预报【" + model.getPredictionNo() + "】已被作废，不允许进行保存操作，请重新选择预报!");
                    }
                    if ("2".equals(railwayScale.getValueFlag())) {
                        throw new MarkerMetException(500, "预报【" + model.getPredictionNo() + "】已经使用（已出净重），不允许再次使用，请重新选择预报或者进行复磅!");
                    }
                }

            }

            //验证当前使用的预报号与一次计量的预报号是否一致
            MeterWorkRailwayActFirst searchFirst = new MeterWorkRailwayActFirst();
            searchFirst.setRailwayNo(model.getRailwayNo());
            searchFirst.setValueFlag("1");
            Map<String, Object> mapSearchFirst = FieldsCollector.beanToMap(searchFirst);
            List<MeterWorkRailwayActFirst> searchFirstList = meterWorkRailwayActFirstMapper.selectByParameters(mapSearchFirst);
            if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                if (searchFirstList.size() > 1) {
                    throw new MarkerMetException(500, "车号【" + model.getRailwayNo() + "】存在【" + searchFirstList.size() + "】条有效数据，不允许计量操作，请先在网页上进行作废!");
                }
                if (searchFirstList.size() == 1) {
                    if (StringUtils.isNotEmpty(searchFirst.getPredictionNo())) {
                        if (!model.getPredictionNo().equals(searchFirstList.get(0).getPredictionNo())) {
                            throw new MarkerMetException(500, "车号【" + model.getRailwayNo() + "】一次计量的预报【"
                                    + searchFirstList.get(0).getPredictionNo() + "】与当前使用的预报【" + model.getPredictionNo() + "】不一致，不允许计量操作，请先在网页上进行修改!");
                        }
                    } else {
                        //判断上一次的一次计量是否匹配委托，没有委托将当前委托匹配给上一次计量数据
                        String predictionNo01 = model.getPredictionNo();
                        MeterWorkRailwayActFirst first01 = searchFirstList.get(0);
                        //将委托中的编号和收发货的等数据拷贝进一次数据中
                        MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(first01.getActualFirstNo(), predictionNo01);
                        BeanUtils.copyProperties(first1, first01);

                        first01.setValueFlag("1");
                        first01.setUpdateTime(new Date());
                        first01.setIsPreSacale("1");
                        first01.setPredictionNo(predictionNo01);
                        meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(first01);
                    }
                }
            }
        }

        //1、判断是否是二次数据（二次数据需要结净）
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("railwayNo", model.getRailwayNo());
        obj.put("railwayCarriageNo", model.getRailwayCarriageNo());
        obj.put("valueFlag", "1");//有效，未结净
        List<MeterWorkRailwayActFirst> mwrafs = meterWorkRailwayActFirstMapper.selectBy(obj);

        //2、判断是否为混装,若为混装将计量预报分割出
        ArrayList<String> ids = new ArrayList<>(); //ids用于更新预报操作
        if (StringUtils.isNotEmpty(model.getPredictionCombination())) {
            String[] preSclas = model.getPredictionCombination().split(",");
            Collections.addAll(ids, preSclas);
        }

        if (mwrafs.size() == 0) {
            //===========一次计量处理开始==============
            if (model.getRailwayFactoryWeight() == null || model.getRailwayFactoryWeight().compareTo(zero) <= 0) {
                //============一毛一皮的计量数据处理开始==============
                model.setWeightType("0");
                model.setMeterSortWeight(zero); //分检重量
                model.setMeterBalanceWeight(model.getMeterWeight()); //结算重量
                doAddWfStaticFrist(model);
                //3、修改预报状态为【使用中】
                if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                    ids.add(model.getPredictionNo());
                    preRailwayScaleMapper.batchUpdateByIdsForIn(ids);
                }

                firsts.add(model);
                map.put("firsts", firsts);
                //============一毛一皮的计量数据处理结束==============
            } else {
                //============毛重+自重的计量数据处理开始==============
                //毛重数据保存
                model.setWeightType("0");
                model.setMeterSortWeight(zero); //分检重量
                model.setMeterBalanceWeight(model.getMeterWeight());//结算重量
                model.setCreateTime(new Date()); //服务端重新赋值，避免出现皮重比毛重时间晚的情况；2021年6月10日
                doAddWfStaticFrist(model);

                //皮重计量实体的组装；皮重数据保存
                MeterWorkRailwayActFirst railwayActFirstTare = new MeterWorkRailwayActFirst();
                BeanUtils.copyProperties(model, railwayActFirstTare);
                railwayActFirstTare.setMeterWeight(model.getRailwayFactoryWeight()); //皮重等于自重
                railwayActFirstTare.setMeterSortWeight(zero);//分检重量
                railwayActFirstTare.setMeterBalanceWeight(model.getRailwayFactoryWeight());//结算重量
                railwayActFirstTare.setWeightType("1");
                Calendar tareDate = Calendar.getInstance();
                tareDate.add(Calendar.SECOND, -10);//自重的皮重减10s,用于查询最近的一条毛重数据，保证复磅数据准确
                railwayActFirstTare.setCreateTime(tareDate.getTime());
                doAddWfStaticFrist(railwayActFirstTare);

                //保存历史皮重
                if (railwayActFirstTare.getWeightType().equals("1")) {
                    doSaveTare(railwayActFirstTare);
                }
                MeterWorkRailwayActual actual = new MeterWorkRailwayActual();
                if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                    //净重数据保存
                    PreRailwayScale preRailwayScale = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
                    actual = doAddWfStaticNet(preRailwayScale, model, railwayActFirstTare); //预报，毛重，皮重
                } else {
                    actual = doAddWfStaticNetNot(model, railwayActFirstTare);

                }

                railwayActFirstTare.setValueFlag("0");
                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(railwayActFirstTare);
                //3、修改预报状态为【已使用】
                if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                    ids.add(model.getPredictionNo());
                    preRailwayScaleMapper.batchUpdateByIdsForUsed(ids);
                }

                firsts.add(model);
                map.put("firsts", firsts);
                actuals.add(actual);
                map.put("actuals", actuals);
                //============毛重+自重的计量数据处理结束==============
            }
            //===========一次计量处理结束==============
        } else {
            //===========二次计量处理开始==============
            MeterWorkRailwayActual actual = new MeterWorkRailwayActual();
            MeterWorkRailwayActFirst first = new MeterWorkRailwayActFirst();
            for (MeterWorkRailwayActFirst mwraf : mwrafs) {

                //保存第二次的一次计量数据；特别说明：先皮后毛不会做分检，所以不需要考虑
                MeterWorkRailwayActFirst meterWorkRailwayActFirst = new MeterWorkRailwayActFirst();
                BeanUtils.copyProperties(mwraf, meterWorkRailwayActFirst);

                //查询预报信息
                if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                    PreRailwayScale prs = preRailwayScaleMapper.selectByPrimaryKey(mwraf.getPredictionNo());
                    meterWorkRailwayActFirst.setPredictionNo(prs.getPredictionNo());
                    meterWorkRailwayActFirst.setNoticeNo(prs.getNoticeNo());
                    meterWorkRailwayActFirst.setMatterNo(prs.getMatterNo());
                    meterWorkRailwayActFirst.setMatterName(prs.getMatterName());
                    meterWorkRailwayActFirst.setContractNo(prs.getContractNo());
                    meterWorkRailwayActFirst.setBatchNo(prs.getBatchNo());
                    meterWorkRailwayActFirst.setCustomerSupplierNo(prs.getCustomerSupplierNo());
                    meterWorkRailwayActFirst.setCustomerSupplierName(prs.getCustomerSupplierName());
                    meterWorkRailwayActFirst.setForwardingUnitNo(prs.getForwardingUnitNo());
                    meterWorkRailwayActFirst.setForwardingUnitName(prs.getForwardingUnitName());
                    meterWorkRailwayActFirst.setReceivingUintNo(prs.getReceivingUintNo());
                    meterWorkRailwayActFirst.setReceivingUintName(prs.getReceivingUintName());
                    meterWorkRailwayActFirst.setMaterialNo(prs.getMaterialNo());
                    meterWorkRailwayActFirst.setMaterialName(prs.getMaterialName());
                    meterWorkRailwayActFirst.setSpecNo(prs.getSpecNo());
                    meterWorkRailwayActFirst.setSpecName(prs.getSpecName());
                    meterWorkRailwayActFirst.setLoadPointNo(prs.getLoadPointNo());
                    meterWorkRailwayActFirst.setLoadPointName(prs.getLoadPointName());
                    meterWorkRailwayActFirst.setSampleNo(prs.getSampleNo());
                    meterWorkRailwayActFirst.setSampleVoucher(prs.getSampleVoucher());
                    meterWorkRailwayActFirst.setCarrierUnitNo(prs.getCarrierUnitNo());
                    meterWorkRailwayActFirst.setCarrierUnitName(prs.getCarrierUnitName());
                    meterWorkRailwayActFirst.setMeterTypeNo(prs.getMeterTypeNo());
                    meterWorkRailwayActFirst.setMeterTypeName(prs.getMeterTypeName());
                    meterWorkRailwayActFirst.setArrival(prs.getArrival());
                    meterWorkRailwayActFirst.setStation(prs.getStation());
                    meterWorkRailwayActFirst.setSourceArea(prs.getSourceArea());
                    meterWorkRailwayActFirst.setIsPreSacale("1");
                }

                meterWorkRailwayActFirst.setCreateTime(new Date());
                meterWorkRailwayActFirst.setCreateManName(model.getCreateManName());
                meterWorkRailwayActFirst.setCreateManNo(model.getCreateManNo());
                meterWorkRailwayActFirst.setMeterGroup(model.getMeterGroup());
                meterWorkRailwayActFirst.setMeterClass(model.getMeterClass());
                meterWorkRailwayActFirst.setBaseSpotNo(model.getBaseSpotNo());
                meterWorkRailwayActFirst.setBaseSpotName(model.getBaseSpotName());
                meterWorkRailwayActFirst.setWeightType("1"); //假定为皮重，在出净重时会重新调整状态位
                meterWorkRailwayActFirst.setMeterWeight(model.getMeterWeight());
                meterWorkRailwayActFirst.setMeterSortWeight(zero);
                meterWorkRailwayActFirst.setMeterBalanceWeight(meterWorkRailwayActFirst.getMeterWeight());


                doAddWfStaticFrist(meterWorkRailwayActFirst);
                //保存结净数据
                if (mwraf.getMeterWeight().compareTo(meterWorkRailwayActFirst.getMeterWeight()) >= 0) {
                    //保存历史皮重
                    if (meterWorkRailwayActFirst.getWeightType().equals("1")) {
                        doSaveTare(meterWorkRailwayActFirst);
                    }

                    if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                        //净重数据保存
                        PreRailwayScale preRailwayScale = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
                        actual = doAddWfStaticNet(preRailwayScale, model, meterWorkRailwayActFirst); //预报，毛重，皮重
                    } else {
                        actual = doAddWfStaticNetNot(model, meterWorkRailwayActFirst);
                    }
                    // doAddWfStaticNet(prs, mwraf, meterWorkRailwayActFirst); //预报，毛重，皮重
                } else {
                    //保存历史皮重
                    if (meterWorkRailwayActFirst.getWeightType().equals("1")) {
                        doSaveTare(meterWorkRailwayActFirst);
                    }

                    if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                        //净重数据保存
                        PreRailwayScale preRailwayScale = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
                        actual = doAddWfStaticNet(preRailwayScale, meterWorkRailwayActFirst, mwraf); //预报，毛重，皮重
                    } else {
                        actual = doAddWfStaticNetNot(model, meterWorkRailwayActFirst);
                    }

                    //doAddWfStaticNet(prs, meterWorkRailwayActFirst, mwraf);//预报，皮重，毛重
                }
            }
            //3、修改预报状态为【已使用】
            if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                ids.add(model.getPredictionNo());
                preRailwayScaleMapper.batchUpdateByIdsForUsed(ids);
            }
            firsts.add(model);
            map.put("firsts", firsts);
            actuals.add(actual);
            map.put("actuals", actuals);

        }
        //===========二次计量处理结束==============
        //4、保存图片（一、二次计量共用）
        /*try{
            SaveImg(meterWorkRailwayActFirstMapper.selectByPrimaryKey(id));
        }catch(Exception e){
            e.printStackTrace();
            //throw new MarkerMetException(500, "保存图片失败");
        }*/
        rm.setData(map);

        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doAddWfStaticTwo(PreRailwayScale scale, MeterWorkRailwayActFirst
            grossModel, MeterWorkRailwayActFirst tareModel, boolean needUpload) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        try {
            HashMap<String, Object> map = new HashMap<>();
            List<MeterWorkRailwayActFirst> firsts = new ArrayList<>();
            List<MeterWorkRailwayActual> actuals = new ArrayList<>();

            BigDecimal num = grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()).abs();
            /*if(num.compareTo(BigDecimal.valueOf(400)) == -1){
                rm.setMessage("选择的两条一次计量数据不能小于400kg，请确认！！");
                return rm;
            }*/

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            String afl = sdf.format(new Date());
//            String firstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
            String firstNo = PublicMethod.IdCreate(3);
//            model.setActualFirstNo(no);
            String flag = null;
            //存储当前计量的数据
            if (grossModel.getActualFirstNo() == null) {
                grossModel.setActualFirstNo(firstNo);
                grossModel.setCreateTime(new Date());
                meterWorkRailwayActFirstMapper.insertSelective(grossModel);
                flag = "0";
            }
            if (tareModel.getActualFirstNo() == null) {
                tareModel.setActualFirstNo(firstNo);
                tareModel.setCreateTime(new Date());
                meterWorkRailwayActFirstMapper.insertSelective(tareModel);
                flag = "1";
            }

            if(scale == null) {
                if (!grossModel.getRailwayNo().equals(tareModel.getRailwayNo())) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    rm.setMessage("两条一次计量数据车号不一致,请确认！！");
                    return rm;
                }
            }else {
                if(flag.equals("0") && !scale.getRailwayNo().endsWith(grossModel.getRailwayNo())) {
                    rm.setMessage("两条一次计量数据车号不一致,请确认！！");
                    return rm;
                }else if(flag.equals("1") && !scale.getRailwayNo().endsWith(tareModel.getRailwayNo())) {
                    rm.setMessage("两条一次计量数据车号不一致,请确认！！");
                    return rm;
                }
            }

            String scaleNo = "";
            //从勾选的委托、两个一次数据中赋值委托号
            if(scale != null && scale.getPredictionNo() != null && StringUtils.isNotEmpty(scale.getPredictionNo())){
                if((grossModel.getPredictionNo() != null && StringUtils.isNotEmpty(grossModel.getPredictionNo())
                        && !scale.getPredictionNo().equals(grossModel.getPredictionNo()))){
                    rm.setMessage("毛重一次计量数据与勾选中的委托号不一致,请确认！！");
                    return rm;
                }else if(tareModel.getPredictionNo() != null && StringUtils.isNotEmpty(tareModel.getPredictionNo())
                        && !scale.getPredictionNo().equals(tareModel.getPredictionNo())){
                    rm.setMessage("皮重一次计量数据与勾选中的委托号不一致,请确认！！");
                    return rm;
                }
                scaleNo = scale.getPredictionNo();
            }else if((grossModel.getPredictionNo() != null && StringUtils.isNotEmpty(grossModel.getPredictionNo()))){
                scaleNo = grossModel.getPredictionNo();
            }else if(tareModel.getPredictionNo() != null && StringUtils.isNotEmpty(tareModel.getPredictionNo())){
                scaleNo = tareModel.getPredictionNo();
            }
            scale = preRailwayScaleMapper.selectByPrimaryKey(scaleNo);

            MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();
            //毛重一次
            MeterWorkRailwayActFirst model01 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(grossModel.getActualFirstNo());
            //皮重一次
            MeterWorkRailwayActFirst model02 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(tareModel.getActualFirstNo());

            //修改委托状态
            if (scale != null && StringUtils.isNotEmpty(scale.getPredictionNo())) {
                //委托信息是否为有效数据
                if (scale.getValueFlag().equals("2") || scale.getValueFlag().equals("3")) {
                    rm.setMessage("委托信息已使用或者已作废！！");
                    return rm;
                }

                //判断委托车号是否包含毛重后三位；例如：传的委托里的车号（175029）是以毛重里的车号（029）结尾的，得顺便把毛重的车号改了再结净
                if(scale.getRailwayNo().endsWith(model01.getRailwayNo())) {
                    model01.setRailwayNo(scale.getRailwayNo());
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model01);
                }

                //将委托中的编号和收发货的等数据拷贝进一次数据中
                MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first1, model01);
                MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(model02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first2, model02);

                //修改毛重一次数据状态
                model01.setValueFlag(DbConstants.NET);
                model01.setUpdateManNo("system");
                model01.setUpdateManName("自动匹配更新");
                model01.setUpdateTime(new Date());
                model01.setWeightType("0");
                model01.setIsPreSacale("1");
                model01.setPredictionNo(scale.getPredictionNo());
                model01.setPredictionType("1");
                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model01);
                //修改皮重一次数据状态
                model02.setValueFlag(DbConstants.NET);
                model02.setUpdateManNo("system");
                model02.setUpdateManName("自动匹配更新");
                model02.setUpdateTime(new Date());
                model02.setWeightType("1");
                model02.setIsPreSacale("1");
                model02.setPredictionNo(scale.getPredictionNo());
                model02.setPredictionType("1");
                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model02);
                doSaveTare(model02);

                MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(model01.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
//                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);
                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);
                actualRow.setNetTime(new Date());
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setNote("自动匹配结净");
                actualRow.setNetMode("1");
                actualRow.setUploadFlag("1");
                actualRow.setValueFlag("1");
                actualRow.setIsPreSacale("1");
                actualRow.setPredictionNo(scale.getPredictionNo());
                actualRow.setPredictionType("1");
                actualRow.setNetWeight(model01.getMeterWeight().subtract(model02.getMeterWeight()));
                if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                    actualRow.setNetManNo(actualRow.getGrossManNo());
                    actualRow.setNetManName(actualRow.getGrossManName());
                    actualRow.setNetTime(actualRow.getGrossTime());
                    actualRow.setCreateTime(actualRow.getGrossTime());
                    actualRow.setCreateManName(actualRow.getGrossManName());
                    actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                }
                //是否发布 ture发布，false不发布
                if(needUpload) {
                    if(actualRow.getIsPreSacale() != null && actualRow.getIsPreSacale().equals("1")) {
                        actualRow.setValueFlag("2");
                        actualRow.setUploadFlag("1");
                    }
                }
                meterWorkRailwayActualMapper.insertSelective(actualRow);

                //修改委托编号
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("自动匹配更新");
                scale.setUploadFlag("1");
                scale.setPredictionType("1");
                scale.setMeasureStation(actualRow.getNetSpot3No());
                scale.setMeasureStationName(actualRow.getNetSpot3Name());
                preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
            } else {
                //修改毛重一次数据状态
                model01.setValueFlag(DbConstants.NET);
                model01.setUpdateManNo("system");
                model01.setUpdateManName("自动匹配更新");
                model01.setUpdateTime(new Date());
                model01.setWeightType("0");
                model01.setIsPreSacale("0");
                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model01);
                //修改皮重一次数据状态
                model02.setValueFlag(DbConstants.NET);
                model02.setUpdateManNo("system");
                model02.setUpdateManName("自动匹配更新");
                model02.setUpdateTime(new Date());
                model02.setWeightType("1");
                model02.setIsPreSacale("0");
                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model02);
                doSaveTare(model02);

                MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAdd(model01.getActualFirstNo(), model02.getActualFirstNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
//                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);
                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);
                actualRow.setNetTime(new Date());
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setNote("自动匹配结净");
                actualRow.setNetMode("1");
                actualRow.setUploadFlag("1");
                actualRow.setValueFlag("1");
                actualRow.setIsPreSacale("0");
                actualRow.setNetWeight(model01.getMeterWeight().subtract(model02.getMeterWeight()));
                if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                    actualRow.setNetManNo(actualRow.getGrossManNo());
                    actualRow.setNetManName(actualRow.getGrossManName());
                    actualRow.setNetTime(actualRow.getGrossTime());
                    actualRow.setCreateTime(actualRow.getGrossTime());
                    actualRow.setCreateManName(actualRow.getGrossManName());
                    actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                }
                meterWorkRailwayActualMapper.insertSelective(actualRow);
            }

            if (flag.equals("0")) {
                firsts.add(model01);
            } else if (flag.equals("1")) {
                firsts.add(model02);
            }
            actuals.add(actualRow);
            map.put("firsts", firsts);
            map.put("actuals", actuals);

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

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void doSaveTare(MeterWorkRailwayActFirst model) {
        //获取ID
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String afl = formatter.format(model.getCreateTime());
        //查找相应车辆有效的历史皮重信息
        MeterBaseRailwayHisTareData hisTareData = new MeterBaseRailwayHisTareData();
        hisTareData.setRailwayNo(model.getRailwayNo());
        hisTareData.setValueFlag("0");
        Map<String, Object> map = FieldsCollector.beanToMap(hisTareData);
        List<MeterBaseRailwayHisTareData> tareOne = meterBaseRailwayHisTareDataMapper.selectByParameters(map);
        if (tareOne.size() >= 1) {   //已有历史皮重
            hisTareData.setDataNo(tareOne.get(0).getDataNo());
            hisTareData.setBaseSpotNo(model.getBaseSpotNo());
            hisTareData.setBaseSpotName(model.getBaseSpotName());
            hisTareData.setMeterName(model.getMatterName());
            hisTareData.setMeterTime(model.getCreateTime());
            hisTareData.setMeterWeight(Math.round(model.getMeterWeight().doubleValue()));
            hisTareData.setUpWeight(tareOne.get(0).getMeterWeight());
            hisTareData.setUpdateManName(model.getCreateManName());
            hisTareData.setUpdateManNo(model.getCreateManNo());
            hisTareData.setUpdateTime(model.getCreateTime());
            //hisTareData.setTareType("1");
            //查询前几次皮重
            double meterWeight = 0.0;
            try {
                MeterBaseRailwayHisTareDetail mbhtd = new MeterBaseRailwayHisTareDetail();
                mbhtd.setCarNo(model.getRailwayNo());
                Map<String, Object> map1 = FieldsCollector.beanToMap(mbhtd);
                List<MeterBaseRailwayHisTareDetail> lmd = meterBaseRailwayHisTareDetailMapper.selectByParametersTop(map1);
                if(lmd!=null&&lmd.size()!=0){
                    double sum = 0.0;
                    for (MeterBaseRailwayHisTareDetail meterBaseRailwayHisTareDetail : lmd) {
                        sum+=meterBaseRailwayHisTareDetail.getMeterWeight();
                    }
                    meterWeight = sum / lmd.size();
                }
            } catch (Exception e) {
                logger.error("计算平均皮重出错", e);
            }
            //  (之前的重量*次数)/(次数+1)
            //double meterWeight = (tareOne.get(0).getAvgWeight() * tareOne.get(0).getMeterNum() + model.getMeterWeight().longValue()) / (tareOne.get(0).getMeterNum() + 1);
            hisTareData.setAvgWeight(Math.round(meterWeight));//平均皮重
            hisTareData.setMeterNum(tareOne.get(0).getMeterNum() + 1);
            meterBaseRailwayHisTareDataMapper.updateByPrimaryKeySelective(hisTareData);
        } else {     //已有历史皮重
            //新增操作
            String no = meterBaseRailwayHisTareDataMapper.getNewID(afl);
            hisTareData.setDataNo(afl + no);
            hisTareData.setBaseSpotNo(model.getBaseSpotNo());
            hisTareData.setBaseSpotName(model.getBaseSpotName());
            hisTareData.setMeterName(model.getMatterName());
            hisTareData.setMeterTime(model.getCreateTime());
            hisTareData.setMeterWeight(Math.round(model.getMeterWeight().doubleValue()));
            hisTareData.setUpWeight(model.getMeterWeight().longValue()); //上次皮重
            hisTareData.setAvgWeight(Math.round(model.getMeterWeight().doubleValue()));//平均皮重
            hisTareData.setMeterNum(1l);
            hisTareData.setCreateTime(model.getCreateTime());
            hisTareData.setCreateManName(model.getCreateManName());
            hisTareData.setCreateManNo(model.getCreateManNo());
            hisTareData.setTareType("1");
            meterBaseRailwayHisTareDataMapper.insert(hisTareData);
        }
        MeterBaseRailwayHisTareDetail mbhtd = new MeterBaseRailwayHisTareDetail();
        String no = meterBaseRailwayHisTareDetailMapper.getNewID(afl);
        mbhtd.setDetailNo(afl + no);
        mbhtd.setActualFirstNo(model.getActualFirstNo());
        mbhtd.setBaseSpotNo(model.getBaseSpotNo());
        mbhtd.setBaseSpotName(model.getBaseSpotName());
        mbhtd.setCarNo(model.getRailwayNo());
        mbhtd.setMeterOrder("1");
        mbhtd.setMeterTime(model.getCreateTime());
        mbhtd.setMeterName(model.getCreateManName());
        mbhtd.setMeterWeight(model.getMeterWeight().longValue());
        mbhtd.setValueFlag("0");
        meterBaseRailwayHisTareDetailMapper.insert(mbhtd);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void doSaveTare(MeterWorkRailwayActFirst model, String tareType) {
        //获取ID
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String afl = formatter.format(model.getCreateTime());
        //查找相应车辆有效的历史皮重信息
        MeterBaseRailwayHisTareData hisTareData = new MeterBaseRailwayHisTareData();
        hisTareData.setRailwayNo(model.getRailwayNo());
        hisTareData.setValueFlag("0");
        hisTareData.setTareType(tareType);
        Map<String, Object> map = FieldsCollector.beanToMap(hisTareData);
        List<MeterBaseRailwayHisTareData> tareOne = meterBaseRailwayHisTareDataMapper   .selectByParameters(map);
        if (tareOne.size() >= 1) {   //已有历史皮重
            hisTareData.setDataNo(tareOne.get(0).getDataNo());
            hisTareData.setBaseSpotNo(model.getBaseSpotNo());
            hisTareData.setBaseSpotName(model.getBaseSpotName());
            hisTareData.setMeterName(model.getMatterName());
            hisTareData.setMeterTime(model.getCreateTime());
            hisTareData.setMeterWeight(Math.round(model.getMeterWeight().doubleValue()));
            hisTareData.setUpWeight(tareOne.get(0).getMeterWeight());
            hisTareData.setUpdateManName(model.getCreateManName());
            hisTareData.setUpdateManNo(model.getCreateManNo());
            hisTareData.setUpdateTime(model.getCreateTime());
            //查询前几次皮重
            double meterWeight = 0.0;
            try {
                MeterBaseRailwayHisTareDetail mbhtd = new MeterBaseRailwayHisTareDetail();
                mbhtd.setCarNo(model.getRailwayNo());
                Map<String, Object> map1 = FieldsCollector.beanToMap(mbhtd);
                List<MeterBaseRailwayHisTareDetail> lmd = meterBaseRailwayHisTareDetailMapper.selectByParametersTop(map1);
                if(lmd!=null&&lmd.size()!=0){
                    double sum = 0.0;
                    for (MeterBaseRailwayHisTareDetail meterBaseRailwayHisTareDetail : lmd) {
                        sum+=meterBaseRailwayHisTareDetail.getMeterWeight();
                    }
                    meterWeight = sum / lmd.size();
                }
            } catch (Exception e) {
                logger.error("计算平均皮重出错", e);
            }
            //  (之前的重量*次数)/(次数+1)
            //double meterWeight = (tareOne.get(0).getAvgWeight() * tareOne.get(0).getMeterNum() + model.getMeterWeight().longValue()) / (tareOne.get(0).getMeterNum() + 1);
            hisTareData.setAvgWeight(Math.round(meterWeight));//平均皮重
            hisTareData.setMeterNum(tareOne.get(0).getMeterNum() + 1);
            hisTareData.setTareType(tareType);
            meterBaseRailwayHisTareDataMapper.updateByPrimaryKeySelective(hisTareData);
        } else {     //已有历史皮重
            //新增操作
            String no = meterBaseRailwayHisTareDataMapper.getNewID(afl);
            hisTareData.setDataNo(afl + no);
            hisTareData.setBaseSpotNo(model.getBaseSpotNo());
            hisTareData.setBaseSpotName(model.getBaseSpotName());
            hisTareData.setMeterName(model.getMatterName());
            hisTareData.setMeterTime(model.getCreateTime());
            hisTareData.setMeterWeight(Math.round(model.getMeterWeight().doubleValue()));
            hisTareData.setUpWeight(model.getMeterWeight().longValue()); //上次皮重
            hisTareData.setAvgWeight(Math.round(model.getMeterWeight().doubleValue()));//平均皮重
            hisTareData.setMeterNum(1l);
            hisTareData.setCreateTime(model.getCreateTime());
            hisTareData.setTareType(tareType);
            hisTareData.setCreateManName(model.getCreateManName());
            hisTareData.setCreateManNo(model.getCreateManNo());
            hisTareData.setCreateTime(new Date());
            meterBaseRailwayHisTareDataMapper.insert(hisTareData);
        }
        MeterBaseRailwayHisTareDetail mbhtd = new MeterBaseRailwayHisTareDetail();
        String no = meterBaseRailwayHisTareDetailMapper.getNewID(afl);
        mbhtd.setDetailNo(afl + no);
        mbhtd.setActualFirstNo(model.getActualFirstNo());
        mbhtd.setBaseSpotNo(model.getBaseSpotNo());
        mbhtd.setBaseSpotName(model.getBaseSpotName());
        mbhtd.setCarNo(model.getRailwayNo());
        mbhtd.setMeterOrder("1");
        mbhtd.setMeterTime(model.getCreateTime());
        mbhtd.setMeterName(model.getCreateManName());
        mbhtd.setMeterWeight(model.getMeterWeight().longValue());
        mbhtd.setValueFlag("0");
        meterBaseRailwayHisTareDetailMapper.insert(mbhtd);
    }


    //保存计量数据
    private String doAddWfStaticFrist(MeterWorkRailwayActFirst model) {
        //此为第一次计量的数据
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(model.getCreateTime());
        //一次数据
//        String firstNo = meterWorkRailwayActFirstMapper.getNewID(afl);
//        model.setActualFirstNo(afl + firstNo);
        String no = PublicMethod.IdCreate(3);
        model.setActualFirstNo(no);
        model.setValueFlag("1");
        model.setCheckFlag("0");
        model.setMeterMode("1");
        model.setCreateTime(new Date());
        meterWorkRailwayActFirstMapper.insertSelective(model);
        return model.getActualFirstNo();
    }

    //保存计量数据
    private String doAddWfActFrist(MeterWorkRailwayActFirst model) {
        if (StringUtils.isEmpty(model.getActualFirstNo())) {
            //此为第一次计量的数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(model.getCreateTime());
            //一次数据
//            String firstNo = meterWorkRailwayActFirstMapper.getNewID(afl);
//            model.setActualFirstNo(afl + firstNo);
            String firstNo = PublicMethod.IdCreate(3);
            model.setActualFirstNo(firstNo);
        }
        model.setValueFlag("1");
        model.setCheckFlag("0");
        model.setMeterMode("1");
        model.setCreateTime(new Date());
        meterWorkRailwayActFirstMapper.insertSelective(model);
        return model.getActualFirstNo();
    }

    //混装预报补充数据
    private MeterWorkRailwayActFirst doAddWfStaticFristImp(PreRailwayScale prs, MeterWorkRailwayActFirst firstRail) {
        //补充数据
        firstRail.setPredictionNo(prs.getPredictionNo());
        firstRail.setNoticeNo(prs.getNoticeNo());
        firstRail.setMatterNo(prs.getMatterNo());
        firstRail.setMatterName(prs.getMatterName());
        firstRail.setContractNo(prs.getContractNo());
        firstRail.setBatchNo(prs.getBatchNo());
        firstRail.setCustomerSupplierNo(prs.getCustomerSupplierNo());
        firstRail.setCustomerSupplierName(prs.getCustomerSupplierName());
        firstRail.setForwardingUnitNo(prs.getForwardingUnitNo());
        firstRail.setForwardingUnitName(prs.getForwardingUnitName());
        firstRail.setReceivingUintNo(prs.getReceivingUintNo());
        firstRail.setReceivingUintName(prs.getReceivingUintName());
        firstRail.setMaterialNo(prs.getMaterialNo());
        firstRail.setMaterialName(prs.getMaterialName());
        firstRail.setSpecNo(prs.getSpecNo());
        firstRail.setSpecName(prs.getSpecName());
        firstRail.setShipmentGrossWeight(prs.getShipmentGrossWeight());
        firstRail.setShipmentNetWeight(prs.getShipmentNetWeight());
        firstRail.setShipmentNum(prs.getShipmentNum());
        firstRail.setLoadPointNo(prs.getLoadPointNo());
        firstRail.setLoadPointName(prs.getLoadPointName());
        firstRail.setSampleNo(prs.getSampleNo());
        firstRail.setSampleVoucher(prs.getSampleVoucher());
        firstRail.setCarrierUnitNo(prs.getCarrierUnitNo());
        firstRail.setCarrierUnitName(prs.getCarrierUnitName());
        firstRail.setPredictionType(prs.getPredictionType());
        firstRail.setPredictionCombination(prs.getPredictionCombination());
        firstRail.setMeterTypeNo(prs.getMeterTypeNo());
        firstRail.setMeterTypeName(prs.getMeterTypeName());
        firstRail.setIsChemical(prs.getIsChemical());
        firstRail.setLimitWeight(prs.getLimitWeight());
        firstRail.setStation(prs.getStation());
        firstRail.setArrival(prs.getArrival());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(firstRail.getCreateTime());
        //一次数据
//        String firstNo = meterWorkRailwayActFirstMapper.getNewID(afl);
//        firstRail.setActualFirstNo(afl + firstNo);
        String firstNo = PublicMethod.IdCreate(3);
        firstRail.setActualFirstNo(firstNo);
        firstRail.setValueFlag("1");
        firstRail.setCheckFlag("0");
        firstRail.setCreateTime(new Date());
        meterWorkRailwayActFirstMapper.insertSelective(firstRail);
        return firstRail;
    }


    /**
     * 需要两次计量时完善结净数据
     *
     * @param prs 预报
     * @param one 毛重实体
     * @param two 皮重实体
     * @return
     */
    private MeterWorkRailwayActual doAddWfStaticNet(PreRailwayScale prs, MeterWorkRailwayActFirst
            one, MeterWorkRailwayActFirst two) {
        MeterWorkRailwayActual netRail = new MeterWorkRailwayActual();
        //补充编号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(new Date());
       /* String firstno = meterWorkRailwayActFirstMapper.getNewID(afl);
        one.setActualFirstNo(afl + firstno);
        meterWorkRailwayActFirstMapper.insertSelective(one);*/

//        String netNo = meterWorkRailwayActualMapper.getNewID(afl);
//        netRail.setActualNo(afl + netNo);
        String netNo = PublicMethod.IdCreate(3);
        netRail.setActualNo(netNo);

        //补充数据
        netRail.setPredictionNo(prs.getPredictionNo());
        netRail.setIsPreSacale("1");
        netRail.setNoticeNo(prs.getNoticeNo());
        netRail.setMatterNo(prs.getMatterNo());
        netRail.setMatterName(prs.getMatterName());
        netRail.setContractNo(prs.getContractNo());
        netRail.setBatchNo(prs.getBatchNo());
        netRail.setCustomerSupplierNo(prs.getCustomerSupplierNo());
        netRail.setCustomerSupplierName(prs.getCustomerSupplierName());
        netRail.setForwardingUnitNo(prs.getForwardingUnitNo());
        netRail.setForwardingUnitName(prs.getForwardingUnitName());
        netRail.setReceivingUintNo(prs.getReceivingUintNo());
        netRail.setReceivingUintName(prs.getReceivingUintName());
        netRail.setMaterialNo(prs.getMaterialNo());
        netRail.setMaterialName(prs.getMaterialName());
        netRail.setSpecNo(prs.getSpecNo());
        netRail.setSpecName(prs.getSpecName());
        netRail.setLoadPointNo(prs.getLoadPointNo());
        netRail.setLoadPointName(prs.getLoadPointName());
        netRail.setSampleNo(prs.getSampleNo());
        netRail.setSampleVoucher(prs.getSampleVoucher());
        netRail.setCarrierUnitNo(prs.getCarrierUnitNo());
        netRail.setCarrierUnitName(prs.getCarrierUnitName());
        netRail.setMeterTypeNo(prs.getMeterTypeNo());
        netRail.setMeterTypeName(prs.getMeterTypeName());
        netRail.setStation(prs.getStation());
        netRail.setArrival(prs.getArrival());
        netRail.setSourceArea(prs.getSourceArea());

        //将毛重数据补充进来
        netRail.setRailwayNo(one.getRailwayNo());
        netRail.setRailwayCarriageNo(one.getRailwayCarriageNo());
        netRail.setActualFirst1No(one.getActualFirstNo());
        netRail.setGrossWeight(one.getMeterBalanceWeight());
        netRail.setGrossManNo(one.getCreateManNo());
        netRail.setGrossManName(one.getCreateManName());
        netRail.setGrossTime(one.getCreateTime());
        netRail.setBaseSpot1No(one.getBaseSpotNo());
        netRail.setBaseSpot1Name(one.getBaseSpotName());
        netRail.setGrossClass(one.getMeterClass());
        netRail.setGrossGroup(one.getMeterGroup());
        netRail.setGrossMode(one.getMeterMode());
        netRail.setRailwayTypeNo(one.getRailwayTypeNo());
        netRail.setRailwayTypeName(one.getRailwayTypeName());
        netRail.setWaterNum(one.getWaterNum());
        //将皮重数据补充
        netRail.setActualFirst2No(two.getActualFirstNo());
        netRail.setTareWeight(two.getMeterWeight());
        netRail.setTareManNo(two.getCreateManNo());
        netRail.setTareManName(two.getCreateManName());
        netRail.setTareTime(two.getCreateTime());
        netRail.setBaseSpot2No(two.getBaseSpotNo());
        netRail.setBaseSpot2Name(two.getBaseSpotName());
        netRail.setTareClass(two.getMeterClass());
        netRail.setTareGroup(two.getMeterGroup());
        netRail.setTareMode(two.getMeterMode());
        //补充净重数据

        if (one.getCreateTime().after(two.getCreateTime())) {
            netRail.setNetManNo(one.getCreateManNo());
            netRail.setNetManName(one.getCreateManName());
            netRail.setNetTime(one.getCreateTime());
            netRail.setNetSpot3No(one.getBaseSpotNo());
            netRail.setNetSpot3Name(one.getBaseSpotName());
            netRail.setNetClass(one.getMeterClass());
            netRail.setNetGroup(one.getMeterGroup());
        } else {
            netRail.setNetManNo(two.getCreateManNo());
            netRail.setNetManName(two.getCreateManName());
            netRail.setNetTime(two.getCreateTime());
            netRail.setNetSpot3No(two.getBaseSpotNo());
            netRail.setNetSpot3Name(two.getBaseSpotName());
            netRail.setNetClass(two.getMeterClass());
            netRail.setNetGroup(two.getMeterGroup());
        }
        netRail.setNetWeight(one.getMeterBalanceWeight().subtract(two.getMeterBalanceWeight()));
        netRail.setNetMode("1");
        netRail.setValueFlag("1");
        netRail.setUploadFlag("1");
        if ("001019002".equals(one.getRailwayTypeNo())) {
            netRail.setNetTime(new Date()); //动轨的特殊处理
            //假如一次数据为补录，则结净数据中的grossModel为3，标识毛重为手工录入的
            if ("补录".equals(one.getRailwayModelName())) {
                netRail.setGrossMode("3");
                //METER_WORK_RAILWAY_ACTUAL_BAR
                if(netRail.getGrossTime().compareTo(netRail.getTareTime()) >= 0) {
                    netRail.setNetSpot3No(netRail.getBaseSpot1No());
                    netRail.setNetSpot3Name(netRail.getBaseSpot1Name());
                    netRail.setNetManNo(netRail.getGrossManNo());
                    netRail.setNetManName(netRail.getGrossManName());
                    netRail.setNetTime(netRail.getGrossTime());
                    netRail.setCreateTime(netRail.getGrossTime());
                    netRail.setCreateManName(netRail.getGrossManName());
                    netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst1No()));
                }else{
                    netRail.setNetSpot3No(netRail.getBaseSpot2No());
                    netRail.setNetSpot3Name(netRail.getBaseSpot2Name());
                    netRail.setNetManNo(netRail.getTareManNo());
                    netRail.setNetManName(netRail.getTareManName());
                    netRail.setNetTime(netRail.getTareTime());
                    netRail.setCreateTime(netRail.getTareTime());
                    netRail.setCreateManName(netRail.getTareManName());
                    netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst2No()));
                }
                meterWorkRailwayActualMapper.insertSelectiveBar(netRail);
            }
        }
        if(netRail.getGrossTime().compareTo(netRail.getTareTime()) >= 0) {
            netRail.setNetSpot3No(netRail.getBaseSpot1No());
            netRail.setNetSpot3Name(netRail.getBaseSpot1Name());
            netRail.setNetManNo(netRail.getGrossManNo());
            netRail.setNetManName(netRail.getGrossManName());
            netRail.setNetTime(netRail.getGrossTime());
            netRail.setCreateTime(netRail.getGrossTime());
            netRail.setCreateManName(netRail.getGrossManName());
            netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst1No()));
        }else{
            netRail.setNetSpot3No(netRail.getBaseSpot2No());
            netRail.setNetSpot3Name(netRail.getBaseSpot2Name());
            netRail.setNetManNo(netRail.getTareManNo());
            netRail.setNetManName(netRail.getTareManName());
            netRail.setNetTime(netRail.getTareTime());
            netRail.setCreateTime(netRail.getTareTime());
            netRail.setCreateManName(netRail.getTareManName());
            netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst2No()));
        }
        meterWorkRailwayActualMapper.insertSelective(netRail);


        //修改一次的状态
        one.setValueFlag("2");//结净
        one.setWeightType("0");//毛重
        two.setValueFlag("2");//结净
        two.setWeightType("1");//皮重
        meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(one);
        //meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(tow);
        meterWorkRailwayActFirstMapper.deleteByPrimaryKey(two.getActualFirstNo());
        return netRail;
    }

    /**
     * 需要两次计量时完善结净数据
     *
     * @param one 毛重实体
     * @param two 皮重实体
     * @return
     */
    private MeterWorkRailwayActual doAddWfStaticNetNot(MeterWorkRailwayActFirst one, MeterWorkRailwayActFirst two) {
        MeterWorkRailwayActual netRail = new MeterWorkRailwayActual();
        //补充编号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(new Date());
//        String netNo = meterWorkRailwayActualMapper.getNewID(afl);
//        netRail.setActualNo(afl + netNo);
        String netNo = PublicMethod.IdCreate(3);
        netRail.setActualNo(netNo);

        //补充数据
        /*netRail.setPredictionNo(prs.getPredictionNo());
        netRail.setNoticeNo(prs.getNoticeNo());
        netRail.setMatterNo(prs.getMatterNo());
        netRail.setMatterName(prs.getMatterName());
        netRail.setContractNo(prs.getContractNo());
        netRail.setBatchNo(prs.getBatchNo());
        netRail.setCustomerSupplierNo(prs.getCustomerSupplierNo());
        netRail.setCustomerSupplierName(prs.getCustomerSupplierName());
        netRail.setForwardingUnitNo(prs.getForwardingUnitNo());
        netRail.setForwardingUnitName(prs.getForwardingUnitName());
        netRail.setReceivingUintNo(prs.getReceivingUintNo());
        netRail.setReceivingUintName(prs.getReceivingUintName());
        netRail.setMaterialNo(prs.getMaterialNo());
        netRail.setMaterialName(prs.getMaterialName());
        netRail.setSpecNo(prs.getSpecNo());
        netRail.setSpecName(prs.getSpecName());
        netRail.setLoadPointNo(prs.getLoadPointNo());
        netRail.setLoadPointName(prs.getLoadPointName());
        netRail.setSampleNo(prs.getSampleNo());
        netRail.setSampleVoucher(prs.getSampleVoucher());
        netRail.setCarrierUnitNo(prs.getCarrierUnitNo());
        netRail.setCarrierUnitName(prs.getCarrierUnitName());
        netRail.setMeterTypeNo(prs.getMeterTypeNo());
        netRail.setMeterTypeName(prs.getMeterTypeName());
        netRail.setStation(prs.getStation());
        netRail.setArrival(prs.getArrival());
        netRail.setSourceArea(prs.getSourceArea());*/

        //将毛重数据补充进来
        netRail.setRailwayNo(one.getRailwayNo());
        netRail.setRailwayCarriageNo(one.getRailwayCarriageNo());
        netRail.setActualFirst1No(one.getActualFirstNo());
        netRail.setGrossWeight(one.getMeterBalanceWeight());
        netRail.setGrossManNo(one.getCreateManNo());
        netRail.setGrossManName(one.getCreateManName());
        netRail.setGrossTime(one.getCreateTime());
        netRail.setBaseSpot1No(one.getBaseSpotNo());
        netRail.setBaseSpot1Name(one.getBaseSpotName());
        netRail.setGrossClass(one.getMeterClass());
        netRail.setGrossGroup(one.getMeterGroup());
        netRail.setGrossMode(one.getMeterMode());
        netRail.setRailwayTypeNo(one.getRailwayTypeNo());
        netRail.setRailwayTypeName(one.getRailwayTypeName());
        netRail.setWaterNum(one.getWaterNum());
        //将皮重数据补充
        netRail.setActualFirst2No(two.getActualFirstNo());
        netRail.setTareWeight(two.getMeterWeight());
        netRail.setTareManNo(two.getCreateManNo());
        netRail.setTareManName(two.getCreateManName());
        netRail.setTareTime(two.getCreateTime());
        netRail.setBaseSpot2No(two.getBaseSpotNo());
        netRail.setBaseSpot2Name(two.getBaseSpotName());
        netRail.setTareClass(two.getMeterClass());
        netRail.setTareGroup(two.getMeterGroup());
        netRail.setTareMode(two.getMeterMode());
        //补充净重数据

        if (one.getCreateTime().after(two.getCreateTime())) {
            netRail.setNetManNo(one.getCreateManNo());
            netRail.setNetManName(one.getCreateManName());
            netRail.setNetTime(one.getCreateTime());
            netRail.setNetSpot3No(one.getBaseSpotNo());
            netRail.setNetSpot3Name(one.getBaseSpotName());
            netRail.setNetClass(one.getMeterClass());
            netRail.setNetGroup(one.getMeterGroup());
        } else {
            netRail.setNetManNo(two.getCreateManNo());
            netRail.setNetManName(two.getCreateManName());
            netRail.setNetTime(two.getCreateTime());
            netRail.setNetSpot3No(two.getBaseSpotNo());
            netRail.setNetSpot3Name(two.getBaseSpotName());
            netRail.setNetClass(two.getMeterClass());
            netRail.setNetGroup(two.getMeterGroup());
        }
        netRail.setNetWeight(one.getMeterWeight().subtract(two.getMeterWeight()));
        netRail.setNetMode("1");
        netRail.setValueFlag("1");
        netRail.setUploadFlag("1");
        if ("001019002".equals(one.getRailwayTypeNo())) {
            netRail.setNetTime(new Date()); //动轨的特殊处理
            //假如一次数据为补录，则结净数据中的grossModel为3，标识毛重为手工录入的
            if ("补录".equals(one.getRailwayModelName())) {
                netRail.setGrossMode("3");
                //METER_WORK_RAILWAY_ACTUAL_BAR
                if(netRail.getGrossTime().compareTo(netRail.getTareTime()) >= 0) {
                    netRail.setNetSpot3No(netRail.getBaseSpot1No());
                    netRail.setNetSpot3Name(netRail.getBaseSpot1Name());
                    netRail.setNetManNo(netRail.getGrossManNo());
                    netRail.setNetManName(netRail.getGrossManName());
                    netRail.setNetTime(netRail.getGrossTime());
                    netRail.setCreateTime(netRail.getGrossTime());
                    netRail.setCreateManName(netRail.getGrossManName());
                    netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst1No()));
                }else{
                    netRail.setNetSpot3No(netRail.getBaseSpot2No());
                    netRail.setNetSpot3Name(netRail.getBaseSpot2Name());
                    netRail.setNetManNo(netRail.getTareManNo());
                    netRail.setNetManName(netRail.getTareManName());
                    netRail.setNetTime(netRail.getTareTime());
                    netRail.setCreateTime(netRail.getTareTime());
                    netRail.setCreateManName(netRail.getTareManName());
                    netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst2No()));
                }
                meterWorkRailwayActualMapper.insertSelectiveBar(netRail);
            }
        }
        if(netRail.getGrossTime().compareTo(netRail.getTareTime()) >= 0) {
            netRail.setNetSpot3No(netRail.getBaseSpot1No());
            netRail.setNetSpot3Name(netRail.getBaseSpot1Name());
            netRail.setNetManNo(netRail.getGrossManNo());
            netRail.setNetManName(netRail.getGrossManName());
            netRail.setNetTime(netRail.getGrossTime());
            netRail.setCreateTime(netRail.getGrossTime());
            netRail.setCreateManName(netRail.getGrossManName());
            netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst1No()));
        }else{
            netRail.setNetSpot3No(netRail.getBaseSpot2No());
            netRail.setNetSpot3Name(netRail.getBaseSpot2Name());
            netRail.setNetManNo(netRail.getTareManNo());
            netRail.setNetManName(netRail.getTareManName());
            netRail.setNetTime(netRail.getTareTime());
            netRail.setCreateTime(netRail.getTareTime());
            netRail.setCreateManName(netRail.getTareManName());
            netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst2No()));
        }
        meterWorkRailwayActualMapper.insertSelective(netRail);


        //修改一次的状态
        one.setValueFlag("2");//结净
        one.setWeightType("0");//毛重
        two.setValueFlag("2");//结净
        two.setWeightType("1");//皮重s
        meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(one);
        meterWorkRailwayActFirstMapper.deleteByPrimaryKey(two.getActualFirstNo());
        return netRail;
    }

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

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


    @Override
    @Transactional
    public String doAddWfAct(PreRailwayScale prs, MeterWorkRailwayActFirst first, MeterBaseRailwayWeight baseWeight) {
        String id = "";

        PreRailwayScale prsM = preRailwayScaleMapper.selectByPrimaryKey(prs.getPredictionNo());
        String fNo = first.getActualFirstNo();
        //1、保存一次数据
        first.setWeightType("0");//毛重
        BigDecimal a = first.getMeterWeight().multiply(new BigDecimal(1000));
        first.setMeterWeight(new BigDecimal(a.doubleValue()));
        first.setMeterBalanceWeight(first.getMeterWeight());
        id = doAddWfActFrist(first);

        //2、补存一条一次皮重数据
        MeterWorkRailwayActFirst tareWeight = new MeterWorkRailwayActFirst();
        tareWeight.setMeterWeight(baseWeight.getRailwayWeight());
        tareWeight.setMeterBalanceWeight(tareWeight.getMeterWeight());
        tareWeight.setCreateManNo(baseWeight.getCreateManNo());
        tareWeight.setCreateManName(baseWeight.getCreateManName());
        tareWeight.setCreateTime(baseWeight.getCreateTime());
        tareWeight.setRailwayNo(first.getRailwayNo());
        tareWeight.setWeightType("2");//期限皮重
        doAddWfActFrist(tareWeight);
        //3、保存结净数据
        doAddWfStaticNet(prs, first, tareWeight);

        //4、修改预报状态
        prs.setValueFlag("2");//已使用
        prs.setUploadFlag("1");
        prs.setMeasureStation(tareWeight.getBaseSpotNo());
        prs.setMeasureStationName(tareWeight.getBaseSpotName());
        preRailwayScaleMapper.updateByPrimaryKeySelective(prs);


        preRailwayScaleMapper.updateMeterWorkRailwayReceived(fNo);
        //状态（0：未配车；1：未使用；2：已使用；3：已作废；4：正使用）
        if ("00025".equals(baseWeight.getTareType())) { //选矿的才
            int iCount = Integer.parseInt(prsM.getRailwayCarriageNo()) - 1;
            if (iCount > 0) {
                prsM.setRailwayCarriageNo(iCount + "");
                prsM.setValueFlag("1");//未使用
            } else {
                prsM.setRailwayCarriageNo("0");
                prsM.setValueFlag("2");//已使用
            }
        } else {//砂石永远为1
            prsM.setValueFlag("1");//未使用
        }
        prsM.setUploadFlag("1");
        prs.setMeasureStation(tareWeight.getBaseSpotNo());
        prs.setMeasureStationName(tareWeight.getBaseSpotName());
        preRailwayScaleMapper.updateByPrimaryKeySelective(prsM);


        return id;
    }

    //覆盖期限皮重
    public void updateRailWeight(MeterWorkRailwayActFirst model) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("railwayNo", model.getRailwayNo());
            List<MeterBaseRailwayWeight> weightT = meterBaseRailwayWeightMapper.selectByParameters(obj);
            if (weightT.size() > 0) {
                //修改
                MeterBaseRailwayWeight railwayWeight = new MeterBaseRailwayWeight();
                railwayWeight.setRailwayPk(weightT.get(0).getRailwayPk());
                railwayWeight.setRailwayWeight(model.getMeterWeight());
                railwayWeight.setUpdateManNo(model.getCreateManNo());
                railwayWeight.setUpdateManName(model.getCreateManName());
                railwayWeight.setUpdateTime(model.getCreateTime());
                railwayWeight.setStartTime(sdf.parse(sdf.format(model.getCreateTime())));
                //计算结束时间
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(railwayWeight.getStartTime());
                calendar.add(Calendar.DATE, weightT.get(0).getTermValidity());
                railwayWeight.setEndTime(calendar.getTime());
                meterBaseRailwayWeightMapper.updateByPrimaryKeySelective(railwayWeight);
            } else {
                //新增
                MeterBaseRailwayWeight railwayWeight = new MeterBaseRailwayWeight();
                String no = meterBaseRailwayWeightMapper.GetNewID();
                railwayWeight.setRailwayPk(new SimpleDateFormat("yyyyMMdd").format(model.getCreateTime()) + no);
                railwayWeight.setRailwayNo(model.getRailwayNo());
                railwayWeight.setValueFlag("0");//有效
                railwayWeight.setRailwayTypeNo("001019002");
                railwayWeight.setRailwayTypeName("动态轨道衡");
                railwayWeight.setRailwayType("0");
                railwayWeight.setRailwayWeight(model.getMeterWeight());
                railwayWeight.setCreateManNo(model.getCreateManNo());
                railwayWeight.setCreateManName(model.getCreateManName());
                railwayWeight.setCreateTime(model.getCreateTime());
                railwayWeight.setStartTime(sdf.parse(sdf.format(model.getCreateTime())));
                railwayWeight.setTermValidity(30);
                //计算结束时间
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(railwayWeight.getStartTime());
                calendar.add(Calendar.DATE, railwayWeight.getTermValidity());
                railwayWeight.setEndTime(calendar.getTime());
                meterBaseRailwayWeightMapper.insertSelective(railwayWeight);
            }
        } catch (ParseException e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
        }
    }


    @Override
    public PageList<MeterWorkRailwayActFirst> doQueryWf(HashMap parmas, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkRailwayActFirst> rows = meterWorkRailwayActFirstMapper.selectByParameters(parmas);
        for (MeterWorkRailwayActFirst row : rows) {
            row.setMeterWeight(row.getMeterWeight().divide(new BigDecimal(1000)));
        }
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doAddRailWf(List<MeterWorkRailwayActFirst> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            int count = 0;

            for (MeterWorkRailwayActFirst model: models) {
                MeterWorkRailwayReceived modelTemp = meterWorkRailwayReceivedMapper.selectByPrimaryKey(model.getActualFirstNo());
                if(modelTemp.getPredictionNo() != null && StringUtils.isNotEmpty(modelTemp.getPredictionNo())) {
                    rm.setMessage("一次计量数据带有委托，请撤销完委托之后在保存皮重");
                    return rm;
                }
                if(modelTemp.getCreateTime() == null || StringUtils.isEmpty(modelTemp.getCreateTime().toString())) {
                    rm.setMessage("计量时间不能为空");
                    return rm;
                }
            }
            for (MeterWorkRailwayActFirst model : models) {
                meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(model.getActualFirstNo());
                MeterWorkRailwayActFirst mwrac = model;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String afl = sdf.format(new Date());
//                String netNo = meterWorkRailwayActFirstMapper.getNewID(afl);
//                mwrac.setActualFirstNo(afl + netNo);
//                String netNo = PublicMethod.IdCreate(3);
//                mwrac.setActualFirstNo(netNo);
                mwrac.setLcNo(model.getRailwayWorkNo());
                mwrac.setWeightType("1");
//                mwrac.setCreateTime(new Date());
                meterWorkRailwayActFirstMapper.insertSelective(mwrac);
                count++;
            }
            HashMap<String, Object> obj = new HashMap();
            obj.put("railwayWorkNo", models.get(0).getRailwayWorkNo());
            obj.put("valueFlag", "1");
            List<MeterWorkRailwayReceived> receiveds = meterWorkRailwayReceivedMapper.selectByParameters(obj);
            if(receiveds.size() < 1) {
                HashMap<String, Object> recMainMap = new HashMap<>();
                recMainMap.put("soundName", models.get(0).getRailwayWorkNo());
                List<MeterWorkRailwayRecMain> recMain = meterWorkRailwayRecMainMapper.selectByParameters(recMainMap);
                if(recMain.size() < 1) {
                    rm.setMessage("根据车次:" + models.get(0).getActualFirstNo() +"未查询到车次信息");
                    return rm;
                }
                recMain.get(0).setIsDispose("1");
                meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMain.get(0));
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功,总共" + count + "条及记录转换成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常,请确认！！！>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doAddRailNetWf
            (List<MeterWorkRailwayMotorialReceived> grossModels, List<MeterWorkRailwayActFirst> tareModels) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            int count = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            String userId = "", userName = "";
            try {
                UserPayload payload = UserPayload.getCurrUser();
                userId = payload.getUserCode();
                userName = payload.getUserName();
            } catch (Exception ex) {
            }

            for (MeterWorkRailwayMotorialReceived received : grossModels) {
                for (MeterWorkRailwayActFirst first : tareModels) {
                    //两个车号一样则匹配，否则则不匹配
                    if (first.getRailwayNo().equals(received.getRailwayNo())) {
                        //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                        preRailwayScaleMapper.updateMeterWorkRailwayReceived(received.getActualFirstNo());

                        //将一次表中的皮重设置为已结净
                        first.setValueFlag("2");
                        meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(first);

                        //新增一条毛重信息
                        MeterWorkRailwayActFirst second = new MeterWorkRailwayActFirst();
                        second.setActualFirstNo(received.getActualFirstNo());
                        second.setValueFlag("2");
                        second.setRailwayNo(received.getRailwayNo());
                        second.setRailwayCarriageNo(received.getRailwayCarriageNo());
                        second.setRailwaySpeed(received.getRailwaySpeed());
                        second.setMeterWeight(received.getMeterWeight());
                        second.setCreateTime(received.getCreateTime());
                        second.setCreateManNo(userId);
                        second.setCreateManName(userName);
                        second.setWeightType("0");
                        second.setRailwayTypeNo(received.getRailwayTypeNo());
                        second.setRailwayTypeName(received.getRailwayTypeName());
                        second.setBaseSpotNo(received.getBaseSpotNo());
                        second.setBaseSpotName(received.getBaseSpotName());
                        second.setShipmentGrossWeight(received.getShipmentGrossWeight());
                        second.setShipmentNetWeight(received.getShipmentNetWeight());
                        second.setRailwayModelName(received.getRailwayModel());
                        meterWorkRailwayActFirstMapper.insertSelective(second);


                        //新增一条结净数据
                        MeterWorkRailwayActual netRail = new MeterWorkRailwayActual();
//                        String netNo = meterWorkRailwayActualMapper.getNewID(afl);
//                        netRail.setActualNo(afl + netNo);
                        String no = PublicMethod.IdCreate(3);
                        netRail.setActualNo(no);
                        netRail.setRailwayNo(received.getRailwayNo());
                        netRail.setRailwayCarriageNo(received.getRailwayCarriageNo());
                        netRail.setGrossWeight(received.getMeterWeight());
                        netRail.setGrossMode("1");
                        netRail.setGrossManNo(userId);
                        netRail.setGrossManName(userName);
                        netRail.setGrossTime(received.getCreateTime());
                        netRail.setGrossClass(received.getShipmentGrossWeight().doubleValue() + ""); //左右偏载
                        netRail.setGrossGroup(received.getShipmentNetWeight().doubleValue() + "");//前后偏载
                        netRail.setBaseSpot1No(received.getBaseSpotNo());
                        netRail.setBaseSpot1Name(received.getBaseSpotName());
                        netRail.setActualFirst1No(second.getActualFirstNo());

                        netRail.setRailwayTypeNo(received.getRailwayTypeNo());
                        netRail.setRailwayTypeName(received.getRailwayTypeName());
                        netRail.setConveyanceType(received.getRailwayModel());
                        //netRail.setRailwayModelName(received.getRailwayModel());

                        netRail.setTareWeight(first.getMeterWeight());
                        netRail.setTareManNo(userId);
                        netRail.setTareManName(userName);
                        netRail.setTareTime(first.getCreateTime());
                        netRail.setTareMode("1");
                        netRail.setTareClass(first.getShipmentGrossWeight().doubleValue() + ""); //左右偏载
                        netRail.setTareGroup(first.getShipmentNetWeight().doubleValue() + "");//前后偏载
                        netRail.setBaseSpot2No(first.getBaseSpotNo());
                        netRail.setBaseSpot2Name(first.getBaseSpotName());
                        netRail.setActualFirst2No(first.getActualFirstNo());

                        netRail.setNetMode("1");
                        netRail.setNetWeight(second.getMeterWeight().subtract(first.getMeterWeight()));
                        netRail.setNetTime(new Date());
                        netRail.setNetManNo(userId);
                        netRail.setNetManName(userName);
                        netRail.setNetSpot3No(received.getBaseSpotNo());
                        netRail.setNetSpot3Name(received.getBaseSpotName());
                        netRail.setValueFlag("");
                        netRail.setUploadFlag("0");
                        netRail.setMeterTypeNo(received.getMeterTypeNo());
                        netRail.setMeterTypeName(received.getMeterTypeName());
                        if(netRail.getGrossTime().compareTo(netRail.getTareTime()) >= 0) {
                            netRail.setNetSpot3No(netRail.getBaseSpot1No());
                            netRail.setNetSpot3Name(netRail.getBaseSpot1Name());
                            netRail.setNetManNo(netRail.getGrossManNo());
                            netRail.setNetManName(netRail.getGrossManName());
                            netRail.setNetTime(netRail.getGrossTime());
                            netRail.setCreateTime(netRail.getGrossTime());
                            netRail.setCreateManName(netRail.getGrossManName());
                            netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst1No()));
                        }else{
                            netRail.setNetSpot3No(netRail.getBaseSpot2No());
                            netRail.setNetSpot3Name(netRail.getBaseSpot2Name());
                            netRail.setNetManNo(netRail.getTareManNo());
                            netRail.setNetManName(netRail.getTareManName());
                            netRail.setNetTime(netRail.getTareTime());
                            netRail.setCreateTime(netRail.getTareTime());
                            netRail.setCreateManName(netRail.getTareManName());
                            netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst2No()));
                        }
                        int i = meterWorkRailwayActualMapper.insertSelective(netRail);

                        if (i >= 1) {
                            count++;
                        }
                        break;
                    }
                }
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功,总共" + count + "条及记录匹配结净成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！！>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doNetAddMotoriaRailway
            (MeterWorkRailwayMotorialReceived received, MeterWorkRailwayActFirst first, PreRailwayScale tempScale) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            String userId = "", userName = "";
            try {
                UserPayload payload = UserPayload.getCurrUser();
                userId = payload.getUserCode();
                userName = payload.getUserName();
            } catch (Exception ex) {
                userId = "admin";
                userName = "admins";
            }
            String firstCarriageNo = first.getRailwayCarriageNo();
            //两个车号一样则匹配，否则则不匹配
            if (first.getRailwayNo().equals(received.getRailwayNo())) {
                if(tempScale.getPredictionNo() != null) {
                    Assert.isTrue(StringUtils.isNotEmpty(tempScale.getPredictionNo()), "委托编号为空");
                    PreRailwayScale scale = preRailwayScaleMapper.selectByPrimaryKey(tempScale.getPredictionNo());
                    Assert.isTrue(Objects.nonNull(scale), "委托信息不存在");
                    //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                    preRailwayScaleMapper.updateMeterWorkRailwayReceived(received.getActualFirstNo());

                    //将一次表中的皮重设置为已结净
                    first.setValueFlag("2");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(first);

                    //新增一条毛重信息
                    MeterWorkRailwayActFirst second = new MeterWorkRailwayActFirst();
                    second.setActualFirstNo(received.getActualFirstNo());
                    second.setValueFlag("2");
                    second.setRailwayNo(received.getRailwayNo());
                    second.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    second.setRailwaySpeed(received.getRailwaySpeed());
                    second.setMeterWeight(received.getMeterWeight());
                    second.setCreateTime(received.getCreateTime());
                    second.setCreateManNo(userId);
                    second.setCreateManName(userName);
                    second.setWeightType("0");
                    second.setRailwayTypeNo(received.getRailwayTypeNo());
                    second.setRailwayTypeName(received.getRailwayTypeName());
                    second.setBaseSpotNo(received.getBaseSpotNo());
                    second.setBaseSpotName(received.getBaseSpotName());
                    second.setShipmentGrossWeight(received.getShipmentGrossWeight());
                    second.setShipmentNetWeight(received.getShipmentNetWeight());
                    second.setRailwayModelName(received.getRailwayModel());
                    second.setLcNo(received.getRailwayWorkNo());
                    meterWorkRailwayActFirstMapper.insertSelective(second);

                    MeterWorkRailwayActual netRail = new MeterWorkRailwayActual();
                    doMatch(second, first, scale, netRail);

                    first.setRailwaySpeed(first.getRailwaySpeed());
                    first.setCheckFlag("0");
                    first.setMeterMode("1");
                    first.setValueFlag("2");
                    first.setWeightType("1");
                    first.setDataSource("1");
                    first.setNote("匹配结净");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(first);

                    second.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    second.setRailwaySpeed(received.getRailwaySpeed());
                    second.setShipmentGrossWeight(received.getShipmentGrossWeight());
                    second.setShipmentNetWeight(received.getShipmentNetWeight());
                    second.setRailwayModelName(received.getRailwayModel());
                    second.setDataSource("1");
                    second.setNote("匹配结净");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(second);

                    netRail.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    netRail.setGrossClass(received.getShipmentGrossWeight() + ""); //左右偏载
                    netRail.setGrossGroup(received.getShipmentNetWeight() + "");//前后偏载
                    netRail.setTareClass(second.getShipmentGrossWeight() + ""); //左右偏载
                    netRail.setTareGroup(second.getShipmentNetWeight() + "");//前后偏载
                    netRail.setLcNo(second.getLcNo());
                    netRail.setCreateTime(new Date());
                    netRail.setGrossMode("1");
                    netRail.setTareMode("1");
                    netRail.setCreateManName(userName);
                    netRail.setRailwayTypeNo("001019002");
                    netRail.setRailwayTypeName("动态轨道衡");
                    netRail.setNote("标识皮匹配结净");
                    netRail.setDataSource("1");
                    meterWorkRailwayActualMapper.updateByPrimaryKeySelective(netRail);

                }else {
                    //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                    preRailwayScaleMapper.updateMeterWorkRailwayReceived(received.getActualFirstNo());

                    //将一次表中的皮重设置为已结净
                    first.setRailwayCarriageNo(firstCarriageNo);
                    first.setCheckFlag("0");
                    first.setMeterMode("1");
                    first.setValueFlag("2");
                    first.setWeightType("1");
                    first.setDataSource("1");
                    first.setNote("匹配结净");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(first);

                    //新增一条毛重信息
                    MeterWorkRailwayActFirst second = new MeterWorkRailwayActFirst();
                    second.setActualFirstNo(received.getActualFirstNo());
                    second.setValueFlag("2");
                    second.setRailwayNo(received.getRailwayNo());
                    second.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    second.setRailwaySpeed(received.getRailwaySpeed());
                    second.setMeterWeight(received.getMeterWeight());
                    second.setCreateTime(received.getCreateTime());
                    second.setCreateManNo(userId);
                    second.setCreateManName(userName);
                    second.setWeightType("0");
                    second.setRailwayTypeNo(received.getRailwayTypeNo());
                    second.setRailwayTypeName(received.getRailwayTypeName());
                    second.setBaseSpotNo(received.getBaseSpotNo());
                    second.setBaseSpotName(received.getBaseSpotName());
                    second.setShipmentGrossWeight(received.getShipmentGrossWeight());
                    second.setShipmentNetWeight(received.getShipmentNetWeight());
                    second.setRailwayModelName(received.getRailwayModel());
                    second.setLcNo(received.getRailwayWorkNo());
                    second.setDataSource("1");
                    second.setNote("匹配结净");
                    meterWorkRailwayActFirstMapper.insertSelective(second);

                    //新增一条结净数据
                    MeterWorkRailwayActual netRail = new MeterWorkRailwayActual();
//                    String netNo = meterWorkRailwayActualMapper.getNewID(afl);
//                    netRail.setActualNo(afl + netNo);
                    String netNo = PublicMethod.IdCreate(3);
                    netRail.setActualNo(netNo);
                    netRail.setRailwayNo(received.getRailwayNo());
                    netRail.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    netRail.setGrossWeight(received.getMeterWeight());
                    netRail.setGrossMode("1");
                    netRail.setGrossManNo(userId);
                    netRail.setGrossManName(userName);
                    netRail.setGrossTime(received.getCreateTime());
                    netRail.setGrossClass(received.getShipmentGrossWeight() + ""); //左右偏载
                    netRail.setGrossGroup(received.getShipmentNetWeight() + "");//前后偏载
                    netRail.setBaseSpot1No(received.getBaseSpotNo());
                    netRail.setBaseSpot1Name(received.getBaseSpotName());
                    netRail.setActualFirst1No(second.getActualFirstNo());
                    netRail.setRailwayTypeNo(received.getRailwayTypeNo());
                    netRail.setRailwayTypeName(received.getRailwayTypeName());
                    netRail.setConveyanceType(received.getRailwayModel());
                    //netRail.setRailwayModelName(received.getRailwayModel());

                    netRail.setLcNo(received.getRailwayWorkNo());
                    netRail.setTareWeight(first.getMeterWeight());
                    netRail.setTareManNo(userId);
                    netRail.setTareManName(userName);
                    netRail.setTareTime(first.getCreateTime());
                    netRail.setTareMode("1");
                    netRail.setTareClass(first.getShipmentGrossWeight() + ""); //左右偏载
                    netRail.setTareGroup(first.getShipmentNetWeight() + "");//前后偏载
                    netRail.setBaseSpot2No(first.getBaseSpotNo());
                    netRail.setBaseSpot2Name(first.getBaseSpotName());
                    netRail.setActualFirst2No(first.getActualFirstNo());
                    netRail.setNetMode("1");
                    netRail.setNetWeight(second.getMeterWeight().subtract(first.getMeterWeight()));
                    netRail.setNetTime(new Date());
                    netRail.setNetManNo(userId);
                    netRail.setNetManName(userName);
                    netRail.setNetSpot3No(received.getBaseSpotNo());
                    netRail.setNetSpot3Name(received.getBaseSpotName());
                    netRail.setValueFlag("1");
                    netRail.setUploadFlag("0");
                    netRail.setMeterTypeNo(received.getMeterTypeNo());
                    netRail.setMeterTypeName(received.getMeterTypeName());
                    netRail.setRailwayTypeNo("001019002");
                    netRail.setRailwayTypeName("动态轨道衡");
                    netRail.setNote("匹配结净");
                    netRail.setDataSource("1");
                    netRail.setCreateTime(new Date());
                    netRail.setCreateManName(userName);
                    if(netRail.getGrossTime().compareTo(netRail.getTareTime()) >= 0) {
                        netRail.setNetSpot3No(netRail.getBaseSpot1No());
                        netRail.setNetSpot3Name(netRail.getBaseSpot1Name());
                        netRail.setNetManNo(netRail.getGrossManNo());
                        netRail.setNetManName(netRail.getGrossManName());
                        netRail.setNetTime(netRail.getGrossTime());
                        netRail.setCreateTime(netRail.getGrossTime());
                        netRail.setCreateManName(netRail.getGrossManName());
                        netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst1No()));
                    }else{
                        netRail.setNetSpot3No(netRail.getBaseSpot2No());
                        netRail.setNetSpot3Name(netRail.getBaseSpot2Name());
                        netRail.setNetManNo(netRail.getTareManNo());
                        netRail.setNetManName(netRail.getTareManName());
                        netRail.setNetTime(netRail.getTareTime());
                        netRail.setCreateTime(netRail.getTareTime());
                        netRail.setCreateManName(netRail.getTareManName());
                        netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst2No()));
                    }
                    meterWorkRailwayActualMapper.insertSelective(netRail);
                }
            }

            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功,匹配结净成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！！>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doAddRailAINetWf
            (List<MeterWorkRailwayMotorialReceived> grossModels, List<MeterBaseRailwayAiweight> tareModels) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            int count = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            String userId = "", userName = "";
            try {
                UserPayload payload = UserPayload.getCurrUser();
                userId = payload.getUserCode();
                userName = payload.getUserName();
            } catch (Exception ex) {
            }

            for (MeterWorkRailwayMotorialReceived received : grossModels) {
                for (MeterBaseRailwayAiweight first : tareModels) {
                    //两个车号一样则匹配，否则则不匹配
                    if (first.getRailwayNo().equals(received.getRailwayNo())) {
                        MeterWorkRailwayActFirst AIFirst = new MeterWorkRailwayActFirst();
                        //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                        preRailwayScaleMapper.updateMeterWorkRailwayReceived(received.getActualFirstNo());

                        //将一次表中的皮重设置为已结净
//                        String firstNo = afl + meterWorkRailwayActFirstMapper.getNewID(afl);
//                        AIFirst.setActualFirstNo(firstNo);
                        String firstNo = PublicMethod.IdCreate(3);
                        AIFirst.setActualFirstNo(firstNo);
                        AIFirst.setValueFlag("2");
                        AIFirst.setRailwayNo(first.getRailwayNo());
                        AIFirst.setMeterWeight(first.getMeterWeight());
                        AIFirst.setRailwayTypeNo(first.getRailwayTypeNo());
                        AIFirst.setRailwayTypeName(first.getRailwayTypeName());
                        AIFirst.setCreateManNo(first.getCreateManNo());
                        AIFirst.setCreateManName(first.getCreateManName());
                        AIFirst.setCreateTime(first.getCreateTime());
                        AIFirst.setRailwaySpeed(first.getRailwaySpeed());
                        AIFirst.setConveyanceType(first.getRailwayType());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(AIFirst);

                        //作废AI标志皮
                        first.setValueFlag("1");
                        meterBaseRailwayAiweightMapper.updateByPrimaryKeySelective(first);

                        //新增一条毛重信息
                        MeterWorkRailwayActFirst second = new MeterWorkRailwayActFirst();
                        second.setActualFirstNo(received.getActualFirstNo());
                        second.setValueFlag("2");
                        second.setRailwayNo(received.getRailwayNo());
                        second.setRailwayCarriageNo(received.getRailwayCarriageNo());
                        second.setRailwaySpeed(received.getRailwaySpeed());
                        second.setMeterWeight(received.getMeterWeight());
                        second.setCreateTime(received.getCreateTime());
                        second.setCreateManNo(userId);
                        second.setCreateManName(userName);
                        second.setWeightType("0");
                        second.setRailwayTypeNo(received.getRailwayTypeNo());
                        second.setRailwayTypeName(received.getRailwayTypeName());
                        second.setBaseSpotNo(received.getBaseSpotNo());
                        second.setBaseSpotName(received.getBaseSpotName());
                        second.setShipmentGrossWeight(received.getShipmentGrossWeight());
                        second.setShipmentNetWeight(received.getShipmentNetWeight());
                        second.setRailwayModelName(received.getRailwayModel());
                        meterWorkRailwayActFirstMapper.insertSelective(second);


                        //新增一条结净数据
                        MeterWorkRailwayActual netRail = new MeterWorkRailwayActual();
//                        String netNo = meterWorkRailwayActualMapper.getNewID(afl);
//                        netRail.setActualNo(afl + netNo);
                        String netNo = PublicMethod.IdCreate(3);
                        netRail.setActualNo(netNo);
                        netRail.setRailwayNo(received.getRailwayNo());
                        netRail.setRailwayCarriageNo(received.getRailwayCarriageNo());
                        netRail.setGrossWeight(received.getMeterWeight());
                        netRail.setGrossMode("1");
                        netRail.setGrossManNo(userId);
                        netRail.setGrossManName(userName);
                        netRail.setGrossTime(received.getCreateTime());
                        netRail.setGrossClass(received.getShipmentGrossWeight().doubleValue() + ""); //左右偏载
                        netRail.setGrossGroup(received.getShipmentNetWeight().doubleValue() + "");//前后偏载
                        netRail.setBaseSpot1No(received.getBaseSpotNo());
                        netRail.setBaseSpot1Name(received.getBaseSpotName());
                        netRail.setActualFirst1No(second.getActualFirstNo());
                        netRail.setRailwayTypeNo(received.getRailwayTypeNo());
                        netRail.setRailwayTypeName(received.getRailwayTypeName());
                        netRail.setConveyanceType(received.getRailwayModel());
                        //netRail.setRailwayModelName(received.getRailwayModel());
                        netRail.setTareWeight(AIFirst.getMeterWeight());
                        netRail.setTareManNo(userId);
                        netRail.setTareManName(userName);
                        netRail.setTareTime(AIFirst.getCreateTime());
                        netRail.setTareMode("1");
                        netRail.setTareClass(second.getShipmentGrossWeight().doubleValue() + ""); //左右偏载
                        netRail.setTareGroup(second.getShipmentNetWeight().doubleValue() + "");//前后偏载
                        netRail.setBaseSpot2No(second.getBaseSpotNo());
                        netRail.setBaseSpot2Name(second.getBaseSpotName());
                        netRail.setActualFirst2No(second.getActualFirstNo());
                        netRail.setNetMode("1");
                        netRail.setNetWeight(second.getMeterWeight().subtract(AIFirst.getMeterWeight()));
                        netRail.setNetTime(new Date());
                        netRail.setNetManNo(userId);
                        netRail.setNetManName(userName);
                        netRail.setNetSpot3No(received.getBaseSpotNo());
                        netRail.setNetSpot3Name(received.getBaseSpotName());
                        netRail.setValueFlag("0");
                        netRail.setUploadFlag("0");
                        netRail.setMeterTypeNo(received.getMeterTypeNo());
                        netRail.setMeterTypeName(received.getMeterTypeName());
                        if(netRail.getGrossTime().compareTo(netRail.getTareTime()) >= 0) {
                            netRail.setNetSpot3No(netRail.getBaseSpot1No());
                            netRail.setNetSpot3Name(netRail.getBaseSpot1Name());
                            netRail.setNetManNo(netRail.getGrossManNo());
                            netRail.setNetManName(netRail.getGrossManName());
                            netRail.setNetTime(netRail.getGrossTime());
                            netRail.setCreateTime(netRail.getGrossTime());
                            netRail.setCreateManName(netRail.getGrossManName());
                            netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst1No()));
                        }else{
                            netRail.setNetSpot3No(netRail.getBaseSpot2No());
                            netRail.setNetSpot3Name(netRail.getBaseSpot2Name());
                            netRail.setNetManNo(netRail.getTareManNo());
                            netRail.setNetManName(netRail.getTareManName());
                            netRail.setNetTime(netRail.getTareTime());
                            netRail.setCreateTime(netRail.getTareTime());
                            netRail.setCreateManName(netRail.getTareManName());
                            netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst2No()));
                        }
                        int i = meterWorkRailwayActualMapper.insertSelective(netRail);

                        if (i >= 1) {
                            meterWorkRailwayActFirstMapper.deleteByPrimaryKey(firstNo);
                            count++;
                        }
                        break;
                    }
                }
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功,总共" + count + "条记录匹配AI标志皮结净成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！！>>>" + e.getMessage());
        }
        return rm;
    }


    ///动轨标识皮结净
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doAddNetInnerWf(MeterWorkRailwayMotorialReceived received, MeterBaseRailwayAiweight first, PreRailwayScale tempScale) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            String userId = "", userName = "";
            try {
                UserPayload payload = UserPayload.getCurrUser();
                userId = payload.getUserCode();
                userName = payload.getUserName();
            } catch (Exception ex) {
                userId = "admin";
                userName = "admins";
            }

            Assert.isTrue(received.getRailwayNo().equals(first.getRailwayNo()), "车号不一致,请重新选择！");
            //两个车号一样则匹配，否则则不匹配
            if (received.getRailwayNo().equals(first.getRailwayNo())) {
                if(Objects.nonNull(tempScale.getPredictionNo())) {
                    MeterWorkRailwayActFirst AIFirst = new MeterWorkRailwayActFirst();
                    BeanUtils.copyProperties(received, AIFirst);

                    Assert.isTrue(StringUtils.isNotEmpty(tempScale.getPredictionNo()), "委托编号为空");
                    PreRailwayScale scale = preRailwayScaleMapper.selectByPrimaryKey(tempScale.getPredictionNo());
                    Assert.isTrue(Objects.nonNull(scale), "委托信息不存在");

                    //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                    preRailwayScaleMapper.updateMeterWorkRailwayReceived(received.getActualFirstNo());

//                    String firstNo = afl + meterWorkRailwayActFirstMapper.getNewID(afl);
//                    AIFirst.setActualFirstNo(firstNo);
                    String firstNo = PublicMethod.IdCreate(3);
                    AIFirst.setActualFirstNo(firstNo);
                    AIFirst.setValueFlag("2");
                    AIFirst.setRailwayNo(first.getRailwayNo());
                    AIFirst.setMeterWeight(first.getMeterWeight());
                    AIFirst.setRailwayTypeNo(first.getRailwayTypeNo());
                    AIFirst.setRailwayTypeName(first.getRailwayTypeName());
                    AIFirst.setCreateManNo(userId);
                    AIFirst.setCreateManName(userName);
                    AIFirst.setCreateTime(new Date());
                    AIFirst.setRailwaySpeed(first.getRailwaySpeed());
                    AIFirst.setConveyanceType(first.getRailwayType());
                    meterWorkRailwayActFirstMapper.insertSelective(AIFirst);

                    //作废AI标志皮
                    first.setValueFlag("1");
                    meterBaseRailwayAiweightMapper.updateByPrimaryKeySelective(first);

                    MeterWorkRailwayActFirst second = new MeterWorkRailwayActFirst();
                    second.setActualFirstNo(received.getActualFirstNo());
                    second.setValueFlag("2");
                    second.setRailwayNo(received.getRailwayNo());
                    second.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    second.setRailwaySpeed(received.getRailwaySpeed());
                    second.setMeterWeight(received.getMeterWeight());
                    second.setCreateTime(received.getCreateTime());
                    second.setLcNo(received.getRailwayWorkNo());
                    second.setCreateManNo(userId);
                    second.setCreateManName(userName);
                    second.setWeightType("0");
                    second.setRailwayTypeNo(received.getRailwayTypeNo());
                    second.setRailwayTypeName(received.getRailwayTypeName());
                    second.setBaseSpotNo(received.getBaseSpotNo());
                    second.setBaseSpotName(received.getBaseSpotName());
                    second.setShipmentGrossWeight(received.getShipmentGrossWeight());
                    second.setShipmentNetWeight(received.getShipmentNetWeight());
                    second.setRailwayModelName(received.getRailwayModel());
                    meterWorkRailwayActFirstMapper.insertSelective(second);

                    MeterWorkRailwayActual netRail = new MeterWorkRailwayActual();
                    doMatch(second, AIFirst, scale, netRail);

                    AIFirst.setRailwaySpeed(first.getRailwaySpeed());
                    AIFirst.setConveyanceType(first.getRailwayType());
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(AIFirst);

                    second.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    second.setRailwaySpeed(received.getRailwaySpeed());
                    second.setShipmentGrossWeight(received.getShipmentGrossWeight());
                    second.setShipmentNetWeight(received.getShipmentNetWeight());
                    second.setRailwayModelName(received.getRailwayModel());
                    second.setDataSource("1");
                    second.setNote("标识皮匹配结净");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(second);

                    netRail.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    netRail.setGrossClass(received.getShipmentGrossWeight() + ""); //左右偏载
                    netRail.setGrossGroup(received.getShipmentNetWeight() + "");//前后偏载
                    netRail.setTareClass(second.getShipmentGrossWeight() + ""); //左右偏载
                    netRail.setTareGroup(second.getShipmentNetWeight() + "");//前后偏载
                    netRail.setLcNo(second.getLcNo());
                    netRail.setCreateTime(new Date());
                    netRail.setGrossMode("1");
                    netRail.setTareMode("1");
                    netRail.setCreateManName(userName);
                    netRail.setRailwayTypeNo("001019002");
                    netRail.setRailwayTypeName("动态轨道衡");
                    netRail.setNote("标识皮匹配结净");
                    netRail.setDataSource("1");
                    meterWorkRailwayActualMapper.updateByPrimaryKeySelective(netRail);

                    meterWorkRailwayActFirstMapper.deleteByPrimaryKey(firstNo);

                }else {
                    MeterWorkRailwayActFirst AIFirst = new MeterWorkRailwayActFirst();
                    //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                    preRailwayScaleMapper.updateMeterWorkRailwayReceived(received.getActualFirstNo());

                    //将一次表中的皮重设置为已结净
//                    String firstNo = afl + meterWorkRailwayActFirstMapper.getNewID(afl);
//                    AIFirst.setActualFirstNo(firstNo);
                    String firstNo = PublicMethod.IdCreate(3);
                    AIFirst.setActualFirstNo(firstNo);
                    AIFirst.setValueFlag("2");
                    AIFirst.setRailwayNo(first.getRailwayNo());
                    AIFirst.setMeterWeight(first.getMeterWeight());
                    AIFirst.setRailwayTypeNo(first.getRailwayTypeNo());
                    AIFirst.setRailwayTypeName(first.getRailwayTypeName());
                    AIFirst.setCreateManNo(first.getCreateManNo());
                    AIFirst.setCreateManName(first.getCreateManName());
                    AIFirst.setCreateTime(first.getCreateTime());
                    AIFirst.setRailwaySpeed(first.getRailwaySpeed());
                    AIFirst.setConveyanceType(first.getRailwayType());
                    meterWorkRailwayActFirstMapper.insertSelective(AIFirst);
                    //作废AI标志皮
                    first.setValueFlag("1");
                    meterBaseRailwayAiweightMapper.updateByPrimaryKeySelective(first);

                    //新增一条毛重信息
                    MeterWorkRailwayActFirst second = new MeterWorkRailwayActFirst();
                    second.setActualFirstNo(received.getActualFirstNo());
                    second.setValueFlag("2");
                    second.setRailwayNo(received.getRailwayNo());
                    second.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    second.setRailwaySpeed(received.getRailwaySpeed());
                    second.setMeterWeight(received.getMeterWeight());
                    second.setCreateTime(received.getCreateTime());
                    second.setCreateManNo(userId);
                    second.setCreateManName(userName);
                    second.setWeightType("0");
                    second.setLcNo(received.getRailwayWorkNo());
                    second.setRailwayTypeNo(received.getRailwayTypeNo());
                    second.setRailwayTypeName(received.getRailwayTypeName());
                    second.setBaseSpotNo(received.getBaseSpotNo());
                    second.setBaseSpotName(received.getBaseSpotName());
                    second.setShipmentGrossWeight(received.getShipmentGrossWeight());
                    second.setShipmentNetWeight(received.getShipmentNetWeight());
                    second.setRailwayModelName(received.getRailwayModel());
                    second.setDataSource("1");
                    second.setNote("标识皮匹配结净");
                    meterWorkRailwayActFirstMapper.insertSelective(second);

                    //新增一条结净数据
                    MeterWorkRailwayActual netRail = new MeterWorkRailwayActual();
//                    String netNo = meterWorkRailwayActualMapper.getNewID(afl);
//                    netRail.setActualNo(afl + netNo);
                    String netNo = PublicMethod.IdCreate(3);
                    netRail.setActualNo(netNo);
                    netRail.setRailwayNo(received.getRailwayNo());
                    netRail.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    netRail.setGrossWeight(received.getMeterWeight());
                    netRail.setGrossMode("1");
                    netRail.setGrossManNo(userId);
                    netRail.setGrossManName(userName);
                    netRail.setGrossTime(received.getCreateTime());
                    netRail.setGrossClass(received.getShipmentGrossWeight() + ""); //左右偏载
                    netRail.setGrossGroup(received.getShipmentNetWeight() + "");//前后偏载
                    netRail.setBaseSpot1No(received.getBaseSpotNo());
                    netRail.setBaseSpot1Name(received.getBaseSpotName());
                    netRail.setActualFirst1No(second.getActualFirstNo());
                    netRail.setRailwayTypeNo("001019002");
                    netRail.setRailwayTypeName("动态轨道衡");
                    netRail.setConveyanceType(received.getRailwayModel());
                    //netRail.setRailwayModelName(received.getRailwayModel());
                    netRail.setLcNo(second.getLcNo());
                    netRail.setTareWeight(AIFirst.getMeterWeight());
                    netRail.setTareManNo(userId);
                    netRail.setTareManName(userName);
                    netRail.setTareTime(AIFirst.getCreateTime());
                    netRail.setTareMode("1");
                    netRail.setTareClass(second.getShipmentGrossWeight() + ""); //左右偏载
                    netRail.setTareGroup(second.getShipmentNetWeight() + "");//前后偏载
                    netRail.setBaseSpot2No(second.getBaseSpotNo());
                    netRail.setBaseSpot2Name(second.getBaseSpotName());
                    netRail.setActualFirst2No(second.getActualFirstNo());
                    netRail.setNetMode("1");
                    netRail.setNetWeight(second.getMeterWeight().subtract(AIFirst.getMeterWeight()));
                    netRail.setNetTime(new Date());
                    netRail.setNetManNo(userId);
                    netRail.setNetManName(userName);
                    netRail.setNetSpot3No(received.getBaseSpotNo());
                    netRail.setNetSpot3Name(received.getBaseSpotName());
                    netRail.setValueFlag("1");
                    netRail.setUploadFlag("0");
                    netRail.setMeterTypeNo(received.getMeterTypeNo());
                    netRail.setMeterTypeName(received.getMeterTypeName());
                    netRail.setCreateTime(new Date());
                    netRail.setCreateManName(userName);
                    netRail.setNote("标识皮匹配结净");
                    netRail.setDataSource("1");
                    if(netRail.getGrossTime().compareTo(netRail.getTareTime()) >= 0) {
                        netRail.setNetSpot3No(netRail.getBaseSpot1No());
                        netRail.setNetSpot3Name(netRail.getBaseSpot1Name());
                        netRail.setNetManNo(netRail.getGrossManNo());
                        netRail.setNetManName(netRail.getGrossManName());
                        netRail.setNetTime(netRail.getGrossTime());
                        netRail.setCreateTime(netRail.getGrossTime());
                        netRail.setCreateManName(netRail.getGrossManName());
                        netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst1No()));
                    }else{
                        netRail.setNetSpot3No(netRail.getBaseSpot2No());
                        netRail.setNetSpot3Name(netRail.getBaseSpot2Name());
                        netRail.setNetManNo(netRail.getTareManNo());
                        netRail.setNetManName(netRail.getTareManName());
                        netRail.setNetTime(netRail.getTareTime());
                        netRail.setCreateTime(netRail.getTareTime());
                        netRail.setCreateManName(netRail.getTareManName());
                        netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst2No()));
                    }
                    int i = meterWorkRailwayActualMapper.insertSelective(netRail);
                    if(i >= 1) {
                        meterWorkRailwayActFirstMapper.deleteByPrimaryKey(firstNo);
                    }
                }
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功,匹配AI标识皮结净成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！！>>>" + e.getMessage());
        }
        return rm;
    }

    ///动态衡标准皮结净
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doAddNetWeight(MeterWorkRailwayMotorialReceived received, MeterBaseRailwayCarriageWeight first01, PreRailwayScale tempScale) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            String userId = "", userName = "";
            try {
                UserPayload payload = UserPayload.getCurrUser();
                userId = payload.getUserCode();
                userName = payload.getUserName();
            } catch (Exception ex) {
                userId = "admin";
                userName = "admins";
            }
            MeterWorkRailwayActFirst first = new MeterWorkRailwayActFirst();
            BeanUtils.copyProperties(received, first);

            //两个车号一样则匹配，否则则不匹配
            if (received.getRailwayNo().equals(first.getRailwayNo())) {
                MeterWorkRailwayActFirst AIFirst = new MeterWorkRailwayActFirst();

                if(tempScale.getPredictionNo() != null) {
                    Assert.isTrue(StringUtils.isNotEmpty(tempScale.getPredictionNo()), "委托编号为空");
                    PreRailwayScale scale = preRailwayScaleMapper.selectByPrimaryKey(tempScale.getPredictionNo());
                    Assert.isTrue(Objects.nonNull(scale), "委托信息不存在");

                    //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                    preRailwayScaleMapper.updateMeterWorkRailwayReceived(received.getActualFirstNo());

                    //将一次表中的皮重设置为已结净
//                    AIFirst.setActualFirstNo(afl + meterWorkRailwayActFirstMapper.getNewID(afl));
                    String no = PublicMethod.IdCreate(3);
                    AIFirst.setActualFirstNo(no);
                    AIFirst.setValueFlag("2");
                    AIFirst.setCreateTime(new Date());
                    AIFirst.setCreateManNo(userId);
                    AIFirst.setCreateManName(userName);
                    AIFirst.setBaseSpotNo(received.getBaseSpotNo());
                    AIFirst.setBaseSpotName(received.getBaseSpotName());
                    AIFirst.setMeterWeight(new BigDecimal(first01.getCarriageWeight()));
                    AIFirst.setConveyanceType(first01.getCarriageCode());
                    meterWorkRailwayActFirstMapper.insertSelective(AIFirst);


                    MeterWorkRailwayActFirst second = new MeterWorkRailwayActFirst();
                    second.setActualFirstNo(received.getActualFirstNo());
                    second.setValueFlag("2");
                    second.setRailwayNo(received.getRailwayNo());
                    second.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    second.setRailwaySpeed(received.getRailwaySpeed());
                    second.setMeterWeight(received.getMeterWeight());
                    second.setCreateTime(received.getCreateTime());
                    second.setCreateManNo(userId);
                    second.setCreateManName(userName);
                    second.setLcNo(received.getRailwayWorkNo());
                    second.setWeightType("0");
                    second.setRailwayTypeNo(received.getRailwayTypeNo());
                    second.setRailwayTypeName(received.getRailwayTypeName());
                    second.setBaseSpotNo(received.getBaseSpotNo());
                    second.setBaseSpotName(received.getBaseSpotName());
                    second.setShipmentGrossWeight(received.getShipmentGrossWeight());
                    second.setShipmentNetWeight(received.getShipmentNetWeight());
                    second.setRailwayModelName(received.getRailwayModel());
                    second.setDataSource("1");
                    second.setNote("标准皮匹配结净");
                    meterWorkRailwayActFirstMapper.insertSelective(second);

                    MeterWorkRailwayActual netRail = new MeterWorkRailwayActual();
                    doMatch(second, AIFirst, scale, netRail);

                    second.setRailwaySpeed(received.getRailwaySpeed());
                    second.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    second.setShipmentGrossWeight(received.getShipmentGrossWeight());
                    second.setShipmentNetWeight(received.getShipmentNetWeight());
                    second.setRailwayModelName(received.getRailwayModel());
                    second.setDataSource("1");
                    second.setNote("标准皮匹配结净");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(second);

                    netRail.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    netRail.setGrossMode("1");
                    netRail.setGrossClass(received.getShipmentGrossWeight() + ""); //左右偏载
                    netRail.setGrossGroup(received.getShipmentNetWeight() + "");//前后偏载
                    netRail.setConveyanceType(received.getRailwayModel());
                    netRail.setTareMode("1");
                    netRail.setTareClass(second.getShipmentGrossWeight() + ""); //左右偏载
                    netRail.setTareGroup(second.getShipmentNetWeight() + "");//前后偏载
                    netRail.setLcNo(second.getLcNo());
                    netRail.setNetMode("1");
                    netRail.setCreateTime(new Date());
                    netRail.setCreateManName(userName);
                    netRail.setNote("标准皮匹配结净");
                    netRail.setDataSource("1");
                    meterWorkRailwayActualMapper.updateByPrimaryKeySelective(netRail);

                    meterWorkRailwayActFirstMapper.deleteByPrimaryKey(AIFirst.getActualFirstNo());

                }else{
                    //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                    preRailwayScaleMapper.updateMeterWorkRailwayReceived(received.getActualFirstNo());

                    //将一次表中的皮重设置为已结净
//                    String firstNo = afl + meterWorkRailwayActFirstMapper.getNewID(afl);
//                    AIFirst.setActualFirstNo(firstNo);
                    String firstNo = PublicMethod.IdCreate(3);
                    AIFirst.setActualFirstNo(firstNo);
                    AIFirst.setValueFlag("2");
                    AIFirst.setCreateTime(new Date());
                    AIFirst.setCreateManNo(userId);
                    AIFirst.setCreateManName(userName);
                    AIFirst.setBaseSpotNo(received.getBaseSpotNo());
                    AIFirst.setBaseSpotName(received.getBaseSpotName());
                    AIFirst.setMeterWeight(new BigDecimal(first01.getCarriageWeight()));
                    AIFirst.setConveyanceType(first01.getCarriageCode());
                    meterWorkRailwayActFirstMapper.insertSelective(AIFirst);

                    //新增一条毛重信息
                    MeterWorkRailwayActFirst second = new MeterWorkRailwayActFirst();
                    second.setActualFirstNo(received.getActualFirstNo());
                    second.setValueFlag("2");
                    second.setRailwayNo(received.getRailwayNo());
                    second.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    second.setRailwaySpeed(received.getRailwaySpeed());
                    second.setMeterWeight(received.getMeterWeight());
                    second.setCreateTime(received.getCreateTime());
                    second.setCreateManNo(userId);
                    second.setCreateManName(userName);
                    second.setWeightType("0");
                    second.setLcNo(received.getRailwayWorkNo());
                    second.setRailwayTypeNo(received.getRailwayTypeNo());
                    second.setRailwayTypeName(received.getRailwayTypeName());
                    second.setBaseSpotNo(received.getBaseSpotNo());
                    second.setBaseSpotName(received.getBaseSpotName());
                    second.setShipmentGrossWeight(received.getShipmentGrossWeight());
                    second.setShipmentNetWeight(received.getShipmentNetWeight());
                    second.setRailwayModelName(received.getRailwayModel());
                    second.setDataSource("1");
                    second.setNote("标准皮匹配结净");
                    meterWorkRailwayActFirstMapper.insertSelective(second);

                    //新增一条结净数据
                    MeterWorkRailwayActual netRail = new MeterWorkRailwayActual();
//                    String netNo = meterWorkRailwayActualMapper.getNewID(afl);
//                    netRail.setActualNo(afl + netNo);
                    String netNo = PublicMethod.IdCreate(3);
                    netRail.setActualNo(netNo);
                    netRail.setRailwayNo(received.getRailwayNo());
                    netRail.setRailwayCarriageNo(received.getRailwayCarriageNo());
                    netRail.setGrossWeight(received.getMeterWeight());
                    netRail.setGrossMode("1");
                    netRail.setGrossManNo(userId);
                    netRail.setGrossManName(userName);
                    netRail.setGrossTime(received.getCreateTime());
                    netRail.setGrossClass(received.getShipmentGrossWeight() + ""); //左右偏载
                    netRail.setGrossGroup(received.getShipmentNetWeight() + "");//前后偏载
                    netRail.setBaseSpot1No(received.getBaseSpotNo());
                    netRail.setBaseSpot1Name(received.getBaseSpotName());
                    netRail.setActualFirst1No(second.getActualFirstNo());
                    netRail.setRailwayTypeNo("001019002");
                    netRail.setRailwayTypeName("动态轨道衡");
                    netRail.setConveyanceType(received.getRailwayModel());
                    //netRail.setRailwayModelName(received.getRailwayModel());
                    netRail.setLcNo(second.getLcNo());
                    netRail.setTareWeight(AIFirst.getMeterWeight());
                    netRail.setTareManNo(userId);
                    netRail.setTareManName(userName);
                    netRail.setTareTime(AIFirst.getCreateTime());
                    netRail.setTareMode("1");
                    netRail.setTareClass(second.getShipmentGrossWeight() + ""); //左右偏载
                    netRail.setTareGroup(second.getShipmentNetWeight() + "");//前后偏载
                    netRail.setBaseSpot2No(second.getBaseSpotNo());
                    netRail.setBaseSpot2Name(second.getBaseSpotName());
                    netRail.setActualFirst2No(AIFirst.getActualFirstNo());
                    netRail.setNetMode("1");
                    netRail.setNetWeight(second.getMeterWeight().subtract(AIFirst.getMeterWeight()));
                    netRail.setNetTime(new Date());
                    netRail.setNetManNo(userId);
                    netRail.setNetManName(userName);
                    netRail.setNetSpot3No(received.getBaseSpotNo());
                    netRail.setNetSpot3Name(received.getBaseSpotName());
                    netRail.setValueFlag("1");
                    netRail.setUploadFlag("0");
                    netRail.setMeterTypeNo(received.getMeterTypeNo());
                    netRail.setMeterTypeName(received.getMeterTypeName());
                    netRail.setCreateTime(new Date());
                    netRail.setCreateManName(userName);
                    netRail.setNote("标准皮匹配结净");
                    netRail.setDataSource("1");
                    if(netRail.getGrossTime().compareTo(netRail.getTareTime()) >= 0) {
                        netRail.setNetSpot3No(netRail.getBaseSpot1No());
                        netRail.setNetSpot3Name(netRail.getBaseSpot1Name());
                        netRail.setNetManNo(netRail.getGrossManNo());
                        netRail.setNetManName(netRail.getGrossManName());
                        netRail.setNetTime(netRail.getGrossTime());
                        netRail.setCreateTime(netRail.getGrossTime());
                        netRail.setCreateManName(netRail.getGrossManName());
                        netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst1No()));
                    }else{
                        netRail.setNetSpot3No(netRail.getBaseSpot2No());
                        netRail.setNetSpot3Name(netRail.getBaseSpot2Name());
                        netRail.setNetManNo(netRail.getTareManNo());
                        netRail.setNetManName(netRail.getTareManName());
                        netRail.setNetTime(netRail.getTareTime());
                        netRail.setCreateTime(netRail.getTareTime());
                        netRail.setCreateManName(netRail.getTareManName());
                        netRail.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(netRail.getActualFirst2No()));
                    }
                    meterWorkRailwayActualMapper.insertSelective(netRail);

                    meterWorkRailwayActFirstMapper.deleteByPrimaryKey(AIFirst.getActualFirstNo());
                }
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功,匹配标准皮结净成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！！>>>" + e.getMessage());
        }
        return rm;
    }

    //一次记录进行结净，根据scale进行有委托或者无委托结净
    @Transactional(rollbackFor = Exception.class)
    public void doMatch(MeterWorkRailwayActFirst grossModel, MeterWorkRailwayActFirst tareModel, PreRailwayScale scale, MeterWorkRailwayActual actualRow) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        if(scale != null) {
            MeterWorkRailwayActFirst actFirst01 = meterWorkRailwayActFirstMapper.selectAndMatch(grossModel.getActualFirstNo(), scale.getPredictionNo());
            BeanUtils.copyProperties(actFirst01, grossModel);
            MeterWorkRailwayActFirst actFirst02 = meterWorkRailwayActFirstMapper.selectAndMatch(tareModel.getActualFirstNo(), scale.getPredictionNo());
            BeanUtils.copyProperties(actFirst02, tareModel);

            //更新一次数据
            //更新第一次毛重数据库
            grossModel.setValueFlag(DbConstants.NET);
    //        grossModel.setUpdateManNo("system");
    //        grossModel.setUpdateManName("智能计量双委托结净更新");
    //        grossModel.setUpdateTime(new Date());
            grossModel.setWeightType("0");
            grossModel.setIsPreSacale("1");
            grossModel.setPredictionNo(scale.getPredictionNo());
            grossModel.setPredictionType("1");
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);
            //更新第一次皮重数据库
            tareModel.setValueFlag(DbConstants.NET);
    //        tareModel.setUpdateManNo("system");
    //        tareModel.setUpdateManName("自助计量结净更新");
    //        tareModel.setUpdateTime(new Date());
            tareModel.setWeightType("1");
            tareModel.setIsPreSacale("1");
            tareModel.setPredictionNo(scale.getPredictionNo());
            tareModel.setPredictionType("1");
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);

            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), scale.getPredictionNo());
            BeanUtils.copyProperties(mwca1, actualRow);

            //添加结净数据
//            actualRow.setActualNo(sdf.format(new Date()) + meterWorkRailwayActualMapper.getNewID(sdf.format(new Date())));
            String no = PublicMethod.IdCreate(3);
            actualRow.setActualNo(no);
            actualRow.setNetTime(new Date());
            actualRow.setNetMode(DbConstants.TYPE);
//            actualRow.setNote("自助计量匹配结净");
            actualRow.setNetMode("1");
            actualRow.setUploadFlag("1");
            actualRow.setValueFlag("1");
            actualRow.setIsPreSacale("1");
            actualRow.setPredictionNo(scale.getPredictionNo());
            actualRow.setPredictionType("1");
            actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
            if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                actualRow.setNetManNo(actualRow.getGrossManNo());
                actualRow.setNetManName(actualRow.getGrossManName());
                actualRow.setNetTime(actualRow.getGrossTime());
                actualRow.setCreateTime(actualRow.getGrossTime());
                actualRow.setCreateManName(actualRow.getGrossManName());
                actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
            }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
            }
            meterWorkRailwayActualMapper.insertSelective(actualRow);

            //更新预报数据
            scale.setValueFlag(DbConstants.NET);
            //        scale.setUpdateTime(new Date());
            //        scale.setUpdateManNo("system");
            //        scale.setUpdateManName("自助计量结净更新");
            scale.setUploadFlag("1");
            scale.setPredictionType("1");
            scale.setMeasureStation(actualRow.getNetSpot3No());
            scale.setMeasureStationName(actualRow.getNetSpot3Name());
            preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
        } else {
            //更新第一次毛重数据库
            grossModel.setValueFlag(DbConstants.NET);
    //        grossModel.setUpdateManNo("system");
    //        grossModel.setUpdateManName("自助计量结净更新");
    //        grossModel.setUpdateTime(new Date());
            grossModel.setWeightType("0");
            grossModel.setIsPreSacale("0");
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);
            //更新第一次皮重数据库
            tareModel.setValueFlag(DbConstants.NET);
    //        tareModel.setUpdateManNo("system");
    //        tareModel.setUpdateManName("自助计量结净更新");
    //        tareModel.setUpdateTime(new Date());
            tareModel.setWeightType("1");
            tareModel.setIsPreSacale("0");
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);

            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
            BeanUtils.copyProperties(mwca1, actualRow);

            //添加结净数据
//            String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//            actualRow.setActualNo(sdf.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            actualRow.setActualNo(no);
            actualRow.setNetTime(new Date());
            actualRow.setNetMode(DbConstants.TYPE);
            actualRow.setNote("自助计量匹配结净");
            actualRow.setNetMode("1");
            actualRow.setUploadFlag("1");
            actualRow.setValueFlag("1");
            actualRow.setIsPreSacale("0");
            actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
            if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                actualRow.setNetManNo(actualRow.getGrossManNo());
                actualRow.setNetManName(actualRow.getGrossManName());
                actualRow.setNetTime(actualRow.getGrossTime());
                actualRow.setCreateTime(actualRow.getGrossTime());
                actualRow.setCreateManName(actualRow.getGrossManName());
                actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
            }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
            }
            meterWorkRailwayActualMapper.insertSelective(actualRow);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult selfServiceMeasurement(MeterWorkRailwayActFirst first) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        HashMap<String, Object> resultMap = new HashMap<>();
        List<MeterWorkRailwayActFirst> firsts = new ArrayList<>();
        List<MeterWorkRailwayActual> actuals = new ArrayList<>();

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            //报警信息
            MeterWorkRailwayScheduledHelp railwayHelp = new MeterWorkRailwayScheduledHelp();
            String railwayNo = first.getRailwayNo() == null ? "" : first.getRailwayNo();
            railwayHelp.setRailwayNo(railwayNo);
            railwayHelp.setBaseSpotNo(first.getBaseSpotNo());
            railwayHelp.setBaseSpotName(first.getBaseSpotName());

            //查询车号是否为空，为空写入报警信息表
            if (StringUtils.isEmpty(first.getRailwayNo())) {
//                first.setActualFirstNo(afl + meterWorkRailwayActFirstMapper.getNewID(afl));
                String no = PublicMethod.IdCreate(3);
                first.setActualFirstNo(no);
                first.setRailwayNo("无车号");
                first.setCreateTime(new Date());
                int i = meterWorkRailwayActFirstMapper.insertSelective(first);
                if (i >= 1) {
                    firsts.add(first);
                    railwayHelp.setHelpContent("车号为空,请联系处理");
                    meterWorkRailwayScheduledHelpService.checkAdd(railwayHelp);
                    rm.setSucceed();
                    rm.setMessage("车号为空,请联系处理");
                    resultMap.put("firsts", firsts);
                    resultMap.put("acutals", null);
                    rm.setData(resultMap);
                    return rm;
                }
            } else {
                //查询根据车号查询有效的一次数据
                HashMap<String, Object> map = new HashMap<>();
                map.put("railwayNo", railwayNo);
                map.put("valueFlag", "1");
                List<MeterWorkRailwayActFirst> firstList = meterWorkRailwayActFirstMapper.selectByParameters(map);
                if (firstList.size() >= 1) {
                    if (firstList.size() == 1) {
//                        first.setActualFirstNo(afl + meterWorkRailwayActFirstMapper.getNewID(afl));
                        String no = PublicMethod.IdCreate(3);
                        first.setActualFirstNo(no);
                        first.setCreateTime(new Date());
                        meterWorkRailwayActFirstMapper.insertSelective(first);
                        BigDecimal num = (firstList.get(0).getMeterWeight().subtract(first.getMeterWeight())).abs();
                        //结净数据不能小于10t
                        if (num.compareTo(new BigDecimal(10000)) == -1) {
//                            first.setActualFirstNo(afl + meterWorkRailwayActFirstMapper.getNewID(afl));
                            first.setActualFirstNo(PublicMethod.IdCreate(3));
                            first.setCreateTime(new Date());
                            int i = meterWorkRailwayActFirstMapper.insertSelective(first);
                            if (i >= 1) {
                                railwayHelp.setHelpContent("计量完成，车号" + first.getRailwayNo() + "存在重量相近的一次记录，无法自动结净匹配");
                                meterWorkRailwayScheduledHelpService.checkAdd(railwayHelp);
                                rm.setSucceed();
                                rm.setMessage("计量完成，车号" + first.getRailwayNo() + "存在重量相近的一次记录，无法自动结净匹配");
                                resultMap.put("firsts", firsts);
                                resultMap.put("actuals", null);
                                rm.setData(resultMap);
                                return rm;
                            }
                        }else{
                            railwayHelp.setHelpContent("计量完成，车号" + first.getRailwayNo() + "结净数据小于10吨，无法自动结净匹配");
                            meterWorkRailwayScheduledHelpService.checkAdd(railwayHelp);
                            rm.setSucceed();
                            rm.setMessage("计量完成，车号" + first.getRailwayNo() + "结净数据小于10吨，无法自动结净匹配");
                            resultMap.put("firsts", firsts);
                            resultMap.put("actuals", null);
                            rm.setData(resultMap);
                            return rm;
                        }
                        String Content;
                        HashMap<String, Object> obj = new HashMap<>();
                        if (first.getMeterWeight().compareTo(firstList.get(0).getMeterWeight()) == -1) {
                            Content = matchPreClean(firstList.get(0), first);
                            obj.put("actualFirst2No", first.getActualFirstNo());
                        } else {
                            Content = matchPreClean(first, firstList.get(0));
                            obj.put("acutalFirst1No", first.getActualFirstNo());
                        }
                        railwayHelp.setHelpContent(Content);
                        meterWorkRailwayScheduledHelpService.checkAdd(railwayHelp);
                        rm.setSucceed();
                        rm.setMessage(Content);

                        List<MeterWorkRailwayActual> models = meterWorkRailwayActualMapper.selectByParameter(obj);
                        if (models.size() >= 1) {
                            actuals.add(models.get(0));
                        } else {
                            actuals.add(null);
                        }
                        resultMap.put("firsts", firsts);
                        resultMap.put("actuals", actuals);
                        rm.setData(resultMap);
                        return rm;
                    } else if (firstList.size() >= 2) {
//                        first.setActualFirstNo(afl + meterWorkRailwayActFirstMapper.getNewID(afl));
                        first.setActualFirstNo(PublicMethod.IdCreate(3));
                        first.setCreateTime(new Date());
                        int i = meterWorkRailwayActFirstMapper.insertSelective(first);
                        if (i >= 1) {
                            firsts.add(first);
                            railwayHelp.setHelpContent("计量完成，车号" + first.getRailwayNo() + "存在多条一次记录未结净，保存一次记录成功！！");
                            meterWorkRailwayScheduledHelpService.checkAdd(railwayHelp);
                            rm.setSucceed();
                            rm.setMessage("计量完成，车号" + first.getRailwayNo() + "存在多条一次记录未结净，保存一次记录成功！！");
                            resultMap.put("firsts", firsts);
                            resultMap.put("actuals", null);
                            rm.setData(resultMap);
                            return rm;
                        }
                    }
                } else {
//                    first.setActualFirstNo(afl + meterWorkRailwayActFirstMapper.getNewID(afl));
                    first.setActualFirstNo(PublicMethod.IdCreate(3));
                    first.setCreateTime(new Date());
                    int i = meterWorkRailwayActFirstMapper.insertSelective(first);
                    if (i >= 1) {
                        firsts.add(first);
                        railwayHelp.setHelpContent("计量完成，车号" + first.getRailwayNo() + "保存一次记录成功！！");
                        meterWorkRailwayScheduledHelpService.checkAdd(railwayHelp);
                        rm.setSucceed();
                        rm.setMessage("计量完成，车号" + first.getRailwayNo() + "保存一次记录成功！！");
                        resultMap.put("firsts", firsts);
                        resultMap.put("actuals", null);
                        rm.setData(resultMap);
                        return rm;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常！！>>>" + e.getMessage());
        }
        return rm;
    }


    @Transactional(rollbackFor = Exception.class)
    public String matchPreClean(MeterWorkRailwayActFirst grossModel, MeterWorkRailwayActFirst tareModel) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String sfl = sdf.format(new Date());
        MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();

        List<PreRailwayScale> railwayScales = preRailwayScaleMapper.selectByRailwayNo(grossModel.getRailwayNo());
        if (railwayScales.size() >= 1) {
            if (railwayScales.size() == 1) {
                ///////////无委托自动计量supplementaryData

                PreRailwayScale scale = railwayScales.get(0);
                MeterWorkRailwayActFirst actFirst01 = meterWorkRailwayActFirstMapper.selectAndMatch(grossModel.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(actFirst01, grossModel);
                MeterWorkRailwayActFirst actFirst02 = meterWorkRailwayActFirstMapper.selectAndMatch(tareModel.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(actFirst02, tareModel);

                //更新一次数据
                //更新第一次毛重数据库
                grossModel.setValueFlag(DbConstants.NET);
                grossModel.setUpdateManNo("system");
                grossModel.setUpdateManName("智能计量双委托结净更新");
                grossModel.setUpdateTime(new Date());
                grossModel.setWeightType("0");
                grossModel.setIsPreSacale("1");
                grossModel.setPredictionNo(scale.getPredictionNo());
                grossModel.setPredictionType("1");
                meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);
                //更新第一次皮重数据库
                tareModel.setValueFlag(DbConstants.NET);
                tareModel.setUpdateManNo("system");
                tareModel.setUpdateManName("自助计量结净更新");
                tareModel.setUpdateTime(new Date());
                tareModel.setWeightType("1");
                tareModel.setIsPreSacale("1");
                tareModel.setPredictionNo(scale.getPredictionNo());
                tareModel.setPredictionType("1");
                meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);

                //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(mwca1, actualRow);

                //添加结净数据
//                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);
                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);
                actualRow.setNetTime(new Date());
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setNote("自助计量匹配结净");
                actualRow.setNetMode("1");
                actualRow.setUploadFlag("1");
                actualRow.setValueFlag("1");
                actualRow.setIsPreSacale("1");
                actualRow.setPredictionNo(scale.getPredictionNo());
                actualRow.setPredictionType("1");
                actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                    actualRow.setNetManNo(actualRow.getGrossManNo());
                    actualRow.setNetManName(actualRow.getGrossManName());
                    actualRow.setNetTime(actualRow.getGrossTime());
                    actualRow.setCreateTime(actualRow.getGrossTime());
                    actualRow.setCreateManName(actualRow.getGrossManName());
                    actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                }
                meterWorkRailwayActualMapper.insertSelective(actualRow);

                //更新预报数据
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("自助计量结净更新");
                scale.setUploadFlag("1");
                scale.setPredictionType("1");
                scale.setMeasureStation(actualRow.getNetSpot3No());
                scale.setMeasureStationName(actualRow.getNetSpot3Name());
                preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
                return "计量完成，" + grossModel.getRailwayNo() + "与委托编号" + scale.getPredictionNo() + "计量结净";
            } else {
                //更新第一次毛重数据库
                grossModel.setValueFlag(DbConstants.NET);
                grossModel.setUpdateManNo("system");
                grossModel.setUpdateManName("自助计量结净更新");
                grossModel.setUpdateTime(new Date());
                grossModel.setWeightType("0");
                grossModel.setIsPreSacale("0");
                meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);
                //更新第一次皮重数据库
                tareModel.setValueFlag(DbConstants.NET);
                tareModel.setUpdateManNo("system");
                tareModel.setUpdateManName("自助计量结净更新");
                tareModel.setUpdateTime(new Date());
                tareModel.setWeightType("1");
                tareModel.setIsPreSacale("0");
                meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);

                //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                BeanUtils.copyProperties(mwca1, actualRow);

                //添加结净数据
//                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);
                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);
                actualRow.setNetTime(new Date());
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setNote("自助计量匹配结净");
                actualRow.setNetMode("1");
                actualRow.setUploadFlag("1");
                actualRow.setValueFlag("1");
                actualRow.setIsPreSacale("0");
                actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                    actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                    actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                    actualRow.setNetManNo(actualRow.getGrossManNo());
                    actualRow.setNetManName(actualRow.getGrossManName());
                    actualRow.setNetTime(actualRow.getGrossTime());
                    actualRow.setCreateTime(actualRow.getGrossTime());
                    actualRow.setCreateManName(actualRow.getGrossManName());
                    actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                }
                meterWorkRailwayActualMapper.insertSelective(actualRow);
                return "计量完成，车号" + grossModel.getRailwayNo() + "在委托信息表含有多条有效委托信息,无委托结净";
            }
        } else {
            ///////////无委托自动计量

            //更新第一次毛重数据库
            grossModel.setValueFlag(DbConstants.NET);
            grossModel.setUpdateManNo("system");
            grossModel.setUpdateManName("自助计量结净更新");
            grossModel.setUpdateTime(new Date());
            grossModel.setWeightType("0");
            grossModel.setIsPreSacale("0");
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);
            //更新第一次皮重数据库
            tareModel.setValueFlag(DbConstants.NET);
            tareModel.setUpdateManNo("system");
            tareModel.setUpdateManName("自助计量结净更新");
            tareModel.setUpdateTime(new Date());
            tareModel.setWeightType("1");
            tareModel.setIsPreSacale("0");
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);

            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
            BeanUtils.copyProperties(mwca1, actualRow);

            //添加结净数据
//            String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//            actualRow.setActualNo(sdf.format(new Date()) + no);
            String no = PublicMethod.IdCreate(3);
            actualRow.setActualNo(no);
            actualRow.setNetTime(new Date());
            actualRow.setNetMode(DbConstants.TYPE);
            actualRow.setNote("自助计量匹配结净");
            actualRow.setNetMode("1");
            actualRow.setUploadFlag("1");
            actualRow.setValueFlag("1");
            actualRow.setIsPreSacale("0");
            actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
            if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                actualRow.setNetManNo(actualRow.getGrossManNo());
                actualRow.setNetManName(actualRow.getGrossManName());
                actualRow.setNetTime(actualRow.getGrossTime());
                actualRow.setCreateTime(actualRow.getGrossTime());
                actualRow.setCreateManName(actualRow.getGrossManName());
                actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
            }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
            }
            meterWorkRailwayActualMapper.insertSelective(actualRow);
            return "计量完成，车号" + grossModel.getRailwayNo() + "无委托结净";
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult batchUpdateLcNo(List<MeterWorkRailwayActFirst> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            if(!CollectionUtils.isEmpty(models)){
                int lcCount = 0;
                for (MeterWorkRailwayActFirst model : models) {
                    MeterWorkRailwayActFirst actFirst = meterWorkRailwayActFirstMapper.selectByPrimaryKey(model.getActualFirstNo());
                    BeanUtils.copyProperties(actFirst,model);
                    if(!(actFirst != null && StringUtils.isNotEmpty(actFirst.getLcNo()) && !actFirst.getLcNo().equals("无列次"))) {
                        lcCount ++;
                    }
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String afl = sdf.format(new Date());
                String lcNo = meterWorkRailwayActFirstMapper.getLcNo(afl, models.get(0).getBaseSpotNo());
                int count = 0;

                for (MeterWorkRailwayActFirst model : models) {
                    MeterWorkRailwayActFirst actFirst = meterWorkRailwayActFirstMapper.selectByPrimaryKey(model.getActualFirstNo());
                    if(actFirst != null && StringUtils.isNotEmpty(actFirst.getLcNo()) && !actFirst.getLcNo().equals("无列次")) {
                        continue;
                    }
                    MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                    logModel.setId(PublicMethod.IdCreate(2));
                    logModel.setCreateManNo(model.getCreateManNo());
                    logModel.setCreateManName(model.getCreateManName());
                    logModel.setCreateTime(new Date());
                    logModel.setOperationPage("C#端-静态轨道衡一次数据维护");
                    logModel.setOperationFunction("批量生成列次号");
                    logModel.setOperationId(model.getActualFirstNo());
                    logModel.setOperationPredictionNo(model.getPredictionNo());
                    logModel.setOperationCarnNo(model.getRailwayNo());
                    logModel.setOperationBeforeFix(model.toString());

                    model.setLcNo(model.getBaseSpotName().replaceAll("#", "")
                            .replaceAll("轨道衡", "")
                            .replaceAll("静轨", "")
                            .replaceAll("静态", "") + "-" + afl + lcNo + "(" + lcCount + ")");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model);
                    MeterWorkRailwayActual actualModel = meterWorkRailwayActualMapper.selectActual(model.getActualFirstNo());
                    if(actualModel != null) {
                        actualModel.setLcNo(model.getLcNo());
                        meterWorkRailwayActualMapper.updateByPrimaryKeySelective(actualModel);
                    }

                    logModel.setOperationAfterFix(model.toString());
                    meterBaseOperationLogMapper.insertSelective(logModel);
                    count++;
                }
                rm.setSucceed();
                rm.setCode("200");
                rm.setData(count);
                rm.setMessage("操作成功,总共" + count + "条数据修改列次编号成功!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常！！>>>" + e.getMessage());
        }
        return rm;
    }


    //静轨标识皮结净
    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doIdentifyWeight(MeterWorkRailwayActFirst model, boolean needUpload) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        try {
            HashMap<String, Object> map = new HashMap<>();
            List<MeterWorkRailwayActFirst> firsts = new ArrayList<>();
            List<MeterWorkRailwayActual> actuals = new ArrayList<>();

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            //一次毛重数据
            MeterWorkRailwayActFirst grossModel = model;
//            grossModel.setActualFirstNo(afl + meterWorkRailwayActFirstMapper.getNewID(afl));
            String no = PublicMethod.IdCreate(3);
            grossModel.setActualFirstNo(no);
            grossModel.setWeightType("0");
            grossModel.setCreateTime(new Date());
            meterWorkRailwayActFirstMapper.insertSelective(grossModel);
            //标识皮重保存

            MeterWorkRailwayActFirst tareModel = new MeterWorkRailwayActFirst();
            BeanUtils.copyProperties(grossModel,tareModel);
            tareModel.setWeightType("4");
            tareModel.setMeterWeight(tareModel.getRailwayFactoryWeight());
            //meterWorkRailwayActFirstMapper.insertSelective(tareModel);

//            MeterWorkRailwayActFirst tareModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(grossModel.getActualFirstNo());
//            tareModel.setActualFirstNo(afl + meterWorkRailwayActFirstMapper.getNewID(afl));
//            tareModel.setMeterWeight(model.getRailwayFactoryWeight());
//            tareModel.setWeightType("1");
//            tareModel.setCreateTime(new Date());
//            meterWorkRailwayActFirstMapper.insertSelective(tareModel);

            MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();
            if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                PreRailwayScale scale = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
                //毛重一次
                MeterWorkRailwayActFirst model01 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(grossModel.getActualFirstNo());
                //皮重一次
                MeterWorkRailwayActFirst model02 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(tareModel.getActualFirstNo());

                //修改委托状态
                if (scale != null) {
                    //委托信息是否为有效数据
                    if (scale.getValueFlag().equals("2") || scale.getValueFlag().equals("3")) {
                        rm.setMessage("委托信息已使用或者已作废！！");
                        return rm;
                    }

                    //将委托中的编号和收发货的等数据拷贝进一次数据中
                    MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first1, model01);
//                    MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(model02.getActualFirstNo(), scale.getPredictionNo());
//                    BeanUtils.copyProperties(first2, model02);

                    //修改毛重一次数据状态
                    model01.setValueFlag(DbConstants.NET);
                    model01.setUpdateManNo("system");
                    model01.setUpdateManName("标识皮结净更新");
                    model01.setUpdateTime(new Date());
                    model01.setWeightType("0");
                    model01.setIsPreSacale("1");
                    model01.setPredictionNo(scale.getPredictionNo());
                    model01.setPredictionType("1");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model01);
                    //修改皮重一次数据状态
//                    model02.setValueFlag(DbConstants.NET);
//                    model02.setUpdateManNo("system");
//                    model02.setUpdateManName("标识皮结净更新");
//                    model02.setUpdateTime(new Date());
//                    model02.setWeightType("1");
//                    model02.setIsPreSacale("1");
//                    model02.setPredictionNo(scale.getPredictionNo());
//                    model02.setPredictionType("1");
//                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model02);

                    MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(model01.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(mwca1, actualRow);
                    //保存结净
//                    String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    actualRow.setActualNo(PublicMethod.IdCreate(3));
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("标识皮匹配结净");
                    actualRow.setTareWeight(model.getRailwayFactoryWeight());
                    actualRow.setNetMode("1");
                    actualRow.setUploadFlag("1");
                    actualRow.setValueFlag("1");
                    actualRow.setIsPreSacale("1");
                    actualRow.setPredictionNo(scale.getPredictionNo());
                    actualRow.setPredictionType("1");
                    actualRow.setNetWeight(model01.getMeterWeight().subtract(model.getRailwayFactoryWeight()));
                    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());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                    }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                    }
                    //是否发布 ture发布，false不发布
                    if(needUpload) {
                        if(actualRow.getIsPreSacale() != null && actualRow.getIsPreSacale().equals("1")) {
                            actualRow.setValueFlag("2");
                            actualRow.setUploadFlag("1");
                        }
                    }
                    meterWorkRailwayActualMapper.insertSelective(actualRow);
                    doSaveTare(tareModel,"2");

                    //修改委托编号
                    scale.setValueFlag(DbConstants.NET);
                    scale.setUpdateTime(new Date());
                    scale.setUpdateManNo("system");
                    scale.setUpdateManName("标识皮结净更新");
                    scale.setUploadFlag("1");
                    scale.setPredictionType("1");
                    scale.setMeasureStation(actualRow.getNetSpot3No());
                    scale.setMeasureStationName(actualRow.getNetSpot3Name());
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
                    firsts.add(model01);
                } else {
                    rm.setMessage("委托信息表未查询到委托信息！！");
                    return rm;
                }
            } else {
                //修改毛重一次数据状态
                grossModel.setValueFlag(DbConstants.NET);
                grossModel.setUpdateManNo("system");
                grossModel.setUpdateManName("标识皮结净更新");
                grossModel.setUpdateTime(new Date());
                grossModel.setWeightType("0");
                grossModel.setIsPreSacale("0");
                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(grossModel);
                //修改皮重一次数据状态
//                tareModel.setValueFlag(DbConstants.NET);
//                tareModel.setUpdateManNo("system");
//                tareModel.setUpdateManName("标志皮结净更新");
//                tareModel.setUpdateTime(new Date());
//                tareModel.setWeightType("1");
//                tareModel.setIsPreSacale("0");
//                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(tareModel);

                MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
//                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);
                actualRow.setActualNo(PublicMethod.IdCreate(3));
                actualRow.setNetTime(new Date());
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setNote("标识皮结净");
                actualRow.setTareWeight(model.getRailwayFactoryWeight());
                actualRow.setNetMode("1");
                actualRow.setUploadFlag("1");
                actualRow.setValueFlag("1");
                actualRow.setIsPreSacale("0");
                actualRow.setNetWeight(grossModel.getMeterWeight().subtract(model.getRailwayFactoryWeight()));
                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());
                    actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                }
                //是否发布 ture发布，false不发布
                if(needUpload) {
                    if(actualRow.getIsPreSacale() != null && actualRow.getIsPreSacale().equals("1")) {
                        actualRow.setValueFlag("2");
                        actualRow.setUploadFlag("1");
                    }
                }
                meterWorkRailwayActualMapper.insertSelective(actualRow);
                doSaveTare(tareModel,"2");
                firsts.add(grossModel);
            }
            actuals.add(actualRow);
//            meterWorkRailwayActFirstMapper.deleteByPrimaryKey(tareModel.getActualFirstNo());
            map.put("firsts", firsts);
            map.put("actuals", actuals);

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

    @Override
    public PageList<MeterWorkRailwayActFirst> likeByDescTSDG(HashMap<String, Object> param, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        HashMap<String, Object> obj = new HashMap<>();
//        obj.put("pBaseCode", "001036");
//        List<String> matterList = comBaseInfoMapper.matterNoByBaseCode(obj);
//        param.put("list", matterList);
        List<MeterWorkRailwayActFirst> rows = meterWorkRailwayActFirstMapper.likeByDescTSDG(param);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doStandardMatch(HashMap<String, Object> param) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            String actualNo = param.get("actualNo") == null ? "": param.get("actualNo").toString();
            String predictionNo = param.get("predictionNo") == null ? "": param.get("predictionNo").toString();

            Assert.isTrue(StringUtils.isNotEmpty(actualNo), "请选择一次记录");
            MeterWorkRailwayActFirst grossModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo);
            Assert.isTrue(grossModel != null , "请选择一次记录");
            MeterWorkRailwayActFirst tareModel = new MeterWorkRailwayActFirst();
            BeanUtils.copyProperties(grossModel, tareModel);

            HashMap<String, Object> obj = new HashMap();
            obj.put("carriageCode", grossModel.getConveyanceType());
            obj.put("valueFlag", "1");
            List<MeterBaseRailwayCarriageWeight> carriageWeightModel = meterBaseRailwayCarriageWeightMapper.selectByParameters(obj);
            Assert.isTrue(carriageWeightModel.size() >= 1 , "未找到有效的标准皮，不允许结净");
            tareModel.setMeterWeight(new BigDecimal(carriageWeightModel.get(0).getCarriageWeight()));
            doSaveTare(tareModel,"3");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();
            if (StringUtils.isNotEmpty(predictionNo)) {
                PreRailwayScale scale = preRailwayScaleMapper.selectByPrimaryKey(predictionNo);
                Assert.isTrue(scale.getPredictionNo() != null && StringUtils.isNotEmpty(scale.getPredictionNo()), "未找到有效的委托信息");
                Assert.isTrue(!(scale.getValueFlag().equals("2") || scale.getValueFlag().equals("3")), "委托信息已使用或者已作废");
                //毛重一次
                MeterWorkRailwayActFirst model01 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(grossModel.getActualFirstNo());
                //皮重一次
                MeterWorkRailwayActFirst model02 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(tareModel.getActualFirstNo());
                //将委托中的编号和收发货的等数据拷贝进一次数据中
                MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first1, model01);

                //修改毛重一次数据状态
                model01.setValueFlag(DbConstants.NET);
                model01.setNote("标准皮结净更新");
                model01.setWeightType("0");
                model01.setIsPreSacale("1");
                model01.setPredictionNo(scale.getPredictionNo());
                model01.setPredictionType("1");
                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model01);

                MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(model01.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
//                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);
                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);
                actualRow.setNetTime(new Date());
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setNote("标准皮匹配结净");
                actualRow.setTareWeight(new BigDecimal(carriageWeightModel.get(0).getCarriageWeight()));
                actualRow.setNetMode("1");
                actualRow.setUploadFlag("1");
                actualRow.setValueFlag("1");
                actualRow.setIsPreSacale("1");
                actualRow.setPredictionNo(scale.getPredictionNo());
                actualRow.setPredictionType("1");
                actualRow.setNetWeight(model01.getMeterWeight().subtract(new BigDecimal(carriageWeightModel.get(0).getCarriageWeight())));
                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());
                    actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                }
                meterWorkRailwayActualMapper.insertSelective(actualRow);

                //修改委托编号
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("标准皮结净更新");
                scale.setUploadFlag("1");
                scale.setPredictionType("1");
                scale.setMeasureStation(actualRow.getNetSpot3No());
                scale.setMeasureStationName(actualRow.getNetSpot3Name());
                preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
            } else {
                //修改毛重一次数据状态
                grossModel.setValueFlag(DbConstants.NET);
                grossModel.setUpdateManNo("system");
                grossModel.setUpdateManName("标准皮结净更新");
                grossModel.setUpdateTime(new Date());
                grossModel.setWeightType("0");
                grossModel.setIsPreSacale("0");
                meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(grossModel);
                //修改皮重一次数据状态

                MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
//                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
//                actualRow.setActualNo(sdf.format(new Date()) + no);
                String no = PublicMethod.IdCreate(3);
                actualRow.setActualNo(no);
                actualRow.setNetTime(new Date());
                actualRow.setNetMode(DbConstants.TYPE);
                actualRow.setNote("标准皮结净");
                actualRow.setTareWeight(new BigDecimal(carriageWeightModel.get(0).getCarriageWeight()));
                actualRow.setNetMode("1");
                actualRow.setUploadFlag("1");
                actualRow.setValueFlag("1");
                actualRow.setIsPreSacale("0");
                actualRow.setNetWeight(grossModel.getMeterWeight().subtract(new BigDecimal(carriageWeightModel.get(0).getCarriageWeight())));
                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());
                    actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                }
                meterWorkRailwayActualMapper.insertSelective(actualRow);
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            return rm;
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！>>>" + e.getMessage());
        }
        return null;
    }

    @Override
    public RESTfulResult batchMatchView(HashMap<String, Object> param) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            String[] lcNoStr = param.get("lcNoTemp").toString().split(",");
            param.put("lcNoStr", lcNoStr);
            for (String str: lcNoStr) {
                if(str.equals("")) {
                    param.put("lcNoNull", "1");
                    break;
                }else {
                    param.put("lcNoNull", "");
                }
            }
            rm.setData(meterWorkRailwayActFirstMapper.batchMatchView(param));
            rm.setSucceed();
            rm.setMessage("操作成功");
            rm.setCode("200");
        }catch(Exception e) {
            rm.setMessage(e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult batchMatchFirstView(HashMap<String, Object> param) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            Assert.isTrue(param.get("lcNoTemp") != null, "未勾选列次号");
            String[] lcNoStr = param.get("lcNoTemp").toString().split(",");
            param.put("lcNoStr", lcNoStr);
            for (String str: lcNoStr) {
                if(str.equals("")) {
                    param.put("lcNoNull", "1");
                    break;
                }else {
                    param.put("lcNoNull", "");
                }
            }
            List<HashMap> data = meterWorkRailwayActFirstMapper.batchMatchFirstView(param);
            for (HashMap model: data) {
                if(model.get("PREDICTION_NO") == null || StringUtils.isEmpty("PREDICTION_NO")){
                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("railwayNo", model.get("RAILWAY_NO"));
                    List<PreRailwayScale> scaleList = preRailwayScaleMapper.noPage(obj);
                    if(scaleList.size() == 1) {
                        model.remove("PREDICTION_NO");
                        model.put("PREDICTION_NO", scaleList.get(0).getPredictionNo());
                    }
                }
            }
            Map<String, Integer> map = new HashMap<String, Integer>();
            for (Map<String, String> temp : data) {
                Integer count = map.get(temp.get("RAILWAY_NO"));
                map.put(temp.get("RAILWAY_NO"), (count == null) ? 1 : count + 1);
                Assert.isTrue(map.get(temp.get("RAILWAY_NO")) <= 2, "车号["+temp.get("RAILWAY_NO")+"]存在超过2条一次记录，请先处理后再进行批量处理！");
            }

            rm.setData(data);
            rm.setSucceed();
            rm.setMessage("操作成功");
            rm.setCode("200");
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        }
        return rm;
    }

    @Override
    public List<MeterWorkRailwayActFirst> noPage(HashMap<String, Object> params) {
        List<MeterWorkRailwayActFirst> rows = meterWorkRailwayActFirstMapper.likeByDesc(params);
        return rows;
    }

    public static void main(String[] args) {
        BigDecimal a = new BigDecimal(95120);
        BigDecimal b = a.divide(new BigDecimal(1000));
        //system.out.println(b);
    }

    @Override
    public RESTfulResult DynamicBatchMatchAIweight(List<MeterWorkRailwayReceived> receivedList) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            List<String> carList = new ArrayList<>();
            String strCar = "";
            //判断从表数据中是否包含委托编号
//            for (MeterWorkRailwayReceived receivedModel: receivedList) {
//                Assert.isTrue(receivedModel.getPredictionNo() != null && StringUtils.isNotEmpty(receivedModel.getPredictionNo()), "所选择的数据中包含没有委托的数据，不能结净");
//            }
            int count = 0;
            //将从表数据的车号所对应有效的AI标识皮进行结净,未找到Ai标识皮就跳过
            for (MeterWorkRailwayReceived receivedModel: receivedList) {

                String userId = receivedModel.getUpdateManNo();
                String userName = receivedModel.getUpdateManName();

                HashMap obj = new HashMap();
                obj.put("railwayNo", receivedModel.getRailwayNo());
                obj.put("valueFlag", "0");
                List<MeterBaseRailwayAiweight> AiWeightList = meterBaseRailwayAiweightMapper.selectByParameters(obj);
                if(AiWeightList.size() >= 1) {
                    MeterBaseRailwayAiweight AiWeightModel = AiWeightList.get(0);

                    MeterWorkRailwayActFirst grossModel = new MeterWorkRailwayActFirst();
                    MeterWorkRailwayActFirst tareModel = new MeterWorkRailwayActFirst();
                    MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();
                    PreRailwayScale railwayScale = preRailwayScaleMapper.selectByPrimaryKey(receivedModel.getPredictionNo());

                    if(railwayScale != null) {
                        meterWorkRailwayReceivedMapper.selectAndMatch(receivedModel.getActualFirstNo(), railwayScale.getPredictionNo());
                        receivedModel.setWeightType("0");
                        receivedModel.setUpdateManNo(userId);
                        receivedModel.setUpdateManName(userName);
                        receivedModel.setUpdateTime(new Date());
                        meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(receivedModel);
                        //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                        meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(receivedModel.getActualFirstNo());

                        //从表赋值grossModel、AI也赋值tareModel并修改重量
                        BeanUtils.copyProperties(receivedModel, grossModel);
                        grossModel.setLcNo(receivedModel.getRailwayWorkNo());
                        meterWorkRailwayActFirstMapper.insertSelective(grossModel);
                        BeanUtils.copyProperties(grossModel, tareModel);
                        tareModel.setMeterWeight(AiWeightModel.getMeterWeight());
                        doSaveTare(tareModel,"2");

                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setWeightType("0");
                        grossModel.setNote("AI标识皮匹配委托结净更新");
                        grossModel.setIsPreSacale("1");
                        grossModel.setPredictionNo(railwayScale.getPredictionNo());
                        grossModel.setPredictionType("1");
                        grossModel.setUpdateManNo(userId);
                        grossModel.setUpdateManName(userName);
                        grossModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                        //添加第二次毛重数据
                        SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyyMMdd");
                        String afl = sdfFormat.format(new Date());
                        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), railwayScale.getPredictionNo());
                        BeanUtils.copyProperties(mwca, actualRow);

                        //添加结净数据
//                    actualRow.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.setActualNo(no);
                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);
                        actualRow.setTareWeight(tareModel.getMeterWeight());
                        actualRow.setNetTime(new Date());
                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setNote("AI标识皮匹配委托结净更新");
                        actualRow.setNetMode("2");
                        actualRow.setUploadFlag("1");
                        actualRow.setIsPreSacale("1");
                        actualRow.setValueFlag("2");
                        actualRow.setPredictionType("1");
                        actualRow.setPredictionNo(railwayScale.getPredictionNo());
                        //添加车型
                        actualRow.setConveyanceType(receivedModel.getRailwayModel());
                        actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                        }else {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                            actualRow.setNetManNo(actualRow.getTareManNo());
                            actualRow.setNetManName(actualRow.getTareManName());
                            actualRow.setNetTime(actualRow.getTareTime());
                            actualRow.setCreateTime(actualRow.getTareTime());
                            actualRow.setCreateManName(actualRow.getTareManName());
                        }
                        actualRow.setUpdateManNo(userId);
                        actualRow.setUpdateManName(userName);
                        actualRow.setUpdateTime(new Date());
                        actualRow.setNetManNo(userId);
                        actualRow.setNetManName(userName);
                        actualRow.setNetTime(new Date());
                        actualRow.setMemo("标识皮结净");
                        actualRow.setNote(receivedModel.getMemo());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                        meterWorkRailwayActualMapper.insertSelective(actualRow);

                        //更新预报数
                        railwayScale.setValueFlag(DbConstants.NET);
                        railwayScale.setUploadFlag("1");
                        railwayScale.setValueFlag("2");
                        railwayScale.setPredictionType("1");
                        railwayScale.setMeasureStation(actualRow.getNetSpot3No());
                        railwayScale.setMeasureStationName(actualRow.getNetSpot3Name());
                        railwayScale.setUpdateTime(new Date());
                        railwayScale.setUpdateManNo(userId);
                        railwayScale.setUpdateManName(userName);
                        preRailwayScaleMapper.updateByPrimaryKeySelective(railwayScale);

                        count++;
                    }else {
                        receivedModel.setWeightType("0");
                        receivedModel.setUpdateManNo(userId);
                        receivedModel.setUpdateManName(userName);
                        receivedModel.setUpdateTime(new Date());
                        meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(receivedModel);
                        //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                        meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(receivedModel.getActualFirstNo());

                        //从表赋值grossModel、AI也赋值tareModel并修改重量
                        BeanUtils.copyProperties(receivedModel, grossModel);
                        grossModel.setLcNo(receivedModel.getRailwayWorkNo());
                        meterWorkRailwayActFirstMapper.insertSelective(grossModel);
                        BeanUtils.copyProperties(grossModel, tareModel);
                        tareModel.setMeterWeight(AiWeightModel.getMeterWeight());
                        doSaveTare(tareModel,"2");

                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setWeightType("0");
                        grossModel.setNote("AI标识皮无委托结净更新");
                        grossModel.setIsPreSacale("0");
                        grossModel.setUpdateManNo(userId);
                        grossModel.setUpdateManName(userName);
                        grossModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                        //添加第二次毛重数据
                        SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyyMMdd");
                        String afl = sdfFormat.format(new Date());
                        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                        BeanUtils.copyProperties(mwca, actualRow);

                        //添加结净数据
//                    actualRow.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.setActualNo(no);
                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);
                        actualRow.setTareWeight(tareModel.getMeterWeight());
                        actualRow.setNetTime(new Date());
                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setNote("AI标识皮无委托结净更新");
                        actualRow.setNetMode("2");
                        actualRow.setUploadFlag("1");
                        actualRow.setIsPreSacale("0");
                        actualRow.setValueFlag("1");
                        //添加车型
                        actualRow.setConveyanceType(receivedModel.getRailwayModel());
                        actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                        }else {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                            actualRow.setNetManNo(actualRow.getTareManNo());
                            actualRow.setNetManName(actualRow.getTareManName());
                            actualRow.setNetTime(actualRow.getTareTime());
                            actualRow.setCreateTime(actualRow.getTareTime());
                            actualRow.setCreateManName(actualRow.getTareManName());
                        }
                        actualRow.setUpdateManNo(userId);
                        actualRow.setUpdateManName(userName);
                        actualRow.setUpdateTime(new Date());
                        actualRow.setNetManNo(userId);
                        actualRow.setNetManName(userName);
                        actualRow.setNetTime(new Date());
                        actualRow.setMemo("标识皮结净");
                        actualRow.setNote(receivedModel.getMemo());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                        meterWorkRailwayActualMapper.insertSelective(actualRow);

                        count++;
                    }
                    MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                    logModel.setId(PublicMethod.IdCreate(2));
                    logModel.setCreateManNo(userId);
                    logModel.setCreateManName(userName);
                    logModel.setCreateTime(new Date());
                    logModel.setOperationPage("C#端-动轨数据维护");
                    logModel.setOperationFunction("AI标识皮结净");
                    logModel.setOperationId(grossModel.getActualFirstNo());
                    logModel.setOperationPredictionNo(grossModel.getPredictionNo());
                    logModel.setOperationCarnNo(grossModel.getRailwayNo());
                    logModel.setOperationBeforeFix(receivedModel.toString());
                    logModel.setOperationAfterFix(grossModel.toString());
                    meterBaseOperationLogMapper.insertSelective(logModel);

                    if(receivedModel.getMemo()!=null&&!"".equals(receivedModel)){
                        MeterBaseOperationLog logModel2 = new MeterBaseOperationLog();
                        logModel2.setId(PublicMethod.IdCreate(2));
                        logModel2.setCreateManNo(userId);
                        logModel2.setCreateManName(userName);
                        logModel2.setCreateTime(new Date());
                        logModel2.setOperationPage("C#端-动轨净重数据维护");
                        logModel2.setOperationFunction("AI标识皮结净报警处理");
                        logModel2.setOperationId(actualRow.getActualNo());
                        logModel2.setOperationPredictionNo(actualRow.getPredictionNo());
                        logModel2.setOperationCarnNo(actualRow.getRailwayNo());
                        logModel2.setOperationBeforeFix(receivedModel.toString());
                        logModel2.setOperationAfterFix(actualRow.toString());
                        meterBaseOperationLogMapper.insertSelective(logModel2);
                    }
                }else {
                    carList.add(receivedModel.getRailwayNo());
                }
            }
            //修改主表是否已处理完
            if(receivedList.size() >= 1) {
                MeterWorkRailwayReceived receivedModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(receivedList.get(0).getActualFirstNo());
                if(receivedModel != null){
                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
                    HashMap<String, Object> countMap = meterWorkRailwayReceivedMapper.getCount(obj);
                    int countNum = Integer.parseInt(countMap.get("COUNTNUM").toString());
                    if(countNum < 1) {
                        HashMap<String, Object> objMap = new HashMap<>();
                        objMap.put("soundName", receivedModel.getRailwayWorkNo());
                        List<MeterWorkRailwayRecMain> recMains = meterWorkRailwayRecMainMapper.selectByParameters(objMap);
                        if(recMains.size() >= 1) {
                            recMains.get(0).setIsDispose("1");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMains.get(0));
                        }
                    }
                }
            }
            if(carList.size() >= 1) {
                strCar = carList.get(0);
                if(carList.size() >= 2) {
                    for(String car: carList) {
                        strCar = strCar + "," + car;
                    }
                }
            }
            rm.setSucceed();
            rm.setCode("200");
//            rm.setMessage("操作成功,总共"+ receivedList.size() +"条数据，共有：" + count + "条数据匹配AI标识皮成功");
            rm.setMessage(strCar);
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常：>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult DynamicBatchMatchCarriageWeight(List<MeterWorkRailwayReceived> receivedList) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            List<String> carList = new ArrayList<>();
            String strCar = "";
            //判断从表数据中是否包含委托编号
//            for (MeterWorkRailwayReceived receivedModel: receivedList) {
//                Assert.isTrue(receivedModel.getPredictionNo() != null && StringUtils.isNotEmpty(receivedModel.getPredictionNo()), "所选择的数据中包含没有委托的数据，不能结净");
//            }
            int count = 0;
            //将从表数据的车号所对应有效的AI标识皮进行结净,未找到Ai标识皮就跳过
            for (MeterWorkRailwayReceived receivedModel: receivedList) {
                String userId = receivedModel.getUpdateManNo();
                String userName = receivedModel.getUpdateManName();

                HashMap obj = new HashMap();
                obj.put("carriageCode", receivedModel.getRailwayModel());
                obj.put("valueFlag", "1");
                List<MeterBaseRailwayCarriageWeight> carriageWeightList = meterBaseRailwayCarriageWeightMapper.selectByParameters(obj);
                if(carriageWeightList.size() >= 1) {
                    MeterBaseRailwayCarriageWeight carriageWeightModel = carriageWeightList.get(0);

                    MeterWorkRailwayActFirst grossModel = new MeterWorkRailwayActFirst();
                    MeterWorkRailwayActFirst tareModel = new MeterWorkRailwayActFirst();
                    MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();
                    PreRailwayScale railwayScale = preRailwayScaleMapper.selectByPrimaryKey(receivedModel.getPredictionNo());
                    if(railwayScale != null) {
                        meterWorkRailwayReceivedMapper.selectAndMatch(receivedModel.getActualFirstNo(), railwayScale.getPredictionNo());
                        receivedModel.setWeightType("0");
                        receivedModel.setUpdateManNo(userId);
                        receivedModel.setUpdateManName(userName);
                        receivedModel.setUpdateTime(new Date());
                        meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(receivedModel);
                        //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                        meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(receivedModel.getActualFirstNo());

                        //更新主表状态METER_WORK_RAILWAY_REC_MAIN
//                    if(receivedModel.getRailwayWorkNo() != null && StringUtils.isNotEmpty(receivedModel.getRailwayWorkNo())) {
//                        HashMap<String, Object> objRec = new HashMap<>();
//                        objRec.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
//                        objRec.put("valueFlag", "1");
//                        List<MeterWorkRailwayReceived> receiveds = meterWorkRailwayReceivedMapper.selectByParameters(objRec);
//                        if(receiveds.size() < 1) {
//                            HashMap<String, Object> objMap = new HashMap<>();
//                            objMap.put("soundName", receivedModel.getRailwayWorkNo());
//                            List<MeterWorkRailwayRecMain> recMains = meterWorkRailwayRecMainMapper.selectByParameters(objMap);
//                            if(recMains.size() >= 1) {
//                                recMains.get(0).setIsDispose("1");
//                                meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMains.get(0));
//                            }
//                        }
//                    }

                        //从表赋值grossModel、AI也赋值tareModel并修改重量
                        BeanUtils.copyProperties(receivedModel, grossModel);
                        grossModel.setLcNo(receivedModel.getRailwayWorkNo());
                        meterWorkRailwayActFirstMapper.insertSelective(grossModel);
                        BeanUtils.copyProperties(grossModel, tareModel);
                        tareModel.setMeterWeight(new BigDecimal(carriageWeightModel.getCarriageWeight()));
                        doSaveTare(tareModel,"3");
                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setWeightType("0");
                        grossModel.setNote("规则皮匹配委托结净更新");
                        grossModel.setIsPreSacale("1");
                        grossModel.setPredictionNo(railwayScale.getPredictionNo());
                        grossModel.setPredictionType("1");
                        grossModel.setUpdateManNo(userId);
                        grossModel.setUpdateManName(userName);
                        grossModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                        //添加第二次毛重数据
                        SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyyMMdd");
                        String afl = sdfFormat.format(new Date());
                        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), railwayScale.getPredictionNo());
                        BeanUtils.copyProperties(mwca, actualRow);

                        //添加结净数据
//                    actualRow.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.setActualNo(no);
                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);
                        actualRow.setTareWeight(tareModel.getMeterWeight());
                        actualRow.setNetTime(new Date());
                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setNote("规则皮配委托结净更新");
                        actualRow.setNetMode("2");
                        actualRow.setUploadFlag("1");
                        actualRow.setIsPreSacale("1");
                        actualRow.setValueFlag("2");
                        actualRow.setPredictionType("1");
                        actualRow.setPredictionNo(railwayScale.getPredictionNo());
                        //添加车型
                        actualRow.setConveyanceType(receivedModel.getRailwayModel());
                        actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                        }else {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                            actualRow.setNetManNo(actualRow.getTareManNo());
                            actualRow.setNetManName(actualRow.getTareManName());
                            actualRow.setNetTime(actualRow.getTareTime());
                            actualRow.setCreateTime(actualRow.getTareTime());
                            actualRow.setCreateManName(actualRow.getTareManName());
                        }
                        actualRow.setUpdateManNo(userId);
                        actualRow.setUpdateManName(userName);
                        actualRow.setUpdateTime(new Date());
                        actualRow.setNetManNo(userId);
                        actualRow.setNetManName(userName);
                        actualRow.setNetTime(new Date());
                        actualRow.setMemo("规则皮结净");
                        actualRow.setNote(receivedModel.getMemo());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                        meterWorkRailwayActualMapper.insertSelective(actualRow);

                        //更新预报数
                        railwayScale.setValueFlag(DbConstants.NET);
                        railwayScale.setUploadFlag("1");
                        railwayScale.setValueFlag("2");
                        railwayScale.setPredictionType("1");
                        railwayScale.setMeasureStation(actualRow.getNetSpot3No());
                        railwayScale.setMeasureStationName(actualRow.getNetSpot3Name());
                        railwayScale.setUpdateTime(new Date());
                        railwayScale.setUpdateManNo(userId);
                        railwayScale.setUpdateManName(userName);
                        preRailwayScaleMapper.updateByPrimaryKeySelective(railwayScale);

                        count++;
                    }else {
                        receivedModel.setWeightType("0");
                        receivedModel.setUpdateManNo(userId);
                        receivedModel.setUpdateManName(userName);
                        receivedModel.setUpdateTime(new Date());
                        meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(receivedModel);
                        //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                        meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(receivedModel.getActualFirstNo());

                        //从表赋值grossModel、AI也赋值tareModel并修改重量
                        BeanUtils.copyProperties(receivedModel, grossModel);
                        grossModel.setLcNo(receivedModel.getRailwayWorkNo());
                        meterWorkRailwayActFirstMapper.insertSelective(grossModel);
                        BeanUtils.copyProperties(grossModel, tareModel);
                        tareModel.setMeterWeight(new BigDecimal(carriageWeightModel.getCarriageWeight()));
                        doSaveTare(tareModel,"3");
                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setWeightType("0");
                        grossModel.setNote("规则皮无委托结净更新");
                        grossModel.setIsPreSacale("0");
                        grossModel.setUpdateManNo(userId);
                        grossModel.setUpdateManName(userName);
                        grossModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                        //添加第二次毛重数据
                        SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyyMMdd");
                        String afl = sdfFormat.format(new Date());
                        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                        BeanUtils.copyProperties(mwca, actualRow);

                        //添加结净数据
//                    actualRow.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.setActualNo(no);
                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);
                        actualRow.setTareWeight(tareModel.getMeterWeight());
                        actualRow.setNetTime(new Date());
                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setNote("规则皮无委托结净更新");
                        actualRow.setNetMode("2");
                        actualRow.setUploadFlag("1");
                        actualRow.setIsPreSacale("0");
                        actualRow.setValueFlag("1");
                        //添加车型
                        actualRow.setConveyanceType(receivedModel.getRailwayModel());
                        actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                        }else {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                            actualRow.setNetManNo(actualRow.getTareManNo());
                            actualRow.setNetManName(actualRow.getTareManName());
                            actualRow.setNetTime(actualRow.getTareTime());
                            actualRow.setCreateTime(actualRow.getTareTime());
                            actualRow.setCreateManName(actualRow.getTareManName());
                        }
                        actualRow.setUpdateManNo(userId);
                        actualRow.setUpdateManName(userName);
                        actualRow.setUpdateTime(new Date());
                        actualRow.setNetManNo(userId);
                        actualRow.setNetManName(userName);
                        actualRow.setNetTime(new Date());
                        actualRow.setMemo("规则皮结净");
                        actualRow.setNote(receivedModel.getMemo());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                        meterWorkRailwayActualMapper.insertSelective(actualRow);

                        count++;
                    }

                    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(grossModel.getActualFirstNo());
                    logModel.setOperationPredictionNo(grossModel.getPredictionNo());
                    logModel.setOperationCarnNo(grossModel.getRailwayNo());
                    logModel.setOperationBeforeFix(receivedModel.toString());
                    logModel.setOperationAfterFix(grossModel.toString());
                    meterBaseOperationLogMapper.insertSelective(logModel);
                    if(receivedModel.getMemo()!=null&&!"".equals(receivedModel)){
                        MeterBaseOperationLog logModel2 = new MeterBaseOperationLog();
                        logModel2.setId(PublicMethod.IdCreate(2));
                        logModel2.setCreateManNo(userId);
                        logModel2.setCreateManName(userName);
                        logModel2.setCreateTime(new Date());
                        logModel2.setOperationPage("C#端-动轨净重数据维护");
                        logModel2.setOperationFunction("AI标识皮结净报警处理");
                        logModel2.setOperationId(actualRow.getActualNo());
                        logModel2.setOperationPredictionNo(actualRow.getPredictionNo());
                        logModel2.setOperationCarnNo(actualRow.getRailwayNo());
                        logModel2.setOperationBeforeFix(receivedModel.toString());
                        logModel2.setOperationAfterFix(actualRow.toString());
                        meterBaseOperationLogMapper.insertSelective(logModel2);
                    }
                }else {
                    carList.add(receivedModel.getRailwayNo());
                }
            }
            //修改主表是否已处理完
            if(receivedList.size() >= 1) {
                MeterWorkRailwayReceived receivedModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(receivedList.get(0).getActualFirstNo());
                if(receivedModel != null){
                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
                    HashMap<String, Object> countMap = meterWorkRailwayReceivedMapper.getCount(obj);
                    int countNum = Integer.parseInt(countMap.get("COUNTNUM").toString());
                    if(countNum < 1) {
                        HashMap<String, Object> objMap = new HashMap<>();
                        objMap.put("soundName", receivedModel.getRailwayWorkNo());
                        List<MeterWorkRailwayRecMain> recMains = meterWorkRailwayRecMainMapper.selectByParameters(objMap);
                        if(recMains.size() >= 1) {
                            recMains.get(0).setIsDispose("1");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMains.get(0));
                        }
                    }
                }
            }
           if(carList.size() >= 1) {
                strCar = carList.get(0);
                if(carList.size() >= 2) {
                    for(String car: carList) {
                        strCar = strCar + "," + car;
                    }
                }
            }
            rm.setSucceed();
            rm.setCode("200");
//            rm.setMessage("操作成功,总共"+ receivedList.size() +"条数据，共有：" + count + "条数据匹配标准皮成功");
            rm.setMessage(strCar);
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常：>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation= Propagation.NESTED)
    public RESTfulResult DynamicBatchMatchFirstWeight(List<MeterWorkRailwayReceived> receivedList) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
            List<String> carList = new ArrayList<>();
            String strCar = "";
            //判断从表数据中是否包含委托编号
            for (MeterWorkRailwayReceived receivedModel: receivedList) {
                Assert.isTrue(receivedModel.getPredictionNo() != null && StringUtils.isNotEmpty(receivedModel.getPredictionNo()), "所选择的数据中包含没有委托的数据，不能结净");
            }
            try{
            int count = 0;
            //将从表数据的车号所对应有效的AI标识皮进行结净,未找到历史皮就跳过
            for (MeterWorkRailwayReceived receivedModel: receivedList) {
                String userId = receivedModel.getUpdateManNo();
                String userName = receivedModel.getUpdateManName();
                HashMap obj = new HashMap();
                obj.put("railwayNo", receivedModel.getRailwayNo());
                obj.put("valueFlag", "1");
                obj.put("weightType", "1");
                List<MeterWorkRailwayActFirst> firstWeightList = meterWorkRailwayActFirstMapper.selectByParameters(obj);
                if(firstWeightList.size() == 1) {
                    MeterWorkRailwayActFirst firstWeightModel = firstWeightList.get(0);
                    //查找到一条有效的数据并且这条数据小于从表重量才进行结净，否则跳过
                    if(receivedModel.getMeterWeight().compareTo(firstWeightModel.getMeterWeight()) <= 0) {
                        continue;
                    }
                    MeterWorkRailwayActFirst grossModel = new MeterWorkRailwayActFirst();
                    MeterWorkRailwayActFirst tareModel = firstWeightModel;
                    MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();
                    PreRailwayScale railwayScale = preRailwayScaleMapper.selectByPrimaryKey(receivedModel.getPredictionNo());
                    //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用

                    if(railwayScale != null) {
                        meterWorkRailwayReceivedMapper.selectAndMatch(receivedModel.getActualFirstNo(), railwayScale.getPredictionNo());
                        receivedModel.setWeightType("0");
                        receivedModel.setUpdateManNo(userId);
                        receivedModel.setUpdateManName(userName);
                        receivedModel.setUpdateTime(new Date());
                        meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(receivedModel);
                        meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(receivedModel.getActualFirstNo());

                        //更新主表状态METER_WORK_RAILWAY_REC_MAIN
//                    if(receivedModel.getRailwayWorkNo() != null && StringUtils.isNotEmpty(receivedModel.getRailwayWorkNo())) {
//                        HashMap<String, Object> objRec = new HashMap<>();
//                        objRec.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
//                        objRec.put("valueFlag", "1");
//                        List<MeterWorkRailwayReceived> receiveds = meterWorkRailwayReceivedMapper.selectByParameters(objRec);
//                        if(receiveds.size() < 1) {
//                            HashMap<String, Object> objMap = new HashMap<>();
//                            objMap.put("soundName", receivedModel.getRailwayWorkNo());
//                            List<MeterWorkRailwayRecMain> recMains = meterWorkRailwayRecMainMapper.selectByParameters(objMap);
//                            if(recMains.size() >= 1) {
//                                recMains.get(0).setIsDispose("1");
//                                meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMains.get(0));
//                            }
//                        }
//                    }

                        //从表赋值grossModel、AI也赋值tareModel并修改重量
                        BeanUtils.copyProperties(receivedModel, grossModel);
                        grossModel.setLcNo(receivedModel.getRailwayWorkNo());
                        meterWorkRailwayActFirstMapper.insertSelective(grossModel);

                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setWeightType("0");
                        grossModel.setNote("空皮匹配委托结净更新");
                        grossModel.setWeightType("0");
                        grossModel.setIsPreSacale("1");
                        grossModel.setPredictionNo(railwayScale.getPredictionNo());
                        grossModel.setPredictionType("1");
                        grossModel.setUpdateManNo(userId);
                        grossModel.setUpdateManName(userName);
                        grossModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                        tareModel.setValueFlag(DbConstants.NET);
                        tareModel.setWeightType("1");
                        tareModel.setNote("空皮匹配委托结净更新");
                        tareModel.setIsPreSacale("1");
                        tareModel.setPredictionNo(railwayScale.getPredictionNo());
                        tareModel.setPredictionType("1");
                        tareModel.setUpdateManNo(userId);
                        tareModel.setUpdateManName(userName);
                        tareModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);

                        //保存历史皮重
                        doSaveTare(tareModel);

                        //添加第二次毛重数据
                        SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyyMMdd");
                        String afl = sdfFormat.format(new Date());
                        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), railwayScale.getPredictionNo());
                        BeanUtils.copyProperties(mwca, actualRow);

                        //添加结净数据
//                    actualRow.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.setActualNo(no);
                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);
                        actualRow.setTareWeight(tareModel.getMeterWeight());
                        actualRow.setNetTime(new Date());
                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setNote("空皮配委托结净更新");
                        actualRow.setNetMode("2");
                        actualRow.setUploadFlag("1");
                        actualRow.setIsPreSacale("1");
                        actualRow.setValueFlag("2");
                        actualRow.setPredictionType("1");
                        actualRow.setPredictionNo(railwayScale.getPredictionNo());
                        //添加车型
                        actualRow.setConveyanceType(receivedModel.getRailwayModel());
                        actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                        }else {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                            actualRow.setNetManNo(actualRow.getTareManNo());
                            actualRow.setNetManName(actualRow.getTareManName());
                            actualRow.setNetTime(actualRow.getTareTime());
                            actualRow.setCreateTime(actualRow.getTareTime());
                            actualRow.setCreateManName(actualRow.getTareManName());
                        }
                        //铁水衡自动导入情况
                        if(actualRow.getTareManNo() != null && actualRow.getTareManNo().equals("system") &&
                                actualRow.getGrossManNo() != null && actualRow.getGrossManNo().equals("system")) {
                            if(userId != null && StringUtils.isNotEmpty(userId) && userName != null &&StringUtils.isNotEmpty(userName)) {
                                actualRow.setNetManNo(userId);
                                actualRow.setNetManName(userName);
                            }
                        }
                        actualRow.setSampleNo(tareModel.getLcNo());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                        actualRow.setUpdateManNo(userId);
                        actualRow.setUpdateManName(userName);
                        actualRow.setUpdateTime(new Date());
                        actualRow.setMemo("回空皮结净");
                        actualRow.setNote(receivedModel.getMemo());
                        meterWorkRailwayActualMapper.insertSelective(actualRow);

                        //更新预报数
                        railwayScale.setValueFlag(DbConstants.NET);
                        railwayScale.setUploadFlag("1");
                        railwayScale.setValueFlag("2");
                        railwayScale.setPredictionType("1");
                        railwayScale.setMeasureStation(actualRow.getNetSpot3No());
                        railwayScale.setMeasureStationName(actualRow.getNetSpot3Name());
                        railwayScale.setUpdateTime(new Date());
                        railwayScale.setUpdateManNo(userId);
                        railwayScale.setUpdateManName(userName);
                        preRailwayScaleMapper.updateByPrimaryKeySelective(railwayScale);

                        count++;
                    }else {
                        receivedModel.setWeightType("0");
                        receivedModel.setUpdateManNo(userId);
                        receivedModel.setUpdateManName(userName);
                        receivedModel.setUpdateTime(new Date());
                        meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(receivedModel);
                        meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(receivedModel.getActualFirstNo());

                        //从表赋值grossModel、AI也赋值tareModel并修改重量
                        BeanUtils.copyProperties(receivedModel, grossModel);
                        grossModel.setLcNo(receivedModel.getRailwayWorkNo());
                        meterWorkRailwayActFirstMapper.insertSelective(grossModel);

                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setWeightType("0");
                        grossModel.setNote("空皮无委托结净更新");
                        grossModel.setIsPreSacale("0");
                        grossModel.setUpdateManNo(userId);
                        grossModel.setUpdateManName(userName);
                        grossModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                        tareModel.setValueFlag(DbConstants.NET);
                        tareModel.setWeightType("1");
                        tareModel.setNote("空皮无委托结净更新");
                        tareModel.setIsPreSacale("0");
                        tareModel.setUpdateManNo(userId);
                        tareModel.setUpdateManName(userName);
                        tareModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);

                        //保存历史皮重
                        doSaveTare(tareModel);

                        //添加第二次毛重数据
                        SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyyMMdd");
                        String afl = sdfFormat.format(new Date());
                        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                        BeanUtils.copyProperties(mwca, actualRow);

                        //添加结净数据
//                    actualRow.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.setActualNo(no);
                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);
                        actualRow.setTareWeight(tareModel.getMeterWeight());
                        actualRow.setNetTime(new Date());
                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setNote("空皮无委托结净更新");
                        actualRow.setNetMode("2");
                        actualRow.setUploadFlag("1");
                        actualRow.setIsPreSacale("0");
                        actualRow.setValueFlag("1");
                        //添加车型
                        actualRow.setConveyanceType(receivedModel.getRailwayModel());
                        actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                        }else {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                            actualRow.setNetManNo(actualRow.getTareManNo());
                            actualRow.setNetManName(actualRow.getTareManName());
                            actualRow.setNetTime(actualRow.getTareTime());
                            actualRow.setCreateTime(actualRow.getTareTime());
                            actualRow.setCreateManName(actualRow.getTareManName());
                        }
                        //铁水衡自动导入情况
                        if(actualRow.getTareManNo() != null && actualRow.getTareManNo().equals("system") &&
                                actualRow.getGrossManNo() != null && actualRow.getGrossManNo().equals("system")) {
                            if(userId != null && StringUtils.isNotEmpty(userId) && userName != null &&StringUtils.isNotEmpty(userName)) {
                                actualRow.setNetManNo(userId);
                                actualRow.setNetManName(userName);
                            }
                        }
                        actualRow.setSampleNo(tareModel.getLcNo());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                        actualRow.setUpdateManNo(userId);
                        actualRow.setUpdateManName(userName);
                        actualRow.setUpdateTime(new Date());
                        actualRow.setMemo("回空皮结净");
                        actualRow.setNote(receivedModel.getMemo());
                        meterWorkRailwayActualMapper.insertSelective(actualRow);

                        count++;
                    }
                    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(grossModel.getActualFirstNo() + "," + tareModel.getActualFirstNo());
                    logModel.setOperationPredictionNo(grossModel.getPredictionNo());
                    logModel.setOperationCarnNo(grossModel.getRailwayNo());
                    logModel.setOperationBeforeFix(receivedModel.toString());
                    logModel.setOperationAfterFix(grossModel.toString());
                    meterBaseOperationLogMapper.insertSelective(logModel);
                    if(receivedModel.getMemo()!=null&&!"".equals(receivedModel)){
                        MeterBaseOperationLog logModel2 = new MeterBaseOperationLog();
                        logModel2.setId(PublicMethod.IdCreate(2));
                        logModel2.setCreateManNo(userId);
                        logModel2.setCreateManName(userName);
                        logModel2.setCreateTime(new Date());
                        logModel2.setOperationPage("C#端-动轨净重数据维护");
                        logModel2.setOperationFunction("AI标识皮结净报警处理");
                        logModel2.setOperationId(actualRow.getActualNo());
                        logModel2.setOperationPredictionNo(actualRow.getPredictionNo());
                        logModel2.setOperationCarnNo(actualRow.getRailwayNo());
                        logModel2.setOperationBeforeFix(receivedModel.toString());
                        logModel2.setOperationAfterFix(actualRow.toString());
                        meterBaseOperationLogMapper.insertSelective(logModel2);
                    }
                }else {
                    carList.add(receivedModel.getRailwayNo());
                }
            }
            //修改主表是否已处理完
            if(receivedList.size() >= 1) {
                MeterWorkRailwayReceived receivedModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(receivedList.get(0).getActualFirstNo());
                if(receivedModel != null){
                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
                    HashMap<String, Object> countMap = meterWorkRailwayReceivedMapper.getCount(obj);
                    int countNum = Integer.parseInt(countMap.get("COUNTNUM").toString());
                    if(countNum < 1) {
                        HashMap<String, Object> objMap = new HashMap<>();
                        objMap.put("soundName", receivedModel.getRailwayWorkNo());
                        List<MeterWorkRailwayRecMain> recMains = meterWorkRailwayRecMainMapper.selectByParameters(objMap);
                        if(recMains.size() >= 1) {
                            recMains.get(0).setIsDispose("1");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMains.get(0));
                        }
                    }
                }
            }
            if(carList.size() >= 1) {
                strCar = carList.get(0);
                if(carList.size() >= 2) {
                    for(String car: carList) {
                        strCar = strCar + "," + car;
                    }
                }
            }
            rm.setSucceed();
            rm.setCode("200");
//            rm.setMessage("操作成功,总共"+ receivedList.size() +"条数据，共有：" + count + "条数据匹配空皮成功");
            rm.setMessage(strCar);
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常：>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult DynamicBatchMatchReceivedWeight(List<MeterWorkRailwayReceived> receivedList) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            List<String> carList = new ArrayList<>();
            String strCar = "";
            //判断从表数据中是否包含委托编号
//            for (MeterWorkRailwayReceived receivedModel: receivedList) {
//                Assert.isTrue(receivedModel.getPredictionNo() != null && StringUtils.isNotEmpty(receivedModel.getPredictionNo()), "所选择的数据中包含没有委托的数据，不能结净");
//            }
            int count = 0;
            //将从表数据的车号所对应有效的AI标识皮进行结净,未找到历史皮就跳过
            for (MeterWorkRailwayReceived received: receivedList) {
                String userId = received.getUpdateManNo();
                String userName = received.getUpdateManName();

                //sampleNo:对应毛重、actualFirstNo：对应皮重;
                MeterWorkRailwayReceived grossReceived = meterWorkRailwayReceivedMapper.selectByPrimaryKey(received.getSampleNo());
                if(grossReceived != null) {
                    MeterWorkRailwayReceived tareReceived = meterWorkRailwayReceivedMapper.selectByPrimaryKey(received.getActualFirstNo());

                    //查找到一条有效的数据并且这条数据小于从表重量才进行结净，否则跳过
                    if(grossReceived.getMeterWeight().compareTo(tareReceived.getMeterWeight()) <= 0) {
                        continue;
                    }

                    MeterWorkRailwayActFirst grossModel = new MeterWorkRailwayActFirst();
                    MeterWorkRailwayActFirst tareModel = new MeterWorkRailwayActFirst();
                    MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();
                    PreRailwayScale railwayScale = preRailwayScaleMapper.selectByPrimaryKey(grossReceived.getPredictionNo());

                    if(railwayScale != null) {
                        meterWorkRailwayReceivedMapper.selectAndMatch(grossReceived.getActualFirstNo(), railwayScale.getPredictionNo());
                        grossReceived.setWeightType("0");
                        grossReceived.setUpdateManNo(userId);
                        grossReceived.setUpdateManName(userName);
                        grossReceived.setUpdateTime(new Date());
                        meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(grossReceived);
                        //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                        meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(grossReceived.getActualFirstNo());
                        meterWorkRailwayReceivedMapper.selectAndMatch(tareReceived.getActualFirstNo(), railwayScale.getPredictionNo());
                        tareReceived.setWeightType("1");
                        tareReceived.setUpdateManNo(userId);
                        tareReceived.setUpdateManName(userName);
                        tareReceived.setUpdateTime(new Date());
                        meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(tareReceived);
                        meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(tareReceived.getActualFirstNo());
                        //从表赋值grossModel、
                        BeanUtils.copyProperties(grossReceived, grossModel);
                        grossModel.setLcNo(grossReceived.getRailwayWorkNo());
                        meterWorkRailwayActFirstMapper.insertSelective(grossModel);
                        BeanUtils.copyProperties(tareReceived, tareModel);
                        tareModel.setLcNo(tareReceived.getRailwayWorkNo());
                        meterWorkRailwayActFirstMapper.insertSelective(tareModel);

                        //修改主表是否已处理完
                        MeterWorkRailwayReceived receivedModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(grossReceived.getActualFirstNo());
                        if(receivedModel != null){
                            HashMap<String, Object> obj = new HashMap<>();
                            obj.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
                            HashMap<String, Object> countMap = meterWorkRailwayReceivedMapper.getCount(obj);
                            int countNum = Integer.parseInt(countMap.get("COUNTNUM").toString());
                            if(countNum < 1) {
                                HashMap<String, Object> objMap = new HashMap<>();
                                objMap.put("soundName", receivedModel.getRailwayWorkNo());
                                List<MeterWorkRailwayRecMain> recMains = meterWorkRailwayRecMainMapper.selectByParameters(objMap);
                                if(recMains.size() >= 1) {
                                    recMains.get(0).setIsDispose("1");
                                    meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMains.get(0));
                                }
                            }
                        }

                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setWeightType("0");
                        grossModel.setNote("从表空皮匹配委托结净更新");
                        grossModel.setIsPreSacale("1");
                        grossModel.setPredictionNo(railwayScale.getPredictionNo());
                        grossModel.setPredictionType("1");
                        grossModel.setUpdateManNo(userId);
                        grossModel.setUpdateManName(userName);
                        grossModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                        tareModel.setValueFlag(DbConstants.NET);
                        tareModel.setWeightType("1");
                        tareModel.setNote("从表空皮匹配委托结净更新");
                        tareModel.setIsPreSacale("1");
                        tareModel.setPredictionNo(railwayScale.getPredictionNo());
                        tareModel.setPredictionType("1");
                        tareModel.setUpdateManNo(userId);
                        tareModel.setUpdateManName(userName);
                        tareModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);

                        //保存历史皮重
                        doSaveTare(tareModel);

                        //添加第二次毛重数据
                        SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyyMMdd");
                        String afl = sdfFormat.format(new Date());
                        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), railwayScale.getPredictionNo());
                        BeanUtils.copyProperties(mwca, actualRow);

                        //添加结净数据
//                    actualRow.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.setActualNo(no);
                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);
                        actualRow.setTareWeight(tareModel.getMeterWeight());
                        actualRow.setNetTime(new Date());
                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setNote("从表空皮配委托结净更新");
                        actualRow.setNetMode("2");
                        actualRow.setUploadFlag("1");
                        actualRow.setIsPreSacale("1");
                        actualRow.setValueFlag("2");
                        actualRow.setPredictionType("1");
                        actualRow.setPredictionNo(railwayScale.getPredictionNo());
                        //添加车型
                        actualRow.setConveyanceType(receivedModel.getRailwayModel());
                        actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                        }else {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                            actualRow.setNetManNo(actualRow.getTareManNo());
                            actualRow.setNetManName(actualRow.getTareManName());
                            actualRow.setNetTime(actualRow.getTareTime());
                            actualRow.setCreateTime(actualRow.getTareTime());
                            actualRow.setCreateManName(actualRow.getTareManName());
                        }
                        //铁水衡自动导入情况
                        if(actualRow.getTareManNo() != null && actualRow.getTareManNo().equals("system") &&
                                actualRow.getGrossManNo() != null && actualRow.getGrossManNo().equals("system")) {
                            if(userId != null && StringUtils.isNotEmpty(userId) && userName != null &&StringUtils.isNotEmpty(userName)) {
                                actualRow.setNetManNo(userId);
                                actualRow.setNetManName(userName);
                            }
                        }
                        actualRow.setSampleNo(tareModel.getLcNo());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                        actualRow.setUpdateManNo(userId);
                        actualRow.setUpdateManName(userName);
                        actualRow.setUpdateTime(new Date());
                        actualRow.setMemo("从表回空皮结净");
                        meterWorkRailwayActualMapper.insertSelective(actualRow);

                        //更新预报数
                        railwayScale.setValueFlag(DbConstants.NET);
                        railwayScale.setUploadFlag("1");
                        railwayScale.setValueFlag("2");
                        railwayScale.setPredictionType("1");
                        railwayScale.setMeasureStation(actualRow.getNetSpot3No());
                        railwayScale.setMeasureStationName(actualRow.getNetSpot3Name());
                        railwayScale.setUpdateTime(new Date());
                        railwayScale.setUpdateManNo(userId);
                        railwayScale.setUpdateManName(userName);
                        preRailwayScaleMapper.updateByPrimaryKeySelective(railwayScale);

                        count++;
                    }else {
                        grossReceived.setWeightType("0");
                        grossReceived.setUpdateManNo(userId);
                        grossReceived.setUpdateManName(userName);
                        grossReceived.setUpdateTime(new Date());
                        meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(grossReceived);
                        //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                        meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(grossReceived.getActualFirstNo());
                        tareReceived.setWeightType("1");
                        tareReceived.setUpdateManNo(userId);
                        tareReceived.setUpdateManName(userName);
                        tareReceived.setUpdateTime(new Date());
                        meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(tareReceived);
                        meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(tareReceived.getActualFirstNo());
                        //从表赋值grossModel、
                        BeanUtils.copyProperties(grossReceived, grossModel);
                        grossModel.setLcNo(grossReceived.getRailwayWorkNo());
                        meterWorkRailwayActFirstMapper.insertSelective(grossModel);
                        BeanUtils.copyProperties(tareReceived, tareModel);
                        tareModel.setLcNo(tareReceived.getRailwayWorkNo());
                        meterWorkRailwayActFirstMapper.insertSelective(tareModel);

                        //修改主表是否已处理完
                        MeterWorkRailwayReceived receivedModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(grossReceived.getActualFirstNo());
                        if(receivedModel != null){
                            HashMap<String, Object> obj = new HashMap<>();
                            obj.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
                            HashMap<String, Object> countMap = meterWorkRailwayReceivedMapper.getCount(obj);
                            int countNum = Integer.parseInt(countMap.get("COUNTNUM").toString());
                            if(countNum < 1) {
                                HashMap<String, Object> objMap = new HashMap<>();
                                objMap.put("soundName", receivedModel.getRailwayWorkNo());
                                List<MeterWorkRailwayRecMain> recMains = meterWorkRailwayRecMainMapper.selectByParameters(objMap);
                                if(recMains.size() >= 1) {
                                    recMains.get(0).setIsDispose("1");
                                    meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMains.get(0));
                                }
                            }
                        }

                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setWeightType("0");
                        grossModel.setNote("从表空皮无委托结净更新");
                        grossModel.setIsPreSacale("0");
                        grossModel.setUpdateManNo(userId);
                        grossModel.setUpdateManName(userName);
                        grossModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                        tareModel.setValueFlag(DbConstants.NET);
                        tareModel.setWeightType("1");
                        tareModel.setNote("从表空皮无委托结净更新");
                        tareModel.setIsPreSacale("0");
                        tareModel.setUpdateManNo(userId);
                        tareModel.setUpdateManName(userName);
                        tareModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);

                        //保存历史皮重
                        doSaveTare(tareModel);

                        //添加第二次毛重数据
                        SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyyMMdd");
                        String afl = sdfFormat.format(new Date());
                        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                        BeanUtils.copyProperties(mwca, actualRow);

                        //添加结净数据
//                    actualRow.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.setActualNo(no);
                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);
                        actualRow.setTareWeight(tareModel.getMeterWeight());
                        actualRow.setNetTime(new Date());
                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setNote("从表空皮无委托结净更新");
                        actualRow.setNetMode("2");
                        actualRow.setUploadFlag("1");
                        actualRow.setIsPreSacale("0");
                        actualRow.setValueFlag("1");
                        //添加车型
                        actualRow.setConveyanceType(receivedModel.getRailwayModel());
                        actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                        }else {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                            actualRow.setNetManNo(actualRow.getTareManNo());
                            actualRow.setNetManName(actualRow.getTareManName());
                            actualRow.setNetTime(actualRow.getTareTime());
                            actualRow.setCreateTime(actualRow.getTareTime());
                            actualRow.setCreateManName(actualRow.getTareManName());
                        }
                        //铁水衡自动导入情况
                        if(actualRow.getTareManNo() != null && actualRow.getTareManNo().equals("system") &&
                                actualRow.getGrossManNo() != null && actualRow.getGrossManNo().equals("system")) {
                            if(userId != null && StringUtils.isNotEmpty(userId) && userName != null &&StringUtils.isNotEmpty(userName)) {
                                actualRow.setNetManNo(userId);
                                actualRow.setNetManName(userName);
                            }
                        }
                        actualRow.setSampleNo(tareModel.getLcNo());
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                        actualRow.setUpdateManNo(userId);
                        actualRow.setUpdateManName(userName);
                        actualRow.setUpdateTime(new Date());
                        actualRow.setMemo("从表回空皮结净");
                        meterWorkRailwayActualMapper.insertSelective(actualRow);

                        count++;
                    }
                    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(grossModel.getActualFirstNo() + "," + tareModel.getActualFirstNo());
                    logModel.setOperationPredictionNo(grossModel.getPredictionNo());
                    logModel.setOperationCarnNo(grossModel.getRailwayNo());
                    logModel.setOperationBeforeFix(grossReceived.toString());
                    logModel.setOperationAfterFix(grossModel.toString());
                    meterBaseOperationLogMapper.insertSelective(logModel);
                }else {
                    carList.add(received.getRailwayNo());
                }
            }

            //修改主表是否已处理完
            if(receivedList.size() >= 1) {
                MeterWorkRailwayReceived receivedModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(receivedList.get(0).getActualFirstNo());
                if(receivedModel != null){
                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
                    HashMap<String, Object> countMap = meterWorkRailwayReceivedMapper.getCount(obj);
                    int countNum = Integer.parseInt(countMap.get("COUNTNUM").toString());
                    if(countNum < 1) {
                        HashMap<String, Object> objMap = new HashMap<>();
                        objMap.put("soundName", receivedModel.getRailwayWorkNo());
                        List<MeterWorkRailwayRecMain> recMains = meterWorkRailwayRecMainMapper.selectByParameters(objMap);
                        if(recMains.size() >= 1) {
                            recMains.get(0).setIsDispose("1");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMains.get(0));
                        }
                    }
                }
            }
            if(carList.size() >= 1) {
                strCar = carList.get(0);
                if(carList.size() >= 2) {
                    for(String car: carList) {
                        strCar = strCar + "," + car;
                    }
                }
            }
            rm.setSucceed();
            rm.setCode("200");
//            rm.setMessage("操作成功,总共"+ receivedList.size() +"条数据，共有：" + count + "条数据匹配空皮成功");
            rm.setMessage(strCar);
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常：>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult DynamicBatchMatchHisTareWeight(List<MeterWorkRailwayReceived> receivedList) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            List<String> carList = new ArrayList<>();
            String strCar = "";
            //判断从表数据中是否包含委托编号
//            for (MeterWorkRailwayReceived receivedModel: receivedList) {
//                Assert.isTrue(receivedModel.getPredictionNo() != null && StringUtils.isNotEmpty(receivedModel.getPredictionNo()), "所选择的数据中包含没有委托的数据，不能结净");
//            }
            int count = 0;
            //将从表数据的车号所对应有效的AI标识皮进行结净,未找到Ai标识皮就跳过
            for (MeterWorkRailwayReceived receivedModel: receivedList) {
                String userId = receivedModel.getUpdateManNo();
                String userName = receivedModel.getUpdateManName();
                HashMap obj = new HashMap();
                obj.put("railwayNo", receivedModel.getRailwayNo());
                obj.put("valueFlag", "0");
                List<MeterBaseRailwayHisTareData> hisTareWeightList = meterBaseRailwayHisTareDataMapper.selectByParameters(obj);
                if(hisTareWeightList.size() >= 1) {
                    MeterBaseRailwayHisTareData hisTareWeightModel = hisTareWeightList.get(0);

                    MeterWorkRailwayActFirst grossModel = new MeterWorkRailwayActFirst();
                    MeterWorkRailwayActFirst tareModel = new MeterWorkRailwayActFirst();
                    MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();
                    PreRailwayScale railwayScale = preRailwayScaleMapper.selectByPrimaryKey(receivedModel.getPredictionNo());

                    if(railwayScale != null) {
                        meterWorkRailwayReceivedMapper.selectAndMatch(receivedModel.getActualFirstNo(), railwayScale.getPredictionNo());
                        receivedModel.setWeightType("0");
                        receivedModel.setUpdateManNo(userId);
                        receivedModel.setUpdateManName(userName);
                        receivedModel.setUpdateTime(new Date());
                        meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(receivedModel);
                        //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                        meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(receivedModel.getActualFirstNo());

                        //更新主表状态METER_WORK_RAILWAY_REC_MAIN
//                    if(receivedModel.getRailwayWorkNo() != null && StringUtils.isNotEmpty(receivedModel.getRailwayWorkNo())) {
//                        HashMap<String, Object> objRec = new HashMap<>();
//                        objRec.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
//                        objRec.put("valueFlag", "1");
//                        List<MeterWorkRailwayReceived> receiveds = meterWorkRailwayReceivedMapper.selectByParameters(objRec);
//                        if(receiveds.size() < 1) {
//                            HashMap<String, Object> objMap = new HashMap<>();
//                            objMap.put("soundName", receivedModel.getRailwayWorkNo());
//                            List<MeterWorkRailwayRecMain> recMains = meterWorkRailwayRecMainMapper.selectByParameters(objMap);
//                            if(recMains.size() >= 1) {
//                                recMains.get(0).setIsDispose("1");
//                                meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMains.get(0));
//                            }
//                        }
//                    }

                        //从表赋值grossModel、历史皮也赋值tareModel并修改重量
                        BeanUtils.copyProperties(receivedModel, grossModel);
                        grossModel.setLcNo(receivedModel.getRailwayWorkNo());
                        meterWorkRailwayActFirstMapper.insertSelective(grossModel);
                        BeanUtils.copyProperties(grossModel, tareModel);
                        tareModel.setMeterWeight(new BigDecimal(hisTareWeightModel.getMeterWeight()));

                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setWeightType("0");
                        grossModel.setNote("历史皮匹配委托结净更新");
                        grossModel.setIsPreSacale("1");
                        grossModel.setPredictionNo(railwayScale.getPredictionNo());
                        grossModel.setPredictionType("1");
                        grossModel.setUpdateManNo(userId);
                        grossModel.setUpdateManName(userName);
                        grossModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                        //添加第二次毛重数据
                        SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyyMMdd");
                        String afl = sdfFormat.format(new Date());
                        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), railwayScale.getPredictionNo());
                        BeanUtils.copyProperties(mwca, actualRow);

                        //添加结净数据
//                    actualRow.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.setActualNo(no);
                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);
                        actualRow.setTareWeight(tareModel.getMeterWeight());
                        actualRow.setNetTime(new Date());
                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setNote("历史皮匹配委托结净更新");
                        actualRow.setNetMode("2");
                        actualRow.setUploadFlag("1");
                        actualRow.setIsPreSacale("1");
                        actualRow.setValueFlag("2");
                        actualRow.setPredictionType("1");
                        actualRow.setPredictionNo(railwayScale.getPredictionNo());
                        //添加车型
                        actualRow.setConveyanceType(receivedModel.getRailwayModel());
                        actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                        }else {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                            actualRow.setNetManNo(actualRow.getTareManNo());
                            actualRow.setNetManName(actualRow.getTareManName());
                            actualRow.setNetTime(actualRow.getTareTime());
                            actualRow.setCreateTime(actualRow.getTareTime());
                            actualRow.setCreateManName(actualRow.getTareManName());
                        }
                        actualRow.setUpdateManNo(userId);
                        actualRow.setUpdateManName(userName);
                        actualRow.setUpdateTime(new Date());
                        actualRow.setNetManNo(userId);
                        actualRow.setNetManName(userName);
                        actualRow.setNetTime(new Date());
                        actualRow.setMemo("历史皮结净");
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                        meterWorkRailwayActualMapper.insertSelective(actualRow);

                        //更新预报数
                        railwayScale.setValueFlag(DbConstants.NET);
                        railwayScale.setUploadFlag("1");
                        railwayScale.setValueFlag("2");
                        railwayScale.setPredictionType("1");
                        railwayScale.setMeasureStation(actualRow.getNetSpot3No());
                        railwayScale.setMeasureStationName(actualRow.getNetSpot3Name());
                        railwayScale.setUpdateTime(new Date());
                        railwayScale.setUpdateManNo(userId);
                        railwayScale.setUpdateManName(userName);
                        preRailwayScaleMapper.updateByPrimaryKeySelective(railwayScale);

                        count++;
                    }else {
                        receivedModel.setWeightType("0");
                        receivedModel.setUpdateManNo(userId);
                        receivedModel.setUpdateManName(userName);
                        receivedModel.setUpdateTime(new Date());
                        meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(receivedModel);
                        //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                        meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(receivedModel.getActualFirstNo());

                        //从表赋值grossModel、历史皮也赋值tareModel并修改重量
                        BeanUtils.copyProperties(receivedModel, grossModel);
                        grossModel.setLcNo(receivedModel.getRailwayWorkNo());
                        meterWorkRailwayActFirstMapper.insertSelective(grossModel);
                        BeanUtils.copyProperties(grossModel, tareModel);
                        tareModel.setMeterWeight(new BigDecimal(hisTareWeightModel.getMeterWeight()));

                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setWeightType("0");
                        grossModel.setNote("历史皮无委托结净更新");
                        grossModel.setIsPreSacale("0");
                        grossModel.setUpdateManNo(userId);
                        grossModel.setUpdateManName(userName);
                        grossModel.setUpdateTime(new Date());
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                        //添加第二次毛重数据
                        SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyyMMdd");
                        String afl = sdfFormat.format(new Date());
                        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                        BeanUtils.copyProperties(mwca, actualRow);

                        //添加结净数据
//                    actualRow.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.setActualNo(no);
                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);
                        actualRow.setTareWeight(tareModel.getMeterWeight());
                        actualRow.setNetTime(new Date());
                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setNote("历史皮无委托结净更新");
                        actualRow.setNetMode("2");
                        actualRow.setUploadFlag("1");
                        actualRow.setIsPreSacale("0");
                        actualRow.setValueFlag("1");
                        //添加车型
                        actualRow.setConveyanceType(receivedModel.getRailwayModel());
                        actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                        }else {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
                            actualRow.setNetManNo(actualRow.getTareManNo());
                            actualRow.setNetManName(actualRow.getTareManName());
                            actualRow.setNetTime(actualRow.getTareTime());
                            actualRow.setCreateTime(actualRow.getTareTime());
                            actualRow.setCreateManName(actualRow.getTareManName());
                        }
                        actualRow.setUpdateManNo(userId);
                        actualRow.setUpdateManName(userName);
                        actualRow.setUpdateTime(new Date());
                        actualRow.setNetManNo(userId);
                        actualRow.setNetManName(userName);
                        actualRow.setNetTime(new Date());
                        actualRow.setMemo("历史皮结净");
                        actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                        meterWorkRailwayActualMapper.insertSelective(actualRow);

                        count++;
                    }
                    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(grossModel.getActualFirstNo());
                    logModel.setOperationPredictionNo(grossModel.getPredictionNo());
                    logModel.setOperationCarnNo(grossModel.getRailwayNo());
                    logModel.setOperationBeforeFix(receivedModel.toString());
                    logModel.setOperationAfterFix(grossModel.toString());
                    meterBaseOperationLogMapper.insertSelective(logModel);
                }else {
                    carList.add(receivedModel.getRailwayNo());
                }
            }
            //修改主表是否已处理完
            if(receivedList.size() >= 1) {
                MeterWorkRailwayReceived receivedModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(receivedList.get(0).getActualFirstNo());
                if(receivedModel != null){
                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
                    HashMap<String, Object> countMap = meterWorkRailwayReceivedMapper.getCount(obj);
                    int countNum = Integer.parseInt(countMap.get("COUNTNUM").toString());
                    if(countNum < 1) {
                        HashMap<String, Object> objMap = new HashMap<>();
                        objMap.put("soundName", receivedModel.getRailwayWorkNo());
                        List<MeterWorkRailwayRecMain> recMains = meterWorkRailwayRecMainMapper.selectByParameters(objMap);
                        if(recMains.size() >= 1) {
                            recMains.get(0).setIsDispose("1");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMains.get(0));
                        }
                    }
                }
            }
            if(carList.size() >= 1) {
                strCar = carList.get(0);
                if(carList.size() >= 2) {
                    for(String car: carList) {
                        strCar = strCar + "," + car;
                    }
                }
            }
            rm.setSucceed();
            rm.setCode("200");
//            rm.setMessage("操作成功,总共"+ receivedList.size() +"条数据，共有：" + count + "条数据匹配历史皮成功");
            rm.setMessage(strCar);
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常：>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult WebBatchMatchidentifyWeight(List<HashMap> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            String userId = "";
            String userName = "";
            try{
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch (Exception e) {
                userId = "system";
                userName = "system";
            }
            int count = 0;
            //将一次数据表的与标识皮进行结净
            for (HashMap model: models) {
                MeterWorkRailwayActFirst firstModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(model.get("ACTUAL_FIRST_NO") == null ? "": model.get("ACTUAL_FIRST_NO").toString());
                BigDecimal identifyWeight = new BigDecimal(0);
                String scaleNo = null;
                if(model.get("IDENTIFY_WEIGHT") != null && StringUtils.isNotEmpty(model.get("IDENTIFY_WEIGHT").toString())) {
                    identifyWeight = new BigDecimal(model.get("IDENTIFY_WEIGHT").toString()).multiply(new BigDecimal(1000));
                }
                if(model.get("PREDICTION_NO") != null && StringUtils.isNotEmpty(model.get("PREDICTION_NO").toString())) {
                    scaleNo = model.get("PREDICTION_NO").toString();
                }
                if(firstModel != null && identifyWeight.compareTo(new BigDecimal(0)) > 0) {

                    MeterWorkRailwayActFirst grossModel = new MeterWorkRailwayActFirst();
                    MeterWorkRailwayActFirst tareModel = new MeterWorkRailwayActFirst();
                    MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();
                    if(scaleNo != null && StringUtils.isNotEmpty(scaleNo)) {
                        PreRailwayScale railwayScale = preRailwayScaleMapper.selectByPrimaryKey(scaleNo);

                        //从表赋值grossModel、标识皮也赋值tareModel并修改重量
                        BeanUtils.copyProperties(firstModel, grossModel);
                        BeanUtils.copyProperties(grossModel, tareModel);
                        tareModel.setMeterWeight(identifyWeight);

                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setWeightType("0");
                        grossModel.setNote("标识皮匹配委托结净更新");
                        grossModel.setUpdateTime(new Date());
                        grossModel.setIsPreSacale("1");
                        grossModel.setPredictionNo(railwayScale.getPredictionNo());
                        grossModel.setPredictionType("1");
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                        //添加第二次毛重数据
                        SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyyMMdd");
                        String afl = sdfFormat.format(new Date());
                        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), railwayScale.getPredictionNo());
                        BeanUtils.copyProperties(mwca, actualRow);

                        //添加结净数据
//                        actualRow.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.setActualNo(no);
                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);
                        actualRow.setTareWeight(tareModel.getMeterWeight());
                        actualRow.setNetTime(new Date());
                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setNote("标识皮匹配委托结净更新");
                        actualRow.setMemo("标识皮结净");
                        actualRow.setNetMode("2");
                        actualRow.setUploadFlag("1");
                        actualRow.setIsPreSacale("1");
                        actualRow.setValueFlag("1");
                        actualRow.setPredictionType("1");
                        actualRow.setPredictionNo(railwayScale.getPredictionNo());
                        actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                            actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                        }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                        }
                        actualRow.setUpdateManNo(userId);
                        actualRow.setUpdateManName(userName);
                        actualRow.setUpdateTime(new Date());
                        actualRow.setNote("一次批量匹配标识皮结净");
                        meterWorkRailwayActualMapper.insertSelective(actualRow);

                        //更新预报数
                        railwayScale.setValueFlag(DbConstants.NET);
                        railwayScale.setUpdateTime(new Date());
                        railwayScale.setUpdateManNo("system");
                        railwayScale.setUpdateManName("标识皮匹配委托结净更新");
                        railwayScale.setUploadFlag("1");
                        railwayScale.setValueFlag("2");
                        railwayScale.setPredictionType("1");
                        railwayScale.setMeasureStation(actualRow.getNetSpot3No());
                        railwayScale.setMeasureStationName(actualRow.getNetSpot3Name());
                        preRailwayScaleMapper.updateByPrimaryKeySelective(railwayScale);
                    }else {
                        //从表赋值grossModel、标识皮也赋值tareModel并修改重量
                        BeanUtils.copyProperties(firstModel, grossModel);
                        BeanUtils.copyProperties(grossModel, tareModel);
                        tareModel.setMeterWeight(identifyWeight);

                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setWeightType("0");
                        grossModel.setNote("标识皮无委托结净更新");
                        grossModel.setUpdateTime(new Date());
                        grossModel.setIsPreSacale("0");
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                        //添加第二次毛重数据
                        SimpleDateFormat sdfFormat = new SimpleDateFormat("yyyyMMdd");
                        String afl = sdfFormat.format(new Date());
                        MeterWorkRailwayActual mwca = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                        BeanUtils.copyProperties(mwca, actualRow);

                        //添加结净数据
//                        actualRow.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.setActualNo(no);
                        String no = PublicMethod.IdCreate(3);
                        actualRow.setActualNo(no);
                        actualRow.setTareWeight(tareModel.getMeterWeight());
                        actualRow.setNetTime(new Date());
                        actualRow.setNetMode(DbConstants.TYPE);
                        actualRow.setNote("标识皮无委托结净更新");
                        actualRow.setMemo("标识皮结净");
                        actualRow.setNetMode("2");
                        actualRow.setUploadFlag("1");
                        actualRow.setIsPreSacale("0");
                        actualRow.setValueFlag("1");
                        actualRow.setNetWeight(grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()));
                        if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                            actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                            actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
                            actualRow.setNetManNo(actualRow.getGrossManNo());
                            actualRow.setNetManName(actualRow.getGrossManName());
                            actualRow.setNetTime(actualRow.getGrossTime());
                            actualRow.setCreateTime(actualRow.getGrossTime());
                            actualRow.setCreateManName(actualRow.getGrossManName());
                            actualRow.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst1No()));
                        }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.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualRow.getActualFirst2No()));
                        }
                        actualRow.setUpdateManNo(userId);
                        actualRow.setUpdateManName(userName);
                        actualRow.setUpdateTime(new Date());
                        actualRow.setNote("一次批量匹配标识皮结净");
                        meterWorkRailwayActualMapper.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(grossModel.getActualFirstNo());
                    logModel.setOperationPredictionNo(grossModel.getPredictionNo());
                    logModel.setOperationCarnNo(grossModel.getRailwayNo());
                    meterBaseOperationLogMapper.insertSelective(logModel);
                    count++;
                }
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功,总共"+ models.size() +"条数据，共有：" + count + "条数据匹配标识皮成功");
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常：>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult saveAutoScale(CommonCombination<MeterWorkRailwayActFirst, MeterWorkRailwayActFirst, MeterWorkRailwayActual, MeterWorkRailwayScheduledHelp> com) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

            List<MeterWorkRailwayActFirst> firsts = new ArrayList<>();
            List<MeterWorkRailwayActual> actuals = new ArrayList<>();
            HashMap<String, Object> map = new HashMap<>();

//            com.getOne().setActualFirstNo(sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(sdf.format(new Date())));
            String no = PublicMethod.IdCreate(3);
            com.getOne().setActualFirstNo(no);
            meterWorkRailwayActFirstMapper.insertSelective(com.getOne());
            firsts.add(com.getOne());
            if(com.getTwo() != null) {
                int i = meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(com.getTwo());
                if( i  == 1) {
                    firsts.add(com.getTwo());
                }
            }
            if(com.getThree() != null) {
                if(com.getTwo() != null) {
                    if(com.getOne().getMeterWeight().compareTo(com.getTwo().getMeterWeight()) > 0) {
                        com.getThree().setActualFirst1No(com.getOne().getActualFirstNo());
                    }else {
                        com.getThree().setActualFirst2No(com.getOne().getActualFirstNo());
                    }
                }
//                com.getThree().setActualNo(sdf.format(new Date()) + meterWorkRailwayActualMapper.getNewID(sdf.format(new Date())));
                com.getThree().setActualNo(PublicMethod.IdCreate(3));
                if(com.getThree().getGrossTime().compareTo(com.getThree().getTareTime()) >= 0) {
                    com.getThree().setNetSpot3No(com.getThree().getBaseSpot1No());
                    com.getThree().setNetSpot3Name(com.getThree().getBaseSpot1Name());
                    com.getThree().setNetManNo(com.getThree().getGrossManNo());
                    com.getThree().setNetManName(com.getThree().getGrossManName());
                    com.getThree().setNetTime(com.getThree().getGrossTime());
                    com.getThree().setCreateTime(com.getThree().getGrossTime());
                    com.getThree().setCreateManName(com.getThree().getGrossManName());
                    com.getThree().setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(com.getThree().getActualFirst1No()));
                }else {
                    com.getThree().setNetSpot3No(com.getThree().getBaseSpot2No());
                    com.getThree().setNetSpot3Name(com.getThree().getBaseSpot2Name());
                    com.getThree().setNetManNo(com.getThree().getTareManNo());
                    com.getThree().setNetManName(com.getThree().getTareManName());
                    com.getThree().setNetTime(com.getThree().getTareTime());
                    com.getThree().setCreateTime(com.getThree().getTareTime());
                    com.getThree().setCreateManName(com.getThree().getTareManName());
                    com.getThree().setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(com.getThree().getActualFirst2No()));
                }
                int i = meterWorkRailwayActualMapper.insertSelective(com.getThree());
                MeterWorkRailwayActFirst tareModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(com.getThree().getActualFirst2No());
                doSaveTare(tareModel);
                //如果有委托号就更新委托状态，
                if( i  == 1) {
                    actuals.add(com.getThree());
                    if(com.getThree().getPredictionNo() != null && StringUtils.isNotEmpty(com.getThree().getPredictionNo())) {
                        PreRailwayScale scaleModel = preRailwayScaleMapper.selectByPrimaryKey(com.getThree().getPredictionNo());
                        scaleModel.setValueFlag("2");
                        scaleModel.setUpdateManNo("Systems");
                        scaleModel.setUpdateManName("自助计量结净更新");
                        scaleModel.setUpdateTime(new Date());
                        scaleModel.setUploadFlag("1");
                        scaleModel.setPredictionType("1");
                        scaleModel.setMeasureStation(com.getThree().getNetSpot3No());
                        scaleModel.setMeasureStationName(com.getThree().getNetSpot3Name());
                        preRailwayScaleMapper.updateByPrimaryKeySelective(scaleModel);
                    }
                }
            }else {
                if(com.getOne().getPredictionNo() != null && StringUtils.isNotEmpty(com.getOne().getPredictionNo())) {
                    PreRailwayScale scaleModel = preRailwayScaleMapper.selectByPrimaryKey(com.getOne().getPredictionNo());
                    scaleModel.setValueFlag("4");
                    scaleModel.setUpdateManNo("Systems");
                    scaleModel.setUpdateManName("自助计量结净更新");
                    scaleModel.setUpdateTime(new Date());
                    scaleModel.setUploadFlag("1");
                    scaleModel.setPredictionType("1");
                    scaleModel.setMeasureStation(com.getOne().getBaseSpotNo());
                    scaleModel.setMeasureStationName(com.getOne().getBaseSpotName());
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scaleModel);
                }
            }

            if(com.getFour() != null) {
                com.getFour().setHelpId(sdf.format(new Date()) + meterWorkRailwayScheduledHelpMapper.getNewID(sdf.format(new Date())));
                meterWorkRailwayScheduledHelpMapper.insertSelective(com.getFour());
            }

            map.put("firsts", firsts);
            map.put("actuals", actuals);
            rm.setSucceed();
            rm.setData(map);
            rm.setMessage("操作成功");
        }catch (MarkerMetException e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常：>>>" + e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常：>>>" + e.getMessage());
        }
        return rm;
    }

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

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

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

//            String no = PublicMethod.IdCreate(3);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String afl = formatter.format(new Date());
//            String no = afl + meterWorkRailwayActualMapper.getNewID(afl);
//            actualModel.setActualNo(no);
            String no = PublicMethod.IdCreate(3);
            actualModel.setActualNo(no);
            actualModel.setNetTime(new Date());
            actualModel.setNetMode("1");
            actualModel.setNote("紧急结净保存");
            actualModel.setUploadFlag("1");
            actualModel.setIsPreSacale("0");
            actualModel.setValueFlag("1");
            actualModel.setNetWeight(meterWeight);
            actualModel.setCreateTime(new Date());
            actualModel.setRailwayTypeNo("001019001");
            actualModel.setRailwayTypeName("静态轨道衡");
            if(actualModel.getGrossTime().compareTo(actualModel.getTareTime()) >= 0) {
                actualModel.setNetSpot3No(actualModel.getBaseSpot1No());
                actualModel.setNetSpot3Name(actualModel.getBaseSpot1Name());
                actualModel.setNetManNo(actualModel.getGrossManNo());
                actualModel.setNetManName(actualModel.getGrossManName());
                actualModel.setNetTime(actualModel.getGrossTime());
                actualModel.setCreateTime(actualModel.getGrossTime());
                actualModel.setCreateManName(actualModel.getGrossManName());
                actualModel.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualModel.getActualFirst1No()));
            }else {
                actualModel.setNetSpot3No(actualModel.getBaseSpot2No());
                actualModel.setNetSpot3Name(actualModel.getBaseSpot2Name());
                actualModel.setNetManNo(actualModel.getTareManNo());
                actualModel.setNetManName(actualModel.getTareManName());
                actualModel.setNetTime(actualModel.getTareTime());
                actualModel.setCreateTime(actualModel.getTareTime());
                actualModel.setCreateManName(actualModel.getTareManName());
                actualModel.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualModel.getActualFirst2No()));
            }
            meterWorkRailwayActualMapper.insertSelective(actualModel);

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

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

        try{
            String userId = "";
            String userName = "";
            try{
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch (Exception e) {
                userId = "system";
                userName = "system";
            }

            if(params.get("actualFirstNo") != null && com.steerinfo.util.StringUtils.isNotEmpty(params.get("actualFirstNo").toString())){
                MeterWorkRailwayActFirst model = meterWorkRailwayActFirstMapper.selectByPrimaryKey(params.get("actualFirstNo").toString());
                if(model != null) {
                    if(model.getValueFlag().equals("2")){
                        rm.setMessage("毛皮转换失败：数据已结净，请确认！！");
                        return rm;
                    }
                    if(model.getValueFlag().equals("1")) {
                        if(model.getWeightType() == null || StringUtils.isEmpty(model.getWeightType())) {
//                            rm.setMessage("毛皮转换失败：重量类型为空，请确认！！");
//                            return rm;
                            model.setUpdateManNo(userId);
                            model.setUpdateManName(userName);
                            model.setUpdateTime(new Date());
                            model.setNote("毛皮转换");
                            model.setWeightType("1");
                            meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model);

                            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.getActualFirstNo());
                            logModel.setOperationPredictionNo(model.getPredictionNo());
                            logModel.setOperationCarnNo(model.getRailwayNo());
                            rm.setSucceed();
                            rm.setMessage("重量类型为空，毛皮转换成皮重!!");
                            rm.setData(model);
                            rm.setCode("200");
                            return rm;
                        }else {
                            if(model.getWeightType().equals("0")){
                                model.setWeightType("1");
                            }else if (model.getWeightType().equals("1")){
                                model.setWeightType("0");
                            }else{
                                rm.setMessage("毛皮转换失败：毛皮转换只针对毛重和常规皮重，请确认！！");
                                return rm;
                            }
                            model.setUpdateManNo(userId);
                            model.setUpdateManName(userName);
                            model.setUpdateTime(new Date());
                            model.setNote("毛皮转换");
                            meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model);

                            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.getActualFirstNo());
                            logModel.setOperationPredictionNo(model.getPredictionNo());
                            logModel.setOperationCarnNo(model.getRailwayNo());
                            rm.setSucceed();
                            rm.setMessage("毛皮转换成功!!");
                            rm.setData(model);
                            rm.setCode("200");
                            return rm;
                        }
                    }else{
                        rm.setMessage("毛皮转换失败：毛皮转换只针对有效的毛重和常规皮重，请确认！！");
                    }
                }else {
                    rm.setMessage("未找到相应的一次计量数据，不能毛皮转换！！");
                }
            }else {
                rm.setMessage("实绩编号不能为空！！");
            }
            return rm;
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！>>>" + e.getMessage());
            return rm;
        }
    }

    @Override
    public synchronized RESTfulResult batchRailwayFirst(List<HashMap> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            UserPayload userPayload = UserPayload.getCurrUser();
            String userId = userPayload.getUserCode();
            String userName = userPayload.getUserName();
            int count = 0;

            for (HashMap model: models) {
                String grossNo = model.get("GROSS_NO") == null ? "": model.get("GROSS_NO").toString();
                String tareNo = model.get("TARE_NO") == null ? "": model.get("TARE_NO").toString();
                BigDecimal tareWeight = model.get("RAILWAY_FACTORY_WEIGHT") == null ? new BigDecimal("0"): new BigDecimal(model.get("RAILWAY_FACTORY_WEIGHT").toString()).multiply(new BigDecimal(1000));
                BigDecimal tareWeight2 = model.get("CARRIAGE_WEIGHT") == null ? new BigDecimal("0"): new BigDecimal(model.get("CARRIAGE_WEIGHT").toString()).multiply(new BigDecimal(1000));
                BigDecimal tareWeight3 = model.get("TARE_WEIGHT") == null ? new BigDecimal("0"): new BigDecimal(model.get("TARE_WEIGHT").toString()).multiply(new BigDecimal(1000));
                String scaleNo = model.get("PREDICTION_NO") == null ? "": model.get("PREDICTION_NO").toString();
                Assert.isTrue(StringUtils.isNotEmpty(grossNo), "毛重计量编号不能为空");


                String typeNo = models.get(0).get("TYPE_NO") == null? "": models.get(0).get("TYPE_NO").toString();
                if(StringUtils.isEmpty(typeNo)) {
                    rm.setMessage("计量方式为空，请重新选择计量方式！！");
                    return rm;
                }
                String msg = "";
                MeterWorkRailwayActFirst meterWorkRailwayActFirst = meterWorkRailwayActFirstMapper.selectByPrimaryKey(grossNo);
                if(meterWorkRailwayActFirst==null){
                    meterWorkRailwayActFirst = meterWorkRailwayActFirstMapper.selectByPrimaryKey(tareNo);
                }
                if(meterWorkRailwayActFirst==null){
                    rm.setMessage("车号："+model.get("RAILWAY_NO")+"毛重编号无效");
                    return rm;
                }
                if(StringUtils.isNotEmpty(tareNo)) {    //毛重-回空皮
                    MeterWorkRailwayActFirst tareModel1 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(tareNo);
                    if(tareModel1==null){
                        rm.setMessage("车号："+model.get("RAILWAY_NO")+"皮重编号无效");
                        return rm;
                    }
                    doSaveTare(tareModel1);
                    msg = "回空皮结净";
                }else if(typeNo.equals("2")) {      //毛重-标识皮
                    MeterWorkRailwayActFirst tareModel1 =new MeterWorkRailwayActFirst();
                    BeanUtils.copyProperties(meterWorkRailwayActFirst, tareModel1);
                    tareModel1.setMeterWeight(tareWeight);
                    if(tareWeight.compareTo(new BigDecimal(0))==0){
                        rm.setMessage("标识皮重不能为空");
                        return rm;
                    }
                    doSaveTare(tareModel1,"2");
                    msg = "标识皮结净";
                }else if(typeNo.equals("3")) {      //毛重-标准皮
                    MeterWorkRailwayActFirst tareModel1 =new MeterWorkRailwayActFirst();
                    BeanUtils.copyProperties(meterWorkRailwayActFirst, tareModel1);
                    tareModel1.setMeterWeight(tareWeight);
                    if(tareWeight.compareTo(new BigDecimal(0))==0){
                        rm.setMessage("车型皮重不能为空");
                        return rm;
                    }
                    doSaveTare(tareModel1,"3");
                    msg = "标准皮结净";
                }else if(typeNo.equals("4")||typeNo.equals("5")||typeNo.equals("6")){       //毛重-历史皮
                    msg = "历史皮结净";
                }

                MeterWorkRailwayActFirst grossModel = new MeterWorkRailwayActFirst();
                MeterWorkRailwayActFirst tareModel = new MeterWorkRailwayActFirst();
                if(StringUtils.isNotEmpty(scaleNo)) {
                    PreRailwayScale scale = preRailwayScaleMapper.selectByPrimaryKey(scaleNo);
                    if(scale==null){
                        throw new MarkerMetException(500, "无效的委托编号");
                    }
                    if(StringUtils.isNotEmpty(tareNo)) {
                        grossModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(grossNo);
                        tareModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(tareNo);
                        MeterWorkRailwayActual actualModel = new MeterWorkRailwayActual();

                        MeterWorkRailwayActFirst grossTemp = meterWorkRailwayActFirstMapper.selectAndMatch(grossModel.getActualFirstNo(), scale.getPredictionNo());
                        BeanUtils.copyProperties(grossTemp, grossModel);
                        MeterWorkRailwayActFirst tareTemp = meterWorkRailwayActFirstMapper.selectAndMatch(tareModel.getActualFirstNo(), scale.getPredictionNo());
                        BeanUtils.copyProperties(tareTemp, tareModel);

                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setUpdateManNo(userId);
                        grossModel.setUpdateManName(userName);
                        grossModel.setUpdateTime(new Date());
                        grossModel.setWeightType("0");
                        grossModel.setIsPreSacale("1");
                        grossModel.setPredictionType("1");
                        grossModel.setNote("一次批量匹配" + msg);
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);
                        //更新第一次皮重数据库
                        tareModel.setValueFlag(DbConstants.NET);
                        tareModel.setUpdateManNo(userId);
                        tareModel.setUpdateManName(userName);
                        tareModel.setUpdateTime(new Date());
                        tareModel.setWeightType("1");
                        tareModel.setIsPreSacale("1");
                        tareModel.setPredictionType("1");
                        tareModel.setNote("一次批量匹配" + msg);
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);

                        MeterWorkRailwayActual actualTemp = meterWorkRailwayActualMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), scale.getPredictionNo());
                        BeanUtils.copyProperties(actualTemp, actualModel);
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//                        String afl = formatter.format(new Date());
//                        actualModel.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        String no = PublicMethod.IdCreate(3);
                        actualModel.setActualNo(no);
                        actualModel.setNetMode("1");
                        actualModel.setNote("一次批量匹配" + msg+","+model.get("memo"));
                        actualModel.setMemo("一次批量匹配" + msg);
                        actualModel.setUploadFlag("1");
                        actualModel.setIsPreSacale("1");
                        actualModel.setPredictionType("1");
                        actualModel.setValueFlag("1");
                        actualModel.setNetWeight(actualModel.getGrossWeight().subtract(actualModel.getTareWeight()));
                        if(actualModel.getGrossTime().compareTo(actualModel.getTareTime()) >= 0) {
                            actualModel.setNetSpot3No(actualModel.getBaseSpot1No());
                            actualModel.setNetSpot3Name(actualModel.getBaseSpot1Name());
                            actualModel.setNetManNo(userId);
                            actualModel.setNetManName(userName);
                            actualModel.setNetTime(actualModel.getGrossTime());
                            actualModel.setCreateTime(actualModel.getNetTime());
                            actualModel.setCreateManName(userName);
                            actualModel.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualModel.getActualFirst1No()));
                        }else {
                            actualModel.setNetSpot3No(actualModel.getBaseSpot2No());
                            actualModel.setNetSpot3Name(actualModel.getBaseSpot2Name());
                            actualModel.setNetManNo(userId);
                            actualModel.setNetManName(userName);
                            actualModel.setNetTime(actualModel.getTareTime());
                            actualModel.setCreateTime(actualModel.getNetTime());
                            actualModel.setCreateManName(userName);
                            actualModel.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualModel.getActualFirst2No()));
                        }
                        meterWorkRailwayActualMapper.insertSelective(actualModel);

                        if(model.get("memo")!=null&&!"".equals((String)model.get("memo"))){
                            MeterBaseOperationLog logModel2 = new MeterBaseOperationLog();
                            logModel2.setId(PublicMethod.IdCreate(2));
                            logModel2.setCreateManNo(userId);
                            logModel2.setCreateManName(userName);
                            logModel2.setCreateTime(new Date());
                            logModel2.setOperationPage("web端-轨道衡净重数据维护");
                            logModel2.setOperationFunction("web端一次批量处理结净报警");
                            logModel2.setOperationId(actualModel.getActualNo());
                            logModel2.setOperationPredictionNo(actualModel.getPredictionNo());
                            logModel2.setOperationCarnNo(actualModel.getRailwayNo());
                            logModel2.setOperationBeforeFix("");
                            logModel2.setOperationAfterFix(actualModel.toString());
                            meterBaseOperationLogMapper.insertSelective(logModel2);
                        }

                        scale.setValueFlag("2");
                        scale.setUpdateManNo(userId);
                        scale.setUpdateManName(userName);
                        scale.setUpdateTime(new Date());
                        scale.setUploadFlag("1");
                        scale.setPredictionType("1");
                        scale.setMeasureStation(actualModel.getNetSpot3No());
                        scale.setMeasureStationName(actualModel.getNetSpot3Name());
                        preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
                        count ++;
                    }else {
                        if(tareWeight.compareTo(new BigDecimal(0)) != 0 && tareWeight.compareTo(new BigDecimal(35000)) < 0) {
                            grossModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(grossNo);
                            MeterWorkRailwayActual actualModel = new MeterWorkRailwayActual();

                            MeterWorkRailwayActFirst grossTemp = meterWorkRailwayActFirstMapper.selectAndMatch(grossModel.getActualFirstNo(), scale.getPredictionNo());
                            BeanUtils.copyProperties(grossTemp, grossModel);
                            BeanUtils.copyProperties(grossModel, tareModel);

                            grossModel.setValueFlag(DbConstants.NET);
                            grossModel.setUpdateManNo(userId);
                            grossModel.setUpdateManName(userName);
                            grossModel.setUpdateTime(new Date());
                            grossModel.setWeightType("0");
                            grossModel.setIsPreSacale("1");
                            grossModel.setPredictionType("1");
                            grossModel.setNote("一次批量匹配" + msg);
                            meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                            MeterWorkRailwayActual actualTemp = meterWorkRailwayActualMapper.manualAddPre(grossModel.getActualFirstNo(), tareModel.getActualFirstNo(), scale.getPredictionNo());
                            BeanUtils.copyProperties(actualTemp, actualModel);
                            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//                            String afl = formatter.format(new Date());
//                            actualModel.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                            String no = PublicMethod.IdCreate(3);
                            actualModel.setActualNo(no);
                            actualModel.setTareWeight(tareWeight);
                            actualModel.setNetMode("1");
                            actualModel.setNote("一次批量匹配" + msg+","+model.get("memo"));
                            actualModel.setMemo("一次批量匹配" + msg);
                            actualModel.setUploadFlag("1");
                            actualModel.setIsPreSacale("1");
                            actualModel.setPredictionType("1");
                            actualModel.setValueFlag("1");
                            actualModel.setPredictionNo(scale.getPredictionNo());
                            actualModel.setNetWeight(actualModel.getGrossWeight().subtract(tareWeight));
                            if(actualModel.getGrossTime().compareTo(actualModel.getTareTime()) >= 0) {
                                actualModel.setNetSpot3No(actualModel.getBaseSpot1No());
                                actualModel.setNetSpot3Name(actualModel.getBaseSpot1Name());
                                actualModel.setNetManNo(userId);
                                actualModel.setNetManName(userName);
                                actualModel.setNetTime(new Date());
                                actualModel.setCreateTime(actualModel.getNetTime());
                                actualModel.setCreateManName(userName);
                                actualModel.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualModel.getActualFirst1No()));
                            }else {
                                actualModel.setNetSpot3No(actualModel.getBaseSpot2No());
                                actualModel.setNetSpot3Name(actualModel.getBaseSpot2Name());
                                actualModel.setNetManNo(userId);
                                actualModel.setNetManName(userName);
                                actualModel.setNetTime(new Date());
                                actualModel.setCreateTime(actualModel.getNetTime());
                                actualModel.setCreateManName(userName);
                                actualModel.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualModel.getActualFirst2No()));
                            }
                            meterWorkRailwayActualMapper.insertSelective(actualModel);

                            if(model.get("memo")!=null&&!"".equals((String)model.get("memo"))){
                                MeterBaseOperationLog logModel2 = new MeterBaseOperationLog();
                                logModel2.setId(PublicMethod.IdCreate(2));
                                logModel2.setCreateManNo(userId);
                                logModel2.setCreateManName(userName);
                                logModel2.setCreateTime(new Date());
                                logModel2.setOperationPage("web端-轨道衡净重数据维护");
                                logModel2.setOperationFunction("web端一次批量处理结净报警");
                                logModel2.setOperationId(actualModel.getActualNo());
                                logModel2.setOperationPredictionNo(actualModel.getPredictionNo());
                                logModel2.setOperationCarnNo(actualModel.getRailwayNo());
                                logModel2.setOperationBeforeFix("");
                                logModel2.setOperationAfterFix(actualModel.toString());
                                meterBaseOperationLogMapper.insertSelective(logModel2);
                            }

                            scale.setValueFlag("2");
                            scale.setUpdateManNo(userId);
                            scale.setUpdateManName(userName);
                            scale.setUpdateTime(new Date());
                            scale.setUploadFlag("1");
                            scale.setPredictionType("1");
                            scale.setMeasureStation(actualModel.getNetSpot3No());
                            scale.setMeasureStationName(actualModel.getNetSpot3Name());
                            preRailwayScaleMapper.updateByPrimaryKeySelective(scale);

                            count ++;
                        }

                    }
                }else {
                    if(StringUtils.isNotEmpty(tareNo)) {
                        grossModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(grossNo);
                        tareModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(tareNo);
                        MeterWorkRailwayActual actualModel = new MeterWorkRailwayActual();

                        grossModel.setValueFlag(DbConstants.NET);
                        grossModel.setUpdateManNo(userId);
                        grossModel.setUpdateManName(userName);
                        grossModel.setUpdateTime(new Date());
                        grossModel.setWeightType("0");
                        grossModel.setIsPreSacale("0");
                        grossModel.setNote("一次批量匹配" + msg);
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);
                        //更新第一次皮重数据库
                        tareModel.setValueFlag(DbConstants.NET);
                        tareModel.setUpdateManNo(userId);
                        tareModel.setUpdateManName(userName);
                        tareModel.setUpdateTime(new Date());
                        tareModel.setWeightType("1");
                        tareModel.setIsPreSacale("0");
                        tareModel.setNote("一次批量匹配" + msg);
                        meterWorkRailwayActFirstMapper.updateByPrimaryKey(tareModel);

                        MeterWorkRailwayActual actualTemp = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                        BeanUtils.copyProperties(actualTemp, actualModel);
                        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//                        String afl = formatter.format(new Date());
//                        actualModel.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                        String no = PublicMethod.IdCreate(3);
                        actualModel.setActualNo(no);
                        actualModel.setNetMode("1");
                        actualModel.setNote("一次批量匹配" + msg+","+model.get("memo"));
                        actualModel.setMemo("一次批量匹配" + msg);
                        actualModel.setUploadFlag("1");
                        actualModel.setIsPreSacale("0");
                        actualModel.setValueFlag("1");
                        actualModel.setNetWeight(actualModel.getGrossWeight().subtract(actualModel.getTareWeight()));
                        if(actualModel.getGrossTime().compareTo(actualModel.getTareTime()) >= 0) {
                            actualModel.setNetSpot3No(actualModel.getBaseSpot1No());
                            actualModel.setNetSpot3Name(actualModel.getBaseSpot1Name());
                            actualModel.setNetManNo(userId);
                            actualModel.setNetManName(userName);
                            actualModel.setNetTime(actualModel.getGrossTime());
                            actualModel.setCreateTime(actualModel.getGrossTime());
                            actualModel.setCreateManName(userName);
                            actualModel.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualModel.getActualFirst1No()));
                        }else {
                            actualModel.setNetSpot3No(actualModel.getBaseSpot2No());
                            actualModel.setNetSpot3Name(actualModel.getBaseSpot2Name());
                            actualModel.setNetManNo(userId);
                            actualModel.setNetManName(userName);
                            actualModel.setNetTime(actualModel.getTareTime());
                            actualModel.setCreateTime(actualModel.getTareTime());
                            actualModel.setCreateManName(userName);
                            actualModel.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualModel.getActualFirst2No()));
                        }
                        meterWorkRailwayActualMapper.insertSelective(actualModel);

                        if(model.get("memo")!=null&&!"".equals((String)model.get("memo"))){
                            MeterBaseOperationLog logModel2 = new MeterBaseOperationLog();
                            logModel2.setId(PublicMethod.IdCreate(2));
                            logModel2.setCreateManNo(userId);
                            logModel2.setCreateManName(userName);
                            logModel2.setCreateTime(new Date());
                            logModel2.setOperationPage("web端-轨道衡净重数据维护");
                            logModel2.setOperationFunction("web端一次批量处理结净报警");
                            logModel2.setOperationId(actualModel.getActualNo());
                            logModel2.setOperationPredictionNo(actualModel.getPredictionNo());
                            logModel2.setOperationCarnNo(actualModel.getRailwayNo());
                            logModel2.setOperationBeforeFix("");
                            logModel2.setOperationAfterFix(actualModel.toString());
                            meterBaseOperationLogMapper.insertSelective(logModel2);
                        }

                        count ++;
                    }else {
                        if(tareWeight.compareTo(new BigDecimal(0)) != 0 && tareWeight.compareTo(new BigDecimal(35000)) < 0) {
                            grossModel = meterWorkRailwayActFirstMapper.selectByPrimaryKey(grossNo);
                            MeterWorkRailwayActual actualModel = new MeterWorkRailwayActual();
                            BeanUtils.copyProperties(grossModel, tareModel);

                            grossModel.setValueFlag(DbConstants.NET);
                            grossModel.setUpdateManNo(userId);
                            grossModel.setUpdateManName(userName);
                            grossModel.setUpdateTime(new Date());
                            grossModel.setWeightType("0");
                            grossModel.setIsPreSacale("0");
                            grossModel.setNote("一次批量匹配" + msg);
                            meterWorkRailwayActFirstMapper.updateByPrimaryKey(grossModel);

                            MeterWorkRailwayActual actualTemp = meterWorkRailwayActualMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                            BeanUtils.copyProperties(actualTemp, actualModel);
                            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//                            String afl = formatter.format(new Date());
//                            actualModel.setActualNo(afl + meterWorkRailwayActualMapper.getNewID(afl));
                            String no = PublicMethod.IdCreate(3);
                            actualModel.setActualNo(no);
                            actualModel.setTareWeight(tareWeight);
                            actualModel.setNetMode("1");
                            actualModel.setNote("一次批量匹配" + msg+","+model.get("memo"));
                            actualModel.setMemo("一次批量匹配" + msg);
                            actualModel.setUploadFlag("1");
                            actualModel.setIsPreSacale("0");
                            actualModel.setValueFlag("1");
                            actualModel.setNetWeight(actualModel.getGrossWeight().subtract(tareWeight));
                            if(actualModel.getGrossTime().compareTo(actualModel.getTareTime()) >= 0) {
                                actualModel.setNetSpot3No(actualModel.getBaseSpot1No());
                                actualModel.setNetSpot3Name(actualModel.getBaseSpot1Name());
                                actualModel.setNetManNo(userId);
                                actualModel.setNetManName(userName);
                                actualModel.setNetTime(new Date());
                                actualModel.setCreateTime(actualModel.getGrossTime());
                                actualModel.setCreateManName(userName);
                                actualModel.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualModel.getActualFirst1No()));
                            }else {
                                actualModel.setNetSpot3No(actualModel.getBaseSpot2No());
                                actualModel.setNetSpot3Name(actualModel.getBaseSpot2Name());
                                actualModel.setNetManNo(userId);
                                actualModel.setNetManName(userName);
                                actualModel.setNetTime(new Date());
                                actualModel.setCreateTime(actualModel.getTareTime());
                                actualModel.setCreateManName(userName);
                                actualModel.setLcNo(meterWorkRailwayActFirstMapper.getFirstLcNo(actualModel.getActualFirst2No()));
                            }
                            meterWorkRailwayActualMapper.insertSelective(actualModel);

                            if(model.get("memo")!=null&&!"".equals((String)model.get("memo"))){
                                MeterBaseOperationLog logModel2 = new MeterBaseOperationLog();
                                logModel2.setId(PublicMethod.IdCreate(2));
                                logModel2.setCreateManNo(userId);
                                logModel2.setCreateManName(userName);
                                logModel2.setCreateTime(new Date());
                                logModel2.setOperationPage("web端-轨道衡净重数据维护");
                                logModel2.setOperationFunction("web端一次批量处理结净报警");
                                logModel2.setOperationId(actualModel.getActualNo());
                                logModel2.setOperationPredictionNo(actualModel.getPredictionNo());
                                logModel2.setOperationCarnNo(actualModel.getRailwayNo());
                                logModel2.setOperationBeforeFix("");
                                logModel2.setOperationAfterFix(actualModel.toString());
                                meterBaseOperationLogMapper.insertSelective(logModel2);
                            }

                            count ++;
                        }
                    }
                }
            }
            rm.setSucceed();
            rm.setData("200");
            rm.setMessage("共有" + count + "条数据一次批量成功");
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage(e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult doRecovery(List<MeterWorkRailwayActFirst> models) {
        RESTfulResult rm = new RESTfulResult();
        try{
            for(MeterWorkRailwayActFirst model: models) {
                MeterWorkRailwayActFirst modelTmep = meterWorkRailwayActFirstMapper.selectByPrimaryKey(model.getActualFirstNo());
                Assert.isTrue(!(modelTmep.getWeightType().equals("1") && modelTmep.getValueFlag().equals("0")), "一次数据包含非皮重数据，不允许还原皮重");
                Assert.isTrue(!(modelTmep.getValueFlag().equals("2") && modelTmep.getValueFlag().equals("0")), "一次数据包含已结净或者已作废，不允许还原皮重");
            }
            int count = 0;
            for(MeterWorkRailwayActFirst model: models) {
                MeterWorkRailwayActFirst modelTmep = meterWorkRailwayActFirstMapper.selectByPrimaryKey(model.getActualFirstNo());
                if(modelTmep != null) {
                    //删除一次皮重数据、还原从表
                    meterWorkRailwayActFirstMapper.deleteByPrimaryKey(modelTmep.getActualFirstNo());
                    //还原从表信息
                    meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceiveds(modelTmep.getActualFirstNo());
                    //还原主表为未处理状态
                    MeterWorkRailwayReceived receivedModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(modelTmep.getActualFirstNo());
                    if(receivedModel != null){
                        HashMap<String, Object> objMap = new HashMap<>();
                        objMap.put("soundName", receivedModel.getRailwayWorkNo());
                        List<MeterWorkRailwayRecMain> recMains = meterWorkRailwayRecMainMapper.selectByParameters(objMap);
                        if(recMains.size() >= 1) {
                            recMains.get(0).setIsDispose("0");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMains.get(0));
                        }
                    }
                    count ++;
                }
            }

            rm.setSucceed(true);
            rm.setData("200");
            rm.setMessage("共有" + count + "条皮重数据还原成功");
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage(e.getMessage());
        }
        return rm;
    }

    @Override
    public List<String> distinctHookRailwayNo(HashMap<String, Object> param) {
        if(param==null){
            param=new HashMap<>();
        }
        List<MeterWorkRailwayActFirst> templist = meterWorkRailwayActFirstMapper.distinctHookRailwayNo(param);
        List<String> list = new ArrayList<>();
        if (templist != null && !templist.isEmpty() ) {
            try{
                list = templist.stream().map(MeterWorkRailwayActFirst::getRailwayNo).distinct().collect(Collectors.toList());
                if(list==null && list.isEmpty()) {
                    list.add("");
                }
            }catch (Exception e) {
                list.add("");
            }
        }
        return list;
    }

    @Override
    public List<MeterWorkRailwayActFirst> hookFirstByDesc(HashMap<String, Object> parameters) {
//        String[] railwayNoList = parameters.get("railwayNoList") == null? "".split(",") :parameters.get("railwayNoList").toString().split(",");
//        parameters.remove("railwayNoList");
//        parameters.put("railwayNoList", railwayNoList);
//        for (String str: railwayNoList) {
//            if(str.equals("")) {
//                parameters.put("railwayNoNull", "1");
//                break;
//            }else {
//                parameters.put("railwayNoNull", "");
//            }
//        }
        List<MeterWorkRailwayActFirst> rows = meterWorkRailwayActFirstMapper.hookFirstByDesc(parameters);
        return rows;
    }

    @Override
    public PageList<MeterWorkRailwayActFirst> hookFirstPageList(HashMap<String, Object> param, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkRailwayActFirst> rows = meterWorkRailwayActFirstMapper.hookFirstByDesc(param);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }
    @Override
    public int abolishCSharp(String railwayNo, String startTime, String endTime) {
        try {
            if (StringUtils.isEmpty(railwayNo)) {
                throw new MarkerMetException(500, "参数为空!!");
            }
            String userId = "";
            String userName = "";
            try{
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch (Exception e) {
                userId = "天车秤自动作废";
                userName = "天车秤自动作废";
            }
            int num = meterWorkRailwayActFirstMapper.abolishCSharp(railwayNo,userId,userName,startTime,endTime);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

}
