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

namespace MeterSceneLibrary
{
    /// <summary>
    /// 先皮后毛 一车多运互锁式
    /// </summary>
    public class FirstTaresSecondlyGrossesLock
    {
        private MeterBaseTermTareDataService termTareDate = new MeterBaseTermTareDataService();//期限皮操作

        public rtInfo MeterMethod(PreTrackScale scale, CoreAppUser appUser, RESTfulResult<List<MeterWorkPreLink>> rmDb)
        {
            rtInfo rt = new rtInfo();
            rt.isError = false;

            //0：批次预报；1：单次预报；2：联运预报；3：分检预报
            if (scale.predictionType != "2")
            {
                rt.result = false;
                rt.isError = false;
                return rt;
            }

            RESTfulResult<List<MeterWorkPreLink>> rm = rmDb;


            //如果配置了则走下面的流程
            //如果存在一次计量未结净数据，则认为是结净计量，否则认为一次计量
            MeterWorkCarActualFirstService mwcfs = new MeterWorkCarActualFirstService();

            RESTfulResult<List<MeterWorkCarActualFirst>> rmX = mwcfs.doQueryWf(new MeterWorkCarActualFirst { valueFlag = "1", predictionNo = scale.predictionNo, carNo = scale.carNo });
            if (!rmX.Succeed)
            {
                rt.result = false;
                rt.resultInfo = "没有计量信息点语音求助";
                PbCache.ResultMessage = "获取一次计量数据异常！";
                rt.isError = true;
            }

            if (rm.Data[0].linkCode == "001021003") //一次计量
            {
                if (rmX.Data != null && rmX.Data.Count > 0)
                {
                    PbCache.ResultMessage = "计量环节为一次计量，但存在一次计量数据！";
                    rt.resultInfo = "已经存在一次计量信息";
                    rt.result = false;
                    rt.isError = true;
                }
                else
                {
                    //验证两次计量时间间隔
                    validTimeInterval vti = new validTimeInterval();
                    vti.ValidMethod(scale, "2", null);
                    if (!PbCache.monitorResult.valid_time_interval)
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = PbCache.ResultMessage;
                        return rt;
                    }

                    //保存皮重信息***********************
                    MeterWorkCarActualFirst mwaf = new MeterWorkCarActualFirst();

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

                    mwaf.actualFirstNo = DateTime.Now.ToString("yyyyMMdd");//10
                    mwaf.meterWeight = PbCache.lockWgt;//计量重量
                                                        //mwaf.addWeight = 0;//附加重量
                    mwaf.weightType = "1"; //0=毛重；1=常规皮重；2=期限皮重；3=历史皮重；4=标识皮重；5=自重皮重
                    mwaf.createManNo = appUser.userid;
                    mwaf.createManName = appUser.username;
                    mwaf.meterGroup = appUser.userGroup;
                    mwaf.meterClass = appUser.userClass;
                    mwaf.createTime = DateTime.Now;
                    mwaf.baseSpotNo = PbCache.sportInfo.baseSpotNo;//计量点编号
                    mwaf.baseSpotName = PbCache.sportInfo.baseSpotName;
                    //mwaf.memo = "";
                    mwaf.valueFlag = "1"; //0有效
                    mwaf.updateManName = "";
                    mwaf.updateManNo = "";
                    mwaf.updateTime = null;
                    mwaf.checkFlag = "0"; //0未审核
                    mwaf.meterMode = string.IsNullOrEmpty(appUser.userid) ? "2" : "1"; //1远程计量，2智能计量
                    mwaf.addWeight = PbCache.addWgt;

                    MeterWorkPreLink mwpl = new MeterWorkPreLink();
                    mwpl.preLinkNo = rm.Data[0].preLinkNo;
                    mwpl.preProcessNo = rm.Data[0].preProcessNo;

                    //操作日志
                    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 = string.IsNullOrEmpty(appUser.userid) ? "0" : "4";
                    monitorNote.meterManNo = appUser.userid;
                    monitorNote.meterManName = appUser.username;


                    RESTfulResult<string> rmI = mwcfs.doAddWf(mwaf, mwpl, monitorNote, "1", "0");
                    if (rmI.Succeed)
                    {
                        rt.result = true;
                        rt.resultInfo = "称重完成";
                        PbCache.ResultMessage = "称重完成";
                        PbCache.actualFirstNo = rmI.Data;
                        PbCache.strQRCode = rmI.Message;
                        PbCache.isTareWeight = true;//皮重
                        RESTfulResult<string> RESTfulResult = termTareDate.abolishForCSharp(new MeterBaseTermTareData { carNo = mwaf.carNo });
                        if (RESTfulResult.Succeed)
                        {
                            if (!string.IsNullOrEmpty(RESTfulResult.Message))
                            {
                                PbCache.ResultMessage = PbCache.ResultMessage + "；请注意期限皮重已作废";
                            }
                        }
                        else
                        {
                            PbCache.ResultMessage = PbCache.ResultMessage + "；作废期限皮重异常，请点击【语音求助】联系计量员";
                        }
                    }
                    else
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = "称重数据保存失败";
                        PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                    }
                }
            }
            else if (rm.Data[0].linkCode == "001021005")  //结净
            {
                if (string.IsNullOrEmpty(scale.forwardingUnitName) || string.IsNullOrEmpty(scale.receivingUintName))
                {
                    PbCache.ResultMessage = "结净时，预报中的发货单位以及收货单位均不允许为空，请点击[语音求助]！";
                    rt.result = false;
                    rt.isError = false;
                    return rt;
                }
                if (rmX.Data == null || rmX.Data.Count == 0)
                {
                    //rt.resultInfo = "未找到一次计量数据！";
                    rt.result = false;
                    rt.isError = false;
                    PbCache.ResultMessage = "未找到一次计量数据,请先计量皮重！";
                }
                else if (rmX.Data.Count == 1) 
                {
                    if (!PbCache.isNotValid)
                    {
                        if (Math.Abs(rmX.Data[0].meterWeight.Value - PbCache.lockWgt) < AppConfigCache.differenceWgt)
                        {
                            //重复计量
                            rt.result = false;
                            rt.isError = true;
                            rt.resultInfo = "第一次计量重量和第二次计量重量接近";
                            //PbCache.ResultMessage = "第一次计量重量和第二次计量重量接近";
                            PbCache.ResultMessage = "第一次计量重量【" + rmX.Data[0].meterWeight.Value / 1000 + "T】和第二次计量重量【" + PbCache.lockWgt / 1000 + "T】接近，小于结净最小值【" + AppConfigCache.differenceWgt / 1000 + " T】,请点击[语音求助]，由司秤工过磅！";
                            return rt;
                        }
                    }

                    //一次重量大于当前重量，所以一次的时候应该是毛重
                    if (rmX.Data[0].meterWeight.Value - PbCache.lockWgt > 0)
                    {
                        //将一次数据类型修改为皮重
                        mwcfs.doUpdateWf(new MeterWorkCarActualFirst { actualFirstNo = rmX.Data[0].actualFirstNo, weightType = "0" });
                        rt.result = false;
                        rt.isError = false;
                        PbCache.ResultMessage = "请再次点击[保存]按钮";
                        return rt;
                    }

                    MeterWorkCarActualFirst first = rmX.Data[0];
                    //存在一次计量，则进行结净操作
                    //验证物料时间段
                    validMatTime vmt = new validMatTime();
                    vmt.ValidMethod(scale, "1");
                    if (!PbCache.monitorResult.valid_mat_time)
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = PbCache.ResultMessage;
                        return rt;
                    }

                    //验证两次计量时间间隔
                    validTimeInterval vti = new validTimeInterval();
                    vti.ValidMethod(scale, "3", first);
                    if (!PbCache.monitorResult.valid_time_interval)
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = PbCache.ResultMessage;
                        return rt;
                    }

