using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Stat.DBUtility;

namespace Stat.SQLServerDAL
{
    /// <summary>
    /// 站点
    /// </summary>
    ///<remarks>Juju 2011-4-18</remarks>
    class WebSites : IDAL.IWebSites
    {
        private const string PARM_WEBID = "@webid";
        private const string PARM_WEBNAME = "@WebName";
        private const string PARM_WEBURL = "@WebUrl";
        private const string PARM_WEBSTATE = "@WebState";
        private const string PARM_USERID = "@UserId";
        private const string PARM_IS_CODELOCK = "@IsCodeLock";
        private const string PARM_VIEW_STYPE = "@ViewStype";
        private const string PARM_SHOW_STYPE = "@ShowStype";
        private const string PARM_OPENITEM = "@OpenItem";
        private const string PARM_GATHER_SERVER_ID = "@GatherServerID";





        #region ：Methods

        public IList<Model.WebSites> GetListByGatherId(string gatherId)
        {
            SqlParameter parm = new SqlParameter(PARM_GATHER_SERVER_ID, SqlDbType.Int);
            parm.Value = gatherId;

            IList<Model.WebSites> list = GetList(DBUtility.SqlHelper.ExecuteDataTable(DBUtility.SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsitesLoadByGatherId", parm));

            return list;

        }

        /// <summary>
        /// 获取某站点的详细信息
        /// </summary>
        /// <param name="webid"></param>
        /// <returns></returns>
        public Model.WebSites Get(int webid)
        {
            SqlParameter parm = new SqlParameter(PARM_WEBID, SqlDbType.Int);
            parm.Value = webid;

            IList<Model.WebSites> list = GetList(DBUtility.SqlHelper.ExecuteDataTable(DBUtility.SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsitesLoadByWebid", parm));
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 删除某站点的统计信息
        /// </summary>
        /// <param name="webid"></param>
        /// <returns></returns>
        public bool ClearAll(int webid)
        {
            SqlParameter parm = new SqlParameter(PARM_WEBID, SqlDbType.Int);
            parm.Value = webid;
            int rInt = DBUtility.SqlHelper.ExecuteNonQuery(DBUtility.SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsiteClearAll", parm);
            return (rInt > 0);
        }
        /// <summary>
        /// 某用户的所有站点列表
        /// </summary>
        /// <param name="nPower"></param>
        /// <returns></returns>
        public IList<Model.WebSites> GetWebsitesInPower(string nPower)
        {
            SqlParameter parm = new SqlParameter("@nPower", SqlDbType.NVarChar);
            parm.Value = nPower;
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_websitesLoadByPower", parm));
        }
        /// <summary>
        /// 添加站点
        /// </summary>
        public bool Add(Model.WebSites model)
        {

            SqlParameter[] parms = SetParmeters(model);

            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebSitesInsert", parms);


            return rInt > 0;

        }
        /// <summary>
        /// 修改站点
        /// </summary>
        /// <param name="w"></param>
        /// <returns></returns>
        public bool Edit(Model.WebSites model)
        {

            SqlParameter[] parms = SetParmeters(model);

            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsiteUpdate", parms);

            return rInt > 0;

        }
        /// <summary>
        /// 删除站点
        /// </summary>
        public bool Delete(int webid)
        {
            SqlParameter parm = new SqlParameter(PARM_WEBID, SqlDbType.Int);
            parm.Value = webid;
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsiteDelete", parm);

            return rInt > 0;
        }
        /// <summary>
        ///搜索 名称 URL
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public IList<Model.WebSites> GetList(string key)
        {
            SqlParameter parm = new SqlParameter("@key", SqlDbType.NVarChar);
            parm.Value = key;
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsitesLoadByKey", parm));

        }

        /// <summary>
        /// 所有站点列表
        /// </summary>
        /// <returns></returns>
        public IList<Model.WebSites> GetWebSitesList()
        {
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsiteLoadAll", null));
        }
        public bool UpdateShowStype(Model.WebSites model)
        {
            SqlParameter[] parms = SetParmeters(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsiteUpdateShowstype", parms);

            return rInt > 0;
        }
        /// <summary>
        /// 获取站点的总数
        /// </summary>
        /// <returns></returns>
        public int GetWebSum()
        {
            SqlParameter parm = new SqlParameter("@wCount", SqlDbType.Int);
            parm.Direction = ParameterDirection.Output;

            object obj = SqlHelper.ExecuteReturnParmeter(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsiteCount", parm);

            if (obj != null)
            {
                int rInt;
                Int32.TryParse(obj.ToString(), out rInt);
                return rInt;
            }
            return 0;

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="webid"></param>
        /// <returns></returns>
        /// <remarks>By Juju 2011-11-07</remarks>
        public int GetDayRank(int webid)
        {
            SqlParameter[] parms = new SqlParameter[] { 
            new SqlParameter("@rank", SqlDbType.Int),
            new SqlParameter(PARM_WEBID, SqlDbType.Int),
            new SqlParameter("@TableName", SqlDbType.NVarChar)            
            };
            parms[0].Direction = ParameterDirection.Output;
            parms[1].Value = webid;
            parms[2].Value = "Stat_ViewDay_" + DateTime.Now.AddDays(-1).Year;

            object obj = SqlHelper.ExecuteReturnParmeter(0, SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsiteRankByDay", parms);

            if (obj != null)
            {
                Int32 rInt;
                Int32.TryParse(obj.ToString(), out rInt);
                return rInt;
            }
            return 0;

        }

        /// <summary>
        /// 获取某站点的总浏览数
        /// </summary>
        /// <param name="webid"></param>
        /// <returns></returns>
        /// <remarks>2009-10-13 1:04</remarks>
        public Int32 GetViewSum(string webid)
        {
            SqlParameter[] parms = new SqlParameter[] { 
            new SqlParameter("@sum", SqlDbType.Int),
            new SqlParameter(PARM_WEBID, SqlDbType.Int)
            };
            parms[0].Direction = ParameterDirection.Output;
            parms[1].Value = Convert.ToInt32(webid);

            object obj = SqlHelper.ExecuteReturnParmeter(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_WebsiteViewCount", parms);


            if (obj != null)
            {
                Int32 rInt;
                Int32.TryParse(obj.ToString(), out rInt);
                return rInt;
            }
            return 0;
        }

        #endregion

        #region ： Set Parmeters
        private static SqlParameter[] SetParmeters(Model.WebSites model)
        {
            SqlParameter[] parms = GetParmeters();
            parms[0].Value = model.WebName;
            parms[1].Value = model.WebUrl;
            parms[2].Value = model.WebState;
            parms[3].Value = model.UserId;
            parms[4].Value = model.IsCodeLock ? 1 : 0;
            parms[5].Value = model.IsReFresh ? 1 : 0;
            parms[6].Value = model.ShowStype == null ? "" : model.ShowStype;
            parms[7].Value = model.OpenItem;
            parms[8].Value = model.GatherServerID;
            parms[9].Value = model.WebId;
            return parms;
        }

        private static SqlParameter[] GetParmeters()
        {
            SqlParameter[] parms = new SqlParameter[] {
            new SqlParameter(PARM_WEBNAME, SqlDbType.NVarChar),
            new SqlParameter(PARM_WEBURL, SqlDbType.NVarChar),
            new SqlParameter(PARM_WEBSTATE, SqlDbType.TinyInt),
            new SqlParameter(PARM_USERID, SqlDbType.Int),
            new SqlParameter(PARM_IS_CODELOCK, SqlDbType.Int),
            new SqlParameter(PARM_VIEW_STYPE, SqlDbType.Int),
            new SqlParameter(PARM_SHOW_STYPE, SqlDbType.NVarChar),
            new SqlParameter(PARM_OPENITEM, SqlDbType.NText),
            new SqlParameter(PARM_GATHER_SERVER_ID, SqlDbType.Int),
            new SqlParameter(PARM_WEBID, SqlDbType.Int)
            };
            return parms;
        }
        #endregion

        #region ：Utilities
        /// <summary>
        /// 获取实体
        /// </summary>
        ///<param name="row"></param>
        /// <returns></returns>
        private static Model.WebSites GetModel(System.Data.DataRow row)
        {
            Model.WebSites model = new Model.WebSites();
            if (row != null)
            {
                if (row["WebId"] != DBNull.Value) model.WebId = Convert.ToInt32(row["WebId"]);
                if (row["WebName"] != DBNull.Value) model.WebName = row["WebName"].ToString();
                if (row["WebUrl"] != DBNull.Value) model.WebUrl = row["WebUrl"].ToString();
                if (row["WebState"] != DBNull.Value) model.WebState = Convert.ToInt32(row["WebState"]);
                if (row["UserId"] != DBNull.Value) model.UserId = Convert.ToInt32(row["UserId"]);
                if (row["AddTime"] != DBNull.Value) model.AddTime = Convert.ToDateTime(row["AddTime"]);
                if (row["ViewSum"] != DBNull.Value) model.YdIpCount = Convert.ToInt32(row["ViewSum"]);
                if (row["IsCodeLock"] != DBNull.Value) model.IsCodeLock = Convert.ToInt32(row["IsCodeLock"]) == 1 ? true : false;
                if (row["ShowStype"] != DBNull.Value) model.ShowStype = row["ShowStype"].ToString();
                //
                if (row["GatherServerID"] != DBNull.Value) model.GatherServerID = Convert.ToInt32(row["GatherServerID"]);
                //                
                if (row.Table.Columns.Contains("ViewStype") && !Convert.IsDBNull(row["ViewStype"])) model.IsReFresh = Convert.ToInt32(row["ViewStype"]) == 1 ? true : false;
                if (row.Table.Columns.Contains("OpenItem") && !Convert.IsDBNull(row["OpenItem"])) model.OpenItem = row["OpenItem"].ToString(); // OpenItem
                if (row.Table.Columns.Contains("YdPvCount") && !Convert.IsDBNull(row["YdPvCount"])) model.YdPvCount = Convert.ToInt32(row["YdPvCount"]); // YdPvCount
                if (row.Table.Columns.Contains("YdIpCount") && !Convert.IsDBNull(row["YdIpCount"])) model.YdIpCount = Convert.ToInt32(row["YdIpCount"]); // YdIpCount
            }
            return model;
        }
        /// <summary>
        /// 获取 列表
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        internal static IList<Model.WebSites> GetList(System.Data.DataTable table)
        {
            List<Model.WebSites> list = new List<Model.WebSites>();
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(GetModel(table.Rows[i]));
                }
            }
            return list;
        }
        #endregion

    }
}
