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

namespace MeterConditionLibrary
{
    /// <summary>
    /// 验证危化品超重,是否可运危化品
    /// 这个类应该是在计量验证重量及
    /// 验证流程后，从计量场景库的类中调用过来的
    /// </summary>
    public class validExceedWgt
    {
        private Log lg = Log.GetInstance();

        /// <summary>
        /// 验证物料是否是危化品（是否在物资限载配配置表：meter_base_limit_chemical中配置数据）
        /// </summary>
        /// <param name="scale">预报信息</param>
        /// <param name="sMeterType">计量类型，1过毛重,2过皮重,3结净</param>
        /// <param name="fristMeter">一次计量信息，若为结净状态才有值</param>
        public void validMatMethod(PreTrackScale scale, string sMeterType, MeterWorkCarActualFirst fristMeter)
        {
            //未锁定的情况下
            //if (!PbCache.isLockFrm)
            {
                //验证危化品
                if (PbCache.monitor.validExceedWgt == "1" && !PbCache.isNotValid)
                {
                    if (PbCache.limit != null && PbCache.limit.Count > 0)
                    {
                        List<MeterBaseLimitChemical> mblc = PbCache.limit.Where(s => s.matterNo == scale.matterNo).ToList();
                        //该物料是危化品
                        if (mblc.Count > 0)
                        {
                            PbCache.monitorResult.valid_exceed_mat = true;
                            validTransMethod(scale, sMeterType, fristMeter);
                        }
                        else
                        {
                            PbCache.monitorResult.valid_exceed_mat = false;
                            PbCache.monitorResult.valid_exceed_train = false;
                            PbCache.monitorResult.valid_exceed_wgt = true;
                        }
                    }
                    else
                    {
                        PbCache.monitorResult.valid_exceed_mat = false;
                        PbCache.monitorResult.valid_exceed_train = false;
                        PbCache.monitorResult.valid_exceed_wgt = true;
                    }
                }
                else
                {
                    PbCache.monitorResult.valid_exceed_mat = false;
                    PbCache.monitorResult.valid_exceed_train = false;
                    PbCache.monitorResult.valid_exceed_wgt = true;
                }
            }
        }

        /// <summary>
        /// 验证该车是否能拖危化品
        /// 这个需要在得到预报的情况下开始验证，且要比所有其它的验证要靠前一点，且只验证一次
        /// </summary>
        /// <param name="scale">预报信息</param>
        /// <param name="sMeterType">计量类型，1过毛重,2过皮重,3结净</param>
        private void validTransMethod(PreTrackScale scale, string sMeterType, MeterWorkCarActualFirst fristMeter)
        {
            MeterWorkCarCheckService service = new MeterWorkCarCheckService();
            MeterWorkCarCheck oci = new MeterWorkCarCheck();
            oci.predictionNo = scale.predictionNo;
            RESTfulResult<List<MeterWorkCarCheck>> rlm = service.doQueryWf(oci);
            if (rlm.Succeed)
            {
                if (rlm.Data != null && rlm.Data.Count > 0)
                {
                    PbCache.monitorResult.valid_exceed_train = true;
                    //验证危化品是否超重
                    validWgtMethod(scale, rlm.Data[0], sMeterType, fristMeter);
                }
                else
                {
                    PbCache.monitorResult.valid_exceed_train = false;
                    PbCache.monitorResult.valid_exceed_wgt = false;
                    PbCache.ResultMessage = "未找到预报对应的物资限载配置信息";
                }
            }
            else
            {
                PbCache.monitorResult.valid_exceed_train = false;
                PbCache.monitorResult.valid_exceed_wgt = false;
                PbCache.ResultMessage = rlm.ResultMessage;
            }
        }

