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

namespace MeterSceneLibrary
{
    /// <summary>
    /// 司秤工远程计量
    /// 没有录入物料或者录入的物料是内倒计量业务编号，则认为是内倒计量
    /// 若是内倒计量，没有录入物料信息，则认为是过皮，录入了认为是过毛
    /// 若存在有效期限皮重，且不存一次计量数据则与期限皮重结合，若没有期限皮重则保存数据
    /// 若保存的时候有一次有效数据则与一次数据结净
    /// </summary>
    public class InnerRemoteMeter
    {
        public rtInfo MeterMethod(InnerMeterInfo scale, CoreAppUser appUser)
        {
            if (appUser == null)
            {
                appUser = new CoreAppUser
                {
                    userid = "",
                    username = "",
                    userGroup = "",
                    userClass = ""
                };
            }

            rtInfo rt = new rtInfo();
            rt.isError = false;

            #region 只要是计量员计量则不验证任何条件，只要录入了物料则是毛，否则是皮

            /*
            验证是否是内倒车辆/是否黑名单等
            string ResultMessage = "";
            bool isError = false;
            validCarCard vcc = new validCarCard();
            if (!vcc.ValidMethod(scale, out ResultMessage, out isError))
            {
                MessageBox.Show(ResultMessage);
                return;
            }
            //*/

            #endregion 只要是计量员计量则不验证任何条件，只要录入了物料则是毛，否则是皮

            #region 获取一次计量数据及期限皮重数据

            //获取一次计量数据
            MeterWorkCarActualFirstService mwcfs = new MeterWorkCarActualFirstService();
            RESTfulResult<List<MeterWorkCarActualFirst>> rmX = mwcfs.doQueryWf(new MeterWorkCarActualFirst { valueFlag = "1", carNo = scale.carNo });
            if (!rmX.Succeed)
            {
                rt.result = false;
                rt.resultInfo = "获取一次计量数据异常";
                PbCache.ResultMessage = "获取一次计量数据异常！";
                rt.isError = true;
                return rt;
            }

            //获取期限皮重
            MeterBaseTermTareDataService mbttd = new MeterBaseTermTareDataService();
            RESTfulResult<List<MeterBaseTermTareData>> rml = mbttd.doQueryWf(new MeterBaseTermTareData { carNo = scale.carNo, valueFlag = "0" });
            if (!rml.Succeed)
            {
                rt.result = false;
                rt.isError = true;
                rt.resultInfo = "称重数据保存失败";
                PbCache.ResultMessage = "获取期限皮重异常，计量失败：" + rml.ResultMessage;
                return rt;
            }

            #endregion 获取一次计量数据及期限皮重数据

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

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

            if (i == 0)
            {
                rml.Data = null;
            }

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

            //相同名称的均赋值一次
            MeterWorkCarActualFirst mwaf = new MeterWorkCarActualFirst();
            EntityBase<InnerMeterInfo> entityBase = new EntityBase<InnerMeterInfo>();
            mwaf = entityBase.format<MeterWorkCarActualFirst>(scale, mwaf);
            mwaf.actualFirstNo = DateTime.Now.ToString("yyyyMMdd");//10
            mwaf.meterWeight = PbCache.lockWgt;//计量重量
            mwaf.addWeight = PbCache.addWgt;//附加重量
            //mwaf.weightType = "0"; //0=毛重；1=常规皮重；2=期限皮重；3=历史皮重；4=标识皮重；5=自重皮重
            mwaf.createManNo = appUser.userid;
            mwaf.createManName = appUser.username;
            mwaf.meterClass = appUser.userClass;
            mwaf.meterGroup = appUser.userGroup;
            mwaf.createTime = DateTime.Now;
            mwaf.baseSpotNo = PbCache.sportInfo.baseSpotNo;//计量点编号
            mwaf.baseSpotName = PbCache.sportInfo.baseSpotName;
            //mwaf.memo = "";
            mwaf.valueFlag = "1"; //1有效
            mwaf.updateManName = "";
            mwaf.updateManNo = "";
            mwaf.updateTime = null;
            mwaf.checkFlag = "0"; //0未审核
            mwaf.meterMode = "1"; //智能计量

            //车辆上来后，若scale中的物料没有值，则认为是过皮，此时要去验证下是否存在历史皮，若有且符合则过皮，否则若重量大于提示请录入xxx
            if (string.IsNullOrEmpty(scale.matterNo))
            {
                //如果没有一次计量数据
                if (rmX.Data == null || rmX.Data.Count == 0)
                {
                    //操作日志
                    MeterMonitorNote monitorNote = new MeterMonitorNote();
                    monitorNote.baseSpotNo = mwaf.baseSpotNo;
                    monitorNote.baseSpotName = mwaf.baseSpotName;
                    monitorNote.operationContent = string.Format("称重保存");
                    monitorNote.operationTime = mwaf.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                    monitorNote.carNo = mwaf.carNo;
                    monitorNote.meterNoteSource = "4";
                    monitorNote.meterManNo = appUser.userid;
                    monitorNote.meterManName = appUser.username;

                    //存储皮重信息及历史皮重信息
                    mwaf.weightType = "1";
                    RESTfulResult<string> rmI = mwcfs.doAddInnerWf(mwaf, monitorNote, "1");//保存皮重及历史皮重
                    if (rmI.Succeed)
                    {
                        rt.result = true;
                        rt.isError = false;
                        rt.resultInfo = "称重完成";
                        PbCache.ResultMessage = "称重完成";
                        PbCache.actualFirstNo = rmI.Data;
                        PbCache.isTareWeight = true;//皮重
                    }
                    else
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = "称重数据保存失败";
                        PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                    }
                }
                else
                {
                    if (rmX.Data[0].weightType == "0") //一次为毛，当前为皮
                    {
                        mwaf.weightType = "1";
                        scale.matterNo = rmX.Data[0].matterNo;
                        scale.matterName = rmX.Data[0].matterName;
                        scale.batchNo = rmX.Data[0].batchNo;
                        scale.meterTypeNo = "001006003";
                        scale.forwardingUnitNo = rmX.Data[0].forwardingUnitNo;
                        scale.forwardingUnitName = rmX.Data[0].forwardingUnitName;
                        scale.receivingUintNo = rmX.Data[0].receivingUintNo;
                        scale.receivingUintName = rmX.Data[0].receivingUintName;

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

                        //netDb.actualFirst1No = mb.actualFirstNo; //皮重计量作业编号
                        netDb.tareTime = DateTime.Now;
                        netDb.tareWeight = PbCache.lockWgt;
                        netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
                        netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
                        netDb.tareMode = string.IsNullOrEmpty(appUser.userid) ? "2" : "1";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                        netDb.tareClass = appUser.userClass;
                        netDb.tareGroup = appUser.userGroup;
                        netDb.tareManNo = appUser.userid;
                        netDb.tareManName = appUser.username;

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

                        netDb.netTime = DateTime.Now;

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

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

                        //操作日志
                        MeterMonitorNote monitorNote = new MeterMonitorNote();
                        monitorNote.baseSpotNo = mwaf.baseSpotNo;
                        monitorNote.baseSpotName = mwaf.baseSpotName;
                        monitorNote.operationContent = string.Format("结净称重保存");
                        monitorNote.operationTime = mwaf.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                        monitorNote.carNo = mwaf.carNo;
                        monitorNote.meterNoteSource = "4";
                        monitorNote.meterManNo = appUser.userid;
                        monitorNote.meterManName = appUser.username;

                        RESTfulResult<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb, monitorNote, "1");
                        if (rmI.Succeed)
                        {
                            rt.result = true;
                            rt.isError = false;
                            rt.resultInfo = "称重完成";
                            PbCache.ResultMessage = "称重完成";
                            PbCache.actualFirstNo = rmI.Data;
                            PbCache.isTareWeight = true;//皮重
                        }
                        else
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = "称重数据保存失败";
                            PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                        }
                    }
                    else
                    {
                        //重复计量
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = "称重数据保存失败";
                        PbCache.ResultMessage = "第一次计量数据为皮重信息，皮皮不能结合；若计量毛重请点击【业务编号】输入业务号，若业务号已存在，请在业务号文本框中按回车键！！";
                    }
                }
            }
            else //若录入了物料等信息认为是毛
            {
                //如果没有一次计量数据
                if (rmX.Data == null || rmX.Data.Count == 0)
                {
                    if (rml.Data != null && rml.Data.Count > 0 && PbCache.isTermTare)
                    {
                        //============无一次计量数据：先毛后皮，期限皮重结净：开始===============
                        mwaf.weightType = "0";//毛重信息
                        mwaf.addWeight = PbCache.addWgt;

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

                        //netDb.actualFirst1No = mb.actualFirstNo; //皮重计量作业编号
                        netDb.grossTime = DateTime.Now;
                        netDb.grossWeight = PbCache.lockWgt;
                        netDb.baseSpot1No = PbCache.sportInfo.baseSpotNo;
                        netDb.baseSpot1Name = PbCache.sportInfo.baseSpotName;
                        netDb.grossMode = string.IsNullOrEmpty(appUser.userid) ? "2" : "1"; //1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                        netDb.grossClass = appUser.userClass;
                        netDb.grossGroup = appUser.userGroup;
                        netDb.grossManNo = appUser.userid;
                        netDb.grossManName = appUser.username;

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

                        netDb.netTime = DateTime.Now;

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

                        netDb.addWeight = PbCache.addWgt;
                        //净重=毛-皮-附加重量
                        netDb.netWeight = netDb.grossWeight - netDb.tareWeight - netDb.addWeight;
                        netDb.netMode = "1"; //1：正常结净；2：匹配结净
                        netDb.valueFlag = "0"; //0=有效，1=作废
                        netDb.uploadFlag = "0"; //0=待审核；1：待上传；2=已上传

                        //操作日志
                        MeterMonitorNote monitorNote = new MeterMonitorNote();
                        monitorNote.baseSpotNo = mwaf.baseSpotNo;
                        monitorNote.baseSpotName = mwaf.baseSpotName;
                        monitorNote.operationContent = string.Format("结净称重保存");
                        monitorNote.operationTime = mwaf.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                        monitorNote.carNo = mwaf.carNo;
                        monitorNote.meterNoteSource = "4";
                        monitorNote.meterManNo = appUser.userid;
                        monitorNote.meterManName = appUser.username;

                        RESTfulResult<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb, monitorNote, "0");
                        if (rmI.Succeed)
                        {
                            rt.result = true;
                            rt.isError = false;
                            rt.resultInfo = "称重完成";
                            PbCache.ResultMessage = "称重完成";
                            PbCache.actualFirstNo = rmI.Data;
                            PbCache.isTareWeight = false;//毛重
                        }
                        else
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = "称重数据保存失败";
                            PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                        }
                        //============无一次计量数据：先毛后皮，期限皮重结净：结束===============
                    }
                    else
                    {
                        //============无一次计量数据：先毛后皮，单计量毛重：开始===============
                        MeterMonitorNote monitorNote = new MeterMonitorNote(); //操作日志
                        monitorNote.baseSpotNo = mwaf.baseSpotNo;
                        monitorNote.baseSpotName = mwaf.baseSpotName;
                        monitorNote.operationContent = string.Format("称重保存");
                        monitorNote.operationTime = mwaf.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                        monitorNote.carNo = mwaf.carNo;
                        monitorNote.meterNoteSource = "4";
                        monitorNote.meterManNo = appUser.userid;
                        monitorNote.meterManName = appUser.username;

                        //保存毛重计量信息
                        mwaf.weightType = "0";//毛重信息
                        RESTfulResult<string> rmI = mwcfs.doAddInnerWf(mwaf, monitorNote, "0");
                        if (rmI.Succeed)
                        {
                            rt.result = true;
                            rt.isError = false;
                            rt.resultInfo = "称重完成";
                            PbCache.ResultMessage = "称重完成";
                            PbCache.actualFirstNo = rmI.Data;
                        }
                        else
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = "称重数据保存失败";
                            PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                        }
                        //============无一次计量数据：先毛后皮，单计量毛重：开始===============
                    }
                }
                else
                {
                    //============当前为毛重+存在一次计量数据：再计量数据：开始===============
                    if (rmX.Data[0].weightType == "0" && rmX.Data[0].meterWeight < PbCache.lockWgt) //一次为毛重，且当前重量大于数据库毛重
                    {
                        DialogResult dr = MessageBox.Show("已经存在一条毛重数据，是否继续保存！", "提示", MessageBoxButtons.OKCancel);
                        if (dr == DialogResult.Cancel)
                        { 
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = "称重数据取消保存";
                            PbCache.ResultMessage = "第一次计量数据为毛重信息，毛毛不能结合";
                            return rt;
                        }
                    }

                    mwaf.weightType = "0";//毛重信息
                    mwaf.addWeight = PbCache.addWgt;

                    // 结净数据
                    MeterWorkCarActual netDb = new MeterWorkCarActual();
                    EntityBase<InnerMeterInfo> entityActual = new EntityBase<InnerMeterInfo>();
                    netDb = entityBase.format<MeterWorkCarActual>(scale, netDb);
                    if (rmX.Data[0].meterWeight < PbCache.lockWgt)
                    {
                        //netDb.actualFirst1No = mb.actualFirstNo; //皮重计量作业编号
                        netDb.grossTime = DateTime.Now;
                        netDb.grossWeight = PbCache.lockWgt;
                        netDb.baseSpot1No = PbCache.sportInfo.baseSpotNo;
                        netDb.baseSpot1Name = PbCache.sportInfo.baseSpotName;
                        netDb.grossMode = string.IsNullOrEmpty(appUser.userid) ? "2" : "1";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                        netDb.grossClass = appUser.userClass;
                        netDb.grossGroup = appUser.userGroup;
                        netDb.grossManNo = appUser.userid;
                        netDb.grossManName = appUser.username;

                        netDb.actualFirst2No = rmX.Data[0].actualFirstNo;
                        netDb.tareTime = rmX.Data[0].createTime;
                        netDb.tareWeight = rmX.Data[0].meterWeight;
                        netDb.baseSpot2No = rmX.Data[0].baseSpotNo;
                        netDb.baseSpot2Name = rmX.Data[0].baseSpotName;
                        netDb.tareMode = rmX.Data[0].meterMode;//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                        netDb.tareManNo = rmX.Data[0].createManNo;
                        netDb.tareManName = rmX.Data[0].createManName;
                        netDb.tareClass = rmX.Data[0].meterClass;
                        netDb.tareGroup = rmX.Data[0].meterGroup;
                    }
                    else
                    {
                        netDb.tareTime = DateTime.Now;
                        netDb.tareWeight = PbCache.lockWgt;
                        netDb.baseSpot2No = PbCache.sportInfo.baseSpotNo;
                        netDb.baseSpot2Name = PbCache.sportInfo.baseSpotName;
                        netDb.tareMode = string.IsNullOrEmpty(appUser.userid) ? "2" : "1";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                        netDb.tareClass = appUser.userClass;
                        netDb.tareGroup = appUser.userGroup;
                        netDb.tareManNo = appUser.userid;
                        netDb.tareManName = appUser.username;

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

                    netDb.netTime = DateTime.Now;

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

                    netDb.addWeight = PbCache.addWgt;
                    //净重=毛-皮-附加重量
                    netDb.netWeight = netDb.grossWeight - netDb.tareWeight - netDb.addWeight;
                    netDb.netMode = "1"; //1：正常结净；2：匹配结净
                    netDb.valueFlag = "0"; //0=有效，1=作废
                    netDb.uploadFlag = "0"; //0=待审核；1：待上传；2=已上传

                    //操作日志
                    MeterMonitorNote monitorNote = new MeterMonitorNote();
                    monitorNote.baseSpotNo = mwaf.baseSpotNo;
                    monitorNote.baseSpotName = mwaf.baseSpotName;
                    monitorNote.operationContent = string.Format("结净称重保存");
                    monitorNote.operationTime = mwaf.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                    monitorNote.carNo = mwaf.carNo;
                    monitorNote.meterNoteSource = "4";
                    monitorNote.meterManNo = appUser.userid;
                    monitorNote.meterManName = appUser.username;

                    RESTfulResult<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb, monitorNote, "0");
                    if (rmI.Succeed)
                    {
                        rt.result = true;
                        rt.isError = false;
                        rt.resultInfo = "称重完成";
                        PbCache.ResultMessage = "称重完成";
                        PbCache.actualFirstNo = rmI.Data;
                        PbCache.isTareWeight = false;//毛重
                    }
                    else
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = "称重数据保存失败";
                        PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                    }
                    //============当前为毛重+存在一次计量数据：再计量数据：结束===============
                }
            }

            return rt;
        }
    }
}