using com.hnshituo.core.webapp.vo;
using Common;
using MeterConditionLibrary;
using System;
using System.Collections.Generic;

namespace MeterSceneLibrary
{
    /// <summary>
    /// 先毛后皮 根据计量流程配置界面中配置的基础数据可知业务是先毛后皮
    /// </summary>
    public class FirstGrossSecondlyTare
    {
        private MeterBaseTermTareDataService termTareDate = new MeterBaseTermTareDataService();//期限皮操作

        /// <summary>
        /// 先毛后皮 根据计量流程配置界面中配置的基础数据可知业务是先毛后皮
        /// </summary>
        /// <param name="scale">计量预报</param>
        /// <returns></returns>
        public rtInfo MeterMethod(PreTrackScale scale, CoreAppUser appUser, RESTfulResult<List<MeterWorkPreLink>> rmDb)
        {
            rtInfo rt = new rtInfo();
            rt.isError = false;

            //0：批次预报；1：单次预报；2：联运预报；3：分检预报
            if (scale.predictionType == "2")
            {
                rt.result = false;
                rt.isError = false;
                return rt;
            }

            RESTfulResult<List<MeterWorkPreLink>> rm = rmDb;
            //如果配置了则走下面的流程
            //如果存在一次计量未结净数据，则认为是结净计量，否则认为一次计量
            MeterWorkCarActualFirstService mwcfs = new MeterWorkCarActualFirstService();

            RESTfulResult<List<MeterWorkCarActualFirst>> rmX = mwcfs.doQueryWf(new MeterWorkCarActualFirst { valueFlag = "1", predictionNo = scale.predictionNo, carNo = scale.carNo });
            if (!rmX.Succeed)
            {
                rt.result = false;
                rt.resultInfo = "没有计量信息点语音求助";
                PbCache.ResultMessage = "获取一次计量数据异常！";
                rt.isError = true;
            }

            PbCache.ResultMessage = "";

            //验证是否存在历史皮
            validWgt vw = new validWgt();

            if (rm.Data[0].linkCode == "001021003")  //计量环节为 一次计量   计量场景为先毛后皮
            {
                if (rmX.Data != null && rmX.Data.Count > 0)
                {
                    PbCache.ResultMessage = "计量环节为一次计量，存在未结净数据数据！";
                    rt.resultInfo = "已经存在一次计量信息";
                    rt.result = false;
                    rt.isError = true;
                }
                else //验证当前毛重是否符合要求
                {
                    vw.ValidNotInnerMethod(scale);

                    if (PbCache.monitorResult.haveHisTare)
                    {
                        long meterWeight = (PbCache.tareCount.contrastTypeNo == "001012001" ? PbCache.monitorResult.hisTareData[0].upWeight.Value : PbCache.monitorResult.hisTareData[0].avgWeight.Value);

                        double maxWgt = 0, minWgt = 0, rateV = 0;
                        if (PbCache.tareCount.countTypeNo == "001011002") //按比例
                        {
                            if (PbCache.tareCount.rateValue != null)
                            {
                                rateV = PbCache.tareCount.rateValue.Value / 100;
                            }
                            maxWgt = meterWeight * (1 + rateV);
                            minWgt = meterWeight * (1 - rateV);
                        }
                        else //固重
                        {
                            maxWgt = meterWeight + (PbCache.tareCount.fixedWeightValue == null ? 0 : PbCache.tareCount.fixedWeightValue.Value);
                            minWgt = meterWeight - (PbCache.tareCount.fixedWeightValue == null ? 0 : PbCache.tareCount.fixedWeightValue.Value);
                        }

                        //如果不验证重量则可跳过这个逻辑，否则：当前重量-皮重需要>=1000
                        if (PbCache.monitor.validWgt == "1")
                        {
                            //当前重量是皮重，或者小于最小皮重的数据
                            if (PbCache.lockWgt < minWgt)
                            {
                                rt.result = false;
                                rt.isError = false;
                                rt.resultInfo = "当前毛重介于皮重及毛重之间无法计量"; //语音
                                //2021年5月10日界面显示单位，调整为T
                                PbCache.ResultMessage = "当前重量被判定为皮重，需先计量毛重再计量皮重，" +
                                    "历史皮重为[" + Math.Round((double)meterWeight / 1000, 2) + "] T ,请点【语音求助】，由司秤工处理！"; //界面提示信息
                                return rt;
                            }
                            else if (PbCache.lockWgt < maxWgt)
                            {
                                rt.result = false;
                                rt.isError = true;
                                rt.resultInfo = "当前毛重介于皮重及毛重之间无法计量";
                                PbCache.ResultMessage = "当前为毛重计量，计量重量[ " + PbCache.lockWgt / 1000 + " ]介于皮重及毛重之间无法计量，" +
                                    "历史皮重为[" + Math.Round((double)meterWeight / 1000, 2) + "] T ,请点【语音求助】，由司秤工处理！";
                                return rt;
                            }
                        }
                    }

                    //验证物料时间段
                    validMatTime vmt = new validMatTime();
                    vmt.ValidMethod(scale, "1");
                    if (!PbCache.monitorResult.valid_mat_time)
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = PbCache.ResultMessage;
                        return rt;
                    }

                    //验证危化品验证
                    validExceedWgt vew = new validExceedWgt();
                    vew.validMatMethod(scale, "1", null);
                    if (!PbCache.monitorResult.valid_exceed_wgt)
                    {
                        rt.result = false;
                        rt.isError = true;
                        return rt;
                    }

                    //验证预报超差信息
                    validPredictionDiff vpd = new validPredictionDiff();
                    vpd.ValidMethod(scale, "1", null); //验证毛重
                    if (!PbCache.monitorResult.valid_prediction_diff)
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = PbCache.ResultMessage;
                        return rt;
                    }

                    //验证联达码头；江西联达联达料场，预报超差
                    validLianDaWgtDiff lianDaWgtDiff = new validLianDaWgtDiff();
                    lianDaWgtDiff.ValidMethod(scale, "1", null); //验证毛重
                    if (!PbCache.monitorResult.valid_prediction_diff)
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = PbCache.ResultMessage;
                        return rt;
                    }

