using System;
using System.Collections.Generic;
using System.Data;
using Stat.DBUtility;
namespace Stat.SQLServerDAL
{
    /// <summary>
    /// 分页
    /// </summary>
    class ViewOnClickPageIndex : IDAL.IViewOnClickPageIndex
    {

        #region IViewOnClickPageIndex 成员


        public List<Model.ViewClick> GetList(ref Stat.Model.PageInfo pageInfo, Stat.Model.SearchInfo searchInfo)
        {
            string tableName = "View_" + searchInfo.StartTime.Year + "_" + searchInfo.StartTime.Month + "_" + searchInfo.StartTime.Day;

            string sqlWhere = string.Empty;
            sqlWhere += "Webid=" + searchInfo.WebID;
            //
            if (searchInfo.IP != null && searchInfo.IP.Length > 0) { sqlWhere += " and IP = '" + searchInfo.IP + "'"; }
            //
            if (searchInfo.BrowserID != 0) { sqlWhere += " and BranId = " + searchInfo.BrowserID + ""; }
            if (searchInfo.OperatingSystemId != 0) { sqlWhere += " and OprId = " + searchInfo.OperatingSystemId + ""; }
            if (searchInfo.ResolutionId != 0) { sqlWhere += " and PxId = " + searchInfo.ResolutionId + ""; }
            if (searchInfo.LanguageId != 0) { sqlWhere += " and Lang = " + searchInfo.LanguageId + ""; }
            if (searchInfo.ColorDepthId != 0) { sqlWhere += " and ColorDepth = " + searchInfo.ColorDepthId + ""; }
            if (searchInfo.NetClrVersionId != 0) { sqlWhere += " and NetFW = " + searchInfo.NetClrVersionId + ""; }
            //
            if (searchInfo.IsJava > -1) { sqlWhere += " and IsJava = " + searchInfo.IsJava + ""; }
            if (searchInfo.IsAlexa > -1) { sqlWhere += " and IsAlexa = " + searchInfo.IsAlexa + ""; }
            //
            if (searchInfo.CoId > 0) { sqlWhere += " and CoID = " + searchInfo.CoId + ""; }
            if (searchInfo.CtId > 0) { sqlWhere += " and CtID = " + searchInfo.CtId + ""; }
            if (searchInfo.View_Alone > 0) { sqlWhere += " and View_Alone = " + searchInfo.View_Alone + ""; }
            //  
            int recordCount = 0;
            DataTable dataTable = JPageIndex.ProGetList.GetFromDataTable(tableName, "id,IP,Mac,ViewTime,UrlPage,UrlFrom,View_Alone", sqlWhere, "id", 0, pageInfo.PageSize, pageInfo.PageIndex, ref recordCount, "Stat_PageIndex", SqlHelper.ConnectionStringGather(searchInfo.GatherID));
            pageInfo.RecordCount = recordCount;
            //排序
            if (dataTable != null)
            {
                DataView dv = dataTable.DefaultView;
                dv.Sort = "Mac asc,ViewTime asc";
                dataTable = dv.ToTable();
            }
            //
            List<Model.ViewClick> list = GetList(dataTable);
            return list;
        }

