fix: 修复值对象查询导致问题,已同步sqlsugar更新

This commit is contained in:
陈淳
2024-04-26 19:08:18 +08:00
parent 4c12626b44
commit ae30d4b2cb
4 changed files with 53 additions and 36 deletions

View File

@@ -3,6 +3,7 @@ using SqlSugar;
using TencentCloud.Tcr.V20190924.Models; using TencentCloud.Tcr.V20190924.Models;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Caching;
using Volo.Abp.EventBus.Local; using Volo.Abp.EventBus.Local;
using Volo.Abp.Users; using Volo.Abp.Users;
using Yi.Framework.Ddd.Application; using Yi.Framework.Ddd.Application;
@@ -12,6 +13,7 @@ using Yi.Framework.Rbac.Domain.Authorization;
using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Managers; using Yi.Framework.Rbac.Domain.Managers;
using Yi.Framework.Rbac.Domain.Repositories; using Yi.Framework.Rbac.Domain.Repositories;
using Yi.Framework.Rbac.Domain.Shared.Caches;
using Yi.Framework.Rbac.Domain.Shared.Consts; using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.Rbac.Domain.Shared.Etos; using Yi.Framework.Rbac.Domain.Shared.Etos;
using Yi.Framework.Rbac.Domain.Shared.OperLog; using Yi.Framework.Rbac.Domain.Shared.OperLog;
@@ -25,10 +27,11 @@ namespace Yi.Framework.Rbac.Application.Services.System
public class UserService : YiCrudAppService<UserEntity, UserGetOutputDto, UserGetListOutputDto, Guid, UserGetListInputVo, UserCreateInputVo, UserUpdateInputVo>,IUserService public class UserService : YiCrudAppService<UserEntity, UserGetOutputDto, UserGetListOutputDto, Guid, UserGetListInputVo, UserCreateInputVo, UserUpdateInputVo>,IUserService
//IUserService //IUserService
{ {
public UserService(ISqlSugarRepository<UserEntity, Guid> repository, UserManager userManager, IUserRepository userRepository, ICurrentUser currentUser, IDeptService deptService, ILocalEventBus localEventBus) : base(repository) private IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> _userCache;
public UserService(ISqlSugarRepository<UserEntity, Guid> repository, UserManager userManager, IUserRepository userRepository, ICurrentUser currentUser, IDeptService deptService, ILocalEventBus localEventBus, IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache) : base(repository)
=> =>
(_userManager, _userRepository, _currentUser, _deptService, _repository, _localEventBus) = (_userManager, _userRepository, _currentUser, _deptService, _repository, _localEventBus, _userCache) =
(userManager, userRepository, currentUser, deptService, repository, localEventBus); (userManager, userRepository, currentUser, deptService, repository, localEventBus, userCache);
private UserManager _userManager { get; set; } private UserManager _userManager { get; set; }
private ISqlSugarRepository<UserEntity, Guid> _repository; private ISqlSugarRepository<UserEntity, Guid> _repository;
private IUserRepository _userRepository { get; set; } private IUserRepository _userRepository { get; set; }
@@ -77,6 +80,14 @@ namespace Yi.Framework.Rbac.Application.Services.System
return result; return result;
} }
protected override UserEntity MapToEntity(UserCreateInputVo createInput)
{
var output= base.MapToEntity(createInput);
output.EncryPassword = new Domain.Entities.ValueObjects.EncryPasswordValueObject(createInput.Password);
return output;
}
/// <summary> /// <summary>
/// 添加用户 /// 添加用户
/// </summary> /// </summary>
@@ -99,13 +110,13 @@ namespace Yi.Framework.Rbac.Application.Services.System
{ {
throw new UserFriendlyException(UserConst.User_Exist); throw new UserFriendlyException(UserConst.User_Exist);
} }
var entities = await MapToEntityAsync(input); var entitiy = await MapToEntityAsync(input);
entities.BuildPassword(); entitiy.BuildPassword();
//using (var uow = _unitOfWorkManager.CreateContext()) //using (var uow = _unitOfWorkManager.CreateContext())
//{ //{
var returnEntity = await _repository.InsertReturnEntityAsync(entities); var returnEntity = await _repository.InsertReturnEntityAsync(entitiy);
await _userManager.GiveUserSetRoleAsync(new List<Guid> { returnEntity.Id }, input.RoleIds); await _userManager.GiveUserSetRoleAsync(new List<Guid> { returnEntity.Id }, input.RoleIds);
await _userManager.GiveUserSetPostAsync(new List<Guid> { returnEntity.Id }, input.PostIds); await _userManager.GiveUserSetPostAsync(new List<Guid> { returnEntity.Id }, input.PostIds);
//uow.Commit(); //uow.Commit();
@@ -156,13 +167,13 @@ namespace Yi.Framework.Rbac.Application.Services.System
entity.BuildPassword(); entity.BuildPassword();
} }
await MapToEntityAsync(input, entity); await MapToEntityAsync(input, entity);
//using (var uow = _unitOfWorkManager.CreateContext())
//{
var res1 = await _repository.UpdateAsync(entity); var res1 = await _repository.UpdateAsync(entity);
await _userManager.GiveUserSetRoleAsync(new List<Guid> { id }, input.RoleIds); await _userManager.GiveUserSetRoleAsync(new List<Guid> { id }, input.RoleIds);
await _userManager.GiveUserSetPostAsync(new List<Guid> { id }, input.PostIds); await _userManager.GiveUserSetPostAsync(new List<Guid> { id }, input.PostIds);
// uow.Commit();
//} await _userCache.RefreshAsync(new UserInfoCacheKey(_currentUser.GetId()));
return await MapToGetOutputDtoAsync(entity); return await MapToGetOutputDtoAsync(entity);
} }
@@ -179,6 +190,7 @@ namespace Yi.Framework.Rbac.Application.Services.System
await _repository.UpdateAsync(entity); await _repository.UpdateAsync(entity);
var dto = await MapToGetOutputDtoAsync(entity); var dto = await MapToGetOutputDtoAsync(entity);
await _userCache.RefreshAsync(new UserInfoCacheKey(_currentUser.GetId()));
return dto; return dto;
} }
@@ -200,13 +212,16 @@ namespace Yi.Framework.Rbac.Application.Services.System
} }
entity.State = state; entity.State = state;
await _repository.UpdateAsync(entity); await _repository.UpdateAsync(entity);
await _userCache.RefreshAsync(new UserInfoCacheKey(id));
return await MapToGetOutputDtoAsync(entity); return await MapToGetOutputDtoAsync(entity);
} }
[OperLog("删除用户", OperEnum.Delete)] [OperLog("删除用户", OperEnum.Delete)]
[Permission("system:user:delete")] [Permission("system:user:delete")]
public override async Task DeleteAsync(Guid id) public override async Task DeleteAsync(Guid id)
{ {
await base.DeleteAsync(id); await base.DeleteAsync(id);
await _userCache.RefreshAsync(new UserInfoCacheKey(id));
} }
[Permission("system:user:export")] [Permission("system:user:export")]