                    //验证两次计量时间间隔
                    validTimeInterval vti = new validTimeInterval();
                    vti.ValidMethod(scale, "1", null);
                    if (!PbCache.monitorResult.valid_time_interval)
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = PbCache.ResultMessage;
                        return rt;
                    }

                    //验证通过，保存毛重信息***********************
                    MeterWorkCarActualFirst mwaf = new MeterWorkCarActualFirst();

                    //相同名称的均赋值一次
                    EntityBase<PreTrackScale> entityBase = new EntityBase<PreTrackScale>();
                    mwaf = entityBase.format<MeterWorkCarActualFirst>(scale, mwaf);

                    mwaf.actualFirstNo = DateTime.Now.ToString("yyyyMMdd");//10
                    mwaf.meterWeight = PbCache.lockWgt;//计量重量
                    mwaf.addWeight = PbCache.addWgt;//附加重量
                    mwaf.weightType = "0"; //0=毛重；1=常规皮重；2=期限皮重；3=历史皮重；4=标识皮重；5=自重皮重
                    mwaf.createManNo = appUser.userid;
                    mwaf.createManName = appUser.username;
                    mwaf.meterGroup = appUser.userGroup;
                    mwaf.meterClass = appUser.userClass;
                    mwaf.createTime = DateTime.Now;
                    mwaf.baseSpotNo = PbCache.sportInfo.baseSpotNo;//计量点编号
                    mwaf.baseSpotName = PbCache.sportInfo.baseSpotName;
                    //mwaf.memo = "";
                    mwaf.valueFlag = "1"; //0有效
                    mwaf.updateManName = "";
                    mwaf.updateManNo = "";
                    mwaf.updateTime = null;
                    mwaf.checkFlag = "0"; //0未审核
                    mwaf.meterMode = string.IsNullOrEmpty(appUser.userid) ? "2" : "1"; //1远程计量，2智能计量
                    mwaf.addWeight = PbCache.addWgt;

                    MeterWorkPreLink mwpl = new MeterWorkPreLink();
                    mwpl.preLinkNo = rm.Data[0].preLinkNo;
                    mwpl.preProcessNo = rm.Data[0].preProcessNo;

                    //操作日志
                    MeterMonitorNote monitorNote = new MeterMonitorNote();
                    monitorNote.baseSpotNo = mwaf.baseSpotNo;
                    monitorNote.baseSpotName = mwaf.baseSpotName;
                    monitorNote.operationContent = string.Format("称重保存");
                    monitorNote.operationTime = mwaf.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                    monitorNote.carNo = mwaf.carNo;
                    monitorNote.meterNoteSource = string.IsNullOrEmpty(appUser.userid) ? "0" : "4";
                    monitorNote.meterManNo = appUser.userid;
                    monitorNote.meterManName = appUser.username;

