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

namespace MeterSceneLibrary.JISCO
{

    public class SaveMethod
    {
        // 一次计量数据
        private MeterWorkCarActualFirstService mwcfs = new MeterWorkCarActualFirstService();
        // 车辆委托表
        private PreTrackScaleService preTrackScaleService = new PreTrackScaleService();

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

        private rtInfo rt = new rtInfo();

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

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

        #region 私有方法
        /// <summary>
        /// 保存一次计量数据
        /// </summary>
        public rtInfo doAddFirst(PreTrackScale scale,MeterWorkCarActualFirst model)
        {
            if(scale != null)
            {
                EntityBase<PreTrackScale> entityBase = new EntityBase<PreTrackScale>();
                model = entityBase.format<MeterWorkCarActualFirst>(scale, model);
            }
            //操作日志
            MeterMonitorNote monitorNote = new MeterMonitorNote();
            monitorNote.baseSpotNo = model.baseSpotNo;
            monitorNote.baseSpotName = model.baseSpotName;
            monitorNote.operationContent = string.Format("产生一次计量数据");
            monitorNote.operationTime = model.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            monitorNote.carNo = model.carNo;
            monitorNote.meterNoteSource = "0";

            PbSomeModel<MeterWorkCarActualFirst, MeterMonitorNote, MeterMonitorNote, MeterMonitorNote> pbSomeModel = new PbSomeModel<MeterWorkCarActualFirst, MeterMonitorNote, MeterMonitorNote, MeterMonitorNote>();
            pbSomeModel.one = model;
            pbSomeModel.two = monitorNote;

            RESTfulResult<string> rmI = mwcfs.doAdd(pbSomeModel);
            if (rmI.Succeed)
            {
                rt.result = true;
                rt.resultInfo = "计量完成";
                PbCache.actualFirstNo = rmI.Data;
                PbCache.strQRCode = rmI.Message;
                if (model.weightType == "0")
                {
                    PbCache.isTareWeight = false;//毛重
                }
                else
                {
                    PbCache.isTareWeight = true;//皮重
                }
            }
            else
            {
                rt.result = false;
                rt.isError = true;
                rt.resultInfo = "称重数据保存失败";
            }

            return rt;
        }

