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

import cn.hutool.db.handler.HandleHelper;
import com.github.pagehelper.util.StringUtil;
import com.steerinfo.baseinfo.meterbaseoperationlog.mapper.MeterBaseOperationLogMapper;
import com.steerinfo.baseinfo.meterbaseoperationlog.model.MeterBaseOperationLog;
import com.steerinfo.baseinfo.meterbaserailwayaiweight.mapper.MeterBaseRailwayAiweightMapper;
import com.steerinfo.baseinfo.meterbaserailwayaiweight.model.MeterBaseRailwayAiweight;
import com.steerinfo.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.metermonitor.metermonitornote.mapper.MeterMonitorNoteMapper;
import com.steerinfo.metermonitor.metermonitornote.model.MeterMonitorNote;
import com.steerinfo.meterwork.meterworkrailwayactfirst.mapper.MeterWorkRailwayActFirstMapper;
import com.steerinfo.meterwork.meterworkrailwayactfirst.model.MeterWorkRailwayActFirst;
import com.steerinfo.meterwork.meterworkrailwayactual.model.MeterWorkRailwayActual;
import com.steerinfo.meterwork.meterworkrailwayreceived.model.MeterWorkRailwayReceived;
import com.steerinfo.meterwork.meterworkrailwayreceived.mapper.MeterWorkRailwayReceivedMapper;
import com.steerinfo.meterwork.meterworkrailwayreceived.service.IMeterWorkRailwayReceivedService;
import com.steerinfo.meterwork.meterworkrailwayrecmain.mapper.MeterWorkRailwayRecMainMapper;
import com.steerinfo.meterwork.meterworkrailwayrecmain.model.MeterWorkRailwayRecMain;
import com.steerinfo.pretrack.prerailwayscale.mapper.PreRailwayScaleMapper;
import com.steerinfo.pretrack.prerailwayscale.model.PreRailwayScale;
import com.steerinfo.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

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

    @Resource
    private MeterWorkRailwayReceivedMapper meterWorkRailwayReceivedMapper;

    @Resource
    private MeterWorkRailwayRecMainMapper meterWorkRailwayRecMainMapper;

    @Resource
    private MeterWorkRailwayActFirstMapper meterWorkRailwayActFirstMapper;

    @Resource
    private MeterBaseRailwayAiweightMapper meterBaseRailwayAiweightMapper;

    @Resource
    private MeterBaseOperationLogMapper meterBaseOperationLogMapper;

    @Resource
    private PreRailwayScaleMapper preRailwayScaleMapper;

    @Override
    protected IBaseMapper<MeterWorkRailwayReceived, String> getMapper() {
        return meterWorkRailwayReceivedMapper;
    }

    @Override
    public RESTfulResult checkandadd(MeterWorkRailwayReceived model) throws Exception {
        try{
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("200");
            rm.setData(model.getActualFirstNo());
            if(model.getRailwayNo() == null && StringUtils.isEmpty(model.getRailwayNo())) {
                model.setRailwayNo("车号异常");
            }
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            String afl = sdf.format(new Date());
//            String no = meterWorkRailwayReceivedMapper.getNewID(afl);
//            model.setActualFirstNo(sdf.format(new Date()) + no);
            meterWorkRailwayReceivedMapper.insertSelective(model);
            rm.setMessage("成功");
            return rm;
        }
        catch (Exception e){
            throw e;
        }
    }

    @Override
    public List<MeterWorkRailwayReceived> noPage(HashMap<String, Object> parmas) {
        List<MeterWorkRailwayReceived> rows = meterWorkRailwayReceivedMapper.noPage(parmas);
        return rows;
    }

    @Override
    @Transactional
    public RESTfulResult updateList(List<MeterWorkRailwayReceived> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            int count = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            for (MeterWorkRailwayReceived model: models) {
                if(model.getMemo() != null && StringUtils.isNotEmpty(model.getMemo())) {
                    MeterWorkRailwayReceived received = meterWorkRailwayReceivedMapper.selectByPrimaryKey(model.getActualFirstNo());
                    MeterBaseOperationLog note = new MeterBaseOperationLog();
                    String no = PublicMethod.IdCreate(3);
                    note.setId(no);
                    note.setCreateManNo(model.getUpdateManNo());
                    note.setCreateManName(model.getUpdateManName());
                    note.setCreateTime(new Date());
                    note.setOperationPage("终端-轨道衡从表数据维护");
                    note.setOperationFunction("修改");
                    note.setOperationBeforeFix(received.toString());
                    note.setOperationAfterFix(model.toString());
                    note.setOperationCarnNo(model.getRailwayNo());
                    meterBaseOperationLogMapper.insertSelective(note);
                    model.setMemo("");
                }

                if(model.getAiWeight() != null && StringUtils.isNotEmpty(model.getAiWeight())) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("railwayNo", model.getRailwayNo());
                    List<MeterBaseRailwayAiweight> AiModels = meterBaseRailwayAiweightMapper.selectByParameters(map);
                    MeterBaseRailwayAiweight AiModel = new MeterBaseRailwayAiweight();
                    if(AiModels.size() >= 1) {
                        AiModel = AiModels.get(0);
                        AiModel.setMeterWeight(new BigDecimal(model.getAiWeight()));
                        AiModel.setRailwayType(model.getRailwayModelName());
                        AiModel.setRailwaySpeed(model.getRailwaySpeed());
                        AiModel.setUpdateManNo(model.getUpdateManNo());
                        AiModel.setUpdateManName(model.getUpdateManName());
                        AiModel.setUpdateTime(new Date());
                        AiModel.setValueFlag("0");
                        meterBaseRailwayAiweightMapper.updateByPrimaryKeySelective(AiModel);
                    }else {
                        AiModel.setActualFirstNo(model.getActualFirstNo());
                        AiModel.setRailwayNo(model.getRailwayNo());
                        AiModel.setMeterWeight(new BigDecimal(model.getAiWeight()));
                        AiModel.setRailwayType(model.getRailwayModelName());
                        AiModel.setRailwayTypeNo("001019002");
                        AiModel.setRailwayTypeName("动态轨道衡");
                        AiModel.setValueFlag("0");
                        AiModel.setCreateManNo(model.getUpdateManNo());
                        AiModel.setCreateManName(model.getUpdateManName());
                        AiModel.setCreateTime(new Date());
                        AiModel.setRailwaySpeed(model.getRailwaySpeed());
                        meterBaseRailwayAiweightMapper.insertSelective(AiModel);
                    }
                }
                meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(model);
                count ++;
            }
            //修改主表是否已处理完
            if(models.size() >= 1) {
                MeterWorkRailwayReceived receivedModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(models.get(0).getActualFirstNo());
                if(receivedModel != null){
                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
                    HashMap<String, Object> countMap = meterWorkRailwayReceivedMapper.getCount(obj);
                    int countNum = Integer.parseInt(countMap.get("COUNTNUM").toString());
                    if(countNum < 1) {
                        HashMap<String, Object> mainMap = new HashMap<>();
                        mainMap.put("soundName", receivedModel.getRailwayWorkNo());
                        List<MeterWorkRailwayRecMain> mainModel = meterWorkRailwayRecMainMapper.selectByParameters(mainMap);
                        if(mainModel.size() >= 1) {
                            mainModel.get(0).setIsDispose("1");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(mainModel.get(0));
                        }
                    }
                }
            }
            
            rm.setSucceed();
            rm.setMessage("操作成功，共有" + count + "条数据更新成功");
            rm.setCode("200");
            rm.setData(count);
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常");
        }
        return rm;
    }

    @Override
    public RESTfulResult updateListStart(List<MeterWorkRailwayReceived> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            int count = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            for (MeterWorkRailwayReceived model: models) {
//                if(model.getMemo() != null && StringUtils.isNotEmpty(model.getMemo())) {
//                    MeterBaseOperationLog note = new MeterBaseOperationLog();
//                    String no = PublicMethod.IdCreate(3);
//                    note.setId(no);
//                    note.setCreateManNo(model.getCreateManNo());
//                    note.setCreateManName(model.getCreateManName());
//                    note.setCreateTime(new Date());
//                    note.setOperationPage("终端-轨道衡从表数据维护");
//                    note.setOperationFunction("修改");
//                    note.setOperationCarnNo(model.getRailwayNo());
//                    meterBaseOperationLogMapper.insertSelective(note);
//                    model.setMemo("");
//                }

//                if(model.getAiWeight() != null && StringUtils.isNotEmpty(model.getAiWeight())) {
//                    HashMap<String, Object> map = new HashMap<>();
//                    map.put("railwayNo", model.getRailwayNo());
//                    List<MeterBaseRailwayAiweight> AiModels = meterBaseRailwayAiweightMapper.selectByParameters(map);
//                    MeterBaseRailwayAiweight AiModel = new MeterBaseRailwayAiweight();
//                    if(AiModels.size() >= 1) {
//                        AiModel = AiModels.get(0);
//                        AiModel.setMeterWeight(new BigDecimal(model.getAiWeight()));
//                        AiModel.setRailwayType(model.getRailwayModelName());
//                        AiModel.setRailwaySpeed(model.getRailwaySpeed());
//                        AiModel.setUpdateManNo("systemAdd");
//                        AiModel.setUpdateManName("systemAdd");
//                        AiModel.setUpdateTime(new Date());
//                        AiModel.setValueFlag("1");
//                        meterBaseRailwayAiweightMapper.updateByPrimaryKeySelective(AiModel);
//                    }else {
//                        AiModel.setActualFirstNo(model.getActualFirstNo());
//                        AiModel.setRailwayNo(model.getRailwayNo());
//                        AiModel.setMeterWeight(new BigDecimal(model.getAiWeight()));
//                        AiModel.setRailwayType(model.getRailwayModelName());
//                        AiModel.setRailwayTypeNo("001019002");
//                        AiModel.setRailwayTypeNo("动态轨道衡");
//                        AiModel.setValueFlag("1");
//                        AiModel.setCreateManNo("systemAdd");
//                        AiModel.setCreateManName("systemAdd");
//                        AiModel.setCreateTime(new Date());
//                        AiModel.setRailwaySpeed(model.getRailwaySpeed());
//                        meterBaseRailwayAiweightMapper.insertSelective(AiModel);
//                    }
//                }
//                meterWorkRailwayReceivedMapper.deleteByPrimaryKey(model.getActualFirstNo());
//                rm.setData(model.getActualFirstNo());
//                if(model.getRailwayNo() == null && StringUtils.isEmpty(model.getRailwayNo())) {
//                    model.setRailwayNo("车号异常");
//                }
////            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
////            String afl = sdf.format(new Date());
////            String no = meterWorkRailwayReceivedMapper.getNewID(afl);
////            model.setActualFirstNo(sdf.format(new Date()) + no);
//                meterWorkRailwayReceivedMapper.insertSelective(model);
                model.setValueFlag("1");
                meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(model);
                count ++;
            }
            
            //修改主表是否已处理完
            if(models.size() >= 1) {
                MeterWorkRailwayReceived receivedModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(models.get(0).getActualFirstNo());
                if(receivedModel != null){
                    HashMap<String, Object> mainMap = new HashMap<>();
                    mainMap.put("soundName", receivedModel.getRailwayWorkNo());
                    List<MeterWorkRailwayRecMain> mainModel = meterWorkRailwayRecMainMapper.selectByParameters(mainMap);
                    if(mainModel.size() >= 1) {
                        mainModel.get(0).setIsDispose("0");
                        meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(mainModel.get(0));
                    }
                }
            }
            rm.setMessage("操作成功，共有" + count + "条数据更新成功");
            rm.setCode("200");
            rm.setData(count);
            rm.setSucceed(true);
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常");
        }
        return rm;
    }

    @Override
    public RESTfulResult addList(List<MeterWorkRailwayReceived> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            Assert.isTrue(models.size() >= 1, "未勾选数据新增");
            int count = 0;
            List<String> spotList = models
                    .stream()
                    .map(MeterWorkRailwayReceived::getBaseSpotNo)
                    .distinct()
                    .collect(Collectors.toList());
            for (String spotNo: spotList) {
                List<MeterWorkRailwayReceived> receiveds = new ArrayList<>();
                for(MeterWorkRailwayReceived receivedTemp: models){
                    if(receivedTemp.getRailwayWorkNo() != null && StringUtils.isNotEmpty(receivedTemp.getRailwayWorkNo())) {
                        HashMap<String, Object> objMap = new HashMap();
                        objMap.put("soundName", receivedTemp.getRailwayWorkNo());
                        List<MeterWorkRailwayRecMain> recMainTemp = meterWorkRailwayRecMainMapper.selectByParameters(objMap);
                        if(recMainTemp.size() < 1) {
                            rm.setMessage("未找到" + receivedTemp.getRailwayWorkNo()  + "列次号信息");
                            return rm;
                        }
                        recMainTemp.get(0).setIsDispose("0");
                        recMainTemp.get(0).setCarriageNum((short)(receiveds.size() + 1));
                        meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(recMainTemp.get(0));

                        if(receivedTemp.getMemo() != null && StringUtils.isNotEmpty(receivedTemp.getMemo())) {
                            MeterBaseOperationLog note = new MeterBaseOperationLog();
                            note.setId(PublicMethod.IdCreate(3));
                            note.setCreateManNo(receivedTemp.getCreateManNo());
                            note.setCreateManName(receivedTemp.getCreateManName());
                            note.setCreateTime(new Date());
                            note.setOperationPage("终端-轨道衡从表数据维护");
                            note.setOperationFunction("新增");
                            note.setOperationCarnNo(receivedTemp.getRailwayNo());
                            meterBaseOperationLogMapper.insertSelective(note);
                        }
                        receivedTemp.setRailwayWorkNo(recMainTemp.get(0).getActualNo());
                        receivedTemp.setActualFirstNo(PublicMethod.IdCreate(3));
                        receivedTemp.setValueFlag("1");
                        receivedTemp.setRailwayTypeNo("001019002");
                        receivedTemp.setRailwayTypeName("动态轨道衡");
                        meterWorkRailwayReceivedMapper.insertSelective(receivedTemp);
                        count ++;
                    }else {
                        if(spotNo.equals(receivedTemp.getBaseSpotNo())){
                            receiveds.add(receivedTemp);
                        }
                    }
                }

                if(receiveds.size()>=1) {
                    //插入主表数据
                    MeterWorkRailwayRecMain recMain = new MeterWorkRailwayRecMain();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                    String afl = sdf.format(new Date());
                    String lcNo = meterWorkRailwayRecMainMapper.getLcNo(afl, receiveds.get(0).getBaseSpotNo());
                    recMain.setActualNo(receiveds.get(0).getBaseSpotName().replaceAll("#", "")
                            .replaceAll("轨道衡", "")
                            .replaceAll("动轨", "")
                            .replaceAll("动态", "") + "-" + afl + lcNo + "(" + receiveds.size() + ")");
                    recMain.setIsDispose("0");
                    recMain.setSoundName(recMain.getActualNo());
                    recMain.setCarriageNum((short)receiveds.size());
                    recMain.setBaseSpotNo(receiveds.get(0).getBaseSpotNo());
                    recMain.setBaseSpotName(receiveds.get(0).getBaseSpotName());
                    recMain.setCreateTime(new Date());
                    meterWorkRailwayRecMainMapper.insertSelective(recMain);

                    for(MeterWorkRailwayReceived received: receiveds){
                        if(received.getMemo() != null && StringUtils.isNotEmpty(received.getMemo())) {
                            MeterBaseOperationLog note = new MeterBaseOperationLog();
                            note.setId(PublicMethod.IdCreate(3));
                            note.setCreateManNo(received.getCreateManNo());
                            note.setCreateManName(received.getCreateManName());
                            note.setCreateTime(new Date());
                            note.setOperationPage("终端-轨道衡从表数据维护");
                            note.setOperationFunction("新增");
                            note.setOperationCarnNo(received.getRailwayNo());
                            meterBaseOperationLogMapper.insertSelective(note);
                        }
                        received.setRailwayWorkNo(recMain.getActualNo());
                        received.setActualFirstNo(PublicMethod.IdCreate(3));
                        received.setValueFlag("1");
                        received.setRailwayTypeNo("001019002");
                        received.setRailwayTypeName("动态轨道衡");
                        meterWorkRailwayReceivedMapper.insertSelective(received);
                        count ++;
                    }
                }
            }

            rm.setSucceed();
            rm.setMessage("操作成功，共有" + count + "条数据更新成功");
            rm.setCode("200");
            rm.setData(count);
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常");
        }
        return rm;
    }

    @Override
    public RESTfulResult RemovePre(List<MeterWorkRailwayReceived> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            int count = 0;
            for(MeterWorkRailwayReceived model: models){
                MeterWorkRailwayReceived modelTemp =  meterWorkRailwayReceivedMapper.selectByPrimaryKey(model.getActualFirstNo());
                if(modelTemp.getPredictionNo() != null && StringUtils.isNotEmpty(modelTemp.getPredictionNo())){
                    PreRailwayScale scale = new PreRailwayScale();
                    scale.setValueFlag("1");
                    scale.setUploadFlag("1");
                    scale.setPredictionNo(modelTemp.getPredictionNo());
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
                }
                modelTemp = meterWorkRailwayReceivedMapper.selectAndRemove(modelTemp.getActualFirstNo());
                modelTemp.setIsAutoOperation("1");
                meterWorkRailwayReceivedMapper.updateByPrimaryKey(modelTemp);
                count++;
            }
            rm.setSucceed(true);
            rm.setMessage("操作成功，共有" + count + "条数据更新成功");
            rm.setCode("200");
            rm.setData(count);
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常");
        }
        return rm;
    }

    @Override
    public RESTfulResult removeList(List<MeterWorkRailwayReceived> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            int count = 0;
            for(MeterWorkRailwayReceived model: models){
                model.setValueFlag("0");
                model.setIsAutoOperation("1");
                meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(model);
                if(model.getPredictionNo() != null &&  StringUtils.isNotEmpty(model.getPredictionNo())){
                    PreRailwayScale preRailwayScale = new PreRailwayScale();
                    preRailwayScale.setValueFlag("1");
                    preRailwayScale.setUploadFlag("1");
                    preRailwayScale.setPredictionNo(model.getPredictionNo());
                    preRailwayScaleMapper.updateByPrimaryKeySelective(preRailwayScale);
                }
                count++;
            }

            //修改主表是否已处理完
            if(models.size() >= 1) {
                MeterWorkRailwayReceived receivedModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(models.get(0).getActualFirstNo());
                if(receivedModel != null){
                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
                    HashMap<String, Object> countMap = meterWorkRailwayReceivedMapper.getCount(obj);
                    int countNum = Integer.parseInt(countMap.get("COUNTNUM").toString());
                    if(countNum < 1) {
                        HashMap<String, Object> mainMap = new HashMap<>();
                        mainMap.put("soundName", receivedModel.getRailwayWorkNo());
                        List<MeterWorkRailwayRecMain> mainModel = meterWorkRailwayRecMainMapper.selectByParameters(mainMap);
                        if(mainModel.size() >= 1) {
                            mainModel.get(0).setIsDispose("1");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(mainModel.get(0));
                        }
                    }
                }
            }
            rm.setSucceed(true);
            rm.setMessage("操作成功，共有" + count + "条数据更新成功");
            rm.setCode("200");
            rm.setData(count);
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常");
        }
        return rm;
    }

    @Override
    public RESTfulResult matchPre(MeterWorkRailwayReceived model) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            PreRailwayScale preRailwayScale = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
            preRailwayScale.setValueFlag("4");
            preRailwayScale.setUploadFlag("1");
            preRailwayScaleMapper.updateByPrimaryKeySelective(preRailwayScale);
            model.setRailwayNo(preRailwayScale.getRailwayNo());
            meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(model);
            rm.setSucceed();
            rm.setMessage("操作成功");
            rm.setCode("200");
            rm.setData(null);
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常");
        }
        return rm;
    }

    @Override
    @Transactional
    public RESTfulResult batchMatchPre(List<MeterWorkRailwayReceived> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            int count = 0;
            for(MeterWorkRailwayReceived model: models) {
                MeterWorkRailwayReceived modelTemp = meterWorkRailwayReceivedMapper.selectByPrimaryKey(model.getActualFirstNo());
                if(StringUtil.isEmpty(modelTemp.getPredictionNo())) {
                    PreRailwayScale scale = preRailwayScaleMapper.selectByPrimaryKey(model.getPredictionNo());
                    scale.setValueFlag("4");
                    scale.setUploadFlag("1");
                    preRailwayScaleMapper.updateByPrimaryKeySelective(scale);
                    model.setRailwayNo(scale.getRailwayNo());
                    model.setMatterNo(scale.getMatterNo());
                    model.setMatterName(scale.getMatterName());
                    model.setReceivingUintNo(scale.getReceivingUintNo());
                    model.setReceivingUintName(scale.getReceivingUintName());
                    model.setForwardingUnitNo(scale.getForwardingUnitNo());
                    model.setForwardingUnitName(scale.getForwardingUnitName());
                    meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(model);
                    count ++;
                }
            }
            rm.setSucceed();
            rm.setMessage("操作成功,共有" + count + "条数据匹配委托成功");
            rm.setCode("200");
            rm.setData(null);
        }catch(Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("操作异常");
        }
        return rm;
    }
}
