using System.Collections.Generic;
using System.Web;
using System.Web.Caching;
namespace Stat.Cache
{
    class Notify : INotify
    {
        /// <summary>
        /// 公告
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public IList<Stat.Model.Notify> Get(int id)
        {
            string cName = "Notify_" + id.ToString();
            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                string cTxt = @"\CacheTxt\Notify.txt";
                Methods.CreatFile(HttpContext.Current.Server.MapPath(cTxt));
                CacheDependency dep = new CacheDependency(HttpContext.Current.Server.MapPath(cTxt));

                IList<Stat.Model.Notify> list = BLL.Notify.Get(id);

                if (list != null && list.Count > 0)
                {
                    HttpContext.Current.Cache.Insert(cName, list, dep, System.DateTime.MaxValue, System.TimeSpan.FromMinutes(10.0), CacheItemPriority.Default, null);
                }
                return list;
            }
            return (IList<Stat.Model.Notify>)HttpContext.Current.Cache.Get(cName);
        }

        #region INotify 成员


        public IList<Stat.Model.Notify> Get(Stat.Model.Notify notieModel, ref Stat.Model.PageInfo pageModel)
        {
            string cName = "Notify_" + pageModel.SortType + "_" + pageModel.PageIndex + "_" + pageModel.PageSize;

            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                string cTxt = @"\CacheTxt\Notify.txt";
                Methods.CreatFile(HttpContext.Current.Server.MapPath(cTxt));
                CacheDependency depList = new CacheDependency(HttpContext.Current.Server.MapPath(cTxt));

                IList<Stat.Model.Notify> list = BLL.Notify.Get(notieModel, ref pageModel);

                if (list != null && list.Count > 0)
                {
                    HttpContext.Current.Cache.Insert(cName, list, depList, System.DateTime.MaxValue, System.TimeSpan.FromHours(24.0), System.Web.Caching.CacheItemPriority.Low, null);
                    HttpContext.Current.Cache.Insert(cName + "_RecordCount", pageModel.RecordCount, null, System.DateTime.MaxValue, System.TimeSpan.FromHours(24.5), System.Web.Caching.CacheItemPriority.Normal, null);

                }
                return list;
            }
            pageModel.RecordCount = int.Parse(HttpContext.Current.Cache.Get(cName + "_RecordCount").ToString());
            return (IList<Stat.Model.Notify>)HttpContext.Current.Cache.Get(cName);
        }

        #endregion
    }
}