        /// <summary>
        /// 产生一次计量数据和没有委托的洁净数据
        /// </summary>
        /// <param name="scale"></param>
        /// <param name="actualFirst"></param>
        public rtInfo doNet(PreTrackScale scale, MeterWorkCarActualFirst actualFirst, MeterWorkCarActualFirst actualFirst2)
        {
            // 构造结净数据
            MeterWorkCarActual netDb = new MeterWorkCarActual();

            // 将第一个一次数据状态改为 已结净
            //actualFirst.valueFlag = "2";
            netDb.isPreScale = "0";
            // 复制对象
            if (scale != null)
            {
                EntityBase<PreTrackScale> entityBase = new EntityBase<PreTrackScale>();
                netDb = entityBase.format<MeterWorkCarActual>(scale, netDb);
                netDb.isPreScale = "1";
            }

            if (actualFirst2.meterWeight.Value > actualFirst.meterWeight.Value)
            {
                actualFirst2.weightType = "0";//毛重
            }
            else
            {
                actualFirst2.weightType = "1";//皮重
            }

            netDb.carNo = actualFirst.carNo;
            netDb.tareTime = DateTime.Now;
            netDb.tareWeight = actualFirst.meterWeight.Value < PbCache.lockWgt ? actualFirst.meterWeight.Value : PbCache.lockWgt; // 谁小谁是皮 BourneCao
            netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
            netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
            netDb.tareMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            /* netDb.tare_man_no = "";
             netDb.tare_man_name = "";
             netDb.tare_class = "";
             netDb.tare_group = "";*/

            // 毛重计量作业编号
            // netDb.actualFirst1No = actualFirst.meterWeight.Value > PbCache.lockWgt ? actualFirst.actualFirstNo : actualFirst2.actualFirstNo;
            // 皮重计量作业编号
            // netDb.actualFirst2No = actualFirst.meterWeight.Value < PbCache.lockWgt ? actualFirst.actualFirstNo : actualFirst2.actualFirstNo;
            netDb.actualFirst1No = actualFirst2.meterWeight.Value > actualFirst.meterWeight.Value ? "" : actualFirst.actualFirstNo;
            netDb.actualFirst2No = actualFirst2.meterWeight.Value > actualFirst.meterWeight.Value ? actualFirst.actualFirstNo : "";
            netDb.grossTime = actualFirst.createTime;
            netDb.grossWeight = actualFirst.meterWeight.Value > PbCache.lockWgt ? actualFirst.meterWeight.Value : PbCache.lockWgt; // 谁大谁是毛 BourneCao
            netDb.baseSpot1No = actualFirst.baseSpotNo;
            netDb.baseSpot1Name = actualFirst.baseSpotName;
            netDb.grossMode = "2"; // 1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            netDb.grossClass = actualFirst.meterClass;
            netDb.grossGroup = actualFirst.meterGroup;
            netDb.valueFlag = "1";

            netDb.netTime = DateTime.Now;

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

            netDb.addWeight = (actualFirst.addWeight == null ? 0 : actualFirst.addWeight.Value);
            //净重=毛-皮-附加重量
            netDb.netWeight = netDb.grossWeight - netDb.tareWeight - netDb.addWeight;
            netDb.netMode = "1"; //1：正常结净；2：匹配结净
            netDb.uploadFlag = "1"; //0=待审核；1：待上传；2=已上传
            netDb.dataSource = "1";//1 = 智能终端，2 = 本地计量，3 = 应急计量，4 = 手动录入(计量票据录入)
                                   //要判断下当前是否勾选了不验证皮重

            if (actualFirst2.weightType == "0")
            {
                netDb.grossManNo = "admin";
                netDb.grossManName = "admin";
                netDb.grossTime = DateTime.Now;

                netDb.tareManName = actualFirst.createManName;
                netDb.tareManNo = actualFirst.createManNo;
                netDb.tareTime = actualFirst.createTime;
            }
            else
            {
                netDb.tareManName = "admin";
                netDb.tareManNo = "admin";
                netDb.tareTime = DateTime.Now;

                netDb.grossManName = actualFirst.createManName;
                netDb.grossManNo = actualFirst.createManNo;
                netDb.grossTime = actualFirst.createTime;
            }
            netDb.netManName = "admin";
            netDb.netManNo = "admin";
            netDb.netTime = DateTime.Now;
            netDb.createManName = "admin";
            netDb.createTime = DateTime.Now;
            netDb.updateManName = "admin";
            netDb.updateManNo = "admin";
            netDb.updateTime = DateTime.Now;


            //操作日志
            MeterMonitorNote monitorNote = new MeterMonitorNote();
            monitorNote.baseSpotNo = actualFirst2.baseSpotNo;
            monitorNote.baseSpotName = actualFirst2.baseSpotName;
            monitorNote.operationContent = string.Format("产生一次计量数据和洁净数据");
            monitorNote.operationTime = actualFirst2.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            monitorNote.carNo = actualFirst2.carNo;
            monitorNote.meterNoteSource = "0";

            // 保存结净记录，后续改成统一提交模式  BourneCao

            MeterWorkCarActualService actualService = new MeterWorkCarActualService();
            RESTfulResult<string> rmI = actualService.doAddNet(actualFirst, actualFirst2, netDb, monitorNote, "1");
            if (rmI.Succeed)
            {
                rt.result = true;
                rt.resultInfo = "计量完成";
                PbCache.actualFirstNo = rmI.Data;
                PbCache.strQRCode = rmI.Message;
                if (actualFirst2.meterWeight.Value > actualFirst.meterWeight.Value)
                {
                    PbCache.isTareWeight = false;//毛重
                }
                else
                {
                    PbCache.isTareWeight = true;//皮重
                }
                
            }
            else
            {
                rt.result = false;
                rt.isError = true;
                rt.resultInfo = "称重数据保存失败";
            }

            return rt;
        }


