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

import com.google.inject.internal.cglib.proxy.$UndeclaredThrowableException;
import com.steerinfo.baseinfo.meterbasematterinfo.mapper.MeterBaseMatterInfoMapper;
import com.steerinfo.baseinfo.meterbaserailwayaiweight.model.MeterBaseRailwayAiweight;
import com.steerinfo.baseinfo.meterbaserailwayhistaredata.mapper.MeterBaseRailwayHisTareDataMapper;
import com.steerinfo.baseinfo.meterbaserailwayhistaredata.model.MeterBaseRailwayHisTareData;
import com.steerinfo.baseinfo.meterbaserailwayhistaredetail.mapper.MeterBaseRailwayHisTareDetailMapper;
import com.steerinfo.baseinfo.meterbaserailwayhistaredetail.model.MeterBaseRailwayHisTareDetail;
import com.steerinfo.baseinfo.meterbaserailwayweight.mapper.MeterBaseRailwayWeightMapper;
import com.steerinfo.baseinfo.meterbaserailwayweight.model.MeterBaseRailwayWeight;
import com.steerinfo.baseinfo.meterbasevideoinfo.mapper.MeterBaseVideoinfoMapper;
import com.steerinfo.baseinfo.meterbasevideoinfo.model.MeterBaseVideoinfo;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.metermonitor.metermonitornote.model.MeterMonitorNote;
import com.steerinfo.meterwork.constants.DbConstants;
import com.steerinfo.meterwork.dto.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.meterworkimage.mapper.MeterWorkImageMapper;
import com.steerinfo.meterwork.meterworkimage.model.MeterWorkImage;
import com.steerinfo.meterwork.meterworkrailwayactfirst.mapper.MeterWorkRailwayActFirstMapper;
import com.steerinfo.meterwork.meterworkrailwayactfirst.model.MeterWorkRailwayActFirst;
import com.steerinfo.meterwork.meterworkrailwayactfirst.service.IMeterWorkRailwayActFirstService;
import com.steerinfo.meterwork.meterworkrailwayactual.mapper.MeterWorkRailwayActualMapper;
import com.steerinfo.meterwork.meterworkrailwayactual.model.MeterWorkRailwayActual;
import com.steerinfo.meterwork.meterworkrailwaymotorialreceived.model.MeterWorkRailwayMotorialReceived;
import com.steerinfo.meterwork.meterworkrailwayreceived.mapper.MeterWorkRailwayReceivedMapper;
import com.steerinfo.meterwork.meterworkrailwayreceived.model.MeterWorkRailwayReceived;
import com.steerinfo.meterwork.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.StringUtils;
import io.micrometer.core.instrument.Meter;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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


    @Resource
    private MeterWorkRailwayActFirstMapper meterWorkRailwayActFirstMapper;

    @Resource
    private IMeterWorkRailwayScheduledHelpService meterWorkRailwayScheduledHelpService;

    @Resource
    private MeterWorkRailwayScheduledHelpMapper meterWorkRailwayScheduledHelpMapper;

    @Resource
    private PreRailwayScaleMapper preRailwayScaleMapper;

    @Resource
    private MeterWorkRailwayReceivedMapper meterWorkRailwayReceivedMapper;

    @Resource
    private MeterWorkRailwayActualMapper meterWorkRailwayActualMapper;

    @Resource
    private MeterBaseRailwayHisTareDataMapper meterBaseRailwayHisTareDataMapper;

    @Resource
    private MeterBaseVideoinfoMapper meterBaseVideoinfoMapper;

    @Resource
    private MeterWorkImageMapper meterWorkImageMapper;

    @Resource
    private MeterBaseRailwayHisTareDetailMapper meterBaseRailwayHisTareDetailMapper;

    @Resource
    private MeterBaseRailwayWeightMapper meterBaseRailwayWeightMapper;

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

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

            check1(model);
            model.setDataSource("1");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String no = meterWorkRailwayActFirstMapper.getNewID(sdf.format(new Date()));
            model.setActualFirstNo(sdf.format(new Date()) + no);
            int i = meterWorkRailwayActFirstMapper.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 int checkandupdate(MeterWorkRailwayActFirst model) {
        try {
            check(model);
            if (model.getValueFlag().equals(DbConstants.NET)) {
                throw new MarkerMetException(500, "已经结净的数据无法修改！！");
            }
            int num = meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model);

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

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


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

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

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

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

            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");
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
                }
            }
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateCar(MeterWorkRailwayActFirst model) {
        try {
            ckeckcar(model);
            int num = meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    private void ckeckcar(MeterWorkRailwayActFirst 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.getId();
        String userName = payload.getUserName();
        model.setUpdateTime(new Date());
        model.setUpdateManNo(userId);
        model.setUpdateManName(userName);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> distinctRailwayNo(String railwayTypeNo, String lcNo, String railwayNo, String startTime, String endTime, String baseSpotNo,String matterName, String predictionNo,String forwardingUnitName,String receivingUintName,String valueFlag) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("railwayTypeNo", railwayTypeNo);
        obj.put("lcNo", lcNo);
        obj.put("railwayNo", railwayNo);
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        obj.put("baseSpotNo", baseSpotNo);
        obj.put("matterName", matterName);
        obj.put("predictionNo", predictionNo);
        obj.put("forwardingUnitName", forwardingUnitName);
        obj.put("receivingUintName", receivingUintName);
        obj.put("valueFlag", valueFlag);
        List<String> list = meterWorkRailwayActFirstMapper.distinctRailwayNo(obj).stream().map(MeterWorkRailwayActFirst::getRailwayNo).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<String> distinctLcNo(String railwayTypeNo,String startTime, String endTime){
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("railwayTypeNo", railwayTypeNo);
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        List<MeterWorkRailwayActFirst> templist = meterWorkRailwayActFirstMapper.distinctLcNo(obj);
        List<String> list = null;
        if(templist.size() != 0)
        {
            list = templist.stream().map(MeterWorkRailwayActFirst::getLcNo).distinct().collect(Collectors.toList());
        }
        return list;
    }

    @Override
    public RESTfulResult distinctLcNoAndRailwayNo(String railwayTypeNo, String startTime, String endTime, String railwayNo, String baseSpotName, String matterName, String predictionNo, String valueFlag) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            HashMap<String, Object> param = new HashMap<>();
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("railwayTypeNo", railwayTypeNo);
            obj.put("startTime", startTime);
            obj.put("endTime", endTime);
            obj.put("railwayNo", railwayNo);
            obj.put("baseSpotName", baseSpotName);
            obj.put("matterName", matterName);
            obj.put("predictionNo", predictionNo);
            obj.put("valueFlag", valueFlag);

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

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

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

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

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

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

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

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

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

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

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

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

                MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(first02.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存洁净
                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + no);
                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()));
                meterWorkRailwayActualMapper.insertSelective(actualRow);

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

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

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

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

                //保存洁净
                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + no);
                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()));
                meterWorkRailwayActualMapper.insertSelective(actualRow);
            }

            //存储当前计量的数据
            if(first03 != null) {
                String fristNo03 = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
                first03.setActualFirstNo(fristNo03);
                meterWorkRailwayActFirstMapper.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(MeterWorkRailwayActFirst one, MeterWorkRailwayActual two, PreRailwayScale three, PreRailwayScale four) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

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

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

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

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

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

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

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

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

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

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

            String no1 = meterWorkRailwayActualMapper.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()));
            meterWorkRailwayActualMapper.insertSelective(actualRow2);

            /*//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;
        }
    }

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

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

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

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

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

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

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

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

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

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

                    MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAdd(model01.getActualFirstNo(), model02.getActualFirstNo());
                    BeanUtils.copyProperties(mwca1, actualRow);
                    //保存洁净
                    String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("智能计量混装匹配洁净");
                    actualRow.setNetMode("4");
                    actualRow.setUploadFlag("1");
                    actualRow.setValueFlag("1");
                    actualRow.setIsPreSacale("0");
                    meterWorkRailwayActualMapper.insertSelective(actualRow);

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

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

                    //修改委托编号
                    scale.setValueFlag(DbConstants.NET);
                    scale.setUpdateTime(new Date());
                    scale.setUpdateManNo("system");
                    scale.setUpdateManName("智能计量混卸洁净更新");
                    scale.setUploadFlag("1");
                    scale.setPredictionType("6");
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);

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

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

                    //保存洁净
                    String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    actualRow.setNetTime(new Date());
                    actualRow.setNetMode(DbConstants.TYPE);
                    actualRow.setNote("智能计量混卸匹配洁净");
                    actualRow.setNetMode("6");
                    actualRow.setUploadFlag("1");
                    actualRow.setValueFlag("1");
                    actualRow.setIsPreSacale("0");
                    meterWorkRailwayActualMapper.insertSelective(actualRow);
                }

                //存储当前计量的数据
                if(flag.equals("0")) {
                    if(first01.getWeightType().equals("0")) {
                        first02.setUpdateManName("system");
                        first02.setUpdateManName("智能计量混卸一次复制");
                    }else {
                        first02.setUpdateManName("system");
                        first02.setUpdateManName("智能计量混装一次复制");
                    }
                    String firstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
                    first02.setActualFirstNo(firstNo);
                    first02.setPredictionNo("混装混卸复制");
                    first02.setValueFlag("1");
                    meterWorkRailwayActFirstMapper.insertSelective(first02);
                }else {
                    first02.setPredictionNo("");
                    meterWorkRailwayActFirstMapper.updateByPrimaryKeySelective(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;
            }else {
                if(scale == null){
                    rm.setMessage("未找到相应的委托信息,请确认！！");
                    return rm;
                }else {
                    //委托信息是否为有效数据
                    if(scale.getValueFlag().equals("2") || scale.getValueFlag().equals("3")){
                        rm.setMessage("委托信息已使用或者已作废！！");
                        return rm;
                    }
                }
                //获取两个一次重量的差值
                BigDecimal num01 = first01.getMeterWeight().subtract(first02.getMeterWeight()).abs();
                //判断两个一次车辆是否一致
                if(!first01.getRailwayNo().equals(first02.getRailwayNo())){
                    rm.setMessage("两条一次计量数据车号不一致,请确认！！");
                    return rm;
                }
                //洁净数据不能小于400
                if(num01.compareTo(BigDecimal.valueOf(400)) == -1){
                    rm.setMessage("选择的两条一次计量数据不能小于400kg，请确认！！");
                    return rm;
                }
                //一次数据是否为有效
                if(first01.getValueFlag().equals("2") || first01.getValueFlag().equals("0")){
                    rm.setMessage("选择的一次计量数据已作废或者已洁净，请确认！！");
                    return rm;
                }

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

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

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

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

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

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

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

                    MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(model01.getActualFirstNo(), model02.getActualFirstNo(), scale.getPredictionNo());
                    BeanUtils.copyProperties(mwca1, actualRow);
                    //保存洁净
                    String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    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()));
                    meterWorkRailwayActualMapper.insertSelective(actualRow);

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

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

                    //修改委托编号
                    scale.setValueFlag(DbConstants.NET);
                    scale.setUpdateTime(new Date());
                    scale.setUpdateManNo("system");
                    scale.setUpdateManName("智能计量混卸洁净更新");
                    scale.setUploadFlag("1");
                    scale.setPredictionType("6");
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);

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

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

                    //保存洁净
                    String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
                    actualRow.setActualNo(sdf.format(new Date()) + no);
                    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()));
                    meterWorkRailwayActualMapper.insertSelective(actualRow);
                }

                //存储当前计量的数据
                if(flag.equals("0")) {
                    if(first01.getWeightType().equals("0")) {
                        first02.setUpdateManName("system");
                        first02.setUpdateManName("智能计量混卸一次复制");
                    }else {
                        first02.setUpdateManName("system");
                        first02.setUpdateManName("智能计量混装一次复制");
                    }
                    String firstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
                    first02.setActualFirstNo(firstNo);
                    first02.setPredictionNo("混装混卸复制");
                    first02.setValueFlag("1");
                    meterWorkRailwayActFirstMapper.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, MeterWorkRailwayActFirst first01, MeterWorkRailwayActFirst first02) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        HashMap<String, Object> map = new HashMap<>();
        List<MeterWorkRailwayActFirst> firsts = new ArrayList<>();
        List<MeterWorkRailwayActual> actuals = new ArrayList<>();
        try{
            if(scale01 == null || scale02 == null){
                rm.setMessage("未找到相应的委托信息,请确认！！");
                return rm;
            }else {
                //委托信息是否为有效数据
                if(scale01.getValueFlag().equals("2") || scale02.getValueFlag().equals("2")){
                    rm.setMessage("委托信息已使用！！");
                    return rm;
                }
                if(scale01.getValueFlag().equals("3") || scale02.getValueFlag().equals("3")){
                    rm.setMessage("委托信息已作废！！");
                    return rm;
                }
                //判断双委托 物料相同、A委托的收货单位是B委托的发货单位 或者 B委托的收货单位是A委托的发货货单位
                if(!(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()) + meterWorkRailwayActFirstMapper.getNewID(afl);
            first02.setActualFirstNo(firstNo01);
            meterWorkRailwayActFirstMapper.insertSelective(first02);

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

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

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

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

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

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

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

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

            String no1 = meterWorkRailwayActualMapper.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()));
            meterWorkRailwayActualMapper.insertSelective(actualRow2);

            /*//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{
            //查询出一次计量数据
            MeterWorkRailwayActFirst mwcaf01 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirst mwcaf02 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirst mwcaf03 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirst mwcaf04 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo2);
            //查询出所有委托信息
            PreRailwayScale pts01 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo1);
            PreRailwayScale pts02 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo2);
            //定义两个洁净类
            MeterWorkRailwayActual actualRow1 = new MeterWorkRailwayActual();
            MeterWorkRailwayActual actualRow2 = new MeterWorkRailwayActual();
            //获取两个一次重量的差值
            BigDecimal num01 = mwcaf01.getMeterWeight().subtract(mwcaf03.getMeterWeight()).abs();
            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 "一次计量数据已作废，请确认！！";
            }
            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo1, predictionNo1);
            BeanUtils.copyProperties(first1,mwcaf01);
            MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo2, predictionNo1);
            BeanUtils.copyProperties(first2, mwcaf02);
            MeterWorkRailwayActFirst first3 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo1, predictionNo2);
            BeanUtils.copyProperties(first3,mwcaf03);
            MeterWorkRailwayActFirst first4 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo2, predictionNo2);
            BeanUtils.copyProperties(first4, mwcaf04);

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

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

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

            String no1 = meterWorkRailwayActualMapper.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()));
            meterWorkRailwayActualMapper.insertSelective(actualRow2);
        }catch(Exception e){
            e.printStackTrace();
            return "操作异常，请确认";
        }
        return "";
    }

    @Override
    public String mixedLoading(String actualNo1, String actualNo2, String actualNo3, String predictionNo1, String predictionNo2) {
        try{
            //查询所有的一次数据
            MeterWorkRailwayActFirst mwcaf1 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirst mwcaf2 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirst mwcaf3 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirst mwcaf4 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo3);
            //查询所有的委托数据
            PreRailwayScale pts1 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo1);
            PreRailwayScale pts2 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo2);
            //定义新的结净数据实体
            MeterWorkRailwayActual actualRow1 = new MeterWorkRailwayActual();
            MeterWorkRailwayActual actualRow2 = new MeterWorkRailwayActual();
            //判断是否为混装逻辑
            if(mwcaf1.getMeterWeight().compareTo(mwcaf2.getMeterWeight()) == 1 ||
                    mwcaf1.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == 1 ||
                    mwcaf2.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == 1){
                return "不为混装逻辑，请确认！！";
            }
            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
            String fromDate = simpleFormat.format(pts1.getCreateTime());
            String toDate = simpleFormat.format(pts2.getCreateTime());
            long from = simpleFormat.parse(fromDate).getTime();
            long to = simpleFormat.parse(toDate).getTime();
            int hours = (int) ((to - from) / (1000 * 60 * 60));
            if ( pts1.getMatterNo().equals(pts2.getMatterNo()) &&
                    pts1.getForwardingUnitNo().equals(pts2.getForwardingUnitNo()) &&
                    hours <= 4) {
                return "委托信息不符合混装委托规则，请确认!!";
            }
            //判断一次数据是否存在
            if (mwcaf1 == null || mwcaf2 == null || mwcaf3 == null || mwcaf4 == null) {
                return "未查询到相应的一次数据！！";
            }
            //判断一次数据是否已结净
            if (mwcaf1.getValueFlag().equals("2") || mwcaf2.getValueFlag().equals("2") ||
                    mwcaf3.getValueFlag().equals("2") || mwcaf4.getValueFlag().equals("2")) {
                return "存在已结净的一次数据！！";
            }
            //判断委托数据是否存在
            if (pts1 == null || pts2 == null) {
                return "未查询到相应的委托数据！！";
            }
            //判断委托数据是否已使用
            if (!pts1.getValueFlag().equals("1") || !pts1.getValueFlag().equals("1")) {
                return "委托不是未使用状态！！";
            }
            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo1, predictionNo1);
            BeanUtils.copyProperties(first1,mwcaf1);
            MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo2, predictionNo1);
            BeanUtils.copyProperties(first2, mwcaf2);
            MeterWorkRailwayActFirst first3 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo2, predictionNo2);
            BeanUtils.copyProperties(first3,mwcaf3);
            MeterWorkRailwayActFirst first4 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo3, predictionNo2);
            BeanUtils.copyProperties(first4, mwcaf4);

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

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

            //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
            MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAddPre(actualNo2, actualNo1, predictionNo1);
            BeanUtils.copyProperties(mwca1, actualRow1);
            MeterWorkRailwayActual mwca2 = meterWorkRailwayActualMapper.manualAddPre(actualNo3, actualFirstNo, predictionNo2);
            BeanUtils.copyProperties(mwca2,actualRow2);
            //添加结净数据
            String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
            actualRow1.setActualNo(sdf.format(new Date()) + no);
            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()));
            meterWorkRailwayActualMapper.insertSelective(actualRow1);

            String no1 = meterWorkRailwayActualMapper.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()));
            meterWorkRailwayActualMapper.insertSelective(actualRow2);
        }
        catch (Exception e){
            e.printStackTrace();
            return "操作异常，请确认";
        }
        return "";
    }

    @Override
    public String mixedDischarge(String actualNo1, String actualNo2, String actualNo3, String predictionNo1, String predictionNo2) {
        try{
            //查询所有的一次数据
            MeterWorkRailwayActFirst mwcaf1 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo1);
            MeterWorkRailwayActFirst mwcaf2 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirst mwcaf3 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo2);
            MeterWorkRailwayActFirst mwcaf4 = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualNo3);
            //查询所有的委托数据
            PreRailwayScale pts1 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo1);
            PreRailwayScale pts2 = preRailwayScaleMapper.selectByPrimaryKey(predictionNo2);
            //定义新的结净数据实体
            MeterWorkRailwayActual actualRow1 = new MeterWorkRailwayActual();
            MeterWorkRailwayActual actualRow2 = new MeterWorkRailwayActual();
            //判断是否为混装逻辑
            if(mwcaf1.getMeterWeight().compareTo(mwcaf2.getMeterWeight()) == -1 ||
                    mwcaf1.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == -1 ||
                    mwcaf2.getMeterWeight().compareTo(mwcaf4.getMeterWeight()) == -1){
                return "不为混卸逻辑！！";
            }
            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm");
            String fromDate = simpleFormat.format(pts1.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 "委托不是未使用状态！！";
            }
            //将委托中的编号和收发货的等数据拷贝进一次数据中
            MeterWorkRailwayActFirst first1 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo1, predictionNo1);
            BeanUtils.copyProperties(first1,mwcaf1);
            MeterWorkRailwayActFirst first2 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo2, predictionNo1);
            BeanUtils.copyProperties(first2, mwcaf2);
            MeterWorkRailwayActFirst first3 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo2, predictionNo2);
            BeanUtils.copyProperties(first3,mwcaf3);
            MeterWorkRailwayActFirst first4 = meterWorkRailwayActFirstMapper.selectAndMatch(actualNo3, predictionNo2);
            BeanUtils.copyProperties(first4, mwcaf4);

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

            //更新一次数据
            //更新第一次毛重数据
            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);
            meterWorkRailwayActFirstMapper.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);
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(mwcaf2);
            //添加第二次毛重数据
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.getNewID(afl);
            mwcaf3.setActualFirstNo(actualFirstNo);
            mwcaf3.setValueFlag(DbConstants.NET);
            mwcaf3.setUpdateManName("混卸洁净更新");
            mwcaf3.setUpdateTime(new Date());
            mwcaf3.setIsPreSacale("1");
            mwcaf3.setPredictionNo(predictionNo2);
            mwcaf3.setPredictionType("6");
            mwcaf3.setPredictionCombination(predictionNo1 + "," + predictionNo2);
            meterWorkRailwayActFirstMapper.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);
            meterWorkRailwayActFirstMapper.updateByPrimaryKey(mwcaf4);

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

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

            String no1 = meterWorkRailwayActualMapper.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()));
            meterWorkRailwayActualMapper.insertSelective(actualRow2);
        }
        catch (Exception e){
            e.printStackTrace();
            return "操作异常，请确认";
        }
        return "";
    }

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

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

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

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

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

                //更新预报数据
                scale.setPredictionNo(predictionNo);
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("二次计量洁净更新");
                scale.setUploadFlag("1");
                preRailwayScaleMapper.updateByPrimaryKeySelective(scale);

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

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

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

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

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

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

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

                    //更新预报数据
                    scale.setPredictionNo(predictionNo);
                    scale.setValueFlag(DbConstants.NET);
                    scale.setUpdateTime(new Date());
                    scale.setUpdateManNo("system");
                    scale.setUpdateManName("二次计量洁净更新");
                    scale.setUploadFlag("1");
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);

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

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

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

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

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

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

            //毛重
            String actualFirstNo = sdf.format(new Date()) + meterWorkRailwayActFirstMapper.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 = meterWorkRailwayActFirstMapper.insertSelective(mwcaf1);

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

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

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


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


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


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

            PreRailwayScale pls = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
            pls.setPredictionNo(model.getPredictionNo());
            pls.setUpdateManName("轨道衡一次计量解除委托操作");
            pls.setUpdateTime(new Date());
            pls.setValueFlag("1");
            pls.setUploadFlag("1");

            preRailwayScaleMapper.updateByPrimaryKeySelective(pls);
            MeterWorkRailwayActFirst mwcaf = meterWorkRailwayActFirstMapper.selectAndRemove(model.getActualFirstNo());
            mwcaf.setValueFlag("1");
            mwcaf.setIsPreSacale("0");
            int num = meterWorkRailwayActFirstMapper.updateByPrimaryKey(mwcaf);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    /**
     * 一次计量信息与预报信息进行匹配
     *
     * @param actualFirstNo
     * @param predictionNo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkRailwayActFirst match(String actualFirstNo, String predictionNo) {
        try {
            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");
            MeterWorkRailwayActFirst meterWorkRailwayActFirst = meterWorkRailwayActFirstMapper.selectByPrimaryKey(actualFirstNo);
            if (meterWorkRailwayActFirst == null) {
                throw new MarkerMetException(500, "该计量作业信息不存在");
            }
            if (meterWorkRailwayActFirst.getPredictionNo() != null && !"".equals(meterWorkRailwayActFirst.getPredictionNo())) {
                throw new MarkerMetException(500, "该计量作业已匹配！！");
            }
            MeterWorkRailwayActFirst model = meterWorkRailwayActFirstMapper.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("火车一次计量匹配操作");

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


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

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

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

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

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

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

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

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

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

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

//        String firstNoNew= PublicMethod.IdCreate(2);
//        firstRail.setActualFirstNo(firstNoNew);
        //洁净数据
        String netNo = meterWorkRailwayActualMapper.getNewID(afl);
        netRail.setActualNo(afl + netNo);
        netRail.setUploadFlag("1");
//        String netNoNew= PublicMethod.IdCreate(2);
//        netRail.setActualNo(netNoNew);

        meterWorkRailwayActFirstMapper.insertSelective(firstRail);
        meterWorkRailwayActualMapper.insertSelective(netRail);
        //修改预报状态
        return firstRail.getActualFirstNo();
    }

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

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

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

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

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

            }

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

        return rm;
    }

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

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

            BigDecimal num = grossModel.getMeterWeight().subtract(tareModel.getMeterWeight()).abs();
            if(num.compareTo(BigDecimal.valueOf(400)) == -1){
                rm.setMessage("选择的两条一次计量数据不能小于400kg，请确认！！");
                return rm;
            }
            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()) + meterWorkRailwayActFirstMapper.getNewID(afl);
            String flag = null;
            //存储当前计量的数据
            if(grossModel.getActualFirstNo() == null) {
                grossModel.setActualFirstNo(firstNo);
                meterWorkRailwayActFirstMapper.insertSelective(grossModel);
                flag = "0";
            }
            if(tareModel.getActualFirstNo() == null) {
                tareModel.setActualFirstNo(firstNo);
                meterWorkRailwayActFirstMapper.insertSelective(tareModel);
                flag = "1";
            }

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

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

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

                //修改委托编号
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("自动匹配更新");
                scale.setUploadFlag("1");
                scale.setPredictionType("1");
                preRailwayScaleMapper.updateByPrimaryKeySelective(scale);

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

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

                MeterWorkRailwayActual mwca1 = meterWorkRailwayActualMapper.manualAdd(model01.getActualFirstNo(), model02.getActualFirstNo());
                BeanUtils.copyProperties(mwca1, actualRow);
                //保存洁净
                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + no);
                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()));
                meterWorkRailwayActualMapper.insertSelective(actualRow);
            }

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

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

    }

    public void doSaveTare(MeterWorkRailwayActFirst model){
        //获取ID
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String afl = formatter.format(model.getCreateTime());
        //查找相应车辆有效的历史皮重信息
        MeterBaseRailwayHisTareData hisTareData = new MeterBaseRailwayHisTareData();
        hisTareData.setRailwayNo(model.getRailwayNo());
        hisTareData.setValueFlag("0");
        Map<String, Object> map = FieldsCollector.beanToMap(hisTareData);
        List<MeterBaseRailwayHisTareData> tareOne = meterBaseRailwayHisTareDataMapper.selectByParameters(map);
        if(tareOne.size() >= 1) {   //已有历史皮重
            hisTareData.setDataNo(tareOne.get(0).getDataNo());
            hisTareData.setBaseSpotNo(model.getBaseSpotNo());
            hisTareData.setBaseSpotName(model.getBaseSpotName());
            hisTareData.setMeterName(model.getMatterName());
            hisTareData.setMeterTime(model.getCreateTime());
            hisTareData.setMeterWeight(Math.round(model.getMeterWeight().doubleValue()));
            hisTareData.setUpWeight(model.getMeterWeight().longValue());
            //  (之前的重量*次数)/(次数+1)
            double meterWeight = (tareOne.get(0).getAvgWeight() * tareOne.get(0).getMeterNum() + tareOne.get(0).getMeterWeight()) / (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(MeterWorkRailwayActFirst model) {
        //此为第一次计量的数据
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(model.getCreateTime());
        //一次数据
        String firstNo = meterWorkRailwayActFirstMapper.getNewID(afl);
        model.setActualFirstNo(afl + firstNo);
        model.setValueFlag("1");
        model.setCheckFlag("0");
        model.setMeterMode("1");
        meterWorkRailwayActFirstMapper.insertSelective(model);
        return model.getActualFirstNo();
    }

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

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

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


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

        String netNo = meterWorkRailwayActualMapper.getNewID(afl);
        netRail.setActualNo(afl + netNo);

        //补充数据
        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
                meterWorkRailwayActualMapper.insertSelectiveBar(netRail);
            }
        }
        meterWorkRailwayActualMapper.insertSelective(netRail);


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

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

        //补充数据
        /*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.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
                meterWorkRailwayActualMapper.insertSelectiveBar(netRail);
            }
        }
        meterWorkRailwayActualMapper.insertSelective(netRail);


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

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

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


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

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

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

        //4、修改预报状态
        prs.setValueFlag("2");//已使用
        prs.setUploadFlag("1");
        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");
        preRailwayScaleMapper.updateByPrimaryKeySelective(prsM);


        return id;
    }

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


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

    @Override
    public RESTfulResult doAddRailWf(List<MeterWorkRailwayActFirst> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            int count = 0;
            for (MeterWorkRailwayActFirst model : models) {
                //若存在相同车号的数据，则将之前的数据状态还原，然后再删除一次计量表的数据，之后再新增一次计量表数据，以及修改数据状态
                Map<String,Object> mp=new HashMap<>();
                mp.put("railwayNo",model.getRailwayNo());
                mp.put("valueFlag","1");
                mp.put("railwayTypeNo","001019002");
                List<MeterWorkRailwayActFirst> ll=meterWorkRailwayActFirstMapper.selectByParameters(mp);
                if(ll!=null&&ll.size()>0){
                    for(MeterWorkRailwayActFirst fs:ll){
                        meterWorkRailwayActFirstMapper.deleteByPrimaryKey(fs.getActualFirstNo());
                        preRailwayScaleMapper.updateMeterWorkRailwayReceiveds(fs.getActualFirstNo());
                    }
                }
                meterWorkRailwayReceivedMapper.updateMeterWorkRailwayReceived(model.getActualFirstNo());
                MeterWorkRailwayActFirst mwrac = model;
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String afl = sdf.format(new Date());
                String netNo = meterWorkRailwayActFirstMapper.getNewID(afl);
                mwrac.setActualFirstNo(afl + netNo);
                meterWorkRailwayActFirstMapper.insertSelective(mwrac);
                count++;
            }
            /*int count = 0;
            for(MeterWorkRailwayActFirst model: models) {
                model.setWeightType("1");
                model.setCreateTime(new Date());
                int i = meterWorkRailwayActFirstMapper.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());
        }
        return rm;
    }

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

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

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

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


                        //新增一条洁净数据
                        MeterWorkRailwayActual netRail=new MeterWorkRailwayActual();
                        String netNo = meterWorkRailwayActualMapper.getNewID(afl);
                        netRail.setActualNo(afl + netNo);
                        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("0");
                        netRail.setUploadFlag("0");

                        netRail.setMeterTypeNo(received.getMeterTypeNo());
                        netRail.setMeterTypeName(received.getMeterTypeName());

                        int i  = meterWorkRailwayActualMapper.insertSelective(netRail);

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

    @Override
    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.getId();
                userName = payload.getUserName();
            }catch (Exception ex){}

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

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

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


                        //新增一条洁净数据
                        MeterWorkRailwayActual netRail=new MeterWorkRailwayActual();
                        String netNo = meterWorkRailwayActualMapper.getNewID(afl);
                        netRail.setActualNo(afl + netNo);
                        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());

                        int i  = meterWorkRailwayActualMapper.insertSelective(netRail);

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

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

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

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

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

                        List<MeterWorkRailwayActual> models = meterWorkRailwayActualMapper.selectByParameter(obj);
                        if(models.size() >= 1) {
                            actuals.add(models.get(0));
                        }else {
                            actuals.add(null);
                        }
                        resultMap.put("firsts", firsts);
                        resultMap.put("actuals", actuals);
                        rm.setData(resultMap);
                        return rm;
                    } else if (firstList.size() >= 2) {
                        first.setActualFirstNo(afl + meterWorkRailwayActFirstMapper.getNewID(afl));
                        int i = meterWorkRailwayActFirstMapper.insertSelective(first);
                        if (i >= 1) {
                            firsts.add(first);
                            railwayHelp.setHelpContent("计量完成，车号" + first.getRailwayNo() + "存在多个委托，无法自动匹配");
                            meterWorkRailwayScheduledHelpService.checkAdd(railwayHelp);
                            rm.setSucceed();
                            rm.setMessage("存在多个委托，无法自动匹配委托，自动保存一次数据成功！！");
                            resultMap.put("firsts", firsts);
                            resultMap.put("actuals", null);
                            rm.setData(resultMap);
                            return rm;
                        }
                    }
                } else {
                    first.setActualFirstNo(afl + meterWorkRailwayActFirstMapper.getNewID(afl));
                    int i = meterWorkRailwayActFirstMapper.insertSelective(first);
                    if (i >= 1) {
                        firsts.add(first);
                        railwayHelp.setHelpContent("计量完成，车号" + first.getRailwayNo() + "存在多条一次记录，无法自动匹配结净");
                        meterWorkRailwayScheduledHelpService.checkAdd(railwayHelp);
                        rm.setSucceed();
                        rm.setMessage("存在多条一次记录，无法自动匹配结净，自动保存一次数据成功！！");
                        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(MeterWorkRailwayActFirst grossModel, MeterWorkRailwayActFirst tareModel){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String sfl = sdf.format(new Date());
        MeterWorkRailwayActual actualRow = new MeterWorkRailwayActual();

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

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

                //更新预报数据
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("自助计量洁净更新");
                scale.setUploadFlag("1");
                scale.setPredictionType("1");
                preRailwayScaleMapper.updateByPrimaryKeySelective(scale);

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

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

                //添加结净数据
                String no = meterWorkRailwayActualMapper.getNewID(sdf.format(new Date()));
                actualRow.setActualNo(sdf.format(new Date()) + no);
                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()));
                meterWorkRailwayActualMapper.insertSelective(actualRow);
                return "计量完成，" + grossModel.getRailwayNo() + "与委托编号" + scale.getPredictionNo() +"计量结净";
            }else {
                return grossModel.getRailwayNo() + "车号在委托信息表含有多条有效委托信息,不允许匹配委托洁净,请确认！！";
            }
        }else {
            ///////////无委托自动计量

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

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

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

    @Override
    public RESTfulResult batchUpdateLcNo(List<MeterWorkRailwayActFirst> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
            String lcNo = sdf.format(new Date());
            int count = 0;
            for(MeterWorkRailwayActFirst model: models) {
                model.setLcNo(lcNo);
                meterWorkRailwayActFirstMapper.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;
    }

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