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_Email_SendEmail
    {
        public T_Email_SendEmail()
        { }
        #region  BasicMethod

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

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

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.T_Email_SendEmail model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Email_SendEmail(");
            strSql.Append("E_WorkOrderId,E_TypeId,E_SendEmail,E_SendName,E_ReceiveEmail,E_ReceiveName,E_Title,E_Content,E_AddTime,E_IsSucces,F_UserId,F_UserName)");
            strSql.Append(" values (");
            strSql.Append("@E_WorkOrderId,@E_TypeId,@E_SendEmail,@E_SendName,@E_ReceiveEmail,@E_ReceiveName,@E_Title,@E_Content,@E_AddTime,@E_IsSucces,@F_UserId,@F_UserName)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@E_WorkOrderId", SqlDbType.VarChar,20),
                    new SqlParameter("@E_TypeId", SqlDbType.Int,4),
                    new SqlParameter("@E_SendEmail", SqlDbType.VarChar,50),
                    new SqlParameter("@E_SendName", SqlDbType.NVarChar,20),
                    new SqlParameter("@E_ReceiveEmail", SqlDbType.VarChar,50),
                    new SqlParameter("@E_ReceiveName", SqlDbType.NVarChar,20),
                    new SqlParameter("@E_Title", SqlDbType.VarChar,50),
                    new SqlParameter("@E_Content", SqlDbType.NVarChar,500),
                    new SqlParameter("@E_AddTime", SqlDbType.DateTime),
                    new SqlParameter("@E_IsSucces", SqlDbType.Int,4),
                    new SqlParameter("@F_UserId", SqlDbType.Int,4),
                    new SqlParameter("@F_UserName", SqlDbType.VarChar,50)};
            parameters[0].Value = model.E_WorkOrderId;
            parameters[1].Value = model.E_TypeId;
            parameters[2].Value = model.E_SendEmail;
            parameters[3].Value = model.E_SendName;
            parameters[4].Value = model.E_ReceiveEmail;
            parameters[5].Value = model.E_ReceiveName;
            parameters[6].Value = model.E_Title;
            parameters[7].Value = model.E_Content;
            parameters[8].Value = model.E_AddTime;
            parameters[9].Value = model.E_IsSucces;
            parameters[10].Value = model.F_UserId;
            parameters[11].Value = model.F_UserName;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.T_Email_SendEmail model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Email_SendEmail set ");
            strSql.Append("E_WorkOrderId=@E_WorkOrderId,");
            strSql.Append("E_TypeId=@E_TypeId,");
            strSql.Append("E_SendEmail=@E_SendEmail,");
            strSql.Append("E_SendName=@E_SendName,");
            strSql.Append("E_ReceiveEmail=@E_ReceiveEmail,");
            strSql.Append("E_ReceiveName=@E_ReceiveName,");
            strSql.Append("E_Title=@E_Title,");
            strSql.Append("E_Content=@E_Content,");
            strSql.Append("E_AddTime=@E_AddTime,");
            strSql.Append("E_IsSucces=@E_IsSucces,");
            strSql.Append("F_UserId=@F_UserId,");
            strSql.Append("F_UserName=@F_UserName");
            strSql.Append(" where E_Id=@E_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@E_WorkOrderId", SqlDbType.VarChar,20),
                    new SqlParameter("@E_TypeId", SqlDbType.Int,4),
                    new SqlParameter("@E_SendEmail", SqlDbType.VarChar,50),
                    new SqlParameter("@E_SendName", SqlDbType.NVarChar,20),
                    new SqlParameter("@E_ReceiveEmail", SqlDbType.VarChar,50),
                    new SqlParameter("@E_ReceiveName", SqlDbType.NVarChar,20),
                    new SqlParameter("@E_Title", SqlDbType.VarChar,50),
                    new SqlParameter("@E_Content", SqlDbType.NVarChar,500),
                    new SqlParameter("@E_AddTime", SqlDbType.DateTime),
                    new SqlParameter("@E_IsSucces", SqlDbType.Int,4),
                    new SqlParameter("@F_UserId", SqlDbType.Int,4),
                    new SqlParameter("@F_UserName", SqlDbType.VarChar,50),
                    new SqlParameter("@E_Id", SqlDbType.Int,4)};
            parameters[0].Value = model.E_WorkOrderId;
            parameters[1].Value = model.E_TypeId;
            parameters[2].Value = model.E_SendEmail;
            parameters[3].Value = model.E_SendName;
            parameters[4].Value = model.E_ReceiveEmail;
            parameters[5].Value = model.E_ReceiveName;
            parameters[6].Value = model.E_Title;
            parameters[7].Value = model.E_Content;
            parameters[8].Value = model.E_AddTime;
            parameters[9].Value = model.E_IsSucces;
            parameters[10].Value = model.F_UserId;
            parameters[11].Value = model.F_UserName;
            parameters[12].Value = model.E_Id;

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

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

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

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


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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 E_Id,E_WorkOrderId,E_TypeId,E_SendEmail,E_SendName,E_ReceiveEmail,E_ReceiveName,E_Title,E_Content,E_AddTime,E_IsSucces,F_UserId,F_UserName from T_Email_SendEmail ");
            strSql.Append(" where E_Id=@E_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@E_Id", SqlDbType.Int,4)
            };
            parameters[0].Value = E_Id;

            Model.T_Email_SendEmail model = new Model.T_Email_SendEmail();
            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_Email_SendEmail DataRowToModel(DataRow row)
        {
            Model.T_Email_SendEmail model = new Model.T_Email_SendEmail();
            if (row != null)
            {
                if (row["E_Id"] != null && row["E_Id"].ToString() != "")
                {
                    model.E_Id = int.Parse(row["E_Id"].ToString());
                }
                if (row["E_WorkOrderId"] != null)
                {
                    model.E_WorkOrderId = row["E_WorkOrderId"].ToString();
                }
                if (row["E_TypeId"] != null && row["E_TypeId"].ToString() != "")
                {
                    model.E_TypeId = int.Parse(row["E_TypeId"].ToString());
                }
                if (row["E_SendEmail"] != null)
                {
                    model.E_SendEmail = row["E_SendEmail"].ToString();
                }
                if (row["E_SendName"] != null)
                {
                    model.E_SendName = row["E_SendName"].ToString();
                }
                if (row["E_ReceiveEmail"] != null)
                {
                    model.E_ReceiveEmail = row["E_ReceiveEmail"].ToString();
                }
                if (row["E_ReceiveName"] != null)
                {
                    model.E_ReceiveName = row["E_ReceiveName"].ToString();
                }
                if (row["E_Title"] != null)
                {
                    model.E_Title = row["E_Title"].ToString();
                }
                if (row["E_Content"] != null)
                {
                    model.E_Content = row["E_Content"].ToString();
                }
                if (row["E_AddTime"] != null && row["E_AddTime"].ToString() != "")
                {
                    model.E_AddTime = DateTime.Parse(row["E_AddTime"].ToString());
                }
                if (row["E_IsSucces"] != null && row["E_IsSucces"].ToString() != "")
                {
                    model.E_IsSucces = int.Parse(row["E_IsSucces"].ToString());
                }
                if (row["F_UserId"] != null && row["F_UserId"].ToString() != "")
                {
                    model.F_UserId = int.Parse(row["F_UserId"].ToString());
                }
                if (row["F_UserName"] != null)
                {
                    model.F_UserName = row["F_UserName"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select E_Id,E_WorkOrderId,E_TypeId,E_SendEmail,E_SendName,E_ReceiveEmail,E_ReceiveName,E_Title,E_Content,E_AddTime,E_IsSucces,F_UserId,F_UserName ");
            strSql.Append(" FROM T_Email_SendEmail ");
            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(" E_Id,E_WorkOrderId,E_TypeId,E_SendEmail,E_SendName,E_ReceiveEmail,E_ReceiveName,E_Title,E_Content,E_AddTime,E_IsSucces,F_UserId,F_UserName ");
            strSql.Append(" FROM T_Email_SendEmail ");
            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_Email_SendEmail ");
            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.E_Id desc");
            }
            strSql.Append(")AS Row, T.*  from T_Email_SendEmail 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_Email_SendEmail";
			parameters[1].Value = "E_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
    }
}