        /// <summary>
        /// 混装/混卸保存逻辑
        /// </summary>
        /// <param name="scale">委托数据</param>
        /// <param name="actualFirst">自动创建的一次计量数据</param>
        /// <param name="actualFirst2">本次一次计量数据</param>
        public rtInfo doNetHZHX(PreTrackScale scale, MeterWorkCarActualFirst actualFirst, MeterWorkCarActualFirst actualFirst2)
        {
            // 构造结净数据
            MeterWorkCarActual netDb = new MeterWorkCarActual();

            // 复制委托
            if (scale != null)
            {
                EntityBase<PreTrackScale> entityBase3 = new EntityBase<PreTrackScale>();
                netDb = entityBase3.format<MeterWorkCarActual>(scale, netDb);
                netDb.isPreScale = "1";
            }

            

            netDb.carNo = actualFirst.carNo;
            netDb.tareTime = DateTime.Now;
            netDb.tareWeight = actualFirst.meterWeight.Value < actualFirst2.meterWeight.Value ? actualFirst.meterWeight.Value : actualFirst2.meterWeight.Value; // 谁小谁是皮 BourneCao
            netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
            netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
            netDb.tareMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            /* netDb.tare_man_no = "";
             netDb.tare_man_name = "";
             netDb.tare_class = "";
             netDb.tare_group = "";*/

            //netDb.actualFirst1No = actualFirst.meterWeight.Value > actualFirst2.meterWeight.Value ? actualFirst.actualFirstNo : actualFirst2.actualFirstNo;
            //netDb.actualFirst2No = actualFirst.meterWeight.Value < actualFirst2.meterWeight.Value ? actualFirst.actualFirstNo : actualFirst2.actualFirstNo;
            netDb.grossTime = actualFirst.createTime;
            netDb.grossWeight = actualFirst.meterWeight.Value > actualFirst2.meterWeight.Value ? actualFirst.meterWeight.Value : actualFirst2.meterWeight.Value; // 谁大谁是毛 BourneCao
            netDb.baseSpot1No = actualFirst.baseSpotNo;
            netDb.baseSpot1Name = actualFirst.baseSpotName;
            netDb.grossMode = "2"; // 1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            netDb.netManName = "admin";
            netDb.netManNo = "admin";
            netDb.netTime = DateTime.Now;
            netDb.grossManNo = "admin";
            netDb.grossManName = "admin";
            netDb.grossTime = DateTime.Now;
            netDb.tareManName = "admin";
            netDb.tareManNo = "admin";
            netDb.tareTime = DateTime.Now;
            netDb.createManName = "admin";
            netDb.createTime = DateTime.Now;
            netDb.updateManName = "admin";
            netDb.updateManNo = "admin";
            netDb.updateTime = DateTime.Now;
            netDb.grossClass = actualFirst.meterClass;
            netDb.grossGroup = actualFirst.meterGroup;
            netDb.valueFlag = "1";

            netDb.netTime = DateTime.Now;

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

            netDb.addWeight = (actualFirst.addWeight == null ? 0 : actualFirst.addWeight.Value);
            //净重=毛-皮-附加重量
            netDb.netWeight = netDb.grossWeight - netDb.tareWeight - netDb.addWeight;
            netDb.netMode = "1"; //1：正常结净；2：匹配结净
            netDb.valueFlag = "0"; //0=有效，1=作废
            netDb.uploadFlag = "0"; //0=待审核；1：待上传；2=已上传
            netDb.dataSource = "1";//1 = 智能终端，2 = 本地计量，3 = 应急计量，4 = 手动录入(计量票据录入)
                                   //要判断下当前是否勾选了不验证皮重


            //操作日志
            MeterMonitorNote monitorNote = new MeterMonitorNote();
            monitorNote.baseSpotNo = actualFirst2.baseSpotNo;
            monitorNote.baseSpotName = actualFirst2.baseSpotName;
            monitorNote.operationContent = string.Format("混装/混卸保存逻辑");
            monitorNote.operationTime = actualFirst2.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            monitorNote.carNo = actualFirst2.carNo;
            monitorNote.meterNoteSource = "0";

            // 保存结净记录，后续改成统一提交模式  BourneCao

            MeterWorkCarActualService actualService = new MeterWorkCarActualService();
            RESTfulResult<string> rmI = actualService.doAddNetHZHX(actualFirst2, actualFirst, netDb, monitorNote, "0");

            if (rmI.Succeed)
            {
                rt.result = true;
                rt.resultInfo = "计量完成";
                PbCache.actualFirstNo = rmI.Data;
                PbCache.strQRCode = rmI.Message;
                if (actualFirst2.meterWeight.Value > actualFirst.meterWeight.Value)
                {
                    PbCache.isTareWeight = false;//毛重
                }
                else
                {
                    PbCache.isTareWeight = true;//皮重
                }
            }
            else
            {
                rt.result = false;
                rt.isError = true;
                rt.resultInfo = "称重数据保存失败";
            }

            return rt;
        }

