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

import com.fasterxml.jackson.databind.node.BigIntegerNode;
import com.google.inject.internal.cglib.proxy.$UndeclaredThrowableException;
import com.jisco.dynamicweight.util.UserUtil;
import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.baseinfo.meterbasematterinfo.mapper.MeterBaseMatterInfoMapper;
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.meterbasespotinfo.model.MeterBaseSpotInfo;
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.constants.DjPbModel;
import com.steerinfo.meterwork.dto.CommonCombination;
import com.steerinfo.meterwork.dto.StaffPerformanceAnalysis;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkcaractual.model.MeterWorkCarActual;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.meterwork.meterworkcaractualfirstbak.controller.MeterWorkCarActualFirstBakController;
import com.steerinfo.meterwork.meterworkcaractualfirstbak.model.MeterWorkCarActualFirstBak;
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.meterworkrailwayactfirstbak.mapper.MeterWorkRailwayActFirstBakMapper;
import com.steerinfo.meterwork.meterworkrailwayactfirstbak.model.MeterWorkRailwayActFirstBak;
import com.steerinfo.meterwork.meterworkrailwayactfirstbak.service.IMeterWorkRailwayActFirstBakService;
import com.steerinfo.meterwork.meterworkrailwayactual.mapper.MeterWorkRailwayActualMapper;
import com.steerinfo.meterwork.meterworkrailwayactual.model.MeterWorkRailwayActual;
import com.steerinfo.meterwork.meterworkrailwayactualbak.mapper.MeterWorkRailwayActualBakMapper;
import com.steerinfo.meterwork.meterworkrailwayactualbak.model.MeterWorkRailwayActualBak;
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.meterwork.meterworkscheduledhelp.model.MeterWorkScheduledHelp;
import com.steerinfo.pretrack.prerailwayscale.mapper.PreRailwayScaleMapper;
import com.steerinfo.pretrack.prerailwayscale.model.PreRailwayScale;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.util.FieldsCollector;
import com.steerinfo.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import io.micrometer.core.instrument.Meter;
import org.apache.poi.hssf.record.ArrayRecord;
import org.apache.shiro.crypto.hash.Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import 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 = "meterWorkRailwayActFirstBakService")
public class MeterWorkRailwayActFirstBakServiceImpl extends BaseServiceImpl<MeterWorkRailwayActFirstBak, String> implements IMeterWorkRailwayActFirstBakService {


    @Resource
    private MeterWorkRailwayActFirstBakMapper meterWorkRailwayActFirstBakMapper;

    @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 MeterWorkRailwayActualBakMapper meterWorkRailwayActualBakMapper;

    @Resource
    private MeterBaseRailwayHisTareDataMapper meterBaseRailwayHisTareDataMapper;

    @Resource
    private MeterBaseVideoinfoMapper meterBaseVideoinfoMapper;

    @Resource
    private MeterWorkImageMapper meterWorkImageMapper;

    @Resource
    private MeterWorkRailwayActFirstMapper meterWorkRailwayActFirstMapper;

    @Resource
    private MeterBaseRailwayHisTareDetailMapper meterBaseRailwayHisTareDetailMapper;

    @Resource
    private MeterBaseOperationLogMapper meterBaseOperationLogMapper;

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

