using System;
using System.Collections.Generic;
using System.Web;
namespace Stat.BLL
{
    /// <summary>
    /// 搜索引擎
    /// </summary>
    public class ViewEngine
    {
        static Stat.IDAL.IViewEngine dal = Stat.DALFactory.AbstractFactory.GetInstance().CreateViewEngine();

        /// <summary>
        /// 搜索引擎分类(来自基础)
        /// </summary>
        /// <param name="model"></param> 
        /// <returns></returns>
        public static List<Model.ViewItem_Engine> GetItem(Model.StatInfo model)
        {
            return dal.GetItem(model);
        }
        /// <summary>
        /// 搜索引擎分类(来自报表)
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        ///<remarks>Juju 2011-10-27</remarks>
        public static IList<Model.StatItem> GetList(Model.StatInfo model)
        {
            string cName = "V_Engine_" + model.WebID + "_" + model.StartTime + "_" + model.EndTime + "_" + model.EngineId + "_" + model.EngineKeyWord;
            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                //历史记录
                List<Model.StatItem> list = dal.GetList(model);
                //基础表数据
                if (list == null || list.Count < 1)
                    list = Report.Rp_Engine.SingleDay(BLL.ViewEngine.GetItem(model));
                else
                    list = Report.Rp_Engine.MultiDay(list);
                //
                HttpContext.Current.Cache.Insert(cName, list, null, DateTime.MaxValue, TimeSpan.FromSeconds(3.0), System.Web.Caching.CacheItemPriority.Low, null);
                return list;
            }
            return (List<Model.StatItem>)HttpContext.Current.Cache.Get(cName);//从缓存中读取
        }
        /// <summary>
        /// 搜索引擎详细
        /// </summary>
        /// <param name="pageInfo"></param>
        /// <param name="searchInfo"></param>
        /// <returns></returns>
        /// <remarks>Juju 2011-10-27</remarks>
        public static List<Model.ViewClick> GetDetail(ref Model.PageInfo pageInfo, Model.SearchInfo searchInfo)
        {
            //历史数据
            List<Model.ViewClick> listHistory = GetDetailFromMaster(ref pageInfo, searchInfo);
            //基础表数据（单天） 
            if (listHistory == null || listHistory.Count == 0)
                return GetDetailFromGether(ref pageInfo, searchInfo);
            else
                return listHistory;
        }
        /// <summary>
        /// 关键字[今日数据与历史数据，分开，]
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        ///<remarks>Juju 2011-10-27</remarks>
        public static IList<Model.StatItem> GetKeyword(Model.StatInfo model)
        {
            string cName = "V_Keyword_" + model.WebID + "_" + model.StartTime + "_" + model.EndTime + "_" + model.EngineId + "_" + model.EngineKeyWord;
            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                //历史数据    
                List<Model.StatItem> list = dal.GetList(model);
                //基础表数据（单天）
                if (list == null || list.Count == 0)
                    list = Report.Rp_ViewKeyword.SingleDay(BLL.ViewEngine.GetItem(model));
                else
                    list = Report.Rp_ViewKeyword.MultiDay(list);

                HttpContext.Current.Cache.Insert(cName, list, null, DateTime.MaxValue, TimeSpan.FromSeconds(10.0), System.Web.Caching.CacheItemPriority.Low, null);
                return list;
            }
            return (List<Model.StatItem>)HttpContext.Current.Cache.Get(cName);//从缓存中读取
        }


        #region ： 私有成员
        /// <summary>
        /// 获取搜索引擎详细记录(基础表)
        /// </summary>
        /// <param name="pageInfo"></param>
        /// <param name="searchInfo"></param>
        /// <returns></returns>
        /// <remarks>Juju 2011-10-25</remarks>
        private static List<Model.ViewClick> GetDetailFromGether(ref Model.PageInfo pageInfo, Model.SearchInfo searchInfo)
        {
            return dal.GetDetailFromGether(ref pageInfo, searchInfo);
        }
        /// <summary>
        /// 获取搜索引擎详细记录(综合表)
        /// </summary>
        /// <param name="pageInfo"></param>
        /// <param name="searchInfo"></param>
        /// <returns></returns>
        /// <remarks>Juju 2011-10-25</remarks>
        private static List<Model.ViewClick> GetDetailFromMaster(ref Model.PageInfo pageInfo, Model.SearchInfo searchInfo)
        {
            return dal.GetDetailFromMaster(ref pageInfo, searchInfo);
        }
        #endregion
    }
}