using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Stat.DBUtility;
namespace Stat.SQLServerDAL
{
    /// <summary>
    /// 新闻公告
    /// </summary>
    /// <remarks> Juju 2011-4-19</remarks>
    class Notify : IDAL.INotify
    {
        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_NUM = "@Num";


        #region Utilities
        /// <summary>
        /// 获取实体
        /// </summary>
        ///<param name="row"></param>
        /// <returns></returns>
        private Model.Notify GetModel(System.Data.DataRow row)
        {
            Model.Notify model = new Model.Notify();
            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.Notify> GetList(System.Data.DataTable table)
        {
            List<Model.Notify> list = new List<Model.Notify>();
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(GetModel(table.Rows[i]));
                }
            }
            return list;
        }
        #endregion

        #region Methods

        /// <summary>
        /// 添加 新闻公告
        /// </summary>
        /// <returns></returns>
        public bool Insert(Model.Notify model)
        {
            SqlParameter[] parms = SetParmeters(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NotifyInsert", parms);
            return rInt > 0;
        }
        /// <summary>
        /// 修改 新闻公告
        /// </summary>
        /// <returns></returns>
        public bool Update(Model.Notify model)
        {

            SqlParameter[] parms = SetParmeters(model);

            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NotifyUpdate", 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_NotifyDelete", parm);
            return rInt > 0;
        }
        /// <summary>
        /// 获取 新闻公告
        /// </summary>
        /// <returns></returns>
        public IList<Model.Notify> Get()
        {
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NotifyLoadAll", null));
        }

        /// <summary>
        /// 获取 新闻公告
        /// </summary>
        /// <returns></returns>
        public IList<Model.Notify> Get(int id)
        {

            SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.Int);

            parm.Value = id;
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NotifyLoadById", parm));

        }
        /// <summary>
        /// 获取 新闻公告
        /// </summary>
        /// <returns></returns>
        public IList<Model.Notify> Get(int Sum, int IsPass)
        {
            SqlParameter[] parms = new SqlParameter[]{
            new SqlParameter(PARM_NUM, SqlDbType.Int),
            new SqlParameter(PARM_ISPASS, SqlDbType.Int)            
            };
            parms[0].Value = Sum;
            parms[0].Value = IsPass;
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NotifyLoadByIsPass", parms));

        }
        /// <summary>
        /// 获取 新闻公告
        /// </summary>
        /// <returns></returns>
        public IList<Model.Notify> GetTop1(int Num, int IsTop)
        {
            SqlParameter[] parms = new SqlParameter[]{
            new SqlParameter(PARM_NUM, 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_NotifyLoadByIstop", parms));
        }
        #endregion

        #region ：Set Parmeters

        private static SqlParameter[] SetParmeters(Model.Notify model)
        {
            SqlParameter[] parms = GetParmeters();
            parms[0].Value = model.title;
            parms[1].Value = model.Author;
            parms[2].Value = model.content;
            parms[3].Value = model.Source;
            parms[4].Value = model.IsPass;
            parms[5].Value = model.IsTop;
            parms[6].Value = model.ID;
            return parms;
        }

        private static SqlParameter[] GetParmeters()
        {
            SqlParameter[] parms = new SqlParameter[] { 
            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),
            new SqlParameter(PARM_ID, SqlDbType.Int)
            };
            return parms;
        }
        #endregion

        #region ：Page Index

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

            if (notieModel.ID > 0)
            {
                sqlWhere += " and id=" + notieModel.ID;
            }
            if (notieModel.title != null && notieModel.title.Length > 0)
            {
                sqlWhere += " and title like '%" + notieModel.title + "%'";
            }
            if (notieModel.Author != null && notieModel.Author.Length > 0)
            {
                sqlWhere += " and Author like '%" + notieModel.Author + "%'";
            }
            if (notieModel.IsPass != 0)
            {
                sqlWhere += " and IsPass=" + notieModel.IsPass;
            }
            if (notieModel.IsTop != 0)
            {
                sqlWhere += " and IsTop=" + notieModel.IsTop;
            }
            int recordCount = 0;
            IList<global::Stat.Model.Notify> list = GetList(JPageIndex.ProGetList.GetFromDataTable("Stat_Notify", "*", sqlWhere, "Id", pageModel.SortType, pageModel.PageSize, pageModel.PageIndex, ref recordCount, "Stat_PageIndex",SqlHelper.ConnectionStringMaster));

            pageModel.RecordCount = recordCount;
            return list;
        }

        #endregion
    }
}
