using CallCenterApi.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CallCenterApi.DAL
{
    public partial class T_SMS_InternalMessages
    {
        public T_SMS_InternalMessages()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("SMS_Id", "T_SMS_InternalMessages");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int SMS_Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_SMS_InternalMessages");
            strSql.Append(" where SMS_Id=@SMS_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@SMS_Id", SqlDbType.Int,4)
            };
            parameters[0].Value = SMS_Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.T_SMS_InternalMessages model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_SMS_InternalMessages(");
            strSql.Append("SMS_Title,SMS_Content,SMS_ReceiveUserCode,SMS_SendUserCode,SMS_IsRead,SMS_SendTime,SMS_ReadTime,SMS_DeleteTime,SMS_IsDelete,SMS_Order,SMS_IsTop)");
            strSql.Append(" values (");
            strSql.Append("@SMS_Title,@SMS_Content,@SMS_ReceiveUserCode,@SMS_SendUserCode,@SMS_IsRead,@SMS_SendTime,@SMS_ReadTime,@SMS_DeleteTime,@SMS_IsDelete,@SMS_Order,@SMS_IsTop)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@SMS_Title", SqlDbType.VarChar,100),
                    new SqlParameter("@SMS_Content", SqlDbType.NVarChar,500),
                    new SqlParameter("@SMS_ReceiveUserCode", SqlDbType.VarChar,50),
                    new SqlParameter("@SMS_SendUserCode", SqlDbType.VarChar,50),
                    new SqlParameter("@SMS_IsRead", SqlDbType.Int,4),
                    new SqlParameter("@SMS_SendTime", SqlDbType.DateTime),
                    new SqlParameter("@SMS_ReadTime", SqlDbType.DateTime),
                    new SqlParameter("@SMS_DeleteTime", SqlDbType.DateTime),
                    new SqlParameter("@SMS_IsDelete", SqlDbType.Int,4),
                    new SqlParameter("@SMS_Order", SqlDbType.Int,4),
                    new SqlParameter("@SMS_IsTop", SqlDbType.Int,4)};
            parameters[0].Value = model.SMS_Title;
            parameters[1].Value = model.SMS_Content;
            parameters[2].Value = model.SMS_ReceiveUserCode;
            parameters[3].Value = model.SMS_SendUserCode;
            parameters[4].Value = model.SMS_IsRead;
            parameters[5].Value = model.SMS_SendTime;
            parameters[6].Value = model.SMS_ReadTime;
            parameters[7].Value = model.SMS_DeleteTime;
            parameters[8].Value = model.SMS_IsDelete;
            parameters[9].Value = model.SMS_Order;
            parameters[10].Value = model.SMS_IsTop;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.T_SMS_InternalMessages model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_SMS_InternalMessages set ");
            strSql.Append("SMS_Title=@SMS_Title,");
            strSql.Append("SMS_Content=@SMS_Content,");
            strSql.Append("SMS_ReceiveUserCode=@SMS_ReceiveUserCode,");
            strSql.Append("SMS_SendUserCode=@SMS_SendUserCode,");
            strSql.Append("SMS_IsRead=@SMS_IsRead,");
            strSql.Append("SMS_SendTime=@SMS_SendTime,");
            strSql.Append("SMS_ReadTime=@SMS_ReadTime,");
            strSql.Append("SMS_DeleteTime=@SMS_DeleteTime,");
            strSql.Append("SMS_IsDelete=@SMS_IsDelete,");
            strSql.Append("SMS_Order=@SMS_Order,");
            strSql.Append("SMS_IsTop=@SMS_IsTop");
            strSql.Append(" where SMS_Id=@SMS_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@SMS_Title", SqlDbType.VarChar,100),
                    new SqlParameter("@SMS_Content", SqlDbType.NVarChar,500),
                    new SqlParameter("@SMS_ReceiveUserCode", SqlDbType.VarChar,50),
                    new SqlParameter("@SMS_SendUserCode", SqlDbType.VarChar,50),
                    new SqlParameter("@SMS_IsRead", SqlDbType.Int,4),
                    new SqlParameter("@SMS_SendTime", SqlDbType.DateTime),
                    new SqlParameter("@SMS_ReadTime", SqlDbType.DateTime),
                    new SqlParameter("@SMS_DeleteTime", SqlDbType.DateTime),
                    new SqlParameter("@SMS_IsDelete", SqlDbType.Int,4),
                    new SqlParameter("@SMS_Order", SqlDbType.Int,4),
                    new SqlParameter("@SMS_IsTop", SqlDbType.Int,4),
                    new SqlParameter("@SMS_Id", SqlDbType.Int,4)};
            parameters[0].Value = model.SMS_Title;
            parameters[1].Value = model.SMS_Content;
            parameters[2].Value = model.SMS_ReceiveUserCode;
            parameters[3].Value = model.SMS_SendUserCode;
            parameters[4].Value = model.SMS_IsRead;
            parameters[5].Value = model.SMS_SendTime;
            parameters[6].Value = model.SMS_ReadTime;
            parameters[7].Value = model.SMS_DeleteTime;
            parameters[8].Value = model.SMS_IsDelete;
            parameters[9].Value = model.SMS_Order;
            parameters[10].Value = model.SMS_IsTop;
            parameters[11].Value = model.SMS_Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int SMS_Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_SMS_InternalMessages ");
            strSql.Append(" where SMS_Id=@SMS_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@SMS_Id", SqlDbType.Int,4)
            };
            parameters[0].Value = SMS_Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string SMS_Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_SMS_InternalMessages ");
            strSql.Append(" where SMS_Id in (" + SMS_Idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.T_SMS_InternalMessages GetModel(int SMS_Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 SMS_Id,SMS_Title,SMS_Content,SMS_ReceiveUserCode,SMS_SendUserCode,SMS_IsRead,SMS_SendTime,SMS_ReadTime,SMS_DeleteTime,SMS_IsDelete,SMS_Order,SMS_IsTop from T_SMS_InternalMessages ");
            strSql.Append(" where SMS_Id=@SMS_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@SMS_Id", SqlDbType.Int,4)
            };
            parameters[0].Value = SMS_Id;

            Model.T_SMS_InternalMessages model = new Model.T_SMS_InternalMessages();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.T_SMS_InternalMessages DataRowToModel(DataRow row)
        {
            Model.T_SMS_InternalMessages model = new Model.T_SMS_InternalMessages();
            if (row != null)
            {
                if (row["SMS_Id"] != null && row["SMS_Id"].ToString() != "")
                {
                    model.SMS_Id = int.Parse(row["SMS_Id"].ToString());
                }
                if (row["SMS_Title"] != null)
                {
                    model.SMS_Title = row["SMS_Title"].ToString();
                }
                if (row["SMS_Content"] != null)
                {
                    model.SMS_Content = row["SMS_Content"].ToString();
                }
                if (row["SMS_ReceiveUserCode"] != null)
                {
                    model.SMS_ReceiveUserCode = row["SMS_ReceiveUserCode"].ToString();
                }
                if (row["SMS_SendUserCode"] != null)
                {
                    model.SMS_SendUserCode = row["SMS_SendUserCode"].ToString();
                }
                if (row["SMS_IsRead"] != null && row["SMS_IsRead"].ToString() != "")
                {
                    model.SMS_IsRead = int.Parse(row["SMS_IsRead"].ToString());
                }
                if (row["SMS_SendTime"] != null && row["SMS_SendTime"].ToString() != "")
                {
                    model.SMS_SendTime = DateTime.Parse(row["SMS_SendTime"].ToString());
                }
                if (row["SMS_ReadTime"] != null && row["SMS_ReadTime"].ToString() != "")
                {
                    model.SMS_ReadTime = DateTime.Parse(row["SMS_ReadTime"].ToString());
                }
                if (row["SMS_DeleteTime"] != null && row["SMS_DeleteTime"].ToString() != "")
                {
                    model.SMS_DeleteTime = DateTime.Parse(row["SMS_DeleteTime"].ToString());
                }
                if (row["SMS_IsDelete"] != null && row["SMS_IsDelete"].ToString() != "")
                {
                    model.SMS_IsDelete = int.Parse(row["SMS_IsDelete"].ToString());
                }
                if (row["SMS_Order"] != null && row["SMS_Order"].ToString() != "")
                {
                    model.SMS_Order = int.Parse(row["SMS_Order"].ToString());
                }
                if (row["SMS_IsTop"] != null && row["SMS_IsTop"].ToString() != "")
                {
                    model.SMS_IsTop = int.Parse(row["SMS_IsTop"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select SMS_Id,SMS_Title,SMS_Content,SMS_ReceiveUserCode,SMS_SendUserCode,SMS_IsRead,SMS_SendTime,SMS_ReadTime,SMS_DeleteTime,SMS_IsDelete,SMS_Order,SMS_IsTop ");
            strSql.Append(" FROM T_SMS_InternalMessages ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" SMS_Id,SMS_Title,SMS_Content,SMS_ReceiveUserCode,SMS_SendUserCode,SMS_IsRead,SMS_SendTime,SMS_ReadTime,SMS_DeleteTime,SMS_IsDelete,SMS_Order,SMS_IsTop ");
            strSql.Append(" FROM T_SMS_InternalMessages ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_SMS_InternalMessages ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.SMS_Id desc");
            }
            strSql.Append(")AS Row, T.*  from T_SMS_InternalMessages T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "T_SMS_InternalMessages";
			parameters[1].Value = "SMS_Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