        /// <summary>
        /// 危化品是否超重
        /// </summary>
        /// <param name="scale">预报信息</param>
        /// <param name="sMeterType">计量类型，1过毛重,2过皮重,3结净</param>
        private void validWgtMethod(PreTrackScale scale, MeterWorkCarCheck data, string sMeterType, MeterWorkCarActualFirst fristMeter)
        {
            //配置了危化品验证信息
            if (PbCache.limit != null && PbCache.limit.Count > 0)
            {
                List<MeterBaseLimitChemical> mblc = PbCache.limit.Where(s => s.matterNo == scale.matterNo).ToList();
                if (mblc != null)
                {
                    if (mblc.Count == 1)
                    {
                        //******** eason 2020-10-10 001015002为毛重计量类型   001015001净重计量类型
                        if (sMeterType == "1" && mblc[0].limitTypeNo == "001015002")
                        {
                            //mblc[0].limit_class_no 001016001固定重量   001016002跟车重量
                            if (mblc[0].limitClassNo == "001016001") //固定重量
                            {
                                if (mblc[0].limitWeight > PbCache.lockWgt)
                                {
                                    PbCache.monitorResult.valid_exceed_wgt = true;
                                }
                                else
                                {
                                    PbCache.monitorResult.valid_exceed_wgt = false;
                                    PbCache.ResultMessage = "车辆重量超过物资限载重量[" + mblc[0].limitWeight / 1000 + "T]";
                                }
                            }
                            else if (mblc[0].limitClassNo == "001016002") //跟车重量
                            {
                                if (data.limitWeight > PbCache.lockWgt)
                                {
                                    PbCache.monitorResult.valid_exceed_wgt = true;
                                }
                                else
                                {
                                    PbCache.monitorResult.valid_exceed_wgt = false;
                                    PbCache.ResultMessage = "车辆重量超过跟车重量[" + mblc[0].limitWeight / 1000 + "T]";
                                }
                            }
                            else
                            {
                                PbCache.monitorResult.valid_exceed_wgt = false;
                                PbCache.ResultMessage = "没有限载种类的逻辑请远程求助";
                                lg.WriteLog(3, "没有限载种类[" + mblc[0].limitClassName + "]的逻辑");
                            }
                        }
                        //出净重时，毛重限载
                        else if (sMeterType == "3" && mblc[0].limitTypeNo == "001015002")
                        {
                            //mblc[0].limit_class_no 001016001固定重量   001016002跟车重量
                            if (mblc[0].limitClassNo == "001016001") //固定重量
                            {
                                if (mblc[0].limitWeight > PbCache.lockWgt)
                                {
                                    PbCache.monitorResult.valid_exceed_wgt = true;
                                }
                                else
                                {
                                    PbCache.monitorResult.valid_exceed_wgt = false;
                                    PbCache.ResultMessage = "车辆重量超过物资限载重量[" + mblc[0].limitWeight / 1000 + "T]";
                                }
                            }
                            else if (mblc[0].limitClassNo == "001016002") //跟车重量
                            {
                                if (data.limitWeight > PbCache.lockWgt)
                                {
                                    PbCache.monitorResult.valid_exceed_wgt = true;
                                }
                                else
                                {
                                    PbCache.monitorResult.valid_exceed_wgt = false;
                                    PbCache.ResultMessage = "车辆重量超过跟车重量[" + mblc[0].limitWeight / 1000 + "T]";
                                }
                            }
                            else
                            {
                                PbCache.monitorResult.valid_exceed_wgt = false;
                                PbCache.ResultMessage = "没有限载种类的逻辑请远程求助";
                                lg.WriteLog(3, "没有限载种类[" + mblc[0].limitClassName + "]的逻辑");
                            }
                        }
                        else if (sMeterType == "3" && mblc[0].limitTypeNo == "001015001")
                        {
                            //mblc[0].limit_class_no 001016001固定重量   001016002跟车重量
                            if (mblc[0].limitClassNo == "001016001")
                            {
                                if (mblc[0].limitWeight > Math.Abs(fristMeter.meterWeight.Value - PbCache.lockWgt - (fristMeter.addWeight == null ? 0 : fristMeter.addWeight.Value))) //毛重-皮重的值- 附加重量
                                {
                                    PbCache.monitorResult.valid_exceed_wgt = true;
                                }
                                else
                                {
                                    PbCache.monitorResult.valid_exceed_wgt = false;
                                    PbCache.ResultMessage = "车辆重量超过物资限载重量[" + mblc[0].limitWeight / 1000 + "T]";
                                }
                            }
                            else if (mblc[0].limitClassNo == "001016002") //跟车重量
                            {
                                if (data.limitWeight > Math.Abs(fristMeter.meterWeight.Value - PbCache.lockWgt - (fristMeter.addWeight == null ? 0 : fristMeter.addWeight.Value))) //毛重-皮重的值- 附加重量
                                {
                                    PbCache.monitorResult.valid_exceed_wgt = true;
                                }
                                else
                                {
                                    PbCache.monitorResult.valid_exceed_wgt = false;
                                    PbCache.ResultMessage = "车辆重量超过跟车重量[" + mblc[0].limitWeight / 1000 + "T]";
                                }
                            }
                            else
                            {
                                PbCache.monitorResult.valid_exceed_wgt = false;
                                PbCache.ResultMessage = "没有限载种类的逻辑请远程求助";
                                lg.WriteLog(3, "没有限载种类[" + mblc[0].limitClassName + "]的逻辑");
                            }
                        }
                        else
                        {
                            PbCache.monitorResult.valid_exceed_wgt = false;
                            lg.WriteLog(3, string.Format("未找到物料为[{0}],限制类型为[{0}],的物资限载配置信息", mblc[0].matterName, (sMeterType == "1" ? "毛重" : "净重")));
                            PbCache.ResultMessage = string.Format("未找到物料为[{0}],限制类型为[{0}],的物资限载配置信息", mblc[0].matterName, (sMeterType == "1" ? "毛重" : "净重"));
                        }
                    }
                    else
                    {
                        PbCache.monitorResult.valid_exceed_wgt = false;
                        PbCache.ResultMessage = "该物料配置了多条物资限载配置信息";
                    }
                }
                else
                {
                    PbCache.monitorResult.valid_exceed_wgt = false;
                    PbCache.ResultMessage = "未配置物资限载配置信息";
                }
            }
            else
            {
                PbCache.monitorResult.valid_exceed_wgt = false;
                PbCache.ResultMessage = "未配置物资限载配置信息";
            }
        }
    }
}