                    //验证危化品验证
                    validExceedWgt vew = new validExceedWgt();
                    vew.validMatMethod(scale, "3", first);
                    if (!PbCache.monitorResult.valid_exceed_wgt)
                    {
                        rt.result = false;
                        rt.isError = true;
                        return rt;
                    }

                    //验证预报超差信息
                    validPredictionDiff vpd = new validPredictionDiff();
                    vpd.ValidMethod(scale, "1", first); //验证毛重
                    if (!PbCache.monitorResult.valid_prediction_diff)
                    {
                        rt.result = false;
                        rt.isError = false;
                        return rt;
                    }

                    //验证联达码头；江西联达联达料场，预报超差
                    validLianDaWgtDiff lianDaWgtDiff = new validLianDaWgtDiff();
                    lianDaWgtDiff.ValidMethod(scale, "1", null); //验证毛重
                    if (!PbCache.monitorResult.valid_prediction_diff)
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = PbCache.ResultMessage;
                        return rt;
                    }

                    vpd.ValidMethod(scale, "3", first); //验证净重
                    if (!PbCache.monitorResult.valid_prediction_diff)
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = PbCache.ResultMessage;
                        return rt;
                    }

                    //验证计量区域：若二次计量数据的计量区域与一次计量的计量区域不一致，则禁止自助计量；
                    validMeterArea vMeterArea = new validMeterArea();
                    vMeterArea.ValidMethod(first.baseSpotNo);
                    if (!PbCache.monitorResult.valid_meter_area)
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = PbCache.ResultMessage;
                        return rt;
                    }

                    //====所有验证均通过后，此时进行结净=====================
                    //保存毛重信息  即：二次计量信息***********************
                    MeterWorkCarActualFirst secondDb = new MeterWorkCarActualFirst();

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

                    secondDb.actualFirstNo = DateTime.Now.ToString("yyyyMMdd");//10
                    secondDb.meterWeight = PbCache.lockWgt;//计量重量
                    secondDb.addWeight = PbCache.addWgt;//附加重量
                    secondDb.weightType = "0"; //0=毛重；1=常规皮重；2=期限皮重；3=历史皮重；4=标识皮重；5=自重皮重
                    secondDb.createManNo = appUser.userid;
                    secondDb.createManName = appUser.username;
                    secondDb.meterGroup = appUser.userGroup;
                    secondDb.meterClass = appUser.userClass;
                    secondDb.createTime = DateTime.Now;
                    secondDb.baseSpotNo = PbCache.sportInfo.baseSpotNo;//计量点编号
                    secondDb.baseSpotName = PbCache.sportInfo.baseSpotName;
                    //secondDb.memo = "";
                    secondDb.valueFlag = "1"; //0有效
                    secondDb.updateManName = "";
                    secondDb.updateManNo = "";
                    secondDb.updateTime = null;
                    secondDb.checkFlag = "0"; //0未审核
                    secondDb.meterMode = string.IsNullOrEmpty(appUser.userid) ? "2" : "1"; //1远程计量，2智能计量


                    //环节信息
                    MeterWorkPreLink link = new MeterWorkPreLink();
                    link.preLinkNo = rm.Data[0].preLinkNo;
                    link.preProcessNo = rm.Data[0].preProcessNo;

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

                    //netDb.actualFirst1No = ""; //毛重计量作业编号
                    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智能计量
                    netDb.grossGroup = appUser.userGroup;
                    netDb.grossClass = appUser.userClass;
                    netDb.grossManNo = appUser.userid;
                    netDb.grossManName = appUser.username;


                    netDb.actualFirst2No = first.actualFirstNo;
                    netDb.tareWeight = first.meterWeight;
                    netDb.tareTime = first.createTime;
                    netDb.baseSpot2No = first.baseSpotNo;
                    netDb.baseSpot2Name = first.baseSpotName;
                    netDb.tareMode = first.meterMode;
                    netDb.tareClass = first.meterClass;
                    netDb.tareGroup = first.meterGroup;
                    netDb.tareManName = first.createManName;
                    netDb.tareManNo = first.createManNo;


                    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=已上传
                    netDb.netTime = DateTime.Now;

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

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

                    RESTfulResult<string> rmI = mwcfs.doAddNetWf(secondDb, netDb, link, monitorNote, "1", "0");
                    if (rmI.Succeed)
                    {
                        rt.result = true;
                        rt.resultInfo = "称重完成";
                        PbCache.ResultMessage = "称重完成";
                        PbCache.actualFirstNo = rmI.Data;
                        PbCache.strQRCode = rmI.Message;
                        PbCache.isTareWeight = false;//毛重
                        RESTfulResult<string> RESTfulResult = termTareDate.abolishForCSharp(new MeterBaseTermTareData { carNo = netDb.carNo });
                        if (RESTfulResult.Succeed)
                        {
                            if (!string.IsNullOrEmpty(RESTfulResult.Message))
                            {
                                PbCache.ResultMessage = PbCache.ResultMessage + "；请注意期限皮重已作废";
                            }
                        }
                        else
                        {
                            PbCache.ResultMessage = PbCache.ResultMessage + "；作废期限皮重异常，请点击【语音求助】联系计量员";
                        }
                    }
                    else
                    {
                        rt.result = false;
                        rt.isError = true;
                        rt.resultInfo = "称重数据保存失败";
                        PbCache.ResultMessage = "计量失败：" + rmI.ResultMessage;
                    }
                }
                else
                {
                    PbCache.ResultMessage = string.Format("当前预报[{0}]存在多条有效的一次计量数据", scale.predictionNo);
                    rt.resultInfo = "找到多条未结净数据";
                    rt.result = false;
                    rt.isError = true;
                }
            }

            return rt;
        }
    }
}
