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

namespace MeterConditionLibrary
{
    /// <summary>
    /// 联达的重量偏差，对应基础信息001050001向
    /// 若都没得则提示
    /// </summary>
    public class validLianDaWgtDiff
    {
        /// <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 (!string.IsNullOrEmpty(scale.meterPierNo)  && "001048009".Equals(scale.meterPierNo)
                        && !string.IsNullOrEmpty(scale.receivingUintName) && "江西联达联达料场".Equals(scale.receivingUintName) && scale.shipmentGrossWeight != null )
                    {
                        if (sMeterType == "1") //过毛
                        {
                            if (scale.shipmentGrossWeight != null && scale.shipmentGrossWeight > 0)
                            {
                                ValidMethods(scale, "1", fristMeter);
                            }
                            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)
        {
            ComBaseInfoService comBaseInfoService = new ComBaseInfoService(); //基础数服务
            RESTfulResult<List<ComBaseInfo>> rm = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001050" });
            if (rm.Succeed)
            {
                if (rm.Data != null && rm.Data.Count > 0)
                {
                    List<ComBaseInfo> dt = rm.Data;

                    double maxWgt = 0, minWgt = 0, wgt = 0;
                    if (sType == "1")  //PbCache.lockWgt在预报毛重+-固定值
                    {
                        maxWgt = scale.shipmentGrossWeight.Value + (dt[0].baseName == null ? 0 : Convert.ToDouble(dt[0].baseName));
                        minWgt = scale.shipmentGrossWeight.Value - (dt[0].baseName == null ? 0 : Convert.ToDouble(dt[0].baseName));
                        wgt = PbCache.lockWgt;//计量重量
                    }
                    else
                    {
                        maxWgt = scale.shipmentNetWeight.Value + (dt[0].baseName == null ? 0 : Convert.ToDouble(dt[0].baseName));
                        minWgt = scale.shipmentNetWeight.Value - (dt[0].baseName == null ? 0 : Convert.ToDouble(dt[0].baseName));
                        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) + "]" +
                              "与预报重量[" + Math.Round(scale.shipmentGrossWeight.Value / 1000, 2) + "]" +
                               "的差值超出偏差范围,偏差值[" + Math.Round(Convert.ToDouble(dt[0].baseName) / 1000, 2) + "]";
                        }
                    }
                }
                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 = "未找到物料对应的预报超差值";
            }
         }
     }
 }