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

namespace MeterConditionLibrary
{
    /// <summary>
    /// 验证重量 这里必须是皮重计量，只能通过流程来估算
    /// 这里是指与历史皮重比较，如果超了可通过该功能跳过
    /// </summary>
    /// <param name="scale">预报信息</param>
    public class validWgt
    {
        public void ValidMethod(PreTrackScale scale)
        {
            //未锁定的情况下
            //if (!PbCache.isLockFrm)
            {
                //验证重量
                if (PbCache.monitor.validWgt == "1")
                {
                    MeterBaseHisTareDataService mbtds = new MeterBaseHisTareDataService();
                    RESTfulResult<List<MeterBaseHisTareData>> rm = mbtds.doQueryWf(new MeterBaseHisTareData { valueFlag = "0", carNo = scale.carNo });

                    if (rm.Succeed)
                    {
                        if (rm.Data != null && rm.Data.Count > 0)
                        {
                            //比较历史皮重与当前重量验证；默认为通用
                            MeterBaseHisTareCount mbtc = PbCache.tareCount;

                            //特殊配置项处理；物资类：
                            if (scale != null && !string.IsNullOrEmpty(scale.matterNo))
                            {
                                MeterBaseHisTareCountService meterCount = new MeterBaseHisTareCountService();
                                RESTfulResult<List<MeterBaseHisTareCount>> rmMeterCount = meterCount.doQuerySpecWf(new MeterBaseHisTareCount { countTypeNo = scale.matterNo });
                                if (rmMeterCount.Succeed && rmMeterCount.Data != null && rmMeterCount.Data.Count > 0)
                                {
                                    mbtc = rmMeterCount.Data[0];
                                }
                            }

                            //计算
                            double maxWgt = 0, minWgt = 0, wgt = PbCache.lockWgt;
                            if (mbtc.countTypeNo == "001011001") //固重 当前重量>=历史皮重-固重   当前重量<=历史皮重+固重
                            {
                                long meterWeight = (PbCache.tareCount.contrastTypeNo == "001012001" ? rm.Data[0].upWeight.Value : rm.Data[0].meterWeight.Value);

                                maxWgt = meterWeight + mbtc.fixedWeightValue.Value;
                                minWgt = meterWeight - mbtc.fixedWeightValue.Value;

                                if (PbCache.lockWgt >= minWgt && PbCache.lockWgt <= maxWgt)
                                {
                                    PbCache.monitorResult.valid_wgt = true;
                                }
                                else
                                {
                                    PbCache.monitorResult.valid_wgt = false;
                                    PbCache.ResultMessage = "当前皮重【"+ PbCache.lockWgt/1000 + "T 】不在皮重配置范围，历史皮重为【" + Convert.ToDouble(meterWeight)  / 1000 + "T】,偏差最大允许重量为【" + mbtc.fixedWeightValue.Value + "kg】";
                                }
                            }
                            else if (mbtc.countTypeNo == "001011002") //比例 当前重量>=历史皮重-历史皮重*比例   当前重量<=历史皮重+历史皮重*比例
                            {
                                long meterWeight = (PbCache.tareCount.contrastTypeNo == "001012001" ? rm.Data[0].upWeight.Value : rm.Data[0].meterWeight.Value);

                                maxWgt = meterWeight + meterWeight * (mbtc.rateValue.Value / 100); //历史皮重+历史皮重*比例
                                minWgt = meterWeight - meterWeight * (mbtc.rateValue.Value / 100); //历史皮重-历史皮重*比例

                                if (PbCache.lockWgt >= minWgt && PbCache.lockWgt <= maxWgt)
                                {
                                    PbCache.monitorResult.valid_wgt = true;
                                }
                                else
                                {
                                    PbCache.monitorResult.valid_wgt = false;
                                    PbCache.ResultMessage = "当前皮重【" + PbCache.lockWgt / 1000 + "T 】不在皮重配置范围，历史皮重为【" + Convert.ToDouble(meterWeight)  / 1000 + "T】,偏差最大允许比例值为【" + mbtc.fixedWeightValue.Value + "%】";
                                }
                            }
                            else
                            {
                                PbCache.monitorResult.valid_wgt = false;
                                PbCache.ResultMessage = string.Format("未找到历史皮重计算类型{0}配置数据", mbtc.countTypeNo);
                            }

                            //当前重量>=历史皮重+历史皮重*比例   当前重量<=历史皮重+历史皮重*比例
                        }
                        else
                        {
                            PbCache.monitorResult.valid_wgt = false;
                            PbCache.ResultMessage = "未找到历史皮重数据";
                        }
                    }
                    else
                    {
                        PbCache.monitorResult.valid_wgt = false;
                        PbCache.ResultMessage = "查询异常！请点击语音求助，联系系统管理员处理！";
                    }
                }
                else
                {
                    PbCache.monitorResult.valid_wgt = true;
                }
            }
        }