        /// <summary>
        /// 产生一次计量数据，同时结净并匹配委托
        /// </summary>
        /// <param name="scale"></param>
        /// <param name="actualFirst"></param>
        public rtInfo doNetAndScale(PreTrackScale scale, MeterWorkCarActualFirst actualFirst, MeterWorkCarActualFirst actualFirst2)
        {
            // 构造结净数据
            MeterWorkCarActual netDb = new MeterWorkCarActual();

            // 复制对象
            if (scale != null)
            {
                EntityBase<PreTrackScale> entityBase = new EntityBase<PreTrackScale>();
                netDb = entityBase.format<MeterWorkCarActual>(scale, netDb);
            }

            netDb.tareTime = DateTime.Now;
            netDb.tareWeight = actualFirst.meterWeight.Value < PbCache.lockWgt ? actualFirst.meterWeight.Value : PbCache.lockWgt; // 谁小谁是皮 BourneCao
            netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
            netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
            netDb.tareMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            /* netDb.tare_man_no = "";
             netDb.tare_man_name = "";
             netDb.tare_class = "";
             netDb.tare_group = "";*/

            netDb.actualFirst1No = actualFirst.actualFirstNo;
            netDb.actualFirst2No = actualFirst.actualFirstNo;
            netDb.grossTime = actualFirst.createTime;
            netDb.grossWeight = actualFirst.meterWeight.Value > PbCache.lockWgt ? actualFirst.meterWeight.Value : PbCache.lockWgt; // 谁大谁是毛 BourneCao
            netDb.baseSpot1No = actualFirst.baseSpotNo;
            netDb.baseSpot1Name = actualFirst.baseSpotName;
            netDb.grossMode = "2"; // 1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            netDb.grossManNo = actualFirst.createManNo;
            netDb.grossManName = actualFirst.createManName;
            netDb.grossClass = actualFirst.meterClass;
            netDb.grossGroup = actualFirst.meterGroup;
            netDb.valueFlag = "1";

            netDb.netTime = DateTime.Now;

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

            netDb.addWeight = (actualFirst.addWeight == null ? 0 : actualFirst.addWeight.Value);
            //净重=毛-皮-附加重量
            netDb.netWeight = netDb.grossWeight - netDb.tareWeight - netDb.addWeight;
            netDb.netMode = "1"; //1：正常结净；2：匹配结净
            netDb.valueFlag = "0"; //0=有效，1=作废
            netDb.uploadFlag = "0"; //0=待审核；1：待上传；2=已上传
                                    //要判断下当前是否勾选了不验证皮重


            // 保存结净记录，后续改成统一提交模式  BourneCao

            return rt;
        }

        /// <summary>
        /// 产生一次计量数据，同时结净并匹配委托（多委托）
        /// </summary>
        /// <param name="scale"></param>
        /// <param name="actualFirst"></param>
        /// <param name="actualFirst1"></param>
        public rtInfo doNetAddTwo(List<PreTrackScale> scale, MeterWorkCarActualFirst actualFirst, MeterWorkCarActualFirst actualFirst1)
        {
            // 构造结净数据
            MeterWorkCarActual netDb = new MeterWorkCarActual();

            
           
            netDb.actualFirst1No = actualFirst1.meterWeight.Value > actualFirst.meterWeight.Value ? "" : actualFirst.actualFirstNo;
            netDb.actualFirst2No = actualFirst1.meterWeight.Value > actualFirst.meterWeight.Value ? actualFirst.actualFirstNo : "";

            netDb.tareTime = DateTime.Now;
            netDb.tareWeight = actualFirst.meterWeight.Value < actualFirst1.meterWeight.Value ? actualFirst.meterWeight.Value : actualFirst1.meterWeight.Value; // 谁小谁是皮 BourneCao
            netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
            netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
            netDb.tareMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            
            netDb.tareClass = actualFirst1.meterClass;
            netDb.tareGroup = actualFirst1.meterGroup;
            
            netDb.grossTime = actualFirst.createTime;
            netDb.grossWeight = actualFirst.meterWeight.Value > PbCache.lockWgt ? actualFirst.meterWeight.Value : PbCache.lockWgt; // 谁大谁是毛 BourneCao
            netDb.baseSpot1No = actualFirst.baseSpotNo;
            netDb.baseSpot1Name = actualFirst.baseSpotName;
            netDb.grossMode = "2"; // 1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            
            netDb.grossClass = actualFirst.meterClass;
            netDb.grossGroup = actualFirst.meterGroup;

            netDb.netTime = DateTime.Now;

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

            netDb.netManName = "admin";
            netDb.netManNo = "admin";
            netDb.netTime = DateTime.Now;
            netDb.grossManNo = "admin";
            netDb.grossManName = "admin";
            netDb.grossTime = DateTime.Now;
            netDb.tareManName = "admin";
            netDb.tareManNo = "admin";
            netDb.tareTime = DateTime.Now;
            netDb.createManName = "admin";
            netDb.createTime = DateTime.Now;
            netDb.updateManName = "admin";
            netDb.updateManNo = "admin";
            netDb.updateTime = DateTime.Now;

            netDb.addWeight = (actualFirst.addWeight == null ? 0 : actualFirst.addWeight.Value);
            //净重=毛-皮-附加重量
            netDb.netWeight = netDb.grossWeight - netDb.tareWeight - netDb.addWeight;
            netDb.netMode = "1"; //1：正常结净；2：匹配结净
            netDb.valueFlag = "0"; //0=有效，1=作废
            netDb.uploadFlag = "0"; //0=待审核；1：待上传；2=已上传
            netDb.dataSource = "1"; //1 = 智能终端，2 = 本地计量，3 = 应急计量，4 = 手动录入(计量票据录入)
                                    //要判断下当前是否勾选了不验证皮重

            // 保存结净记录，后续改成统一提交模式  BourneCao

            MeterWorkCarActualService actualService = new MeterWorkCarActualService();
            RESTfulResult<string> rmI = actualService.doNetAddTwo(scale, actualFirst1, netDb);

            if (rmI.Succeed)
            {
                rt.result = true;
                rt.resultInfo = "计量完成，双委托洁净";
                PbCache.actualFirstNo = rmI.Data;
                PbCache.strQRCode = rmI.Message;
                if (actualFirst1.meterWeight.Value > actualFirst.meterWeight.Value)
                {
                    PbCache.isTareWeight = false;//毛重
                }
                else
                {
                    PbCache.isTareWeight = true;//皮重
                }
            }
            else
            {
                rt.result = false;
                rt.isError = true;
                rt.resultInfo = "称重数据保存失败";
            }
            return rt;
        }

