using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Stat.DBUtility;
namespace Stat.SQLServerDAL
{
    /// <summary>
    /// 邮件
    /// </summary>
    /// <remarks>Juju 2011-3-20</remarks>
    class Stmp : IDAL.IStmp
    {
        private const string PARM_ID = "@ID";
        private const string PARM_MAIL_NAME = "@MailName";
        private const string PARM_MAIL_PASS = "@MailPass";
        private const string PARM_MAIL_STMP = "@MailSmtp";
        private const string PARM_MAIL_CONTENT = "@MailContent";
        private const string PARM_STATE = "@State";
        private const string PARM_EXPANSION = "@expansion";

        #region Methods
        /// <summary>
        /// 添加STMP信息
        /// </summary>
        /// <returns></returns>
        public bool Insert(Model.Stmp model)
        {
            SqlParameter[] parms = SetSqlParmeter(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_StmpInsert", parms);
            return rInt > 0;
        }


        /// <summary>
        /// 更新STMP信息
        /// </summary>
        /// <returns></returns>
        public bool Update(Model.Stmp model)
        {
            SqlParameter[] parms = SetSqlParmeter(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_StmpUpdate", parms);
            return rInt > 0;
        }
        /// <summary>
        ///删除STMP信息
        /// </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_StmpDelete", parm);
            return rInt > 0;

        }
        /// <summary>
        /// 获取STMP信息
        /// </summary>
        /// <returns></returns>
        public IList<Model.Stmp> Get(int id)
        {
            SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.Int);
            parm.Value = id;
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_StmpLoadById", parm));
        }
        /// <summary>
        /// 获取STMP信息
        /// </summary>
        /// <returns></returns>
        public IList<Model.Stmp> Get()
        {
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_StmpLoadAll", null));
        }
        #endregion

        #region Utilites
        /// <summary>
        /// 获取一条实体
        /// </summary>
        ///<param name="row"></param>
        /// <returns></returns>
        private Model.Stmp GetModel(System.Data.DataRow row)
        {
            Model.Stmp model = new Model.Stmp();
            if (row != null)
            {
                if (row["ID"] != DBNull.Value)
                    model.ID = Convert.ToInt32(row["ID"]);
                if (row["MailName"] != DBNull.Value)
                    model.MailName = row["MailName"].ToString();
                if (row["MailPass"] != DBNull.Value)
                    model.MailPass = row["MailPass"].ToString();
                if (row["MailSmtp"] != DBNull.Value)
                    model.MailSmtp = row["MailSmtp"].ToString();
                if (row["MailContent"] != DBNull.Value)
                    model.MailContent = row["MailContent"].ToString();
                if (row["State"] != DBNull.Value)
                    model.State = Convert.ToInt32(row["State"]);
            }
            return model;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private IList<Model.Stmp> GetList(System.Data.DataTable table)
        {
            List<Model.Stmp> list = new List<Model.Stmp>();
            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[] SetSqlParmeter(Model.Stmp model)
        {
            SqlParameter[] parms = GetSqlParmeter();
            parms[0].Value = model.ID;
            parms[1].Value = model.MailName;
            parms[2].Value = model.MailPass;
            parms[3].Value = model.MailSmtp;
            parms[4].Value = model.MailContent;
            parms[5].Value = model.State;
            parms[6].Value = "";

            return parms;
        }

        private static SqlParameter[] GetSqlParmeter()
        {
          SqlParameter[] parms = new SqlParameter[]{
          new SqlParameter(PARM_ID, SqlDbType.Int),
          new SqlParameter(PARM_MAIL_NAME, SqlDbType.NVarChar),
          new SqlParameter(PARM_MAIL_PASS, SqlDbType.NVarChar),
          new SqlParameter(PARM_MAIL_STMP, SqlDbType.NVarChar),
          new SqlParameter(PARM_MAIL_CONTENT, SqlDbType.NVarChar),
          new SqlParameter(PARM_STATE, SqlDbType.Int),
          new SqlParameter(PARM_EXPANSION, SqlDbType.NVarChar)

        };
            return parms;
        }
        #endregion
    }
}