        #endregion


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        internal static List<Model.ViewClick> GetList(System.Data.DataTable table)
        {
            List<Model.ViewClick> list = new List<Model.ViewClick>();
            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>
        /// <remarks>Juju 2011-10-25</remarks>
        private static Model.ViewClick GetModel(System.Data.DataRow dr)
        {
            Model.ViewClick model = new Model.ViewClick();

            if (dr.Table.Columns.Contains("id") && !Convert.IsDBNull(dr["id"])) model.id = Convert.ToInt64(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("IP") && !Convert.IsDBNull(dr["IP"])) model.IP = Convert.ToString(dr["IP"]).Trim(); // IP地址
            if (dr.Table.Columns.Contains("IPno") && !Convert.IsDBNull(dr["IPno"])) model.IPno = Convert.ToInt64(dr["IPno"]); // IP范围ID
            if (dr.Table.Columns.Contains("UrlFrom") && !Convert.IsDBNull(dr["UrlFrom"])) model.LastUrl = Convert.ToString(dr["UrlFrom"]).Trim(); // 来源地址
            if (dr.Table.Columns.Contains("UrlPage") && !Convert.IsDBNull(dr["UrlPage"])) model.CurrentUrl = Convert.ToString(dr["UrlPage"]).Trim(); // 访问页面
            if (dr.Table.Columns.Contains("domain") && !Convert.IsDBNull(dr["domain"])) model.domain = Convert.ToString(dr["domain"]).Trim(); // 来源域名
            if (dr.Table.Columns.Contains("OprId") && !Convert.IsDBNull(dr["OprId"])) model.OperatingSystemId = Convert.ToInt32(dr["OprId"]); // 操作系统ID
            if (dr.Table.Columns.Contains("BranId") && !Convert.IsDBNull(dr["BranId"])) model.BrowserId = Convert.ToInt32(dr["BranId"]); // 浏览器ID
            if (dr.Table.Columns.Contains("PxId") && !Convert.IsDBNull(dr["PxId"])) model.ResolutionId = Convert.ToInt32(dr["PxId"]); // 屏幕大小ID
            //
            if (dr.Table.Columns.Contains("SoId") && !Convert.IsDBNull(dr["SoId"])) model.EngineId = Convert.ToInt32(dr["SoId"]); // 搜索引擎ID1
            else if (dr.Table.Columns.Contains("Egid") && !Convert.IsDBNull(dr["Egid"])) model.EngineId = Convert.ToInt32(dr["Egid"]); // 搜索引擎ID2
            //
            if (dr.Table.Columns.Contains("soKeyWord") && !Convert.IsDBNull(dr["soKeyWord"])) model.EngineKeyWord = Convert.ToString(dr["soKeyWord"]).Trim(); // 关键字1
            else if (dr.Table.Columns.Contains("EgKeyword") && !Convert.IsDBNull(dr["EgKeyword"])) model.EngineKeyWord = Convert.ToString(dr["EgKeyword"]).Trim(); // 关键字2
            //
            if (dr.Table.Columns.Contains("Mac") && !Convert.IsDBNull(dr["Mac"])) model.Mac = Convert.ToString(dr["Mac"]).Trim(); // Cookies值，识别独立访客
            if (dr.Table.Columns.Contains("View_Alone") && !Convert.IsDBNull(dr["View_Alone"])) model.View_Alone = Convert.ToInt32(dr["View_Alone"]); // 来访次数（0：新访客）
            if (dr.Table.Columns.Contains("NetFW") && !Convert.IsDBNull(dr["NetFW"])) model.NetClrVersionId = Convert.ToInt32(dr["NetFW"]); // .net 版本
            if (dr.Table.Columns.Contains("Lang") && !Convert.IsDBNull(dr["Lang"])) model.LanguageId = Convert.ToInt32(dr["Lang"]); // 语言
            if (dr.Table.Columns.Contains("ColorDepth") && !Convert.IsDBNull(dr["ColorDepth"])) model.ColorDepthId = Convert.ToInt32(dr["ColorDepth"]); // 颜色质量
            if (dr.Table.Columns.Contains("IsCookies") && !Convert.IsDBNull(dr["IsCookies"])) model.IsCookies = Convert.ToInt32(dr["IsCookies"]); // IsCookies
            if (dr.Table.Columns.Contains("IsJava") && !Convert.IsDBNull(dr["IsJava"])) model.IsJava = Convert.ToInt32(dr["IsJava"]); // 是否支持JavaApplets（1：支持）
            if (dr.Table.Columns.Contains("IsJavaScript") && !Convert.IsDBNull(dr["IsJavaScript"])) model.IsJavaScript = Convert.ToInt32(dr["IsJavaScript"]); // IsJavaScript
            if (dr.Table.Columns.Contains("IsAlexa") && !Convert.IsDBNull(dr["IsAlexa"])) model.IsAlexa = Convert.ToInt32(dr["IsAlexa"]); // 是否已安装Alexa（1：已安装）
            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("vWeek") && !Convert.IsDBNull(dr["vWeek"])) model.VWeek = Convert.ToInt32(dr["vWeek"]); // 周
            if (dr.Table.Columns.Contains("vWork") && !Convert.IsDBNull(dr["vWork"])) model.VWork = Convert.ToByte(dr["vWork"]); // 星期
            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.ToByte(dr["vHour"]); // 小时
            if (dr.Table.Columns.Contains("vMinute") && !Convert.IsDBNull(dr["vMinute"])) model.VMinute = Convert.ToInt32(dr["vMinute"]); // 分钟
            if (dr.Table.Columns.Contains("ViewTime") && !Convert.IsDBNull(dr["ViewTime"])) model.ViewTime = Convert.ToDateTime(dr["ViewTime"]); // 来访时间
            if (dr.Table.Columns.Contains("vLeaveTime") && !Convert.IsDBNull(dr["vLeaveTime"])) model.VLeaveTime = Convert.ToDateTime(dr["vLeaveTime"]); // 离开时间
            if (dr.Table.Columns.Contains("CoID") && !Convert.IsDBNull(dr["CoID"])) model.CoId = Convert.ToInt32(dr["CoID"]); // CoID
            if (dr.Table.Columns.Contains("CtId") && !Convert.IsDBNull(dr["CtId"])) model.CtId = Convert.ToInt32(dr["CtId"]); // CtId
            if (dr.Table.Columns.Contains("Area1") && !Convert.IsDBNull(dr["Area1"])) model.AreaId1 = Convert.ToInt32(dr["Area1"]); // Area1
            //
            return model;
        }
    }
}
