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

import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.meterwork.constants.DbConstants;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkhookactfirst.mapper.MeterWorkHookActFirstMapper;
import com.steerinfo.meterwork.meterworkhookactfirst.model.MeterWorkHookActFirst;
import com.steerinfo.meterwork.meterworkhookactual.model.MeterWorkHookActual;
import com.steerinfo.meterwork.meterworkhookactual.mapper.MeterWorkHookActualMapper;
import com.steerinfo.meterwork.meterworkhookactual.service.IMeterWorkHookActualService;
import com.steerinfo.meterwork.meterworkrailwayactfirst.model.MeterWorkRailwayActFirst;
import com.steerinfo.meterwork.meterworkrailwayactual.model.MeterWorkRailwayActual;
import com.steerinfo.pretrack.prehookscale.mapper.PreHookScaleMapper;
import com.steerinfo.pretrack.prehookscale.model.PreHookScale;
import com.steerinfo.pretrack.prerailwayscale.model.PreRailwayScale;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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

    @Resource
    private MeterWorkHookActualMapper meterWorkHookActualMapper;

    @Resource
    private MeterWorkHookActFirstMapper meterWorkHookActFirstMapper;

    @Resource
    private PreHookScaleMapper preHookScaleMapper;

    @Override
    protected IBaseMapper<MeterWorkHookActual, String> getMapper() {
        return meterWorkHookActualMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addData(MeterWorkHookActual model) throws MarkerMetException {
        if (model.getActualFirst1No() == null || "".equals(model.getActualFirst1No())) {
            throw new MarkerMetException(500, "毛重数据主键不能为空");
        }
        if (model.getRailwayNo() == null || "".equals(model.getRailwayNo())) {
            throw new MarkerMetException(500, "火车号不能为空");
        }
        if (model.getPredictionNo() == null || "".equals(model.getPredictionNo())) {
            throw new MarkerMetException(500, "选中的毛重数据需要先匹配委托");
        }
        MeterWorkHookActFirst queryrow = meterWorkHookActFirstMapper.selectByPrimaryKey(model.getActualFirst1No());
        if (queryrow == null) {
            throw new MarkerMetException(500, "不存在对应的毛重数据");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();

        // 处理一次计量数据
        // 毛重
        queryrow.setUpdateTime(new Date());
        queryrow.setUpdateManName(userName + ": 一次数据输入历史皮重结净操作");
        queryrow.setUpdateManNo(userId);
        queryrow.setValueFlag("2");
        meterWorkHookActFirstMapper.updateByPrimaryKey(queryrow);

        // 新增一条皮重
        MeterWorkHookActFirst tarerow = new MeterWorkHookActFirst();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date time = new Date();
        String idNo = sdf.format(new Date()) + meterWorkHookActFirstMapper.getNewID(sdf.format(time));
        tarerow.setActualFirstNo(idNo);
        tarerow.setRailwayNo(model.getRailwayNo());
        tarerow.setMeterWeight(model.getTareWeight());
        tarerow.setCreateManName(userName);
        tarerow.setCreateManNo(userId);
        tarerow.setCreateTime(new Date());
        tarerow.setMeterMode("3");
        //tarerow.setDataSource(queryrow.getDataSource());
        tarerow.setMemo("吊钩称一次计量数据输入历史皮重结净操作新增皮重数据");
        tarerow.setValueFlag("2");
        tarerow.setWeightType("3");
        int num1 = meterWorkHookActFirstMapper.insertSelective(tarerow);

        // 处理预报信息
        PreHookScale pres = new PreHookScale();
        pres.setPredictionNo(model.getPredictionNo());
        pres.setValueFlag("2");
        pres.setUpdateManName(userName + ": 使用历史皮重结净操作");
        pres.setUpdateTime(new Date());
        pres.setUpdateManNo(userId);
        preHookScaleMapper.updateByPrimaryKeySelective(pres);

        // 新增净重数据
        String idNo2 = sdf.format(new Date()) + meterWorkHookActualMapper.getNewID(sdf.format(time));
        model.setActualNo(idNo2);
        model.setActualFirst2No(idNo); //皮重计量编号
        model.setTareManName(userName);
        model.setTareManNo(userId);
        model.setTareTime(time);
        model.setTareMode("3");
        if ( model.getGrossWeight().compareTo(model.getTareWeight()) >= 0){
            model.setNetWeight(model.getGrossWeight().subtract(model.getTareWeight()));
        }else {
            throw new MarkerMetException(500, "输入的皮重数据不能大于称重毛重数据！");
        }
        model.setNetMode("2");
        model.setNetTime(time);
        model.setNetManName(userName);
        model.setNetManNo(userId);
        //model.setDataSource(queryrow.getDataSource());
        model.setValueFlag("1");
        int num = meterWorkHookActualMapper.insertSelective(model);
        return num;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> distinctCarNo(String railwayNo, String startTime, String endTime) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("railwayNo", railwayNo);
        obj.put("startTime", startTime);
        obj.put("endTime", endTime);
        List<String> list = meterWorkHookActualMapper.distinctCarNo(obj).stream().map(MeterWorkHookActual::getRailwayNo).collect(Collectors.toList());
        return list;
    }

    @Override
    public int publish(MeterWorkHookActual model) {
        int i = 0;
        if (model == null) {
            throw new MarkerMetException(500, "参数为空！！");
        }
        if (model.getActualNo() == null || "".equals(model.getActualNo())) {
            throw new MarkerMetException(500, "数据主键为空");
        }
        if (model.getPredictionNo() == null || "".equals(model.getPredictionNo()) || !"1".equals(model.getValueFlag())) {
            throw new MarkerMetException(500, "只有已匹配委托且未发布的数据才能进行数据发布操作");
        }

        UserPayload userPayload = new UserPayload();
        String userName = userPayload.getUserName();
        String userId = userPayload.getUserCode();
        model.setValueFlag("2");
        model.setUpdateTime(new Date());
        model.setUpdateManName(userName);
        model.setUpdateManNo(userId);
        try {
            meterWorkHookActualMapper.updateByPrimaryKeySelective(model);
        } catch (Exception e) {
            throw e;
        }
        return ++i;
    }

    @Override
    public int cancel(MeterWorkHookActual model) {
        int i = 0;
        if (model == null) {
            throw new MarkerMetException(500, "参数为空！！");
        }
        if (model.getActualNo() == null || "".equals(model.getActualNo())) {
            throw new MarkerMetException(500, "数据主键为空");
        }
        if (model.getPredictionNo() == null || "".equals(model.getPredictionNo()) || !"2".equals(model.getValueFlag())) {
            throw new MarkerMetException(500, "只有已匹配委托且已发布的数据才能进行撤销发布操作");
        }

        UserPayload userPayload = new UserPayload();
        String userName = userPayload.getUserName();
        String userId = userPayload.getUserCode();
        model.setValueFlag("1");
        model.setUpdateTime(new Date());
        model.setUpdateManName(userName);
        model.setUpdateManNo(userId);
        try {
            meterWorkHookActualMapper.updateByPrimaryKeySelective(model);
        } catch (Exception e) {
            throw e;
        }
        return ++i;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(List<MeterWorkHookActual> models) throws MarkerMetException {
        try {
            int num = 0;
            if (models.size() <= 0 || models == null) {
                throw new MarkerMetException(500, "参数为空！！！");
            }
            for (MeterWorkHookActual model : models) {
                //毛重
                MeterWorkHookActFirst mwcaf1 = meterWorkHookActFirstMapper.selectByPrimaryKey(model.getActualFirst1No());
                if (mwcaf1 == null) {
                    throw new MarkerMetException(500, "未查询到相应的毛重数据！！");
                }
                // 清空委托信息
                MeterWorkHookActFirst grossrow = meterWorkHookActFirstMapper.selectAndRemove(model.getActualFirst1No());
                grossrow.setValueFlag("3");
                UserPayload payload = UserPayload.getCurrUser();
                String userId = payload.getUserCode();
                String userName = payload.getUserName();
                grossrow.setUpdateManNo(userId);
                grossrow.setUpdateManName(userName);
                grossrow.setUpdateTime(new Date());
                meterWorkHookActFirstMapper.updateByPrimaryKey(grossrow);
                //皮重
                MeterWorkHookActFirst mwcaf2 = meterWorkHookActFirstMapper.selectByPrimaryKey(model.getActualFirst2No());
                //MeterBaseRailwayWeight mbrw = meterBaseRailwayWeightMapper.selectByPrimaryKey(model.getActualFirst2No());
                if (mwcaf2 == null ) {
                    throw new MarkerMetException(500, "未查询到相应的皮重数据！！");
                }
                // 清空委托信息
                MeterWorkHookActFirst tarerow = meterWorkHookActFirstMapper.selectAndRemove(model.getActualFirst2No());
                if (tarerow != null) {
                    tarerow.setValueFlag("3");
                    tarerow.setUpdateManNo(userId);
                    tarerow.setUpdateManName(userName);
                    tarerow.setUpdateTime(new Date());
                    meterWorkHookActFirstMapper.updateByPrimaryKey(tarerow);
                }
                if (StringUtils.isNotEmpty(model.getPredictionNo())) {
                    PreHookScale scale = new PreHookScale();
                    scale.setPredictionNo(model.getPredictionNo());
                    scale.setValueFlag("1"); //使用中
                    scale.setUpdateManNo(userId);
                    scale.setUpdateManName(userName);
                    scale.setUpdateTime(new Date());
                    preHookScaleMapper.updateByPrimaryKeySelective(scale);
                }
                meterWorkHookActualMapper.deleteByPrimaryKey(model.getActualNo());
                num++;
            }

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


    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkHookActual clean(String actualFirstNo, String dataNo, String predictionNo) throws MarkerMetException {
        try {
            if (actualFirstNo == null && actualFirstNo.equals("")) {
                throw new MarkerMetException(500, "毛重数据编号为空");
            }
            if (dataNo == null && dataNo.equals("")) {
                throw new MarkerMetException(500, "皮重数据编号为空");
            }
            MeterWorkHookActFirst mwcaf1 = meterWorkHookActFirstMapper.selectByPrimaryKey(actualFirstNo);
            if (mwcaf1 == null) {
                throw new MarkerMetException(500, "未查询到相应的毛重数据！！");
            }

            MeterWorkHookActFirst mwcaf2 = meterWorkHookActFirstMapper.selectByPrimaryKey(dataNo);
            if (mwcaf2 == null) {
                throw new MarkerMetException(500, "未查询到相应的皮重数据！！");
            }
            if (mwcaf2.getMeterWeight().compareTo(mwcaf1.getMeterWeight()) == 1) {
                throw new MarkerMetException(500, "皮重重量大于毛重重量！！");
            }

            // 选中的是否为同一车的数据
            if (!mwcaf1.getRailwayNo().substring(mwcaf1.getRailwayNo().length()-2).equals(mwcaf2.getRailwayNo().substring(mwcaf2.getRailwayNo().length()-2))) {
                throw new MarkerMetException(500, "请选择同一灌号的一次计量数据进行结净");
            }

            // 一次计量数据是否已经完成委托匹配
            if ((mwcaf1.getPredictionNo() != null && "".equals(mwcaf1.getPredictionNo())) || (mwcaf2.getPredictionNo() != null && "".equals(mwcaf2.getPredictionNo()))) {
                if (!mwcaf1.getPredictionNo().equals(mwcaf2.getPredictionNo())) {
                    throw new MarkerMetException(500, "选中的两条一次计量数据匹配的委托不一致，不允许进行结净操作！！！");
                } else if (predictionNo != null && !"".equals(predictionNo)) {
                    throw new MarkerMetException(500, "选中的两条一次计量数据已经匹配委托，请正确选择数据！！！");
                }
            }

            String preNo = (predictionNo == null ? mwcaf1.getPredictionNo() : predictionNo);

            //修改预报
            if (preNo != null && !"".equals(preNo)) {
                // 选中预报和选中一次计量数据中车号是否一致
                PreHookScale queryRow = preHookScaleMapper.selectByPrimaryKey(preNo);
                if (!mwcaf1.getRailwayNo().substring(mwcaf1.getRailwayNo().length()-2).equals(queryRow.getRailwayNo().substring(queryRow.getRailwayNo().length()-2))) {
                    throw new MarkerMetException(500, "选中预报中吊钩灌号与一次计量数据中灌号不一致！！！");
                }
                PreHookScale scale = new PreHookScale();
                scale.setPredictionNo(preNo);
                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("二次计量结净更新");
                preHookScaleMapper.updateByPrimaryKeySelective(scale);
            }

            MeterWorkHookActual actualRow = new MeterWorkHookActual();

            // 判断前端是否传入了预报编号
            if (predictionNo != null && !"".equals(predictionNo)) {
                // 根据一次计量数据和预报数据生成净重相关信息
                MeterWorkHookActFirst first1 = meterWorkHookActFirstMapper.selectAndMatch(actualFirstNo, predictionNo);
                BeanUtils.copyProperties(first1,mwcaf1);
                MeterWorkHookActFirst first2 = meterWorkHookActFirstMapper.selectAndMatch(dataNo, predictionNo);
                BeanUtils.copyProperties(first2, mwcaf2);
                MeterWorkHookActual mwca1 = meterWorkHookActualMapper.manualAddPre(actualFirstNo, dataNo, predictionNo);
                BeanUtils.copyProperties(mwca1, actualRow);
            } else {
                MeterWorkHookActual mwca2 = meterWorkHookActualMapper.manualAdd(actualFirstNo, dataNo);
                BeanUtils.copyProperties(mwca2, actualRow);
            }

            mwcaf1.setValueFlag(DbConstants.NET);
            meterWorkHookActFirstMapper.updateByPrimaryKey(mwcaf1);
            mwcaf2.setValueFlag(DbConstants.NET);
            meterWorkHookActFirstMapper.updateByPrimaryKey(mwcaf2);

            cleanCheck(actualRow);

            if (actualRow.getGrossTime().compareTo(actualRow.getTareTime()) == 1) {
                actualRow.setNetSpot3No(actualRow.getBaseSpot1No());
                actualRow.setNetSpot3Name(actualRow.getBaseSpot1Name());
            } else {
                actualRow.setNetSpot3No(actualRow.getBaseSpot2No());
                actualRow.setNetSpot3Name(actualRow.getBaseSpot2Name());
            }
            actualRow.setNetMode(DbConstants.TYPE);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String no = meterWorkHookActualMapper.getNewID(sdf.format(new Date()));
            actualRow.setActualNo(sdf.format(new Date()) + no);
            actualRow.setNetWeight(mwcaf1.getMeterWeight().subtract(mwcaf2.getMeterWeight()));
            meterWorkHookActualMapper.insertSelective(actualRow);

            //预报的处理
            if (StringUtils.isNotEmpty(actualRow.getPredictionNo())) {
                PreHookScale prs = new PreHookScale();
                prs.setPredictionNo(actualRow.getPredictionNo());
                prs.setValueFlag(DbConstants.NET); //已结净
                prs.setUpdateTime(new Date());
                prs.setUpdateManNo("system");
                prs.setUpdateManName("火车二次计量结净操作");
                preHookScaleMapper.updateByPrimaryKeySelective(prs);
            }
            return actualRow;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    private void cleanCheck(MeterWorkHookActual model) throws MarkerMetException {
        if (model.getRailwayNo() == null || model.getRailwayNo().equals("")) {
            throw new MarkerMetException(500, "火车车号不能为空");
        }
        if (model.getMatterNo() == null || model.getMatterNo().equals("")) {
            throw new MarkerMetException(500, "物料编号不能为空");
        }
        if (model.getMatterName() == null || model.getMatterName().equals("")) {
            throw new MarkerMetException(500, "物料名称能为空");
        }
        if (model.getMeterTypeNo() == null || model.getMeterTypeNo().equals("")) {
            throw new MarkerMetException(500, "计量类型编号不能为空");
        }
        if (model.getMeterTypeName() == null || model.getMeterTypeName().equals("")) {
            throw new MarkerMetException(500, "计量类型名称不能为空");
        }
        if (model.getActualFirst1No() == null || model.getActualFirst1No().equals("")) {
            throw new MarkerMetException(500, "毛重计量作业编号不能为空");
        }
        if (model.getGrossTime() == null || model.getGrossTime().equals("")) {
            throw new MarkerMetException(500, "毛重计量时间不能为空");
        }
        if (model.getBaseSpot1No() == null || model.getBaseSpot1No().equals("")) {
            throw new MarkerMetException(500, "毛重计量点编号不能为空");
        }
        if (model.getBaseSpot1Name() == null || model.getBaseSpot1Name().equals("")) {
            throw new MarkerMetException(500, "毛重计量点名称不能为空");
        }
        if (model.getActualFirst2No() == null || model.getActualFirst2No().equals("")) {
            throw new MarkerMetException(500, "皮重计量作业编号不能为空");
        }
        if (model.getTareTime() == null || model.getTareTime().equals("")) {
            throw new MarkerMetException(500, "皮重计量时间不能为空");
        }
        if (model.getActualNo() == null && model.getValueFlag() == null && (model.getPredictionNo() == null || "".equals(model.getPredictionNo()))) {
            model.setValueFlag("3"); // 未匹配
        } else if (model.getActualNo() == null && model.getValueFlag() == null && (model.getPredictionNo() != null && !"".equals(model.getPredictionNo()))) {
            model.setValueFlag("1"); // 已匹配
        }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkHookActual matchPrediction (String actualNo, String predictionNo, String note) throws MarkerMetException {
        if (actualNo == null || "".equals(actualNo)) {
            throw new MarkerMetException(500, "结净数据主键不能为空");
        }
        if (predictionNo == null || "".equals(predictionNo)) {
            throw new MarkerMetException(500, "委托编号不能为空");
        }
        MeterWorkHookActual queryrow = meterWorkHookActualMapper.selectByPrimaryKey(actualNo);
        if (queryrow == null) {
            throw new MarkerMetException(500, "不存在对应的结净数据");
        } else if (!queryrow.getValueFlag().equals("3") || queryrow.getPredictionNo() != null || "".equals(queryrow.getPredictionNo())) {
            throw new MarkerMetException(500, "只有未匹配的净重数据才能进行委托匹配操作");
        }
        PreHookScale prerow = preHookScaleMapper.selectByPrimaryKey(predictionNo);
        if (prerow == null) {
            throw new MarkerMetException(500, "不存在对应的委托数据");
        } else if (!prerow.getValueFlag().equals("1")) {
            throw new MarkerMetException(500, "只有未使用的委托才能进行匹配委托操作");
        }
        if (!queryrow.getRailwayNo().equals(prerow.getRailwayNo())) {
            throw new MarkerMetException(500, "结净数据中车号和预报中车号不一致，不允许进行匹配委托操作");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();

        // 处理一次计量数据
        // 毛重
        MeterWorkHookActFirst grossrow = meterWorkHookActFirstMapper.selectAndMatch(queryrow.getActualFirst1No(), predictionNo);
        grossrow.setUpdateTime(new Date());
        grossrow.setUpdateManName(userName + ": 结净数据匹配委托操作");
        grossrow.setUpdateManNo(userId);
        meterWorkHookActFirstMapper.updateByPrimaryKey(grossrow);

        // 皮重
        MeterWorkHookActFirst tarerow = meterWorkHookActFirstMapper.selectAndMatch(queryrow.getActualFirst2No(), predictionNo);
        tarerow.setUpdateTime(new Date());
        tarerow.setUpdateManName(userName + ": 结净数据匹配委托操作");
        tarerow.setUpdateManNo(userId);
        meterWorkHookActFirstMapper.updateByPrimaryKey(tarerow);

        // 处理预报
        if (StringUtils.isNotEmpty(predictionNo)) {
            PreHookScale scale = new PreHookScale();
            scale.setPredictionNo(predictionNo);
            scale.setValueFlag("2");
            scale.setUpdateTime(new Date());
            scale.setUpdateManNo("system");
            scale.setUpdateManName("计量结净数据匹配委托操作");
            preHookScaleMapper.updateByPrimaryKeySelective(scale);
        }

        MeterWorkHookActual mwca = meterWorkHookActualMapper.matchPredcition(actualNo, predictionNo);
        mwca.setValueFlag("1");
//        mwca.setNote(note); 添加修改原因记录
        meterWorkHookActualMapper.updateByPrimaryKey(mwca);
        return mwca;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkHookActual cleanPrediction (MeterWorkHookActual model) throws MarkerMetException {
        if (model.getActualNo() == null || "".equals(model.getActualNo())) {
            throw new MarkerMetException(500, "结净数据主键不能为空");
        }
        if (!model.getValueFlag().equals("1") && model.getPredictionNo() != null && !"".equals(model.getPredictionNo())) {
            throw new MarkerMetException(500, "只有有效已匹配的结净数据才能进行解除操作");
        }
        MeterWorkHookActual mwcaquery = meterWorkHookActualMapper.selectByPrimaryKey(model.getActualNo());
        if (mwcaquery == null) {
            throw new MarkerMetException(500, "不存在对应的结净数据");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();

        // 处理一次计量数据
        // 毛重
        MeterWorkHookActFirst grossrow = meterWorkHookActFirstMapper.selectAndRemove(model.getActualFirst1No());
        grossrow.setValueFlag("3");
        grossrow.setUpdateTime(new Date());
        grossrow.setUpdateManName(userName + ": 结净数据解除委托操作");
        grossrow.setUpdateManNo(userId);
        meterWorkHookActFirstMapper.updateByPrimaryKey(grossrow);

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

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

        mwca.setUpdateManNo(userId);
        mwca.setUpdateManName(userName);
        mwca.setUpdateTime(new Date());
        mwca.setValueFlag("0");
        meterWorkHookActualMapper.updateByPrimaryKey(mwca);
        return mwca;
    }

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

        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            //一次数据
            MeterWorkHookActFirst mwcaf1 = new MeterWorkHookActFirst();
            MeterWorkHookActFirst mwcaf2 = new MeterWorkHookActFirst();
            MeterWorkHookActual actualRow = new MeterWorkHookActual();
            String predictionNo = model.getPredictionNo();

            PreHookScale scale = preHookScaleMapper.selectByPrimaryKey(predictionNo);
            if(scale.getValueFlag().equals("2")) {
                rm.setMessage("委托已匹配，请确认！！");
                return rm;
            }

            /*UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();*/
            //毛重
            //毛重
            String actualFirstNo = sdf.format(new Date()) + meterWorkHookActFirstMapper.getNewID(afl);
            mwcaf1.setActualFirstNo(actualFirstNo);
            mwcaf1.setRailwayNo(model.getRailwayNo());
            mwcaf1.setMeterWeight(model.getGrossWeight());
            mwcaf1.setRailwayTypeNo(model.getRailwayTypeNo());
            mwcaf1.setRailwayTypeName(model.getRailwayTypeName());
            mwcaf1.setWeightType("0");
            mwcaf1.setCreateManNo(model.getGrossManNo());
            mwcaf1.setCreateManName(model.getGrossManName());
            mwcaf1.setCreateTime(model.getGrossTime());
            mwcaf1.setBaseSpotNo(model.getBaseSpot1No());
            mwcaf1.setBaseSpotName(model.getBaseSpot1Name());
            mwcaf1.setValueFlag("2");
            mwcaf1.setCheckFlag("0");
            mwcaf1.setDataSource("4");
            mwcaf1.setMeterMode("3");
            mwcaf1.setIsPreScale("0");
            meterWorkHookActFirstMapper.insertSelective(mwcaf1);
            //皮重
            String actualSecondNo = sdf.format(new Date()) + meterWorkHookActFirstMapper.getNewID(afl);
            mwcaf2.setActualFirstNo(actualSecondNo);
            mwcaf2.setRailwayNo(model.getRailwayNo());
            mwcaf2.setMeterWeight(model.getTareWeight());
            mwcaf2.setWeightType("1");
            mwcaf2.setRailwayTypeNo(model.getRailwayTypeNo());
            mwcaf2.setRailwayTypeName(model.getRailwayTypeName());
            mwcaf2.setCreateManNo(model.getGrossManNo());
            mwcaf2.setCreateManName(model.getGrossManName());
            mwcaf2.setCreateTime(model.getGrossTime());
            mwcaf2.setBaseSpotNo(model.getBaseSpot2No());
            mwcaf2.setBaseSpotName(model.getBaseSpot2Name());
            mwcaf2.setValueFlag("2");
            mwcaf2.setCheckFlag("0");
            mwcaf2.setDataSource("4");
            mwcaf2.setMeterMode("3");
            mwcaf2.setIsPreScale("0");
            meterWorkHookActFirstMapper.insertSelective(mwcaf2);
            //净重
            if(com.steerinfo.util.StringUtils.isNotEmpty(predictionNo)){
                //将委托中的编号和收发货的等数据拷贝进一次数据中
                MeterWorkHookActFirst first1 = meterWorkHookActFirstMapper.selectAndMatch(actualFirstNo, predictionNo);
                BeanUtils.copyProperties(first1,mwcaf1);
                mwcaf1.setIsPreScale("1");
                meterWorkHookActFirstMapper.updateByPrimaryKeySelective(mwcaf1);
                MeterWorkHookActFirst first2 = meterWorkHookActFirstMapper.selectAndMatch(actualSecondNo, predictionNo);
                BeanUtils.copyProperties(first2, mwcaf2);
                mwcaf2.setIsPreScale("1");
                meterWorkHookActFirstMapper.updateByPrimaryKeySelective(mwcaf2);
                //更新预报数据

                scale.setValueFlag(DbConstants.NET);
                scale.setUpdateTime(new Date());
                scale.setUpdateManNo("system");
                scale.setUpdateManName("手动录入");
                scale.setUploadFlag("1");
                scale.setValueFlag("2");
                preHookScaleMapper.updateByPrimaryKeySelective(scale);
                //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                MeterWorkHookActual mwca1 = meterWorkHookActualMapper.manualAddPre(actualFirstNo, actualSecondNo, predictionNo);
                BeanUtils.copyProperties(mwca1, actualRow);

            }else {
                //将委托中的编号和收发货单位和一次数据等数据拷贝进结净数据中
                MeterWorkHookActual mwca1 = meterWorkHookActualMapper.manualAdd(actualFirstNo, actualSecondNo);
                BeanUtils.copyProperties(mwca1, actualRow);
            }
            String no = meterWorkHookActualMapper.getNewID(sdf.format(new Date()));
            actualRow.setActualNo(sdf.format(new Date()) + no);
            actualRow.setRailwayTypeNo(model.getRailwayTypeNo());
            actualRow.setRailwayTypeName(model.getRailwayTypeName());
            actualRow.setNetTime(model.getNetTime());
            actualRow.setNetMode(DbConstants.TYPE);
            actualRow.setUploadFlag("1");
            actualRow.setIsPreScale("1");
            actualRow.setValueFlag("1");
            actualRow.setGrossMode("3");
            actualRow.setTareMode("3");
            actualRow.setDataSource("4");
            actualRow.setPredictionType("1");
            actualRow.setUpdateManNo("system");
            actualRow.setUpdateManName("手动录入");
            actualRow.setUpdateTime(new Date());
            actualRow.setPredictionNo(predictionNo);
            actualRow.setNetWeight(mwcaf1.getMeterWeight().subtract(mwcaf2.getMeterWeight()));
            meterWorkHookActualMapper.insertSelective(actualRow);
            rm.setSucceed();
            rm.setMessage("操作成功！！！");
            rm.setCode("200");
            return rm;
        }catch(Exception e){
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！！");
            return rm;
        }
    }

}