        /// <summary>
        /// 期限皮重保存洁净
        /// </summary>
        /// <param name="scale"></param>
        /// <param name="actualFirst"></param>
        public rtInfo doNetTermTare(PreTrackScale scale, MeterWorkCarActualFirst actualFirst)
        {
            // 构造结净数据
            MeterWorkCarActual netDb = new MeterWorkCarActual();

            if (scale != null)
            {
                EntityBase<PreTrackScale> entityBase = new EntityBase<PreTrackScale>();
                netDb = entityBase.format<MeterWorkCarActual>(scale, netDb);
                netDb.isPreScale = "1";//是否匹配委托，0未匹配，1已匹配
            }

            netDb.actualFirst1No = actualFirst.actualFirstNo;

            netDb.tareTime = DateTime.Now;
            netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
            netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
            netDb.tareMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配

            netDb.grossTime = actualFirst.createTime;
            netDb.grossWeight = actualFirst.meterWeight.Value; // 谁大谁是毛 BourneCao
            netDb.baseSpot1No = actualFirst.baseSpotNo;
            netDb.baseSpot1Name = actualFirst.baseSpotName;
            netDb.grossMode = "2"; // 1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            netDb.grossClass = actualFirst.meterClass;
            netDb.grossGroup = actualFirst.meterGroup;

            netDb.netTime = DateTime.Now;

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

            netDb.addWeight = (actualFirst.addWeight == null ? 0 : actualFirst.addWeight.Value);
            //净重=毛-皮-附加重量
            netDb.netMode = "2"; //1：正常结净；2：匹配结净
            netDb.valueFlag = "1"; //0=有效，1=作废
            netDb.uploadFlag = "1"; //0=待审核；1：待上传；2=已上传
            netDb.dataSource = "1"; //1 = 智能终端，2 = 本地计量，3 = 应急计量，4 = 手动录入(计量票据录入)
                                    //要判断下当前是否勾选了不验证皮重

            netDb.netManName = "admin";
            netDb.netManNo = "admin";
            netDb.netTime = DateTime.Now;
            netDb.grossManNo = "admin";
            netDb.grossManName = "admin";
            netDb.grossTime = DateTime.Now;
            netDb.tareManName = "admin";
            netDb.tareManNo = "admin";
            netDb.tareTime = DateTime.Now;
            netDb.createManName = "admin";
            netDb.createTime = DateTime.Now;
            netDb.updateManName = "admin";
            netDb.updateManNo = "admin";
            netDb.updateTime = DateTime.Now;

            // 保存结净记录，后续改成统一提交模式  BourneCao

            MeterWorkCarActualService actualService = new MeterWorkCarActualService();
            RESTfulResult<string> rmI = actualService.doNetTermTare(actualFirst, netDb,"1");

            if (rmI.Succeed)
            {
                rt.result = true;
                rt.resultInfo = "计量完成";
                PbCache.actualFirstNo = rmI.Data;
                PbCache.strQRCode = rmI.Message;
                PbCache.isTareWeight = false;//皮重
            }
            else
            {
                rt.result = false;
                rt.isError = true;
                rt.resultInfo = "称重数据保存失败";
            }
            return rt;
        }

