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

namespace MeterConditionLibrary
{
    /// <summary>
    /// 验证时间间隔
    /// 两次计量时间间隔需大于等于该时间
    /// </summary>
    public class validTimeInterval
    {
        /// <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.validTimeInterval == "1" && !PbCache.isNotValid)
                    {
                        if (PbCache.timeSpace != null || PbCache.timeSpace.Count != 0)
                        {
                            if (scale == null || string.IsNullOrEmpty(scale.meterTypeNo))
                            {
                                scale.meterTypeNo = "001006003"; //过皮重没有检斤类型，按照中控倒运走；
                            }
                            MeterBaseTimeSpace mbt = PbCache.timeSpace.Where(s => s.meterTypeNo == scale.meterTypeNo).FirstOrDefault();
                            if (mbt != null && !string.IsNullOrEmpty(mbt.meterTypeNo))
                            {
                                if (fristMeter == null || fristMeter.createTime == null)
                                {
                                    MeterWorkCarActualFirstService mwcfs = new MeterWorkCarActualFirstService();
                                    RESTfulResult<List<MeterWorkCarActualFirst>> rmX = mwcfs.queryForTimeInterval(new MeterWorkCarActualFirst { carNo = scale.carNo });
                                    if (rmX.Succeed && rmX.Data != null && rmX.Data.Count > 0)
                                    {
                                        fristMeter = rmX.Data[0];
                                    }
                                }
                                if (fristMeter == null || fristMeter.createTime == null)
                                {
                                    PbCache.monitorResult.valid_time_interval = true;
                                }
                                else
                                {
                                    if (DateTime.Compare(DateTime.Now, fristMeter.createTime.Value.AddMinutes((double)mbt.spaceTime)) > 0)
                                    {
                                        PbCache.monitorResult.valid_time_interval = true;
                                    }
                                    else
                                    {
                                        PbCache.monitorResult.valid_time_interval = false;
                                        PbCache.ResultMessage = string.Format("一次二次计量时间间隔太近,时间间隔要求大于【" + mbt.spaceTime + "】分钟，请点语音求助！");
                                    }
                                }
                            }
                            else
                            {
                                PbCache.monitorResult.valid_time_interval = true;
                                //PbCache.ResultMessage = string.Format("未找到二次计量时间间隔配置");
                            }
                        }
                        else
                        {
                            PbCache.monitorResult.valid_time_interval = true;
                            //PbCache.ResultMessage = string.Format("未找到二次计量时间间隔配置");
                        }
                    }
                    else
                    {
                        PbCache.monitorResult.valid_time_interval = true;
                    }
                }
                else if (sMeterType == "2" || sMeterType == "1")
                {
                    if (PbCache.monitor.validTimeInterval == "1" && !PbCache.isNotValid)
                    {
                        if (PbCache.timeSpace != null || PbCache.timeSpace.Count != 0)
                        {
                            if (scale == null || string.IsNullOrEmpty(scale.meterTypeNo))
                            {
                                scale.meterTypeNo = "001006003"; //过皮重没有检斤类型，按照中控倒运走；
                            }
                            MeterBaseTimeSpace mbt = PbCache.timeSpace.Where(s => s.meterTypeNo == scale.meterTypeNo).FirstOrDefault();
                            if (mbt != null && !string.IsNullOrEmpty(mbt.meterTypeNo))
                            {
                                if (fristMeter == null || fristMeter.createTime == null)
                                {
                                    MeterWorkCarActualFirstService mwcfs = new MeterWorkCarActualFirstService();
                                    RESTfulResult<List<MeterWorkCarActualFirst>> rmX = mwcfs.queryForTimeInterval(new MeterWorkCarActualFirst { carNo = scale.carNo });
                                    if (rmX.Succeed && rmX.Data != null && rmX.Data.Count > 0)
                                    {
                                        fristMeter = rmX.Data[0];
                                    }
                                }
                                if (fristMeter == null || fristMeter.createTime == null)
                                {
                                    PbCache.monitorResult.valid_time_interval = true;
                                }
                                else
                                {
                                    if (DateTime.Compare(DateTime.Now, fristMeter.createTime.Value.AddMinutes((double)mbt.spaceTime)) > 0)
                                    {
                                        PbCache.monitorResult.valid_time_interval = true;
                                    }
                                    else
                                    {
                                        PbCache.monitorResult.valid_time_interval = false;
                                        PbCache.ResultMessage = string.Format("连续两次计量时间间隔太近,时间间隔要求大于【" + mbt.spaceTime + "】分钟，请点语音求助！");
                                    }
                                }
                            }
                            else
                            {
                                PbCache.monitorResult.valid_time_interval = true;
                                //PbCache.ResultMessage = string.Format("未找到二次计量时间间隔配置");
                            }
                        }
                        else
                        {
                            PbCache.monitorResult.valid_time_interval = true;
                            //PbCache.ResultMessage = string.Format("未找到二次计量时间间隔配置");
                        }
                    }
                    else
                    {
                        PbCache.monitorResult.valid_time_interval = true;
                    }
                }
                else
                {
                    PbCache.monitorResult.valid_time_interval = true;
                }
            }
        }
    }
}