View File

@@ -59,7 +59,7 @@ namespace Yi.Framework.Rbac.Domain.Entities
/// 加密密码 /// 加密密码
/// </summary> /// </summary>
[SugarColumn(IsOwnsOne = true)] [SugarColumn(IsOwnsOne = true)]
public EncryPasswordValueObject EncryPassword { get; set; } public EncryPasswordValueObject EncryPassword { get; set; } = new EncryPasswordValueObject();
///// <summary> ///// <summary>
///// 密码 ///// 密码
@@ -182,7 +182,7 @@ namespace Yi.Framework.Rbac.Domain.Entities
//如果不传值那就把自己的password当作传进来的password //如果不传值那就把自己的password当作传进来的password
if (password == null) if (password == null)
{ {
if (EncryPassword.Password == null) if (EncryPassword?.Password == null)
{ {
throw new ArgumentNullException(nameof(EncryPassword.Password)); throw new ArgumentNullException(nameof(EncryPassword.Password));
} }

View File

@@ -95,42 +95,26 @@ namespace Yi.Framework.Rbac.Domain.Managers
/// <returns></returns> /// <returns></returns>
public async Task<UserRoleMenuDto> GetInfoAsync(Guid userId) public async Task<UserRoleMenuDto> GetInfoAsync(Guid userId)
{ {
var user = await _userRepository.GetUserAllInfoAsync(userId);
var output = await GetInfoByCacheAsync(user); var output = await GetInfoByCacheAsync(userId);
return output; return output;
} }
private async Task<UserRoleMenuDto> GetInfoByCacheAsync(Guid userId)
/// <summary>
/// 批量查询用户信息
/// </summary>
/// <param name="userIds"></param>
/// <returns></returns>
public async Task<List<UserRoleMenuDto>> GetInfoListAsync(List<Guid> userIds)
{
List<UserRoleMenuDto> output = new List<UserRoleMenuDto>();
var users = await _userRepository.GetListUserAllInfoAsync(userIds);
foreach (var user in users)
{
output.Add(await GetInfoByCacheAsync(user));
}
return output;
}
private async Task<UserRoleMenuDto> GetInfoByCacheAsync(UserEntity user)
{ {
//此处优先从缓存中获取 //此处优先从缓存中获取
UserRoleMenuDto output = null; UserRoleMenuDto output = null;
var tokenExpiresMinuteTime = LazyServiceProvider.GetRequiredService<IOptions<JwtOptions>>().Value.ExpiresMinuteTime; var tokenExpiresMinuteTime = LazyServiceProvider.GetRequiredService<IOptions<JwtOptions>>().Value.ExpiresMinuteTime;
var cacheData = await _userCache.GetOrAddAsync(new UserInfoCacheKey(user.Id), var cacheData = await _userCache.GetOrAddAsync(new UserInfoCacheKey(userId),
async () => async () =>
{ {
var user = await _userRepository.GetUserAllInfoAsync(userId);
var data = EntityMapToDto(user); var data = EntityMapToDto(user);
//系统用户数据被重置,老前端访问重新授权 //系统用户数据被重置,老前端访问重新授权
if (data is null) if (data is null)
{ {
throw new AbpAuthorizationException(); throw new AbpAuthorizationException();
} }
data.Menus.Clear(); //data.Menus.Clear();
output = data; output = data;
return new UserInfoCacheItem(data); return new UserInfoCacheItem(data);
}, },
@@ -143,6 +127,24 @@ namespace Yi.Framework.Rbac.Domain.Managers
return output!; return output!;
} }
/// <summary>
/// 批量查询用户信息
/// </summary>
/// <param name="userIds"></param>
/// <returns></returns>
public async Task<List<UserRoleMenuDto>> GetInfoListAsync(List<Guid> userIds)
{
List<UserRoleMenuDto> output = new List<UserRoleMenuDto>();
foreach (var userId in userIds)
{
output.Add(await GetInfoByCacheAsync(userId));
}
return output;
}
private UserRoleMenuDto EntityMapToDto(UserEntity user) private UserRoleMenuDto EntityMapToDto(UserEntity user)
{ {

View File

@@ -1,6 +1,6 @@
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<AbpVersion>8.0.5</AbpVersion> <AbpVersion>8.0.5</AbpVersion>
<SqlSugarVersion>5.1.4.149</SqlSugarVersion> <SqlSugarVersion>5.1.4.154-preview01</SqlSugarVersion>
</PropertyGroup> </PropertyGroup>
</Project> </Project>