                    #region 若此时有期限皮，且当前物料是可用期限皮的数据，则走着看的代码，否则走下面的doAddWf方法
                    // and t2.meter_nature_name like  '%外%'  //此为车辆计量属性； 例如：若不是中控，则不允许使用期限皮重（九江取消了）
                    bool bTerm = false;
                    string sql = @"select t1.meter_weight meterWeight,
                                          t1.data_no dataNo,
                                          t1.create_time createTime,
                                          t1.base_spot_no baseSpotNo,
                                          t1.base_spot_name baseSpotName
                                      from Meter_Base_Term_Tare_Data t1, Meter_Base_Term_Tare_Car t2
                                     where t1.car_no = t2.car_no
                                       and t2.value_flag = '0'
                                       and t1.value_flag = '0'
                                       and t1.car_no = '" + scale.carNo + @"'
                                       and t1.end_time>sysdate";
                    PbModelDbService<List<MeterBaseTermTareData>> pb = new PbModelDbService<List<MeterBaseTermTareData>>();
                    RESTfulResult<List<MeterBaseTermTareData>> rmType = pb.executeSqlDataWf(sql);
                    if (rmType.Succeed)
                    {
                        //and t.meter_nature_name like '%外%' //此为物料计量属性，例如：若不是外购物料，则不允许使用期限皮重；（九江取消了）
                        if (rmType.Data != null && rmType.Data.Count > 0)
                        {
                            //查下当前拖的物料是否可用期限皮;验证业务类型是否允许使用期限皮重
                            string sqls = @"select t.matter_no id, t.matter_name text
                                              from meter_base_matter_info t,meter_base_metertype_conf t2
                                             where t.allowed_tare_weight = '1'
                                               and t.valid_flag = '1'
                                               and t.matter_no='" + scale.matterNo + @"'
                                               and t2.is_allow_term_tare = '1' and  t2.meter_type_no = '" + scale.meterTypeNo + @"'";

                            PbModelDbService<List<PbModelDb>> pbp = new PbModelDbService<List<PbModelDb>>();
                            RESTfulResult<List<PbModelDb>> rmT = pbp.executeSqlDataWf(sqls);
                            if (rmT.Succeed)
                            {
                                if (rmT.Data != null && rmT.Data.Count > 0)
                                {
                                    bTerm = true;
                                }
                            }
                            else
                            {
                                rt.result = false;
                                rt.isError = true;
                                rt.resultInfo = "称重数据保存失败";
                                PbCache.ResultMessage = "计量失败，获取期限皮可用物料异常：" + rmType.ResultMessage;
                            }
                        }
                    }
                    else
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = "称重数据保存失败";
                        PbCache.ResultMessage = "计量失败，获取期限皮数据异常：" + rmType.ResultMessage;
                    }

                    if (bTerm)
                    {
                        //结净数据
                        MeterWorkCarActual netDb = new MeterWorkCarActual();
                        EntityBase<PreTrackScale> entityActual = new EntityBase<PreTrackScale>();
                        netDb = entityBase.format<MeterWorkCarActual>(scale, netDb);

                        netDb.grossTime = DateTime.Now;
                        netDb.grossWeight = PbCache.lockWgt;
                        netDb.baseSpot1No = PbCache.sportInfo.baseSpotNo;
                        netDb.baseSpot1Name = PbCache.sportInfo.baseSpotName;
                        netDb.grossMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配

                        netDb.actualFirst2No = rmType.Data[0].dataNo;
                        netDb.tareTime = rmType.Data[0].createTime;
                        netDb.tareWeight = rmType.Data[0].meterWeight;
                        netDb.baseSpot2No = rmType.Data[0].baseSpotNo;
                        netDb.baseSpot2Name = rmType.Data[0].baseSpotName;

                        netDb.netManNo = appUser.userid; //净重计量员编码 
                        netDb.netManName = appUser.username; //净重计量员名称
                        netDb.netSpot3No = PbCache.sportInfo.baseSpotNo; //净重计量点编号
                        netDb.netSpot3Name = PbCache.sportInfo.baseSpotName; //净重计量点

                        netDb.netTime = DateTime.Now;

                        netDb.addWeight = PbCache.addWgt;
                        //净重=毛-皮-附加重量
                        netDb.netWeight = netDb.grossWeight - netDb.tareWeight - netDb.addWeight;
                        netDb.netMode = "1"; //1：正常结净；2：匹配结净
                        netDb.valueFlag = "0"; //0=有效，1=作废
                        netDb.uploadFlag = "0"; //0=待审核；1：待上传；2=已上传

                        RESTfulResult<string> rmIs = mwcfs.doAddNetByTermWf(mwaf, netDb, monitorNote);
                        if (rmIs.Succeed)
                        {
                            rt.result = true;
                            rt.resultInfo = "称重完成";
                            PbCache.ResultMessage = "称重完成";
                            PbCache.actualFirstNo = rmIs.Data;
                            PbCache.strQRCode = rmIs.Message;
                            PbCache.isTareWeight = false;//毛重
                        }
                        else
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = "称重数据保存失败";
                            PbCache.ResultMessage = "计量失败：" + rmIs.ResultMessage;
                        }

                        return rt;
                    }

                    #endregion 若此时有期限皮，且当前物料是可用期限皮的数据，则走着看的代码，否则走下面的doAddWf方法

                    RESTfulResult<string> rmI = mwcfs.doAddWf(mwaf, mwpl, monitorNote, "", "0");
                    if (rmI.Succeed)
                    {
                        rt.result = true;
                        rt.resultInfo = "称重完成";
                        PbCache.ResultMessage = "称重完成";
                        PbCache.actualFirstNo = rmI.Data;
                        PbCache.strQRCode = rmI.Message;
                        PbCache.isTareWeight = false;//毛重

                        RESTfulResult<string> RESTfulResult = termTareDate.abolishForCSharp(new MeterBaseTermTareData { carNo = mwaf.carNo });
                        if (RESTfulResult.Succeed)
                        {
                            if (!string.IsNullOrEmpty(RESTfulResult.Message))
                            {
                                PbCache.ResultMessage = PbCache.ResultMessage + "；请注意期限皮重已作废";
                            }
                        }
                        else
                        {
                            PbCache.ResultMessage = PbCache.ResultMessage + "；但作废期限皮重异常，请点击【语音求助】联系计量员";
                        }
                    }
                    else
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = "称重数据保存失败";
                        PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                    }
                }
            }
            else if (rm.Data[0].linkCode == "001021005")  //计量环节为 结净  计量场景为先毛后皮
            {
                if (string.IsNullOrEmpty(scale.forwardingUnitName) || string.IsNullOrEmpty(scale.receivingUintName))
                {
                    PbCache.ResultMessage = "结净时，预报中的发货单位以及收货单位均不允许为空，请点击[语音求助]！";
                    rt.result = false;
                    rt.isError = false;
                    return rt;
                }
                if (rmX.Data == null || rmX.Data.Count == 0)
                {
                    rt.resultInfo = "未找到一次计量数据！";
                    rt.result = false;
                    rt.isError = false;
                    PbCache.ResultMessage = "未找到一次计量数据,请先计量毛重！";
                }
                else if (rmX.Data.Count == 1)
                {
                    //则走其他的验证，比如与历史皮重验证，与其他的信息验证，若验证都通过则保存一次计量信息
                    MeterWorkCarActualFirst first = rmX.Data[0];
                    if (first.weightType != "0") //一次不为毛重，即一次是皮重，那么皮皮不能结合
                    {
                        rt.result = false;
                        rt.isError = false;
                        PbCache.ResultMessage = "已经存在一条重量为[" + first.meterWeight + "]的皮重数据，皮重与皮重不能计量！";
                    }
                    else //一次为毛重，那么当前是皮重  走先毛后皮流程
                    {
                        if (!PbCache.isNotValid)
                        {
                            if (Math.Abs(rmX.Data[0].meterWeight.Value - PbCache.lockWgt) < AppConfigCache.differenceWgt)
                            {
                                //重复计量
                                rt.result = false;
                                rt.isError = true;
                                rt.resultInfo = "第一次计量重量和第二次计量重量接近";
                                //PbCache.ResultMessage = "第一次计量重量和第二次计量重量接近";
                                PbCache.ResultMessage = "第一次计量重量【" + rmX.Data[0].meterWeight.Value / 1000 + "T】和第二次计量重量【" + PbCache.lockWgt / 1000 + "T】接近，小于结净最小值【" + AppConfigCache.differenceWgt / 1000 + " T】,请点击[语音求助]，由司秤工过磅！";
                                return rt;
                            }
                        }

                        //验证当前皮重是否符合要求
                        vw.ValidNotInnerMethod(scale);
                        if (!PbCache.monitorResult.valid_wgt)
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = "当前皮重不在皮重配置范围";
                            if (PbCache.ResultMessage != null && !PbCache.ResultMessage.Contains("皮重"))
                            {
                                PbCache.ResultMessage = "当前皮重不在皮重配置范围";
                            }
                            return rt;
                        }

                        //一次重量小于当前重量，所以一次的时候应该是皮重
                        if (rmX.Data[0].meterWeight.Value - PbCache.lockWgt < 0)
                        {
                            //存在历史皮
                            if (PbCache.monitorResult.haveHisTare)
                            {
                                rt.result = false;
                                rt.isError = true;
                                rt.resultInfo = "存在一次毛重信息，当前重量大于一次毛重毛毛不能结合";
                                PbCache.ResultMessage = "存在一次毛重信息，当前重量大于一次毛重，毛重与毛重不能计量";
                                return rt;
                            }
                            else
                            {
                                //没有历史皮的情况，将一次数据类型修改为皮重；2021年3月12日杨秀东新增提示信息，让司机重新扫码；
                                mwcfs.doUpdateWf(new MeterWorkCarActualFirst { actualFirstNo = rmX.Data[0].actualFirstNo, weightType = "1" });
                                rt.result = false;
                                rt.isError = false;
                                PbCache.ResultMessage = "请再次点击[保存]按钮";
                                return rt;
                            }
                        }

                        //卸货时间验证
                        ValidUnloadTime unloadTime = new ValidUnloadTime();
                        unloadTime.ValidMethod(first);
                        if (!PbCache.monitorResult.valid_unload_time)
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = PbCache.ResultMessage;// 方法内已经有提示信息
                            return rt;
                        }

                        //验证两次计量时间间隔
                        validTimeInterval vti = new validTimeInterval();
                        vti.ValidMethod(scale, "3", first);
                        if (!PbCache.monitorResult.valid_time_interval)
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = PbCache.ResultMessage; // 方法内已经有提示信息
                            return rt;
                        }

                        //验证预报超差信息
                        validPredictionDiff vpd = new validPredictionDiff();
                        vpd.ValidMethod(scale, "3", first); //验证净重
                        if (!PbCache.monitorResult.valid_prediction_diff)
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = PbCache.ResultMessage; // 方法内已经有提示信息
                            return rt;
                        }

                        //验证计量区域：若二次计量数据的计量区域与一次计量的计量区域不一致，则禁止自助计量；
                        validMeterArea vMeterArea = new validMeterArea();
                        vMeterArea.ValidMethod(first.baseSpotNo);
                        if (!PbCache.monitorResult.valid_meter_area)
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = PbCache.ResultMessage;
                            return rt;
                        }

                        //====所有验证均通过后，此时进行结净=====================
                        //保存皮重信息  即：二次计量信息***********************
                        MeterWorkCarActualFirst secondDb = new MeterWorkCarActualFirst();

                        //相同名称的均赋值一次
                        EntityBase<PreTrackScale> entityBase = new EntityBase<PreTrackScale>();
                        secondDb = entityBase.format<MeterWorkCarActualFirst>(scale, secondDb);

                        secondDb.actualFirstNo = DateTime.Now.ToString("yyyyMMdd");//10
                        secondDb.meterWeight = PbCache.lockWgt;//计量重量
                        secondDb.addWeight = first.addWeight;//附加重量
                        secondDb.weightType = "1"; //0=毛重；1=常规皮重；2=期限皮重；3=历史皮重；4=标识皮重；5=自重皮重
                        secondDb.createManNo = appUser.userid;
                        secondDb.createManName = appUser.username;
                        secondDb.meterGroup = appUser.userGroup;
                        secondDb.meterClass = appUser.userClass;
                        secondDb.createTime = DateTime.Now;
                        secondDb.baseSpotNo = PbCache.sportInfo.baseSpotNo;//计量点编号
                        secondDb.baseSpotName = PbCache.sportInfo.baseSpotName;
                        //secondDb.memo = "";
                        secondDb.valueFlag = "1"; //0有效
                        secondDb.updateManName = "";
                        secondDb.updateManNo = "";
                        secondDb.updateTime = null;
                        secondDb.checkFlag = "0"; //0未审核
                        secondDb.meterMode = string.IsNullOrEmpty(appUser.userid) ? "2" : "1"; //1远程计量，2智能计量

                        //环节信息
                        MeterWorkPreLink link = new MeterWorkPreLink();
                        link.preLinkNo = rm.Data[0].preLinkNo;
                        link.preProcessNo = rm.Data[0].preProcessNo;

                        //结净数据
                        MeterWorkCarActual netDb = new MeterWorkCarActual();
                        EntityBase<PreTrackScale> entityActual = new EntityBase<PreTrackScale>();
                        netDb = entityBase.format<MeterWorkCarActual>(scale, netDb);

                        netDb.actualFirst1No = first.actualFirstNo; //毛重计量作业编号
                        netDb.grossTime = first.createTime;
                        netDb.grossWeight = first.meterWeight;
                        netDb.baseSpot1No = first.baseSpotNo;
                        netDb.baseSpot1Name = first.baseSpotName;
                        netDb.grossMode = first.meterMode;//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                        netDb.grossManNo = first.createManNo;
                        netDb.grossManName = first.createManName;
                        netDb.grossClass = first.meterClass;
                        netDb.grossGroup = first.meterGroup;

                        //netDb.actualFirst2No = ""; //皮重计量作业编号
                        netDb.tareWeight = PbCache.lockWgt; //皮重
                        netDb.tareTime = DateTime.Now;
                        netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo; //皮重计量点编号
                        netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName; //皮重计量点
                        netDb.tareMode = string.IsNullOrEmpty(appUser.userid) ? "2" : "1"; //1远程计量，2智能计量
                        netDb.netTime = DateTime.Now;
                        netDb.tareGroup = appUser.userGroup;
                        netDb.tareClass = appUser.userClass;
                        netDb.tareManNo = appUser.userid;
                        netDb.tareManName = appUser.username;

                        netDb.netManNo = appUser.userid; //净重计量员编码 
                        netDb.netManName = appUser.username; //净重计量员名称
                        netDb.netSpot3No = PbCache.sportInfo.baseSpotNo; //净重计量点编号
                        netDb.netSpot3Name = PbCache.sportInfo.baseSpotName; //净重计量点

                        netDb.addWeight = (first.addWeight == null ? 0 : first.addWeight.Value);
                        //净重=毛-皮-附加重量
                        netDb.netWeight = netDb.grossWeight - netDb.tareWeight - netDb.addWeight;
                        netDb.netMode = "1"; //1：正常结净；2：匹配结净
                        netDb.valueFlag = "0"; //0=有效，1=作废
                        netDb.uploadFlag = "0"; //0=待审核；1：待上传；2=已上传

                        //操作日志
                        MeterMonitorNote monitorNote = new MeterMonitorNote();
                        monitorNote.baseSpotNo = secondDb.baseSpotNo;
                        monitorNote.baseSpotName = secondDb.baseSpotName;
                        monitorNote.operationContent = string.Format("结净称重保存");
                        monitorNote.operationTime = secondDb.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                        monitorNote.carNo = secondDb.carNo;
                        monitorNote.meterNoteSource = string.IsNullOrEmpty(appUser.userid) ? "0" : "4";
                        monitorNote.meterManNo = appUser.userid;
                        monitorNote.meterManName = appUser.username;

                        RESTfulResult<string> rmI = mwcfs.doAddNetWf(secondDb, netDb, link, monitorNote, "", "1");
                        if (rmI.Succeed)
                        {
                            rt.result = true;
                            rt.resultInfo = "称重完成";
                            PbCache.ResultMessage = "称重完成";
                            PbCache.actualFirstNo = rmI.Data;
                            PbCache.strQRCode = rmI.Message;
                            PbCache.isTareWeight = true;//皮重
                        }
                        else
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = "称重数据保存失败";
                            PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                        }
                    }
                }
                else
                {
                    PbCache.ResultMessage = string.Format("当前预报[{0}]存在多条未结净数据", scale.predictionNo);
                    rt.resultInfo = "找到多条未结净数据";
                    rt.result = false;
                    rt.isError = true;
                }
            }

            return rt;
        }
    }
}