using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Stat.DBUtility;
namespace Stat.SQLServerDAL
{
    /// <summary>
    /// 
    /// </summary>
    class Navigate : IDAL.INavigate
    {
        private const string PARM_ID = "@id";
        private const string PARM_TITLE = "@title";
        private const string PARM_AUTHOR = "@Author";
        private const string PARM_CONTENT = "@content";
        private const string PARM_SOURCE = "@Source";
        private const string PARM_ISPASS = "@IsPass";
        private const string PARM_ISTOP = "@IsTop";
        private const string PARM_SUM = "@Sum";


        #region Methods
        /// <summary>
        /// 添加 栏目导航
        /// </summary>
        /// <returns></returns>
        public bool Insert(Model.Navigate model)
        {
            SqlParameter[] parms = SetSqlParmeter(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NavigateInsert", parms);
            return rInt > 0;
        }

        /// <summary>
        /// 修改 栏目导航
        /// </summary>
        /// <returns></returns>
        public bool Update(Model.Navigate model)
        {
            SqlParameter[] parms = SetSqlParmeter(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NavigateUpdate", parms);

            return rInt > 0;
        }
        /// <summary>
        ///删除 栏目导航
        /// </summary>
        /// <returns></returns>
        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_NavigateDelete", parm);
            return rInt > 0;
        }
        /// <summary>
        /// 获取 栏目导航
        /// </summary>
        /// <returns></returns>
        public IList<Model.Navigate> Get()
        {
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NavigateLoadAll", null));

        }
        /// <summary>
        /// 获取 栏目导航
        /// </summary>
        /// <returns></returns>
        public IList<Model.Navigate> Get(int id)
        {
            SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.Int);
            parm.Value = id;
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NavigateLoadById", parm));
        }
        /// <summary>
        /// 获取 栏目导航
        /// </summary>
        /// <returns></returns>
        public IList<Model.Navigate> Get(int Sum, int IsPass)
        {
            SqlParameter[] parms = new SqlParameter[]{
                    new SqlParameter(PARM_SUM, SqlDbType.Int),
                    new SqlParameter(PARM_ISPASS, SqlDbType.Int)
            };
            parms[0].Value = Sum;
            parms[1].Value = IsPass;
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NavigateLoadByIsPass", parms));
        }
        /// <summary>
        /// 获取 栏目导航
        /// </summary>
        /// <returns></returns>
        public IList<Model.Navigate> GetTop1(int Num, int IsTop)
        {
            SqlParameter[] parms = new SqlParameter[]{
                    new SqlParameter(PARM_SUM, SqlDbType.Int),
                    new SqlParameter(PARM_ISTOP, SqlDbType.Int)
            };
            parms[0].Value = Num;
            parms[1].Value = IsTop;

            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_MessageLoadByIstop", parms));

        }
        #endregion

        #region Utilities
        /// <summary>
        /// 获取实体
        /// </summary>
        ///<param name="row"></param>
        /// <returns></returns>
        private Model.Navigate GetModel(System.Data.DataRow row)
        {
            Model.Navigate model = new Model.Navigate();
            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["Author"] != DBNull.Value)
                    model.Author = row["Author"].ToString();
                if (row["content"] != DBNull.Value)
                    model.content = row["content"].ToString();
                if (row["Source"] != DBNull.Value)
                    model.Source = row["Source"].ToString();
                if (row["IsPass"] != DBNull.Value)
                    model.IsPass = Convert.ToInt32(row["IsPass"]);
                if (row["IsTop"] != DBNull.Value)
                    model.IsTop = Convert.ToInt32(row["IsTop"]);
                if (row["AddTime"] != DBNull.Value)
                    model.AddTime = Convert.ToDateTime(row["AddTime"]);
            }
            return model;
        }
        /// <summary>
        /// 获取 列表
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private List<Model.Navigate> GetList(System.Data.DataTable table)
        {
            List<Model.Navigate> list = new List<Model.Navigate>();
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(GetModel(table.Rows[i]));
                }
            }
            return list;
        }
        #endregion


        #region ：Set SqlParmeter
        private static SqlParameter[] GetSqlParmeter()
        {
            SqlParameter[] parms = new SqlParameter[]{
               new SqlParameter(PARM_ID, SqlDbType.Int),
               new SqlParameter(PARM_TITLE, SqlDbType.NVarChar),
               new SqlParameter(PARM_AUTHOR, SqlDbType.NVarChar),
               new SqlParameter(PARM_CONTENT, SqlDbType.NVarChar),
               new SqlParameter(PARM_SOURCE, SqlDbType.NVarChar),
               new SqlParameter(PARM_ISPASS, SqlDbType.Int),
               new SqlParameter(PARM_ISTOP, SqlDbType.Int)
        };
            return parms;
        }

        private static SqlParameter[] SetSqlParmeter(Model.Navigate model)
        {
            SqlParameter[] parms = GetSqlParmeter();
            parms[0].Value = model.ID;
            parms[1].Value = model.title;
            parms[2].Value = model.Author;
            parms[3].Value = model.content;
            parms[4].Value = model.Source;
            parms[5].Value = model.IsPass;
            parms[6].Value = model.IsTop;

            return parms;
        }
        #endregion
    }
}