    @Override
    protected IBaseMapper<MeterWorkRailwayActFirstBak, String> getMapper() {
        return meterWorkRailwayActFirstBakMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult checkandadd(MeterWorkRailwayActFirstBak model) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            List<MeterWorkRailwayActFirstBak> firsts = new ArrayList<>();
            HashMap<String, Object> map = new HashMap<>();

            check1(model);
            model.setDataSource("1");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String no = meterWorkRailwayActFirstBakMapper.getNewID(sdf.format(new Date()));
            model.setActualFirstNo(sdf.format(new Date()) + no);
            model.setCreateTime(new Date());
            int i = meterWorkRailwayActFirstBakMapper.insertSelective(model);

            if (i >= 1) {
                firsts.add(model);
            }
            map.put("firsts", firsts);
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            rm.setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult dynamicAdd(MeterWorkRailwayActFirstBak 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<MeterWorkRailwayActFirstBak> modelList = meterWorkRailwayActFirstBakMapper.selectByParameters(obj);
            if(modelList.size() >= 1) {
                for (MeterWorkRailwayActFirstBak model_i:
                     modelList) {
                    model_i.setValueFlag("0");
                    model_i.setNote("一次新增，作废皮重");
                    model_i.setUpdateManNo(userId);
                    model_i.setUpdateManName(userName);
                    model_i.setUpdateTime(new Date());
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model_i);
                }
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            model.setActualFirstNo(sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(sdf.format(new Date())));
            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("一次新增");
            meterWorkRailwayActFirstBakMapper.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(MeterWorkRailwayActFirstBak model) {
        try {
            check(model);
            if (model.getValueFlag().equals(DbConstants.NET)) {
                throw new MarkerMetException(500, "已经结净的数据无法修改！！");
            }
            int num = meterWorkRailwayActFirstBakMapper.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
    public RESTfulResult updateList(List<MeterWorkRailwayActFirstBak> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            for (MeterWorkRailwayActFirstBak model: models) {
                check(model);
                if (model.getValueFlag().equals(DbConstants.NET)) {
                    throw new MarkerMetException(500, "已经结净的数据无法修改！！");
                }
            }
            int count = 0;
            for (MeterWorkRailwayActFirstBak model: models) {
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model);
                count ++;
            }
            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(MeterWorkRailwayActFirstBak 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(MeterWorkRailwayActFirstBak model) {
        try {
            check(model);
            if (model.getCheckFlag().equals("0")) {
                model.setCheckFlag(DbConstants.VALID);
            } else {
                throw new MarkerMetException(500, "当前状态不可审核");
            }
            int num = meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int abolish(MeterWorkRailwayActFirstBak model) {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数为空!!");
            }
            check(model);
            if (model.getValueFlag().equals(DbConstants.NET)) {
                throw new MarkerMetException(500, "已结净的数据不可再废除!!");
            }
            model.setValueFlag("0");
            int num = meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model);
            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("3");
                    scale.setUploadFlag("1");
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
                }
            }
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int recover(MeterWorkRailwayActFirstBak model) {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数为空!!");
            }
            check(model);
            if (model.getValueFlag().equals(DbConstants.NET)) {
                throw new MarkerMetException(500, "该状态无法启用");
            }
            HashMap map = new HashMap();
            map.put("actualFirstNo", model.getActualFirstNo());
            List<MeterWorkRailwayActualBak> rows = meterWorkRailwayActualBakMapper.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 = meterWorkRailwayActualBakMapper.selectByParameter(map);
                if(rows.size() >= 1) {
                    model.setValueFlag("2");
                }else{
                    model.setValueFlag("1");
                }
            }else {
                map.put("actualFirst2No", model.getActualFirstNo());
                List<MeterWorkRailwayActual> rows = meterWorkRailwayActualBakMapper.selectByParameter(map);
                if(rows.size() >= 1) {
                    model.setValueFlag("2");
                }else{
                    model.setValueFlag("1");
                }
            }*/
            int num = meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model);

            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(MeterWorkRailwayActFirstBak model) {
        try {
            ckeckcar(model);
            int num = meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    private void ckeckcar(MeterWorkRailwayActFirstBak model) throws MarkerMetException {
        if (model.getActualFirstNo() == null || "".equals(model.getActualFirstNo())) {
            throw new MarkerMetException(500, "操作数据主键不能为空");
        }
        if (model.getValueFlag() == "2") {
            throw new MarkerMetException(500, "已经结净的数据不能进行修改");
        }
        if (model.getPredictionNo() != null || "".equals(model.getPredictionNo())) {
            throw new MarkerMetException(500, "已经匹配委托的数据无法修改车号！！");
        }
        if (model.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<MeterWorkRailwayActFirstBak> likeByDesc(HashMap<String, Object> parameters, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkRailwayActFirstBak> rows = meterWorkRailwayActFirstBakMapper.likeByDesc(parameters);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public List<MeterWorkRailwayActFirstBak> 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<MeterWorkRailwayActFirstBak> rows = meterWorkRailwayActFirstBakMapper.likeByDesc(parameters);
        return rows;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> distinctRailwayNo(HashMap<String, Object> param) {
        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<MeterWorkRailwayActFirstBak> templist = meterWorkRailwayActFirstBakMapper.distinctRailwayNo(param);
        List<String> list = new ArrayList<>();
        if(templist != null && !templist.isEmpty() ) {
            try{
                list = templist.stream().map(MeterWorkRailwayActFirstBak::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<MeterWorkRailwayActFirstBak> templist = meterWorkRailwayActFirstBakMapper.distinctLcNo(param);
        List<String> list = new ArrayList<>();
        if (templist != null && !templist.isEmpty() ) {
            try{
                list = templist.stream().map(MeterWorkRailwayActFirstBak::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<MeterWorkRailwayActFirstBak> tempList = meterWorkRailwayActFirstBakMapper.distinctLcNo(param);
            List<String> list = null;
            List<String> railwayNum = null;
            if (tempList.size() != 0) {
                list = tempList.stream().map(MeterWorkRailwayActFirstBak::getLcNo).distinct().collect(Collectors.toList());
                railwayNum = tempList.stream().map(MeterWorkRailwayActFirstBak::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<MeterWorkRailwayActFirstBak> selectAct(HashMap parmas, Integer pageNum, Integer pageSize) {
        try {
            parmas.put("railwayTypeName", "动态轨道衡");
            parmas.put("railwayTypeNo", DbConstants.ACT);
            List<MeterWorkRailwayActFirstBak> mwrafs = meterWorkRailwayActFirstBakMapper.selectAct(parmas);
            return mwrafs;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    /**
     * 静态轨道衡复磅保存
     *
     * @param model
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String doAddWfRecover(MeterWorkRailwayActFirstBak model) {
        MeterWorkRailwayActFirstBak actFirst = meterWorkRailwayActFirstBakMapper.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() + "]");
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(actFirst);
            //=================一次计量数据处理：结束=====================

            //=================已结净的数据处理：开始=====================
            MeterWorkRailwayActualBak actual = new MeterWorkRailwayActualBak();
            if ("0".equals(actFirst.getWeightType())) {
                actual.setActualFirst1No(actFirst.getActualFirstNo()); //二次计量的毛重作业编号
            } else {
                actual.setActualFirst2No(actFirst.getActualFirstNo());//二次计量的皮重作业编号
            }
            List<MeterWorkRailwayActualBak> actualList = meterWorkRailwayActualBakMapper.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");
                meterWorkRailwayActualBakMapper.updateByPrimaryKeySelective(actual);
            }
            //=================已结净的数据处理：结束=====================
        }
        return model.getActualFirstNo();
    }

    /**
     * C#端轨道衡实绩静轨-复磅-更新自重
     *
     * @param model
     * @return
     */
    @Override
    public String doUpdateSelfWeight(MeterWorkRailwayActFirstBak model) {
        MeterWorkRailwayActFirstBak actFirst = meterWorkRailwayActFirstBakMapper.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() + "]");
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(actFirst);
            //=================一次计量数据处理：结束=====================

            //=================已结净的数据处理：开始=====================
            MeterWorkRailwayActualBak actual = new MeterWorkRailwayActualBak();
            if ("0".equals(actFirst.getWeightType())) {
                actual.setActualFirst1No(actFirst.getActualFirstNo()); //二次计量的毛重作业编号
            } else {
                actual.setActualFirst2No(actFirst.getActualFirstNo());//二次计量的皮重作业编号
            }
            List<MeterWorkRailwayActualBak> actualList = meterWorkRailwayActualBakMapper.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");
                meterWorkRailwayActualBakMapper.updateByPrimaryKeySelective(actual);
            }
            //=================已结净的数据处理：结束=====================
        }
        return model.getActualFirstNo();
    }

    @Override
    public RESTfulResult doAddNetHZHX(MeterWorkRailwayActFirstBak first02, MeterWorkRailwayActualBak carModel, MeterWorkRailwayActFirstBak 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()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
            first02.setActualFirstNo(firstNo01);
            first02.setCreateTime(new Date());
            meterWorkRailwayActFirstBakMapper.insertSelective(first02);

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

                MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAddPre(first02.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
                String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + 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());
                }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());
                }
                MeterWorkRailwayActFirstBak grossModel = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualRow.getActualFirst1No());
                MeterWorkRailwayActFirstBak tareModel = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualRow.getActualFirst2No());
                if(actualRow.getGrossTime().compareTo(actualRow.getTareTime()) >= 0) {
                    actualRow.setLcNo(grossModel.getLcNo());
                }else {
                    actualRow.setLcNo(tareModel.getLcNo());
                }
                meterWorkRailwayActualBakMapper.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")) {         //混卸
                //修改委托状态
                //将委托中的编号和收发货的等数据拷贝进一次数据中
                MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first1, model01);
                MeterWorkRailwayActFirstBak first2 = meterWorkRailwayActFirstBakMapper.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");
                meterWorkRailwayActFirstBakMapper.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");
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model01);

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

                //保存结净
                String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + 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());
                }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());
                }
                meterWorkRailwayActualBakMapper.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()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
                first03.setActualFirstNo(fristNo03);
                first03.setCreateTime(new Date());
                meterWorkRailwayActFirstBakMapper.insertSelective(first03);
            }
            /*//6、修改计量监控
            updateMonitor(first02.getBaseSpotNo());
            //保存日志
            saveNote(note);
            updateMonitorOver(first02.getActualFirstNo(), first02.getBaseSpotNo());

            //补充附加重量的数据
            if (StringUtils.isNotEmpty(first02.getAddWgtPk())) {
                insertAddWgtNo(first02.getActualFirstNo(), first02.getAddWgtPk(), first02.getCreateManNo(), first02.getCreateManName());
            }*/
            rm.setSucceed();
            rm.setData(first02.getActualFirstNo());
            rm.setCode("200");
            rm.setMessage("操作成功！！");
            return rm;
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！！>>>>" + e.getMessage());
            return rm;
        }
    }

    @Override
    public RESTfulResult doNetAddTwo(MeterWorkRailwayActFirstBak one, MeterWorkRailwayActualBak 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()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
            one.setActualFirstNo(firstNo01);
            one.setCreateTime(new Date());
            meterWorkRailwayActFirstBakMapper.insertSelective(one);

            MeterWorkRailwayActFirstBak model01 = new MeterWorkRailwayActFirstBak();
            MeterWorkRailwayActFirstBak model02 = new MeterWorkRailwayActFirstBak();

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

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

            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale01.getPredictionNo());
            BeanUtils.copyProperties(first1, model01);
            MeterWorkRailwayActFirstBak first2 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model02.getActualFirstNo(), scale01.getPredictionNo());
            BeanUtils.copyProperties(first2, model02);
            MeterWorkRailwayActFirstBak first3 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale02.getPredictionNo());
            BeanUtils.copyProperties(first3, model03);
            MeterWorkRailwayActFirstBak first4 = meterWorkRailwayActFirstBakMapper.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());
            meterWorkRailwayActFirstBakMapper.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());
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(model02);
            //添加第一次毛重数据库
            String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
            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());
            meterWorkRailwayActFirstBakMapper.insertSelective(model03);
            //添加第二次皮重数据库
            String actualSecondNo = sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
            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());
            meterWorkRailwayActFirstBakMapper.insertSelective(model04);

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

            //添加结净数据
            String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow1.setActualNo(sdf.format(new Date()) + 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());
            }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());
            }
            meterWorkRailwayActualBakMapper.insertSelective(actualRow1);

            String no1 = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow2.setActualNo(sdf.format(new Date()) + 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());
            }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());
            }
            meterWorkRailwayActualBakMapper.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);

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

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

    @Transactional
    @Override
    public RESTfulResult doNetAddTwo02(List<MeterWorkRailwayActFirstBak> selectedFirsts, List<MeterWorkRailwayActFirstBak> addFirsts, List<PreRailwayScale> scales, String note) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {

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

            HashMap map = new HashMap();
            List<MeterWorkRailwayActFirstBak> newFirsts = new ArrayList<>();
            List<MeterWorkRailwayActualBak> newActuals = new ArrayList<>();


            //如果计量员选择一次记录是空就是第一次 真，不是空就是第二次 假
            boolean isFirst = CollectionUtils.isEmpty(selectedFirsts);
            Assert.isTrue(!CollectionUtils.isEmpty(addFirsts) && addFirsts.size() >= 1, "新增一次记录必须不能为空");
            if(!isFirst) {
                Assert.isTrue(!CollectionUtils.isEmpty(scales) && scales.size() == 2, "双委托结净必须有两条委托");
            }
            if(!CollectionUtils.isEmpty(scales)) {
                Assert.isTrue(  scales.size() <= 2, "委托最多只能选择两条");
            }
            //第一次：所选择的数据为空
            if (isFirst) {
                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("1");
                    scales.get(i).setUpdateManName("双委托更新");
                    scales.get(i).setPredictionType("5");
                }
                for (int i = 0; i < addFirsts.size(); i++) {
                    String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
                    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("双委托更新");
                    meterWorkRailwayActFirstBakMapper.insertSelective(addFirsts.get(i));
                }
                meterWorkRailwayActFirstBakMapper.batchUpdate(addFirsts);
                if(!CollectionUtils.isEmpty(scales)) {
                    preRailwayScaleMapper.batchUpdate(scales);
                }
                newFirsts.addAll(addFirsts);
            }
            //第二次，所选择数据一条或者两条
            else {
                String userId = selectedFirsts.get(0).getCreateManNo();
                String userName = selectedFirsts.get(0).getCreateManName();
                //如果只选择了一条一次记录，就复制一条
                if (selectedFirsts.size() == 1) {
                    MeterWorkRailwayActFirstBak first = selectedFirsts.get(0);
                    MeterWorkRailwayActFirstBak firstCopy = new MeterWorkRailwayActFirstBak();
                    BeanUtils.copyProperties(selectedFirsts.get(0), firstCopy);
                    String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
                    firstCopy.setActualFirstNo(actualFirstNo);
                    firstCopy.setCreateManNo(userId);
                    firstCopy.setCreateManName(userName);
                    firstCopy.setCreateTime(new Date());
                    meterWorkRailwayActFirstBakMapper.insertSelective(firstCopy);
                    selectedFirsts.add(firstCopy);
                    newFirsts.add(firstCopy);
                }

                for (int i = 0; i < scales.size(); i++) {
                    String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
                    addFirsts.get(i).setActualFirstNo(actualFirstNo);
                    addFirsts.get(i).setValueFlag(DbConstants.NET);
                    addFirsts.get(i).setUpdateManNo("system");
                    addFirsts.get(i).setUpdateManName("双委托结净更新");
                    addFirsts.get(i).setCreateManNo(userId);
                    addFirsts.get(i).setCreateManName(userName);
                    addFirsts.get(i).setCreateTime(new Date());
                    addFirsts.get(i).setWeightType("0");
                    addFirsts.get(i).setUpdateTime(new Date());
                    addFirsts.get(i).setIsPreSacale("1");
                    addFirsts.get(i).setPredictionNo(scales.get(i).getPredictionNo());
                    addFirsts.get(i).setPredictionType("5");
                    addFirsts.get(i).setPredictionCombination(selectedFirsts.get(i).getPredictionNo() + "," + addFirsts.get(i).getPredictionNo());
                    addFirsts.get(i).setCreateTime(new Date());
                    meterWorkRailwayActFirstBakMapper.insertSelective(addFirsts.get(i));
                    //添加第二次皮重数据库
                    selectedFirsts.get(i).setValueFlag(DbConstants.NET);
                    selectedFirsts.get(i).setUpdateManNo("system");
                    selectedFirsts.get(i).setUpdateManName("双委托结净更新");
                    selectedFirsts.get(i).setWeightType("0");
                    selectedFirsts.get(i).setUpdateTime(new Date());
                    selectedFirsts.get(i).setIsPreSacale("1");
                    selectedFirsts.get(i).setPredictionNo(scales.get(i).getPredictionNo());
                    selectedFirsts.get(i).setPredictionType("5");
                    selectedFirsts.get(i).setPredictionCombination(selectedFirsts.get(i).getPredictionNo() + "," + addFirsts.get(i).getPredictionNo());
                    selectedFirsts.get(i).setCreateTime(new Date());
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(selectedFirsts.get(i));
                    scales.get(i).setValueFlag("2");
                    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++) {
                    MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();
                    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("0");
                        selectedFirsts.get(i).setWeightType("1");
                        doSaveTare(selectedFirsts.get(i));
                    }else {
                        doNet(actualRow, addFirsts.get(i), selectedFirsts.get(i),  addFirsts.get(i).getPredictionNo());
                        addFirsts.get(i).setWeightType("1");
                        selectedFirsts.get(i).setWeightType("0");
                        doSaveTare(addFirsts.get(i));
                    }
                    String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    actualRow.setNote("双委托匹配结净");
                    actualRow.setNetMode("3");
                    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());
                    }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());
                    }
                    meterWorkRailwayActualBakMapper.insertSelective(actualRow);
                    newActuals.add(actualRow);
                }
                meterWorkRailwayActFirstBakMapper.batchUpdate(addFirsts);
                meterWorkRailwayActFirstBakMapper.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();
            rm.setMessage("操作异常，请确认！！>>>>" + e.getMessage());
            return rm;
        }
    }

    //一次记录委托结净匹配
    private void doNet(MeterWorkRailwayActualBak actualRow, MeterWorkRailwayActFirstBak grossFirst, MeterWorkRailwayActFirstBak tareFirst, String predictionNo) {
        MeterWorkRailwayActualBak mwca = meterWorkRailwayActualBakMapper.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.setNetWeight(grossFirst.getMeterWeight().subtract(tareFirst.getMeterWeight()));
    }

    //一次记录匹配委托
    private void firstMatchPreScale(MeterWorkRailwayActFirstBak first, PreRailwayScale scale) {
        BeanUtils.copyProperties(scale, first);
        first.setIsPreSacale("1");
        first.setPredictionNo(scale.getPredictionNo());
        scale.setUpdateTime(new Date());
        scale.setUpdateManNo("system");
        scale.setUploadFlag("1");
    }

    @Override
    public RESTfulResult doAddNetHZHXTwo(PreRailwayScale scale, MeterWorkRailwayActFirstBak
            first01, MeterWorkRailwayActFirstBak first02, MeterMonitorNote note, String flag) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

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

            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()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
                first02.setActualFirstNo(firstNo01);
                first02.setCreateTime(new Date());
                meterWorkRailwayActFirstBakMapper.insertSelective(first02);

                MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();
                MeterWorkRailwayActFirstBak model01 = new MeterWorkRailwayActFirstBak();
                MeterWorkRailwayActFirstBak model02 = new MeterWorkRailwayActFirstBak();

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

                    //修改委托状态
                    //将委托中的编号和收发货的等数据拷贝进一次数据中
                    /*MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first1, model01);
                    MeterWorkRailwayActFirstBak first2 = meterWorkRailwayActFirstBakMapper.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");
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model01);
                    //修改皮重一次数据状态
                    model02.setValueFlag(DbConstants.NET);
                    model02.setUpdateManNo("system");
                    model02.setUpdateManName("智能计量混装结净更新");
                    model02.setUpdateTime(new Date());
                    model02.setWeightType("1");
                    model02.setIsPreSacale("0");
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model02);

                    MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAdd(model01.getActualFirstNo(), model02.getActualFirstNo());
                    BeanUtils.copyProperties(mwca1, actualRow);
                    //保存结净
                    String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("智能计量混装匹配结净");
                    actualRow.setNetMode("4");
                    actualRow.setUploadFlag("1");
                    actualRow.setValueFlag("1");
                    actualRow.setIsPreSacale("0");
                    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());
                    }
                    meterWorkRailwayActualBakMapper.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 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(first01.getActualFirstNo());
                    //皮重一次
                    model02 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(first02.getActualFirstNo());

                    //修改委托状态
                    //将委托中的编号和收发货的等数据拷贝进一次数据中
                    /*MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first1, model01);
                    MeterWorkRailwayActFirstBak first2 = meterWorkRailwayActFirstBakMapper.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");
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model01);
                    //修改皮重一次数据状态
                    model02.setValueFlag(DbConstants.NET);
                    model02.setUpdateManNo("system");
                    model02.setUpdateManName("智能计量混卸结净更新");
                    model02.setUpdateTime(new Date());
                    model02.setWeightType("1");
                    model02.setIsPreSacale("0");
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model02);

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

                    //保存结净
                    String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("智能计量混卸匹配结净");
                    actualRow.setNetMode("6");
                    actualRow.setUploadFlag("1");
                    actualRow.setValueFlag("1");
                    actualRow.setIsPreSacale("0");
                    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());
                    }
                    meterWorkRailwayActualBakMapper.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);
                    }
                    String firstNo = sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
                    first02.setActualFirstNo(firstNo);
                    first02.setPredictionNo("混装混卸复制");
                    first02.setValueFlag("1");
                    first02.setCreateTime(new Date());
                    meterWorkRailwayActFirstBakMapper.insertSelective(first02);
                } else {
                    model01.setPredictionNo("");
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model01);
                    model02.setPredictionNo("");
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model02);
                }
            /*//6、修改计量监控
            updateMonitor(first02.getBaseSpotNo());
            //保存日志
            saveNote(note);
            updateMonitorOver(first02.getActualFirstNo(), first02.getBaseSpotNo());

            //补充附加重量的数据
            if (StringUtils.isNotEmpty(first02.getAddWgtPk())) {
                insertAddWgtNo(first02.getActualFirstNo(), first02.getAddWgtPk(), first02.getCreateManNo(), first02.getCreateManName());
            }*/
                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()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
                first02.setActualFirstNo(firstNo01);
                first02.setCreateTime(new Date());
                meterWorkRailwayActFirstBakMapper.insertSelective(first02);

                MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();
                MeterWorkRailwayActFirstBak model01 = new MeterWorkRailwayActFirstBak();
                MeterWorkRailwayActFirstBak model02 = new MeterWorkRailwayActFirstBak();

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

                    //修改委托状态
                    //将委托中的编号和收发货的等数据拷贝进一次数据中
                    MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first1, model01);
                    MeterWorkRailwayActFirstBak first2 = meterWorkRailwayActFirstBakMapper.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");
                    meterWorkRailwayActFirstBakMapper.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");
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model02);

                    MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAddPre(model01.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(mwca1, actualRow);
                    //保存结净
                    String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                    actualRow.setActualNo(sdf.format(new Date()) + 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());
                    }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());
                    }
                    meterWorkRailwayActualBakMapper.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 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(first01.getActualFirstNo());
                    //皮重一次
                    model02 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(first02.getActualFirstNo());

                    //修改委托状态
                    //将委托中的编号和收发货的等数据拷贝进一次数据中
                    MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first1, model01);
                    MeterWorkRailwayActFirstBak first2 = meterWorkRailwayActFirstBakMapper.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");
                    meterWorkRailwayActFirstBakMapper.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");
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model02);

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

                    //保存结净
                    String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                    actualRow.setActualNo(sdf.format(new Date()) + 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());
                    }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());
                    }
                    meterWorkRailwayActualBakMapper.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);
                    }
                    String firstNo = sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
                    first02.setActualFirstNo(firstNo);
                    first02.setPredictionNo("混装混卸复制");
                    first02.setValueFlag("1");
                    first02.setCreateTime(new Date());
                    meterWorkRailwayActFirstBakMapper.insertSelective(first02);
                }
            /*//6、修改计量监控
            updateMonitor(first02.getBaseSpotNo());
            //保存日志
            saveNote(note);
            updateMonitorOver(first02.getActualFirstNo(), first02.getBaseSpotNo());

            //补充附加重量的数据
            if (StringUtils.isNotEmpty(first02.getAddWgtPk())) {
                insertAddWgtNo(first02.getActualFirstNo(), first02.getAddWgtPk(), first02.getCreateManNo(), first02.getCreateManName());
            }*/
                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;
            }
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！！>>>>" + e.getMessage());
            return rm;
        }
    }

    @Override
    public RESTfulResult doNetAddTwoDouble(PreRailwayScale scale01, PreRailwayScale
            scale02, MeterWorkRailwayActFirstBak first01, MeterWorkRailwayActFirstBak first02) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        HashMap<String, Object> map = new HashMap<>();
        List<MeterWorkRailwayActFirstBak> firsts = new ArrayList<>();
        List<MeterWorkRailwayActualBak> 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 (!(scale01.getMatterNo().equals(scale02.getMatterNo()) &&
                        (scale01.getReceivingUintNo().equals(scale02.getForwardingUnitNo()) ||
                                scale01.getForwardingUnitNo().equals(scale02.getReceivingUintNo())))) {
                    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()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
            first02.setActualFirstNo(firstNo01);
            first02.setCreateTime(new Date());
            meterWorkRailwayActFirstBakMapper.insertSelective(first02);

            MeterWorkRailwayActFirstBak model01 = new MeterWorkRailwayActFirstBak();
            MeterWorkRailwayActFirstBak model02 = new MeterWorkRailwayActFirstBak();

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

            MeterWorkRailwayActFirstBak model03 = new MeterWorkRailwayActFirstBak();
            MeterWorkRailwayActFirstBak model04 = new MeterWorkRailwayActFirstBak();

            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale01.getPredictionNo());
            BeanUtils.copyProperties(first1, model01);
            MeterWorkRailwayActFirstBak first2 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model02.getActualFirstNo(), scale01.getPredictionNo());
            BeanUtils.copyProperties(first2, model02);
            MeterWorkRailwayActFirstBak first3 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale02.getPredictionNo());
            BeanUtils.copyProperties(first3, model03);
            MeterWorkRailwayActFirstBak first4 = meterWorkRailwayActFirstBakMapper.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());
            meterWorkRailwayActFirstBakMapper.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());
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(model02);
            //添加第一次毛重数据库
            String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
            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());
            meterWorkRailwayActFirstBakMapper.insertSelective(model03);
            //添加第二次皮重数据库
            String actualSecondNo = sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
            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());
            meterWorkRailwayActFirstBakMapper.insertSelective(model04);

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

            //添加结净数据
            String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow1.setActualNo(sdf.format(new Date()) + 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());
            }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());
            }
            meterWorkRailwayActualBakMapper.insertSelective(actualRow1);

            String no1 = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow2.setActualNo(sdf.format(new Date()) + 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());
            }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());
            }
            meterWorkRailwayActualBakMapper.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
    public String doubleEntrustMatch(String actualNo1, String actualNo2, String predictionNo1, String predictionNo2) {

        try {
            //查询出一次计量数据
            MeterWorkRailwayActFirstBak mwcaf01 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirstBak mwcaf02 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirstBak mwcaf03 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirstBak mwcaf04 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualNo2);
            //查询出所有委托信息
            PreRailwayScale pts01 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo1);
            PreRailwayScale pts02 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo2);
            //定义两个结净类
            MeterWorkRailwayActualBak actualRow1 = new MeterWorkRailwayActualBak();
            MeterWorkRailwayActualBak actualRow2 = new MeterWorkRailwayActualBak();
            //获取两个一次重量的差值
            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 (!mwcaf01.getRailwayNo().equals(mwcaf03.getRailwayNo())) {
                return "两条一次计量数据车号不一致,请确认！！";
            }
            //结净数据不能小于400
            /*if(num01.compareTo(BigDecimal.valueOf(400)) == -1){
                return "选择的两条一次计量数据不能小于400kg，请确认！！";
            }*/
            //委托信息是否为有效数据
            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 "一次计量数据已作废，请确认！！";
            }
            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.selectAndMatch(actualNo1, predictionNo1);
            BeanUtils.copyProperties(first1, mwcaf01);
            MeterWorkRailwayActFirstBak first2 = meterWorkRailwayActFirstBakMapper.selectAndMatch(actualNo2, predictionNo1);
            BeanUtils.copyProperties(first2, mwcaf02);
            MeterWorkRailwayActFirstBak first3 = meterWorkRailwayActFirstBakMapper.selectAndMatch(actualNo1, predictionNo2);
            BeanUtils.copyProperties(first3, mwcaf03);
            MeterWorkRailwayActFirstBak first4 = meterWorkRailwayActFirstBakMapper.selectAndMatch(actualNo2, predictionNo2);
            BeanUtils.copyProperties(first4, mwcaf04);

            //更新一次数据
            //更新第一次毛重数据库
            mwcaf01.setValueFlag(DbConstants.NET);
            mwcaf01.setUpdateManNo("system");
            mwcaf01.setUpdateManName("双委托结净更新");
            mwcaf01.setUpdateTime(new Date());
            mwcaf01.setIsPreSacale("1");
            mwcaf01.setPredictionNo(predictionNo1);
            mwcaf01.setPredictionType("5");
            mwcaf01.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(mwcaf01);
            //更新第一次皮重数据库
            mwcaf02.setValueFlag(DbConstants.NET);
            mwcaf02.setUpdateManNo("system");
            mwcaf02.setUpdateManName("双委托结净更新");
            mwcaf02.setUpdateTime(new Date());
            mwcaf02.setIsPreSacale("1");
            mwcaf02.setPredictionNo(predictionNo1);
            mwcaf02.setPredictionType("5");
            mwcaf02.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(mwcaf02);
            //添加第一次毛重数据库
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
            mwcaf03.setActualFirstNo(actualFirstNo);
            mwcaf03.setValueFlag(DbConstants.NET);
            mwcaf03.setUpdateManNo("system");
            mwcaf03.setUpdateManName("双委托结净更新");
            mwcaf03.setUpdateTime(new Date());
            mwcaf03.setIsPreSacale("1");
            mwcaf03.setPredictionNo(predictionNo2);
            mwcaf03.setPredictionType("5");
            mwcaf03.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            mwcaf03.setCreateTime(new Date());
            meterWorkRailwayActFirstBakMapper.insertSelective(mwcaf03);
            //添加第二次皮重数据库
            SimpleDateFormat sdf01 = new SimpleDateFormat("yyyyMMdd");
            String afl01 = sdf01.format(new Date());
            String actualSecondNo = sdf01.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl01);
            mwcaf04.setActualFirstNo(actualSecondNo);
            mwcaf04.setValueFlag(DbConstants.NET);
            mwcaf04.setUpdateManNo("system");
            mwcaf04.setUpdateManName("双委托结净更新");
            mwcaf04.setUpdateTime(new Date());
            mwcaf04.setIsPreSacale("1");
            mwcaf04.setPredictionNo(predictionNo2);
            mwcaf04.setPredictionType("5");
            mwcaf04.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            mwcaf04.setCreateTime(new Date());
            meterWorkRailwayActFirstBakMapper.insertSelective(mwcaf04);
            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAddPre(actualNo1, actualNo2, predictionNo1);
            BeanUtils.copyProperties(mwca1, actualRow1);
            MeterWorkRailwayActualBak mwca2 = meterWorkRailwayActualBakMapper.manualAddPre(actualFirstNo, actualSecondNo, predictionNo2);
            BeanUtils.copyProperties(mwca2, actualRow2);

            //添加结净数据
            String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow1.setActualNo(sdf.format(new Date()) + 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(predictionNo1);
            actualRow1.setPredictionType("5");
            actualRow1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow1.setNetWeight(mwcaf01.getMeterWeight().subtract(mwcaf02.getMeterWeight()));
            if(actualRow1.getGrossTime().compareTo(actualRow1.getTareTime()) >= 0) {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot1No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot1Name());
                actualRow1.setNetManNo(actualRow1.getGrossManNo());
                actualRow1.setNetManName(actualRow1.getGrossManName());
                actualRow1.setNetTime(actualRow1.getGrossTime());
                actualRow1.setCreateTime(actualRow1.getGrossTime());
                actualRow1.setCreateManName(actualRow1.getGrossManName());
            }else {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot2No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot2Name());
                actualRow1.setNetManNo(actualRow1.getTareManNo());
                actualRow1.setNetManName(actualRow1.getTareManName());
                actualRow1.setNetTime(actualRow1.getTareTime());
                actualRow1.setCreateTime(actualRow1.getTareTime());
                actualRow1.setCreateManName(actualRow1.getTareManName());
            }
            meterWorkRailwayActualBakMapper.insertSelective(actualRow1);

            String no1 = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow2.setActualNo(sdf.format(new Date()) + 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(predictionNo2);
            actualRow2.setPredictionType("5");
            actualRow2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow2.setNetWeight(mwcaf03.getMeterWeight().subtract(mwcaf04.getMeterWeight()));
            if(actualRow2.getGrossTime().compareTo(actualRow2.getTareTime()) >= 0) {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot1No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot1Name());
                actualRow2.setNetManNo(actualRow2.getGrossManNo());
                actualRow2.setNetManName(actualRow2.getGrossManName());
                actualRow2.setNetTime(actualRow2.getGrossTime());
                actualRow2.setCreateTime(actualRow2.getGrossTime());
                actualRow2.setCreateManName(actualRow2.getGrossManName());
            }else {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot2No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot2Name());
                actualRow2.setNetManNo(actualRow2.getTareManNo());
                actualRow2.setNetManName(actualRow2.getTareManName());
                actualRow2.setNetTime(actualRow2.getTareTime());
                actualRow2.setCreateTime(actualRow2.getTareTime());
                actualRow2.setCreateManName(actualRow2.getTareManName());
            }
            meterWorkRailwayActualBakMapper.insertSelective(actualRow2);

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

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

    @Override
    public String mixedLoading(String actualNo1, String actualNo2, String actualNo3, String predictionNo1, String
            predictionNo2) {
        try {
            //查询所有的一次数据
            MeterWorkRailwayActFirstBak mwcaf1 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirstBak mwcaf2 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirstBak mwcaf3 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirstBak mwcaf4 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualNo3);
            //查询所有的委托数据
            PreRailwayScale pts1 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo1);
            PreRailwayScale pts2 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo2);
            //定义新的结净数据实体
            MeterWorkRailwayActualBak actualRow1 = new MeterWorkRailwayActualBak();
            MeterWorkRailwayActualBak actualRow2 = new MeterWorkRailwayActualBak();
            //判断是否为混装逻辑
            if (mwcaf1.getMeterWeight().compareTo(mwcaf2.getMeterWeight()) == 1 ||
                    mwcaf1.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == 1 ||
                    mwcaf2.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == 1) {
                return "请根据一次数据从小到大的规则，依次选择三条一次数据！！";
            }
            //SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            //String fromDate = simpleFormat.format(pts1.getCreateTime());
            //String toDate = simpleFormat.format(pts2.getCreateTime());
            //long from = simpleFormat.parse(fromDate).getTime();
            //long to = simpleFormat.parse(toDate).getTime();
            //int hours = (int) ((to - from) / (1000 * 60 * 60));
            //if (pts1.getMatterNo().equals(pts2.getMatterNo()) &&
            //        pts1.getForwardingUnitNo().equals(pts2.getForwardingUnitNo()) &&
            //        hours <= 4) {
            //    return "委托信息不符合混装委托规则(同物料同发货单位委托创建时间不能小于4小时)，请确认!!";
            //}
            //判断一次数据是否存在
            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 "委托不是未使用状态！！";
            }
            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.selectAndMatch(actualNo1, predictionNo1);
            BeanUtils.copyProperties(first1, mwcaf1);
            MeterWorkRailwayActFirstBak first2 = meterWorkRailwayActFirstBakMapper.selectAndMatch(actualNo2, predictionNo1);
            BeanUtils.copyProperties(first2, mwcaf2);
            MeterWorkRailwayActFirstBak first3 = meterWorkRailwayActFirstBakMapper.selectAndMatch(actualNo2, predictionNo2);
            BeanUtils.copyProperties(first3, mwcaf3);
            MeterWorkRailwayActFirstBak first4 = meterWorkRailwayActFirstBakMapper.selectAndMatch(actualNo3, predictionNo2);
            BeanUtils.copyProperties(first4, mwcaf4);

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

            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAddPre(actualNo2, actualNo1, predictionNo1);
            BeanUtils.copyProperties(mwca1, actualRow1);
            MeterWorkRailwayActualBak mwca2 = meterWorkRailwayActualBakMapper.manualAddPre(actualNo3, actualFirstNo, predictionNo2);
            BeanUtils.copyProperties(mwca2, actualRow2);
            //添加结净数据
            String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow1.setActualNo(sdf.format(new Date()) + 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("混装匹配结净");
            actualRow1.setPredictionType("7");
            actualRow1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow1.setPredictionNo(predictionNo1);
            actualRow1.setNetWeight(mwcaf2.getMeterWeight().subtract(mwcaf1.getMeterWeight()));
            if(actualRow1.getGrossTime().compareTo(actualRow1.getTareTime()) >= 0) {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot1No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot1Name());
                actualRow1.setNetManNo(actualRow1.getGrossManNo());
                actualRow1.setNetManName(actualRow1.getGrossManName());
                actualRow1.setNetTime(actualRow1.getGrossTime());
                actualRow1.setCreateTime(actualRow1.getGrossTime());
                actualRow1.setCreateManName(actualRow1.getGrossManName());
            }else {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot2No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot2Name());
                actualRow1.setNetManNo(actualRow1.getTareManNo());
                actualRow1.setNetManName(actualRow1.getTareManName());
                actualRow1.setNetTime(actualRow1.getTareTime());
                actualRow1.setCreateTime(actualRow1.getTareTime());
                actualRow1.setCreateManName(actualRow1.getTareManName());
            }
            meterWorkRailwayActualBakMapper.insertSelective(actualRow1);

            String no1 = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow2.setActualNo(sdf.format(new Date()) + 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("混装匹配结净");
            actualRow2.setPredictionType("7");
            actualRow2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow2.setPredictionNo(predictionNo2);
            actualRow2.setNetWeight(mwcaf4.getMeterWeight().subtract(mwcaf3.getMeterWeight()));
            if(actualRow2.getGrossTime().compareTo(actualRow2.getTareTime()) >= 0) {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot1No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot1Name());
                actualRow2.setNetManNo(actualRow2.getGrossManNo());
                actualRow2.setNetManName(actualRow2.getGrossManName());
                actualRow2.setNetTime(actualRow2.getGrossTime());
                actualRow2.setCreateTime(actualRow2.getGrossTime());
                actualRow2.setCreateManName(actualRow2.getGrossManName());
            }else {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot2No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot2Name());
                actualRow2.setNetManNo(actualRow2.getTareManNo());
                actualRow2.setNetManName(actualRow2.getTareManName());
                actualRow2.setNetTime(actualRow2.getTareTime());
                actualRow2.setCreateTime(actualRow2.getTareTime());
                actualRow2.setCreateManName(actualRow2.getTareManName());
            }
            meterWorkRailwayActualBakMapper.insertSelective(actualRow2);

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

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

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

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

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

            //添加结净数据
            String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow1.setActualNo(sdf.format(new Date()) + 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("混卸匹配结净");
            actualRow1.setCreateTime(new Date());
            actualRow1.setPredictionType("6");
            actualRow1.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow1.setPredictionNo(predictionNo1);
            actualRow1.setNetWeight(mwcaf1.getMeterWeight().subtract(mwcaf2.getMeterWeight()));
            if(actualRow1.getGrossTime().compareTo(actualRow1.getTareTime()) >= 0) {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot1No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot1Name());
                actualRow1.setNetManNo(actualRow1.getGrossManNo());
                actualRow1.setNetManName(actualRow1.getGrossManName());
                actualRow1.setNetTime(actualRow1.getGrossTime());
                actualRow1.setCreateTime(actualRow1.getGrossTime());
                actualRow1.setCreateManName(actualRow1.getGrossManName());
            }else {
                actualRow1.setNetSpot3No(actualRow1.getBaseSpot2No());
                actualRow1.setNetSpot3Name(actualRow1.getBaseSpot2Name());
                actualRow1.setNetManNo(actualRow1.getTareManNo());
                actualRow1.setNetManName(actualRow1.getTareManName());
                actualRow1.setNetTime(actualRow1.getTareTime());
                actualRow1.setCreateTime(actualRow1.getTareTime());
                actualRow1.setCreateManName(actualRow1.getTareManName());
            }
            meterWorkRailwayActualBakMapper.insertSelective(actualRow1);

            String no1 = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow2.setActualNo(sdf.format(new Date()) + 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("混卸匹配结净");
            actualRow2.setCreateTime(new Date());
            actualRow2.setPredictionType("6");
            actualRow2.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            actualRow2.setPredictionNo(predictionNo2);
            actualRow2.setNetWeight(mwcaf3.getMeterWeight().subtract(mwcaf4.getMeterWeight()));
            if(actualRow2.getGrossTime().compareTo(actualRow2.getTareTime()) >= 0) {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot1No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot1Name());
                actualRow2.setNetManNo(actualRow2.getGrossManNo());
                actualRow2.setNetManName(actualRow2.getGrossManName());
                actualRow2.setNetTime(actualRow2.getGrossTime());
                actualRow2.setCreateTime(actualRow2.getGrossTime());
                actualRow2.setCreateManName(actualRow2.getGrossManName());
            }else {
                actualRow2.setNetSpot3No(actualRow2.getBaseSpot2No());
                actualRow2.setNetSpot3Name(actualRow2.getBaseSpot2Name());
                actualRow2.setNetManNo(actualRow2.getTareManNo());
                actualRow2.setNetManName(actualRow2.getTareManName());
                actualRow2.setNetTime(actualRow2.getTareTime());
                actualRow2.setCreateTime(actualRow2.getTareTime());
                actualRow2.setCreateManName(actualRow2.getTareManName());
            }
            meterWorkRailwayActualBakMapper.insertSelective(actualRow2);

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

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

    @Override
    public String fixedTare(String actualNo, String tareWeight, String predictionNo) {

        if (StringUtils.isEmpty(actualNo)) {
            return "实绩编号不存在，请确认！！";
        }
        MeterWorkRailwayActFirstBak grossModel = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualNo);
        if (!grossModel.getValueFlag().equals("1")) {
            return "当前计量数据已结净，请确认！！";
        }
        MeterWorkRailwayActFirstBak tareModel = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualNo);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(new Date());
        String firstNo = sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
        tareModel.setActualFirstNo(firstNo);
        BigDecimal weight = (new BigDecimal(tareWeight)).multiply(new BigDecimal(1000));
        tareModel.setMeterWeight(weight);
        tareModel.setWeightType("4");
        tareModel.setCreateTime(new Date());
        int i = meterWorkRailwayActFirstBakMapper.insert(tareModel);

        if (i >= 1) {
            tareModel = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(tareModel.getActualFirstNo());
            MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();

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

                //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.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 {
                //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                BeanUtils.copyProperties(mwca1, actualRow);
            }
            grossModel.setValueFlag(DbConstants.NET);
            grossModel.setUpdateManNo("system");
            grossModel.setUpdateManName("二次计量结净更新");
            grossModel.setUpdateTime(new Date());
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(grossModel);
            //更新第一次皮重数据
            tareModel.setValueFlag(DbConstants.NET);
            tareModel.setUpdateManNo("system");
            tareModel.setUpdateManName("二次计量结净更新");
            tareModel.setUpdateTime(new Date());
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(tareModel);

            String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow.setActualNo(sdf.format(new Date()) + 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());
            }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());
            }
            meterWorkRailwayActualBakMapper.insertSelective(actualRow);
            return "操作成功";
        }
        return "";
    }

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

        if (StringUtils.isEmpty(actualNo)) {
            rm.setMessage("实绩编号不存在，请确认！！");
            return rm;
        }
        MeterWorkRailwayActFirstBak grossModel = meterWorkRailwayActFirstBakMapper.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();
            MeterWorkRailwayActFirstBak tareModel = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualNo);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            String firstNo = sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
            tareModel.setActualFirstNo(firstNo);
            tareModel.setMeterWeight(new BigDecimal(tareWeight));
            tareModel.setWeightType("3");
            tareModel.setCreateTime(new Date());
            int i = meterWorkRailwayActFirstBakMapper.insert(tareModel);

            if (i >= 1) {
                tareModel = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(tareModel.getActualFirstNo());
                MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();

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

                    //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                    MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.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 {
                    //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                    MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                    BeanUtils.copyProperties(mwca1, actualRow);
                }
                grossModel.setValueFlag(DbConstants.NET);
                grossModel.setUpdateManNo("system");
                grossModel.setUpdateManName("二次计量结净更新");
                grossModel.setUpdateTime(new Date());
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(grossModel);
                //更新第一次皮重数据
                tareModel.setValueFlag(DbConstants.NET);
                tareModel.setUpdateManNo("system");
                tareModel.setUpdateManName("二次计量结净更新");
                tareModel.setUpdateTime(new Date());
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(tareModel);

                String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + 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());
                }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());
                }
                meterWorkRailwayActualBakMapper.insertSelective(actualRow);
                rm.setSucceed();
                rm.setMessage("操作成功");
                return rm;
            }
            return rm;
        } else {
            rm.setMessage("当前火车车号在历史皮重表里未找到历史皮重信息，不能历史皮重结净，请确认！！");
            return rm;
        }
    }

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

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

            /*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()) + meterWorkRailwayActFirstBakMapper.getNewID(afl);
            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());
            mwcaf1.setValueFlag("2");
            mwcaf1.setCheckFlag("0");
            mwcaf1.setDataSource("4");
            mwcaf1.setMeterMode("3");
            mwcaf1.setIsPreSacale("0");
            int i = meterWorkRailwayActFirstBakMapper.insertSelective(mwcaf1);

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

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

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


    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageList<MeterWorkRailwayActFirstBak> 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<MeterWorkRailwayActFirstBak> mwrafs = meterWorkRailwayActFirstBakMapper.selectView(parmas);
        PageList pageInfo = new PageList(mwrafs);
        return pageInfo;
    }


    @Override
    @Transactional
    public PageList<MeterWorkRailwayActFirstBak> 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<MeterWorkRailwayActFirstBak> mwrafs = meterWorkRailwayActFirstBakMapper.selectDiff(railwayTypeNo, railwayTypeName);
        PageList pageInfo = new PageList(mwrafs);
        return pageInfo;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(MeterWorkRailwayActFirstBak model) {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数为空!!");
            }
            if (model.getPredictionNo() == null || "".equals(model.getPredictionNo())) {
                throw new MarkerMetException(500, "该条数据还未匹配！！");
            }

            PreRailwayScale pls = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
            if (pls.getPredictionNo().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);
            MeterWorkRailwayActFirstBak mwcaf = meterWorkRailwayActFirstBakMapper.selectAndRemove(model.getActualFirstNo());
            mwcaf.setValueFlag("1");
            mwcaf.setIsPreSacale("0");
            int num = meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(mwcaf);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    /**
     * 一次计量信息与预报信息进行匹配
     *
     * @param actualFirstNo
     * @param predictionNo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkRailwayActFirstBak match(String actualFirstNo, String predictionNo) {
        try {
            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, "该预报信息不存在");
            }
            prs.setValueFlag(DbConstants.IN); //使用中
            prs.setUploadFlag("1");
            MeterWorkRailwayActFirstBak meterWorkRailwayActFirst = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualFirstNo);
            if (meterWorkRailwayActFirst == null) {
                throw new MarkerMetException(500, "该计量作业信息不存在");
            }
            if (meterWorkRailwayActFirst.getPredictionNo() != null && !"".equals(meterWorkRailwayActFirst.getPredictionNo())) {
                throw new MarkerMetException(500, "该计量作业已匹配！！");
            }
            MeterWorkRailwayActFirstBak model = meterWorkRailwayActFirstBakMapper.selectAndMatch(actualFirstNo, predictionNo);
            model.setValueFlag("1");
            model.setUpdateTime(new Date());
            model.setUpdateManNo("system");
            model.setIsPreSacale("1");
            model.setUpdateManName("火车一次计量匹配操作");
            prs.setUpdateTime(new Date());
            prs.setUpdateManNo("system");
            prs.setUpdateManName("火车一次计量匹配操作");
            prs.setMeasureStation(model.getBaseSpotNo());
            prs.setMeasureStationName(model.getBaseSpotName());

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


//            prs.setNote(); 需要添加修改原因写入
            preRailwayScaleMapper.updateByPrimaryKeySelective(prs);
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model);
            return model;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageList<MeterWorkRailwayActFirstBak> 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<MeterWorkRailwayActFirstBak> mwrafs = meterWorkRailwayActFirstBakMapper.selectTa(parmas);
            PageList pageInfo = new PageList(mwrafs);
            return pageInfo;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MeterWorkRailwayActFirstBak> 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<MeterWorkRailwayActFirstBak> mwrafs = meterWorkRailwayActFirstBakMapper.selectNet(railwayNo, railwayTypeNo);
            return mwrafs;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MeterWorkRailwayActFirstBak> 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<MeterWorkRailwayActFirstBak> list = meterWorkRailwayActFirstBakMapper.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 = meterWorkRailwayActFirstBakMapper.analysis(obj);
            return list;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    private void check1(MeterWorkRailwayActFirstBak 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(MeterWorkRailwayActFirstBak 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(MeterWorkRailwayActFirstBak firstRail, MeterWorkRailwayActualBak 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) {
                    //补充一次数据
                    MeterWorkRailwayActFirstBak first = improveDataFirst(firstRail, prs);
                    first.setMeterBalanceWeight(first.getMeterWeight());
                    //补充结净数据
                    MeterWorkRailwayActualBak 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(MeterWorkRailwayActFirstBak firstRail, MeterWorkRailwayActualBak netRail) {
        //补充编号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(firstRail.getCreateTime());
        //一次数据
        String firstNo = meterWorkRailwayActFirstBakMapper.getNewID(afl);
        firstRail.setActualFirstNo(afl + firstNo);

//        String firstNoNew= PublicMethod.IdCreate(2);
//        firstRail.setActualFirstNo(firstNoNew);
        //结净数据
        String netNo = meterWorkRailwayActualBakMapper.getNewID(afl);
        netRail.setActualNo(afl + netNo);
        netRail.setUploadFlag("1");
//        String netNoNew= PublicMethod.IdCreate(2);
//        netRail.setActualNo(netNoNew);
        firstRail.setCreateTime(new Date());
        meterWorkRailwayActFirstBakMapper.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());
        }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());
        }
        meterWorkRailwayActualBakMapper.insertSelective(netRail);
        //修改预报状态
        return firstRail.getActualFirstNo();
    }

    //结净补充数据
    private MeterWorkRailwayActualBak improveDataActual(MeterWorkRailwayActualBak 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 MeterWorkRailwayActFirstBak improveDataFirst(MeterWorkRailwayActFirstBak 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(MeterWorkRailwayActFirstBak model) throws Exception {
        BigDecimal zero = new BigDecimal(0);
        //String id = "";//需要返回的作业编号
        HashMap<String, Object> map = new HashMap<>();
        List<MeterWorkRailwayActFirstBak> firsts = new ArrayList<>();
        List<MeterWorkRailwayActualBak> 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() + "】已经使用（已出净重），不允许再次使用，请重新选择预报或者进行复磅!");
                    }
                }

            }

            //验证当前使用的预报号与一次计量的预报号是否一致
            MeterWorkRailwayActFirstBak searchFirst = new MeterWorkRailwayActFirstBak();
            searchFirst.setRailwayNo(model.getRailwayNo());
            searchFirst.setValueFlag("1");
            Map<String, Object> mapSearchFirst = FieldsCollector.beanToMap(searchFirst);
            List<MeterWorkRailwayActFirstBak> searchFirstList = meterWorkRailwayActFirstBakMapper.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();
                        MeterWorkRailwayActFirstBak first01 = searchFirstList.get(0);
                        //将委托中的编号和收发货的等数据拷贝进一次数据中
                        MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.selectAndMatch(first01.getActualFirstNo(), predictionNo01);
                        BeanUtils.copyProperties(first1, first01);

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

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

                //皮重计量实体的组装；皮重数据保存
                MeterWorkRailwayActFirstBak railwayActFirstTare = new MeterWorkRailwayActFirstBak();
                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);
                }
                MeterWorkRailwayActualBak actual = new MeterWorkRailwayActualBak();
                if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                    //净重数据保存
                    PreRailwayScale preRailwayScale = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
                    actual = doAddWfStaticNet(preRailwayScale, model, railwayActFirstTare); //预报，毛重，皮重
                } else {
                    actual = doAddWfStaticNetNot(model, railwayActFirstTare);

                }

                railwayActFirstTare.setValueFlag("0");
                meterWorkRailwayActFirstBakMapper.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 {
            //===========二次计量处理开始==============
            MeterWorkRailwayActualBak actual = new MeterWorkRailwayActualBak();
            MeterWorkRailwayActFirstBak first = new MeterWorkRailwayActFirstBak();
            for (MeterWorkRailwayActFirstBak mwraf : mwrafs) {

                //保存第二次的一次计量数据；特别说明：先皮后毛不会做分检，所以不需要考虑
                MeterWorkRailwayActFirstBak meterWorkRailwayActFirst = new MeterWorkRailwayActFirstBak();
                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(meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(id));
        }catch(Exception e){
            e.printStackTrace();
            //throw new MarkerMetException(500, "保存图片失败");
        }*/
        rm.setData(map);

        return rm;
    }

    @Override
    public RESTfulResult doAddWfStaticTwo(PreRailwayScale scale, MeterWorkRailwayActFirstBak
            grossModel, MeterWorkRailwayActFirstBak tareModel) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

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

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

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

            MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();
            //毛重一次
            MeterWorkRailwayActFirstBak model01 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(grossModel.getActualFirstNo());
            //皮重一次
            MeterWorkRailwayActFirstBak model02 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(tareModel.getActualFirstNo());

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

                //将委托中的编号和收发货的等数据拷贝进一次数据中
                MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(first1, model01);
                MeterWorkRailwayActFirstBak first2 = meterWorkRailwayActFirstBakMapper.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");
                meterWorkRailwayActFirstBakMapper.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");
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model02);
                doSaveTare(model02);

                MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAddPre(model01.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
                String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + 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());
                }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());
                }
                meterWorkRailwayActualBakMapper.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");
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model01);
                //修改皮重一次数据状态
                model02.setValueFlag(DbConstants.NET);
                model02.setUpdateManNo("system");
                model02.setUpdateManName("自动匹配更新");
                model02.setUpdateTime(new Date());
                model02.setWeightType("1");
                model02.setIsPreSacale("0");
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model02);
                doSaveTare(model02);

                MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAdd(model01.getActualFirstNo(), model02.getActualFirstNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
                String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + 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());
                }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());
                }
                meterWorkRailwayActualBakMapper.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();
            rm.setMessage("操作异常，请确认！>>>" + e.getMessage());
        }
        return rm;

    }

    public void doSaveTare(MeterWorkRailwayActFirstBak 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());
            //  (之前的重量*次数)/(次数+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());
            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.setMeterWeight(model.getMeterWeight().longValue());
        meterBaseRailwayHisTareDetailMapper.insert(mbhtd);
    }


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

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

    //混装预报补充数据
    private MeterWorkRailwayActFirstBak doAddWfStaticFristImp(PreRailwayScale prs, MeterWorkRailwayActFirstBak 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 = meterWorkRailwayActFirstBakMapper.getNewID(afl);
        firstRail.setActualFirstNo(afl + firstNo);
        firstRail.setValueFlag("1");
        firstRail.setCheckFlag("0");
        firstRail.setCreateTime(new Date());
        meterWorkRailwayActFirstBakMapper.insertSelective(firstRail);
        return firstRail;
    }


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

        String netNo = meterWorkRailwayActualBakMapper.getNewID(afl);
        netRail.setActualNo(afl + 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());
                }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());
                }
                meterWorkRailwayActualBakMapper.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());
        }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());
        }
        meterWorkRailwayActualBakMapper.insertSelective(netRail);


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

    /**
     * 需要两次计量时完善结净数据
     *
     * @param one 毛重实体
     * @param two 皮重实体
     * @return
     */
    private MeterWorkRailwayActualBak doAddWfStaticNetNot(MeterWorkRailwayActFirstBak one, MeterWorkRailwayActFirstBak two) {
        MeterWorkRailwayActualBak netRail = new MeterWorkRailwayActualBak();
        //补充编号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(new Date());
        String netNo = meterWorkRailwayActualBakMapper.getNewID(afl);
        netRail.setActualNo(afl + 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());
                }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());
                }
                meterWorkRailwayActualBakMapper.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());
        }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());
        }
        meterWorkRailwayActualBakMapper.insertSelective(netRail);


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

    /**
     * 图片保存信息
     *
     * @param oci
     * @throws Exception
     */
    @Transactional(rollbackFor = Exception.class)
    public void SaveImg(MeterWorkRailwayActFirstBak 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, MeterWorkRailwayActFirstBak 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、补存一条一次皮重数据
        MeterWorkRailwayActFirstBak tareWeight = new MeterWorkRailwayActFirstBak();
        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;
    }

    //覆盖期限皮重
    private void updateRailWeight(MeterWorkRailwayActFirstBak 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) {
            e.printStackTrace();
        }
    }


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

    @Override
    public RESTfulResult doAddRailWf(List<MeterWorkRailwayActFirstBak> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            int count = 0;
            for (MeterWorkRailwayActFirstBak model: models) {
                if(model.getCreateTime() == null || StringUtils.isEmpty(model.getCreateTime().toString())) {
                    rm.setMessage("计量时间不能为空");
                    return rm;
                }
            }
            for (MeterWorkRailwayActFirstBak model : models) {
                //若存在相同车号的数据，则将之前的数据状态还原，然后再删除一次计量表的数据，之后再新增一次计量表数据，以及修改数据状态
                Map<String, Object> mp = new HashMap<>();
                mp.put("railwayNo", model.getRailwayNo());
                mp.put("valueFlag", "1");
                mp.put("railwayTypeNo", "001019002");
                List<MeterWorkRailwayActFirstBak> ll = meterWorkRailwayActFirstBakMapper.selectByParameters(mp);
                if (ll != null && ll.size() > 0) {
                    for (MeterWorkRailwayActFirstBak fs : ll) {
                        meterWorkRailwayActFirstBakMapper.deleteByPrimaryKey(fs.getActualFirstNo());
                        preRailwayScaleMapper.updateMeterWorkRailwayReceiveds(fs.getActualFirstNo());
                    }
                }
                meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(model.getActualFirstNo());
                MeterWorkRailwayActFirstBak mwrac = model;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String afl = sdf.format(new Date());
                String netNo = meterWorkRailwayActFirstBakMapper.getNewID(afl);
                mwrac.setActualFirstNo(afl + netNo);
//                mwrac.setCreateTime(new Date());
                meterWorkRailwayActFirstBakMapper.insertSelective(mwrac);
                count++;
            }
            /*int count = 0;
            for(MeterWorkRailwayActFirstBak model: models) {
                model.setWeightType("1");
                model.setCreateTime(new Date());
                int i = meterWorkRailwayActFirstBakMapper.insertSelective(model);
                int j = meterWorkRailwayMotorialReceive.update
                if(i >= 1) {
                    count++;
                }
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功,总共" + count + "条及记录转换成功！！");*/
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功,总共" + count + "条及记录转换成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！！>>>" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return rm;
    }

    @Override
    public RESTfulResult doAddRailNetWf
            (List<MeterWorkRailwayMotorialReceived> grossModels, List<MeterWorkRailwayActFirstBak> 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 (MeterWorkRailwayActFirstBak first : tareModels) {
                    //两个车号一样则匹配，否则则不匹配
                    if (first.getRailwayNo().equals(received.getRailwayNo())) {
                        //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                        preRailwayScaleMapper.updateMeterWorkRailwayReceived(received.getActualFirstNo());

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

                        //新增一条毛重信息
                        MeterWorkRailwayActFirstBak second = new MeterWorkRailwayActFirstBak();
                        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());
                        meterWorkRailwayActFirstBakMapper.insertSelective(second);


                        //新增一条结净数据
                        MeterWorkRailwayActualBak netRail = new MeterWorkRailwayActualBak();
                        String netNo = meterWorkRailwayActualBakMapper.getNewID(afl);
                        netRail.setActualNo(afl + 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(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());
                        }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());
                        }
                        int i = meterWorkRailwayActualBakMapper.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
    public RESTfulResult doNetAddMotoriaRailway
            (MeterWorkRailwayMotorialReceived received, MeterWorkRailwayActFirstBak 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");
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(first);

                    //新增一条毛重信息
                    MeterWorkRailwayActFirstBak second = new MeterWorkRailwayActFirstBak();
                    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());
                    meterWorkRailwayActFirstBakMapper.insertSelective(second);

                    MeterWorkRailwayActualBak netRail = new MeterWorkRailwayActualBak();
                    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("匹配结净");
                    meterWorkRailwayActFirstBakMapper.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("匹配结净");
                    meterWorkRailwayActFirstBakMapper.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");
                    meterWorkRailwayActualBakMapper.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("匹配结净");
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(first);

                    //新增一条毛重信息
                    MeterWorkRailwayActFirstBak second = new MeterWorkRailwayActFirstBak();
                    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("匹配结净");
                    meterWorkRailwayActFirstBakMapper.insertSelective(second);

                    //新增一条结净数据
                    MeterWorkRailwayActualBak netRail = new MeterWorkRailwayActualBak();
                    String netNo = meterWorkRailwayActualBakMapper.getNewID(afl);
                    netRail.setActualNo(afl + 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());
                    }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());
                    }
                    meterWorkRailwayActualBakMapper.insertSelective(netRail);
                }
            }

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

    @Override
    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())) {
                        MeterWorkRailwayActFirstBak AIFirst = new MeterWorkRailwayActFirstBak();
                        //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                        preRailwayScaleMapper.updateMeterWorkRailwayReceived(received.getActualFirstNo());

                        //将一次表中的皮重设置为已结净
                        String firstNo = afl + meterWorkRailwayActFirstBakMapper.getNewID(afl);
                        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());
                        meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(AIFirst);

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

                        //新增一条毛重信息
                        MeterWorkRailwayActFirstBak second = new MeterWorkRailwayActFirstBak();
                        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());
                        meterWorkRailwayActFirstBakMapper.insertSelective(second);


                        //新增一条结净数据
                        MeterWorkRailwayActualBak netRail = new MeterWorkRailwayActualBak();
                        String netNo = meterWorkRailwayActualBakMapper.getNewID(afl);
                        netRail.setActualNo(afl + 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());
                        }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());
                        }
                        int i = meterWorkRailwayActualBakMapper.insertSelective(netRail);

                        if (i >= 1) {
                            meterWorkRailwayActFirstBakMapper.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
    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())) {
                    MeterWorkRailwayActFirstBak AIFirst = new MeterWorkRailwayActFirstBak();
                    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 + meterWorkRailwayActFirstBakMapper.getNewID(afl);
                    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());
                    meterWorkRailwayActFirstBakMapper.insertSelective(AIFirst);

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

                    MeterWorkRailwayActFirstBak second = new MeterWorkRailwayActFirstBak();
                    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());
                    meterWorkRailwayActFirstBakMapper.insertSelective(second);

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

                    AIFirst.setRailwaySpeed(first.getRailwaySpeed());
                    AIFirst.setConveyanceType(first.getRailwayType());
                    meterWorkRailwayActFirstBakMapper.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("标识皮匹配结净");
                    meterWorkRailwayActFirstBakMapper.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");
                    meterWorkRailwayActualBakMapper.updateByPrimaryKeySelective(netRail);

                    meterWorkRailwayActFirstBakMapper.deleteByPrimaryKey(firstNo);

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

                    //将一次表中的皮重设置为已结净
                    String firstNo = afl + meterWorkRailwayActFirstBakMapper.getNewID(afl);
                    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());
                    meterWorkRailwayActFirstBakMapper.insertSelective(AIFirst);
                    //作废AI标志皮
                    first.setValueFlag("1");
                    meterBaseRailwayAiweightMapper.updateByPrimaryKeySelective(first);

                    //新增一条毛重信息
                    MeterWorkRailwayActFirstBak second = new MeterWorkRailwayActFirstBak();
                    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("标识皮匹配结净");
                    meterWorkRailwayActFirstBakMapper.insertSelective(second);

                    //新增一条结净数据
                    MeterWorkRailwayActualBak netRail = new MeterWorkRailwayActualBak();
                    String netNo = meterWorkRailwayActualBakMapper.getNewID(afl);
                    netRail.setActualNo(afl + 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());
                    }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());
                    }
                    int i = meterWorkRailwayActualBakMapper.insertSelective(netRail);
                    if(i >= 1) {
                        meterWorkRailwayActFirstBakMapper.deleteByPrimaryKey(firstNo);
                    }
                }
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功,匹配AI标识皮结净成功！！");
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！！>>>" + e.getMessage());
        }
        return rm;
    }

    ///动态衡标准皮结净
    @Override
    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";
            }
            MeterWorkRailwayActFirstBak first = new MeterWorkRailwayActFirstBak();
            BeanUtils.copyProperties(received, first);

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

                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 + meterWorkRailwayActFirstBakMapper.getNewID(afl));
                    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());
                    meterWorkRailwayActFirstBakMapper.insertSelective(AIFirst);


                    MeterWorkRailwayActFirstBak second = new MeterWorkRailwayActFirstBak();
                    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("标准皮匹配结净");
                    meterWorkRailwayActFirstBakMapper.insertSelective(second);

                    MeterWorkRailwayActualBak netRail = new MeterWorkRailwayActualBak();
                    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("标准皮匹配结净");
                    meterWorkRailwayActFirstBakMapper.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");
                    meterWorkRailwayActualBakMapper.updateByPrimaryKeySelective(netRail);

                    meterWorkRailwayActFirstBakMapper.deleteByPrimaryKey(AIFirst.getActualFirstNo());

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

                    //将一次表中的皮重设置为已结净
                    String firstNo = afl + meterWorkRailwayActFirstBakMapper.getNewID(afl);
                    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());
                    meterWorkRailwayActFirstBakMapper.insertSelective(AIFirst);

                    //新增一条毛重信息
                    MeterWorkRailwayActFirstBak second = new MeterWorkRailwayActFirstBak();
                    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("标准皮匹配结净");
                    meterWorkRailwayActFirstBakMapper.insertSelective(second);

                    //新增一条结净数据
                    MeterWorkRailwayActualBak netRail = new MeterWorkRailwayActualBak();
                    String netNo = meterWorkRailwayActualBakMapper.getNewID(afl);
                    netRail.setActualNo(afl + 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());
                    }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());
                    }
                    meterWorkRailwayActualBakMapper.insertSelective(netRail);

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

    //一次记录进行结净，根据scale进行有委托或者无委托结净
    public void doMatch(MeterWorkRailwayActFirstBak grossModel, MeterWorkRailwayActFirstBak tareModel, PreRailwayScale scale, MeterWorkRailwayActualBak actualRow) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        if(scale != null) {
            MeterWorkRailwayActFirstBak actFirst01 = meterWorkRailwayActFirstBakMapper.selectAndMatch(grossModel.getActualFirstNo(), scale.getPredictionNo());
            BeanUtils.copyProperties(actFirst01, grossModel);
            MeterWorkRailwayActFirstBak actFirst02 = meterWorkRailwayActFirstBakMapper.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");
            meterWorkRailwayActFirstBakMapper.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");
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(tareModel);

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

            //添加结净数据
            actualRow.setActualNo(sdf.format(new Date()) + meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date())));
            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());
            }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());
            }
            meterWorkRailwayActualBakMapper.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");
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(grossModel);
            //更新第一次皮重数据库
            tareModel.setValueFlag(DbConstants.NET);
    //        tareModel.setUpdateManNo("system");
    //        tareModel.setUpdateManName("自助计量结净更新");
    //        tareModel.setUpdateTime(new Date());
            tareModel.setWeightType("1");
            tareModel.setIsPreSacale("0");
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(tareModel);

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

            //添加结净数据
            String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow.setActualNo(sdf.format(new Date()) + 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());
            }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());
            }
            meterWorkRailwayActualBakMapper.insertSelective(actualRow);
        }
    }

    @Override
    public RESTfulResult selfServiceMeasurement(MeterWorkRailwayActFirstBak first) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        HashMap<String, Object> resultMap = new HashMap<>();
        List<MeterWorkRailwayActFirstBak> firsts = new ArrayList<>();
        List<MeterWorkRailwayActualBak> 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 + meterWorkRailwayActFirstBakMapper.getNewID(afl));
                first.setRailwayNo("无车号");
                first.setCreateTime(new Date());
                int i = meterWorkRailwayActFirstBakMapper.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<MeterWorkRailwayActFirstBak> firstList = meterWorkRailwayActFirstBakMapper.selectByParameters(map);
                if (firstList.size() >= 1) {
                    if (firstList.size() == 1) {
                        first.setActualFirstNo(afl + meterWorkRailwayActFirstBakMapper.getNewID(afl));
                        first.setCreateTime(new Date());
                        meterWorkRailwayActFirstBakMapper.insertSelective(first);
                        BigDecimal num = (firstList.get(0).getMeterWeight().subtract(first.getMeterWeight())).abs();
                        //结净数据不能小于10t
                        if (num.compareTo(new BigDecimal(10000)) == -1) {
                            first.setActualFirstNo(afl + meterWorkRailwayActFirstBakMapper.getNewID(afl));
                            first.setCreateTime(new Date());
                            int i = meterWorkRailwayActFirstBakMapper.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<MeterWorkRailwayActualBak> models = meterWorkRailwayActualBakMapper.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 + meterWorkRailwayActFirstBakMapper.getNewID(afl));
                        first.setCreateTime(new Date());
                        int i = meterWorkRailwayActFirstBakMapper.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 + meterWorkRailwayActFirstBakMapper.getNewID(afl));
                    first.setCreateTime(new Date());
                    int i = meterWorkRailwayActFirstBakMapper.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;
    }


    public String matchPreClean(MeterWorkRailwayActFirstBak grossModel, MeterWorkRailwayActFirstBak tareModel) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String sfl = sdf.format(new Date());
        MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();

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

                PreRailwayScale scale = railwayScales.get(0);
                MeterWorkRailwayActFirstBak actFirst01 = meterWorkRailwayActFirstBakMapper.selectAndMatch(grossModel.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(actFirst01, grossModel);
                MeterWorkRailwayActFirstBak actFirst02 = meterWorkRailwayActFirstBakMapper.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");
                meterWorkRailwayActFirstBakMapper.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");
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(tareModel);

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

                //添加结净数据
                String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + 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());
                }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());
                }
                meterWorkRailwayActualBakMapper.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");
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(grossModel);
                //更新第一次皮重数据库
                tareModel.setValueFlag(DbConstants.NET);
                tareModel.setUpdateManNo("system");
                tareModel.setUpdateManName("自助计量结净更新");
                tareModel.setUpdateTime(new Date());
                tareModel.setWeightType("1");
                tareModel.setIsPreSacale("0");
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(tareModel);

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

                //添加结净数据
                String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + 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());
                }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());
                }
                meterWorkRailwayActualBakMapper.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");
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(grossModel);
            //更新第一次皮重数据库
            tareModel.setValueFlag(DbConstants.NET);
            tareModel.setUpdateManNo("system");
            tareModel.setUpdateManName("自助计量结净更新");
            tareModel.setUpdateTime(new Date());
            tareModel.setWeightType("1");
            tareModel.setIsPreSacale("0");
            meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(tareModel);

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

            //添加结净数据
            String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
            actualRow.setActualNo(sdf.format(new Date()) + 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());
            }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());
            }
            meterWorkRailwayActualBakMapper.insertSelective(actualRow);
            return "计量完成，车号" + grossModel.getRailwayNo() + "无委托结净";
        }
    }

    @Override
        public RESTfulResult batchUpdateLcNo(List<MeterWorkRailwayActFirstBak> models) {
            RESTfulResult rm = new RESTfulResult();
            rm.setFailed();
            try {
                if(!CollectionUtils.isEmpty(models)){
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                    String afl = sdf.format(new Date());
                    String lcNo = meterWorkRailwayActFirstBakMapper.getLcNo(afl, models.get(0).getBaseSpotNo());
                    int count = 0;
                    for (MeterWorkRailwayActFirstBak model : models) {
                        model.setMeterWeight(null);
                        model.setLcNo(model.getBaseSpotName().replaceAll("#", "")
                                .replaceAll("轨道衡", "")
                                .replaceAll("静轨", "")
                                .replaceAll("静态", "") + "-" + afl + lcNo + "(" + models.size() + ")");
                        meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model);
                        count++;
                    }
                rm.setSucceed();
                rm.setCode("200");
                rm.setData(count);
                rm.setMessage("操作成功,总共" + count + "条数据修改列次编号成功!!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常！！>>>" + e.getMessage());
        }
        return rm;
    }


    //静轨标识皮结净
    @Override
    public RESTfulResult doIdentifyWeight(MeterWorkRailwayActFirstBak model) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

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

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

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

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

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

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

                    //将委托中的编号和收发货的等数据拷贝进一次数据中
                    MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.selectAndMatch(model01.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(first1, model01);
//                    MeterWorkRailwayActFirstBak first2 = meterWorkRailwayActFirstBakMapper.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");
                    meterWorkRailwayActFirstBakMapper.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");
//                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model02);

                    MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAddPre(model01.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(mwca1, actualRow);
                    //保存结净
                    String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    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());
                    }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());
                    }
                    meterWorkRailwayActualBakMapper.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);
                    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");
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(grossModel);
                //修改皮重一次数据状态
