using Mesnac.Action.ChemicalWeighing.Entity; using Mesnac.Codd.Session; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Mesnac.Action.ChemicalWeighing.UserManage { class UserHelper { #region 查询用户列表 /// /// 查询用户信息 /// /// 返回一个包含用户信息的数据表 public static DataTable getUsers() { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; string strSql = "SELECT UID, UName, UPwd, RoleName, URoleID FROM Pst_user LEFT JOIN pst_Role ON Pst_user.URoleID=pst_Role.Id ORDER BY UID"; dbHelper.CommandText = strSql; DataTable table = dbHelper.ToDataTable(); return table; } #endregion #region 获取用户对象集合 /// /// 获取包含所有用户对象的集合 /// /// 返回一个list链表 其中是User对象 public static List getUserList() { DataTable userTable = getUsers(); List lst = new List(); foreach (DataRow row in userTable.Rows) { SimplePstUser user = new SimplePstUser { UID = Mesnac.Basic.DataProcessor.RowValue(row, "UID", 0), UName = Mesnac.Basic.DataProcessor.RowValue(row, "UName", String.Empty), UPwd = Mesnac.Basic.DataProcessor.RowValue(row, "UPwd", String.Empty), URoleName = Mesnac.Basic.DataProcessor.RowValue(row, "URoleName", String.Empty), URoleID = Mesnac.Basic.DataProcessor.RowValue(row, "URoleID", 0) }; lst.Add(user); } return lst; } #endregion #region 新增用户 /// /// 向数据库用户表中添加一条用户数据 /// /// public static void addUser(SimplePstUser user) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; string insertUserSql = "INSERT INTO Pst_user(UName, UPwd, URoleName, URoleID) VALUES(@U_Name, @U_Pwd, @U_RoleName, @U_RoleID)"; dbHelper.CommandText = insertUserSql; dbHelper.AddParameter("@U_Name", user.UName); dbHelper.AddParameter("@U_Pwd", user.UPwd); dbHelper.AddParameter("@U_RoleName", user.URoleName); dbHelper.AddParameter("@U_RoleID", user.URoleID); dbHelper.ExecuteNonQuery(); } #endregion #region 根据UID修改用户信息 public static void updateUser(SimplePstUser user) { try { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; string strSql = "UPDATE Pst_user SET UName=@UName, UPwd=@UPwd, URoleName=@URoleName, URoleID=@URoleID WHERE UID=@UID"; dbHelper.CommandText = strSql; dbHelper.AddParameter("@UName", user.UName); dbHelper.AddParameter("@UPwd", user.UPwd); dbHelper.AddParameter("@URoleName", user.URoleName); dbHelper.AddParameter("@URoleID", user.URoleID); dbHelper.AddParameter("@UID", user.UID); dbHelper.ExecuteNonQuery(); } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("修改用户信息异常:" + ex.Message, ex); } } #endregion #region 获取用户类型对象集合 /// /// 获取所有用户类型及对应类型ID /// /// public static List GetUserRoleList() { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } dbHelper.ClearParameter(); dbHelper.CommandType = CommandType.Text; string sqlstr = "SELECT Id, RoleName, Description FROM pst_Role"; dbHelper.CommandText = sqlstr; DataTable table = dbHelper.ToDataTable(); List lst = new List(); SimplePstRole role = null; foreach (DataRow row in table.Rows) { role = new SimplePstRole(); role.ID = Mesnac.Basic.DataProcessor.RowValue(row, "Id", 0); role.RoleName = Mesnac.Basic.DataProcessor.RowValue(row, "RoleName", String.Empty); role.Description = Mesnac.Basic.DataProcessor.RowValue(row, "Description", String.Empty); lst.Add(role); } return lst; } #endregion #region 根据用户名删除用户 public static void deleteUser(string uname) { DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local); if (dbHelper == null) { throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError); } dbHelper.CommandType = CommandType.Text; dbHelper.ClearParameter(); string strSql = "DELETE FROM Pst_user WHERE UName = @UserName"; dbHelper.CommandText = strSql; dbHelper.AddParameter("@UserName", uname); dbHelper.ExecuteNonQuery(); } #endregion } }