using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace Stat.SQLServerDAL.CommonParmeters
{
    /// <summary>
    /// 
    /// </summary>
    class ViewItemParmeters
    {
        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_IP = "@IP";
        private const string PARM_URL = "@Url";
        private const string PARM_TABLENAME = "@TableName";

        #region ：List<T>


        /// <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>
        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("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"]); // 搜索引擎ID
            if (dr.Table.Columns.Contains("soKeyWord") && !Convert.IsDBNull(dr["soKeyWord"])) model.EngineKeyWord = Convert.ToString(dr["soKeyWord"]).Trim(); // 关键字
            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;
        }
        #endregion

        #region：Set SqlParmeter
        /// <summary>
        /// 有(@Webid,@Cells,@TableName) 可有 @IP,@Url
        /// </summary>
        /// <param name="model"></param>
        /// <param name="Cells"></param>
        /// <returns></returns>
        internal static SqlParameter[] SetSqlParmeter(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.IsIp)
            {
                SqlParameter[] newParms;
                int parmsCount = parms.Length + 1;
                newParms = (SqlParameter[])Redim(parms, parmsCount);
                newParms[parmsCount - 1] = new SqlParameter(PARM_IP, SqlDbType.NVarChar);
                newParms[parmsCount - 1].Value = model.IP;
                parms = newParms;
            }
            if (model.IsLastUrl)
            {
                SqlParameter[] newParms;
                int parmsCount = parms.Length + 1;
                newParms = (SqlParameter[])Redim(parms, parmsCount);
                newParms[parmsCount - 1] = new SqlParameter(PARM_URL, SqlDbType.NVarChar);
                newParms[parmsCount - 1].Value = model.LastUrl;
                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
        /// <summary>
        /// 
        /// </summary>
        /// <param name="origArray"></param>
        /// <param name="desiredsize"></param>
        /// <returns></returns>
        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;
        }
    }
}
