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

namespace MeterSceneLibrary
{
    public class InnerFirstTareSecondlyGross
    {
        public rtInfo MeterMethod(InnerMeterInfo scale, ResultModel<List<MeterWorkPreLink>> rmDb)
        {
            DbHelper db = new DbHelper();

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

            /*
            ResultModel<List<MeterWorkPreLink>> rm = rmDb;

            //如果配置了则走下面的流程
            //如果存在一次计量未洁净数据，则认为是洁净计量，否则认为一次计量
            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;
            }


            if (rm.data[0].link_code == "001021003")  //计量环节为 一次计量   计量场景为先皮后毛
            {
                if (rmX.data != null && rmX.data.Count > 0)
                {
                    rt.resultInfo = "已存在一次计量数据！";
                    rt.result = false;
                    rt.isError = true;
                }
                else
                {
                    //验证是否是内倒车辆
                    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;
                    }

                    //验证当前皮重是否符合要求
                    validWgt vw = new validWgt();
                    vw.ValidMethod(new PreTrackScale { car_no = scale.carno });
                    if (!PbCache.monitorResult.valid_wgt)
                    {
                        rt.result = false;
                        rt.isError = false;
                        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 = 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"; //智能计量


                    MeterWorkPreLink mwpl = new MeterWorkPreLink();
                    mwpl.pre_link_no = rm.data[0].pre_link_no;
                    mwpl.pre_process_no = rm.data[0].pre_process_no;

                    ResultModel<string> rmI = mwcfs.doAddInnerWf(mwaf, mwpl);
                    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 (rm.data[0].link_code == "001021005")  //计量环节为 洁净  计量场景为先皮后毛
            {
                if (rmX.data == null || rmX.data.Count == 0)
                {
                    rt.resultInfo = "未找到一次计量数据！";
                    rt.result = false;
                    rt.isError = false;
                }
                else if (rmX.data.Count == 1)
                {
                    //则走其他的验证，比如与历史皮重验证，与其他的信息验证，若验证都通过则保存一次计量信息
                    MeterWorkCarActualFirst first = rmX.data[0];
                    if (first.weight_type == "0") //一次为毛重，那么毛毛不能结合
                    {
                        rt.result = false;
                        rt.isError = false;
                    }
                    else //一次为皮重，那么当前是毛重  走先皮后毛流程
                    {
                        //验证两次计量时间间隔
                        validTimeInterval vti = new validTimeInterval();
                        vti.ValidMethod(new PreTrackScale { meter_type_no = scale.meter_type_no }, "3", first);
                        if (!PbCache.monitorResult.valid_time_interval)
                        {
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = PbCache.msgInfo;
                            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 secondDb = new MeterWorkCarActualFirst();

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

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


                        //环节信息
                        MeterWorkPreLink link = new MeterWorkPreLink();
                        link.pre_link_no = rm.data[0].pre_link_no;
                        link.pre_process_no = rm.data[0].pre_process_no;

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

                        netDb.actual_first1_no = first.actual_first_no; //皮重计量作业编号
                        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.actual_first2_no = ""; //皮重计量作业编号
                        netDb.tare_weight = first.meter_weight; //皮重
                        netDb.tare_time = first.create_time;
                        netDb.base_spot2_no = first.base_spot_no; //毛重计量点编号
                        netDb.base_spot2_name = first.base_spot_name; //毛重计量点
                        netDb.tare_mode = first.meter_mode;//1：远程计量；2：智能计量；3：手工录入；4：放行智能计量；5：滞后匹配
                        netDb.tare_man_no = first.create_man_no;
                        netDb.tare_man_name = first.create_man_name;
                        netDb.tare_class = first.meter_class;
                        netDb.tare_group = first.meter_group;
                        

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

                        ResultModel<string> rmI = mwcfs.doAddNetInnerWf(secondDb, netDb, link);
                        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.resultInfo = "找到多条一次计量数据！";
                    rt.result = false;
                    rt.isError = true;
                }
            }

            //*/
            return rt;
        }
    }
}