//                tareModel.setValueFlag(DbConstants.NET);
//                tareModel.setUpdateManNo("system");
//                tareModel.setUpdateManName("标志皮结净更新");
//                tareModel.setUpdateTime(new Date());
//                tareModel.setWeightType("1");
//                tareModel.setIsPreSacale("0");
//                meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(tareModel);

                MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
                String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + no);
                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());
                }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());
                }
                meterWorkRailwayActualBakMapper.insertSelective(actualRow);
                firsts.add(grossModel);
            }
            actuals.add(actualRow);
//            meterWorkRailwayActFirstBakMapper.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();
            rm.setMessage("操作异常，请确认！>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    public PageList<MeterWorkRailwayActFirstBak> 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<MeterWorkRailwayActFirstBak> rows = meterWorkRailwayActFirstBakMapper.likeByDescTSDG(param);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    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), "请选择一次记录");
            MeterWorkRailwayActFirstBak grossModel = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(actualNo);
            Assert.isTrue(grossModel != null , "请选择一次记录");
            MeterWorkRailwayActFirstBak tareModel = new MeterWorkRailwayActFirstBak();
            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()));

            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();
            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")), "委托信息已使用或者已作废");
                //毛重一次
                MeterWorkRailwayActFirstBak model01 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(grossModel.getActualFirstNo());
                //皮重一次
                MeterWorkRailwayActFirstBak model02 = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(tareModel.getActualFirstNo());

                //将委托中的编号和收发货的等数据拷贝进一次数据中
                MeterWorkRailwayActFirstBak first1 = meterWorkRailwayActFirstBakMapper.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");
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model01);

                MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAddPre(model01.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
                String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + 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());
                }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());
                }
                meterWorkRailwayActualBakMapper.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");
                meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(grossModel);
                //修改皮重一次数据状态

                MeterWorkRailwayActualBak mwca1 = meterWorkRailwayActualBakMapper.manualAdd(grossModel.getActualFirstNo(), tareModel.getActualFirstNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存结净
                String no = meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + 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());
                }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());
                }
                meterWorkRailwayActualBakMapper.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.setSucceed();
        rm.setMessage("操作成功");
        rm.setCode("200");
        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(meterWorkRailwayActFirstBakMapper.batchMatchView(param));
        return rm;
    }

    @Override
    public List<MeterWorkRailwayActFirstBak> noPage(HashMap<String, Object> params) {
        List<MeterWorkRailwayActFirstBak> rows = meterWorkRailwayActFirstBakMapper.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) {
                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);

                    MeterWorkRailwayActFirstBak grossModel = new MeterWorkRailwayActFirstBak();
                    MeterWorkRailwayActFirstBak tareModel = new MeterWorkRailwayActFirstBak();
                    MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();
                    PreRailwayScale railwayScale = preRailwayScaleMapper.selectByPrimaryKey(receivedModel.getPredictionNo());
                    //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                    meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(receivedModel.getActualFirstNo());

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

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

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

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

                    //添加结净数据
                    actualRow.setActualNo(afl + meterWorkRailwayActualBakMapper.getNewID(afl));
                    //    String no = PublicMethod.IdCreate(3);
                    //    actualRow1.setActualNo(no);
                    actualRow.setTareWeight(tareModel.getMeterWeight());
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("AI标识皮匹配委托结净更新");
                    actualRow.setNetMode("2");
                    actualRow.setUploadFlag("0");
                    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());
                    }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());
                    }
                    meterWorkRailwayActualBakMapper.insertSelective(actualRow);

                    //更新预报数
                    railwayScale.setValueFlag(DbConstants.NET);
                    railwayScale.setUpdateTime(new Date());
                    railwayScale.setUpdateManNo("system");
                    railwayScale.setUpdateManName("AI标识皮匹配委托结净更新");
                    railwayScale.setUploadFlag("1");
                    railwayScale.setValueFlag("2");
                    railwayScale.setPredictionType("1");
                    railwayScale.setMeasureStation(actualRow.getNetSpot3No());
                    railwayScale.setMeasureStationName(actualRow.getNetSpot3Name());
                    preRailwayScaleMapper.updateByPrimaryKeySelective(railwayScale);

                    count++;
                }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) {
                        MeterWorkRailwayRecMain mainModel = meterWorkRailwayRecMainMapper.selectByPrimaryKey(receivedModel.getRailwayWorkNo());
                        if(mainModel != null) {
                            mainModel.setIsDispose("1");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(mainModel);
                        }
                    }
                }
            }
            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();
            rm.setMessage("操作异常：>>>" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return rm;
    }

    @Override
    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) {
                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);

                    MeterWorkRailwayActFirstBak grossModel = new MeterWorkRailwayActFirstBak();
                    MeterWorkRailwayActFirstBak tareModel = new MeterWorkRailwayActFirstBak();
                    MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();
                    PreRailwayScale railwayScale = preRailwayScaleMapper.selectByPrimaryKey(receivedModel.getPredictionNo());
                    //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                    meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(receivedModel.getActualFirstNo());

                    //从表赋值grossModel、AI也赋值tareModel并修改重量
                    BeanUtils.copyProperties(receivedModel, grossModel);
                    grossModel.setLcNo(receivedModel.getRailwayModelNo());
                    meterWorkRailwayActFirstBakMapper.insertSelective(grossModel);
                    BeanUtils.copyProperties(grossModel, tareModel);
                    tareModel.setMeterWeight(new BigDecimal(carriageWeightModel.getCarriageWeight()));

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

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

                    //添加结净数据
                    actualRow.setActualNo(afl + meterWorkRailwayActualBakMapper.getNewID(afl));
                    //    String no = PublicMethod.IdCreate(3);
                    //    actualRow1.setActualNo(no);
                    actualRow.setTareWeight(tareModel.getMeterWeight());
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("标准皮配委托结净更新");
                    actualRow.setNetMode("2");
                    actualRow.setUploadFlag("0");
                    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());
                    }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());
                    }
                    meterWorkRailwayActualBakMapper.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);

                    count++;
                }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) {
                        MeterWorkRailwayRecMain mainModel = meterWorkRailwayRecMainMapper.selectByPrimaryKey(receivedModel.getRailwayWorkNo());
                        if(mainModel != null) {
                            mainModel.setIsDispose("1");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(mainModel);
                        }
                    }
                }
            }
           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();
            rm.setMessage("操作异常：>>>" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return rm;
    }

    @Override
    public RESTfulResult DynamicBatchMatchFirstWeight(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 receivedModel: receivedList) {
                HashMap obj = new HashMap();
                obj.put("railwayNo", receivedModel.getRailwayNo());
                obj.put("valueFlag", "1");
                obj.put("weightType", "1");
                List<MeterWorkRailwayActFirstBak> firstWeightList = meterWorkRailwayActFirstBakMapper.selectByParameters(obj);
                if(firstWeightList.size() == 1) {
                    MeterWorkRailwayActFirstBak firstWeightModel = firstWeightList.get(0);
                    //查找到一条有效的数据并且这条数据小于从表重量才进行结净，否则跳过
                    if(receivedModel.getMeterWeight().compareTo(firstWeightModel.getMeterWeight()) <= 0) {
                        continue;
                    }
                    MeterWorkRailwayActFirstBak grossModel = new MeterWorkRailwayActFirstBak();
                    MeterWorkRailwayActFirstBak tareModel = firstWeightModel;
                    MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();
                    PreRailwayScale railwayScale = preRailwayScaleMapper.selectByPrimaryKey(receivedModel.getPredictionNo());
                    //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                    meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(receivedModel.getActualFirstNo());

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

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

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

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

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

                    //添加结净数据
                    actualRow.setActualNo(afl + meterWorkRailwayActualBakMapper.getNewID(afl));
                    //    String no = PublicMethod.IdCreate(3);
                    //    actualRow1.setActualNo(no);
                    actualRow.setTareWeight(tareModel.getMeterWeight());
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("空皮配委托结净更新");
                    actualRow.setNetMode("2");
                    actualRow.setUploadFlag("0");
                    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());
                    }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());
                    }
                    meterWorkRailwayActualBakMapper.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);

                    count++;
                }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) {
                        MeterWorkRailwayRecMain mainModel = meterWorkRailwayRecMainMapper.selectByPrimaryKey(receivedModel.getRailwayWorkNo());
                        if(mainModel != null) {
                            mainModel.setIsDispose("1");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(mainModel);
                        }
                    }
                }
            }
            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();
            rm.setMessage("操作异常：>>>" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        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) {
                HashMap obj = new HashMap();
                obj.put("railwayNo", receivedModel.getRailwayNo());
                obj.put("valueFlag", "1");
                List<MeterBaseRailwayHisTareData> hisTareWeightList = meterBaseRailwayHisTareDataMapper.selectByParameters(obj);
                if(hisTareWeightList.size() >= 1) {
                    MeterBaseRailwayHisTareData hisTareWeightModel = hisTareWeightList.get(0);

                    MeterWorkRailwayActFirstBak grossModel = new MeterWorkRailwayActFirstBak();
                    MeterWorkRailwayActFirstBak tareModel = new MeterWorkRailwayActFirstBak();
                    MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();
                    PreRailwayScale railwayScale = new PreRailwayScale();
                    //已使用 METER_WORK_RAILWAY_RECEIVED表状态设置为已使用
                    meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(receivedModel.getActualFirstNo());

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

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

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

                    //添加结净数据
                    actualRow.setActualNo(afl + meterWorkRailwayActualBakMapper.getNewID(afl));
                    //    String no = PublicMethod.IdCreate(3);
                    //    actualRow1.setActualNo(no);
                    actualRow.setTareWeight(tareModel.getMeterWeight());
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("历史皮匹配委托结净更新");
                    actualRow.setNetMode("2");
                    actualRow.setUploadFlag("0");
                    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());
                    }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());
                    }
                    meterWorkRailwayActualBakMapper.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);

                    count++;
                }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) {
                        MeterWorkRailwayRecMain mainModel = meterWorkRailwayRecMainMapper.selectByPrimaryKey(receivedModel.getRailwayWorkNo());
                        if(mainModel != null) {
                            mainModel.setIsDispose("1");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(mainModel);
                        }
                    }
                }
            }
            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();
            rm.setMessage("操作异常：>>>" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return rm;
    }

    @Override
    public RESTfulResult WebBatchMatchidentifyWeight(List<HashMap> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            int count = 0;
            //将一次数据表的与标识皮进行结净
            for (HashMap model: models) {
                MeterWorkRailwayActFirstBak firstModel = meterWorkRailwayActFirstBakMapper.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) {

                    MeterWorkRailwayActFirstBak grossModel = new MeterWorkRailwayActFirstBak();
                    MeterWorkRailwayActFirstBak tareModel = new MeterWorkRailwayActFirstBak();
                    MeterWorkRailwayActualBak actualRow = new MeterWorkRailwayActualBak();
                    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");
                        meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(grossModel);

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

                        //添加结净数据
                        actualRow.setActualNo(afl + meterWorkRailwayActualBakMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.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("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());
                        }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());
                        }
                        meterWorkRailwayActualBakMapper.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");
                        meterWorkRailwayActFirstBakMapper.updateByPrimaryKey(grossModel);

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

                        //添加结净数据
                        actualRow.setActualNo(afl + meterWorkRailwayActualBakMapper.getNewID(afl));
                        //    String no = PublicMethod.IdCreate(3);
                        //    actualRow1.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.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());
                        }
                        meterWorkRailwayActualBakMapper.insertSelective(actualRow);
                    }
                    count++;
                }
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功,总共"+ models.size() +"条数据，共有：" + count + "条数据匹配标识皮成功");
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常：>>>" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return rm;
    }

    @Override
    public RESTfulResult saveAutoScale(CommonCombination<MeterWorkRailwayActFirstBak, MeterWorkRailwayActFirstBak, MeterWorkRailwayActualBak, MeterWorkRailwayScheduledHelp> com) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

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

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

            com.getOne().setActualFirstNo(sdf.format(new Date()) + meterWorkRailwayActFirstBakMapper.getNewID(sdf.format(new Date())));
            meterWorkRailwayActFirstBakMapper.insertSelective(com.getOne());
            firsts.add(com.getOne());
            if(com.getTwo() != null) {
                int i = meterWorkRailwayActFirstBakMapper.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()) + meterWorkRailwayActualBakMapper.getNewID(sdf.format(new Date())));
                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());
                }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());
                }
                int i = meterWorkRailwayActualBakMapper.insertSelective(com.getThree());
                MeterWorkRailwayActFirstBak tareModel = meterWorkRailwayActFirstBakMapper.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();
            rm.setMessage("操作异常：>>>" + e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常：>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult addFirstEmergency(MeterWorkRailwayActFirstBak model) throws Exception {
        try{
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("200");
            int num = 0;
//            if(StringUtils.isNotEmpty(model.getActualFirstNo())){
//                MeterWorkRailwayActFirstBak list = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(model.getActualFirstNo());
//            }
//            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
//                String afl = formatter.format(new Date());
//                String no = meterWorkRailwayActFirstBakMapper.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("静态轨道衡");
            meterWorkRailwayActFirstBakMapper.insertSelective(model);
            rm.setData(num);
            rm.setMessage("成功");
            return rm;
        }
        catch (Exception e){
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("500");
            rm.setData(null);
            rm.setMessage(e.getMessage());
            e.printStackTrace();
            return rm;
        }
    }

    @Override
    public RESTfulResult doNetEmergency(MeterWorkRailwayActualBak 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())), "一次数据编号不能为空");
            MeterWorkRailwayActFirstBak firstTemp = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(model.getActualFirst1No());
            MeterWorkRailwayActFirstBak secondTemp = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(model.getActualFirst2No());
            Assert.isTrue(!(Objects.isNull(firstTemp) || Objects.isNull(secondTemp)), "一次数据未查到有效的数据");

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

