using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Stat.DBUtility;
namespace Stat.SQLServerDAL
{
    class Log : IDAL.ILog
    {
        #region Utllities

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private List<Model.Log> GetList(System.Data.IDataReader table)
        {
            List<Model.Log> list = new List<Model.Log>();
            while (table.Read())
            {
                list.Add(GetModel(table));
            }
            return list;
        }

        /// <summary>
        /// 将记录集转为实体类
        /// </summary>
        /// <param name="dr">记录集</param> 
        /// <returns>Model.Log</returns>
        private Model.Log GetModel(System.Data.IDataReader rdr)
        {
            Model.Log model = new Model.Log();
            model.ID =SqlHelper.GetInt(rdr, "ID"); // ID
            model.Name = Convert.ToString(rdr["name"]).Trim(); // name
            model.Value = Convert.ToString(rdr["value"]).Trim(); // value
            model.Type = SqlHelper.GetInt(rdr, "Type"); // Type
            model.Webid = SqlHelper.GetInt(rdr, "webid"); // webid
            model.UserID = SqlHelper.GetInt(rdr, "userID"); // userID
            model.AddTime = SqlHelper.GetUtcDateTime(rdr, "AddTime"); // AddTime
            model.Expansion = SqlHelper.GetString(rdr, "expansion").Trim(); // expansion
            return model;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private List<Model.Log> GetList(System.Data.DataTable table)
        {
            List<Model.Log> list = new List<Model.Log>();
            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>Model.Log</returns>
        private Model.Log GetModel(System.Data.DataRow dr)
        {
            Model.Log model = new Model.Log();

            if (dr.Table.Columns.Contains("ID") && !Convert.IsDBNull(dr["ID"])) model.ID = Convert.ToInt32(dr["ID"]); // ID
            if (dr.Table.Columns.Contains("name") && !Convert.IsDBNull(dr["name"])) model.Name = Convert.ToString(dr["name"]).Trim(); // name
            if (dr.Table.Columns.Contains("value") && !Convert.IsDBNull(dr["value"])) model.Value = Convert.ToString(dr["value"]).Trim(); // value
            if (dr.Table.Columns.Contains("Type") && !Convert.IsDBNull(dr["Type"])) model.Type = Convert.ToInt32(dr["Type"]); // Type
            if (dr.Table.Columns.Contains("webid") && !Convert.IsDBNull(dr["webid"])) model.Webid = Convert.ToInt32(dr["webid"]); // webid
            if (dr.Table.Columns.Contains("userID") && !Convert.IsDBNull(dr["userID"])) model.UserID = Convert.ToInt32(dr["userID"]); // userID
            if (dr.Table.Columns.Contains("AddTime") && !Convert.IsDBNull(dr["AddTime"])) model.AddTime = Convert.ToDateTime(dr["AddTime"]); // AddTime
            if (dr.Table.Columns.Contains("expansion") && !Convert.IsDBNull(dr["expansion"])) model.Expansion = Convert.ToString(dr["expansion"]).Trim(); // expansion
            return model;
        }


        #endregion
        #region Methods
        /// <summary>
        /// 新增/删除/修改
        /// </summary>
        /// <param name="model">实体类</param>
        /// <returns>-1:存储过程执行失败,-2:存在相同的主键,Insert:返回插入自动ID,Update:返回更新记录数,Delete:返回删除记录数</returns>
        public Int32 InsertUpdateDelete(Model.Log model)
        {
            SqlParameter[] parms = SetParmeters(model);    
            return SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_Log_InsertUpdateDelete", parms);
        }
        #endregion
        #region ：Set Parmeters
        private static SqlParameter[] SetParmeters(Model.Log model)
        {
            SqlParameter[] parms = GetParmeters();
            parms[0].Value = model.DbAction;
            parms[1].Value = model.ID;  //ID
            parms[2].Value = model.Name;  //name
            parms[3].Value = model.Value;  //value
            parms[4].Value = model.Type;  //Type
            parms[5].Value = model.Webid;  //webid
            parms[6].Value = model.UserID;  //userID
            parms[7].Value = model.Expansion;  //expansion

            return parms;
        }

        private static SqlParameter[] GetParmeters()
        {
            SqlParameter[] parms = new SqlParameter[] { 
            new SqlParameter("@DataTable_Action_", SqlDbType.VarChar),
             new SqlParameter("@ID", SqlDbType.Int),
              new SqlParameter("@name", SqlDbType.NVarChar),
               new SqlParameter("@value", SqlDbType.NText),
                new SqlParameter("@Type", SqlDbType.Int),
                 new SqlParameter("@webid", SqlDbType.Int),
                  new SqlParameter("@userID", SqlDbType.Int),
                   new SqlParameter("@expansion", SqlDbType.NText) 
            };
            return parms;
        }

        #endregion
        #region：Page Index

        public IList<Stat.Model.Log> Get(Stat.Model.Log logModel, ref Stat.Model.PageInfo pageModel)
        {
            string sqlWhere = "1=1";

            if (logModel.ID > 0)
            {
                sqlWhere += " And ID = " + logModel.ID + "";
            }

            if (logModel.Name.Length > 0)
            {
                sqlWhere += " And Name like '%" + logModel.Name + "%'";
            }
            if (logModel.Value.Length > 0)
            {
                sqlWhere += " And Value like '%" + logModel.Value + "%'";
            }
            if (logModel.Type > 0)
            {
                sqlWhere += " And Type = " + logModel.Type + "";
            }
            int recordCount = 0;
            IList<global::Stat.Model.Log> dt = GetList(JPageIndex.ProGetList.GetFromDataTable("Stat_Log", "*", sqlWhere, "Id", 1, pageModel.PageSize, pageModel.PageIndex, ref recordCount, "Stat_PageIndex",SqlHelper.ConnectionStringMaster));
            pageModel.RecordCount = recordCount;

            return dt;
        }

        #endregion
    }
}
