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

namespace MeterConditionLibrary
{
    /// <summary>
    /// 验证回皮时间,必须是先毛后皮的流程才验证回皮时间
    /// </summary>
    public class validTareTime
    {
        /// <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)
            {
                if (sMeterType == "3")
                {
                    //验证回皮时间
                    if (PbCache.monitor.validTareTime == "1" && !PbCache.isNotValid)
                    {
                        MeterBaseTareService limitService = new MeterBaseTareService();
                        RESTfulResult<List<MeterBaseTare>> rmL = limitService.doQueryWf(new MeterBaseTare { matterNo = scale.matterNo, meterTypeNo = scale.meterTypeNo });
                        if (rmL.Succeed)
                        {
                            if (rmL.Data != null && rmL.Data.Count > 0)
                            {
                                string createTime = DateTime.Now.ToString("yyyy-MM-dd"); //fristMeter.createTime.Value.ToString("yyyy-MM-dd");
                                if (rmL.Data[0].tareTypeNo == "001007001") //固定时间
                                {
                                    if (DateTime.Compare(Convert.ToDateTime(createTime + " " + rmL.Data[0].tareTime), DateTime.Now) > 0)
                                    {
                                        PbCache.monitorResult.valid_tare_time = true;
                                    }
                                    else
                                    {
                                        PbCache.monitorResult.valid_tare_time = false;
                                        PbCache.ResultMessage = string.Format("计量回皮时间超出范围");
                                    }
                                }
                                else if (rmL.Data[0].tareTypeNo == "001007002") //时间间隔
                                {
                                    if (DateTime.Compare(fristMeter.createTime.Value.AddSeconds(rmL.Data[0].tareSpaceTime == null ? 0 : (double)rmL.Data[0].tareSpaceTime), DateTime.Now) > 0)
                                    {
                                        PbCache.monitorResult.valid_tare_time = true;
                                    }
                                    else
                                    {
                                        PbCache.monitorResult.valid_tare_time = false;
                                        PbCache.ResultMessage = string.Format("计量回皮时间超出范围");
                                    }
                                }
                                else 
                                {
                                    PbCache.monitorResult.valid_tare_time = false;
                                    PbCache.ResultMessage = "未找到回皮时间配置";
                                    //PbCache.ResultMessage = string.Format("未找到回皮类型[{0}]配置的验证计量回皮时间逻辑信息", rmL.Data[0].tare_type_no);
                                }

                            }
                            else
                            {
                                PbCache.monitorResult.valid_tare_time = true;
                                //PbCache.ResultMessage = string.Format("未找到计量类型[{0}]对应的物料[{1}]配置的计量回皮时间信息", scale.meterTypeName, scale.matterName);
                                //PbCache.ResultMessage = "未找到回皮时间配置";
                            }
                        }
                        else
                        {
                            PbCache.monitorResult.valid_tare_time = false;
                            PbCache.ResultMessage = "未找到回皮时间配置";
                            //PbCache.ResultMessage = string.Format("未找到计量类型[{0}]对应的物料[{1}]配置的计量回皮时间信息", scale.meterTypeName, scale.matterName);
                        }
                    }
                    else
                    {
                        PbCache.monitorResult.valid_tare_time = true;
                    }
                }
                else 
                {
                    PbCache.monitorResult.valid_tare_time = true;
                }
            }
        }
    }
}
