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

namespace MeterConditionLibrary
{
    /// <summary>
    /// 验证预报超差，如果预报里面有毛重则验证毛重，有净重则验证净重
    /// 若都没得则提示
    /// </summary>
    public class validPredictionDiff
    {
        /// <summary>
        /// 验证预报超差，如果预报里面有毛重则验证毛重，有净重则验证净重
        /// </summary>
        /// <param name="scale">预报信息</param>
        /// <param name="sMeterType">计量类型，1过毛重,2过皮重,3结净</param>
        /// <param name="fristMeter">一次计量信息，若为结净状态才有值</param>
        public void ValidMethod(PreTrackScale scale, string sMeterType, MeterWorkCarActualFirst fristMeter)
        {
            //未锁定的情况下
            //if (!PbCache.isLockFrm)
            {
                PbCache.ResultMessage = "";
                if (PbCache.monitor.validPredictionDiff == "1" && !PbCache.isNotValid)
                {
                    if (scale.shipmentGrossWeight != null || scale.shipmentNetWeight != null)
                    {
                        if (sMeterType == "1") //过毛
                        {
                            if (scale.shipmentGrossWeight != null && scale.shipmentGrossWeight > 0)
                            {
                                ValidMethods(scale, "1", fristMeter);
                            }
                            else
                            {
                                PbCache.monitorResult.valid_prediction_diff = true;
                            }
                        }
                        else if (sMeterType == "3") //结净
                        {
                            if (scale.shipmentNetWeight != null && scale.shipmentNetWeight > 0)
                            {
                                ValidMethods(scale, "2", fristMeter);
                            }
                            else
                            {
                                PbCache.monitorResult.valid_prediction_diff = true;
                            }
                        }
                        else
                        {
                            PbCache.monitorResult.valid_prediction_diff = true;
                        }
                    }
                    else
                    {
                        PbCache.monitorResult.valid_prediction_diff = true;
                        //PbCache.ResultMessage = "预报中未配置发运重量";
                    }
                }
                else
                {
                    PbCache.monitorResult.valid_prediction_diff = true;
                }
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="scale">预报信息</param>
        /// <param name="sType">1则取预报毛重  2则取预报净重</param>
        /// <param name="fristMeter"></param>
        private void ValidMethods(PreTrackScale scale, string sType, MeterWorkCarActualFirst fristMeter)
        {
            //根据物料编号+计量类型编号
            string sql = @"select t1.diff_type_no diffTypeNo,
                                       t1.diff_type_name diffTypeName,
                                       t1.meter_type_no meterTypeNo,
                                       t1.meter_type_name meterTypeName,
                                       t1.rate_value rateValue,
                                       t1.fixed_weight_value fixedWeightValue,
                                       t2.meter_nature_no meterNatureNo,
                                       t2.meter_nature_name meterNatureName
                                  from meter_base_prediction_diff t1, meter_base_diff_relation t2
                                 WHERE t2.matter_no = '" + scale.matterNo + @"'
                                   AND t1.meter_type_no = '" + scale.meterTypeNo + @"'
                                   and t1.diff_no = t2.diff_no
                                   and t1.value_flag = '0'";
            PbModelDbService<List<MeterBasePredictionDiff>> pb = new PbModelDbService<List<MeterBasePredictionDiff>>();
            RESTfulResult<List<MeterBasePredictionDiff>> rm = pb.executeSqlDataWf(sql);
            if (rm.Succeed)
            {
                if (rm.Data != null && rm.Data.Count > 0)
                {
                    List<MeterBasePredictionDiff> dt = rm.Data;

                    //mblc[0].limit_class_no 001014002固定重量   001014001按比例
                    if (dt[0].diffTypeNo == "001014002") //按固定重量
                    {
                        double maxWgt = 0, minWgt = 0, wgt = 0;
                        if (sType == "1")  //PbCache.lockWgt在预报毛重+-固定值
                        {
                            maxWgt = scale.shipmentGrossWeight.Value + (dt[0].fixedWeightValue == null ? 0 : dt[0].fixedWeightValue.Value);
                            minWgt = scale.shipmentGrossWeight.Value - (dt[0].fixedWeightValue == null ? 0 : dt[0].fixedWeightValue.Value);
                            wgt = PbCache.lockWgt;//计量重量
                        }
                        else //净重
                        {
                            maxWgt = scale.shipmentNetWeight.Value + (dt[0].fixedWeightValue == null ? 0 : dt[0].fixedWeightValue.Value);
                            minWgt = scale.shipmentNetWeight.Value - (dt[0].fixedWeightValue == null ? 0 : dt[0].fixedWeightValue.Value);
                            wgt = Math.Abs(PbCache.lockWgt - fristMeter.meterWeight.Value - (fristMeter.addWeight == null ? 0 : fristMeter.addWeight.Value));//当前重量-上次重量-附加重量
                        }

                        if (maxWgt >= wgt && wgt >= minWgt)
                        {
                            PbCache.monitorResult.valid_prediction_diff = true;
                        }
                        else
                        {   //2021年5月10日界面显示单位，调整为T
                            PbCache.monitorResult.valid_prediction_diff = false;
                            if (sType == "1")
                            {
                                PbCache.ResultMessage = "车辆重量[" + Math.Round(PbCache.lockWgt / 1000, 2) + "T]" +
                                  "与预报重量[" + Math.Round(scale.shipmentGrossWeight.Value / 1000, 2) + "T]" +
                                   "的差值超出偏差范围,偏差值[" + Math.Round((double)dt[0].fixedWeightValue.Value / 1000, 2) + "T]";
                            }
                            else
                            {
                                PbCache.ResultMessage = "车辆净重[" + Math.Round(PbCache.lockWgt / 1000 - fristMeter.meterWeight.Value / 1000 - (fristMeter.addWeight == null ? 0 : fristMeter.addWeight.Value / 1000), 2) + " T]" +
                                    "与预报净重[" + Math.Round(scale.shipmentNetWeight.Value / 1000, 2) + " T]" +
                                    "的差值超出偏差范围,偏差值[" + Math.Round((double)dt[0].fixedWeightValue.Value / 1000, 2) + "T]";
                            }
                        }
                    }
                    else if (dt[0].diffTypeNo == "001014001") //按比例10     90  100  110
                    {
                        double maxWgt = 0, minWgt = 0, wgt = 0;
                        if (sType == "1")  //PbCache.lockWgt在预报毛重+-固定值
                        {
                            maxWgt = scale.shipmentGrossWeight.Value + scale.shipmentGrossWeight.Value * (dt[0].rateValue == null ? 1 : dt[0].rateValue.Value / 100);
                            minWgt = scale.shipmentGrossWeight.Value - scale.shipmentGrossWeight.Value * (dt[0].rateValue == null ? 1 : dt[0].rateValue.Value / 100);
                            wgt = PbCache.lockWgt;//计量重量
                        }
                        else //净重+-净重*比例
                        {
                            maxWgt = scale.shipmentNetWeight.Value + scale.shipmentNetWeight.Value * (dt[0].rateValue == null ? 1 : dt[0].rateValue.Value / 100);
                            minWgt = scale.shipmentNetWeight.Value - scale.shipmentNetWeight.Value * (dt[0].rateValue == null ? 1 : dt[0].rateValue.Value / 100);
                            wgt = Math.Abs(PbCache.lockWgt - fristMeter.meterWeight.Value - (fristMeter.addWeight == null ? 0 : fristMeter.addWeight.Value));//当前重量-上次重量-附加重量
                        }

                        if (maxWgt >= wgt && wgt >= minWgt)
                        {
                            PbCache.monitorResult.valid_prediction_diff = true;
                        }
                        else
                        {
                            PbCache.monitorResult.valid_prediction_diff = false;
                            // PbCache.ResultMessage = "车辆重量与预报重量的差值超出偏差范围,偏差比例["+ dt[0].rateValue.Value + "%]";
                            if (sType == "1")
                                PbCache.ResultMessage = "车辆重量[" + Math.Round(PbCache.lockWgt / 1000, 2) + " T]" +
                                    "与预报重量[" + Math.Round(scale.shipmentGrossWeight.Value / 1000, 2) + " T]" +
                                    "的差值超出偏差范围,偏差比例[" + dt[0].fixedWeightValue.Value + "%]";
                            else
                                PbCache.ResultMessage = "车辆净重[" + Math.Round(PbCache.lockWgt / 1000 - fristMeter.meterWeight.Value / 1000 - (fristMeter.addWeight == null ? 0 : fristMeter.addWeight.Value / 1000), 2) + " T]" +
                                    "与预报净重[" + Math.Round(scale.shipmentNetWeight.Value / 1000, 2) + " T]" +
                                    "的差值超出偏差范围,偏差比例[" + dt[0].fixedWeightValue.Value + "%]";
                        }
                    }
                    else
                    {
                        PbCache.monitorResult.valid_prediction_diff = true;
                        PbCache.ResultMessage = "";
                        //PbCache.ResultMessage = "没有超差方式[" + dt.Rows[0]["DIFF_TYPE_NAME"].ToString() + "]的逻辑";
                    }
                }
                else
                {
                    PbCache.monitorResult.valid_prediction_diff = true;
                    //PbCache.ResultMessage = "未找到物料对应的预报超差值";
                }
            }
        }
    }
}