        /// <summary>
        /// 非内倒的重量验证，这里验证的是历史皮重
        /// </summary>
        /// <param name="scale"></param>
        public void ValidNotInnerMethod(PreTrackScale scale)
        {
            PbCache.monitorResult.haveHisTare = false;//不存在历史皮
            List<MeterBaseHisTareData> validHis = validHisTare(scale);

            //计量中心默认 PbCache.monitorResult.valid_wgt = true;
            if (PbCache.isNotValid)
            {
                string sql = @"select t1.meter_weight meterWeight,
                                          t1.data_no dataNo,
                                          t1.create_time createTime,
                                          t1.base_spot_no baseSpotNo,
                                          t1.base_spot_name baseSpotName
                                      from Meter_Base_Term_Tare_Data t1, Meter_Base_Term_Tare_Car t2
                                     where t1.car_no = t2.car_no
                                       and t2.value_flag = '0'
                                       and t1.value_flag = '0'
                                       and t1.car_no = '" + scale.carNo + @"'
                                       and t1.end_time>sysdate";
                PbModelDbService<List<MeterBaseTermTareData>> pb = new PbModelDbService<List<MeterBaseTermTareData>>();
                RESTfulResult<List<MeterBaseTermTareData>> rmType = pb.executeSqlDataWf(sql);
                if (rmType.Succeed)
                {
                    if (rmType.Data != null && rmType.Data.Count > 0)
                    {
                        //查下当前拖的物料是否可用期限皮;验证业务类型是否允许使用期限皮重
                        //and t.meter_nature_name like '%外%' //此为物料计量属性，例如：若不是外购物料，则不允许使用期限皮重；（九江取消了）
                        string sqls = @"select t.matter_no id, t.matter_name text
                                              from meter_base_matter_info t,meter_base_metertype_conf t2
                                             where t.allowed_tare_weight = '1'
                                               and t.valid_flag = '1'
                                               and t.matter_no='" + scale.matterNo + @"'
                                               and t2.is_allow_term_tare = '1' and  t2.meter_type_no = '" + scale.meterTypeNo + @"'";

                        PbModelDbService<List<PbModelDb>> pbp = new PbModelDbService<List<PbModelDb>>();
                        RESTfulResult<List<PbModelDb>> rmT = pbp.executeSqlDataWf(sqls);
                        if (rmT.Succeed)
                        {
                            if (rmT.Data != null && rmT.Data.Count > 0)
                            {
                                // false 的时候，且有期限皮重，则默认为毛重，
                                PbCache.monitorResult.valid_wgt = false;
                            }
                            else
                            {
                                PbCache.monitorResult.valid_wgt = true;
                            }   
                        }
                        else
                        {
                            PbCache.monitorResult.valid_wgt = true;
                            PbCache.ResultMessage = "计量失败，获取期限皮可用物料异常：" + rmType.ResultMessage;
                        }
                    }
                    else
                    {
                        //没有期限皮重，则不验证重量
                        PbCache.monitorResult.valid_wgt = true;
                    }
                }
                else
                {
                    PbCache.monitorResult.valid_wgt = true;
                    PbCache.ResultMessage = "计量失败，获取期限皮数据异常：" + rmType.ResultMessage;
                }
                return;
            }
            if (validHis != null)
            {
                if (validHis.Count > 0)
                {
                    if (PbCache.monitor.validWgt == "1")
                    {
                        //比较历史皮重与当前重量验证；默认为通用
                        MeterBaseHisTareCount mbtc = PbCache.tareCount;

                        //特殊配置项处理；物资类：
                        if (scale != null && !string.IsNullOrEmpty(scale.matterNo))
                        {
                            MeterBaseHisTareCountService meterCount = new MeterBaseHisTareCountService();
                            RESTfulResult<List<MeterBaseHisTareCount>> rmMeterCount = meterCount.doQuerySpecWf(new MeterBaseHisTareCount { countTypeNo = scale.matterNo });
                            if (rmMeterCount.Succeed && rmMeterCount.Data != null && rmMeterCount.Data.Count > 0)
                            {
                                mbtc = rmMeterCount.Data[0];
                            }
                        }
                        double maxWgt = 0, minWgt = 0, wgt = PbCache.lockWgt;

                        if (mbtc.countTypeNo == "001011001") //固重 当前重量>=历史皮重-固重   当前重量<=历史皮重+固重
                        {
                            long meterWeight = (PbCache.tareCount.contrastTypeNo == "001012001" ? validHis[0].upWeight.Value : validHis[0].meterWeight.Value);

                            maxWgt = meterWeight + mbtc.fixedWeightValue.Value;
                            minWgt = meterWeight - mbtc.fixedWeightValue.Value;

                            if (PbCache.lockWgt >= minWgt && PbCache.lockWgt <= maxWgt)
                            {
                                PbCache.monitorResult.valid_wgt = true;
                            }
                            else
                            {
                                PbCache.monitorResult.valid_wgt = false;
                                PbCache.ResultMessage = "当前皮重【" + PbCache.lockWgt / 1000 + "T 】不在皮重配置范围，历史皮重为【" + Convert.ToDouble(meterWeight)  / 1000 + "T】,偏差最大允许重量为【" + mbtc.fixedWeightValue.Value + "kg】";
                            }
                        }
                        else if (mbtc.countTypeNo == "001011002") //比例 当前重量>=历史皮重-历史皮重*比例   当前重量<=历史皮重+历史皮重*比例
                        {
                            long meterWeight = (PbCache.tareCount.contrastTypeNo == "001012001" ? validHis[0].upWeight.Value : validHis[0].meterWeight.Value);

                            maxWgt = meterWeight + meterWeight * (mbtc.rateValue.Value / 100); //历史皮重+历史皮重*比例
                            minWgt = meterWeight - meterWeight * (mbtc.rateValue.Value / 100); //历史皮重-历史皮重*比例

                            if (PbCache.lockWgt >= minWgt && PbCache.lockWgt <= maxWgt)
                            {
                                PbCache.monitorResult.valid_wgt = true;
                            }
                            else
                            {
                                PbCache.monitorResult.valid_wgt = false;
                                PbCache.ResultMessage = "当前皮重【" + PbCache.lockWgt / 1000 + "T 】不在皮重配置范围，历史皮重为【" + (double)Convert.ToDouble(meterWeight)  / 1000 + "T】,偏差最大允许比例值为【" + mbtc.fixedWeightValue.Value + "%】";
                            }
                        }
                        else
                        {
                            PbCache.monitorResult.valid_wgt = false;
                            PbCache.ResultMessage = string.Format("未找到历史皮重计算类型{0}配置数据", mbtc.countTypeNo);
                        }
                    }
                }
                else
                {
                    //非内倒的在内有历史皮的时候让其正常计量
                    PbCache.monitorResult.valid_wgt = true;
                }
            }
            else
            {
                PbCache.monitorResult.valid_wgt = false;
                PbCache.ResultMessage = "查询异常！未找到历史皮重数据";
            }

            if (PbCache.monitor.validWgt == "0")
            {
                PbCache.monitorResult.valid_wgt = true;
            }
        }

        public List<MeterBaseHisTareData> validHisTare(PreTrackScale scale)
        {
            List<MeterBaseHisTareData> lmhtd = new List<MeterBaseHisTareData>();

            PbCache.monitorResult.haveHisTare = false;//不存在历史皮
            MeterBaseHisTareDataService mbtds = new MeterBaseHisTareDataService();
            RESTfulResult<List<MeterBaseHisTareData>> rm = mbtds.doQueryWf(new MeterBaseHisTareData { valueFlag = "0", carNo = scale.carNo });
            if (rm.Succeed)
            {
                if (rm.Data != null && rm.Data.Count > 0)
                {
                    PbCache.monitorResult.haveHisTare = true;//存在历史皮
                    lmhtd = rm.Data;
                }
            }
            else
            {
                lmhtd = null;
            }
            PbCache.monitorResult.hisTareData = lmhtd;
            return lmhtd;
        }
    }
}