using MESClassLibrary.BLL.Log; using MESClassLibrary.Model; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Reflection; using System.Text; namespace MESClassLibrary.DAL.User { public class UserDAL : BasicDAL { public static string TableName = "tb_User"; public string GetTableName() { return TableName; } /// /// 获取全部用户信息 /// /// public DataTable Search_UserInfo() { try { string sql = @"select * from T_Sys_Users where IsEnabled='1'"; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } /// /// 根据登录名密码查询用户信息 /// /// /// /// public DataTable Search_UserInfoByUP(string username, string password) { try { string sql = @"SELECT [UserID] ,[UserName] ,[Password] ,[IsEnabled] ,[RealName] ,[Sex] ,[Tel] ,[LastLoginTime] ,[LastLoginIP] ,[DepartmentID] ,[OpName] ,[OpTime] FROM [T_Sys_Users] where UserName = @Username and Password = @Password and IsEnabled = '1'"; SqlParameter[] param = new SqlParameter[2]; param[0] = new SqlParameter("@Username", SqlDbType.VarChar); param[0].Value = username; param[1] = new SqlParameter("@Password", SqlDbType.VarChar); param[1].Value = password; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } /// /// 根据id查询用户信息 /// /// /// public DataTable Search_UserInfoByID(string id) { try { string sql = @"select c.*,r.RoleName from tb_User c left join tb_Role r on c.RoleID = r.id where c.IsUseing=1 and c.ID=@ID"; SqlParameter[] param = new SqlParameter[1]; param[0] = new SqlParameter("@ID", SqlDbType.VarChar); param[0].Value = id; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } /// /// 根据id查询用户信息 /// /// /// public DataTable Search_UserInfoByID_v2(string id) { try { string sql = @"select * from T_Sys_Users c where c.IsEnabled=1 and c.UserID=@UserID"; SqlParameter[] param = new SqlParameter[1]; param[0] = new SqlParameter("@UserID", SqlDbType.VarChar); param[0].Value = id; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } /// /// 根据登录账号查询用户信息 /// /// 登录账号 /// public DataTable Search_UserInfoByUserName(string username) { try { string sql = @"SELECT [UserID] ,[UserName] ,[Password] ,[IsEnabled] ,[RealName] ,[Sex] ,[Tel] ,[LastLoginTime] ,[LastLoginIP] ,[DepartmentID] ,[OpName] ,[OpTime] FROM [T_Sys_Users] where UserName = @Username and IsEnabled = '1'"; SqlParameter[] param = new SqlParameter[1]; param[0] = new SqlParameter("@Username", SqlDbType.VarChar); param[0].Value = username; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } /// /// 根据角色ID查询该角色的用户信息 /// /// 角色主键ID /// public DataTable Search_UserInfoByRoleID(string roleID) { try { string sql = @"select c.*,r.RoleName,r.RoleJurisdiction from tb_User c left join tb_Role r on c.RoleID = r.id where c.IsUseing=1 and c.RoleID=@RoleID"; SqlParameter[] param = new SqlParameter[1]; param[0] = new SqlParameter("@RoleID", SqlDbType.VarChar); param[0].Value = roleID; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, param).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } /// /// 添加用户信息 /// /// 用户模型 /// public bool Add_UserInfo(UserModel mod) { try { #region 添加数据 string sql = ""; SqlParameter[] param = null; sql = "INSERT INTO tb_User (ID,Username,Password,TrueName,FaceURL,RoleID,CreateUserID"; sql += ") VALUES ("; sql += "@ID,"; sql += "@Username,"; sql += "@Password,"; sql += "@TrueName,"; sql += "@FaceURL,"; sql += "@RoleID,"; sql += "@CreateUserID)"; #region 添加参数 param = new SqlParameter[7]; param[0] = new SqlParameter("@ID", SqlDbType.VarChar); param[0].Value = mod.ID; param[1] = new SqlParameter("@Username", SqlDbType.VarChar); param[1].Value = mod.Username; param[2] = new SqlParameter("@Password", SqlDbType.VarChar); param[2].Value = mod.Password; param[3] = new SqlParameter("@TrueName", SqlDbType.VarChar); param[3].Value = mod.TrueName; param[4] = new SqlParameter("@FaceURL", SqlDbType.VarChar); param[4].Value = mod.FaceURL; param[5] = new SqlParameter("@RoleID", SqlDbType.VarChar); param[5].Value = mod.RoleID; param[6] = new SqlParameter("@CreateUserID", SqlDbType.VarChar); param[6].Value = mod.CreateUserID; #endregion SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param); #endregion return true; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } /// /// 更新用户信息 /// /// 用户模型对象 /// public bool Update_UserInfo(UserModel md) { try { string sql = @"update tb_User set Username = @Username, Password = @Password, TrueName = @TrueName, FaceURL = @FaceURL, RoleID = @RoleID, UpdateUserID = @UpdateUserID, UpdateTime = @UpdateTime where ID=@ID"; SqlParameter[] param = new SqlParameter[8]; param[0] = new SqlParameter("@Username", SqlDbType.VarChar); param[0].Value = md.Username; param[1] = new SqlParameter("@Password", SqlDbType.VarChar); param[1].Value = md.Password; param[2] = new SqlParameter("@TrueName", SqlDbType.VarChar); param[2].Value = md.TrueName; param[3] = new SqlParameter("@FaceURL", SqlDbType.VarChar); param[3].Value = md.FaceURL; param[4] = new SqlParameter("@RoleID", SqlDbType.VarChar); param[4].Value = md.RoleID; param[5] = new SqlParameter("@UpdateUserID", SqlDbType.VarChar); param[5].Value = md.UpdateUserID; param[6] = new SqlParameter("@UpdateTime", SqlDbType.DateTime); param[6].Value = DateTime.Now; param[7] = new SqlParameter("@ID", SqlDbType.VarChar); param[7].Value = md.ID; if (SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param) > 0) { return true; } return false; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } /// /// 查询用户登录日志 /// /// public DataTable Search_UserLoginInfo() { try { string sql = @"select l.*,u.TrueName,r.RoleName from tb_UserLoginLog l left join tb_User u on l.UserID = u.ID left join tb_Role r on u.RoleID = r.ID order by l.LoginTime desc"; return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0]; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return null; } } /// /// 添加用户登录日志 /// /// 用户主键ID /// public bool Add_UserLoginInfo(string userid) { try { #region 添加数据 string sql = ""; SqlParameter[] param = null; sql = "INSERT INTO tb_UserLoginLog (ID,UserID"; sql += ") VALUES ("; sql += "@ID,"; sql += "@UserID)"; #region 添加参数 param = new SqlParameter[2]; param[0] = new SqlParameter("@ID", SqlDbType.VarChar); param[0].Value = Guid.NewGuid().ToString(); param[1] = new SqlParameter("@UserID", SqlDbType.VarChar); param[1].Value = userid; #endregion SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param); #endregion return true; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } public bool Update_Password(string userid,string oldpassword,string newpassword) { try { string sql = @"update T_Sys_Users set Password = @New_Password where UserID=@UserID and Password=@Old_Password"; SqlParameter[] param = new SqlParameter[3]; param[0] = new SqlParameter("@New_Password", SqlDbType.VarChar); param[0].Value = newpassword; param[1] = new SqlParameter("@UserID", SqlDbType.VarChar); param[1].Value = userid; param[2] = new SqlParameter("@Old_Password", SqlDbType.VarChar); param[2].Value = oldpassword; if (SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param) > 0) { return true; } return false; } catch (Exception ex) { LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod()); return false; } } } }