        /// <summary>
        /// 一车多卸
        /// a. 取第一条委托正常洁净产生一条净重记录
        /// b.并自动生成一条一次毛重计量数据并和第二条委托匹配，重量值为当次计量数据中的毛重
        /// </summary>
        /// <param name="scale"></param>
        /// <param name="actualFirst"></param>
        public rtInfo doNetMass(List<PreTrackScale> scale, MeterWorkCarActualFirst actualFirst, MeterWorkCarActualFirst actualFirst1)
        {
            // 构造结净数据
            MeterWorkCarActual netDb = new MeterWorkCarActual();

            // 复制对象
            if (scale != null)
            {
                EntityBase<PreTrackScale> entityBase = new EntityBase<PreTrackScale>();
                netDb = entityBase.format<MeterWorkCarActual>(scale[0], netDb);
            }

            netDb.tareTime = DateTime.Now;
            netDb.tareWeight = actualFirst.meterWeight.Value < PbCache.lockWgt ? actualFirst.meterWeight.Value : PbCache.lockWgt; // 谁小谁是皮 BourneCao
            netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
            netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
            netDb.tareMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            /* netDb.tare_man_no = "";
             netDb.tare_man_name = "";
             netDb.tare_class = "";
             netDb.tare_group = "";*/

            netDb.actualFirst1No = actualFirst.actualFirstNo;
            netDb.grossTime = actualFirst.createTime;
            netDb.grossWeight = actualFirst.meterWeight.Value > PbCache.lockWgt ? actualFirst.meterWeight.Value : PbCache.lockWgt; // 谁大谁是毛 BourneCao
            netDb.baseSpot1No = actualFirst.baseSpotNo;
            netDb.baseSpot1Name = actualFirst.baseSpotName;
            netDb.grossMode = "2"; // 1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            netDb.grossManNo = actualFirst.createManNo;
            netDb.grossManName = actualFirst.createManName;
            netDb.grossClass = actualFirst.meterClass;
            netDb.grossGroup = actualFirst.meterGroup;

            netDb.netTime = DateTime.Now;

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

            netDb.addWeight = (actualFirst.addWeight == null ? 0 : actualFirst.addWeight.Value);
            //净重=毛-皮-附加重量
            netDb.netWeight = netDb.grossWeight - netDb.tareWeight - netDb.addWeight;
            netDb.netMode = "1"; //1：正常结净；2：匹配结净
            netDb.valueFlag = "0"; //0=有效，1=作废
            netDb.uploadFlag = "0"; //0=待审核；1：待上传；2=已上传
                                    //要判断下当前是否勾选了不验证皮重

            // 保存结净记录，后续改成统一提交模式  BourneCao

            return rt;
        }

        public rtInfo validHisTare(string WeightType = "001033001")
        {
            // 查询当前秤点是否启用历史皮重判断（暂时不考虑此限制）By BourneCao
            //String spotNo = PbCache.sportInfo.baseSpotNo;

            /* 判断当前重量是否满足历史皮重相应限制 */
            // 读取车辆历史皮重信息
            List<MeterBaseHisTareData> validHis = getHisTare(PbCache.lockCarNo);

            // 读取历史皮重计算配置
            MeterBaseHisTareCount hisTareCount = PbCache.tareCount;

            // 没有历史皮重记录，相关操作都可跳过
            if (validHis.Count >= 1)
            {
                double maxWgt = 0, minWgt = 0, wgt = PbCache.lockWgt;

                if (hisTareCount.countTypeNo == "001011001") //固重 当前重量>=历史皮重-固重   当前重量<=历史皮重+固重
                {
                    long meterWeight = (PbCache.tareCount.contrastTypeNo == "001012001" ? validHis[0].upWeight.Value : validHis[0].meterWeight.Value);

                    maxWgt = meterWeight + hisTareCount.fixedWeightValue.Value;
                    minWgt = meterWeight - hisTareCount.fixedWeightValue.Value;

                    if (PbCache.lockWgt >= minWgt && PbCache.lockWgt <= maxWgt)
                    {
                        PbCache.monitorResult.valid_wgt = true;
                    }
                    else
                    {
                        PbCache.monitorResult.valid_wgt = false;
                        PbCache.ResultMessage = "当前皮重【" + PbCache.lockWgt / 1000 + "T 】在皮重配置范围，历史皮重为【" + Convert.ToDouble(meterWeight) / 1000 + "T】,偏差最大允许重量为【" + hisTareCount.fixedWeightValue.Value + "kg】";
                    }
                }
                else if (hisTareCount.countTypeNo == "001011002") //比例 当前重量>=历史皮重-历史皮重*比例   当前重量<=历史皮重+历史皮重*比例
                {
                    long meterWeight = (PbCache.tareCount.contrastTypeNo == "001012001" ? validHis[0].upWeight.Value : validHis[0].meterWeight.Value);

                    maxWgt = meterWeight + meterWeight * (hisTareCount.rateValue.Value / 100); //历史皮重+历史皮重*比例
                    minWgt = meterWeight - meterWeight * (hisTareCount.rateValue.Value / 100); //历史皮重-历史皮重*比例

                    if (PbCache.lockWgt >= minWgt && PbCache.lockWgt <= maxWgt)
                    {
                        PbCache.monitorResult.valid_wgt = true;
                    }
                    else
                    {
                        PbCache.monitorResult.valid_wgt = false;
                        PbCache.ResultMessage = "当前皮重【" + PbCache.lockWgt / 1000 + "T 】不在皮重配置范围，历史皮重为【" + (double)Convert.ToDouble(meterWeight) / 1000 + "T】,偏差最大允许比例值为【" + hisTareCount.fixedWeightValue.Value + "%】";
                    }
                }
                else
                {
                    PbCache.monitorResult.valid_wgt = false;
                    PbCache.ResultMessage = string.Format("未找到历史皮重计算类型{0}配置数据", hisTareCount.countTypeNo);
                }
            }
            else
            {
                if (WeightType == "001033002")
                {
                    PbCache.monitorResult.valid_wgt = true;
                }
                //PbCache.monitorResult.valid_wgt = true;
                //PbCache.ResultMessage = "0";
            }

            return rt;
        }

