using com.hnshituo.core.webapp.vo;
using Common;
using Common.DbOption.work;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MeterSceneLibrary
{
    /// <summary>
    /// 酒钢厂内车辆计量业务
    /// </summary>
    public class InnerCarJISCO
    {
        // 一次计量数据
        private MeterWorkCarActualFirstService mwcfs = new MeterWorkCarActualFirstService();

        // 重量类型预处理表
        private MeterWorkPreWgttypeService wgttypeService = new MeterWorkPreWgttypeService();

        // 车辆委托表
        private PreTrackScaleService preTrackScaleService = new PreTrackScaleService();

        // 留期限皮操作
        private MeterBaseTermTareDataService termTareDate = new MeterBaseTermTareDataService();

        private rtInfo rt = new rtInfo();

        // 构建一次计量数据
        private MeterWorkCarActualFirst mwaf = new MeterWorkCarActualFirst();

        // 结净最小值，默认取配置文件
        private double diffrentWgt = AppConfigCache.differenceWgt;

        // 根据称点查询监控信息
        private RESTfulResult<List<MeterWorkMonitor>> listMonitor;

        // 根据车号查询委托表中未使用和使用中的委托
        private List<PreTrackScale> listPreScale;

        private JISCO.SaveMethod saveMethod = new JISCO.SaveMethod();

        public rtInfo MeterMethod(PreTrackScale scale)
        {
            Log lg = Log.GetInstance();
            rt.isError = false;

            string ResultMessage = "";
            bool isError = false;

            //根据称点查询监控信息
            MeterWorkMonitorService meterWorkMonitorService = new MeterWorkMonitorService();
            listMonitor = meterWorkMonitorService.doQueryOneWf(new MeterWorkMonitor { pointNo = PbCache.sportInfo.baseSpotNo });

            mwaf.carNo = PbCache.lockCarNo;
            mwaf.weightType = "";  // 默认重量类型为空
            mwaf.actualFirstNo = DateTime.Now.ToString("yyyyMMdd");//10
            mwaf.meterWeight = PbCache.lockWgt;//计量重量
            mwaf.addWeight = PbCache.addWgt;//附加重量
            mwaf.createManNo = "admin";
            mwaf.createManName = "admin";
            mwaf.meterGroup = "";
            mwaf.meterClass = "";
            mwaf.createManNo = "SYSTEM";
            mwaf.createManName = "system";
            mwaf.createTime = DateTime.Now;
            mwaf.baseSpotNo = PbCache.sportInfo.baseSpotNo;//计量点编号
            mwaf.baseSpotName = PbCache.sportInfo.baseSpotName;
            //mwaf.memo = "";
            mwaf.valueFlag = "1"; //状态（0 = 作废，1 = 有效(已匹配)，2 = 结净）
            mwaf.updateManName = "";
            mwaf.updateManNo = "";
            mwaf.updateTime = null;
            mwaf.checkFlag = "1"; //0未审核,1已审核
            mwaf.meterMode = "2"; //1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            mwaf.addWeight = PbCache.addWgt;
            mwaf.dataSource = "1"; //数据来源(1=智能终端，2=本地计量，3=应急计量，4=手动录入(计量票据录入))
            PbCache.isActual = false;
            PbCache.isInnerCar = false;
            PbCache.ResultMessage = "";

            // 根据车号查询重量类型预处理表
            RESTfulResult<List<MeterWorkPreWgttype>> rmX = wgttypeService.doQueryWf(new MeterWorkPreWgttype { carNo = PbCache.lockCarNo, valueFlag = "1" });

            // 不存在未使用的重量类型数据
            if (rmX.Succeed)
            {
                List<MeterWorkPreWgttype> list = rmX.Data;

                // 不存在一次预处理数据
                if (list == null || list.Count == 0)
                {
                    /* a. 程序报警
                     * b. 自动进行语音求助
                     */
                    rt = saveMethod.doError("未选择称重类型，请选择后开始计量");
                    PbCache.monitorResultMessage = "未选择称重类型，请选择后开始计量";
                }
                // 存在一条预处理数据
                else if (list == null || list.Count == 1)
                {
                    // 查已选择的委托
                    RESTfulResult<List<PreTrackScale>> rmScale = preTrackScaleService.doQueryByFlag(new PreTrackScale { carNo = PbCache.lockCarNo, lineDesc = "1" });

                    if (rmScale.Succeed && rmScale.Data.Count == 1)
                    {
                        // 重量类型为期限皮重
                        if (list[0].weightTypeNo == "001033003")
                        {
                            // 判断这个车是否有留存期限皮重的权限
                            MeterBaseTermTareCarService meterBaseTermTareCarService = new MeterBaseTermTareCarService();
                            RESTfulResult<List<MeterBaseTermTareCar>> tareCarResult = meterBaseTermTareCarService.doQueryWf(new MeterBaseTermTareCar { carNo = PbCache.lockCarNo, valueFlag = "0" });

                            if (tareCarResult.Succeed)
                            {
                                // 没有期限皮重权限
                                if (tareCarResult.Data == null || tareCarResult.Data.Count == 0)
                                {
                                    rt = saveMethod.doError("该车辆没有期限皮重权限，请联系计量大厅");
                                    PbCache.monitorResultMessage = "该车辆没有期限皮重权限";
                                }
                                // 有期限皮重权限 
                                else if (tareCarResult.Data.Count >= 1)
                                {
                                    // 正常保存期限皮重
                                    rt = saveMethod.doTermTareData();
                                }
                            }
                        }
                        // 重量类型为普通毛重/皮重
                        else if (list[0].weightTypeNo == "001033001" || list[0].weightTypeNo == "001033002")
                        {
                            // 查询一次计量数据表
                            RESTfulResult<List<MeterWorkCarActualFirst>> rmFirst = mwcfs.doQueryByFlag(new MeterWorkCarActualFirst { carNo = PbCache.lockCarNo });

                            if (rmX.Succeed)
                            {
                                List<MeterWorkCarActualFirst> firstList = rmFirst.Data;

                                // 不存在有效的一次计量数据
                                if (firstList == null || firstList.Count == 0)
                                {
                                    // 历史皮重验证
                                    rt = saveMethod.validHisTare(list[0].weightTypeNo);

                                    // 重量类型为皮重且在皮重误差内
                                    if (list[0].weightTypeNo == "001033002" && PbCache.monitorResult.valid_wgt == true)
                                    {
                                        /*sds
                                        * a. 正常保存重量匹配委托
                                        * b. 提示称重完成
                                        */
                                        mwaf.weightType = "1";
                                        rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                        PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，计量完成", PbCache.lockCarNo, PbCache.lockWgt);
                                    }
                                    // 重量类型为皮重且在皮重误差外
                                    else if (list[0].weightTypeNo == "001033002" && PbCache.monitorResult.valid_wgt == false)
                                    {
                                        if (PbCache.ResultMessage == "")
                                        {
                                            PbCache.ResultMessage = "重量类型为皮重且不在皮重误差内";
                                        }
                                        rt = saveMethod.doError(PbCache.ResultMessage);
                                        rt.isWarn = true;
                                        rt.WarnContent = "validWgt";
                                    }
                                    // 重量类型为毛重，在皮重误差内
                                    else if (list[0].weightTypeNo == "001033001" && PbCache.monitorResult.valid_wgt == true)
                                    {
                                        if (PbCache.ResultMessage == "")
                                        {
                                            PbCache.ResultMessage = "重量类型为毛重，在皮重误差内";
                                        }
                                        rt = saveMethod.doError(PbCache.ResultMessage);
                                        rt.isWarn = true;
                                        rt.WarnContent = "validWgt";
                                    }
                                    // 重量类型为毛重，不在皮重误差内
                                    else if (list[0].weightTypeNo == "001033001" && PbCache.monitorResult.valid_wgt == false)
                                    {
                                        // 判断车辆是否具有期限皮重权限
                                        MeterBaseTermTareCarService meterBaseTermTareCarService = new MeterBaseTermTareCarService();
                                        RESTfulResult<List<MeterBaseTermTareCar>> tareCarResult = meterBaseTermTareCarService.doQueryWf(new MeterBaseTermTareCar { carNo = PbCache.lockCarNo, valueFlag = "0" });

                                        if (tareCarResult.Succeed)
                                        {
                                            // 没有期限皮重权限
                                            if (tareCarResult.Data == null || tareCarResult.Data.Count == 0)
                                            {
                                                mwaf.weightType = "0";
                                                rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                                PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，计量完成", PbCache.lockCarNo, PbCache.lockWgt);
                                            }
                                            // 有期限皮重权限 
                                            else if (tareCarResult.Data.Count >= 1)
                                            {
                                                //查下当前拖的物料是否可用期限皮;验证业务类型是否允许使用期限皮重
                                                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='" + rmScale.Data[0].matterNo + @"'
                                                and t2.is_allow_term_tare = '1' and  t2.meter_type_no = '" + rmScale.Data[0].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)
                                                    {
                                                        // 判断收发货单位是否具有期限皮重权限
                                                        MeterBaseUnitInfoService meterBaseUnitInfoService = new MeterBaseUnitInfoService();
                                                        MeterBaseUnitInfo meterBaseUnitInfo = new MeterBaseUnitInfo();
                                                        meterBaseUnitInfo.forwardingUnitName = rmScale.Data[0].forwardingUnitName;
                                                        meterBaseUnitInfo.forwardingUnitNo = rmScale.Data[0].forwardingUnitNo;
                                                        meterBaseUnitInfo.receivingUnitName = rmScale.Data[0].receivingUintName;
                                                        meterBaseUnitInfo.receivingUnitNo = rmScale.Data[0].receivingUintNo;
                                                        RESTfulResult<List<MeterBaseUnitInfo>> tareUnitResult = meterBaseUnitInfoService.doQueryWf(meterBaseUnitInfo);
                                                        if (tareUnitResult.Succeed)
                                                        {
                                                            if (tareUnitResult.Data == null || tareUnitResult.Data.Count == 0)
                                                            {
                                                                mwaf.weightType = "0";
                                                                // 只保存一次计量数据
                                                                rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                                                PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，计量完成", PbCache.lockCarNo, PbCache.lockWgt);
                                                            }
                                                            else
                                                            {
                                                                MeterBaseTermTareDataService meterBaseTermTareDataService = new MeterBaseTermTareDataService();
                                                                RESTfulResult<MeterBaseTermTareData> termTareData = meterBaseTermTareDataService.doQueryPageWf(new MeterBaseTermTareData { carNo = PbCache.lockCarNo, valueFlag = "0" });
                                                                if (termTareData.Succeed)
                                                                {
                                                                    if (termTareData.Data == null)
                                                                    {
                                                                        mwaf.weightType = "0";
                                                                        // 只保存一次计量数据
                                                                        rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                                                        PbCache.ResultMessage = "计量成功，期限皮不存在或者已过期，请留存期限皮";
                                                                        PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，计量完成,期限皮不存在或者已过期，请留存期限皮", PbCache.lockCarNo, PbCache.lockWgt);
                                                                    }
                                                                    else
                                                                    {
                                                                        // 存在使用期限皮重权限，并使用期限皮重洁净
                                                                        List<string> l = new List<string>();
                                                                        l.Add("create");
                                                                        //给一次数据匹配委托
                                                                        EntityBase<PreTrackScale> entityBase1 = new EntityBase<PreTrackScale>();
                                                                        mwaf = entityBase1.format<MeterWorkCarActualFirst>(rmScale.Data[0], mwaf);
                                                                        mwaf.isPreScale = "1";
                                                                        mwaf.weightType = "0";

                                                                        rt = saveMethod.doNetTermTare(rmScale.Data[0], mwaf);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        mwaf.weightType = "0";
                                                        // 只保存一次计量数据
                                                        rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                                        PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，计量完成", PbCache.lockCarNo, PbCache.lockWgt);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                // 只存在一条有效的一次计量数据
                                else if (firstList == null || firstList.Count == 1)
                                {
                                    // 读结净最小值配置
                                    ComBaseInfoService cbis = new ComBaseInfoService();
                                    RESTfulResult<List<ComBaseInfo>> res = cbis.doQueryBaseList(new ComBaseInfo { baseCode = "001031001" });
                                    if (res.Succeed)
                                    {
                                        if (res.Data != null && res.Data.Count == 1)
                                        {
                                            try
                                            {
                                                diffrentWgt = Double.Parse(res.Data[0].baseName);
                                            }
                                            finally
                                            {

                                            }
                                        }
                                    }
                                    // 当次重量和上次重量满足洁净最小值条件
                                    if (Math.Abs(rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt) > diffrentWgt || listMonitor.Data[0].validPredictionDiff == "0")
                                    {
                                        //判断上一次的一次数据是为皮重的，这次为毛重,上一次重量比这一次大；
                                        //上一次为皮重4200，这一次是毛重3200
                                        if (firstList[0].weightType == "1" && list[0].weightTypeNo == "001033001" &&  rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt > 0)
                                        {
                                            EntityBase<PreTrackScale> entityBase1 = new EntityBase<PreTrackScale>();
                                            mwaf = entityBase1.format<MeterWorkCarActualFirst>(rmScale.Data[0], mwaf);
                                            mwaf.weightType = "0";
                                            mwaf.isPreScale = "1";
                                            mwaf.actualFirstNo = "";

                                            rt = saveMethod.doNetError(rmScale.Data[0], firstList[0], mwaf,  mwaf);
                                            return rt;
                                        }
                                        //判断上一次的一次数据是为毛重的，这次为皮重,上一次重量比这一次小；
                                        if (firstList[0].weightType == "0" && list[0].weightTypeNo == "001033002" && rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt < 0)
                                        {
                                            EntityBase<PreTrackScale> entityBase1 = new EntityBase<PreTrackScale>();
                                            mwaf = entityBase1.format<MeterWorkCarActualFirst>(rmScale.Data[0], mwaf);
                                            mwaf.weightType = "1";
                                            mwaf.isPreScale = "1";
                                            mwaf.actualFirstNo = "";

                                            rt = saveMethod.doNetError(rmScale.Data[0],  mwaf, firstList[0], mwaf);
                                            return rt;
                                        }
                                        // 查询当前秤点是否启用历史皮重判断（暂时不考虑此限制）By BourneCao
                                        // String spotNo = PbCache.sportInfo.baseSpotNo;
                                        // 历史皮重验证
                                        rt = saveMethod.validHisTare(list[0].weightTypeNo);

                                        // 重量类型为皮重且在皮重误差内，正常结净
                                        if (list[0].weightTypeNo == "001033002" && PbCache.monitorResult.valid_wgt == true)
                                        {
                                            // 正常结净

                                            //给一次数据匹配委托
                                            EntityBase<PreTrackScale> entityBase1 = new EntityBase<PreTrackScale>();
                                            mwaf = entityBase1.format<MeterWorkCarActualFirst>(rmScale.Data[0], mwaf);
                                            mwaf.isPreScale = "1";

                                            rt = saveMethod.doNet(rmScale.Data[0], firstList[0], mwaf);
                                            PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，净重{2}，计量完成", PbCache.lockCarNo, PbCache.lockWgt, Math.Abs(rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt));
                                        }
                                        // 重量类型为皮重且不在皮重误差内
                                        else if (list[0].weightTypeNo == "001033002" && PbCache.monitorResult.valid_wgt == false)
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "重量类型为皮重且不在皮重误差内";
                                            }
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                        }
                                        // 重量类型为毛重，不在皮重误差内，正常结净
                                        else if (list[0].weightTypeNo == "001033001" && PbCache.monitorResult.valid_wgt == false)
                                        {
                                            if (firstList[0].weightType == "0")
                                            {
                                                mwaf.weightType = "0";
                                                rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                                PbCache.ResultMessage = "无皮重信息，无法结净，请计量皮重";
                                                rt.isWarn = true;
                                                rt.WarnContent = "validWgt";
                                            }
                                            else if(firstList[0].weightType == "1")
                                            {
                                                // 判断车辆是否具有期限皮重权限
                                                MeterBaseTermTareCarService meterBaseTermTareCarService = new MeterBaseTermTareCarService();
                                                RESTfulResult<List<MeterBaseTermTareCar>> tareCarResult = meterBaseTermTareCarService.doQueryWf(new MeterBaseTermTareCar { carNo = PbCache.lockCarNo, valueFlag = "0" });

                                                if (tareCarResult.Succeed)
                                                {
                                                    // 没有期限皮重权限
                                                    if (tareCarResult.Data == null || tareCarResult.Data.Count == 0)
                                                    {
                                                        // 正常结净
                                                        //给一次数据匹配委托
                                                        EntityBase<PreTrackScale> entityBase1 = new EntityBase<PreTrackScale>();
                                                        mwaf = entityBase1.format<MeterWorkCarActualFirst>(rmScale.Data[0], mwaf);
                                                        mwaf.isPreScale = "1";
                                                        mwaf.weightType = "0";

                                                        rt = saveMethod.doNet(rmScale.Data[0], firstList[0], mwaf);
                                                        PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，净重{2}，计量完成", PbCache.lockCarNo, PbCache.lockWgt, Math.Abs(rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt));
                                                    }
                                                    // 有期限皮重权限 
                                                    else if (tareCarResult.Data.Count >= 1)
                                                    {
                                                        //查下当前拖的物料是否可用期限皮;验证业务类型是否允许使用期限皮重
                                                        string sqls = @"select t.matter_no id, t.matter_name text
                                                                        from meter_base_matter_info t
                                                                        where t.allowed_tare_weight = '1'
                                                                        and t.valid_flag = '1'
                                                                        and t.matter_no='" + rmScale.Data[0].matterNo + "'";

                                                        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)
                                                            {
                                                                // 判断收发货单位是否具有期限皮重权限
                                                                MeterBaseUnitInfoService meterBaseUnitInfoService = new MeterBaseUnitInfoService();
                                                                MeterBaseUnitInfo meterBaseUnitInfo = new MeterBaseUnitInfo();
                                                                meterBaseUnitInfo.forwardingUnitName = rmScale.Data[0].forwardingUnitName;
                                                                meterBaseUnitInfo.forwardingUnitNo = rmScale.Data[0].forwardingUnitNo;
                                                                meterBaseUnitInfo.receivingUnitName = rmScale.Data[0].receivingUintName;
                                                                meterBaseUnitInfo.receivingUnitNo = rmScale.Data[0].receivingUintNo;
                                                                meterBaseUnitInfo.allowedTareWeight = "1";
                                                                RESTfulResult<List<MeterBaseUnitInfo>> tareUnitResult = meterBaseUnitInfoService.doQueryWf(meterBaseUnitInfo);
                                                                if (tareUnitResult.Succeed)
                                                                {
                                                                    if (tareUnitResult.Data == null || tareUnitResult.Data.Count == 0)
                                                                    {
                                                                        // 正常结净
                                                                        //给一次数据匹配委托
                                                                        EntityBase<PreTrackScale> entityBase3 = new EntityBase<PreTrackScale>();
                                                                        mwaf = entityBase3.format<MeterWorkCarActualFirst>(rmScale.Data[0], mwaf);
                                                                        mwaf.isPreScale = "1";
                                                                        mwaf.weightType = "0";

                                                                        rt = saveMethod.doNet(rmScale.Data[0], firstList[0], mwaf);
                                                                        PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，净重{2}，计量完成", PbCache.lockCarNo, PbCache.lockWgt, Math.Abs(rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt));
                                                                    }
                                                                    else
                                                                    {

                                                                        MeterBaseTermTareDataService meterBaseTermTareDataService = new MeterBaseTermTareDataService();
                                                                        RESTfulResult<MeterBaseTermTareData> termTareData = meterBaseTermTareDataService.doQueryPageWf(new MeterBaseTermTareData { carNo = PbCache.lockCarNo, valueFlag = "0" });
                                                                        if (termTareData.Succeed)
                                                                        {
                                                                            if (termTareData.Data == null)
                                                                            {
                                                                                // 正常结净
                                                                                //给一次数据匹配委托
                                                                                EntityBase<PreTrackScale> entityBase3 = new EntityBase<PreTrackScale>();
                                                                                mwaf = entityBase3.format<MeterWorkCarActualFirst>(rmScale.Data[0], mwaf);
                                                                                mwaf.isPreScale = "1";
                                                                                mwaf.weightType = "0";

                                                                                rt = saveMethod.doTermTareNet(rmScale.Data[0], firstList[0], mwaf);
                                                                            }
                                                                            else
                                                                            {
                                                                                // 存在使用期限皮重权限，并使用期限皮重洁净
                                                                                List<string> l = new List<string>();
                                                                                l.Add("create");
                                                                                //给一次数据匹配委托
                                                                                EntityBase<PreTrackScale> entityBase1 = new EntityBase<PreTrackScale>();
                                                                                mwaf = entityBase1.format<MeterWorkCarActualFirst>(rmScale.Data[0], mwaf);
                                                                                mwaf.isPreScale = "1";
                                                                                mwaf.weightType = "0";

                                                                                rt = saveMethod.doNetTermTare(rmScale.Data[0], mwaf);
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                // 正常结净
                                                                //给一次数据匹配委托
                                                                EntityBase<PreTrackScale> entityBase2 = new EntityBase<PreTrackScale>();
                                                                mwaf = entityBase2.format<MeterWorkCarActualFirst>(rmScale.Data[0], mwaf);
                                                                mwaf.isPreScale = "1";
                                                                mwaf.weightType = "0";

                                                                rt = saveMethod.doNet(rmScale.Data[0], firstList[0], mwaf);
                                                                PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，净重{2}，计量完成", PbCache.lockCarNo, PbCache.lockWgt, Math.Abs(rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt));
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        // 重量类型为毛重，在皮重误差内
                                        else if (list[0].weightTypeNo == "001033001" && PbCache.monitorResult.valid_wgt == true)
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "重量类型为毛重，在皮重误差内";
                                            }
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                        }
                                    }
                                    // 重量类型为毛重，不在皮重误差内，正常结净
                                    else if (list[0].weightTypeNo == "001033001" && PbCache.monitorResult.valid_wgt == false)
                                    {
                                        mwaf.weightType = "0";
                                        rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                        PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，计量完成", PbCache.lockCarNo, PbCache.lockWgt);
                                    }
                                    else
                                    {
                                        rt = saveMethod.doError("第一次计量重量【" + rmFirst.Data[0].meterWeight.Value / 1000 + "T】和第二次计量重量【" + PbCache.lockWgt / 1000 + "T】接近，小于结净最小值【" + diffrentWgt / 1000 + " T】");
                                        rt.isWarn = true;
                                        rt.WarnContent = "validWgt";
                                    }
                                }
                                // 存在多条有效的一次计量数据
                                else if (firstList == null || firstList.Count > 1)
                                {
                                    /* a. 程序报警a
                                     * b. 自动进行语音求助
                                     */
                                    //多条一次记录下判断是否全为毛重，是全部结净
                                    if (firstList[firstList.Count-1].weightType == "0" && list[0].weightTypeNo == "001033002")
                                    {
                                        mwaf.weightType = "1";
                                        rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                        PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，计量完成", PbCache.lockCarNo, PbCache.lockWgt);
                                    }
                                    else if (firstList[firstList.Count - 1].weightType == "0" && list[0].weightTypeNo == "001033001")
                                    {
                                        mwaf.weightType = "0";
                                        rt = saveMethod.doAddFirst(rmScale.Data[0],mwaf);
                                        PbCache.ResultMessage = "无皮重信息，无法结净，请计量皮重";
                                        rt.isWarn = true;
                                        rt.WarnContent = "validWgt";
                                    }
                                    else
                                    {
                                        rt = saveMethod.doError("存在多条有效的一次计量数据，请联系计量大厅");
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (rmScale.Succeed && rmScale.Data.Count == 0)
                        {
                            // 查询一次计量数据表
                            RESTfulResult<List<MeterWorkCarActualFirst>> rmFirst = mwcfs.doQueryByFlag(new MeterWorkCarActualFirst { carNo = PbCache.lockCarNo });

                            if (rmFirst.Succeed)
                            {
                                List<MeterWorkCarActualFirst> firstList = rmFirst.Data;

                                // 重量类型为皮重
                                if (list[0].weightTypeNo == "001033002")
                                {
                                    // 历史皮重验证
                                    rt = saveMethod.validHisTare(list[0].weightTypeNo);
                                    if (firstList == null || firstList.Count == 0)
                                    {
                                        //在皮重误差内
                                        if (PbCache.monitorResult.valid_wgt == true || listMonitor.Data[0].validPredictionDiff == "0")
                                        {
                                            //正常保存一次数据
                                            mwaf.weightType = "1";
                                            rt = saveMethod.doAddFirst(null, mwaf);
                                            PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，计量完成", PbCache.lockCarNo, PbCache.lockWgt);
                                        }
                                        else
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "重量类型为皮重且不在皮重误差内";
                                            }
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                            rt.isWarn = true;
                                            rt.WarnContent = "validWgt";
                                        }
                                    }
                                    else if (firstList.Count == 1 && firstList[0].weightType != "0")
                                    {
                                        // 在皮重误差内
                                        if (PbCache.monitorResult.valid_wgt == true || listMonitor.Data[0].validPredictionDiff == "0")
                                        {
                                           //保存一条作废的皮重数据
                                            mwaf.weightType = "1";
                                            mwaf.valueFlag = "0";
                                            rt = saveMethod.doAddFirst(null, mwaf);
                                            PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，计量完成", PbCache.lockCarNo, PbCache.lockWgt);
                                        }
                                        else
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "重量类型为皮重且不在皮重误差内";
                                            }
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                            rt.isWarn = true;
                                            rt.WarnContent = "validWgt";
                                        }
                                    }
                                    else if (firstList.Count == 1 && firstList[0].weightType == "0")
                                    {
                                        if (PbCache.monitorResult.valid_wgt == true || listMonitor.Data[0].validPredictionDiff == "0")
                                        {
                                            //匹配结净
                                            rt = saveMethod.doNet(null, firstList[0], mwaf);
                                            PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}，净重{2}，计量完成", PbCache.lockCarNo, PbCache.lockWgt, Math.Abs(rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt));
                                        }
                                        else
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "重量类型为皮重且不在皮重误差内";
                                            }
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                            rt.isWarn = true;
                                            rt.WarnContent = "validWgt";
                                        }
                                    }
                                    // 存在多条有效的一次计量数据
                                    else if (firstList == null || firstList.Count > 1)
                                    {
                                        /* a. 程序报警a
                                         * b. 自动进行语音求助
                                         */
                                        //多条一次记录下判断是否全为毛重，是全部结净
                                        if (firstList[firstList.Count - 1].weightType == "0" && list[0].weightTypeNo == "001033002")
                                        {
                                            //给一次数据匹配委托
                                            mwaf.weightType = "0";

                                            rt = saveMethod.doNetAddMuch(mwaf);
                                        }
                                    }
                                }
                                else
                                {
                                    rt = saveMethod.doError("委托匹配异常，无法计量，请联系处理");
                                    rt.isWarn = true;
                                    rt.WarnContent = "validIsPre";
                                }
                            }
                        }
                        else
                        {
                            rt = saveMethod.doError("存在多条已选择的委托，请联系计量大厅");
                            rt.isWarn = true;
                            rt.WarnContent = "validIsPre";
                        }
                    }
                }
                else
                {
                    rt = saveMethod.doError("存在多条上称申请数据，请联系计量大厅");
                }
            }
            return rt;
        }
    }
}
