using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Stat.DBUtility;
namespace Stat.SQLServerDAL
{
    /// <summary>
    /// 广告
    /// </summary>
    class AD : IDAL.IAD
    {
        private const string PARM_ID = "@id";
        private const string PARM_TITLE = "@Title";
        private const string PARM_CODE = "@Code";
        private const string PARM_POSIT = "@Posit";
        private const string PARM_STATE = "@State";
        #region ：Methods

        public bool Insert(Model.AD model)
        {
            SqlParameter[] parms = SetSqlParmeter(model);  
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_ADInsert", parms);
            return rInt > 0;
        }
        
        public bool Update(Model.AD model)
        {
            SqlParameter[] parms = SetSqlParmeter(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_ADUpdate", parms);
            return rInt > 0;
        }
        public bool Delete(int id)
        {
            SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.Int);
            parm.Value = id;
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_ADDelete", parm);
            return rInt > 0;
        }
        public IList<Model.AD> Get()
        {
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_ADLoadAll", null));
        }
        public IList<Model.AD> Get(int id)
        {
            SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.Int);
            parm.Value = id;
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_ADLoadByID", parm));
        }
        public IList<Model.AD> GetPosit(int Posit)
        {
            SqlParameter parm = new SqlParameter(PARM_POSIT, SqlDbType.Int);
            parm.Value = Posit;
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_ADLoadByPosit", parm));
        }
        #endregion
        
        #region ：Utilities
        /// <summary>
        /// 获取一条广告实体
        /// </summary>
        ///<param name="row"></param>
        /// <returns></returns>
        private Model.AD GetModel(System.Data.DataRow row)
        {
            Model.AD model = new Model.AD();
            if (row != null)
            {
                if (row["ID"] != DBNull.Value)
                    model.ID = Convert.ToInt32(row["ID"]);
                if (row["Title"] != DBNull.Value)
                    model.Title = row["Title"].ToString();
                if (row["Code"] != DBNull.Value)
                    model.Code = row["Code"].ToString();
                if (row["State"] != DBNull.Value)
                    model.State = Convert.ToInt32(row["State"]);
                if (row["Posit"] != DBNull.Value)
                    model.Posit = Convert.ToInt32(row["Posit"]);
            }
            return model;
        }
        /// <summary>
        /// 获取 广告 列表
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private IList<Model.AD> GetList(System.Data.DataTable table)
        {
            List<Model.AD> list = new List<Model.AD>();
            if (table != null && table.Rows.Count > 0)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(GetModel(table.Rows[i]));
                }
            }
            return list;
        }
        #endregion


        #region ：Set SqlParmeter
        private static SqlParameter[] SetSqlParmeter(Model.AD model)
        {
            SqlParameter[] parms = GetSqlParmeter();
            parms[0].Value = model.ID;
            parms[1].Value = model.Title;
            parms[2].Value = model.Code;
            parms[3].Value = model.Posit;
            parms[4].Value = model.State;

            return parms;
        }

        private static SqlParameter[] GetSqlParmeter()
        {
            SqlParameter[] parms = new SqlParameter[]{
                 new SqlParameter(PARM_ID, SqlDbType.Int),
                 new SqlParameter(PARM_TITLE, SqlDbType.NVarChar),
                 new SqlParameter(PARM_CODE, SqlDbType.NVarChar),
                 new SqlParameter(PARM_POSIT, SqlDbType.Int),
                 new SqlParameter(PARM_STATE, SqlDbType.Int)
            };
            return parms;
        }

        #endregion
    }
}