From b1ccdb760b9c16200831a2617caf9318b1be86b3 Mon Sep 17 00:00:00 2001
From: hwh <332078369@qq.com>
Date: 星期四, 27 六月 2024 08:11:09 +0800
Subject: [PATCH] 重构用户管理

---
 Wms/WMS.BLL/SysServer/UserInforServer.cs |  244 ++++++++++++++++++------------------------------
 1 files changed, 94 insertions(+), 150 deletions(-)

diff --git a/Wms/WMS.BLL/SysServer/UserInforServer.cs b/Wms/WMS.BLL/SysServer/UserInforServer.cs
index c5761eb..6d6ecac 100644
--- a/Wms/WMS.BLL/SysServer/UserInforServer.cs
+++ b/Wms/WMS.BLL/SysServer/UserInforServer.cs
@@ -11,6 +11,11 @@
 using OracleInternal.Secure.Network;
 using Utility.Tools;
 using System.Linq;
+using SqlSugar;
+using WMS.Entity.Context;
+using WMS.Entity.BllTaskEntity;
+using WMS.DAL;
+using Utility;
 
 namespace WMS.BLL.SysServer
 {
@@ -19,37 +24,36 @@
         public IUserInforRepository UserSvc { get; set; }
         readonly IMapper _mapper;
         private readonly IFunSettingServer _setting;
-        public UserInforServer(IUserInforRepository userSvc, IMapper mapper, IFunSettingServer setting)
+        private static readonly SqlSugarScope Db = DataContext.Db;
+        private readonly UserManager _userManager;
+        public UserInforServer(IUserInforRepository userSvc, IMapper mapper, IFunSettingServer setting, UserManager userManager)
         {
             UserSvc = userSvc;
             _mapper = mapper;
             _setting = setting;
+            _userManager = userManager;
         }
 
-        public bool LoginAdmin(string loginName, string loginPwd, out int adminId)
+        public async Task<int> LoginAdmin(string loginName, string loginPwd)
         {
             //loginPwd = Md5Tools.CalcMd5(loginPwd);
-            var date = UserSvc.GetAllAsync().First(m => m.UserName == loginName && m.PassWord == loginPwd);
-            
-            if (date != null) //璐﹀彿瀵嗙爜鏄惁姝g‘
+            var modUser = await Db.Queryable<SysUserInfor>().FirstAsync(m => m.UserName == loginName && m.PassWord == loginPwd);
+            if (modUser != null) //璐﹀彿瀵嗙爜鏄惁姝g‘
             {
-                if (date.Status == "0") //褰撳墠璐﹀彿鏄惁姝e父鍚敤
+                if (modUser.Status == "0") //褰撳墠璐﹀彿鏄惁姝e父鍚敤
                 {
                     //鏈�鍚庣櫥闄嗘椂闂�
-                    UserSvc.UptUserLoginTime(date.Id);
-                    adminId = date.Id;
-                    return true;
+                    await Db.Updateable(modUser).UpdateColumns(s => s.LoginTime == DateTime.Now).ExecuteCommandAsync();
+                    return modUser.Id;
                 }
                 else //褰撳墠璐﹀彿琚鐢�
                 {
-                    adminId = 0;
-                    return true;
+                    return 0;
                 }
             }
             else //璐﹀彿瀵嗙爜涓嶆纭垨娌℃湁姝よ处鍙�
             {
-                adminId = 0;
-                return false;
+                return -1;
             }
         }
         public SysUserInfor CeShi()
@@ -64,37 +68,19 @@
         /// 鑾峰彇鐢ㄦ埛瑙掕壊淇℃伅
         /// </summary>
         /// <returns></returns>
-        public List<UserInfoDto> GetUserRoleList(string UserName, string DepartmentNo, string RoleNo, string Status)
+        public async Task<List<UserInfoDto>> GetUserRoleList(string UserName, string DepartmentNo, string RoleNo, string Status)
         {
-
-            string str = "select user1.Id Id,user1.UserNo UserNo,user1.UserName UserName,user1.RealName RealName,user1.PassWord PassWord,user1.JobNo JobNo,user1.Status Status,user1.Sex Sex,user1.Nationality Nationality,user1.CreateTime CreateTime,user1.CreateUser CreateUser,user1.Paper Paper,user1.Phone Phone,user1.Email Email,user1.Addres Addres,user1.Demo Demo,role.RoleName,user2.RealName CreateUserName,depa.DepartmentName DepartmentName from SysUserInfor user1 left join SysRoles role on user1.RoleNo = role.RoleNo left join SysUserInfor user2 on user1.CreateUser = user2.Id left join SysDepartment depa on user1.DepartmentNo = depa.DepartmentNo where user1.IsDel = '0'";
-
-            //鍒ゆ柇鐧诲綍鍚嶇О鏄惁涓虹┖
-            if (!string.IsNullOrEmpty(UserName))
-            {
-                str += $" and user1.UserName like '%{UserName}%'";
-            }
-            //鍒ゆ柇閮ㄩ棬鍙锋槸鍚︿负0
-            if (!string.IsNullOrEmpty(DepartmentNo))
-            {
-                str += $" and user1.DepartmentNo = '{DepartmentNo}'";
-            }
-            //鍒ゆ柇瑙掕壊鍙锋槸鍚︿负绌�
-            if (!string.IsNullOrEmpty(RoleNo))
-            {
-                str += $" and user1.RoleNo = '{RoleNo}'";
-            }
-            //鍒ゆ柇鐘舵�佹槸鍚︿负绌�
-            if (!string.IsNullOrEmpty(Status))
-            {
-                str += $" and user1.Status = '{Status}'";
-            }
-
-            List<UserInfoDto> userInfoDtos = UserSvc.GetUserRoleList(str);
-
-            //var arr = Md5Tools.CalcMd5(userInfoDtos[3].PassWord);
-
-            return userInfoDtos;
+            return await Db.Queryable<SysUserInfor>()
+                                        .LeftJoin<SysUserInfor>((a, b) => a.CreateUser == b.Id)
+                                        .LeftJoin<SysDepartment>((a, b, c) => a.DepartmentNo == c.DepartmentNo)
+                                        .LeftJoin<SysRoles>((a, b, c, d) => a.RoleNo == d.RoleNo)
+                                        .WhereIF(!string.IsNullOrEmpty(UserName), a => a.UserName.Contains(UserName))
+                                        .WhereIF(!string.IsNullOrEmpty(DepartmentNo), a => a.DepartmentNo == DepartmentNo)
+                                        .WhereIF(!string.IsNullOrEmpty(RoleNo), a => a.RoleNo == RoleNo)
+                                        .WhereIF(!string.IsNullOrEmpty(Status), a => a.Status == Status)
+                                        .Where(a => a.IsDel == "0")
+                                        .Select<UserInfoDto>()
+                                        .ToListAsync();
         }
 
         /// <summary>
@@ -104,15 +90,13 @@
         /// <param name="userName">鐢ㄦ埛鐧诲綍鍚�</param>
         /// <param name="id">鐢ㄦ埛ID</param>
         /// <returns></returns>
-        public int GetUserInfoByNo(string userNo, string userName,int id)
+        public async Task<int> GetUserInfoByNo(string userNo, string userName, int id)
         {
-            string str = $"select * from SysUserInfor where IsDel=0 and (UserNo = '{userNo}' or UserName = '{userName}') ";
-            if (id!=0)
-            {
-                str += $"and Id != {id} ";
-            }
-            List<SysUserInfor> user = UserSvc.GetUserInfoByNo(str);
-            return user.Count;
+            return await Db.Queryable<SysUserInfor>()
+                        .Where(s => s.IsDel == "0")
+                        .Where(s => s.UserNo == userNo || s.UserName == userName)
+                        .WhereIF(id != 0, a => a.Id != id)
+                        .CountAsync();
         }
 
         /// <summary>
@@ -123,30 +107,27 @@
         /// <exception cref="Exception">寮傚父</exception>
         public async Task<int> InsertUserInfo(UserInfoDto UserInfoDto)
         {
-            //寮傚父
-            try
+            //鏁版嵁妯″瀷鏄犲皠
+            SysUserInfor userinfo = _mapper.Map<SysUserInfor>(UserInfoDto);
+            //鍒ゆ柇鐢ㄦ埛鍙锋槸鍚﹀敮涓�
+            int count = await GetUserInfoByNo(userinfo.UserNo, userinfo.UserName, 0);
+            int i = 0;
+            if (count > 0)
             {
-                //鏁版嵁妯″瀷鏄犲皠
-                SysUserInfor userinfo = _mapper.Map<SysUserInfor>(UserInfoDto);
-                //鍒ゆ柇鐢ㄦ埛鍙锋槸鍚﹀敮涓�
-                int count = GetUserInfoByNo(userinfo.UserNo,userinfo.UserName,0);
-                int i = 0;
-                if (count > 0)
-                {
-                    i = 3;
-                }
-                else if (count ==0)
-                {
-                    //鏂板鐢ㄦ埛
-                    i = await UserSvc.InsertUserInfo(userinfo);
-                }
-                return i;
+                throw Oops.Bah("鐢ㄦ埛鍚嶆垨鐧诲綍鍚嶉噸澶�");
             }
-            catch (Exception ex)
+            else if (count == 0)
             {
-                //鎶涘嚭寮傚父
-                throw new Exception("鏂板鐢ㄦ埛淇℃伅寮傚父", ex);
+                //鏂板鐢ㄦ埛
+                //i = await UserSvc.InsertUserInfo(userinfo);
+                userinfo.PassWord = Md5Tools.CalcMd5("boxline");
+                userinfo.SetPasswordTime = DateTime.Now;
+                userinfo.CreateTime = DateTime.Now;
+                i = await Db.Insertable(userinfo).ExecuteCommandAsync();
+                if (i <= 0)
+                    throw Oops.Bah("鏂板鐢ㄦ埛淇℃伅澶辫触");
             }
+            return i;
 
         }
 
@@ -156,21 +137,15 @@
         /// <param name="user">鐢ㄦ埛瀹炰綋妯″瀷</param>
         /// <returns></returns>
         /// <exception cref="Exception">寮傚父</exception>
-        public async Task<int> DeleteUserinfo(SysUserInfor user)
+        public async Task DeleteUserinfo(SysUserInfor user)
         {
-            //寮傚父
-            try
-            {
-                //鍒犻櫎
-                int i = await UserSvc.DeleteUserinfo(user);
-                //杩斿洖鏁版嵁
-                return i;
-            }
-            catch (Exception ex)
-            {
-                //鎶涘嚭寮傚父
-                throw new Exception("鍒犻櫎鐢ㄦ埛淇℃伅寮傚父", ex);
-            }
+            await Db.Updateable<SysUserInfor>()
+                           .Where(s => s.Id == user.Id)
+                           .SetColumns(s => s.IsDel == "1")
+                           .SetColumns(s => s.UpdateTime == DateTime.Now)
+                           .SetColumns(s => s.UpdateUser == _userManager.UserId)
+                           .ExecuteCommandAsync();
+
         }
 
         /// <summary>
@@ -181,31 +156,25 @@
         /// <exception cref="Exception">寮傚父exception>
         public async Task<int> UpdateUserinfo(UserInfoDto userdto)
         {
-            //寮傚父
-            try
+            //鏄犲皠鏁版嵁妯″瀷
+            SysUserInfor userlist = _mapper.Map<SysUserInfor>(userdto);
+            //鍒ゆ柇鐢ㄦ埛鍙锋槸鍚﹀敮涓�
+            int count = await GetUserInfoByNo(userdto.UserNo, userdto.UserName, userdto.Id);
+            int i = 0;
+            if (count > 0)
             {
-                //鏄犲皠鏁版嵁妯″瀷
-                SysUserInfor userlist = _mapper.Map<SysUserInfor>(userdto);
-                //鍒ゆ柇鐢ㄦ埛鍙锋槸鍚﹀敮涓�
-                int count = GetUserInfoByNo(userdto.UserNo, userdto.UserName,userdto.Id);
-                int i = 0;
-                if (count > 0)
-                {
-                    i = 3;
-                }
-                else if (count == 0)
-                {
-                    //缂栬緫
-                    i = await UserSvc.UpdateUserinfo(userlist);
-                }
-               
-                return i;
+                throw Oops.Bah("鐢ㄦ埛鍚嶆垨鐧诲綍鍚嶉噸澶�");
             }
-            catch (Exception ex)
+            else if (count == 0)
             {
-                //鎶涘嚭寮傚父
-                throw new Exception("缂栬緫鐢ㄦ埛淇℃伅寮傚父", ex);
+                userdto.UpdateTime = DateTime.Now;
+                i = await Db.Updateable(userlist)
+                    .UpdateColumns(s => new { s.UserName, s.UserNo, s.Sex, s.RoleNo, s.RealName, s.Phone, s.Paper, s.Nationality, s.JobNo, s.Email, s.Demo, s.Addres })
+                    .ExecuteCommandAsync();
+                if (i <= 0)
+                    throw Oops.Bah("缂栬緫鐢ㄦ埛淇℃伅澶辫触");
             }
+            return i;
         }
 
         /// <summary>
@@ -213,12 +182,9 @@
         /// </summary>
         /// <param name="userids">鐢ㄦ埛id</param>
         /// <returns></returns>
-        public SysUserInfor GetUserInfoById(int userids)
+        public async Task<SysUserInfor> GetUserInfoById(int userids)
         {
-            //鑾峰彇淇℃伅鍒楄〃
-            SysUserInfor user= UserSvc.GetUserInfoById(userids);
-            //杩斿洖鏁版嵁
-            return user;
+            return await Db.Queryable<SysUserInfor>().FirstAsync(s => s.Id == userids && s.IsDel == "0");
         }
 
         /// <summary>
@@ -230,59 +196,37 @@
         /// <param name="userId">鐢ㄦ埛id</param>
         /// <returns></returns>
         /// <exception cref="Exception">鎹曡幏寮傚父</exception>
-        public async Task<int> UptUserPassWord(string pwdOld, string pwdNew, string pwdNewTwo, int userId)
+        public async Task<int> UptUserPassWord(string pwdNew, int userId)
         {
-            //鎹曡幏寮傚父
-            try
-            {
-                //楠岃瘉涓ゆ鏂板瘑鐮佹槸鍚︿竴鑷�
-                if (pwdNew == pwdNewTwo)
-                {
-                    int i = await UserSvc.UptUserPassWord(pwdOld, pwdNew, pwdNewTwo, userId);
-                    return i;
-                }
-                else
-                {
-                    return 3;
-                }
-            }
-            catch (Exception ex)
-            {
-                //鎶涘嚭寮傚父
-                throw new Exception("淇敼瀵嗙爜寮傚父",ex);
-            }
+            return await Db.Updateable<SysUserInfor>()
+                            .Where(s => s.Id == userId)
+                            .SetColumns(s => s.UpdateTime == DateTime.Now)
+                            .SetColumns(s => s.UpdateUser == _userManager.UserId)
+                            .SetColumns(s => s.SetPasswordTime == DateTime.Now)
+                            .ExecuteCommandAsync();
         }
         /// <summary>
         /// x澶╁悗鎻愰啋鐢ㄦ埛淇敼瀵嗙爜
         /// </summary>
         /// <param name="userId"></param>
         /// <returns></returns>
-        public int IsPassWordTime(int userId)
+        public async Task<int> IsPassWordTime(int userId)
         {
-            //鎹曡幏寮傚父
-            try
+            SysFunSetting funSettings = await Db.Queryable<SysFunSetting>().FirstAsync(a => a.IsDel == "0" && a.FunSetNo == "Fun026");
+            if (funSettings != null && funSettings.IsEnable == "NO")
             {
-                SysFunSetting funSettings = _setting.GetFunSettingByNo("Fun026");
-                if (funSettings != null && funSettings.IsEnable == "NO")
+                SysUserInfor userinfo = await Db.Queryable<SysUserInfor>().FirstAsync(s => s.Id == userId && s.IsDel == "0");
+                if (userinfo.SetPasswordTime != null)
                 {
-                    SysUserInfor userinfo = UserSvc.GetAllAsync().First(m => m.Id == userId);
-                    if (userinfo.SetPasswordTime != null)
+                    int days = string.IsNullOrEmpty(funSettings.SetValue) ? 30 : Convert.ToInt32(funSettings.SetValue);
+                    bool isTime = userinfo.SetPasswordTime.Value.AddDays(days) < DateTime.Now;
+                    if (isTime)
                     {
-                        int days = string.IsNullOrEmpty(funSettings.SetValue) ? 30 : Convert.ToInt32(funSettings.SetValue);
-                        bool isTime = userinfo.SetPasswordTime.Value.AddDays(days) < DateTime.Now;
-                        if (isTime)
-                        {
-                            return 1;
-                        }
+                        return 1;
                     }
                 }
-                return 0;
             }
-            catch (Exception ex)
-            {
-                //鎶涘嚭寮傚父
-                throw new Exception("淇敼瀵嗙爜寮傚父", ex);
-            }
+            return 0;
         }
     }
 }

--
Gitblit v1.8.0