using com.hnshituo.core.webapp.vo;
using Common;
using Common.DbOption.work;
using System;
using System.Collections.Generic;
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 MeterBaseSpotInfoService meterBaseSpotInfoService = new MeterBaseSpotInfoService();

        private rtInfo rt = new rtInfo();

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

        private MeterBaseCarService carService = new MeterBaseCarService();

        private MeterBaseSpecialUnitService meterBaseSpecialUnitService = new MeterBaseSpecialUnitService();

        // 结净最小值，默认取配置文件
        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 = "";
            PbCache.monitorResultMessage = "";
            PbCache.LEDResultMessage = "";

            PbCache.yardActual = null;
            PbCache.yardFirst = null;

            // 根据车号查询重量类型预处理表
            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.voiceType = 21;
                    PbCache.monitorResultMessage = "未选择称重类型，请选择后开始计量";
                    PbCache.LEDResultMessage = "未选择称重类型，请选择后开始计量";
                    PbCache.ResultMessage = "未选择称重类型，请选择后开始计量";
                    return rt;
                }
                // 存在一条预处理数据
                else if (list == null || list.Count == 1)
                {
                    // 历史皮重验证
                    // 001033002：皮重
                    rt = saveMethod.validHisTare2(list[0].weightTypeNo);
                    if (list[0].weightTypeNo == "001033002" && PbCache.monitorResult.valid_wgt == false && listMonitor.Data[0].validWgt == "1")
                    {
                        
                        PbCache.ResultMessage = "毛皮类型选错，请重新选择";
                        PbCache.LEDResultMessage = "计量失败，毛皮类型选错，请重新选择";

                        PbCache.voiceType = 26;
                        rt = saveMethod.doError(PbCache.ResultMessage);
                        rt.isWarn = true;
                        rt.WarnContent = "validWgt";
                        return rt;
                    }
                    // 001033001：毛重
                    if (list[0].weightTypeNo == "001033001" && PbCache.monitorResult.valid_wgt == true && listMonitor.Data[0].validWgt == "1")
                    {
                        
                        PbCache.ResultMessage = "毛皮类型选错，请重新选择";
                        PbCache.LEDResultMessage = "计量失败，毛皮类型选错，请重新选择";

                        PbCache.voiceType = 23;
                        rt = saveMethod.doError(PbCache.ResultMessage);
                        rt.isWarn = true;
                        rt.WarnContent = "validWgt";
                        return rt;
                    }

                    //拿到车辆
                    RESTfulResult<List<MeterBaseCar>> resCarList = carService.doQueryCarList(new MeterBaseCar { carNo = PbCache.lockCarNo });
                    RESTfulResult<List<MeterBaseSpecialUnit>> sunitList = meterBaseSpecialUnitService.doQueryCarList(new MeterBaseSpecialUnit { unitNo = resCarList.Data[0].usetDepartmentNo });

                    if (resCarList.Data[0].usetDepartmentNo == "888888")
                    {}
                    else
                    {
                        if (sunitList.Data == null || sunitList.Data.Count != 1)
                        { }
                        else
                        {
                            bool isGorss = rmX.Data[0].submitFlag == "1" && rmX.Data[0].weightTypeNo == "001033001";
                            if (isGorss || rmX.Data[0].weightTypeNo == "001033002")
                            { }
                            else
                            {
                                rt = saveMethod.doError("");
                                rt.resultInfo = string.Format("车号{0}，当前毛重{1}吨，请确认重量", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                PbCache.LEDResultMessage = string.Format("车号{0}，当前毛重{1}吨，请确认重量", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                PbCache.voiceType = 12;
                                return rt;
                            }
                        }
                    }


                    // 查已选择的委托
                    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 = "该车辆没有期限皮重权限";
                                    return rt;
                                }
                                // 有期限皮重权限 
                                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 || listMonitor.Data[0].validWgt == "0"))
                                    {
                                        /*sds
                                        * a. 正常保存重量匹配委托
                                        * b. 提示称重完成
                                        */
                                        mwaf.weightType = "1";
                                        rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                        PbCache.voiceType = 1;
                                        PbCache.LEDResultMessage = string.Format("车号{0}，皮重{1}吨，计量完成", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                    }
                                    // 重量类型为皮重且在皮重误差外
                                    else if (list[0].weightTypeNo == "001033002" && PbCache.monitorResult.valid_wgt == false && listMonitor.Data[0].validWgt == "1")
                                    {
                                        if (PbCache.ResultMessage == "")
                                        {
                                            PbCache.ResultMessage = "重量类型为皮重且不在皮重误差内";
                                        }
                                        PbCache.voiceType = 13;
                                        rt = saveMethod.doError(PbCache.ResultMessage);
                                        rt.isWarn = true;
                                        rt.WarnContent = "validWgt";
                                        return rt;
                                    }
                                    // 重量类型为毛重，在皮重误差内
                                    else if (list[0].weightTypeNo == "001033001" && PbCache.monitorResult.valid_wgt == true && listMonitor.Data[0].validWgt == "1")
                                    {
                                        if (PbCache.ResultMessage == "")
                                        {
                                            PbCache.ResultMessage = "重量类型为毛重，在皮重误差内";
                                        }
                                        PbCache.voiceType = 23;
                                        rt = saveMethod.doError(PbCache.ResultMessage);
                                        rt.isWarn = true;
                                        rt.WarnContent = "validWgt";
                                        return rt;
                                    }
                                    // 重量类型为毛重，不在皮重误差内
                                    else if (list[0].weightTypeNo == "001033001" && (PbCache.monitorResult.valid_wgt == false || listMonitor.Data[0].validWgt == "0"))
                                    {
                                        //判断称点是否允许期限皮重
                                        if (PbCache.sportInfo.allowedTareWeight == "1")
                                        {
                                            if (AppConfigCache.TareControlType == "3")
                                            {
                                                // 判断是否具有期限皮重权限
                                                MeterBaseUnitInfoService meterBaseUnitInfoService = new MeterBaseUnitInfoService();
                                                MeterBaseUnitInfo meterBaseUnitInfo = new MeterBaseUnitInfo();
                                                meterBaseUnitInfo.matterNo = rmScale.Data[0].matterNo;
                                                meterBaseUnitInfo.matterName = rmScale.Data[0].matterName;
                                                meterBaseUnitInfo.forwardingUnitNo = rmScale.Data[0].forwardingUnitNo;
                                                meterBaseUnitInfo.forwardingUnitName = rmScale.Data[0].forwardingUnitName;
                                                meterBaseUnitInfo.receivingUnitNo = rmScale.Data[0].receivingUintNo;
                                                meterBaseUnitInfo.receivingUnitName = rmScale.Data[0].receivingUintName;
                                                meterBaseUnitInfo.carNo = PbCache.lockCarNo;
                                                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.voiceType = 1;
                                                        PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，计量完成", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                                    }
                                                    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.voiceType = 15;
                                                                rt.resultInfo = "计量成功，期限皮过期，请留存期限皮";
                                                                PbCache.ResultMessage = "计量成功，期限皮过期，请留存期限皮";
                                                                PbCache.monitorResultMessage = "计量成功，期限皮过期，请留存期限皮";
                                                                PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，计量完成，期限皮过期", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                                            }
                                                            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);
                                                                rt.resultInfo += "，期限皮结净";
                                                                PbCache.voiceType = 16;
                                                                PbCache.ResultMessage = "计量完成，期限皮结净";
                                                                PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，净重{2}吨，期限皮结净", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2), Math.Round((Math.Abs(PbCache.lockWgt - termTareData.Data.meterWeight.Value) / 1000), 2));
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        else
                                        {
                                            mwaf.weightType = "0";
                                            // 只保存一次计量数据
                                            rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                            PbCache.voiceType = 1;
                                            PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，计量完成", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                        }
                                    }
                                    else
                                    {
                                        rt = saveMethod.doError("程序处理异常");
                                        return rt;
                                    }
                                }
                                // 只存在一条有效的一次计量数据
                                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 (firstList[0].weightType == "0" && list[0].weightTypeNo == "001033001")
                                    {
                                        // 历史皮重验证
                                        rt = saveMethod.validHisTare2(list[0].weightTypeNo);
                                        if (PbCache.monitorResult.valid_wgt == false || listMonitor.Data[0].validWgt == "0")
                                        {
                                            //判断称点是否允许期限皮重
                                            if (PbCache.sportInfo.allowedTareWeight == "1")
                                            {
                                                if (AppConfigCache.TareControlType == "3")
                                                {
                                                    // 判断收发货单位是否具有期限皮重权限
                                                    MeterBaseUnitInfoService meterBaseUnitInfoService = new MeterBaseUnitInfoService();
                                                    MeterBaseUnitInfo meterBaseUnitInfo = new MeterBaseUnitInfo();
                                                    meterBaseUnitInfo.matterNo = rmScale.Data[0].matterNo;
                                                    meterBaseUnitInfo.matterName = rmScale.Data[0].matterName;
                                                    meterBaseUnitInfo.forwardingUnitNo = rmScale.Data[0].forwardingUnitNo;
                                                    meterBaseUnitInfo.forwardingUnitName = rmScale.Data[0].forwardingUnitName;
                                                    meterBaseUnitInfo.receivingUnitNo = rmScale.Data[0].receivingUintNo;
                                                    meterBaseUnitInfo.receivingUnitName = rmScale.Data[0].receivingUintName;
                                                    meterBaseUnitInfo.carNo = PbCache.lockCarNo;
                                                    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);
                                                            rt.resultInfo = "存在未结净毛重";
                                                            PbCache.voiceType = 11;
                                                            PbCache.ResultMessage = "存在未结净毛重";
                                                            PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，存在未结净毛重，请联系", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                                            PbCache.monitorResultMessage = "存在未结净毛重";
                                                            rt.isWarn = true;
                                                            rt.WarnContent = "validWgt";
                                                        }
                                                        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);
                                                                    rt.resultInfo = "期限皮过期，存在未结净毛重";
                                                                    PbCache.voiceType = 17;
                                                                    PbCache.ResultMessage = "期限皮过期，存在未结净毛重";
                                                                    PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，期限皮过期，存在未结净毛重", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                                                    PbCache.monitorResultMessage = "期限皮过期，存在未结净毛重";
                                                                    rt.isWarn = true;
                                                                    rt.WarnContent = "validWgt";
                                                                }
                                                                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";
                                                                    PbCache.voiceType = 16;
                                                                    PbCache.ResultMessage = "计量完成，期限皮结净";
                                                                    PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，净重{2}吨，期限皮结净", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2), Math.Round((Math.Abs(PbCache.lockWgt - termTareData.Data.meterWeight.Value) / 1000), 2));
                                                                    rt = saveMethod.doNetTermTare(rmScale.Data[0], mwaf);
                                                                    rt.resultInfo += "，期限皮结净";
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                mwaf.weightType = "0";
                                                rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                                rt.resultInfo = "存在未结净毛重";
                                                PbCache.voiceType = 11;
                                                PbCache.ResultMessage = "存在未结净毛重";
                                                PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，存在未结净毛重，请联系", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                                PbCache.monitorResultMessage = "存在未结净毛重";
                                                rt.isWarn = true;
                                                rt.WarnContent = "validWgt";
                                            }
                                            return rt;
                                        }
                                        else
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "重量类型为毛重，在皮重误差内";
                                            }
                                            PbCache.voiceType = 23;
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                            rt.isWarn = true;
                                            rt.WarnContent = "validWgt";
                                            return rt;
                                        }
                                    }
                                    // 当次重量和上次重量满足结净最小值条件
                                    if (Math.Abs(rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt) > diffrentWgt || listMonitor.Data[0].validPredictionDiff == "0")
                                    {
                                        // 历史皮重验证
                                        rt = saveMethod.validHisTare(list[0].weightTypeNo);

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

                                            //给一次数据匹配委托
                                            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.voiceType = 13;
                                            PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，净重{2}吨，计量完成", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2), Math.Round((Math.Abs(rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt) / 1000), 2));
                                        }
                                        // 重量类型为皮重且不在皮重误差内
                                        else if (list[0].weightTypeNo == "001033002" && PbCache.monitorResult.valid_wgt == false && listMonitor.Data[0].validWgt == "1")
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "重量类型为皮重且不在皮重误差内";
                                            }
                                            PbCache.voiceType = 13;
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                            return rt;
                                        }
                                        // 重量类型为毛重，不在皮重误差内，正常结净
                                        else if (list[0].weightTypeNo == "001033001" && (PbCache.monitorResult.valid_wgt == false || listMonitor.Data[0].validWgt == "0"))
                                        {
                                            if (firstList[0].weightType == "0")
                                            {
                                                RESTfulResult<List<PreTrackScale>> scales = preTrackScaleService.doQueryWf(new PreTrackScale { predictionNo = firstList[0].predictionNo });

                                                //判断称点是否允许期限皮重
                                                if (PbCache.sportInfo.allowedTareWeight == "1")
                                                {
                                                    if (AppConfigCache.TareControlType == "3")
                                                    {
                                                        /// 判断是否具有期限皮重权限
                                                        MeterBaseUnitInfoService meterBaseUnitInfoService = new MeterBaseUnitInfoService();
                                                        MeterBaseUnitInfo meterBaseUnitInfo = new MeterBaseUnitInfo();
                                                        meterBaseUnitInfo.matterNo = scales.Data[0].matterNo;
                                                        meterBaseUnitInfo.matterName = scales.Data[0].matterName;
                                                        meterBaseUnitInfo.forwardingUnitNo = scales.Data[0].forwardingUnitNo;
                                                        meterBaseUnitInfo.forwardingUnitName = scales.Data[0].forwardingUnitName;
                                                        meterBaseUnitInfo.receivingUnitNo = scales.Data[0].receivingUintNo;
                                                        meterBaseUnitInfo.receivingUnitName = scales.Data[0].receivingUintName;
                                                        meterBaseUnitInfo.carNo = PbCache.lockCarNo;
                                                        RESTfulResult<List<MeterBaseUnitInfo>> tareUnitResult = meterBaseUnitInfoService.doQueryWf(meterBaseUnitInfo);
                                                        if (tareUnitResult.Succeed)
                                                        {
                                                            if (tareUnitResult.Data == null || tareUnitResult.Data.Count == 0)
                                                            {
                                                                mwaf.weightType = "0";
                                                                rt = saveMethod.doAddFirst(scales.Data[0], mwaf);
                                                                rt.resultInfo = "存在未结净毛重";
                                                                PbCache.voiceType = 11;
                                                                PbCache.ResultMessage = "存在未结净毛重";
                                                                PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，存在未结净毛重，请联系", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                                                PbCache.monitorResultMessage = "存在未结净毛重";
                                                                rt.isWarn = true;
                                                                rt.WarnContent = "validWgt";
                                                            }
                                                            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(scales.Data[0], mwaf);
                                                                        rt.resultInfo = "期限皮过期，存在未结净毛重";
                                                                        PbCache.voiceType = 17;
                                                                        PbCache.ResultMessage = "期限皮过期，存在未结净毛重";
                                                                        PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，期限皮过期，存在未结净毛重", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                                                        PbCache.monitorResultMessage = "期限皮过期，存在未结净毛重";
                                                                        rt.isWarn = true;
                                                                        rt.WarnContent = "validWgt";

                                                                    }
                                                                    else
                                                                    {
                                                                        // 存在使用期限皮重权限，并使用期限皮重结净
                                                                        List<string> l = new List<string>();
                                                                        l.Add("create");
                                                                        //给一次数据匹配委托
                                                                        EntityBase<PreTrackScale> entityBase1 = new EntityBase<PreTrackScale>();
                                                                        mwaf = entityBase1.format<MeterWorkCarActualFirst>(scales.Data[0], mwaf);
                                                                        mwaf.isPreScale = "1";
                                                                        mwaf.weightType = "0";
                                                                        PbCache.voiceType = 16;
                                                                        PbCache.ResultMessage = "计量完成，期限皮结净";
                                                                        PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，净重{2}吨，期限皮结净", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2), Math.Round((Math.Abs(PbCache.lockWgt - termTareData.Data.meterWeight.Value) / 1000), 2));
                                                                        rt = saveMethod.doNetTermTare(scales.Data[0], mwaf);
                                                                        rt.resultInfo += "，期限皮结净";
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    mwaf.weightType = "0";
                                                    rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                                    rt.resultInfo = "存在未结净毛重";
                                                    PbCache.voiceType = 11;
                                                    PbCache.ResultMessage = "存在未结净毛重";
                                                    PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，存在未结净毛重，请联系", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                                    PbCache.monitorResultMessage = "存在未结净毛重";
                                                    rt.isWarn = true;
                                                    rt.WarnContent = "validWgt";
                                                }
                                            }
                                            else
                                            {
                                                if (PbCache.sportInfo.allowedTareWeight == "1")
                                                {
                                                    if (AppConfigCache.TareControlType == "3")
                                                    {
                                                        // 判断是否具有期限皮重权限
                                                        MeterBaseUnitInfoService meterBaseUnitInfoService = new MeterBaseUnitInfoService();
                                                        MeterBaseUnitInfo meterBaseUnitInfo = new MeterBaseUnitInfo();
                                                        meterBaseUnitInfo.matterNo = rmScale.Data[0].matterNo;
                                                        meterBaseUnitInfo.matterName = rmScale.Data[0].matterName;
                                                        meterBaseUnitInfo.forwardingUnitNo = rmScale.Data[0].forwardingUnitNo;
                                                        meterBaseUnitInfo.forwardingUnitName = rmScale.Data[0].forwardingUnitName;
                                                        meterBaseUnitInfo.receivingUnitNo = rmScale.Data[0].receivingUintNo;
                                                        meterBaseUnitInfo.receivingUnitName = rmScale.Data[0].receivingUintName;
                                                        meterBaseUnitInfo.carNo = PbCache.lockCarNo;
                                                        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";
                                                                PbCache.voiceType = 1;
                                                                rt = saveMethod.doNet(rmScale.Data[0], firstList[0], mwaf);
                                                                PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，净重{2}吨，计量完成", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2), Math.Round((Math.Abs(rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt) / 1000), 2));
                                                            }
                                                            else
                                                            {
                                                                MeterBaseTermTareDataService meterBaseTermTareDataService = new MeterBaseTermTareDataService();
                                                                RESTfulResult<MeterBaseTermTareData> termTareData = meterBaseTermTareDataService.doQueryPageWf(new MeterBaseTermTareData { carNo = PbCache.lockCarNo, valueFlag = "0" });
                                                                if (termTareData.Succeed)
                                                                {
                                                                    // 正常结净
                                                                    //给一次数据匹配委托
                                                                    EntityBase<PreTrackScale> entityBase3 = new EntityBase<PreTrackScale>();
                                                                    mwaf = entityBase3.format<MeterWorkCarActualFirst>(rmScale.Data[0], mwaf);
                                                                    mwaf.isPreScale = "1";
                                                                    mwaf.weightType = "0";
                                                                    PbCache.voiceType = 9;
                                                                    rt = saveMethod.doTermTareNet(rmScale.Data[0], mwaf, firstList[0],"0");
                                                                    rt.resultInfo += "，留存期限皮成功";
                                                                    PbCache.ResultMessage = "计量成功，留存期限皮成功";
                                                                    PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，净重{2}吨，留存期限皮成功", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2), Math.Round((Math.Abs(firstList[0].meterWeight.Value - PbCache.lockWgt) / 1000), 2));
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    // 正常结净
                                                    //给一次数据匹配委托
                                                    EntityBase<PreTrackScale> entityBase3 = new EntityBase<PreTrackScale>();
                                                    mwaf = entityBase3.format<MeterWorkCarActualFirst>(rmScale.Data[0], mwaf);
                                                    mwaf.isPreScale = "1";
                                                    mwaf.weightType = "0";
                                                    PbCache.voiceType = 1;
                                                    rt = saveMethod.doNet(rmScale.Data[0], firstList[0], mwaf);
                                                    PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，净重{2}吨，计量完成", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2), Math.Round((Math.Abs(rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt) / 1000), 2));
                                                }
                                            }
                                        }
                                        // 重量类型为毛重，在皮重误差内
                                        else if (list[0].weightTypeNo == "001033001" && PbCache.monitorResult.valid_wgt == true && listMonitor.Data[0].validWgt == "1")
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "毛重接近历史皮重，请联系处理";
                                            }
                                            PbCache.voiceType = 23;
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                            return rt;
                                        }
                                    }
                                    else
                                    {
                                        rt = saveMethod.doError("第一次计量重量【" + rmFirst.Data[0].meterWeight.Value / 1000 + "T】和第二次计量重量【" + PbCache.lockWgt / 1000 + "T】接近，小于结净最小值【" + diffrentWgt / 1000 + " T】");
                                        rt.isWarn = true;
                                        PbCache.voiceType = 4;
                                        PbCache.LEDResultMessage = "计量失败，小于结净最小值，请联系计量大厅";
                                        rt.WarnContent = "validWgt";
                                        return rt;
                                    }
                                }
                                // 存在多条有效的一次计量数据
                                else if (firstList == null || firstList.Count > 1)
                                {
                                    // 历史皮重验证
                                    rt = saveMethod.validHisTare(list[0].weightTypeNo);

                                    /* a. 程序报警a
                                     * b. 自动进行语音求助
                                     */
                                    //多条一次记录下判断是否全为毛重，是全部结净
                                    if (firstList[firstList.Count - 1].weightType == "0" && list[0].weightTypeNo == "001033002")
                                    {
                                        if (PbCache.monitorResult.valid_wgt == true || listMonitor.Data[0].validWgt == "0")
                                        {
                                            mwaf.weightType = "1";
                                            rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                            PbCache.voiceType = 1;
                                            PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，计量完成", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                        }
                                        else
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "重量类型为皮重且不在皮重误差内";
                                            }
                                            PbCache.voiceType = 13;
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                            return rt;
                                        }
                                        
                                    }
                                    else if (firstList[firstList.Count - 1].weightType == "0" && list[0].weightTypeNo == "001033001")
                                    {
                                        if (PbCache.monitorResult.valid_wgt == false || listMonitor.Data[0].validWgt == "0")
                                        {
                                            mwaf.weightType = "0";
                                            rt = saveMethod.doAddFirst(rmScale.Data[0], mwaf);
                                            PbCache.voiceType = 11;
                                            rt.resultInfo = string.Format("存在未结净毛重", firstList.Count);
                                            PbCache.ResultMessage = string.Format("存在未结净毛重", firstList.Count);
                                            PbCache.LEDResultMessage = string.Format("车号{0}，毛重{1}吨，存在未结净毛重，请联系", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2), firstList.Count);
                                            PbCache.monitorResultMessage = string.Format("存在未结净毛重", firstList.Count);
                                            rt.isWarn = true;
                                            rt.WarnContent = "validWgt";
                                        }
                                        else
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "毛重接近历史皮重，请联系处理";
                                            }
                                            PbCache.voiceType = 23;
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                            return rt;
                                        }
                                    }
                                    else
                                    {
                                        rt = saveMethod.doError("存在多条有效的一次计量数据，请联系计量大厅");
                                        PbCache.voiceType = 8;
                                        PbCache.LEDResultMessage = "存在多条有效的一次计量数据，请联系计量大厅";
                                        return rt;
                                    }
                                }
                                else
                                {
                                    rt = saveMethod.doError("存在多条有效的一次计量数据，请联系计量大厅");
                                    PbCache.voiceType = 8;
                                    PbCache.LEDResultMessage = "存在多条有效的一次计量数据，请联系计量大厅";
                                    return rt;
                                }
                            }
                        }
                    }
                    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")
                                {
                                    //if (PbCache.lockWgt > 25000)
                                    //{
                                    //    rt = saveMethod.doError(string.Format("当前重量：{0}，请确认是否，误选为皮重上秤", PbCache.lockWgt));
                                    //    PbCache.voiceType = 26;
                                    //    PbCache.LEDResultMessage = string.Format("当前重量：{0}，请确认是否，误选为皮重上秤", PbCache.lockWgt);
                                    //    return rt;
                                    //}

                                    // 历史皮重验证
                                    rt = saveMethod.validHisTare(list[0].weightTypeNo);
                                    if (firstList == null || firstList.Count == 0)
                                    {
                                        //在皮重误差内
                                        if (PbCache.monitorResult.valid_wgt == true || listMonitor.Data[0].validWgt == "0")
                                        {
                                            //正常保存一次数据
                                            mwaf.weightType = "1";
                                            PbCache.voiceType = 1;
                                            rt = saveMethod.doAddFirst(null, mwaf);
                                            PbCache.LEDResultMessage = string.Format("车号{0}，皮重{1}吨，计量完成", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                        }
                                        else
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "重量类型为皮重且不在皮重误差内";
                                            }
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                            PbCache.voiceType = 13;
                                            rt.isWarn = true;
                                            rt.WarnContent = "validWgt";
                                            return rt;
                                        }
                                    }
                                    else if (firstList.Count == 1 && firstList[0].weightType != "0")
                                    {
                                        // 在皮重误差内
                                        if (PbCache.monitorResult.valid_wgt == true || listMonitor.Data[0].validWgt == "0")
                                        {
                                            //保存一条作废的皮重数据
                                            mwaf.weightType = "1";
                                            mwaf.valueFlag = "0";
                                            PbCache.voiceType = 1;
                                            rt = saveMethod.doAddFirst(null, mwaf);
                                            PbCache.LEDResultMessage = string.Format("车号{0}，皮重{1}吨，计量完成", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                        }
                                        else
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "重量类型为皮重且不在皮重误差内";
                                            }
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                            rt.isWarn = true;
                                            PbCache.voiceType = 13;
                                            rt.WarnContent = "validWgt";
                                            return rt;
                                        }
                                    }
                                    else if (firstList.Count == 1 && firstList[0].weightType == "0")
                                    {
                                        RESTfulResult<List<PreTrackScale>> scales = preTrackScaleService.doQueryWf(new PreTrackScale { predictionNo = firstList[0].predictionNo });
                                        if (PbCache.monitorResult.valid_wgt == true || listMonitor.Data[0].validWgt == "0")
                                        {
                                            //判断称点是否允许期限皮重
                                            if (PbCache.sportInfo.allowedTareWeight == "1")
                                            {
                                                if (AppConfigCache.TareControlType == "3")
                                                {
                                                    /// 判断是否具有期限皮重权限
                                                    MeterBaseUnitInfoService meterBaseUnitInfoService = new MeterBaseUnitInfoService();
                                                    MeterBaseUnitInfo meterBaseUnitInfo = new MeterBaseUnitInfo();
                                                    meterBaseUnitInfo.matterNo = scales.Data[0].matterNo;
                                                    meterBaseUnitInfo.matterName = scales.Data[0].matterName;
                                                    meterBaseUnitInfo.forwardingUnitNo = scales.Data[0].forwardingUnitNo;
                                                    meterBaseUnitInfo.forwardingUnitName = scales.Data[0].forwardingUnitName;
                                                    meterBaseUnitInfo.receivingUnitNo = scales.Data[0].receivingUintNo;
                                                    meterBaseUnitInfo.receivingUnitName = scales.Data[0].receivingUintName;
                                                    meterBaseUnitInfo.carNo = PbCache.lockCarNo;
                                                    RESTfulResult<List<MeterBaseUnitInfo>> tareUnitResult = meterBaseUnitInfoService.doQueryWf(meterBaseUnitInfo);
                                                    if (tareUnitResult.Succeed)
                                                    {
                                                        if (tareUnitResult.Data == null || tareUnitResult.Data.Count == 0)
                                                        {
                                                            //匹配结净
                                                            EntityBase<PreTrackScale> entityBase2 = new EntityBase<PreTrackScale>();
                                                            mwaf = entityBase2.format<MeterWorkCarActualFirst>(scales.Data[0], mwaf);
                                                            mwaf.isPreScale = "1";
                                                            mwaf.weightType = "0";
                                                            PbCache.voiceType = 1;
                                                            rt = saveMethod.doNet(scales.Data[0], firstList[0], mwaf);
                                                            PbCache.LEDResultMessage = string.Format("车号{0}，皮重{1}吨，净重{2}吨，计量完成", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2), Math.Round((Math.Abs(rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt) / 1000), 2));
                                                        }
                                                        else
                                                        {
                                                            MeterBaseTermTareDataService meterBaseTermTareDataService = new MeterBaseTermTareDataService();
                                                            RESTfulResult<MeterBaseTermTareData> termTareData = meterBaseTermTareDataService.doQueryPageWf(new MeterBaseTermTareData { carNo = PbCache.lockCarNo, valueFlag = "0" });
                                                            if (termTareData.Succeed)
                                                            {
                                                                // 正常结净
                                                                //给一次数据匹配委托
                                                                EntityBase<PreTrackScale> entityBase3 = new EntityBase<PreTrackScale>();
                                                                mwaf = entityBase3.format<MeterWorkCarActualFirst>(scales.Data[0], mwaf);
                                                                mwaf.isPreScale = "1";
                                                                mwaf.weightType = "1";
                                                                PbCache.voiceType = 9;
                                                                rt = saveMethod.doTermTareNet(scales.Data[0], firstList[0], mwaf,"1");
                                                                rt.resultInfo += "，留存期限皮成功";
                                                                PbCache.ResultMessage = "计量成功，留存期限皮成功";
                                                                PbCache.LEDResultMessage = string.Format("车号{0}，皮重{1}吨，净重{2}吨，留存期限皮成功", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2), Math.Round((Math.Abs(firstList[0].meterWeight.Value - PbCache.lockWgt) / 1000), 2));
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                //匹配结净
                                                EntityBase<PreTrackScale> entityBase2 = new EntityBase<PreTrackScale>();
                                                mwaf = entityBase2.format<MeterWorkCarActualFirst>(scales.Data[0], mwaf);
                                                mwaf.isPreScale = "1";
                                                mwaf.weightType = "0";
                                                PbCache.voiceType = 1;
                                                rt = saveMethod.doNet(scales.Data[0], firstList[0], mwaf);
                                                PbCache.LEDResultMessage = string.Format("车号{0}，皮重{1}吨，净重{2}吨，计量完成", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2), Math.Round((Math.Abs(rmFirst.Data[0].meterWeight.Value - PbCache.lockWgt) / 1000), 2));
                                            }
                                        }
                                        else
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "重量类型为皮重且不在皮重误差内";
                                            }
                                            PbCache.voiceType = 13;
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                            rt.isWarn = true;
                                            rt.WarnContent = "validWgt";
                                            return rt;
                                        }
                                    }
                                    // 存在多条有效的一次计量数据
                                    else if (firstList.Count > 1)
                                    {
                                        //mwaf.weightType = "1";
                                        //rt = saveMethod.doAddFirst(null, mwaf);
                                        //PbCache.LEDResultMessage = string.Format("车号{0}，重量{1}吨，之前存在多个未结净的毛重，请联系", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                        //rt.resultInfo = "之前存在多个未结净的毛重";
                                        //PbCache.ResultMessage = "之前存在多个未结净的毛重";
                                        //PbCache.monitorResultMessage = "之前存在多个未结净的毛重";
                                        rt = saveMethod.validHisTare(list[0].weightTypeNo);
                                        if (PbCache.monitorResult.valid_wgt == true || listMonitor.Data[0].validWgt == "0")
                                        {
                                            int countTerm = 0;
                                            List<MeterWorkCarActualFirst> meterWorkCarActualFirsts = new List<MeterWorkCarActualFirst>(); 

                                            foreach (var item in firstList)
                                            {
                                                RESTfulResult<List<PreTrackScale>> scales = preTrackScaleService.doQueryWf(new PreTrackScale { predictionNo = item.predictionNo });
                                                if (PbCache.sportInfo.allowedTareWeight == "1")
                                                {
                                                    if (AppConfigCache.TareControlType == "3")
                                                    {
                                                        /// 判断是否具有期限皮重权限
                                                        MeterBaseUnitInfoService meterBaseUnitInfoService = new MeterBaseUnitInfoService();
                                                        MeterBaseUnitInfo meterBaseUnitInfo = new MeterBaseUnitInfo();
                                                        meterBaseUnitInfo.matterNo = scales.Data[0].matterNo;
                                                        meterBaseUnitInfo.matterName = scales.Data[0].matterName;
                                                        meterBaseUnitInfo.forwardingUnitNo = scales.Data[0].forwardingUnitNo;
                                                        meterBaseUnitInfo.forwardingUnitName = scales.Data[0].forwardingUnitName;
                                                        meterBaseUnitInfo.receivingUnitNo = scales.Data[0].receivingUintNo;
                                                        meterBaseUnitInfo.receivingUnitName = scales.Data[0].receivingUintName;
                                                        meterBaseUnitInfo.carNo = PbCache.lockCarNo;
                                                        RESTfulResult<List<MeterBaseUnitInfo>> tareUnitResult = meterBaseUnitInfoService.doQueryWf(meterBaseUnitInfo);
                                                        if (tareUnitResult.Succeed)
                                                        {
                                                            if (tareUnitResult.Data == null || tareUnitResult.Data.Count == 0)
                                                            {
                                                                countTerm++;
                                                            }
                                                            else
                                                            {
                                                                MeterBaseTermTareDataService meterBaseTermTareDataService = new MeterBaseTermTareDataService();
                                                                RESTfulResult<MeterBaseTermTareData> termTareData = meterBaseTermTareDataService.doQueryPageWf(new MeterBaseTermTareData { carNo = PbCache.lockCarNo, valueFlag = "0" });
                                                                if (termTareData.Succeed)
                                                                {
                                                                    meterWorkCarActualFirsts.Add(item);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    countTerm++;
                                                }
                                            }

                                            if (countTerm == 0)
                                            {
                                                rt = saveMethod.doNetAddMuchGross(meterWorkCarActualFirsts,mwaf);
                                                rt.resultInfo = "计量完成，留存期限皮成功";
                                                PbCache.voiceType = 9;
                                                PbCache.ResultMessage = "计量成功，留存期限皮成功";
                                                PbCache.monitorResultMessage = string.Format("计量成功，留存期限皮成功");
                                                PbCache.LEDResultMessage = string.Format("车号{0}，皮重{1}吨，计量完成", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                            }
                                            else if (countTerm == firstList.Count)
                                            {
                                                RESTfulResult<List<PreTrackScale>> scales = preTrackScaleService.doQueryWf(new PreTrackScale { predictionNo = firstList[0].predictionNo });

                                                //匹配结净
                                                EntityBase<PreTrackScale> entityBase2 = new EntityBase<PreTrackScale>();
                                                mwaf = entityBase2.format<MeterWorkCarActualFirst>(scales.Data[0], mwaf);
                                                mwaf.isPreScale = "1";
                                                mwaf.weightType = "0";
                                                PbCache.voiceType = 1;
                                                rt = saveMethod.doNet(scales.Data[0], firstList[0], mwaf);
                                                PbCache.LEDResultMessage = string.Format("车号{0}，皮重{1}吨，净重{2}吨，存在未结净毛重", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2), Math.Round((Math.Abs(firstList[0].meterWeight.Value - PbCache.lockWgt) / 1000), 2));
                                                PbCache.monitorResultMessage = string.Format("车号{0}有{1}条未结净的毛重；", PbCache.lockCarNo,firstList.Count-1);
                                            }
                                            else
                                            {
                                                rt = saveMethod.doNetAddMuchGross(meterWorkCarActualFirsts, mwaf);
                                                PbCache.voiceType = 11;
                                                rt.resultInfo += "，存在未结净毛重";
                                                PbCache.ResultMessage = "计量成功，存在未结净毛重";
                                                PbCache.LEDResultMessage = string.Format("车号{0}，皮重{1}吨，计量完成，存在未结净毛重", PbCache.lockCarNo, Math.Round((PbCache.lockWgt / 1000), 2));
                                                PbCache.monitorResultMessage = string.Format("车号{0}使用期限皮同时结净{1}条毛重数据，还有{2}条未结净的毛重；", PbCache.lockCarNo, firstList.Count - countTerm,countTerm);
                                            }
                                        }
                                        else
                                        {
                                            if (PbCache.ResultMessage == "")
                                            {
                                                PbCache.ResultMessage = "重量类型为皮重且不在皮重误差内";
                                            }
                                            PbCache.voiceType = 13;
                                            rt = saveMethod.doError(PbCache.ResultMessage);
                                            rt.isWarn = true;
                                            rt.WarnContent = "validWgt";
                                            return rt;
                                        }
                                    }
                                }
                                else
                                {
                                    rt = saveMethod.doError("委托匹配异常，无法计量，请联系处理");
                                    PbCache.LEDResultMessage = "委托匹配异常，无法计量，请联系处理";
                                    PbCache.voiceType = 6;
                                    rt.isWarn = true;
                                    rt.WarnContent = "validIsPre";
                                    return rt;
                                }
                            }
                        }
                        else
                        {
                            rt = saveMethod.doError("存在多条已选择的委托，请联系计量大厅");
                            PbCache.LEDResultMessage = "存在多条已选择的委托，请联系计量大厅";
                            rt.isWarn = true;
                            PbCache.voiceType = 19;
                            rt.WarnContent = "validIsPre";
                            return rt;
                        }
                    }
                }
                else
                {
                    rt = saveMethod.doError("存在多条上称申请数据，请联系计量大厅");
                    PbCache.voiceType = 20;
                    PbCache.LEDResultMessage = "存在多条上称申请数据，请联系计量大厅";
                    return rt;
                }
            }
            // 后台失败处理
            if (!rt.result)
            {
                rt.resultInfo = "称重数据保存失败";
                PbCache.voiceType = 25;
                PbCache.LEDResultMessage = "计量失败，请联系计量大厅";
                PbCache.monitorResultMessage = "";
            }
            return rt;
        }
    }
}
