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>
    /// <remarks>Juju 2011-10-24</remarks>
    class ViewOnLine : IDAL.IViewOnLine
    {
        private const string PARM_WEBID = "@Webid";
        private const string PARM_CELLS = "@Cells";
        private const string PARM_T1 = "@t1";
        private const string PARM_T2 = "@t2";
        private const string PARM_TABLENAME = "@TableName";
        private const string PARM_IP = "@IP";

        #region ：Methods
        /// <summary>
        /// 获取在线情况（如用于：PV,IP,UV）
        /// </summary>
        /// <returns></returns>
        /// <remarks>Juju 2011-11-14</remarks>
        public List<Model.Online> GetPvIpUv(global::Stat.Model.StatInfo model)
        {
            model.TableName = "View_" + model.StartTime.Year + "_" + model.StartTime.Month + "_" + model.StartTime.Day;
            model.Cells = "top 10000 ip,Mac,vMinute";
            model.IsTimeslots = true;
            model.IsIp = true;
            SqlParameter[] parms = ViewItemParmeters.SetSqlParmeter(model);
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringGather(model.GatherID), CommandType.StoredProcedure, "[Stat_ViewClick_OnLine_Key_Get]", parms);
            return ViewItems.Online.GetPvIpUv(dt);
        }

        /// <summary>
        /// 获取在线用户列表
        /// </summary>
        /// <returns></returns>
        public IList<global::Stat.Model.Online> GetOnLineList(global::Stat.Model.StatInfo statModel, ref global::Stat.Model.PageInfo pageModel)
        {
            if (string.IsNullOrEmpty(statModel.IP))
            {
                return Get_OnLineList(statModel.WebID, statModel.GatherID, "IP,Area1", statModel.StartTime, statModel.EndTime);
            }
            else
            {
                return GetViewOnClick_OnLine_Key(statModel.WebID, statModel.GatherID, "IP,Area1", statModel.StartTime, statModel.EndTime, statModel.IP);
            }
        }
        /// <summary>
        /// 最近来路页面数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        /// <remarks>Juju 2011-10-24</remarks>
        public List<Model.ViewClick> GetReferer(Model.StatInfo model)
        {
            model.TableName = "View_" + model.StartTime.Year + "_" + model.StartTime.Month + "_" + model.StartTime.Day;
            model.Cells = "top 10000 IP,ViewTime,UrlPage,UrlFrom,Mac";
            model.IsIp = true;
            model.IsTimeslots = true;
            SqlParameter[] parms = ViewItemParmeters.SetSqlParmeter(model);
            return ViewItemParmeters.GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringGather(model.GatherID), CommandType.StoredProcedure, "[Stat_ViewClickLoadRefererByOnline]", parms));
        }

        /// <summary>
        /// 最近停留页面
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        ///<remarks>Juju 2011-10-24</remarks>
        public DataTable GetStayPage(Model.StatInfo model)
        {
            model.Cells = "top 10000 IP,ViewTime,UrlPage,UrlFrom";
            model.IsTimeslots = true;
            SqlParameter[] parms = ViewItemParmeters.SetSqlParmeter(model);
            return SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringGather(model.GatherID), CommandType.StoredProcedure, "Stat_ViewClickLoadPageByWebid", parms);
        }
        #endregion

        #region 私有成员

        /// <summary>
        /// 获取某站点的一段时间内在线用户IP，地址
        /// </summary>
        /// <param name="webid">站点ID</param>
        /// <param name="Cells">列的集合(ip,ipinfo)</param>
        /// <param name="t1">开始时间</param>
        /// <param name="t2">结束时间</param>
        /// <param name="ip">IP</param>
        /// <param name="key">地理位置</param>
        /// <returns></returns>
        private IList<Model.Online> GetViewOnClick_OnLine_Key(int webid, int gatherID, string Cells, DateTime t1, DateTime t2, string ip)
        {
            string tableName = "View_" + t1.Year + "_" + t1.Month + "_" + t1.Day;
            SqlParameter[] parms = SetParmeters(webid, null, Cells, t1, t2, ip);
            return ViewItems.Online.GetPvIpUv(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringGather(gatherID), CommandType.StoredProcedure, "Stat_ViewClick_OnLine_Key_Get", parms));
        }
        /// <summary>
        /// 获取某站点的一段时间内在线用户IP，地址
        /// </summary>
        /// <param name="webid">站点ID</param>
        /// <param name="Cells">列的集合(ip,ipinfo)</param>
        /// <param name="t1">开始时间</param>
        /// <param name="t2">结束时间</param>
        /// <returns></returns>
        private IList<Model.Online> Get_OnLineList(int webid, int gatherID, string Cells, DateTime t1, DateTime t2)
        {
            string tableName = "View_" + t1.Year + "_" + t1.Month + "_" + t1.Day;
            SqlParameter[] parms = SetParmeters(webid, tableName, Cells, t1, t2, null);
            return ViewItems.Online.GetPvIpUv(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringGather(gatherID), CommandType.StoredProcedure, "Stat_ViewClickLoadOnLineByWebid", parms));
        }


        #endregion

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

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