using System;
using System.Collections.Generic;
using System.Web;
namespace Stat.BLL
{
    /// <summary>
    /// 小时数据
    /// </summary>
    public class ViewHour
    {
        private static IDAL.IViewHour dal = DALFactory.AbstractFactory.GetInstance().CreateViewHour();

        /// <summary>
        /// 最近小时报表
        /// </summary>
        /// <returns></returns>
        public static List<Model.StatItem> GetConnect(int webid, DateTime st, DateTime et)
        {
            return Report.Rp_vHour.GetHour(dal.GetList(webid, st, et), webid, st, et);
        }
        /// <summary>
        /// 小时统计 历史记录
        /// </summary>
        /// <param name="webid">站点ID</param>
        /// <param name="st">开始时间</param>
        /// <param name="et">结束时间</param>
        /// <returns></returns>
        public static List<Model.ViewHour> GetViewHour(int webid, DateTime st, DateTime et)
        {
            string cName = "V_ViewHour_" + webid + "_" + st + "_" + et;
            List<Model.ViewHour> list = new List<Stat.Model.ViewHour>();
            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                list = dal.GetList(webid, st, et);
                HttpContext.Current.Cache.Insert(cName, list, null, DateTime.MaxValue, TimeSpan.FromSeconds(3.0), System.Web.Caching.CacheItemPriority.Low, null);
                return list;
            }
            return (List<Model.ViewHour>)HttpContext.Current.Cache.Get(cName);//从缓存中读取
        }
        /// <summary>
        /// 小时统计 历史记录（含今日数据）
        /// </summary>
        /// <param name="statModel"></param>
        /// <param name="hourPo"></param>
        /// <returns></returns>
        public static List<Model.ViewDay> Get24Hour(Model.StatInfo statModel, int hourPo)
        {
            string cName = "V_24Hour_Day_" + statModel.WebID + "_" + statModel.StartTime + "_" + statModel.EndTime;

            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                List<Model.ViewHour> viewHourList = new List<Stat.Model.ViewHour>();
                //包含今天正在统计数据
                if (statModel.EndTime > DateTime.Now)
                {
                    DateTime dtMiddle = DateTime.Now.AddHours(hourPo); //小时断点处
                    dtMiddle = Convert.ToDateTime(dtMiddle.ToShortDateString() + " " + dtMiddle.Hour + ":00:00");

                    //过往小时数据
                    Model.StatInfo modelHistory = new Model.StatInfo();
                    modelHistory.WebID = statModel.WebID;
                    modelHistory.GatherID = statModel.GatherID;
                    modelHistory.StartTime = statModel.StartTime;
                    modelHistory.EndTime = dtMiddle;
                    viewHourList = dal.GetList(modelHistory);
                    //正在的数据 
                    Model.StatInfo modelToday = new Model.StatInfo();
                    modelToday.WebID = statModel.WebID;
                    modelToday.GatherID = statModel.GatherID;
                    modelToday.StartTime = dtMiddle;
                    modelToday.Hour = (dtMiddle.Hour + 1);//
                    List<Model.ViewItem_Int> dtHourSpan_2 = ViewClick.GetListByGeqHour(modelToday);
                    //   
                    if (dtHourSpan_2 != null && dtHourSpan_2.Count > 0)
                    {
                        viewHourList = BLL.Report.Rp_vHour.Hour_Connect(viewHourList, dtHourSpan_2, dtMiddle, statModel.EndTime);
                    }
                }
                else
                {
                    viewHourList = dal.GetList(statModel);
                }
                //
                List<Model.ViewDay> list = Report.Rp_vHour.Get(viewHourList);
                HttpContext.Current.Cache.Insert(cName, list, null, DateTime.MaxValue, TimeSpan.FromSeconds(3.0), System.Web.Caching.CacheItemPriority.Low, null);
                return list;
            }
            return (List<Model.ViewDay>)HttpContext.Current.Cache.Get(cName);//从缓存中读取
        }


        #region ：其他
        /// <summary>
        /// 今日数据（只有大于某一数目才使用）
        /// </summary>
        /// <param name="pHour">小时断点</param> 
        /// <param name="model"></param>
        /// <returns></returns>
        internal static List<Model.ViewHour> GetToday(Model.StatInfo model, int pHour)
        {
            //开始时间
            DateTime stHour = Convert.ToDateTime(DateTime.Now.ToLongDateString());
            //结束时间
            DateTime dtEndTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0);
            DateTime breakTime = dtEndTime.AddHours(pHour); //小时 断点处

            return GetViewHour(model.WebID, stHour, breakTime);
        }
        #endregion
    }
}
