using Common;
using MeterConditionLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MeterSceneLibrary
{
    public class InnerFirstGrossSecondlyTare
    {
        /// <summary>
        /// InnerMeterInfo 一定会有值，汽车上磅的时候如果是皮重则将毛重信息带入进来
        /// </summary>
        /// <param name="scale"></param>
        /// <returns></returns>
        public rtInfo MeterMethod(InnerMeterInfo scale)
        {
            DbHelper db = new DbHelper();
            rtInfo rt = new rtInfo();
            rt.isError = false;
            
            /*
            //验证是否是内倒车辆
            string msgInfo = "";
            bool isError = false;
            validCarCard vcc = new validCarCard();
            if (!vcc.ValidMethod(scale, out msgInfo, out isError))
            {
                rt.result = false;
                rt.isError = isError;
                rt.resultInfo = PbCache.msgInfo;
                return rt;
            }

            //没有物料信息，说明第一没有一次毛重信息(若有的话会自动带到界面上)，第二说明车辆应该是要留皮重
            if (string.IsNullOrEmpty(scale.matter_no))
            {
                rt.result = false;
                rt.isError = false;
                return rt;
            }

            //如果有物料信息，有几种可能，第一种就是存在一次计量毛重信息，那么此时做洁净操作(需要验证下当前是不是重量差值1t内，若是的话，则提示不允许重复计量)
            //第二种可能就是不存在一次计量毛重信息，此时不管是毛还是皮，直接存储到数据表中，等第二次上秤时，若重量差值大于1t则查出并跟后面的数据洁净，同时
            //存储轻的为历史皮
            MeterWorkCarActualFirstService mwcfs = new MeterWorkCarActualFirstService();
            ResultModel<List<MeterWorkCarActualFirst>> rmX = mwcfs.doQueryWf(new MeterWorkCarActualFirst { value_flag = "0", car_no = scale.carno });
            if (rmX.state == "500")
            {
                rt.result = false;
                rt.resultInfo = "获取一次计量数据失败";
                rt.isError = true;
            }



            //历史皮重
            bool isHaveHisTare = false;
            validWgt validWgt = new validWgt();
            MeterBaseHisTareDataService mbtds = new MeterBaseHisTareDataService();
            ResultModel<List<MeterBaseHisTareData>> rm = mbtds.doQueryWf(new MeterBaseHisTareData { value_flag = "0", car_no = scale.carno });
            if (rm.state == "200")
            {
                //存在历史皮
                if (rm.data != null && rm.data.Count > 0)
                {
                    //验证是否是皮重，若为true则是皮，否则无法判断是毛还是皮
                    validWgt.ValidMethod(new PreTrackScale { car_no = scale.carno });
                    isHaveHisTare = true;
                }
            }

            //如果没有一次计量数据
            if (rmX.data == null || rmX.data.Count == 0)
            {
                //如果通过验证发现是皮则不能继续后续流程
                if (PbCache.monitorResult.valid_wgt)
                {
                    rt.result = false;
                    rt.isError = false;
                    return rt;
                }

                
                //验证物料时间段
                validMatTime vmt = new validMatTime();
                vmt.ValidMethod(new PreTrackScale { matter_no = scale.matter_no }, "1");
                if (!PbCache.monitorResult.valid_mat_time)
                {
                    rt.result = false;
                    rt.isError = true;
                    rt.resultInfo = PbCache.msgInfo;
                    return rt;
                }

                //是否增加验证当前重量需大于历史毛重，若无历史毛重也认为是毛重？？
                //存储一次毛重信息
                //验证通过，保存毛重信息***********************
                MeterWorkCarActualFirst mwaf = new MeterWorkCarActualFirst();

                //相同名称的均赋值一次
                EntityBase<InnerMeterInfo> entityBase = new EntityBase<InnerMeterInfo>();
                mwaf = entityBase.format<MeterWorkCarActualFirst>(scale, mwaf);

                mwaf.actual_first_no = DateTime.Now.ToString("yyyyMMdd");//10
                mwaf.meter_weight = PbCache.lockWgt;//计量重量
                mwaf.add_weight = PbCache.addWgt;//附加重量
                mwaf.weight_type = "0"; //0=毛重；1=常规皮重；2=期限皮重；3=历史皮重；4=标识皮重；5=自重皮重
                mwaf.create_man_no = "";
                mwaf.create_man_name = "";
                mwaf.create_time = DateTime.Now;
                mwaf.base_spot_no = PbCache.sportInfo.base_spot_no;//计量点编号
                mwaf.base_spot_name = PbCache.sportInfo.base_spot_name;
                mwaf.memo = "";
                mwaf.value_flag = "0"; //0有效
                mwaf.update_man_name = "";
                mwaf.update_man_no = "";
                mwaf.update_time = null;
                mwaf.check_flag = "0"; //0未审核
                mwaf.meter_mode = "2"; //智能计量

                bool isHaveTare = false; //是否期限皮计量
                MeterBaseTermTareData mb = new MeterBaseTermTareData();
                #region 验证是否有期限皮重情况
                //验证是否存在期限皮重信息，若存在有效期限皮重，且车辆不在黑名单则调用的应该是内倒车辆洁净方法
                //这里还得考虑下如果有洁净环节也得将洁净环节设置为使用

                //验证是否存在期限皮重信息，若存在有效期限皮重，且车辆不在黑名单则调用的应该是内倒车辆洁净方法
                MeterBaseCarBlacelistService bService = new MeterBaseCarBlacelistService();
                ResultModel<List<MeterBaseCarBlacelist>> rmB = bService.doQueryWf(new MeterBaseCarBlacelist { car_no = scale.carno, value_flag = "0" });
                if (rmB.state == "200")
                {
                    if (rmB.data == null || rmB.data.Count == 0) //不在黑名单内
                    {
                        MeterBaseTermTareDataService mbttd = new MeterBaseTermTareDataService();
                        ResultModel<List<MeterBaseTermTareData>> rml = mbttd.doQueryWf(new MeterBaseTermTareData { car_no = scale.carno, value_flag = "0" });
                        if (rml.state == "200")
                        {
                            if (rml.data != null && rml.data.Count > 0)
                            {
                                mb = rml.data[0];
                                isHaveTare = true;
                            }
                        }
                        else
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = PbCache.msgInfo;
                            return rt;
                        }
                    }
                }
                else
                {
                    rt.result = false;
                    rt.isError = true;
                    rt.resultInfo = PbCache.msgInfo;
                    return rt;
                }
                #endregion


                if (isHaveTare) //有期限皮重，做洁净操作
                {
                    //洁净数据
                    MeterWorkCarActual netDb = new MeterWorkCarActual();
                    EntityBase<InnerMeterInfo> entityActual = new EntityBase<InnerMeterInfo>();
                    netDb = entityBase.format<MeterWorkCarActual>(scale, netDb);

                    //netDb.actual_first1_no = mb.actual_first_no; //皮重计量作业编号
                    netDb.tare_time = mb.create_time;
                    netDb.tare_weight = mb.meter_weight;
                    netDb.base_spot2_no = mb.base_spot_no;
                    netDb.base_spot2_name = mb.base_spot_name;
                    netDb.tare_mode = "";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                    netDb.tare_man_no = "";
                    netDb.tare_man_name = "";
                    netDb.tare_class = "";
                    netDb.tare_group = "";

                    netDb.gross_time = DateTime.Now;
                    netDb.gross_weight = PbCache.lockWgt;
                    netDb.base_spot1_no = PbCache.sportInfo.base_spot_no;
                    netDb.base_spot1_name = PbCache.sportInfo.base_spot_name;
                    netDb.gross_mode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配


                    //净重=毛-皮-附加重量
                    netDb.net_weight = netDb.gross_weight - netDb.tare_weight - PbCache.addWgt;
                    netDb.net_mode = "1"; //1：正常结净；2：匹配结净
                    netDb.value_flag = "0"; //0=有效，1=作废
                    netDb.upload_flag = "0"; //0=待审核；1：待上传；2=已上传

                    ResultModel<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb);
                    if (rmI.state == "200")
                    {
                        rt.result = true;
                        rt.resultInfo = "计量完成";
                        PbCache.msgInfo = "计量完成";
                        PbCache.actualFirstNo = rmI.data;
                    }
                    else
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = "服务端执行失败，计量失败：" + rmI.msgInfo;
                    }
                }
                else //没有期限皮，则直接存一次数据
                {
                    ResultModel<string> rmI = mwcfs.doAddInnerWf(mwaf);
                    if (rmI.state == "200")
                    {
                        rt.result = true;
                        rt.resultInfo = "计量完成";
                        PbCache.msgInfo = "计量完成";
                        PbCache.actualFirstNo = rmI.data;
                    }
                    else
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = "服务端执行失败，计量失败：" + rmI.msgInfo;
                    }
                }

            }
            else if (rmX.data != null && rmX.data.Count > 0)
            {
                //isHaveHisTare 存在历史皮 存在一次计量数据

                //一次重量减去当前重量需大于1t
                if (Math.Abs(rmX.data[0].meter_weight - PbCache.lockWgt) <= AppConfigCache.differenceWgt)
                {
                    rt.result = false;
                    rt.isError = true;
                    PbCache.msgInfo = "当前车辆不允许重复计量";
                    return rt;
                }

                //当前车辆时间间隔过近
                validTimeInterval valid = new validTimeInterval();
                valid.ValidMethod(new PreTrackScale { meter_type_no = "001006003" }, "3", rmX.data[0]);
                if (!PbCache.monitorResult.valid_time_interval)
                {
                    rt.result = false;
                    rt.isError = true;
                    return rt;
                }

                //当前数据是皮重
                if (PbCache.monitorResult.valid_wgt)
                {
                    if (rmX.data[0].weight_type == "0") //存在毛重，那么验证当前重量是否符合皮重，符合则进行洁净操作
                    {
                        //进行洁净
                        MeterWorkCarActualFirst mwaf = new MeterWorkCarActualFirst();

                        //相同名称的均赋值一次
                        EntityBase<InnerMeterInfo> entityBase = new EntityBase<InnerMeterInfo>();
                        mwaf = entityBase.format<MeterWorkCarActualFirst>(scale, mwaf);

                        mwaf.actual_first_no = DateTime.Now.ToString("yyyyMMdd");//10
                        mwaf.meter_weight = PbCache.lockWgt;//计量重量
                        mwaf.add_weight = 0;//附加重量
                        mwaf.weight_type = "1"; //0=毛重；1=常规皮重；2=期限皮重；3=历史皮重；4=标识皮重；5=自重皮重
                        mwaf.create_man_no = "";
                        mwaf.create_man_name = "";
                        mwaf.create_time = DateTime.Now;
                        mwaf.base_spot_no = PbCache.sportInfo.base_spot_no;//计量点编号
                        mwaf.base_spot_name = PbCache.sportInfo.base_spot_name;
                        mwaf.memo = "";
                        mwaf.value_flag = "0"; //0有效
                        mwaf.update_man_name = "";
                        mwaf.update_man_no = "";
                        mwaf.update_time = null;
                        mwaf.check_flag = "0"; //0未审核
                        mwaf.meter_mode = "2"; //智能计量

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

                        //netDb.actual_first1_no = mb.actual_first_no; //皮重计量作业编号
                        netDb.tare_time = mb.create_time;
                        netDb.tare_weight = mb.meter_weight;
                        netDb.base_spot2_no = mb.base_spot_no;
                        netDb.base_spot2_name = mb.base_spot_name;
                        netDb.tare_mode = "";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                        netDb.tare_man_no = "";
                        netDb.tare_man_name = "";
                        netDb.tare_class = "";
                        netDb.tare_group = "";

                        netDb.gross_time = DateTime.Now;
                        netDb.gross_weight = PbCache.lockWgt;
                        netDb.base_spot1_no = PbCache.sportInfo.base_spot_no;
                        netDb.base_spot1_name = PbCache.sportInfo.base_spot_name;
                        netDb.gross_mode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配


                        //净重=毛-皮-附加重量
                        netDb.net_weight = netDb.gross_weight - netDb.tare_weight - PbCache.addWgt;
                        netDb.net_mode = "1"; //1：正常结净；2：匹配结净
                        netDb.value_flag = "0"; //0=有效，1=作废
                        netDb.upload_flag = "0"; //0=待审核；1：待上传；2=已上传

                        ResultModel<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb);
                        if (rmI.state == "200")
                        {
                            rt.result = true;
                            rt.resultInfo = "计量完成";
                            PbCache.msgInfo = "计量完成";
                            PbCache.actualFirstNo = rmI.data;
                        }
                        else
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = "服务端执行失败，计量失败：" + rmI.msgInfo;
                        }

                    }
                    else //皮皮不能结合，或者之前不满足这边的业务进入到了先皮后毛流程
                    {
                        rt.result = false;
                        rt.isError = false;
                        return rt;
                    }
                }
                else //当前数据不是皮重
                {
                    //存在历史皮重,，且当前数据不是皮重
                    if (isHaveHisTare)
                    {
                        rt.result = false;
                        rt.isError = false;
                        return rt;
                    }
                    else 
                    { 
                        //不存历史皮，那么两个数据需比较大小，大的为毛，小的为皮，然后将数据洁净并存到一次表跟历史表中
                    }
                }

                    //存在历史皮重 
                if (isHaveHisTare)
                {
                    
                    if (PbCache.monitorResult.valid_wgt)
                    {
                        rt.result = false;
                        rt.isError = false;
                        return rt;
                    }


                    if (rmX.data[0].weight_type == "0") //存在毛重，那么验证当前重量是否符合皮重，符合则进行洁净操作
                    {
                        if (scale.matter_no != rmX.data[0].matter_no)
                        {
                            rt.result = false;
                            rt.isError = true;
                            PbCache.msgInfo = string.Format("已存在一条物料为[0]毛重信息", rmX.data[0].matter_name);
                            return rt;
                        }
                        else //认为当前是皮重计量，进行毛皮洁净
                        {
                            MeterWorkCarActualFirst mwaf = new MeterWorkCarActualFirst();

                            //相同名称的均赋值一次
                            EntityBase<InnerMeterInfo> entityBase = new EntityBase<InnerMeterInfo>();
                            mwaf = entityBase.format<MeterWorkCarActualFirst>(scale, mwaf);

                            mwaf.actual_first_no = DateTime.Now.ToString("yyyyMMdd");//10
                            mwaf.meter_weight = PbCache.lockWgt;//计量重量
                            mwaf.add_weight = 0;//附加重量
                            mwaf.weight_type = "0"; //0=毛重；1=常规皮重；2=期限皮重；3=历史皮重；4=标识皮重；5=自重皮重
                            mwaf.create_man_no = "";
                            mwaf.create_man_name = "";
                            mwaf.create_time = DateTime.Now;
                            mwaf.base_spot_no = PbCache.sportInfo.base_spot_no;//计量点编号
                            mwaf.base_spot_name = PbCache.sportInfo.base_spot_name;
                            mwaf.memo = "";
                            mwaf.value_flag = "0"; //0有效
                            mwaf.update_man_name = "";
                            mwaf.update_man_no = "";
                            mwaf.update_time = null;
                            mwaf.check_flag = "0"; //0未审核
                            mwaf.meter_mode = "2"; //智能计量


                            MeterWorkCarActual netDb = new MeterWorkCarActual();
                            EntityBase<InnerMeterInfo> entityActual = new EntityBase<InnerMeterInfo>();
                            netDb = entityBase.format<MeterWorkCarActual>(scale, netDb);

                            //netDb.actual_first1_no = mb.actual_first_no; //皮重计量作业编号
                            netDb.tare_time = rmX.data[0].create_time;
                            netDb.tare_weight = rmX.data[0].meter_weight;
                            netDb.base_spot2_no = rmX.data[0].base_spot_no;
                            netDb.base_spot2_name = rmX.data[0].base_spot_name;
                            netDb.tare_mode = "";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                            netDb.tare_man_no = "";
                            netDb.tare_man_name = "";
                            netDb.tare_class = "";
                            netDb.tare_group = "";

                            netDb.gross_time = DateTime.Now;
                            netDb.gross_weight = PbCache.lockWgt;
                            netDb.base_spot1_no = PbCache.sportInfo.base_spot_no;
                            netDb.base_spot1_name = PbCache.sportInfo.base_spot_name;
                            netDb.gross_mode = "2";//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配


                            //净重=毛-皮-附加重量
                            netDb.net_weight = netDb.gross_weight - netDb.tare_weight - PbCache.addWgt;
                            netDb.net_mode = "1"; //1：正常结净；2：匹配结净
                            netDb.value_flag = "0"; //0=有效，1=作废
                            netDb.upload_flag = "0"; //0=待审核；1：待上传；2=已上传

                            ResultModel<string> rmI = mwcfs.doAddNetInnerWf(mwaf, netDb);
                            if (rmI.state == "200")
                            {
                                rt.result = true;
                                rt.resultInfo = "计量完成";
                                PbCache.msgInfo = "计量完成";
                                PbCache.actualFirstNo = rmI.data;
                            }
                            else
                            {
                                rt.result = false;
                                rt.isError = true;
                                rt.resultInfo = "服务端执行失败，计量失败：" + rmI.msgInfo;
                            }
                        }
                    }
                    else //那么第一次存的皮，此时
                    {
                        //存在一次计量，则进行洁净操作
                        //验证两次计量时间间隔
                        validTimeInterval vti = new validTimeInterval();
                        //计量类型为内倒计量,一次与二次计量间隔若小于配置值
                        vti.ValidMethod(new PreTrackScale { meter_type_no = "001006003" }, "3", rmX.data[0]);
                        if (!PbCache.monitorResult.valid_time_interval)
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = PbCache.msgInfo;
                            return rt;
                        }
                        else
                        {
                            //进行洁净操作
                        }
                    }
                }
                else //不存在历史皮重 
                { 
                    
                }

                
            }

            //*/

            return rt;
        }
    }
}
