using System;
using System.Collections.Generic;

namespace Stat.SQLServerDAL
{
    public class StatItem
    {
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        internal static List<Model.StatItem> GetList(System.Data.DataTable table, string cellName,int type)
        {
            List<Model.StatItem> list = new List<Model.StatItem>();
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(GetModel(table.Rows[i], cellName, type));
                }
            }
            return list;
        }
        /// <summary>
        /// 将记录集转为实体类
        /// </summary>
        /// <param name="dr">记录集</param> 
        /// <param name="type">0：int，1：String</param>
        /// <returns></returns>
        private static Model.StatItem GetModel(System.Data.DataRow dr, string cellName, int type)
        {
            Model.StatItem nc = new Model.StatItem();
            if (type == 0)
            {
                if (dr.Table.Columns.Contains(cellName) && !Convert.IsDBNull(dr[cellName])) nc.ID = Convert.ToInt32(dr[cellName]); //CellName
            }
            else
            {
                if (dr.Table.Columns.Contains(cellName) && !Convert.IsDBNull(dr[cellName])) nc.Name = dr[cellName].ToString(); // Name
            }
            if (dr.Table.Columns.Contains("pvCount") && !Convert.IsDBNull(dr["PvCount"])) nc.PvCount = Convert.ToInt32(dr["PvCount"]); // PvCount
            if (dr.Table.Columns.Contains("IpCount") && !Convert.IsDBNull(dr["IpCount"])) nc.IpCount = Convert.ToInt32(dr["IpCount"]); // IpCount
            if (dr.Table.Columns.Contains("UvCount") && !Convert.IsDBNull(dr["UvCount"])) nc.UvCount = Convert.ToInt32(dr["UvCount"]); // UvCount
            if (dr.Table.Columns.Contains("NuvCount") && !Convert.IsDBNull(dr["NuvCount"])) nc.NuvCount = Convert.ToInt32(dr["NuvCount"]); // NuvCount
            if (dr.Table.Columns.Contains("TimCount") && !Convert.IsDBNull(dr["TimCount"])) nc.TimCount = Convert.ToInt32(dr["TimCount"]); // TimCount
            if (dr.Table.Columns.Contains("OtimCount") && !Convert.IsDBNull(dr["OtimCount"])) nc.OtimCount = Convert.ToInt32(dr["OtimCount"]); // OtimCount
            if (dr.Table.Columns.Contains("KeyCount") && !Convert.IsDBNull(dr["KeyCount"])) nc.KeyCount = Convert.ToInt32(dr["KeyCount"]); // KeyCount
            if (dr.Table.Columns.Contains("Soid") && !Convert.IsDBNull(dr["Soid"])) nc.EngineID = Convert.ToInt32(dr["Soid"]); // Soid
            if (dr.Table.Columns.Contains("soKeyWord") && !Convert.IsDBNull(dr["soKeyWord"])) nc.EngineKeyword = dr["soKeyWord"].ToString(); // EngineKeyword
            if (dr.Table.Columns.Contains("PerPV") && !Convert.IsDBNull(dr["PerPV"])) nc.PerPV = Convert.ToDouble(dr["PerPV"]); // PerPV
            if (dr.Table.Columns.Contains("Percent") && !Convert.IsDBNull(dr["Percent"])) nc.Percent = Convert.ToDouble(dr["Percent"]); // Percent
            if (dr.Table.Columns.Contains("Vyear") && !Convert.IsDBNull(dr["Vyear"])) nc.Vyear = Convert.ToInt32(dr["Vyear"]); // Vyear
            if (dr.Table.Columns.Contains("Vmonth") && !Convert.IsDBNull(dr["Vmonth"])) nc.Vmonth = Convert.ToInt32(dr["Vmonth"]); // Vmonth
            if (dr.Table.Columns.Contains("Vday") && !Convert.IsDBNull(dr["Vday"])) nc.Vday = Convert.ToInt32(dr["Vday"]); // Vday
            return nc;
        }
    }
}
