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

namespace MeterSceneLibrary
{
    /// <summary>
    /// 先皮后毛
    /// </summary>
    public class FirstTareSecondlyGross
    {
        private validTermTareCar termTareCar = new validTermTareCar();//验证是否可留期限皮
        private MeterBaseTermTareDataService termTareDate = new MeterBaseTermTareDataService();//期限皮操作
        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;
            }

            if (rm.Data[0].linkCode == "001021003")  //计量环节为 一次计量   计量场景为先皮后毛
            {
                if (rmX.Data != null && rmX.Data.Count > 0)
                {
                    rt.resultInfo = "已存在一次计量数据"; 
                    PbCache.ResultMessage = "当前计量环节为【" + rm.Data[0].linkName + "】,但已存在已存在一次计量数据，不允许进行计量操作！";
                    rt.result = false;
                    rt.isError = true;
                }
                else
                {
                    
                    //验证两次计量时间间隔
                    validTimeInterval vti = new validTimeInterval();
                    vti.ValidMethod(scale, "2", null);
                    if (!PbCache.monitorResult.valid_time_interval)
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = PbCache.ResultMessage;
                        return rt;
                    }

                    //验证当前皮重是否符合要求;不符合则验证是否能够使用期限皮重计量；再验证是否使用历史皮重
                    validWgt vw = new validWgt();
                    vw.ValidNotInnerMethod(scale);

                    if (PbCache.monitorResult.valid_wgt)
                    {
                        //期限皮重业务是否能够保存验证；若为true，则不允许进行普通皮重保存
                        if (termTareCar.ValidMethodThree(scale))
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = "不允许进行普通皮重保存";
                            //PbCache.ResultMessage = "不允许进行普通皮重保存！"; //在ValidMethodThree方法里面有处理了
                            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 = 0;//附加重量
                        mwaf.weightType = "1"; //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;

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

                            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 (PbCache.monitorResult.haveHisTare)
                        {
                            long meterWeight = (PbCache.tareCount.contrastTypeNo == "001012001" ? PbCache.monitorResult.hisTareData[0].upWeight.Value : PbCache.monitorResult.hisTareData[0].avgWeight.Value);
                            double maxWgt = 0, rateV = 0;
                            if (PbCache.tareCount.countTypeNo == "001011002") //按比例
                            {
                                if (PbCache.tareCount.rateValue != null)
                                {
                                    rateV = PbCache.tareCount.rateValue.Value / 100;
                                }
                                maxWgt = meterWeight * (1 + rateV);
                            }
                            else //固重
                            {
                                maxWgt = meterWeight + (PbCache.tareCount.fixedWeightValue == null ? 0 : PbCache.tareCount.fixedWeightValue.Value);
                            }

                            if (!PbCache.monitorResult.valid_wgt)
                            {
                                if (PbCache.lockWgt > maxWgt && maxWgt > 0)
                                {
                                    //若皮重不在配置范围，且重量大于最大范围值，则认为是过毛，此时有期限皮，且物料可使用期限皮，则使用期限皮进行计量，否则

                                    #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)
                                    {
                                        if (rmType.Data != null && rmType.Data.Count > 0)
                                        {
                                            //查下当前拖的物料是否可用期限皮;验证业务类型是否允许使用期限皮重
                                            //and t.meter_nature_name like '%外%' //此为物料计量属性，例如：若不是外购物料，则不允许使用期限皮重；（九江取消了）
                                            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
                                                {
                                                    PbCache.ResultMessage = "；且物料【" + scale.matterName + "】【"+ scale.meterTypeName + "】不允许使用期限皮重";
                                                }
                                            }
                                            else
                                            {
                                                rt.result = false;
                                                rt.isError = true;
                                                rt.resultInfo = "称重数据保存失败";
                                                PbCache.ResultMessage = "计量失败，获取期限皮可用物料异常：" + rmType.ResultMessage;
                                            }
                                        }
                                        else
                                        {
                                            //判断一下是不是车辆的期限皮重过期了
                                            sql = @"select t1.meter_weight meterWeight,
                                                 t1.end_time endTime,
                                                 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 + "'";
                                            PbModelDbService<List<MeterBaseTermTareData>> pbEnd = new PbModelDbService<List<MeterBaseTermTareData>>();
                                            RESTfulResult<List<MeterBaseTermTareData>> rmTypeEnd = pb.executeSqlDataWf(sql);
                                            if (rmTypeEnd.Succeed && rmTypeEnd.Data != null && rmTypeEnd.Data.Count > 0)
                                            {
                                                PbCache.ResultMessage += "；且车号【" + scale.carNo + "】的期限皮重已过期,结束时间【"+ rmTypeEnd.Data[0].endTime+ "】！";
                                            }
                                            else
                                            {
                                                PbCache.ResultMessage += "；且车号【" + scale.carNo + "】的没有有效的期限皮重！";
                                            }
                                                   
                                        }
                                    }
                                    else
                                    {
                                        rt.result = false;
                                        rt.isError = true;
                                        rt.resultInfo = "称重数据保存失败";
                                        PbCache.ResultMessage = "计量失败，获取期限皮数据异常：" + rmType.ResultMessage;
                                    }

                                    if (bTerm)
                                    {
                                        MeterWorkPreLinkService mwpps = new MeterWorkPreLinkService();
                                        //获取预环节流程信息;001021007;未进行装货确认
                                        RESTfulResult<List<MeterWorkPreLink>> rmLink = mwpps.doQueryWf(new MeterWorkPreLink { valueFlag = "0", predictionNo = scale.predictionNo });
                                        if (rmLink.Succeed && rmLink.Data != null && rmLink.Data.Count > 0)
                                        {
                                            rmLink.Data = rmLink.Data.OrderBy(s => s.seqNo).ToList();
                                            foreach (var item in rm.Data)
                                            {
                                                if (item.linkCode == "001021007" && item.valueFlag == "0")
                                                {
                                                    rt.result = false;
                                                    rt.isError = true;
                                                    rt.resultInfo = "未进行装货确认";
                                                    PbCache.ResultMessage = string.Format("【"+ scale.meterTypeName + "】业务，未进行【{0}】,不允许进行二次计量操作！", item.linkName);
                                                    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;
                                        }

                                        //验证危化品验证；//2021年7月13日 添加危化品验证，解决硫酸期限皮重结净问题
                                        validExceedWgt vew = new validExceedWgt();
                                        vew.validMatMethod(scale, "1", null);
                                        if (!PbCache.monitorResult.valid_exceed_wgt)
                                        {
                                            rt.result = false;
                                            rt.isError = true;
                                            return rt;
                                        }

                                        MeterWorkCarActualFirst firstValid = new MeterWorkCarActualFirst();
                                        firstValid.meterWeight = rmType.Data[0].meterWeight;
                                        firstValid.addWeight = 0;

                                       //验证物资限载验证
                                       validExceedWgt vewNet = new validExceedWgt();
                                        vewNet.validMatMethod(scale, "3", firstValid); //验证净重
                                        if (!PbCache.monitorResult.valid_exceed_wgt)
                                        {
                                            rt.result = false;
                                            rt.isError = true;
                                            return rt;
                                        }

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

                                        //验证计量区域：若二次计量数据的计量区域与一次计量的计量区域不一致，则禁止自助计量；
                                        validMeterArea vMeterArea = new validMeterArea();
                                        vMeterArea.ValidMethod(rmType.Data[0].baseSpotNo);
                                        if (!PbCache.monitorResult.valid_meter_area)
                                        {
                                            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 = 0;//附加重量
                                        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;

                                        //操作日志
                                        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;

                                        //结净数据
                                        MeterWorkCarActual netDb = new MeterWorkCarActual();
                                        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.netTime = DateTime.Now;

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

                                        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方法

                                    //else
                                    {
                                        rt.result = false;
                                        rt.isError = true;
                                        rt.resultInfo = "当前皮重不在皮重配置范围";
                                        if (PbCache.ResultMessage != null && !PbCache.ResultMessage.Contains("皮重"))
                                        {
                                            PbCache.ResultMessage += "当前皮重不在皮重配置范围";
                                        }
                                    }
                                }
                                else
                                {
                                    rt.result = false;
                                    rt.isError = true;
                                    rt.resultInfo = "当前皮重不在皮重配置范围";
                                    if (PbCache.ResultMessage != null && !PbCache.ResultMessage.Contains("皮重"))
                                    {
                                        PbCache.ResultMessage = "当前皮重不在皮重配置范围";
                                    }
                                }
                            }
                        }
                        else
                        {
                            //沿用ValidNotInnerMethod方法的提示信息
                            rt.result = false;
                            rt.isError = false;
                        }
                    }
                }
            }
            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)
                        {
                            //如果勾选了不验证重量则可以放过 1000kg的判断
                            if (Math.Abs(rmX.Data[0].meterWeight.Value - PbCache.lockWgt) < AppConfigCache.differenceWgt && PbCache.monitor.validWgt != "0")
                            {
                                //重复计量
                                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;
                            }
                        }

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

                        //一次重量大于当前重量，所以一次的时候应该是毛重
                        if (rmX.Data[0].meterWeight.Value - PbCache.lockWgt > 0)
                        {
                            vw.validHisTare(scale);
                            //没有历史皮的情况下，若配置了先皮后毛与先毛后皮，则会先走先皮流程，然后发现一次大于二次则将一次改为毛，继续走后面的先毛后皮流程
                            if (!PbCache.monitorResult.haveHisTare)
                            {
                                //将一次数据类型修改为皮重
                                mwcfs.doUpdateWf(new MeterWorkCarActualFirst { actualFirstNo = rmX.Data[0].actualFirstNo, weightType = "0" });
                                rt.result = false;
                                rt.isError = false;
                                PbCache.ResultMessage = "请再次点击[保存]按钮";
                                return rt;
                            }
                            else
                            {
                                rt.result = false;
                                rt.isError = true;
                                rt.resultInfo = "存在一次皮重信息，当前重量小于一次皮重皮皮不能结合";
                                PbCache.ResultMessage = "存在一次皮重信息，当前重量小于一次皮重皮皮不能结合";
                                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;
                        }

                        //验证两次计量时间间隔
                        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;
                        }

                        //验证危化品验证
                        validExceedWgt vew = new validExceedWgt();
                        vew.validMatMethod(scale, "3", first);
                        if (!PbCache.monitorResult.valid_exceed_wgt)
                        {
                            rt.result = false;
                            rt.isError = true;
                            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;
                        }

                        //验证物资是否需要发运重量
                        validMatterName vmn = new validMatterName();
                        if (!vmn.ValidEffectiveWeight(scale))
                        {
                            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 = PbCache.addWgt;//附加重量
                        secondDb.weightType = "0"; //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 = ""; //毛重计量作业编号
                        netDb.grossTime = DateTime.Now;
                        netDb.grossWeight = PbCache.lockWgt; //毛重
                        netDb.baseSpot1No = PbCache.sportInfo.baseSpotNo; //毛重计量点编号
                        netDb.baseSpot1Name = PbCache.sportInfo.baseSpotName; //毛重计量点
                        netDb.grossMode = string.IsNullOrEmpty(appUser.userid) ? "2" : "1"; //1远程计量，2智能计量
                        netDb.grossGroup = appUser.userGroup;
                        netDb.grossClass = appUser.userClass;
                        netDb.grossManNo = appUser.userid;
                        netDb.grossManName = appUser.username;

                        netDb.actualFirst2No = first.actualFirstNo;
                        netDb.tareWeight = first.meterWeight;
                        netDb.tareTime = first.createTime;
                        netDb.baseSpot2No = first.baseSpotNo;
                        netDb.baseSpot2Name = first.baseSpotName;
                        netDb.tareMode = first.meterMode;
                        netDb.tareClass = first.meterClass;
                        netDb.tareGroup = first.meterGroup;
                        netDb.tareManName = first.createManName;
                        netDb.tareManNo = first.createManNo;

                        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=已上传
                        netDb.netTime = DateTime.Now;

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

                        //操作日志
                        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, "", "0");
                        if (rmI.Succeed)
                        {
                            rt.result = true;
                            rt.resultInfo = "称重完成";
                            PbCache.ResultMessage = "称重完成";
                            PbCache.actualFirstNo = rmI.Data;
                            PbCache.strQRCode = rmI.Message;
                            PbCache.isTareWeight = false;//毛重
                        }
                        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;
        }
    }
}