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 ViewDay : IDAL.IViewDay
    {
        private const string PARM_WEBID = "@Webid";
        private const string PARM_T1 = "@t1";
        private const string PARM_T2 = "@t2";
        private const string PARM_TABLE_NAME = "@TableName";
        private const string PARM_CELLS = "@Cells";

        #region Methods ：
        /// <summary>
        /// 日统计数据
        /// </summary>
        public List<Model.ViewDay> Get(global::Stat.Model.StatInfo model)
        {
            model.Cells = "pvCount,ipCount,cusCount,newCount,timCount,otimCount,vYear,vMonth,vDay";
            SqlParameter[] parms = SetParmeters(model.WebID, null, model.Cells, model.StartTime, model.EndTime);
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_ViewDayLoadByWebid", parms));
        }
        /// <summary>
        /// 风云榜
        /// </summary>
        public List<Stat.Model.ViewDay> Rank(DateTime dt)
        {
            string tableName = "Stat_ViewDay_" + DateTime.Now.AddDays(-1).Year;
            SqlParameter[] parms = SetParmeters(0, tableName, null, dt, DateTime.MinValue);
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_ViewDay_Rank", parms));
        }

        public List<Stat.Model.ViewItem_Int> GetViewClick_TodayExact(Stat.Model.StatInfo model)
        {

            string tableName = "View_" + model.StartTime.Year + "_" + model.StartTime.Month + "_" + model.StartTime.Day;

            SqlParameter[] parms = SetParmeters(model.WebID, tableName, "top 2000 id,IsAlexa,ip,mac,View_Alone", model.StartTime, model.EndTime);
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringGather(model.GatherID), CommandType.StoredProcedure, "Stat_ViewClick_TodayExact_Get", parms);

            return ViewItems.GetInt.GetList(dt, "IsAlexa");
        }

        #endregion

        #region ：SetParmeters

        private static SqlParameter[] GetParmeters()
        {
            SqlParameter[] parms = new SqlParameter[] {
              new SqlParameter(PARM_WEBID, SqlDbType.Int),
              new SqlParameter(PARM_T1, SqlDbType.DateTime),
              new SqlParameter(PARM_T2, SqlDbType.DateTime),
              new SqlParameter(PARM_TABLE_NAME, SqlDbType.NVarChar),
              new SqlParameter(PARM_CELLS, SqlDbType.NVarChar)
            };
            return parms;
        }

        private static SqlParameter[] SetParmeters(int webid, string tableName, string Cells, DateTime t1, DateTime t2)
        {
            SqlParameter[] parms = GetParmeters();
            parms[0].Value = webid;
            parms[1].Value = t1;
            parms[2].Value = t2;
            parms[3].Value = tableName;
            parms[4].Value = Cells;
            return parms;
        }
        #endregion

        #region：Utilities
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private List<Model.ViewDay> GetList(System.Data.DataTable table)
        {
            List<Model.ViewDay> list = new List<Model.ViewDay>();
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(GetModel(table.Rows[i]));
                }
            }
            return list;
        }
        /// <summary>
        /// 将记录集转为实体类
        /// </summary>
        /// <param name="dr">记录集</param> 
        /// <returns></returns>
        private Model.ViewDay GetModel(System.Data.DataRow dr)
        {
            Model.ViewDay model = new Model.ViewDay();

            if (dr.Table.Columns.Contains("id") && !Convert.IsDBNull(dr["id"])) model.id = Convert.ToInt32(dr["id"]); // id
            if (dr.Table.Columns.Contains("Webid") && !Convert.IsDBNull(dr["Webid"])) model.Webid = Convert.ToInt32(dr["Webid"]); // 站点ID
            if (dr.Table.Columns.Contains("pvCount") && !Convert.IsDBNull(dr["pvCount"])) model.pvCount = Convert.ToInt32(dr["pvCount"]); // PV总数
            if (dr.Table.Columns.Contains("ipCount") && !Convert.IsDBNull(dr["ipCount"])) model.ipCount = Convert.ToInt32(dr["ipCount"]); // IP总数
            if (dr.Table.Columns.Contains("cusCount") && !Convert.IsDBNull(dr["cusCount"])) model.cusCount = Convert.ToInt32(dr["cusCount"]); // 独立访客总数
            if (dr.Table.Columns.Contains("newCount") && !Convert.IsDBNull(dr["newCount"])) model.newCount = Convert.ToInt32(dr["newCount"]); // 新访客总数
            if (dr.Table.Columns.Contains("timCount") && !Convert.IsDBNull(dr["timCount"])) model.timCount = Convert.ToInt32(dr["timCount"]); // 来访次总数
            if (dr.Table.Columns.Contains("otimCount") && !Convert.IsDBNull(dr["otimCount"])) model.otimCount = Convert.ToInt32(dr["otimCount"]); // 回访次数总数
            if (dr.Table.Columns.Contains("vYear") && !Convert.IsDBNull(dr["vYear"])) model.vYear = Convert.ToInt32(dr["vYear"]); // 年
            if (dr.Table.Columns.Contains("vMonth") && !Convert.IsDBNull(dr["vMonth"])) model.vMonth = Convert.ToByte(dr["vMonth"]); // 月
            if (dr.Table.Columns.Contains("vDay") && !Convert.IsDBNull(dr["vDay"])) model.vDay = Convert.ToInt32(dr["vDay"]); // 日
            if (dr.Table.Columns.Contains("vWeek") && !Convert.IsDBNull(dr["vWeek"])) model.vWeek = Convert.ToInt32(dr["vWeek"]); // 周
            if (dr.Table.Columns.Contains("vWeekday") && !Convert.IsDBNull(dr["vWeekday"])) model.vWeekday = Convert.ToByte(dr["vWeekday"]); // 星期
            if (dr.Table.Columns.Contains("vDate") && !Convert.IsDBNull(dr["vDate"])) model.vDate = Convert.ToDateTime(dr["vDate"]); // 年月日
            if (dr.Table.Columns.Contains("IsWeek") && !Convert.IsDBNull(dr["IsWeek"])) model.IsWeek = Convert.ToInt32(dr["IsWeek"]); // 周作业是否启动完成（ 1：是）
            if (dr.Table.Columns.Contains("IsWeekDay") && !Convert.IsDBNull(dr["IsWeekDay"])) model.IsWeekDay = Convert.ToInt32(dr["IsWeekDay"]); // 是否星期作业（1：完成）
            if (dr.Table.Columns.Contains("IsMonth") && !Convert.IsDBNull(dr["IsMonth"])) model.IsMonth = Convert.ToInt32(dr["IsMonth"]); // 月作业（1：完成）
            if (dr.Table.Columns.Contains("IsYear") && !Convert.IsDBNull(dr["IsYear"])) model.IsYear = Convert.ToInt32(dr["IsYear"]); // 年作业（1：完成）
            if (dr.Table.Columns.Contains("WebName") && !Convert.IsDBNull(dr["WebName"])) model.WebName = dr["WebName"].ToString(); // WebName
            if (dr.Table.Columns.Contains("WebUrl") && !Convert.IsDBNull(dr["WebUrl"])) model.WebUrl = dr["WebUrl"].ToString(); // WebUrl

            return model;
        }

        #endregion
    }
}
