Files
Yi.Admin/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs

268 lines
11 KiB
C#
Raw Normal View History

2023-12-11 09:55:12 +08:00
using Microsoft.AspNetCore.Mvc;
2023-04-13 21:12:06 +08:00
using SqlSugar;
2023-12-11 09:55:12 +08:00
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Caching;
using Volo.Abp.EventBus.Local;
2023-12-11 09:55:12 +08:00
using Volo.Abp.Users;
2024-08-16 17:57:58 +08:00
using Yi.Framework.Bbs.Domain.Shared.Enums;
using Yi.Framework.Bbs.Domain.Shared.Etos;
2023-12-11 09:55:12 +08:00
using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.User;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Authorization;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Managers;
using Yi.Framework.Rbac.Domain.Repositories;
using Yi.Framework.Rbac.Domain.Shared.Caches;
2023-12-11 09:55:12 +08:00
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.Rbac.Domain.Shared.Etos;
2023-12-11 09:55:12 +08:00
using Yi.Framework.Rbac.Domain.Shared.OperLog;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Rbac.Application.Services.System
2023-04-13 21:12:06 +08:00
{
/// <summary>
/// User服务实现
/// </summary>
2024-08-16 17:57:58 +08:00
public class UserService : YiCrudAppService<UserAggregateRoot, UserGetOutputDto, UserGetListOutputDto, Guid,
UserGetListInputVo, UserCreateInputVo, UserUpdateInputVo>, IUserService
2023-12-11 09:55:12 +08:00
//IUserService
2023-04-13 21:12:06 +08:00
{
2024-08-16 17:57:58 +08:00
protected ILocalEventBus LocalEventBus => LazyServiceProvider.LazyGetRequiredService<ILocalEventBus>();
public UserService(ISqlSugarRepository<UserAggregateRoot, Guid> repository, UserManager userManager,
IUserRepository userRepository, ICurrentUser currentUser, IDeptService deptService,
ILocalEventBus localEventBus,
IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache) : base(repository)
2023-12-11 09:55:12 +08:00
=>
2024-08-16 17:57:58 +08:00
(_userManager, _userRepository, _currentUser, _deptService, _repository, _localEventBus) =
(userManager, userRepository, currentUser, deptService, repository, localEventBus);
2023-04-13 21:12:06 +08:00
private UserManager _userManager { get; set; }
2024-05-22 14:35:08 +08:00
private ISqlSugarRepository<UserAggregateRoot, Guid> _repository;
2023-04-13 21:12:06 +08:00
private IUserRepository _userRepository { get; set; }
2023-05-21 13:16:04 +08:00
private IDeptService _deptService { get; set; }
2023-04-15 17:33:42 +08:00
2023-04-13 21:12:06 +08:00
private ICurrentUser _currentUser { get; set; }
private ILocalEventBus _localEventBus;
2024-08-16 17:57:58 +08:00
2023-04-13 21:12:06 +08:00
/// <summary>
/// 查询用户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
2023-04-18 20:29:53 +08:00
[Permission("system:user:list")]
2023-04-13 21:12:06 +08:00
public override async Task<PagedResultDto<UserGetListOutputDto>> GetListAsync(UserGetListInputVo input)
{
RefAsync<int> total = 0;
2023-12-11 09:55:12 +08:00
List<Guid> deptIds = null;
2023-05-21 13:16:04 +08:00
if (input.DeptId is not null)
{
2023-12-11 09:55:12 +08:00
deptIds = await _deptService.GetChildListAsync(input.DeptId ?? Guid.Empty);
2023-05-21 13:16:04 +08:00
}
2023-04-13 21:12:06 +08:00
2023-12-11 09:55:12 +08:00
List<Guid> ids = input.Ids?.Split(",").Select(x => Guid.Parse(x)).ToList();
2024-08-16 17:57:58 +08:00
var outPut = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.UserName),
x => x.UserName.Contains(input.UserName!))
.WhereIF(input.Phone is not null, x => x.Phone.ToString()!.Contains(input.Phone.ToString()!))
.WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name!.Contains(input.Name!))
.WhereIF(input.State is not null, x => x.State == input.State)
.WhereIF(input.StartTime is not null && input.EndTime is not null,
x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
//这个为过滤当前部门,加入数据权限后,将由数据权限控制
.WhereIF(input.DeptId is not null, x => deptIds.Contains(x.DeptId ?? Guid.Empty))
.WhereIF(ids is not null, x => ids.Contains(x.Id))
.LeftJoin<DeptAggregateRoot>((user, dept) => user.DeptId == dept.Id)
.OrderByDescending(user => user.CreationTime)
.Select((user, dept) => new UserGetListOutputDto(), true)
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
2023-04-13 21:12:06 +08:00
var result = new PagedResultDto<UserGetListOutputDto>();
result.Items = outPut;
2023-12-11 09:55:12 +08:00
result.TotalCount = total;
2023-04-13 21:12:06 +08:00
return result;
}
2024-05-22 14:35:08 +08:00
protected override UserAggregateRoot MapToEntity(UserCreateInputVo createInput)
{
var output = base.MapToEntity(createInput);
output.EncryPassword = new Domain.Entities.ValueObjects.EncryPasswordValueObject(createInput.Password);
return output;
}
2023-04-13 21:12:06 +08:00
/// <summary>
/// 添加用户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[OperLog("添加用户", OperEnum.Insert)]
2024-01-02 23:26:05 +08:00
[Permission("system:user:add")]
2023-04-13 21:12:06 +08:00
public async override Task<UserGetOutputDto> CreateAsync(UserCreateInputVo input)
{
var entitiy = await MapToEntityAsync(input);
2023-04-13 21:12:06 +08:00
await _userManager.CreateAsync(entitiy);
await _userManager.GiveUserSetRoleAsync(new List<Guid> { entitiy.Id }, input.RoleIds);
await _userManager.GiveUserSetPostAsync(new List<Guid> { entitiy.Id }, input.PostIds);
2023-04-13 21:12:06 +08:00
var result = await MapToGetOutputDtoAsync(entitiy);
2023-04-15 17:33:42 +08:00
return result;
2023-04-13 21:12:06 +08:00
}
2024-05-22 14:35:08 +08:00
protected override async Task<UserAggregateRoot> MapToEntityAsync(UserCreateInputVo createInput)
{
var entitiy = await base.MapToEntityAsync(createInput);
entitiy.BuildPassword();
return entitiy;
}
2023-04-13 21:12:06 +08:00
/// <summary>
/// 单查
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
2024-09-24 11:16:19 +08:00
[Permission("system:user:list")]
2023-12-11 09:55:12 +08:00
public override async Task<UserGetOutputDto> GetAsync(Guid id)
2023-04-13 21:12:06 +08:00
{
//使用导航树形查询
2024-08-16 17:57:58 +08:00
var entity = await _repository._DbQueryable.Includes(u => u.Roles).Includes(u => u.Posts)
.Includes(u => u.Dept).InSingleAsync(id);
2023-04-13 21:12:06 +08:00
return await MapToGetOutputDtoAsync(entity);
}
/// <summary>
/// 更新用户
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
[OperLog("更新用户", OperEnum.Update)]
[Permission("system:user:edit")]
2025-06-21 13:29:41 +08:00
public override async Task<UserGetOutputDto> UpdateAsync(Guid id, UserUpdateInputVo input)
2023-04-13 21:12:06 +08:00
{
2025-06-21 13:29:41 +08:00
//不是超管,不能更新成超管
if (!UserConst.Admin.Equals(CurrentUser.UserName))
2024-02-08 19:48:35 +08:00
{
2025-06-21 13:29:41 +08:00
if (input.UserName == UserConst.Admin || input.UserName == UserConst.TenantAdmin)
{
throw new UserFriendlyException(UserConst.Name_Not_Allowed);
}
2024-02-08 19:48:35 +08:00
}
2024-08-16 17:57:58 +08:00
2023-04-13 21:12:06 +08:00
if (await _repository.IsAnyAsync(u => input.UserName!.Equals(u.UserName) && !id.Equals(u.Id)))
{
2024-09-24 11:16:19 +08:00
throw new UserFriendlyException(UserConst.Exist);
2023-04-13 21:12:06 +08:00
}
2024-08-16 17:57:58 +08:00
2023-04-13 21:12:06 +08:00
var entity = await _repository.GetByIdAsync(id);
//更新密码,特殊处理
2024-10-07 22:24:02 +08:00
if (!string.IsNullOrWhiteSpace(input.Password))
2023-04-13 21:12:06 +08:00
{
2024-04-22 18:06:09 +08:00
entity.EncryPassword.Password = input.Password;
2023-04-13 21:12:06 +08:00
entity.BuildPassword();
}
2024-08-16 17:57:58 +08:00
2023-04-13 21:12:06 +08:00
await MapToEntityAsync(input, entity);
2023-04-15 17:33:42 +08:00
var res1 = await _repository.UpdateAsync(entity);
2023-12-11 09:55:12 +08:00
await _userManager.GiveUserSetRoleAsync(new List<Guid> { id }, input.RoleIds);
await _userManager.GiveUserSetPostAsync(new List<Guid> { id }, input.PostIds);
2023-04-13 21:12:06 +08:00
return await MapToGetOutputDtoAsync(entity);
}
/// <summary>
/// 更新个人中心
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[OperLog("更新个人信息", OperEnum.Update)]
public async Task<UserGetOutputDto> UpdateProfileAsync(ProfileUpdateInputVo input)
{
var entity = await _repository.GetByIdAsync(_currentUser.Id);
2023-12-11 09:55:12 +08:00
ObjectMapper.Map(input, entity);
2023-04-13 21:12:06 +08:00
await _repository.UpdateAsync(entity);
2023-12-11 09:55:12 +08:00
var dto = await MapToGetOutputDtoAsync(entity);
2024-08-16 17:57:58 +08:00
//发布更新昵称任务事件
2025-07-13 21:26:46 +08:00
if (input.Nick != entity.Nick)
2024-08-16 17:57:58 +08:00
{
await this.LocalEventBus.PublishAsync(
new AssignmentEventArgs(AssignmentRequirementTypeEnum.UpdateNick, _currentUser.GetId(), input.Nick),
false);
}
2023-04-13 21:12:06 +08:00
return dto;
}
/// <summary>
/// 更新状态
/// </summary>
/// <param name="id"></param>
/// <param name="state"></param>
/// <returns></returns>
2023-12-11 09:55:12 +08:00
[Route("user/{id}/{state}")]
2023-04-13 21:12:06 +08:00
[OperLog("更新用户状态", OperEnum.Update)]
2024-01-02 23:26:05 +08:00
[Permission("system:user:update")]
2023-12-11 09:55:12 +08:00
public async Task<UserGetOutputDto> UpdateStateAsync([FromRoute] Guid id, [FromRoute] bool state)
2023-04-13 21:12:06 +08:00
{
var entity = await _repository.GetByIdAsync(id);
if (entity is null)
{
throw new ApplicationException("用户未存在");
}
2024-08-16 17:57:58 +08:00
2023-04-13 21:12:06 +08:00
entity.State = state;
await _repository.UpdateAsync(entity);
return await MapToGetOutputDtoAsync(entity);
}
2024-08-16 17:57:58 +08:00
2023-04-13 21:12:06 +08:00
[OperLog("删除用户", OperEnum.Delete)]
2023-12-15 23:44:35 +08:00
[Permission("system:user:delete")]
2023-12-11 09:55:12 +08:00
public override async Task DeleteAsync(Guid id)
2023-04-13 21:12:06 +08:00
{
2023-12-11 09:55:12 +08:00
await base.DeleteAsync(id);
2023-04-13 21:12:06 +08:00
}
2024-01-02 23:26:05 +08:00
[Permission("system:user:export")]
public override Task<IActionResult> GetExportExcelAsync(UserGetListInputVo input)
{
return base.GetExportExcelAsync(input);
}
[Permission("system:user:import")]
public override Task PostImportExcelAsync(List<UserCreateInputVo> input)
{
return base.PostImportExcelAsync(input);
}
/// <summary>
/// 通过角色代码给用户添加角色
/// </summary>
/// <param name="userId">用户ID</param>
/// <param name="roleCodes"></param>
/// <returns></returns>
public async Task AddUserRoleByRoleCodeAsync(Guid userId, List<string> roleCodes)
{
// 根据角色代码查找角色ID
var roleRepository = LazyServiceProvider.LazyGetRequiredService<ISqlSugarRepository<RoleAggregateRoot>>();
var roleIds = await roleRepository._DbQueryable
.Where(r => roleCodes.Contains(r.RoleCode) && r.State == true)
.Select(r=>r.Id)
.ToListAsync();
if (!roleIds.Any())
{
return;
}
// 使用UserManager给用户设置角色
await _userManager.GiveUserSetRoleAsync(new List<Guid> { userId }, roleIds);
}
2023-04-13 21:12:06 +08:00
}
2024-08-16 17:57:58 +08:00
}