using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Stat.DBUtility;
using Stat.SQLServerDAL.CommonParmeters;
namespace Stat.SQLServerDAL
{
    /// <summary>
    /// 小时统计
    /// </summary>
    class ViewHour : IDAL.IViewHour
    {

        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";

        /// <summary>
        /// 统计表 【限今年数据】
        /// </summary>
        /// <param name="webid">站点ID</param>
        /// <param name="t1">开始时间</param>
        /// <param name="t2">结束时间</param>
        /// <returns></returns>
        public List<Model.ViewHour> GetList(int webid, DateTime t1, DateTime t2)
        {
            SqlParameter[] parms = SetParmeter(webid, t1, t2, null);

            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_ViewHourLoadByWebid", parms));
        }


        #region ：SetParmeters

        private static SqlParameter[] SetParmeter(int webid, DateTime t1, DateTime t2, string cells)
        {
            SqlParameter[] parms = GetParmeter();
            parms[0].Value = webid;
            parms[1].Value = t1;
            parms[2].Value = t2;
            parms[3].Value = "Stat_ViewHour_" + t1.Year;
            parms[4].Value = cells;
            return parms;
        }

        private static SqlParameter[] GetParmeter()
        {
            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;
        }
        #endregion


        #region

        /// <summary>
        /// 统计表【所有年份】小时 断点 之前的 数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<Model.ViewHour> GetList(global::Stat.Model.StatInfo model)
        {
            return GetViewHour(model.WebID, "pvCount,ipCount,cusCount,newCount,timCount,otimCount,vDay,vHour", model.StartTime, model.EndTime);
        }
        /// <summary>
        /// 统计表【所有年份】
        /// </summary>
        /// <remarks>By Juju 2011-09-18</remarks>
        private List<Model.ViewHour> GetViewHour(int webid, string cells, DateTime t1, DateTime t2)
        {
            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_CELLS, SqlDbType.NVarChar)
            };
            parms[0].Value = webid;
            parms[1].Value = t1;
            parms[2].Value = t2;
            parms[3].Value = cells;

            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_ViewHourLoadUnionTableByWebid", parms));

        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static List<Model.ViewHour> GetList(System.Data.DataTable table)
        {
            List<Model.ViewHour> list = new List<Model.ViewHour>();
            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 static Model.ViewHour GetModel(System.Data.DataRow dr)
        {
            Model.ViewHour model = new Model.ViewHour();
            if (dr.Table.Columns.Contains("id") && !Convert.IsDBNull(dr["id"])) model.id = Convert.ToInt32(dr["id"]); // 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.ToInt32(dr["vMonth"]); // 月
            if (dr.Table.Columns.Contains("vDay") && !Convert.IsDBNull(dr["vDay"])) model.vDay = Convert.ToInt32(dr["vDay"]); // 日
            if (dr.Table.Columns.Contains("vHour") && !Convert.IsDBNull(dr["vHour"])) model.vHour = Convert.ToInt32(dr["vHour"]); // 小时

            return model;
        }

        #region IViewOnClick 成员 ：小时统计

        public List<Model.ViewItem_Int> GetItem(Stat.Model.StatInfo model)
        {
            model.Cells = "top 30000 id,vHour,ip,mac,View_Alone";
            SqlParameter[] parms = ViewItemParmeters.SetSqlParmeter(model);
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringGather(model.GatherID), CommandType.StoredProcedure, "Stat_ViewClickLoadByWebid", parms);

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

        #endregion
    }
}
