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 News : IDAL.INews
    {
        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_ID = "@id";
        private const string PARM_NUM = "@Num";

        #region Utilities
        /// <summary>
        /// 获取实体
        /// </summary>
        ///<param name="row"></param>
        /// <returns></returns>
        private Model.News GetModel(System.Data.DataRow row)
        {
            Model.News model = new Model.News();
            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="row"></param>
        /// <returns></returns>
        private Model.News GetModel(System.Data.IDataReader rdr)
        {
            Model.News model = new Model.News();
            if (rdr != null)
            {
                model.ID = SqlHelper.GetInt(rdr, "ID");
                model.title = SqlHelper.GetString(rdr, "title");
                model.Author = SqlHelper.GetString(rdr, "Author");
                model.content = SqlHelper.GetString(rdr, "content");
                model.Source = SqlHelper.GetString(rdr, "Source");
                model.IsPass = SqlHelper.GetInt(rdr, "IsPass");
                model.IsTop = SqlHelper.GetInt(rdr, "IsTop");
                model.AddTime = SqlHelper.GetUtcDateTime(rdr, "AddTime");
            }
            return model;
        }
        /// <summary>
        /// 获取 列表
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private List<Model.News> GetList(System.Data.DataTable table)
        {
            List<Model.News> list = new List<Model.News>();
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(GetModel(table.Rows[i]));
                }
            }
            return list;
        }
        /// <summary>
        /// 获取 列表
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private List<Model.News> GetList(System.Data.IDataReader rdr)
        {
            List<Model.News> list = new List<Model.News>();
            while (rdr.Read())
            {
                list.Add(GetModel(rdr));
            }
            return list;
        }

        #endregion
        #region NewMethods
        public bool Insert(Model.News model)
        {

            SqlParameter[] parms = SetParmeters(model);   

          int rInt=  SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NewsInsert", parms);

          return rInt > 0;

        }



        public bool Update(Model.News model)
        {

            SqlParameter[] parms = SetParmeters(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NewsUpdate", 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_NewsDelete", parm);
            return rInt > 0;

        }
        public IList<Model.News> Get()
        {
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NewsLoadAll", null));
        }
        public IList<Model.News> Get(int id)
        {

            SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.Int);
            parm.Value = id;
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NewsLoadById", parm));

        }
        public IList<Model.News> 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[1].Value = IsPass;
            return GetList(SqlHelper.ExecuteReader(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_NewsLoadByIspass", parms));

        }
        public IList<Model.News> 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_NewsLoadByIstop", parms));
        }

        #endregion
        #region ：Set Parmeters
        private static SqlParameter[] SetParmeters(Model.News 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 PageIndex

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

            pageModel.RecordCount = recordCount;
            return list;
        }
        #endregion
    }
}
