using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using RMYY_CallCenter_Api.DB;
namespace RMYY_CallCenter_Api.Dal
{
    /// <summary>
    /// 数据访问类:T_Sys_UserAccount
    /// </summary>
    public partial class T_Sys_UserAccount
    {
        public T_Sys_UserAccount()
        { }
        #region  BasicMethod

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

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RMYY_CallCenter_Api.Model.T_Sys_UserAccount model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Sys_UserAccount(");
            strSql.Append("F_UserCode,F_Password,F_ExtensionNumber,F_UserName,F_DeptId,F_RoleId,F_Sex,F_Telephone,F_WxOpenId,F_Remark,F_SeatFlag,F_SeatGroup,F_EnableFlag,F_CreateUser,F_CreateTime,F_DeleteFlag,F_DeleteUser,F_DeleteTime,F_See,F_WorkPhone,F_HomeAddress,F_Job,F_StaffCode,F_Title)");
            strSql.Append(" values (");
            strSql.Append("@F_UserCode,@F_Password,@F_ExtensionNumber,@F_UserName,@F_DeptId,@F_RoleId,@F_Sex,@F_Telephone,@F_WxOpenId,@F_Remark,@F_SeatFlag,@F_SeatGroup,@F_EnableFlag,@F_CreateUser,@F_CreateTime,@F_DeleteFlag,@F_DeleteUser,@F_DeleteTime,@F_See,@F_WorkPhone,@F_HomeAddress,@F_Job,@F_StaffCode,@F_Title)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_UserCode", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Password", SqlDbType.VarChar,50),
                    new SqlParameter("@F_ExtensionNumber", SqlDbType.VarChar,50),
                    new SqlParameter("@F_UserName", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeptId", SqlDbType.Int,4),
                    new SqlParameter("@F_RoleId", SqlDbType.Int,4),
                    new SqlParameter("@F_Sex", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Telephone", SqlDbType.VarChar,50),
                    new SqlParameter("@F_WxOpenId", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Remark", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_SeatFlag", SqlDbType.Int,4),
                    new SqlParameter("@F_SeatGroup", SqlDbType.Int,4),
                    new SqlParameter("@F_EnableFlag", SqlDbType.Int,4),
                    new SqlParameter("@F_CreateUser", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_DeleteFlag", SqlDbType.Int,4),
                    new SqlParameter("@F_DeleteUser", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeleteTime", SqlDbType.DateTime),
                    new SqlParameter("@F_See", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_WorkPhone", SqlDbType.VarChar,50),
                    new SqlParameter("@F_HomeAddress", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Job", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_StaffCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Title", SqlDbType.NVarChar,200)
                    
            };
            parameters[0].Value = model.F_UserCode;
            parameters[1].Value = model.F_Password;
            parameters[2].Value = model.F_ExtensionNumber;
            parameters[3].Value = model.F_UserName;
            parameters[4].Value = model.F_DeptId;
            parameters[5].Value = model.F_RoleId;
            parameters[6].Value = model.F_Sex;
            parameters[7].Value = model.F_Telephone;
            parameters[8].Value = model.F_WxOpenId;
            parameters[9].Value = model.F_Remark;
            parameters[10].Value = model.F_SeatFlag;
            parameters[11].Value = model.F_SeatGroup;
            parameters[12].Value = model.F_EnableFlag;
            parameters[13].Value = model.F_CreateUser;
            parameters[14].Value = model.F_CreateTime;
            parameters[15].Value = model.F_DeleteFlag;
            parameters[16].Value = model.F_DeleteUser;
            parameters[17].Value = model.F_DeleteTime;
            parameters[18].Value = model.F_See;
            parameters[19].Value = model.F_WorkPhone;
            parameters[20].Value = model.F_HomeAddress;
            parameters[21].Value = model.F_Job;
            parameters[22].Value = model.F_StaffCode;
            parameters[23].Value = model.F_Title;
            
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RMYY_CallCenter_Api.Model.T_Sys_UserAccount model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Sys_UserAccount set ");
            strSql.Append("F_UserCode=@F_UserCode,");
            strSql.Append("F_Password=@F_Password,");
            strSql.Append("F_ExtensionNumber=@F_ExtensionNumber,");
            strSql.Append("F_UserName=@F_UserName,");
            strSql.Append("F_DeptId=@F_DeptId,");
            strSql.Append("F_RoleId=@F_RoleId,");
            strSql.Append("F_Sex=@F_Sex,");
            strSql.Append("F_Telephone=@F_Telephone,");
            strSql.Append("F_WxOpenId=@F_WxOpenId,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_SeatFlag=@F_SeatFlag,");
            strSql.Append("F_SeatGroup=@F_SeatGroup,");
            strSql.Append("F_EnableFlag=@F_EnableFlag,");
            strSql.Append("F_CreateUser=@F_CreateUser,");
            strSql.Append("F_CreateTime=@F_CreateTime,");
            strSql.Append("F_DeleteFlag=@F_DeleteFlag,");
            strSql.Append("F_DeleteUser=@F_DeleteUser,");
            strSql.Append("F_DeleteTime=@F_DeleteTime,");
            strSql.Append("F_See=@F_See,");
            strSql.Append("F_WorkPhone=@F_WorkPhone,");
            strSql.Append("F_HomeAddress=@F_HomeAddress,");
            strSql.Append("F_Job=@F_Job,");
            strSql.Append("F_StaffCode=@F_StaffCode,");
            strSql.Append("F_Title=@F_Title");
            
            strSql.Append(" where F_UserId=@F_UserId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_UserCode", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Password", SqlDbType.VarChar,50),
                    new SqlParameter("@F_ExtensionNumber", SqlDbType.VarChar,50),
                    new SqlParameter("@F_UserName", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeptId", SqlDbType.Int,4),
                    new SqlParameter("@F_RoleId", SqlDbType.Int,4),
                    new SqlParameter("@F_Sex", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Telephone", SqlDbType.VarChar,50),
                    new SqlParameter("@F_WxOpenId", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Remark", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_SeatFlag", SqlDbType.Int,4),
                    new SqlParameter("@F_SeatGroup", SqlDbType.Int,4),
                    new SqlParameter("@F_EnableFlag", SqlDbType.Int,4),
                    new SqlParameter("@F_CreateUser", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_DeleteFlag", SqlDbType.Int,4),
                    new SqlParameter("@F_DeleteUser", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeleteTime", SqlDbType.DateTime),
                    new SqlParameter("@F_See", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_WorkPhone", SqlDbType.VarChar,50),
                    new SqlParameter("@F_HomeAddress", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Job", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_StaffCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Title", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_UserId", SqlDbType.Int,4)};
            parameters[0].Value = model.F_UserCode;
            parameters[1].Value = model.F_Password;
            parameters[2].Value = model.F_ExtensionNumber;
            parameters[3].Value = model.F_UserName;
            parameters[4].Value = model.F_DeptId;
            parameters[5].Value = model.F_RoleId;
            parameters[6].Value = model.F_Sex;
            parameters[7].Value = model.F_Telephone;
            parameters[8].Value = model.F_WxOpenId;
            parameters[9].Value = model.F_Remark;
            parameters[10].Value = model.F_SeatFlag;
            parameters[11].Value = model.F_SeatGroup;
            parameters[12].Value = model.F_EnableFlag;
            parameters[13].Value = model.F_CreateUser;
            parameters[14].Value = model.F_CreateTime;
            parameters[15].Value = model.F_DeleteFlag;
            parameters[16].Value = model.F_DeleteUser;
            parameters[17].Value = model.F_DeleteTime;
            parameters[18].Value = model.F_See;
            parameters[19].Value = model.F_WorkPhone;
            parameters[20].Value = model.F_HomeAddress;
            parameters[21].Value = model.F_Job;
            parameters[22].Value = model.F_StaffCode;
            parameters[23].Value = model.F_Title;
            parameters[24].Value = model.F_UserId;

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

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

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

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string F_UserIdlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update    T_Sys_UserAccount  set F_DeleteFlag=1");
            strSql.Append(" where F_UserId in (" + F_UserIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 批量修改数据 删除标志
        /// </summary>
        public bool DeleteFlagList(string F_UserIdlist,int state )
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update  T_Sys_UserAccount set F_EnableFlag ='" + state+"' ");
            strSql.Append(" where F_UserId in (" + F_UserIdlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Sys_UserAccount with(nolock) ");
            strSql.Append(" where F_UserId=@F_UserId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_UserId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_UserId;

            RMYY_CallCenter_Api.Model.T_Sys_UserAccount model = new RMYY_CallCenter_Api.Model.T_Sys_UserAccount();
            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 RMYY_CallCenter_Api.Model.T_Sys_UserAccount GetModel(string F_UserCode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 *  from T_Sys_UserAccount with(nolock) ");
            strSql.Append(" where F_UserCode=@F_UserCode");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_UserCode", SqlDbType.VarChar,50)
            };
            parameters[0].Value = F_UserCode;

            RMYY_CallCenter_Api.Model.T_Sys_UserAccount model = new RMYY_CallCenter_Api.Model.T_Sys_UserAccount();
            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 RMYY_CallCenter_Api.Model.T_Sys_UserAccount DataRowToModel(DataRow row)
        {
            RMYY_CallCenter_Api.Model.T_Sys_UserAccount model = new RMYY_CallCenter_Api.Model.T_Sys_UserAccount();
            if (row != null)
            {
                if (row["F_UserId"] != null && row["F_UserId"].ToString() != "")
                {
                    model.F_UserId = int.Parse(row["F_UserId"].ToString());
                }
                if (row["F_UserCode"] != null)
                {
                    model.F_UserCode = row["F_UserCode"].ToString();
                }
                if (row["F_Password"] != null)
                {
                    model.F_Password = row["F_Password"].ToString();
                }
                if (row["F_ExtensionNumber"] != null)
                {
                    model.F_ExtensionNumber = row["F_ExtensionNumber"].ToString();
                }
                if (row["F_UserName"] != null)
                {
                    model.F_UserName = row["F_UserName"].ToString();
                }
                if (row["F_DeptId"] != null && row["F_DeptId"].ToString() != "")
                {
                    model.F_DeptId = int.Parse(row["F_DeptId"].ToString());
                }
                if (row["F_RoleId"] != null && row["F_RoleId"].ToString() != "")
                {
                    model.F_RoleId = int.Parse(row["F_RoleId"].ToString());
                }
                if (row["F_Sex"] != null)
                {
                    model.F_Sex = row["F_Sex"].ToString();
                }
                if (row["F_Telephone"] != null)
                {
                    model.F_Telephone = row["F_Telephone"].ToString();
                }
                if (row["F_WxOpenId"] != null)
                {
                    model.F_WxOpenId = row["F_WxOpenId"].ToString();
                }
                if (row["F_Remark"] != null)
                {
                    model.F_Remark = row["F_Remark"].ToString();
                }
                if (row["F_SeatFlag"] != null && row["F_SeatFlag"].ToString() != "")
                {
                    model.F_SeatFlag = int.Parse(row["F_SeatFlag"].ToString());
                }
                if (row["F_SeatGroup"] != null && row["F_SeatGroup"].ToString() != "")
                {
                    model.F_SeatGroup = int.Parse(row["F_SeatGroup"].ToString());
                }
                if (row["F_EnableFlag"] != null && row["F_EnableFlag"].ToString() != "")
                {
                    model.F_EnableFlag = int.Parse(row["F_EnableFlag"].ToString());
                }
                if (row["F_CreateUser"] != null)
                {
                    model.F_CreateUser = row["F_CreateUser"].ToString();
                }
                if (row["F_CreateTime"] != null && row["F_CreateTime"].ToString() != "")
                {
                    model.F_CreateTime = DateTime.Parse(row["F_CreateTime"].ToString());
                }
                if (row["F_DeleteFlag"] != null && row["F_DeleteFlag"].ToString() != "")
                {
                    model.F_DeleteFlag = int.Parse(row["F_DeleteFlag"].ToString());
                }
                if (row["F_DeleteUser"] != null)
                {
                    model.F_DeleteUser = row["F_DeleteUser"].ToString();
                }
                if (row["F_DeleteTime"] != null && row["F_DeleteTime"].ToString() != "")
                {
                    model.F_DeleteTime = DateTime.Parse(row["F_DeleteTime"].ToString());
                }
                if (row["F_See"] != null)
                {
                    model.F_See = row["F_See"].ToString();
                }
                if (row["F_WorkPhone"] != null)
                {
                    model.F_WorkPhone = row["F_WorkPhone"].ToString();
                }
                if (row["F_HomeAddress"] != null)
                {
                    model.F_HomeAddress = row["F_HomeAddress"].ToString();
                }
                if (row["F_Job"] != null)
                {
                    model.F_Job = row["F_Job"].ToString();
                }
                if (row["F_StaffCode"] != null)
                {
                    model.F_StaffCode = row["F_StaffCode"].ToString();
                }
                if (row["F_Title"] != null)
                {
                    model.F_Title = row["F_Title"].ToString();
                }
                
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM T_Sys_UserAccount with(nolock) ");
            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(" * ");
            strSql.Append(" FROM T_Sys_UserAccount with(nolock) ");
            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_Sys_UserAccount with(nolock) ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        #endregion  BasicMethod
    }
}