        public List<MeterBaseHisTareData> getHisTare(string carNo)
        {
            List<MeterBaseHisTareData> lmhtd = new List<MeterBaseHisTareData>();

            PbCache.monitorResult.haveHisTare = false;//不存在历史皮
            MeterBaseHisTareDataService mbtds = new MeterBaseHisTareDataService();
            RESTfulResult<List<MeterBaseHisTareData>> rm = mbtds.doQueryWf(new MeterBaseHisTareData { valueFlag = "0", carNo = carNo });
            if (rm.Succeed)
            {
                if (rm.Data != null && rm.Data.Count > 0)
                {
                    PbCache.monitorResult.haveHisTare = true;//存在历史皮
                    lmhtd = rm.Data;
                }
            }
            else
            {
                lmhtd = null;
            }
            PbCache.monitorResult.hisTareData = lmhtd;
            return lmhtd;
        }


        public rtInfo doTermTareData()
        {
            MeterBaseTermTareData mbttd = new MeterBaseTermTareData();
            mbttd.baseSpotNo = PbCache.sportInfo.baseSpotNo;
            mbttd.baseSpotName = PbCache.sportInfo.baseSpotName;
            mbttd.carNo = PbCache.collect.carno;
            mbttd.meterWeight = PbCache.lockWgt;
            mbttd.valueFlag = "0";
            mbttd.createTime = DateTime.Now;
            mbttd.createManNo = PbCache.sportInfo.baseSpotNo;
            mbttd.createManName = PbCache.sportInfo.baseSpotName;
            mbttd.meterTime = mbttd.createTime.Value;
            mbttd.startTime = mbttd.createTime.Value;

            //操作日志
            MeterMonitorNote monitorNote = new MeterMonitorNote();
            monitorNote.baseSpotNo = mbttd.baseSpotNo;
            monitorNote.baseSpotName = mbttd.baseSpotName;
            monitorNote.operationContent = string.Format("保存期限皮重");
            monitorNote.operationTime = mbttd.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            monitorNote.carNo = mbttd.carNo;
            monitorNote.meterNoteSource = "0";

            //期限皮重数据保存
            RESTfulResult<string> RESTfulResult = termTareDate.doSaveWf(mbttd, monitorNote);

            rt.result = true;
            rt.isError = false;
            rt.resultInfo = "期限皮重保存成功";

            return rt;
        }

        public rtInfo doTermTareNet(PreTrackScale scale, MeterWorkCarActualFirst actualFirst, MeterWorkCarActualFirst actualFirst1)
        {
            //期限皮重数据保存
            MeterWorkCarActualService actualService = new MeterWorkCarActualService();
            RESTfulResult<string> rmI = actualService.doTermTareNet(scale, actualFirst, actualFirst1);

            if (rmI.Succeed)
            {
                rt.result = true;
                rt.resultInfo = "计量完成";
                PbCache.actualFirstNo = rmI.Data;
                PbCache.strQRCode = rmI.Message;
                PbCache.isTareWeight = false;//皮重
            }
            else
            {
                rt.result = false;
                rt.isError = true;
                rt.resultInfo = "称重数据保存失败";
            }
            return rt;
        }

        /// <summary>
        /// 多毛一重
        /// </summary>
        /// <param name="scale"></param>
        /// <param name="actualFirst"></param>
        /// <param name="actualFirst1"></param>
        public rtInfo doNetAddMuch(MeterWorkCarActualFirst actualFirst)
        {
            // 保存结净记录，后续改成统一提交模式  BourneCao

            MeterWorkCarActualService actualService = new MeterWorkCarActualService();
            RESTfulResult<string> rmI = actualService.doNetAddMuch(actualFirst);
            if (rmI.Succeed)
            {
                rt.result = true;
                rt.resultInfo = "计量完成";
                PbCache.actualFirstNo = rmI.Data;
                PbCache.strQRCode = rmI.Message;
                PbCache.isTareWeight = true;//皮重
            }
            else
            {
                rt.result = false;
                rt.isError = true;
                rt.resultInfo = "称重数据保存失败";
            }
            return rt;
        }

