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-26</remarks>
    class ViewEngine : IDAL.IViewEngine
    {
        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_EGID = "@EgID";
        private const string PARM_EKEY = "@eKey";
        private const string PARM_TABLENAME = "@TableName";

        #region ： Methods

        public List<Model.StatItem> GetList(global::Stat.Model.StatInfo model)
        {
            model.Cells = "Soid,soKeyWord,pvCount,ipCount,uvCount";
            model.IsTimeslots = true;
            model.IsEngine = true;
            model.IsEngineKey = true;
            return StatItem.GetList(GetListFromReport(model), "CELLNAME1212", 0);
        }

        public List<Model.ViewItem_Engine> GetItem(Stat.Model.StatInfo model)
        {
            model.Cells = "id,SoID,soKeyWord,ip,mac";
            model.TableName = "View_" + model.StartTime.Year + "_" + model.StartTime.Month + "_" + model.StartTime.Day;
            model.IsTimeslots = false;
            model.IsEngine = true;
            model.IsEngineKey = true;
            return ViewItems.Engine.GetList(Exact_Key(model));
        }

        /// <summary>
        /// 读取 主服务器 数据库
        /// </summary>
        /// <returns></returns>
        private DataTable GetListFromReport(Stat.Model.StatInfo model)
        {
            SqlParameter[] parms = SetParmeters(model);
            return SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_ViewEngineLoadAllByWebid", parms);
        }
        /// <summary>
        /// 读取 采集 服务器 数据库
        /// </summary>
        /// <remarks>Juju 2011-10-24</remarks>
        private List<Model.ViewClick> Exact_Key(Stat.Model.StatInfo model)
        {
            SqlParameter[] parms = SetParmeters(model);
            return ViewItemParmeters.GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringGather(model.GatherID), CommandType.StoredProcedure, "Stat_ViewClickLoadEngineByWebid", parms));
        }
        #endregion

        #region ：Set Parmeters

        private static SqlParameter[] SetParmeters(Stat.Model.StatInfo model)
        {
            SqlParameter[] parms = new SqlParameter[] { 
            new SqlParameter(PARM_WEBID, SqlDbType.Int),
            new SqlParameter(PARM_CELLS, SqlDbType.NVarChar),   
            new SqlParameter(PARM_TABLENAME, SqlDbType.NVarChar)
            };
            parms[0].Value = model.WebID;
            parms[1].Value = model.Cells;
            parms[2].Value = model.TableName;
            //指定搜索引擎
            if (model.IsEngine)
            {
                SqlParameter[] newParms;
                int parmsCount = parms.Length + 1;
                newParms = (SqlParameter[])Redim(parms, parmsCount);
                newParms[parmsCount - 1] = new SqlParameter(PARM_EGID, SqlDbType.Int);
                newParms[parmsCount - 1].Value = model.EngineId;
                parms = newParms;
            }
            //指定关键字
            if (model.IsEngineKey)
            {
                SqlParameter[] newParms;
                int parmsCount = parms.Length + 1;
                newParms = (SqlParameter[])Redim(parms, parmsCount);
                newParms[parmsCount - 1] = new SqlParameter(PARM_EKEY, SqlDbType.NVarChar);
                newParms[parmsCount - 1].Value = model.EngineKeyWord;
                parms = newParms;
            }
            //指定时间段参数
            if (model.IsTimeslots)
            {
                SqlParameter[] newParms;
                int parmsCount = parms.Length + 2;
                newParms = (SqlParameter[])Redim(parms, parmsCount);
                newParms[parmsCount - 2] = new SqlParameter(PARM_T1, SqlDbType.DateTime);
                newParms[parmsCount - 1] = new SqlParameter(PARM_T2, SqlDbType.DateTime);
                newParms[parmsCount - 2].Value = model.StartTime;
                newParms[parmsCount - 1].Value = model.EndTime;
                parms = newParms;
            }
            return parms;
        }
        #endregion

        private static Array Redim(Array origArray, Int32 desiredsize)
        {
            //确定每个元素类型    
            Type t = origArray.GetType().GetElementType();
            //创建一个含有期望元素个数的新数组  
            //新数组的类型必须匹配原数组的类型  
            Array newArray = Array.CreateInstance(t, desiredsize);
            //将原数组中的元素拷贝到新数组中  
            Array.Copy(origArray, 0, newArray, 0, Math.Min(origArray.Length, desiredsize));
            return newArray;
        }

        #region IViewEngine 成员

        /// <summary>
        /// 未开发的
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<Model.ViewClick> GetView(Model.StatInfo model)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IViewEngine 成员

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pageInfo"></param>
        /// <param name="searchInfo"></param>
        /// <returns></returns>
        /// <remarks>Juju 2011-11-14</remarks>
        public List<Model.ViewClick> GetDetailFromGether(ref Model.PageInfo pageInfo, Model.SearchInfo searchInfo)
        {
            string tableName = "View_" + searchInfo.StartTime.Year + "_" + searchInfo.StartTime.Month + "_" + searchInfo.StartTime.Day;

            string sqlWhere = string.Empty;
            sqlWhere += " SoId>0 and LEN(soKeyWord)>0";
            sqlWhere += " and Webid=" + searchInfo.WebID;
            if (searchInfo.EngineID > 0)
            {
                sqlWhere += " and SoId=" + searchInfo.EngineID;
            }
            if (!string.IsNullOrEmpty(searchInfo.KeyWord))
            {
                sqlWhere += " and soKeyWord LIKE '%" + searchInfo.KeyWord + "%'";
            }
            int recordCount = 0;

            DataTable dataTable = JPageIndex.ProGetList.GetFromDataTable(tableName, "id,SoId,soKeyWord,IP,ViewTime,UrlPage,UrlFrom,Area1", sqlWhere, "id", 0, pageInfo.PageSize, pageInfo.PageIndex, ref recordCount, "Stat_PageIndex", SqlHelper.ConnectionStringGather(searchInfo.GatherID));

            pageInfo.RecordCount = recordCount;
            return ViewOnClickPageIndex.GetList(dataTable);
        }
        #endregion

        #region IViewEngine 成员


        public List<Model.ViewClick> GetDetailFromMaster(ref Model.PageInfo pageInfo, Model.SearchInfo searchInfo)
        {
            string tableName = "Stat_EngineDetail_" + searchInfo.StartTime.Year;
            string sqlWhere = string.Empty;
            sqlWhere += " Egid>0 and LEN(EgKeyword)>0";
            sqlWhere += " and (ViewTime between '" + searchInfo.StartTime + "' and '" + searchInfo.EndTime + "')";
            sqlWhere += " and Webid=" + searchInfo.WebID;
            if (searchInfo.EngineID > 0)
            {
                sqlWhere += " and Egid=" + searchInfo.EngineID;
            }
            if (!string.IsNullOrEmpty(searchInfo.KeyWord))
            {
                sqlWhere += " and EgKeyword LIKE '%" + searchInfo.KeyWord + "%'";
            }
            int recordCount = 0;
            DataTable dataTable = JPageIndex.ProGetList.GetFromDataTable(tableName, "id,Egid,EgKeyword,IP,ViewTime,UrlPage,UrlFrom,Area1", sqlWhere, "id", 0, pageInfo.PageSize, pageInfo.PageIndex, ref recordCount, "Stat_PageIndex", SqlHelper.ConnectionStringMaster);
            pageInfo.RecordCount = recordCount;
            return ViewOnClickPageIndex.GetList(dataTable);
        }

        #endregion
    }
}
