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


namespace Stat.Cache
{
    class Ad : IAd
    {


        public string GetAD(int Posit)
        {
            string cName = "AD_" + Posit.ToString();
            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                // 建立缓存依赖项，如果文件发生变化，删除缓存
                string _Ctxt = @"\CacheTxt\AD.txt";
                Methods.CreatFile(HttpContext.Current.Server.MapPath(_Ctxt));
                CacheDependency dep = new CacheDependency(HttpContext.Current.Server.MapPath(_Ctxt));
                string tep = string.Empty;
                IList<Stat.Model.AD> list = BLL.AD.GetPosit(Posit);
                if (list.Count > 0)
                {
                    tep = list[0].Code;
                    HttpContext.Current.Cache.Insert(cName, tep, dep, System.DateTime.MaxValue, System.TimeSpan.FromDays(1.0), CacheItemPriority.Default, null);
                }
                return tep;
            }
            return HttpContext.Current.Cache.Get(cName).ToString();//从缓存中读取
        }



        /// <summary>
        /// 获取图片友情链接
        /// </summary>
        /// <param name="num"></param>
        /// <param name="img_w"></param>
        /// <param name="img_h"></param>
        ///<param name="cType">0：一般，1：赞助商</param>
        /// <returns></returns>
        public IList<Stat.Model.FriendLink> GetImg(int num, int cType)
        {

            string cName = "FL_IMG_" + num.ToString() + "_" + cType.ToString();
            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                // 建立缓存依赖项，如果文件发生变化，删除缓存
                string _Ctxt = @"\CacheTxt\FL.txt";
                Methods.CreatFile(HttpContext.Current.Server.MapPath(_Ctxt));
                CacheDependency dep = new CacheDependency(HttpContext.Current.Server.MapPath(_Ctxt));

                IList<Stat.Model.FriendLink> tep = BLL.FriendLink.Get(num, 1, 1, cType);

                if (tep != null)
                {
                    HttpContext.Current.Cache.Insert(cName, tep, dep, System.DateTime.MaxValue, System.TimeSpan.FromDays(1.0), CacheItemPriority.Default, null);
                }
                return tep;
            }
            return (IList<Stat.Model.FriendLink>)(HttpContext.Current.Cache.Get(cName));//从缓存中读取
        }
        /// <summary>
        /// 获取图片友情链接（图文）
        /// </summary>
        /// <param name="num"></param>
        ///<param name="cType">0：一般，1：赞助商</param>
        /// <returns></returns>
        public IList<Stat.Model.FriendLink> GetImg_Txt(int num, int cType)
        {

            string cName = "FL_Txt_" + num.ToString() + "_" + cType.ToString();
            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                // 建立缓存依赖项，如果文件发生变化，删除缓存
                string _Ctxt = @"\CacheTxt\FL.txt";
                Methods.CreatFile(HttpContext.Current.Server.MapPath(_Ctxt));
                CacheDependency dep = new CacheDependency(HttpContext.Current.Server.MapPath(_Ctxt));
                IList<Stat.Model.FriendLink> tep = BLL.FriendLink.Get(num, 1, 1, cType);
                if (tep != null)
                {
                    HttpContext.Current.Cache.Insert(cName, tep, dep, System.DateTime.MaxValue, System.TimeSpan.FromDays(1.0), CacheItemPriority.Default, null);
                }
                return tep;
            }
            return (IList<Stat.Model.FriendLink>)(HttpContext.Current.Cache.Get(cName));//从缓存中读取
        }

        /// <summary>
        /// 获取图片友情链接
        /// </summary>
        /// <param name="num"></param>
        /// <param name="img_w"></param>
        /// <param name="img_h"></param>
        /// <param name="cType">2：LOGO</param>
        /// <param name="Position">0,1,2,3</param>
        /// <returns></returns>
        public IList<Stat.Model.FriendLink> GetImg(int num, int cType, int Position)
        {

            string cName = "FL_IMG_" + num.ToString() + "_" + cType.ToString() + "_" + Position.ToString();
            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                // 建立缓存依赖项，如果文件发生变化，删除缓存
                string _Ctxt = @"\CacheTxt\FL.txt";
                Methods.CreatFile(HttpContext.Current.Server.MapPath(_Ctxt));
                CacheDependency dep = new CacheDependency(HttpContext.Current.Server.MapPath(_Ctxt));
                IList<Stat.Model.FriendLink> tep = BLL.FriendLink.Get(num, 1, 1, cType, Position);
                if (tep != null)
                {
                    HttpContext.Current.Cache.Insert(cName, tep, dep, System.DateTime.MaxValue, System.TimeSpan.FromDays(1.0), CacheItemPriority.Default, null);
                }
                return tep;
            }
            return (IList<Stat.Model.FriendLink>)(HttpContext.Current.Cache.Get(cName));//从缓存中读取

        }

        /// <summary>
        /// 获取图片友情链接（图文）
        /// </summary>
        /// <param name="num"></param>
        /// <param name="img_w"></param>
        /// <param name="img_h"></param>
        /// <param name="cType">2：LOGO</param>
        /// <param name="Position">0,1,2,3</param>
        /// <returns></returns>
        public IList<Stat.Model.FriendLink> GetImg_Txt(int num, int cType, int Position)
        {

            string cName = "FL_IMG_Txt" + num.ToString() + "_" + cType.ToString() + "_" + Position.ToString();
            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                // 建立缓存依赖项，如果文件发生变化，删除缓存
                string _Ctxt = @"\CacheTxt\FL.txt";
                Methods.CreatFile(HttpContext.Current.Server.MapPath(_Ctxt));
                CacheDependency dep = new CacheDependency(HttpContext.Current.Server.MapPath(_Ctxt));
                IList<Stat.Model.FriendLink> tep = BLL.FriendLink.Get(num, 1, 1, cType, Position);
                if (tep != null)
                {
                    HttpContext.Current.Cache.Insert(cName, tep, dep, System.DateTime.MaxValue, System.TimeSpan.FromDays(1.0), CacheItemPriority.Default, null);
                }
                return tep;
            }
            return (IList<Stat.Model.FriendLink>)(HttpContext.Current.Cache.Get(cName));//从缓存中读取

        }
        /// <summary>
        /// 获取图片友情链接（图文）
        /// </summary>
        /// <param name="num"></param>
        /// <param name="cType">2：LOGO</param>
        /// <param name="Position">0,1：首页切换,2：统计菜单下,3</param>
        /// <returns></returns>
        public IList<Stat.Model.FriendLink> GetImg_Play(int num, int cType, int Position)
        {

            string cName = "FL_Play" + num.ToString() + "_" + cType.ToString() + "_" + Position.ToString();
            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                // 建立缓存依赖项，如果文件发生变化，删除缓存
                string _Ctxt = @"\CacheTxt\FL.txt";
                Methods.CreatFile(HttpContext.Current.Server.MapPath(_Ctxt));
                CacheDependency dep = new CacheDependency(HttpContext.Current.Server.MapPath(_Ctxt));

                IList<Stat.Model.FriendLink> tep = BLL.FriendLink.Get(num, 1, 1, cType, Position);
                if (tep != null)
                {
                    HttpContext.Current.Cache.Insert(cName, tep, dep, System.DateTime.MaxValue, System.TimeSpan.FromDays(1.0), CacheItemPriority.Default, null);
                }
                return tep;
            }
            return (IList<Stat.Model.FriendLink>)(HttpContext.Current.Cache.Get(cName));//从缓存中读取

        }

        /// <summary>
        ///  获取友情链接（文本）
        /// </summary>
        /// <param name="num"></param>
        /// <param name="cType"></param>
        /// <returns></returns>
        public IList<Stat.Model.FriendLink> GetText(int num, int cType)
        {
            string cName = "FL_TXT" + num.ToString() + "_" + cType.ToString();
            if (HttpContext.Current.Cache.Get(cName) == null)
            {
                // 建立缓存依赖项，如果文件发生变化，删除缓存
                string _Ctxt = @"\CacheTxt\FL.txt";
                Methods.CreatFile(HttpContext.Current.Server.MapPath(_Ctxt));
                CacheDependency dep = new CacheDependency(HttpContext.Current.Server.MapPath(_Ctxt));
                IList<Stat.Model.FriendLink> tep = BLL.FriendLink.Get(num, 1, 0, cType);
                if (tep != null)
                {
                    HttpContext.Current.Cache.Insert(cName, tep, dep, System.DateTime.MaxValue, System.TimeSpan.FromDays(1.0), CacheItemPriority.Default, null);

                }
                return tep;
            }
            return (IList<Stat.Model.FriendLink>)(HttpContext.Current.Cache.Get(cName));//从缓存中读取
        }

    }
}
