using System;
using System.Collections.Generic;
using System.Web;

namespace Stat.BLL
{
    /// <summary>
    /// 日访问
    /// </summary>
    /// <remarks>By JUju 2011-09-18</remarks>
    public class ViewDay
    {
        private static IDAL.IViewDay dal = DALFactory.AbstractFactory.GetInstance().CreateViewDay();
        /// <summary>
        /// 时段统计：获取日统计数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <remarks>Juju 2011-10-26</remarks>
        public static List<Model.ViewDay> Get(Model.StatInfo model)
        {
            string cName = "V_Day_" + model.WebID + "_" + model.StartTime + "_" + model.EndTime;
            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                //历史数据
                List<Model.ViewDay> list = dal.Get(model);
                //包含今日
                if (model.EndTime >= Convert.ToDateTime(DateTime.Now.AddDays(1).ToShortDateString()))
                {
                    //获取 [小时数据]<=估算
                    list.Add(GetToDay(model));
                }
                //合计
                list = Report.Rp_vDay.Get(list, model.StartTime, model.EndTime);
                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);//从缓存中读取
        }
        /// <summary>
        /// 风云榜 TOP 1000
        /// </summary>
        /// <returns></returns>
        public static List<Model.ViewDay> Rank(DateTime dt)
        {
            return dal.Rank(dt);
        }

        /// <summary>
        /// 获得今日数据
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        /// <remarks>By Juju 2011-10-26</remarks>
        private static Model.ViewDay GetToDay(Model.StatInfo model)
        {

            int rInt = ViewClick.GetTotal(model);

            if (rInt > 3000)
            {
                Model.StatInfo modelHour = new Model.StatInfo();
                modelHour.WebID = model.WebID;
                modelHour.GatherID = model.GatherID;
                modelHour.StartTime = Convert.ToDateTime(DateTime.Now.ToShortDateString());
                //小时段数据
                List<Model.ViewHour> listHour = BLL.ViewHour.GetToday(modelHour, -2);//-2
                //基础表数据  
                Model.StatInfo modelDay = modelHour;
                DateTime dtStartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0);
                modelDay.Hour = dtStartTime.Hour;
                modelDay.Hour = modelDay.Hour > 0 ? (modelDay.Hour - 1) : modelDay.Hour;//-1
                List<Model.ViewItem_Int> afterList = ViewClick.GetListByGeqHour(modelDay);
                //合并
                return Report.Rp_vDay.StatToday(listHour, afterList);
            }
            else
            {
                model.Hour = 0;
                List<Model.ViewItem_Int> afterList = ViewClick.GetListByGeqHour(model);     
                return Report.Rp_vDay.StatToday(null, afterList);
            }
        }
    }
}
