using System.ComponentModel; using Autofac; using Common.Const; using Common.Enum; using Common.Util; using Entity.Base; using Entity.DbModel.System.SysBaseObject; using Entity.Dto.Req; using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Autofac.Attribute; using HybirdFrameworkCore.Redis; using Mapster; using Repository.System; using Service.Mgr; using SqlSugar; namespace Service.System { [Scope("SingleInstance")] public class SysUserService : BaseServices { private readonly SysUserRepository _sysUserRepository; private readonly SysUserRoleMgr _sysUserRoleMgr; public SysUserService( SysUserRepository sysUserRepository, SysUserRoleMgr sysUserRoleService) { _sysUserRepository = sysUserRepository; _sysUserRoleMgr = sysUserRoleService; } /// /// 获取用户分页列表 🔖 /// /// /// [DisplayName("获取用户分页列表")] public async Task> Page(PageUserReq input) { RefAsync total = 0; var items = await _sysUserRepository.QueryPageAsync( entity => true, false, entity => true, !string.IsNullOrEmpty(input.Account), u => u.Account == input.Account, !string.IsNullOrEmpty(input.RealName), u => u.RealName == input.RealName, u => u.CreateTime, input.Page, input.PageSize, total ); return SqlSugarPagedExtensions.CreateSqlSugarPagedList(items, total, input.Page, input.PageSize); } /// /// 增加用户 🔖 /// /// /// public virtual async Task AddUser(AddUserReq input) { string result = ""; var isExist = await _sysUserRepository.QueryByClauseAsync(u => u.Account == input.Account); if (isExist != null) return result = "账号已存在"; input.Password = CryptogramUtil.Encrypt(input.Password); await _sysUserRepository.InsertAsync(input); SysUser newAddUser = await _sysUserRepository.QueryByClauseAsync(u => u.Account == input.Account); input.Id = newAddUser.Id; await UpdateRoleAndExtOrg(input); return result = "新增账号:" + newAddUser.Id; } /// /// 更新角色和扩展机构 /// /// /// private async Task UpdateRoleAndExtOrg(AddUserReq input) { await GrantRole(new UserRoleReq { UserId = input.Id, RoleIdList = input.RoleIdList }); } /// /// 授权用户角色 🔖 /// /// /// public async Task GrantRole(UserRoleReq input) { await _sysUserRoleMgr.GrantUserRole(input); } /// /// 更新用户 🔖 /// /// /// public virtual async Task UpdateUser(UpdateUserReq input) { string result = ""; if (await _sysUserRepository.UpdateAsync(u => u.Account == input.Account && u.Id != input.Id)) return result = "账号已存在"; await _sysUserRepository.UpdateAsync(input.Adapt(), true, u => new { u.Password, u.Status }); await UpdateRoleAndExtOrg(input); return "更新用户" + input.Account; // 若账号的角色和组织架构发生变化,则强制下线账号进行权限更新 //todo //var roleIds = await GetOwnRoleList(input.Id); //if (!input.RoleIdList.OrderBy(u => u).SequenceEqual(roleIds.OrderBy(u => u))) // await _sysOnlineUserService.ForceOffline(input.Id); } /// /// 删除用户 🔖 /// /// /// public virtual async Task DeleteUser(DeleteUserReq input) { //input.Id:用户主键ID var user = await _sysUserRepository.QueryByClauseAsync(u => u.Id == input.Id); if (user == null) throw new ArgumentException($"账号不存在"); //超级管理员不可以删除 if (user.AccountType == AccountTypeEnum.SuperAdmin) throw new ArgumentException($"禁止删除超级管理员"); await _sysUserRepository.DeleteAsync(user); // 删除用户角色 await _sysUserRoleMgr.DeleteUserRoleByUserId(input.Id); } /// /// 查看用户基本信息 🔖 /// /// public virtual async Task GetBaseInfo(String Account) { if (await _sysUserRepository.QueryByClauseAsync(u => u.Account == Account) == null) return default; return await _sysUserRepository.QueryByClauseAsync(u => u.Account == Account); } /// /// 更新用户基本信息 🔖 /// /// public virtual async Task UpdateBaseInfo(SysUser user) { //return await _sysUserRepository.UpdateAsync(u => new { u.CreateTime, u.Account, u.Password, u.AccountType }); return await _sysUserRepository.UpdateAsync(user); } /// /// 设置用户状态 🔖 /// /// /// [DisplayName("设置用户状态")] public virtual async Task SetStatus(UserReq input) { string result = ""; if (UserManager.UserId == input.Id) return result = "禁止修改本人账号状态"; var user = await _sysUserRepository.QueryByClauseAsync(u => u.Id == input.Id); if (user == null) return result = "账号不存在"; if (user.AccountType == AccountTypeEnum.SuperAdmin) return result = "禁止修改超级管理员状态"; if (!Enum.IsDefined(typeof(StatusEnum), input.Status)) return result = "字典状态错误"; user.Status = input.Status; await _sysUserRepository.UpdateColumnsAsync(user, u => new { u.Status }); return result = "设置用户" + input.Id + "状态:" + (input.Status == StatusEnum.Enable ? "启用" : "禁用") + "成功"; } /// /// 修改用户密码 🔖 /// /// /// public virtual async Task ChangePwd(ChangePwdReq input) { string result = ""; // 国密SM2解密(前端密码传输SM2加密后的) input.PasswordOld = CryptogramUtil.SM2Decrypt(input.PasswordOld); input.PasswordNew = CryptogramUtil.SM2Decrypt(input.PasswordNew); var user = await _sysUserRepository.QueryByClauseAsync(u => u.Account == UserManager.Account); if (CryptogramUtil.Decrypt(user.Password) != input.PasswordOld) return result = "旧密码不匹配"; if (input.PasswordOld == input.PasswordNew) return result = "新密码不能与旧密码相同"; user.Password = CryptogramUtil.Encrypt(input.PasswordNew); await _sysUserRepository.UpdateAsync(user); return result = "修改密码成功"; } RedisHelper redisHelper = AppInfo.Container.Resolve(); /// /// 解除登录锁定 🔖 /// /// /// public virtual async Task UnlockLogin(UnlockLoginReq input) { var user = await _sysUserRepository.QueryByClauseAsync(u => u.Id == input.Id); if (user == null) throw new ArgumentException($"账号不存在"); var keyErrorPasswordCount = $"{RedisConstMgr.KeyErrorPasswordCount}{user.Account}"; // 清空密码错误次数 return redisHelper.SetKeyValueStr(keyErrorPasswordCount, "0"); } /// /// 获取用户拥有角色集合 🔖 /// /// /// [DisplayName("获取用户拥有角色集合")] public async Task> GetOwnRoleList(long userId) { return await _sysUserRoleMgr.GetUserRoleIdList(userId); } /// /// 重置用户密码 🔖 /// /// /// [DisplayName("重置用户密码")] public virtual async Task ResetPwd(ResetPwdUserReq input) { string result = ""; var user = await _sysUserRepository.QueryByClauseAsync(u => u.Id == input.Id); if (user == null) return result = "账户不存在"; //var password = await _sysConfigService.GetConfigValue(CommonConst.SysPassword); user.Password = CryptogramUtil.Encrypt(CommonConst.SysPassword); await _sysUserRepository.UpdateColumnsAsync(user, u => u.Password); return result = CommonConst.SysPassword; } } }