//            String no = PublicMethod.IdCreate(3);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String afl = formatter.format(new Date());
            String no = afl + meterWorkRailwayActualBakMapper.getNewID(afl);
            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());
            }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());
            }
            meterWorkRailwayActualBakMapper.insertSelective(actualModel);

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

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

        try{
            if(params.get("actualFirstNo") != null && com.steerinfo.util.StringUtils.isNotEmpty(params.get("actualFirstNo").toString())){
                MeterWorkRailwayActFirstBak model = meterWorkRailwayActFirstBakMapper.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.setWeightType("1");
                            meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model);
                            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;
                            }
                            meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(model);
                            rm.setSucceed();
                            rm.setMessage("毛皮转换成功!!");
                            rm.setData(model);
                            rm.setCode("200");
                            return rm;
                        }
                    }else{
                        rm.setMessage("毛皮转换失败：毛皮转换只针对有效的毛重和常规皮重，请确认！！");
                    }
                }else {
                    rm.setMessage("未找到相应的一次计量数据，不能毛皮转换！！");
                }
            }else {
                rm.setMessage("实绩编号不能为空！！");
            }
            return rm;
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！>>>" + e.getMessage());
            return rm;
        }
    }

    @Override
    public RESTfulResult CopyToFirst(List<String> ids, String railwayNo) throws Exception {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            for (String id : ids){
                //根据id获取对应的一次数据
                MeterWorkRailwayActFirstBak firstModel = meterWorkRailwayActFirstBakMapper.selectByPrimaryKey(id);
                //一次数据为空
                if (firstModel == null) {
                    //抛出异常
                    //事务回滚
                    throw new Exception("一次数据为空");
                }
//                if (!railwayNo.equals(firstModel.getRailwayNo())){
//                    throw new Exception(id+"的车号不一致");
//                }
                //判断是否符合要求
                String note = isSuit(firstModel);
                if (note.equals("1")){
                    //复制到First正式表里面
                    MeterWorkRailwayActFirst first = new MeterWorkRailwayActFirst();
                    //复制一份
                    BeanUtils.copyProperties(firstModel,first);
                    //修改主键
                    first.setActualFirstNo(first.getActualFirstNo()+"bak");
                    MeterWorkRailwayActFirst tempFirst = meterWorkRailwayActFirstMapper.selectByPrimaryKey(first.getActualFirstNo());
                    if(tempFirst != null) {
                        throw new Exception("一次数据已转成正式数据，不能重复转正式数据");
                    }
                    meterWorkRailwayActFirstMapper.insert(first);
                    try {
                        //新增日志
                        MeterBaseOperationLog logModel = new MeterBaseOperationLog();
                        logModel.setId(PublicMethod.IdCreate(2));
                        logModel.setCreateManNo(UserUtil.getCurrentUserId());
                        logModel.setCreateManName(UserUtil.getCurrentUserName());
                        logModel.setCreateTime(new Date());
                        if("001019001".equals(first.getRailwayTypeNo())){
                            logModel.setOperationPage("web端-静态轨道衡一次数据维护");
                        }else{
                            logModel.setOperationPage("web端-吊钩秤一次数据维护");
                        }
                        logModel.setOperationFunction("新增");
                        logModel.setOperationId(first.getActualFirstNo());
                        logModel.setOperationPredictionNo(first.getPredictionNo());
                        logModel.setOperationCarnNo(first.getRailwayNo());
                        meterBaseOperationLogMapper.insertSelective(logModel);
                    }catch (Exception e){
                        logger.error("web端-汽车衡一次数据维护日志新增失败");
                        e.printStackTrace();
                    }
                    firstModel.setValueFlag("0");
                    meterWorkRailwayActFirstBakMapper.updateByPrimaryKeySelective(firstModel);
                }else{
                    throw new Exception(note);
                }
            }
            rm.setSucceed();
            rm.setMessage("复制成功");
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        }
        return rm;
    }

    private String isSuit(MeterWorkRailwayActFirstBak model){

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

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

    //=========================eason add 2023-04-14=====================================
    @Override
    public RESTfulResult addFirstEmergencyList(List<MeterWorkRailwayActFirstBak> lt) throws Exception {
        List<DjPbModel> lst=new ArrayList<>();
        RESTfulResult rm = new RESTfulResult();
        rm.setCode("200");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(new Date());
        String no = meterWorkRailwayActFirstBakMapper.getNewID(afl);
        int i=Integer.parseInt(no);

        for(MeterWorkRailwayActFirstBak model:lt){
            String str=model.getActualFirstNo();

            model.setActualFirstNo(afl + String.format("%04d",i++));
            model.setValueFlag("1");
            //model.setMeterWeight(model.getMeterWeight().multiply(new BigDecimal(1000)));
            if(model.getCreateTime() == null || StringUtils.isEmpty(model.getCreateTime().toString())){
                model.setCreateTime(new Date());
            }

            if(StringUtils.isEmpty(model.getCreateManNo())||StringUtils.isEmpty(model.getCreateManName())) {
                String userId = null;
                String userName = null;
                try {
                    UserPayload payload = UserPayload.getCurrUser();
                    userId = payload.getUserCode();
                    userName = payload.getUserName();
                } catch (Exception e) {
                    userId = "admin";
                    userName = "admins";
                }
                model.setCreateManNo(userId);
                model.setCreateManName(userName);
            }

            try {
                add(model);

                DjPbModel dm = new DjPbModel();
                dm.setKey(str);
                dm.setValue(model.getActualFirstNo());
                lst.add(dm);
            }catch (Exception ex){
                System.out.println(ex.getMessage());
            }
        }

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

}
