You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
9.6 KiB

5 months ago
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<SysUser>
{
private readonly SysUserRepository _sysUserRepository;
private readonly SysUserRoleMgr _sysUserRoleMgr;
public SysUserService(
SysUserRepository sysUserRepository,
SysUserRoleMgr sysUserRoleService)
{
_sysUserRepository = sysUserRepository;
_sysUserRoleMgr = sysUserRoleService;
}
/// <summary>
/// 获取用户分页列表 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("获取用户分页列表")]
public async Task<SqlSugarPagedList<SysUser>> Page(PageUserReq input)
{
RefAsync<int> total = 0;
var items = await _sysUserRepository.QueryPageAsync(
!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);
}
/// <summary>
/// 增加用户 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<string> 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;
}
/// <summary>
/// 更新角色和扩展机构
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
private async Task UpdateRoleAndExtOrg(AddUserReq input)
{
await GrantRole(new UserRoleReq { UserId = input.Id, RoleIdList = input.RoleIdList });
}
/// <summary>
/// 授权用户角色 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task GrantRole(UserRoleReq input)
{
await _sysUserRoleMgr.GrantUserRole(input);
}
/// <summary>
/// 更新用户 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<string> 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<SysUser>(), 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);
}
/// <summary>
/// 删除用户 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 查看用户基本信息 🔖
/// </summary>
/// <returns></returns>
public virtual async Task<SysUser?> GetBaseInfo(String Account)
{
if (await _sysUserRepository.QueryByClauseAsync(u => u.Account == Account) == null)
return default;
return await _sysUserRepository.QueryByClauseAsync(u => u.Account == Account);
}
/// <summary>
/// 更新用户基本信息 🔖
/// </summary>
/// <returns></returns>
public virtual async Task<bool> UpdateBaseInfo(SysUser user)
{
//return await _sysUserRepository.UpdateAsync(u => new { u.CreateTime, u.Account, u.Password, u.AccountType });
return await _sysUserRepository.UpdateAsync(user);
}
/// <summary>
/// 设置用户状态 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("设置用户状态")]
public virtual async Task<string> 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 ? "启用" : "禁用") + "成功";
}
/// <summary>
/// 修改用户密码 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<string> 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<RedisHelper>();
/// <summary>
/// 解除登录锁定 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public virtual async Task<bool> 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");
}
/// <summary>
/// 获取用户拥有角色集合 🔖
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
[DisplayName("获取用户拥有角色集合")]
public async Task<List<long>> GetOwnRoleList(long userId)
{
return await _sysUserRoleMgr.GetUserRoleIdList(userId);
}
/// <summary>
/// 重置用户密码 🔖
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[DisplayName("重置用户密码")]
public virtual async Task<string> 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<string>(CommonConst.SysPassword);
user.Password = CryptogramUtil.Encrypt(CommonConst.SysPassword);
await _sysUserRepository.UpdateColumnsAsync(user, u => u.Password);
return result = CommonConst.SysPassword;
}
}
}