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

namespace MeterConditionLibrary
{
    /// <summary>
    /// 物料计量时间段配置表
    /// 这个只限制毛重计量
    /// </summary>
    public class validMatTime
    {
        Log lg = Log.GetInstance();
        /// <summary>
        /// 验证物料计量时间段配置表 只验证毛重计量
        /// </summary>
        /// <param name="scale">预报信息</param>
        /// <param name="sMeterType">计量类型，1过毛重,2过皮重,3结净</param>
        public void ValidMethod(PreTrackScale scale, string sMeterType)
        {
            //未锁定的情况下
            //if (!PbCache.isLockFrm)
            {
                if (sMeterType == "1")
                {
                    //验证物料计量时间段
                    if (PbCache.monitor.validMatTime == "1" && !PbCache.isNotValid)
                    {
                        string sql = @"SELECT T1.START_TIME startTime, 
                                       T1.END_TIME endTime
                                  FROM METER_BASE_TIMES_INFO T1, METER_BASE_MATTER_TIMES T2
                                 WHERE T1.TIMES_NO = T2.TIMES_NO
                                   AND T1.VALID_FLAG = '1'
                                   AND T2.MATTER_NO = '" + scale.matterNo + "'";

                        PbModelDbService<List<MeterBaseTimesInfo>> pb = new PbModelDbService<List<MeterBaseTimesInfo>>();
                        RESTfulResult<List<MeterBaseTimesInfo>> rm = pb.executeSqlDataWf(sql);
                        if (rm.Succeed)
                        {
                            if (rm.Data != null && rm.Data.Count > 0)
                            {
                                List<MeterBaseTimesInfo> dt = rm.Data;
                                string str = DateTime.Now.ToString("HH:mm:ss");
                                if (str.CompareTo(dt[0].startTime) >= 0 && str.CompareTo(dt[0].endTime) <= 0)
                                {
                                    PbCache.monitorResult.valid_mat_time = true;
                                }
                                else
                                {
                                    PbCache.monitorResult.valid_mat_time = false;
                                    PbCache.ResultMessage = "当前时间段不允许计量该物料";
                                }
                            }
                            else
                            {
                                PbCache.monitorResult.valid_mat_time = true;
                                //PbCache.ResultMessage = "未找到物料配置的时间范围";
                            }
                        }
                        else
                        {
                            PbCache.monitorResult.valid_mat_time = false;
                            PbCache.ResultMessage = "称重数据保存失败";
                            lg.WriteLog(3, "计量失败：" + rm.ResultMessage);
                            //PbCache.ResultMessage = "未找到物料配置的时间范围";
                        }
                    }
                    else
                    {
                        PbCache.monitorResult.valid_mat_time = true;
                    }
                }
                else 
                {
                    PbCache.monitorResult.valid_mat_time = true;
                }
            }
        }
    }
}