        /// <summary>
        /// 产生一次计量数据和没有委托的洁净数据
        /// </summary>
        /// <param name="scale"></param>
        /// <param name="actualFirst"></param>
        public rtInfo doNetError(PreTrackScale scale, MeterWorkCarActualFirst actualFirst, MeterWorkCarActualFirst actualFirst1, MeterWorkCarActualFirst mwaf)
        {
            // 构造结净数据
            MeterWorkCarActual netDb = new MeterWorkCarActual();

            // 将第一个一次数据状态改为 已结净
            //actualFirst.valueFlag = "2";
            netDb.isPreScale = "0";
            // 复制对象
            if (scale != null)
            {
                EntityBase<PreTrackScale> entityBase = new EntityBase<PreTrackScale>();
                netDb = entityBase.format<MeterWorkCarActual>(scale, netDb);
                netDb.isPreScale = "1";
            }

            netDb.carNo = actualFirst.carNo;
            netDb.tareTime = actualFirst.createTime;
            netDb.tareWeight = actualFirst.meterWeight.Value;
            netDb.baseSpot2No = actualFirst.baseSpotNo;
            netDb.baseSpot2Name = actualFirst.baseSpotName;
            netDb.tareMode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            netDb.actualFirst1No = actualFirst.actualFirstNo;
            netDb.actualFirst2No = actualFirst1.actualFirstNo;
            netDb.grossTime = actualFirst1.createTime;
            netDb.grossWeight = actualFirst1.meterWeight.Value;
            netDb.baseSpot1No = actualFirst1.baseSpotNo;
            netDb.baseSpot1Name = actualFirst1.baseSpotName;
            netDb.grossMode = "2"; // 1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
            netDb.grossClass = actualFirst1.meterClass;
            netDb.grossGroup = actualFirst1.meterGroup;
            netDb.valueFlag = "1";

            netDb.netTime = DateTime.Now;

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

            netDb.addWeight = (actualFirst.addWeight == null ? 0 : actualFirst.addWeight.Value);
            //净重=毛-皮-附加重量
            netDb.netWeight = netDb.grossWeight - netDb.tareWeight - netDb.addWeight;
            netDb.netMode = "1"; //1：正常结净；2：匹配结净
            netDb.uploadFlag = "1"; //0=待审核；1：待上传；2=已上传
            netDb.dataSource = "1";//1 = 智能终端，2 = 本地计量，3 = 应急计量，4 = 手动录入(计量票据录入)
                                   //要判断下当前是否勾选了不验证皮重

            netDb.grossManNo = actualFirst1.createManNo;
            netDb.grossManName = actualFirst1.createManName;
            netDb.grossTime = actualFirst1.createTime;


            netDb.tareManName = actualFirst.createManNo;
            netDb.tareManNo = actualFirst.createManName;
            netDb.tareTime = actualFirst.createTime;


            netDb.netManName = "admin";
            netDb.netManNo = "admin";
            netDb.netTime = DateTime.Now;
            netDb.createManName = "admin";
            netDb.createTime = DateTime.Now;
            netDb.updateManName = "admin";
            netDb.updateManNo = "admin";
            netDb.updateTime = DateTime.Now;


            //操作日志
            MeterMonitorNote monitorNote = new MeterMonitorNote();
            monitorNote.baseSpotNo = actualFirst1.baseSpotNo;
            monitorNote.baseSpotName = actualFirst1.baseSpotName;
            monitorNote.operationContent = string.Format("产生一次计量数据和洁净数据");
            monitorNote.operationTime = actualFirst1.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            monitorNote.carNo = actualFirst1.carNo;
            monitorNote.meterNoteSource = "0";

            // 保存结净记录，后续改成统一提交模式  BourneCao

            MeterWorkCarActualService actualService = new MeterWorkCarActualService();
            RESTfulResult<string> rmI = actualService.doAddNetError(actualFirst, mwaf, netDb, monitorNote, "1");
            if (rmI.Succeed)
            {
                rt.result = true;
                rt.resultInfo = "计量完成";
                PbCache.actualFirstNo = rmI.Data;
                PbCache.strQRCode = rmI.Message;
                if (mwaf.weightType == "0")
                {
                    PbCache.isTareWeight = false;//毛重
                }
                else
                {
                    PbCache.isTareWeight = true;//皮重
                }

            }
            else
            {
                rt.result = false;
                rt.isError = true;
                rt.resultInfo = "称重数据保存失败";
            }

            return rt;
        }

        /// <summary>
        /// 失败执行逻辑
        /// </summary>
        public rtInfo doError(string msg)
        {
            rt.result = false;
            rt.isError = true;
            rt.resultInfo = "计量失败：" + msg;
            
            return rt;
        }
        #endregion
    }
}
