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 InnerMeter
    {
        /// <summary>
        /// InnerMeterInfo 如果没有一次数据，那么scale的物料会为空，若有一次数据，由于会写到界面上，所以这个也会有值
        /// </summary>
        /// <param name="scale"></param>
        /// <returns></returns>
        public rtInfo MeterMethod(InnerMeterInfo scale)
        {
            Log lg = Log.GetInstance();
            rtInfo rt = new rtInfo();
            rt.isError = false;
            string ResultMessage = "";
            bool isError = false;

            //验证架子号与附加重量的关系；2021年6月23日 发现存在有架子号没重量的情况
            if (PbCache.addWgt == 0  && !string.IsNullOrEmpty(scale.addWgtPk))
            {
                rt.result = false;
                rt.isError = isError;
                rt.resultInfo = "已经输入架子号，但附加重量为 0 ，请重新选择架子号或清空架子号！";
                PbCache.ResultMessage = "已经输入架子号，但附加重量为 0 ，请重新选择架子号或清空架子号！";
                return rt;
            }

            //验证是否是内倒车辆
            //*
            if (string.IsNullOrEmpty(scale.predictionNo) || !scale.predictionNo.StartsWith("000"))
            {
                validCarCard vcc = new validCarCard();
                if (!vcc.ValidMethod(scale, out ResultMessage, out isError))
                {
                    rt.result = false;
                    rt.isError = isError;
                    rt.resultInfo = ResultMessage;
                    return rt;
                }
            }

            //*/

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

            validWgt validWgt = new validWgt();
            //验证是否是皮重，若为true则是皮，否则无法判断是毛还是皮
            validWgt.ValidMethod(new PreTrackScale { carNo = scale.carNo, matterNo = scale.matterNo });

            //查询车辆的历史皮重
            MeterBaseHisTareDataService mbtds = new MeterBaseHisTareDataService();
            RESTfulResult<List<MeterBaseHisTareData>> rm = mbtds.doQueryWf(new MeterBaseHisTareData { valueFlag = "0", carNo = scale.carNo });
            if (!rm.Succeed)
            {
                rt.result = false;
                rt.isError = true;
                rt.resultInfo = "没有计量信息点语音求助";
                PbCache.ResultMessage = "获取历史皮重异常！";
                return rt;
            }

            //*
            //厂内车，验证危化品验证,存在物料，且不为皮重
            if (!string.IsNullOrEmpty(scale.matterNo) && !PbCache.monitorResult.valid_wgt)
            {
                validInnerExceedWgt vew = new validInnerExceedWgt();
                vew.validMatMethod(scale, rm);
                if (!PbCache.monitorResult.valid_exceed_wgt)
                {
                    rt.result = false;
                    rt.isError = true;
                    rt.resultInfo = PbCache.ResultMessage;
                    return rt;
                }
            }
            //*/

            //获取一次计量数据
            MeterWorkCarActualFirstService mwcfs = new MeterWorkCarActualFirstService();
            RESTfulResult<List<MeterWorkCarActualFirst>> rmX = mwcfs.doQueryWf(new MeterWorkCarActualFirst { valueFlag = "1", carNo = scale.carNo });
            if (!rmX.Succeed)
            {
                rt.result = false;
                rt.resultInfo = "没有计量信息点语音求助";
                PbCache.ResultMessage = "获取一次计量数据异常！请点[语音求助]";
                rt.isError = true;
            }
            else
            {
                if (rmX.Data != null && rmX.Data.Count == 1 && !"中控倒运".Equals(rmX.Data[0].meterTypeName))
                {
                    rt.result = false;
                    rt.isError = isError;
                    rt.resultInfo = "已经存在一次计量信息";
                    lg.WriteLog(3, "一次计量数据的类型[" + rmX.Data[0].meterTypeName + "]与当前的业务类型[中控倒运]不一致，请点击[返回主界面]重新选择业务类型！");
                    PbCache.ResultMessage = "一次计量数据的类型[" + rmX.Data[0].meterTypeName + "]与当前的业务类型[中控倒运]不一致，请点击[返回主界面]重新选择业务类型！";
                    return rt;
                }
            }

            //相同名称的均赋值一次
            MeterWorkCarActualFirst mwaf = new MeterWorkCarActualFirst();
            EntityBase<InnerMeterInfo> entityBase = new EntityBase<InnerMeterInfo>();
            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 = "";
            mwaf.createManName = "";
            mwaf.createTime = DateTime.Now;
            mwaf.baseSpotNo = PbCache.sportInfo.baseSpotNo;//计量点编号
            mwaf.baseSpotName = PbCache.sportInfo.baseSpotName;
            //mwaf.memo = "";
            mwaf.valueFlag = "1"; //1有效
            mwaf.updateManName = "";
            mwaf.updateManNo = "";
            mwaf.updateTime = null;
            mwaf.checkFlag = "0"; //0未审核
            mwaf.meterMode = "2"; //智能计量

            //车辆上来后，若scale中的物料没有值，则认为是过皮，此时要去验证下是否存在历史皮，若有且符合则过皮，否则若重量大于提示请录入xxx
            if (string.IsNullOrEmpty(scale.matterNo))
            {
                //若物料没信息，则认为是过皮重
                //如果是皮重或者计量员勾选了不验证重量
                if (PbCache.monitorResult.valid_wgt)
                {
                    //如果没有一次计量数据
                    if (rmX.Data == null || rmX.Data.Count == 0)
                    {
                        //操作日志
                        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 = "0";

                        //存储皮重信息
                        mwaf.weightType = "1";
                        RESTfulResult<string> rmI = mwcfs.doAddInnerWf(mwaf, monitorNote, PbCache.monitor.validWgt);//说明是需验证皮重，且皮重验证通过了
                        if (rmI.Succeed)
                        {
                            rt.result = true;
                            rt.isError = false;
                            rt.resultInfo = "称重完成";
                            rt.message = rmI.Message;
                            PbCache.ResultMessage = "称重完成";
                            PbCache.actualFirstNo = rmI.Data;
                            PbCache.isTareWeight = true;//皮重
                        }
                        else
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = "称重数据保存失败";
                            PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                        }
                    }
                    else //存在一次计量数据
                    {
                        //看下两次数据差值是否在1t内，若一次减去当前值在1t内则认为是重复计量，若大于1t则比较两者时间差是否满足要求
                        //若时满足要求，则可进行计量，否则提示时间异常
                        //若时间也正常，则毛皮结合，然后保存历史皮重
                        if (Math.Abs(rmX.Data[0].meterWeight.Value - PbCache.lockWgt) >= AppConfigCache.differenceWgt) //一次为毛重
                        {
                            if (PbCache.lockWgt > rmX.Data[0].meterWeight)
                            {
                                rt.result = false;
                                rt.isError = true;
                                PbCache.ResultMessage = "当前重量【" + PbCache.lockWgt / 1000 + "】大于一次计量重量【" + rmX.Data[0].meterWeight / 1000 + "】，系统判断为毛重，计量毛重需要录入业务编号信息";
                                rt.resultInfo = "信息输入有误";
                                return rt;
                            }
                            else if (string.IsNullOrEmpty(rmX.Data[0].matterNo)) //一次为毛，且没有物料信息
                            {
                                rt.result = false;
                                rt.isError = true;
                                rt.resultInfo = "信息输入有误";
                                PbCache.ResultMessage = "一次计量毛重时，未录入业务编号信息，所以当前计量皮重需要录入业务编号信息";
                                return rt;
                            }

                            validTimeInterval vtime = new validTimeInterval();
                            vtime.ValidMethod(new PreTrackScale { meterTypeNo = "001006003" }, "3", rmX.Data[0]);
                            if (PbCache.monitorResult.valid_time_interval)
                            {
                                validTareTime vttime = new validTareTime();
                                vttime.ValidMethod(new PreTrackScale { meterTypeNo = "001006003", matterNo = rmX.Data[0].matterNo }, "3", rmX.Data[0]);
                                if (PbCache.monitorResult.valid_tare_time)
                                {
                                    scale.matterNo = rmX.Data[0].matterNo;
                                    scale.matterName = rmX.Data[0].matterName;
                                    scale.batchNo = rmX.Data[0].batchNo;
                                    scale.meterTypeNo = "001006003";
                                    scale.forwardingUnitNo = rmX.Data[0].forwardingUnitNo;
                                    scale.forwardingUnitName = rmX.Data[0].forwardingUnitName;
                                    scale.receivingUintNo = rmX.Data[0].receivingUintNo;
                                    scale.receivingUintName = rmX.Data[0].receivingUintName;

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

                                    //netDb.actualFirst1No = mb.actualFirstNo; //皮重计量作业编号
                                    netDb.tareTime = DateTime.Now;
                                    netDb.tareWeight = PbCache.lockWgt;
                                    netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
                                    netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
                                    netDb.tareMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                                    /* netDb.tare_man_no = "";
                                     netDb.tare_man_name = "";
                                     netDb.tare_class = "";
                                     netDb.tare_group = "";*/

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

                                    netDb.netTime = DateTime.Now;

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

                                    netDb.addWeight = (rmX.Data[0].addWeight == null ? 0 : rmX.Data[0].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=已上传
                                                            //要判断下当前是否勾选了不验证皮重

                                    mwaf.weightType = "1";

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

                                    RESTfulResult<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb, monitorNote, PbCache.monitor.validWgt);
                                    if (rmI.Succeed)
                                    {
                                        rt.result = true;
                                        rt.isError = false;
                                        rt.resultInfo = "称重完成";
                                        rt.message = rmI.Message;
                                        PbCache.ResultMessage = "称重完成";
                                        PbCache.actualFirstNo = rmI.Data;
                                        PbCache.isTareWeight = true;//皮重
                                    }
                                    else
                                    {
                                        rt.result = false;
                                        rt.isError = true;
                                        rt.resultInfo = "称重数据保存失败";
                                        PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                                    }
                                }
                                else
                                {
                                    rt.result = false;
                                    rt.isError = true;
                                    rt.resultInfo = PbCache.ResultMessage;
                                }
                            }
                            else
                            {
                                rt.result = false;
                                rt.isError = true;
                                rt.resultInfo = PbCache.ResultMessage;
                            }
                        }
                        else
                        {
                            //重复计量称重完成
                            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】,请点击[语音求助]，由司秤工过磅！";
                        }
                    }
                }
                else //皮重验证不通过
                {
                    //存在历史皮重
                    if (rm.Data != null && rm.Data.Count > 0)
                    {
                        //有历史皮重，且当前重量不一定是皮重
                        //当前重量-历史皮重<1t 则认为是皮重异常，提示
                        //当前重量-历史皮重>1t 则认为是过毛重，此时提示计量毛重需要录入物料等信息
                        long meterWeight = (PbCache.tareCount.contrastTypeNo == "001012001" ? rm.Data[0].upWeight.Value : rm.Data[0].avgWeight.Value);
                        if (PbCache.lockWgt - meterWeight >= AppConfigCache.differenceWgt)
                        {
                            //当前是毛重
                            rt.result = false;
                            rt.isError = true;
                            PbCache.ResultMessage = "当前重量【" + PbCache.lockWgt / 1000 + "T】减历史皮重【" + meterWeight / 1000 + "T】大于结净最低值【" + AppConfigCache.differenceWgt / 1000 + "T】，系统判断为毛重，计量毛重需要录入业务编号信息";
                            rt.resultInfo = "信息输入有误";
                        }
                    }
                    else //没有历史皮重信息
                    {
                        //不存在一次计量数据
                        if (rmX.Data == null || rmX.Data.Count == 0)
                        {
                            //操作日志
                            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 = "0";

                            //此时直接存储一条皮重计量数据，等后面二次上秤计量的时候，根据重量差值来判断前一条与当前这条哪个是毛哪个是皮
                            //存储皮重信息
                            mwaf.weightType = "1";
                            mwaf.addWeight = PbCache.addWgt;
                            RESTfulResult<string> rmI = mwcfs.doAddInnerWf(mwaf, monitorNote, "0");//

                            if (rmI.Succeed)
                            {
                                rt.result = true;
                                rt.resultInfo = "称重完成";
                                rt.message = rmI.Message;
                                PbCache.ResultMessage = "称重完成";
                                PbCache.actualFirstNo = rmI.Data;
                                PbCache.isTareWeight = true;//皮重
                            }
                            else
                            {
                                rt.result = false;
                                rt.isError = true;
                                rt.resultInfo = "称重数据保存失败";
                                PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                            }
                        }
                        else //存在一次计量数据
                        {
                            //看下两次数据差值是否在1t内，若一次减去当前值在1t内则认为是重复计量，若大于且1t且一次为毛重则比较两者时间差是否满足要求
                            //若满足要求，则可进行计量，否则提示时间异常
                            //若时间正常，则毛皮结合，然后保存历史皮重
                            if (Math.Abs(PbCache.lockWgt - rmX.Data[0].meterWeight.Value) < 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】,请点击[语音求助]，由司秤工过磅！";
                            }
                            else
                            {
                                //当前是毛重
                                if (PbCache.lockWgt > rmX.Data[0].meterWeight)
                                {
                                    rt.result = false;
                                    rt.isError = true;
                                    PbCache.ResultMessage = "当前重量【" + PbCache.lockWgt / 1000 + "T】大于一次计量重量【" + rmX.Data[0].meterWeight / 1000 + "T】，系统判断为毛重，计量毛重需要录入业务编号信息";
                                    rt.resultInfo = "信息输入有误";
                                }
                                else //当前是皮,第一次是毛
                                {
                                    //没有历史皮，存在一次计量数据，当前是皮，且计量数据没有物料信息
                                    if (string.IsNullOrEmpty(rmX.Data[0].matterNo))
                                    {
                                        rt.result = false;
                                        rt.isError = true;
                                        rt.resultInfo = "信息输入有误";
                                        PbCache.ResultMessage = "一次计量毛重时，未录入业务编号信息，所以当前计量皮重需要录入业务编号信息";
                                        return rt;
                                    }

                                    //时间间隔
                                    validTimeInterval vtime = new validTimeInterval();
                                    vtime.ValidMethod(new PreTrackScale { meterTypeNo = "001006003" }, "3", rmX.Data[0]);
                                    if (PbCache.monitorResult.valid_time_interval)
                                    {
                                        //回皮时间
                                        validTareTime vttime = new validTareTime();
                                        vttime.ValidMethod(new PreTrackScale { meterTypeNo = "001006003", matterNo = rmX.Data[0].matterNo }, "3", rmX.Data[0]);
                                        if (PbCache.monitorResult.valid_tare_time)
                                        {
                                            scale.matterNo = rmX.Data[0].matterNo;
                                            scale.matterName = rmX.Data[0].matterName;
                                            scale.batchNo = rmX.Data[0].batchNo;
                                            scale.meterTypeNo = "001006003";
                                            scale.forwardingUnitNo = rmX.Data[0].forwardingUnitNo;
                                            scale.forwardingUnitName = rmX.Data[0].forwardingUnitName;
                                            scale.receivingUintNo = rmX.Data[0].receivingUintNo;
                                            scale.receivingUintName = rmX.Data[0].receivingUintName;

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

                                            //netDb.actualFirst1No = mb.actualFirstNo; //皮重计量作业编号
                                            netDb.tareTime = DateTime.Now;
                                            netDb.tareWeight = PbCache.lockWgt;
                                            netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
                                            netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
                                            netDb.tareMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                                            /* netDb.tare_man_no = "";
                                             netDb.tare_man_name = "";
                                             netDb.tare_class = "";
                                             netDb.tare_group = "";*/

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

                                            netDb.netTime = DateTime.Now;

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

                                            netDb.addWeight = (rmX.Data[0].addWeight == null ? 0 : rmX.Data[0].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=已上传
                                                                    //要判断下当前是否勾选了不验证皮重

                                            mwaf.weightType = "1";

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

                                            RESTfulResult<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb, monitorNote, PbCache.monitor.validWgt);
                                            if (rmI.Succeed)
                                            {
                                                rt.result = true;
                                                rt.isError = false;
                                                rt.resultInfo = "称重完成";
                                                rt.message = rmI.Message;
                                                PbCache.ResultMessage = "称重完成";
                                                PbCache.actualFirstNo = rmI.Data;
                                                PbCache.isTareWeight = true;//皮重
                                            }
                                            else
                                            {
                                                rt.result = false;
                                                rt.isError = true;
                                                rt.resultInfo = "称重数据保存失败";
                                                PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                                            }
                                        }
                                        else
                                        {
                                            rt.result = false;
                                            rt.isError = true;
                                            rt.resultInfo = PbCache.ResultMessage;
                                        }
                                    }
                                    else
                                    {
                                        rt.result = false;
                                        rt.isError = true;
                                        rt.resultInfo = PbCache.ResultMessage;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else //若录入了物料等信息
            {
                //如果是皮重
                if (PbCache.monitorResult.valid_wgt)
                {
                    //如果没有一次计量数据
                    if (rmX.Data == null || rmX.Data.Count == 0)
                    {
                        //操作日志
                        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 = "0";

                        //存储皮重信息及历史皮重信息
                        mwaf.weightType = "1";
                        RESTfulResult<string> rmI = mwcfs.doAddInnerWf(mwaf, monitorNote, PbCache.monitor.validWgt);
                        if (rmI.Succeed)
                        {
                            rt.result = true;
                            rt.resultInfo = "称重完成";
                            rt.message = rmI.Message;
                            PbCache.ResultMessage = "称重完成";
                            PbCache.actualFirstNo = rmI.Data;
                            PbCache.isTareWeight = true;//皮重
                        }
                        else
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = "服务端执行失败，计量失败：" + rmI.ResultMessage;
                        }
                    }
                    else //存在一次计量数据
                    {
                        //如果车辆上磅，且重量稳定，当司机点了内倒计量后，判断是内倒业务的车辆后，如果有一次过毛信息，则会锁定界面上的物料信息，按钮不可编辑

                        //两次数据差值是否在1t内，若一次减去当前值在1t内则认为是重复计量，若一次计量减去当前重量>1t 则比较两者时间差是否满足要求,否则
                        //若时间满足要求则看物料是否处于可计量的时间范围内，若时间不允许物料计量，则提示
                        //若时间正常则进行 保存二次计量、历史皮重、结净操作

                        if (rmX.Data[0].meterWeight.Value - PbCache.lockWgt >= AppConfigCache.differenceWgt) //一次为毛，当前为皮 先毛后皮流程
                        {
                            //时间间隔
                            validTimeInterval vtime = new validTimeInterval();
                            vtime.ValidMethod(new PreTrackScale { meterTypeNo = "001006003" }, "3", rmX.Data[0]);
                            if (PbCache.monitorResult.valid_time_interval)
                            {
                                //回皮时间
                                validTareTime vttime = new validTareTime();
                                vttime.ValidMethod(new PreTrackScale { meterTypeNo = "001006003", matterNo = rmX.Data[0].matterNo }, "3", rmX.Data[0]);
                                if (PbCache.monitorResult.valid_tare_time)
                                {
                                    // 结净数据
                                    MeterWorkCarActual netDb = new MeterWorkCarActual();
                                    EntityBase<InnerMeterInfo> entityActual = new EntityBase<InnerMeterInfo>();
                                    netDb = entityBase.format<MeterWorkCarActual>(scale, netDb);

                                    //netDb.actualFirst1No = mb.actualFirstNo; //皮重计量作业编号
                                    netDb.tareTime = DateTime.Now;
                                    netDb.tareWeight = PbCache.lockWgt;
                                    netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
                                    netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
                                    netDb.tareMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                                    /* netDb.tare_man_no = "";
                                     netDb.tare_man_name = "";
                                     netDb.tare_class = "";
                                     netDb.tare_group = "";*/

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

                                    netDb.netTime = DateTime.Now;

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

                                    netDb.addWeight = (rmX.Data[0].addWeight == null ? 0 : rmX.Data[0].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=已上传
                                                            //要判断下当前是否勾选了不验证皮重

                                    mwaf.weightType = "1";

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

                                    RESTfulResult<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb, monitorNote, PbCache.monitor.validWgt);
                                    if (rmI.Succeed)
                                    {
                                        rt.result = true;
                                        rt.isError = false;
                                        rt.resultInfo = "称重完成";
                                        rt.message = rmI.Message;
                                        PbCache.ResultMessage = "称重完成";
                                        PbCache.actualFirstNo = rmI.Data;
                                        PbCache.isTareWeight = true;//皮重
                                    }
                                    else
                                    {
                                        rt.result = false;
                                        rt.isError = true;
                                        rt.resultInfo = "称重数据保存失败";
                                        PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                                    }
                                }
                                else
                                {
                                    rt.result = false;
                                    rt.isError = true;
                                    rt.resultInfo = PbCache.ResultMessage;
                                }
                            }
                            else
                            {
                                rt.result = false;
                                rt.isError = true;
                                rt.resultInfo = PbCache.ResultMessage;
                            }
                        }
                        else
                        {
                            //重复计量
                            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】,请点击[语音求助]，由司秤工过磅！";
                        }
                    }
                }
                else //录入了物料等信息 不确定是皮重还是毛重
                {
                    //存在历史皮重
                    if (rm.Data != null && rm.Data.Count > 0)
                    {
                        //如果没有一次计量数据
                        if (rmX.Data == null || rmX.Data.Count == 0)
                        {
                            //存在历史皮重信息，若当前车辆重量大于历史皮重1t，则认为是过毛，否则提示皮重异常，所有皮重异常都可以跳过验证
                            //如果是过毛则物料是否处于可计量的时间范围内，若时间不允许物料计量，则提示
                            //若时间正常则查看是否存在期限皮重，若存在期限皮重，则当前重量与期限皮结合进行结净
                            //若没有期限皮重，则直接存储毛重信息
                            long meterWeight = (PbCache.tareCount.contrastTypeNo == "001012001" ? rm.Data[0].upWeight.Value : rm.Data[0].avgWeight.Value);
                            if (PbCache.lockWgt - meterWeight >= AppConfigCache.differenceWgt) //当前过毛
                            {
                                //验证物料时间段
                                validMatTime vmt = new validMatTime();
                                vmt.ValidMethod(new PreTrackScale { matterNo = scale.matterNo }, "1");
                                if (!PbCache.monitorResult.valid_mat_time)
                                {
                                    rt.result = false;
                                    rt.isError = true;
                                    rt.resultInfo = PbCache.ResultMessage;
                                    return rt;
                                }

                                #region 获取车辆是否能使用中控的皮重信息

                                int i = 0;
                                MeterBaseTermTareCarService carService = new MeterBaseTermTareCarService();
                                RESTfulResult<List<MeterBaseTermTareCar>> rmCar = carService.doQueryWf(new MeterBaseTermTareCar { carNo = scale.carNo, valueFlag = "0" });
                                if (rmCar.Succeed)
                                {
                                    if (rmCar.Data != null && rmCar.Data.Count > 0)
                                    {
                                        i = rmCar.Data.Where(s => s.meterNatureName !=null && s.meterNatureName.Contains("中控")).Count();
                                    }
                                }
                                else
                                {
                                    //获取服务端数据异常！
                                    rt.result = false;
                                    rt.isError = true;
                                    rt.resultInfo = "称重数据保存失败";
                                    PbCache.ResultMessage = "计量失败，获取期限皮重车辆信息：" + rmCar.ResultMessage;
                                    return rt;
                                }

                                #endregion 获取车辆是否能使用中控的皮重信息

                                //查下是否有期限皮重
                                MeterBaseTermTareDataService mbttd = new MeterBaseTermTareDataService();
                                RESTfulResult<List<MeterBaseTermTareData>> rml = mbttd.doQueryWf(new MeterBaseTermTareData { carNo = scale.carNo, valueFlag = "0" });
                                if (rml.Succeed)
                                {
                                    //存在有效的期限皮重，且物料可用期限皮重
                                    if (i > 0 && rml.Data != null && rml.Data.Count > 0 && PbCache.isTermTare)
                                    {
                                        mwaf.weightType = "0";//毛重信息
                                        mwaf.addWeight = PbCache.addWgt;

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

                                        //netDb.actualFirst1No = mb.actualFirstNo; //皮重计量作业编号
                                        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.tare_man_no = "";
                                            netDb.tare_man_name = "";
                                            netDb.tare_class = "";
                                            netDb.tare_group = "";*/

                                        netDb.actualFirst2No = rml.Data[0].dataNo;
                                        netDb.tareTime = rml.Data[0].createTime;
                                        netDb.tareWeight = rml.Data[0].meterWeight;
                                        netDb.baseSpot2No = rml.Data[0].baseSpotNo;
                                        netDb.baseSpot2Name = rml.Data[0].baseSpotName;
                                        /*netDb.tareMode = "9";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                                        netDb.tare_man_no = "";
                                        netDb.tare_man_name = "";
                                        netDb.tare_class = "";
                                        netDb.tare_group = "";*/

                                        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=已上传

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

                                        RESTfulResult<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb, monitorNote, "0");
                                        if (rmI.Succeed)
                                        {
                                            rt.result = true;
                                            rt.isError = false;
                                            rt.resultInfo = "称重完成";
                                            rt.message = rmI.Message;
                                            PbCache.ResultMessage = "称重完成";
                                            PbCache.actualFirstNo = rmI.Data;
                                            PbCache.isTareWeight = false;//毛重
                                        }
                                        else
                                        {
                                            rt.result = false;
                                            rt.isError = true;
                                            rt.resultInfo = "称重数据保存失败";
                                            PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                                        }
                                    }
                                    else
                                    {
                                        //操作日志
                                        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 = "0";

                                        //保存毛重计量信息
                                        mwaf.weightType = "0";//毛重信息
                                        RESTfulResult<string> rmI = mwcfs.doAddInnerWf(mwaf, monitorNote, "0");
                                        if (rmI.Succeed)
                                        {
                                            rt.result = true;
                                            rt.isError = false;
                                            rt.resultInfo = "称重完成";
                                            rt.message = rmI.Message;
                                            PbCache.ResultMessage = "称重完成";
                                            PbCache.actualFirstNo = rmI.Data;
                                        }
                                        else
                                        {
                                            rt.result = false;
                                            rt.isError = true;
                                            rt.resultInfo = "称重数据保存失败";
                                            PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                                        }
                                    }
                                }
                                else
                                {
                                    //获取服务端数据异常！
                                    rt.result = false;
                                    rt.isError = true;
                                    rt.resultInfo = "称重数据保存失败";
                                    PbCache.ResultMessage = "计量失败：" + rml.ResultMessage;
                                    return rt;
                                }
                            }
                            else //当前过皮，且皮重验证未通过
                            {
                                //皮重异常不允许计量
                                rt.result = false;
                                rt.isError = true;
                                rt.resultInfo = "皮重已经超出偏差";
                                PbCache.ResultMessage = "当前重量【" + PbCache.lockWgt / 1000 + "T】减历史皮重【" + meterWeight / 1000 + "T】小于毛重、皮重判断标准【" + AppConfigCache.differenceWgt / 1000 + "T】，请点击语音求助，由计量员进行远程计量！";
                            }
                        }
                        else //存在历史皮重信息，存在一次计量数据
                        {
                            //若一次计量数据为皮重，且当前重量-一次重量大于1t，则认为当前过毛，此时毛皮结合，小于1t则提示不允许重复计量
                            //若一次计量数据为毛重，且一次重量-当前重量大于1t，则认为当前过皮，此时皮毛结合，小于1t则提示不允许重复计量
                            //毛皮结合后，若不是勾选的不验证皮重，则还需存储历史皮重到表中

                            //rmX 一次数据 rm为历史数据
                            if (rmX.Data[0].weightType == "1") //一次为皮
                            {
                                if (PbCache.lockWgt - rmX.Data[0].meterWeight.Value >= AppConfigCache.differenceWgt) //当前重量-一次重量大于1t
                                {
                                    //当前为毛，验证物料条件
                                    //验证物料时间段
                                    validMatTime vmt = new validMatTime();
                                    vmt.ValidMethod(new PreTrackScale { matterNo = scale.matterNo }, "1");
                                    if (!PbCache.monitorResult.valid_mat_time)
                                    {
                                        rt.result = false;
                                        rt.isError = true;
                                        rt.resultInfo = PbCache.ResultMessage;
                                        return rt;
                                    }

                                    //两次计量时间间隔
                                    validTimeInterval vtime = new validTimeInterval();
                                    vtime.ValidMethod(new PreTrackScale { meterTypeNo = "001006003" }, "3", rmX.Data[0]);
                                    if (PbCache.monitorResult.valid_time_interval)
                                    {
                                        mwaf.weightType = "0";//毛重信息
                                        mwaf.addWeight = PbCache.addWgt;

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

                                        //netDb.actualFirst1No = mb.actualFirstNo; //皮重计量作业编号
                                        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.tare_man_no = "";
                                         netDb.tare_man_name = "";
                                         netDb.tare_class = "";
                                         netDb.tare_group = "";*/

                                        netDb.actualFirst2No = rmX.Data[0].actualFirstNo;
                                        netDb.tareTime = rmX.Data[0].createTime;
                                        netDb.tareWeight = rmX.Data[0].meterWeight;
                                        netDb.baseSpot2No = rmX.Data[0].baseSpotNo;
                                        netDb.baseSpot2Name = rmX.Data[0].baseSpotName;
                                        netDb.tareMode = rmX.Data[0].meterMode;//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                                        netDb.tareManNo = rmX.Data[0].createManNo;
                                        netDb.tareManName = rmX.Data[0].createManName;
                                        netDb.tareClass = rmX.Data[0].meterClass;
                                        netDb.tareGroup = rmX.Data[0].meterGroup;

                                        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=已上传

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

                                        RESTfulResult<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb, monitorNote, "0");
                                        if (rmI.Succeed)
                                        {
                                            rt.result = true;
                                            rt.isError = false;
                                            rt.resultInfo = "称重完成";
                                            rt.message = rmI.Message;
                                            PbCache.ResultMessage = "称重完成";
                                            PbCache.actualFirstNo = rmI.Data;
                                            PbCache.isTareWeight = false;//毛重
                                        }
                                        else
                                        {
                                            rt.result = false;
                                            rt.isError = true;
                                            rt.resultInfo = "称重数据保存失败";
                                            PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                                        }
                                    }
                                    else
                                    {
                                        rt.result = false;
                                        rt.isError = true;
                                        rt.resultInfo = PbCache.ResultMessage;
                                    }
                                }
                                else
                                {
                                    if (PbCache.lockWgt - rmX.Data[0].meterWeight.Value < 0)
                                    {
                                        //如果第一个是皮，第二个重量又小于第一个
                                        rt.result = false;
                                        rt.isError = true;
                                        rt.resultInfo = "存在一次皮重信息，当前重量小于一次皮重皮皮不能结合";
                                        PbCache.ResultMessage = "存在一次皮重信息，当前重量小于一次皮重皮皮不能结合";
                                    }
                                    else
                                    {
                                        //重复计量
                                        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】,请点击[语音求助]，由司秤工过磅！";
                                    }
                                }
                            }
                            else //存在历史皮重信息，存在一次计量数据  一次为毛 当前不确定是毛还是皮
                            {
                                if (rmX.Data[0].meterWeight - PbCache.lockWgt >= AppConfigCache.differenceWgt) //一次-当前> 1t
                                {
                                    long meterWeight = (PbCache.tareCount.contrastTypeNo == "001012001" ? rm.Data[0].upWeight.Value : rm.Data[0].avgWeight.Value);
                                    //先验证 Math.Abs(当前重-历史皮重)<1t 认为是皮，否则提示 毛毛不能结合

                                    if (Math.Abs(PbCache.lockWgt - meterWeight) < AppConfigCache.differenceWgt)
                                    {
                                        //当前为皮
                                        //两次计量时间间隔
                                        validTimeInterval vtime = new validTimeInterval();
                                        vtime.ValidMethod(new PreTrackScale { meterTypeNo = "001006003" }, "3", rmX.Data[0]);
                                        if (PbCache.monitorResult.valid_time_interval)
                                        {
                                            //回皮时间
                                            validTareTime vttime = new validTareTime();
                                            vttime.ValidMethod(new PreTrackScale { meterTypeNo = "001006003", matterNo = rmX.Data[0].matterNo }, "3", rmX.Data[0]);
                                            if (PbCache.monitorResult.valid_tare_time)
                                            {
                                                mwaf.weightType = "1";//皮重信息
                                                //*
                                                // 结净数据
                                                MeterWorkCarActual netDb = new MeterWorkCarActual();
                                                EntityBase<InnerMeterInfo> entityActual = new EntityBase<InnerMeterInfo>();
                                                netDb = entityBase.format<MeterWorkCarActual>(scale, netDb);

                                                //netDb.actualFirst1No = mb.actualFirstNo; //皮重计量作业编号
                                                netDb.tareTime = DateTime.Now;
                                                netDb.tareWeight = PbCache.lockWgt;
                                                netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
                                                netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
                                                netDb.tareMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                                                /* netDb.tare_man_no = "";
                                                    netDb.tare_man_name = "";
                                                    netDb.tare_class = "";
                                                    netDb.tare_group = "";
                                                */

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

                                                netDb.netTime = DateTime.Now;

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

                                                netDb.addWeight = (rmX.Data[0].addWeight == null ? 0 : rmX.Data[0].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 = 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 = "0";

                                                RESTfulResult<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb, monitorNote, PbCache.monitor.validWgt);
                                                if (rmI.Succeed)
                                                {
                                                    rt.result = true;
                                                    rt.isError = false;
                                                    rt.resultInfo = "称重完成";
                                                    rt.message = rmI.Message;
                                                    PbCache.ResultMessage = "称重完成";
                                                    PbCache.actualFirstNo = rmI.Data;
                                                    PbCache.isTareWeight = true;//皮重
                                                }
                                                else
                                                {
                                                    rt.result = false;
                                                    rt.isError = true;
                                                    rt.resultInfo = "称重数据保存失败";
                                                    PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                                                }
                                            }
                                            else
                                            {
                                                rt.result = false;
                                                rt.isError = true;
                                                rt.resultInfo = PbCache.ResultMessage;
                                            }
                                        }
                                        else
                                        {
                                            rt.result = false;
                                            rt.isError = true;
                                            rt.resultInfo = PbCache.ResultMessage;
                                        }
                                    }
                                    else
                                    {
                                        if (PbCache.lockWgt < meterWeight)
                                        {
                                            rt.result = false;
                                            rt.isError = true;
                                            rt.resultInfo = PbCache.ResultMessage;
                                        }
                                        else
                                        {
                                            rt.result = false;
                                            rt.isError = true;
                                            rt.resultInfo = "不允许毛重跟毛重计量";
                                            PbCache.ResultMessage = "保存失败：存在一条未结净毛重记录，请语音求助"; //一次为毛，且当前重量-历史> 1t
                                        }
                                    }
                                }
                                else
                                {
                                    //重复计量
                                    rt.result = false;
                                    rt.isError = true;
                                    rt.resultInfo = "当前重量为【" + PbCache.lockWgt / 1000 + "T】，一次计量重量为【" + rmX.Data[0].meterWeight / 1000 + "】差值小于结净最低值【" + AppConfigCache.differenceWgt / 1000 + "T】，不允许毛重跟毛重计量";
                                    PbCache.ResultMessage = "保存失败：存在一条未结净毛重记录，请语音求助";
                                }
                            }
                        }
                    }
                    else //录入了物料等信息 当前皮重验证不通过 不存在历史皮重
                    {
                        //如果没有一次计量数据
                        if (rmX.Data == null || rmX.Data.Count == 0)
                        {
                            //操作日志
                            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 = "0";

                            //存储一次毛重信息
                            mwaf.weightType = "0";
                            RESTfulResult<string> rmI = mwcfs.doAddInnerWf(mwaf, monitorNote, "0");
                            if (rmI.Succeed)
                            {
                                rt.result = true;
                                rt.isError = false;
                                rt.resultInfo = "称重完成";
                                rt.message = rmI.Message;
                                PbCache.ResultMessage = "称重完成";
                                PbCache.actualFirstNo = rmI.Data;
                                PbCache.isTareWeight = false;//毛重
                            }
                            else
                            {
                                rt.result = false;
                                rt.isError = true;
                                rt.resultInfo = "称重数据保存失败";
                                PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                            }
                        }
                        else
                        {
                            //有一次信息，则当前重量与一次进行比较，若Math.Abs(当前-一次)>1t ，当前大则一次为皮，当前为毛，将一次存储为历史皮，且毛皮结合结净
                            //当前小则一次为毛，当前为皮，将当前存储为历史皮，且毛皮结合结净
                            //若 Math.Abs(当前-一次)<=1t 则提示不允许重复计量
                            if (Math.Abs(PbCache.lockWgt - rmX.Data[0].meterWeight.Value) >= AppConfigCache.differenceWgt)
                            {
                                if (PbCache.lockWgt > rmX.Data[0].meterWeight)
                                {
                                    //一次weightType修改为皮1
                                    if (rmX.Data[0].weightType == "0")
                                    {
                                        mwcfs.doUpdateWf(new MeterWorkCarActualFirst { actualFirstNo = rmX.Data[0].actualFirstNo, weightType = "1" });
                                        lg.WriteLog(3, string.Format("车辆[{0}]计量时，无历史皮重，存在一次记录，当前记录小于一次记录，修改一次weightType[{1}]为1", scale.carNo, rmX.Data[0].weightType));
                                    }

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

                                    //两次计量时间间隔
                                    validTimeInterval vtime = new validTimeInterval();
                                    vtime.ValidMethod(new PreTrackScale { meterTypeNo = "001006003" }, "3", rmX.Data[0]);
                                    if (PbCache.monitorResult.valid_time_interval)
                                    {
                                        mwaf.weightType = "0";//毛重信息
                                        mwaf.addWeight = PbCache.addWgt;

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

                                        //netDb.actualFirst1No = mb.actualFirstNo; //皮重计量作业编号
                                        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.tare_man_no = "";
                                         netDb.tare_man_name = "";
                                         netDb.tare_class = "";
                                         netDb.tare_group = "";*/

                                        netDb.actualFirst2No = rmX.Data[0].actualFirstNo;
                                        netDb.tareTime = rmX.Data[0].createTime;
                                        netDb.tareWeight = rmX.Data[0].meterWeight;
                                        netDb.baseSpot2No = rmX.Data[0].baseSpotNo;
                                        netDb.baseSpot2Name = rmX.Data[0].baseSpotName;
                                        netDb.tareMode = rmX.Data[0].meterMode;//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                                        netDb.tareManNo = rmX.Data[0].createManNo;
                                        netDb.tareManName = rmX.Data[0].createManName;
                                        netDb.tareClass = rmX.Data[0].meterClass;
                                        netDb.tareGroup = rmX.Data[0].meterGroup;
                                        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=已上传

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

                                        RESTfulResult<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb, monitorNote, PbCache.monitor.validWgt);
                                        if (rmI.Succeed)
                                        {
                                            rt.result = true;
                                            rt.isError = false;
                                            rt.resultInfo = "称重完成";
                                            rt.message = rmI.Message;
                                            PbCache.ResultMessage = "称重完成";
                                            PbCache.actualFirstNo = rmI.Data;
                                            PbCache.isTareWeight = false;//毛重
                                        }
                                        else
                                        {
                                            rt.result = false;
                                            rt.isError = true;
                                            rt.resultInfo = "称重数据保存失败";
                                            PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                                        }
                                    }
                                    else
                                    {
                                        rt.result = false;
                                        rt.isError = true;
                                        rt.resultInfo = PbCache.ResultMessage;
                                    }
                                }
                                else
                                {
                                    //一次weightType修改为毛
                                    if (rmX.Data[0].weightType != "0")
                                    {
                                        mwcfs.doUpdateWf(new MeterWorkCarActualFirst { actualFirstNo = rmX.Data[0].actualFirstNo, weightType = "0" });
                                        lg.WriteLog(3, string.Format("车辆[{0}]计量时，无历史皮重，存在一次记录，当前记录小于一次记录，修改一次weightType[{1}]为0", scale.carNo, rmX.Data[0].weightType));
                                    }

                                    //当前为皮重
                                    //两次计量时间间隔
                                    validTimeInterval vtime = new validTimeInterval();
                                    vtime.ValidMethod(new PreTrackScale { meterTypeNo = "001006003" }, "3", rmX.Data[0]);
                                    if (PbCache.monitorResult.valid_time_interval)
                                    {
                                        //验证回皮时间,必须是先毛后皮的流程才验证回皮时间
                                        validTareTime vttime = new validTareTime();
                                        vttime.ValidMethod(new PreTrackScale { meterTypeNo = "001006003", matterNo = rmX.Data[0].matterNo }, "3", rmX.Data[0]);
                                        if (PbCache.monitorResult.valid_tare_time)
                                        {
                                            mwaf.weightType = "1";

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

                                            //netDb.actualFirst1No = mb.actualFirstNo; //皮重计量作业编号
                                            netDb.tareTime = DateTime.Now;
                                            netDb.tareWeight = PbCache.lockWgt;
                                            netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
                                            netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
                                            netDb.tareMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                                            /* netDb.tare_man_no = "";
                                             netDb.tare_man_name = "";
                                             netDb.tare_class = "";
                                             netDb.tare_group = "";*/

                                            netDb.actualFirst1No = rmX.Data[0].actualFirstNo;
                                            netDb.grossTime = rmX.Data[0].createTime;
                                            netDb.grossWeight = rmX.Data[0].meterWeight;
                                            netDb.baseSpot1No = rmX.Data[0].baseSpotNo;
                                            netDb.baseSpot1Name = rmX.Data[0].baseSpotName;
                                            netDb.grossMode = rmX.Data[0].meterMode;//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                                            netDb.grossManNo = rmX.Data[0].createManNo;
                                            netDb.grossManName = rmX.Data[0].createManName;
                                            netDb.grossClass = rmX.Data[0].meterClass;
                                            netDb.grossGroup = rmX.Data[0].meterGroup;
                                            netDb.netTime = DateTime.Now;

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

                                            netDb.addWeight = (rmX.Data[0].addWeight == null ? 0 : rmX.Data[0].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 = 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 = "0";

                                            RESTfulResult<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb, monitorNote, PbCache.monitor.validWgt);
                                            if (rmI.Succeed)
                                            {
                                                rt.result = true;
                                                rt.isError = false;
                                                rt.resultInfo = "称重完成";
                                                rt.message = rmI.Message;
                                                PbCache.ResultMessage = "称重完成";
                                                PbCache.actualFirstNo = rmI.Data;
                                                PbCache.isTareWeight = true;//皮重
                                            }
                                            else
                                            {
                                                rt.result = false;
                                                rt.isError = true;
                                                rt.resultInfo = "称重数据保存失败";
                                                PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                                            }
                                        }
                                        else
                                        {
                                            rt.result = false;
                                            rt.isError = true;
                                            rt.resultInfo = PbCache.ResultMessage;
                                        }
                                    }
                                    else
                                    {
                                        rt.result = false;
                                        rt.isError = true;
                                        rt.resultInfo = PbCache.ResultMessage;
                                    }
                                }
                            }
                            else
                            {
                                //重复计量
                                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;
        }
    }
}