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

namespace Stat.BLL
{
    /// <summary>
    /// 站点信息
    /// </summary>
    public class WebSites
    {
        private static IDAL.IWebSites dal = DALFactory.AbstractFactory.GetInstance().CreateWebSites();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="webid"></param>
        /// <returns></returns>
        public static string GetGatherServerUrl(int webid)
        {
            Model.WebSites siteList = dal.Get(webid);
            if (siteList != null)
            {
                Model.GatherServer list = BLL.GatherServer.Get(siteList.GatherServerID);
                if (list != null)
                {
                    return list.ServerDomain;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获取站点的详细信息
        /// </summary>
        /// <param name="webid"></param>
        /// <returns></returns>
        public static Stat.Model.WebSites GetWebSiteInfo(int webid)
        {
            return dal.Get(webid);
        }
        /// <summary>
        ///搜索 名称 URL
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IList<Stat.Model.WebSites> GetList(string key)
        {
            return dal.GetList(key);
        }
        /// <summary>
        /// 获取站点的总浏览数
        /// </summary>
        /// <param name="webid"></param>
        /// <returns></returns>
        public static Int32 GetViewSum(int webid)
        {
            string cName = "CacheViewSum_" + webid;
            Int32 count = 0;
            if (HttpContext.Current.Cache.Get(cName) != null)
            {
                count = Convert.ToInt32(HttpContext.Current.Cache.Get(cName)) + 1;
            }
            else { count = dal.GetViewSum(webid.ToString()); }
            HttpContext.Current.Cache.Insert(cName, count, null, DateTime.Now.AddHours(1), TimeSpan.Zero);
            return count;
        }
        /// <summary>
        /// 获取站点的的今日PV排名
        /// </summary>
        public static Int32 GetDayRank(int webid)
        {
            return dal.GetDayRank(webid);
        }
        /// <summary>
        /// 所有站点列表
        /// </summary>
        /// <returns></returns>
        public static IList<Stat.Model.WebSites> GetWebSitesList()
        {
            return dal.GetWebSitesList();
        }
        /// <summary>
        /// 某用户的所有站点列表
        /// </summary>
        /// <param name="nPower"></param>
        /// <returns></returns>
        public static IList<Stat.Model.WebSites> GetWebsitesInPower(string nPower)
        {
            return dal.GetWebsitesInPower(nPower);
        }
        /// <summary>
        /// 添加站点
        /// </summary>
        public static bool Add(Stat.Model.WebSites model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 修改站点
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool Edit(Stat.Model.WebSites model)
        {
            model.ViewPwd = model.ViewPwd == null ? "" : model.ViewPwd;
            if (model.ViewPwd != "")
            {
                model.ViewPwd = Plugin.Common.MD5(model.ViewPwd);
            }

            return dal.Edit(model);
        }

        /// <summary>
        /// 删除站点
        /// </summary>
        public static bool Delete(int id)
        {
            if (id == 1) { return false; }//保护第一个站点


            return dal.Delete(id);

        }
        /// <summary>
        /// 删除某站点的统计信息
        /// </summary>
        /// <param name="webid"></param>
        /// <returns></returns>
        public static bool ClearAll(int webid)
        {

            return dal.ClearAll(webid);
        }
        /// <summary>
        /// 获取站点的总数
        /// </summary>
        /// <returns></returns>
        public static int GetWebSum()
        {
            return dal.GetWebSum();
        }

        /// <summary>
        /// 修改统计显示图标
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static bool UpdateShowStype(Model.WebSites model)
        {
            return dal.UpdateShowStype(model);
        }
    }
}
