diff --git a/Yi.Abp.Net8/framework/Yi.Framework.Ddd.Application/YiCrudAppService.cs b/Yi.Abp.Net8/framework/Yi.Framework.Ddd.Application/YiCrudAppService.cs index d9a8e346..b2d1475c 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.Ddd.Application/YiCrudAppService.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.Ddd.Application/YiCrudAppService.cs @@ -8,9 +8,11 @@ using Volo.Abp.Domain.Repositories; namespace Yi.Framework.Ddd.Application { - public abstract class YiCrudAppService : YiCrudAppService - where TEntity : class, IEntity - where TEntityDto : IEntityDto + public abstract class + YiCrudAppService : YiCrudAppService + where TEntity : class, IEntity + where TEntityDto : IEntityDto { protected YiCrudAppService(IRepository repository) : base(repository) { @@ -49,16 +51,53 @@ namespace Yi.Framework.Ddd.Application } - public abstract class YiCrudAppService + public abstract class YiCrudAppService : CrudAppService - where TEntity : class, IEntity - where TGetOutputDto : IEntityDto - where TGetListOutputDto : IEntityDto + where TEntity : class, IEntity + where TGetOutputDto : IEntityDto + where TGetListOutputDto : IEntityDto { protected YiCrudAppService(IRepository repository) : base(repository) { } + public override async Task UpdateAsync(TKey id, TUpdateInput input) + { + await CheckUpdatePolicyAsync(); + + var entity = await GetEntityByIdAsync(id); + await CheckUpdateInputDtoAsync(entity,input); + + await MapToEntityAsync(input, entity); + await Repository.UpdateAsync(entity, autoSave: true); + + return await MapToGetOutputDtoAsync(entity); + } + + protected virtual Task CheckUpdateInputDtoAsync(TEntity entity,TUpdateInput input) + { + return Task.CompletedTask; + } + + public override async Task CreateAsync(TCreateInput input) + { + await CheckCreatePolicyAsync(); + await CheckCreateInputDtoAsync(input); + var entity = await MapToEntityAsync(input); + + TryToSetTenantId(entity); + + await Repository.InsertAsync(entity, autoSave: true); + + return await MapToGetOutputDtoAsync(entity); + } + + protected virtual Task CheckCreateInputDtoAsync(TCreateInput input) + { + return Task.CompletedTask; + } + /// /// 多查 /// @@ -70,12 +109,14 @@ namespace Yi.Framework.Ddd.Application //区分多查还是批量查 if (input is IPagedResultRequest pagedInput) { - entites = await Repository.GetPagedListAsync(pagedInput.SkipCount, pagedInput.MaxResultCount, string.Empty); + entites = await Repository.GetPagedListAsync(pagedInput.SkipCount, pagedInput.MaxResultCount, + string.Empty); } else { entites = await Repository.GetListAsync(); } + var total = await Repository.GetCountAsync(); var output = await MapToGetListOutputDtosAsync(entites); return new PagedResultDto(total, output); @@ -146,4 +187,4 @@ namespace Yi.Framework.Ddd.Application //await Repository.InsertManyAsync(entities); } } -} +} \ No newline at end of file diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs index 50bad3c5..abf6de6b 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Repositories/SqlSugarRepository.cs @@ -18,7 +18,7 @@ namespace Yi.Framework.SqlSugarCore.Repositories { public ISqlSugarClient _Db => GetDbContextAsync().Result; - public ISugarQueryable _DbQueryable => GetDbContextAsync().Result.Queryable(); + public ISugarQueryable _DbQueryable => GetDbContextAsync().ConfigureAwait(false).GetAwaiter().GetResult().Queryable(); private ISugarDbContextProvider _sugarDbContextProvider; public IAsyncQueryableExecuter AsyncExecuter { get; } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/ConfigService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/ConfigService.cs index 85c398bf..9a7c2db8 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/ConfigService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/ConfigService.cs @@ -5,6 +5,7 @@ using Yi.Framework.Ddd.Application; using Yi.Framework.Rbac.Application.Contracts.Dtos.Config; using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Domain.Entities; +using Yi.Framework.Rbac.Domain.Shared.Consts; using Yi.Framework.SqlSugarCore.Abstractions; namespace Yi.Framework.Rbac.Application.Services @@ -12,10 +13,12 @@ namespace Yi.Framework.Rbac.Application.Services /// /// Config服务实现 /// - public class ConfigService : YiCrudAppService, - IConfigService + public class ConfigService : YiCrudAppService, + IConfigService { private ISqlSugarRepository _repository; + public ConfigService(ISqlSugarRepository repository) : base(repository) { _repository = repository; @@ -30,11 +33,33 @@ namespace Yi.Framework.Rbac.Application.Services { RefAsync total = 0; - var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.ConfigKey), x => x.ConfigKey.Contains(input.ConfigKey!)) - .WhereIF(!string.IsNullOrEmpty(input.ConfigName), x => x.ConfigName!.Contains(input.ConfigName!)) - .WhereIF(input.StartTime is not null && input.EndTime is not null, x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime) - .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); + var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.ConfigKey), + x => x.ConfigKey.Contains(input.ConfigKey!)) + .WhereIF(!string.IsNullOrEmpty(input.ConfigName), x => x.ConfigName!.Contains(input.ConfigName!)) + .WhereIF(input.StartTime is not null && input.EndTime is not null, + x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime) + .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); return new PagedResultDto(total, await MapToGetListOutputDtosAsync(entities)); } + + protected override async Task CheckCreateInputDtoAsync(ConfigCreateInputVo input) + { + var isExist = + await _repository.IsAnyAsync(x => x.ConfigKey == input.ConfigKey); + if (isExist) + { + throw new UserFriendlyException(ConfigConst.Exist); + } + } + + protected override async Task CheckUpdateInputDtoAsync(ConfigAggregateRoot entity, ConfigUpdateInputVo input) + { + var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id) + .AnyAsync(x => x.ConfigKey == input.ConfigKey); + if (isExist) + { + throw new UserFriendlyException(ConfigConst.Exist); + } + } } -} +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/DictionaryService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/DictionaryService.cs index e31d7596..41da5812 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/DictionaryService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/DictionaryService.cs @@ -6,6 +6,7 @@ using Yi.Framework.Ddd.Application; using Yi.Framework.Rbac.Application.Contracts.Dtos.Dictionary; using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Domain.Entities; +using Yi.Framework.Rbac.Domain.Shared.Consts; using Yi.Framework.SqlSugarCore.Abstractions; @@ -14,26 +15,30 @@ namespace Yi.Framework.Rbac.Application.Services /// /// Dictionary服务实现 /// - public class DictionaryService : YiCrudAppService, - IDictionaryService + public class DictionaryService : YiCrudAppService, + IDictionaryService { private ISqlSugarRepository _repository; + public DictionaryService(ISqlSugarRepository repository) : base(repository) { - _repository= repository; + _repository = repository; } /// /// 查询 /// - - public override async Task> GetListAsync(DictionaryGetListInputVo input) + public override async Task> GetListAsync( + DictionaryGetListInputVo input) { RefAsync total = 0; - var entities = await _repository._DbQueryable.WhereIF(input.DictType is not null, x => x.DictType == input.DictType) - .WhereIF(input.DictLabel is not null, x => x.DictLabel!.Contains(input.DictLabel!)) - .WhereIF(input.State is not null, x => x.State == input.State) - .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); + var entities = await _repository._DbQueryable + .WhereIF(input.DictType is not null, x => x.DictType == input.DictType) + .WhereIF(input.DictLabel is not null, x => x.DictLabel!.Contains(input.DictLabel!)) + .WhereIF(input.State is not null, x => x.State == input.State) + .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); return new PagedResultDto { TotalCount = total, @@ -55,4 +60,4 @@ namespace Yi.Framework.Rbac.Application.Services return result; } } -} +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/DictionaryTypeService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/DictionaryTypeService.cs index 441e1793..fed066c8 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/DictionaryTypeService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/DictionaryTypeService.cs @@ -6,6 +6,7 @@ using Yi.Framework.Ddd.Application; using Yi.Framework.Rbac.Application.Contracts.Dtos.DictionaryType; using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Domain.Entities; +using Yi.Framework.Rbac.Domain.Shared.Consts; using Yi.Framework.SqlSugarCore.Abstractions; namespace Yi.Framework.Rbac.Application.Services @@ -22,7 +23,7 @@ namespace Yi.Framework.Rbac.Application.Services _repository = repository; } - public async override Task> GetListAsync(DictionaryTypeGetListInputVo input) + public override async Task> GetListAsync(DictionaryTypeGetListInputVo input) { RefAsync total = 0; @@ -38,5 +39,25 @@ namespace Yi.Framework.Rbac.Application.Services Items = await MapToGetListOutputDtosAsync(entities) }; } + + protected override async Task CheckCreateInputDtoAsync(DictionaryTypeCreateInputVo input) + { + var isExist = + await _repository.IsAnyAsync(x => x.DictType == input.DictType); + if (isExist) + { + throw new UserFriendlyException(DictionaryConst.Exist); + } + } + + protected override async Task CheckUpdateInputDtoAsync(DictionaryTypeAggregateRoot entity, DictionaryTypeUpdateInputVo input) + { + var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id) + .AnyAsync(x => x.DictType == input.DictType); + if (isExist) + { + throw new UserFriendlyException(DictionaryConst.Exist); + } + } } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/NoticeService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/NoticeService.cs index f1eb66f4..47de3ab4 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/NoticeService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/NoticeService.cs @@ -41,8 +41,6 @@ namespace Yi.Framework.Rbac.Application.Services return new PagedResultDto(total, await MapToGetListOutputDtosAsync(entities)); } - - /// /// 发送在线消息 /// diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/DeptService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/DeptService.cs index 37947ac6..531d610e 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/DeptService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/DeptService.cs @@ -1,28 +1,31 @@ using SqlSugar; -using Volo.Abp; using Volo.Abp.Application.Dtos; -using Volo.Abp.Application.Services; using Yi.Framework.Ddd.Application; using Yi.Framework.Rbac.Application.Contracts.Dtos.Dept; using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Repositories; +using Yi.Framework.Rbac.Domain.Shared.Consts; namespace Yi.Framework.Rbac.Application.Services.System { /// /// Dept服务实现 /// - public class DeptService : YiCrudAppService, IDeptService + public class DeptService : YiCrudAppService, IDeptService { - private IDeptRepository _deptRepository; - public DeptService(IDeptRepository deptRepository) : base(deptRepository) - { _deptRepository = deptRepository; } + private IDeptRepository _repository; + + public DeptService(IDeptRepository repository) : base(repository) + { + _repository = repository; + } [RemoteService(false)] public async Task> GetChildListAsync(Guid deptId) { - return await _deptRepository.GetChildListAsync(deptId); + return await _repository.GetChildListAsync(deptId); } /// @@ -32,7 +35,7 @@ namespace Yi.Framework.Rbac.Application.Services.System //[Route("{roleId}")] public async Task> GetRoleIdAsync(Guid roleId) { - var entities = await _deptRepository.GetListRoleIdAsync(roleId); + var entities = await _repository.GetListRoleIdAsync(roleId); return await MapToGetListOutputDtosAsync(entities); } @@ -44,16 +47,36 @@ namespace Yi.Framework.Rbac.Application.Services.System public override async Task> GetListAsync(DeptGetListInputVo input) { RefAsync total = 0; - var entities = await _deptRepository._DbQueryable - .WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!)) - .WhereIF(input.State is not null, u => u.State == input.State) - .OrderBy(u => u.OrderNum, OrderByType.Asc) - .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); + var entities = await _repository._DbQueryable + .WhereIF(!string.IsNullOrEmpty(input.DeptName), u => u.DeptName.Contains(input.DeptName!)) + .WhereIF(input.State is not null, u => u.State == input.State) + .OrderBy(u => u.OrderNum, OrderByType.Asc) + .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); return new PagedResultDto { Items = await MapToGetListOutputDtosAsync(entities), TotalCount = total }; } + + protected override async Task CheckCreateInputDtoAsync(DeptCreateInputVo input) + { + var isExist = + await _repository.IsAnyAsync(x => x.DeptCode == input.DeptCode); + if (isExist) + { + throw new UserFriendlyException(DeptConst.Exist); + } + } + + protected override async Task CheckUpdateInputDtoAsync(DeptAggregateRoot entity, DeptUpdateInputVo input) + { + var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id) + .AnyAsync(x => x.DeptCode == input.DeptCode); + if (isExist) + { + throw new UserFriendlyException(DeptConst.Exist); + } + } } -} +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/MenuService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/MenuService.cs index 324ae02c..3d5b7baa 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/MenuService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/MenuService.cs @@ -1,10 +1,10 @@ using SqlSugar; using Volo.Abp.Application.Dtos; -using Volo.Abp.Application.Services; using Yi.Framework.Ddd.Application; using Yi.Framework.Rbac.Application.Contracts.Dtos.Menu; using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Domain.Entities; +using Yi.Framework.Rbac.Domain.Shared.Consts; using Yi.Framework.SqlSugarCore.Abstractions; namespace Yi.Framework.Rbac.Application.Services.System @@ -23,15 +23,12 @@ namespace Yi.Framework.Rbac.Application.Services.System public override async Task> GetListAsync(MenuGetListInputVo input) { - RefAsync total = 0; - var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.MenuName), x => x.MenuName.Contains(input.MenuName!)) .WhereIF(input.State is not null, x => x.State == input.State) .Where(x=>x.MenuSource==input.MenuSource) .OrderByDescending(x => x.OrderNum) .ToListAsync(); - //.ToPageListAsync(input.SkipCount, input.MaxResultCount, total); return new PagedResultDto(total, await MapToGetListOutputDtosAsync(entities)); } @@ -46,15 +43,5 @@ namespace Yi.Framework.Rbac.Application.Services.System return await MapToGetListOutputDtosAsync(entities); } - - public override Task UpdateAsync(Guid id, MenuUpdateInputVo input) - { - return base.UpdateAsync(id, input); - } - - public override Task CreateAsync(MenuCreateInputVo input) - { - return base.CreateAsync(input); - } } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/PostService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/PostService.cs index 07fc8d4b..3cf0fe9c 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/PostService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/PostService.cs @@ -1,10 +1,10 @@ using SqlSugar; using Volo.Abp.Application.Dtos; -using Volo.Abp.Application.Services; using Yi.Framework.Ddd.Application; using Yi.Framework.Rbac.Application.Contracts.Dtos.Post; using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Domain.Entities; +using Yi.Framework.Rbac.Domain.Shared.Consts; using Yi.Framework.SqlSugarCore.Abstractions; namespace Yi.Framework.Rbac.Application.Services.System @@ -12,10 +12,12 @@ namespace Yi.Framework.Rbac.Application.Services.System /// /// Post服务实现 /// - public class PostService : YiCrudAppService, - IPostService + public class PostService : YiCrudAppService, + IPostService { private readonly ISqlSugarRepository _repository; + public PostService(ISqlSugarRepository repository) : base(repository) { _repository = repository; @@ -25,10 +27,31 @@ namespace Yi.Framework.Rbac.Application.Services.System { RefAsync total = 0; - var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.PostName), x => x.PostName.Contains(input.PostName!)) - .WhereIF(input.State is not null, x => x.State == input.State) - .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); + var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.PostName), + x => x.PostName.Contains(input.PostName!)) + .WhereIF(input.State is not null, x => x.State == input.State) + .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); return new PagedResultDto(total, await MapToGetListOutputDtosAsync(entities)); } + + protected override async Task CheckCreateInputDtoAsync(PostCreateInputVo input) + { + var isExist = + await _repository.IsAnyAsync(x => x.PostCode == input.PostCode); + if (isExist) + { + throw new UserFriendlyException(PostConst.Exist); + } + } + + protected override async Task CheckUpdateInputDtoAsync(PostAggregateRoot entity, PostUpdateInputVo input) + { + var isExist = await _repository._DbQueryable.Where(x => x.Id != entity.Id) + .AnyAsync(x => x.PostCode == input.PostCode); + if (isExist) + { + throw new UserFriendlyException(RoleConst.Exist); + } + } } -} +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/RoleService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/RoleService.cs index f230591d..7b622a64 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/RoleService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/RoleService.cs @@ -11,6 +11,7 @@ using Yi.Framework.Rbac.Application.Contracts.Dtos.User; using Yi.Framework.Rbac.Application.Contracts.IServices; using Yi.Framework.Rbac.Domain.Entities; using Yi.Framework.Rbac.Domain.Managers; +using Yi.Framework.Rbac.Domain.Shared.Consts; using Yi.Framework.Rbac.Domain.Shared.Enums; using Yi.Framework.SqlSugarCore.Abstractions; @@ -19,13 +20,16 @@ namespace Yi.Framework.Rbac.Application.Services.System /// /// Role服务实现 /// - public class RoleService : YiCrudAppService, - IRoleService + public class RoleService : YiCrudAppService, + IRoleService { - public RoleService(RoleManager roleManager, ISqlSugarRepository roleDeptRepository, ISqlSugarRepository userRoleRepository, ISqlSugarRepository repository) : base(repository) + public RoleService(RoleManager roleManager, ISqlSugarRepository roleDeptRepository, + ISqlSugarRepository userRoleRepository, + ISqlSugarRepository repository) : base(repository) { (_roleManager, _roleDeptRepository, _userRoleRepository, _repository) = - (roleManager, roleDeptRepository, userRoleRepository, repository); + (roleManager, roleDeptRepository, userRoleRepository, repository); } private ISqlSugarRepository _repository; @@ -41,23 +45,25 @@ namespace Yi.Framework.Rbac.Application.Services.System if (input.DataScope == DataScopeEnum.CUSTOM) { await _roleDeptRepository.DeleteAsync(x => x.RoleId == input.RoleId); - var insertEntities = input.DeptIds.Select(x => new RoleDeptEntity { DeptId = x, RoleId = input.RoleId }).ToList(); + var insertEntities = input.DeptIds.Select(x => new RoleDeptEntity { DeptId = x, RoleId = input.RoleId }) + .ToList(); await _roleDeptRepository.InsertRangeAsync(insertEntities); } + var entity = new RoleAggregateRoot() { DataScope = input.DataScope }; EntityHelper.TrySetId(entity, () => input.RoleId); await _repository._Db.Updateable(entity).UpdateColumns(x => x.DataScope).ExecuteCommandAsync(); - } public override async Task> GetListAsync(RoleGetListInputVo input) { RefAsync total = 0; - var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.RoleCode), x => x.RoleCode.Contains(input.RoleCode!)) + var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.RoleCode), + x => x.RoleCode.Contains(input.RoleCode!)) .WhereIF(!string.IsNullOrEmpty(input.RoleName), x => x.RoleName.Contains(input.RoleName!)) - .WhereIF(input.State is not null, x => x.State == input.State) - .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); + .WhereIF(input.State is not null, x => x.State == input.State) + .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); return new PagedResultDto(total, await MapToGetListOutputDtosAsync(entities)); } @@ -68,15 +74,16 @@ namespace Yi.Framework.Rbac.Application.Services.System /// public override async Task CreateAsync(RoleCreateInputVo input) { - RoleGetOutputDto outputDto; - //using (var uow = _unitOfWorkManager.CreateContext()) - //{ + var isExist = + await _repository.IsAnyAsync(x => x.RoleCode == input.RoleCode || x.RoleName == input.RoleName); + if (isExist) + { + throw new UserFriendlyException(RoleConst.Exist); + } + var entity = await MapToEntityAsync(input); await _repository.InsertAsync(entity); - outputDto = await MapToGetOutputDtoAsync(entity); - await _roleManager.GiveRoleSetMenuAsync(new List { entity.Id }, input.MenuIds); - // uow.Commit(); - //} + var outputDto = await MapToGetOutputDtoAsync(entity); return outputDto; } @@ -89,18 +96,20 @@ namespace Yi.Framework.Rbac.Application.Services.System /// public override async Task UpdateAsync(Guid id, RoleUpdateInputVo input) { - var dto = new RoleGetOutputDto(); - //using (var uow = _unitOfWorkManager.CreateContext()) - //{ var entity = await _repository.GetByIdAsync(id); + + var isExist =await _repository._DbQueryable.Where(x=>x.Id!=entity.Id).AnyAsync(x=>x.RoleCode==input.RoleCode||x.RoleName==input.RoleName); + if (isExist) + { + throw new UserFriendlyException(RoleConst.Exist); + } + await MapToEntityAsync(input, entity); await _repository.UpdateAsync(entity); await _roleManager.GiveRoleSetMenuAsync(new List { id }, input.MenuIds); - dto = await MapToGetOutputDtoAsync(entity); - // uow.Commit(); - //} + var dto = await MapToGetOutputDtoAsync(entity); return dto; } @@ -134,7 +143,8 @@ namespace Yi.Framework.Rbac.Application.Services.System /// 是否在该角色下 /// [Route("role/auth-user/{roleId}/{isAllocated}")] - public async Task> GetAuthUserByRoleIdAsync([FromRoute] Guid roleId, [FromRoute] bool isAllocated, [FromQuery] RoleAuthUserGetListInput input) + public async Task> GetAuthUserByRoleIdAsync([FromRoute] Guid roleId, + [FromRoute] bool isAllocated, [FromQuery] RoleAuthUserGetListInput input) { PagedResultDto output; //角色下已授权用户 @@ -147,30 +157,34 @@ namespace Yi.Framework.Rbac.Application.Services.System { output = await GetNotAllocatedAuthUserByRoleIdAsync(roleId, input); } + return output; } - private async Task> GetAllocatedAuthUserByRoleIdAsync(Guid roleId, RoleAuthUserGetListInput input) + private async Task> GetAllocatedAuthUserByRoleIdAsync(Guid roleId, + RoleAuthUserGetListInput input) { RefAsync total = 0; var output = await _userRoleRepository._DbQueryable - .LeftJoin((ur, u) => ur.UserId == u.Id && ur.RoleId == roleId) - .Where((ur, u) => ur.RoleId == roleId) - .WhereIF(!string.IsNullOrEmpty(input.UserName), (ur, u) => u.UserName.Contains(input.UserName)) - .WhereIF(input.Phone is not null, (ur, u) => u.Phone.ToString().Contains(input.Phone.ToString())) - .Select((ur, u) => new UserGetListOutputDto { Id = u.Id }, true) - .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); + .LeftJoin((ur, u) => ur.UserId == u.Id && ur.RoleId == roleId) + .Where((ur, u) => ur.RoleId == roleId) + .WhereIF(!string.IsNullOrEmpty(input.UserName), (ur, u) => u.UserName.Contains(input.UserName)) + .WhereIF(input.Phone is not null, (ur, u) => u.Phone.ToString().Contains(input.Phone.ToString())) + .Select((ur, u) => new UserGetListOutputDto { Id = u.Id }, true) + .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); return new PagedResultDto(total, output); } - private async Task> GetNotAllocatedAuthUserByRoleIdAsync(Guid roleId, RoleAuthUserGetListInput input) + private async Task> GetNotAllocatedAuthUserByRoleIdAsync(Guid roleId, + RoleAuthUserGetListInput input) { RefAsync total = 0; var entities = await _userRoleRepository._Db.Queryable() - .Where(u => SqlFunc.Subqueryable().Where(x => x.RoleId == roleId).Where(x => x.UserId == u.Id).NotAny()) - .WhereIF(!string.IsNullOrEmpty(input.UserName), u => u.UserName.Contains(input.UserName)) - .WhereIF(input.Phone is not null, u => u.Phone.ToString().Contains(input.Phone.ToString())) - .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); + .Where(u => SqlFunc.Subqueryable().Where(x => x.RoleId == roleId) + .Where(x => x.UserId == u.Id).NotAny()) + .WhereIF(!string.IsNullOrEmpty(input.UserName), u => u.UserName.Contains(input.UserName)) + .WhereIF(input.Phone is not null, u => u.Phone.ToString().Contains(input.Phone.ToString())) + .ToPageListAsync(input.SkipCount, input.MaxResultCount, total); var output = entities.Adapt>(); return new PagedResultDto(total, output); } @@ -183,7 +197,8 @@ namespace Yi.Framework.Rbac.Application.Services.System /// public async Task CreateAuthUserAsync(RoleAuthUserCreateOrDeleteInput input) { - var userRoleEntities = input.UserIds.Select(u => new UserRoleEntity { RoleId = input.RoleId, UserId = u }).ToList(); + var userRoleEntities = input.UserIds.Select(u => new UserRoleEntity { RoleId = input.RoleId, UserId = u }) + .ToList(); await _userRoleRepository.InsertRangeAsync(userRoleEntities); } @@ -197,7 +212,8 @@ namespace Yi.Framework.Rbac.Application.Services.System { await _userRoleRepository._Db.Deleteable().Where(x => x.RoleId == input.RoleId) .Where(x => input.UserIds.Contains(x.UserId)) - .ExecuteCommandAsync(); ; + .ExecuteCommandAsync(); + ; } } -} +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs index 6ef827cf..a000789f 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/System/UserService.cs @@ -127,6 +127,7 @@ namespace Yi.Framework.Rbac.Application.Services.System /// /// /// + [Permission("system:user:list")] public override async Task GetAsync(Guid id) { //使用导航树形查询 @@ -153,7 +154,7 @@ namespace Yi.Framework.Rbac.Application.Services.System if (await _repository.IsAnyAsync(u => input.UserName!.Equals(u.UserName) && !id.Equals(u.Id))) { - throw new UserFriendlyException("用户已经存在,更新失败"); + throw new UserFriendlyException(UserConst.Exist); } var entity = await _repository.GetByIdAsync(id); diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/TestServcie.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/TestServcie.cs deleted file mode 100644 index c38bffd8..00000000 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/TestServcie.cs +++ /dev/null @@ -1,120 +0,0 @@ -using System.IdentityModel.Tokens.Jwt; -using System.Security.Claims; -using System.Text; -using Microsoft.AspNetCore.Authorization; -using Microsoft.Extensions.Options; -using Microsoft.IdentityModel.Tokens; -using Volo.Abp; -using Volo.Abp.Application.Services; -using Volo.Abp.Domain.Repositories; -using Volo.Abp.Uow; -using Yi.Framework.Rbac.Domain.Entities; -using Yi.Framework.Rbac.Domain.Shared.Options; -using Yi.Framework.SqlSugarCore.Abstractions; - -namespace Yi.Framework.Rbac.Application.Services -{ - /// - /// 测试文档控制器 - /// - public class TestServcie : ApplicationService - { - private IRepository _repository; - private IUnitOfWorkManager _unitOfWork; - private ISqlSugarRepository _sqlsugarRepository; - - public IOptions options { get; set; } - public TestServcie(IRepository repository, IUnitOfWorkManager unitOfWork, ISqlSugarRepository sqlsugarRepository, IRepository repository2) - { - _unitOfWork = unitOfWork; - _repository = repository; - _sqlsugarRepository = sqlsugarRepository; - - } - /// - /// 你好,多线程 - /// - /// - public async Task GetTaskTest() - { - var tasks = Enumerable.Range(0, 2).Select(x => - { - return Task.Run(async () => - { - using (var uow = _unitOfWork.Begin(true)) - { - // await _repository.GetListAsync(); - await _sqlsugarRepository._DbQueryable.ToListAsync(); - await uow.CompleteAsync(); - } - }); - }).ToList(); - - await Task.WhenAll(tasks); - return "你哈"; - } - - [Authorize] - public async Task> GetTest() - { - //using (var uow = _unitOfWork.Begin(true)) - //{ - var data = await _repository.GetListAsync(); - var data2 = await _repository.GetListAsync(); - //await uow.CompleteAsync(); - return data; - - //} - - - } - - //[UnitOfWork] - public async Task PostTest() - { - //using (var uow = _unitOfWork.Begin()) - //{ - var stu = new StudentEntity() { Name = $"{DateTime.Now.ToString()}你好" }; - - var data = await _repository.InsertAsync(stu); - //await uow.CompleteAsync(); - return data; - //} - } - - public async Task PostError() - { - throw new ApplicationException(); - } - - public async Task PostUserError() - { - throw new UserFriendlyException("直接爆炸"); - } - - public string Login() - { - var data = options.Value; - var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(data.SecurityKey)); - - var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); - var claims = new List() - { - new Claim("name","admin") - }; - var token = new JwtSecurityToken( - issuer: data.Issuer, - audience: data.Audience, - claims: claims, - expires: DateTime.Now.AddSeconds(60 * 60 * 2), - notBefore: DateTime.Now, - signingCredentials: creds); - string returnToken = new JwtSecurityTokenHandler().WriteToken(token); - - return returnToken; - - - - } - } -} diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/ConfigConst.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/ConfigConst.cs new file mode 100644 index 00000000..5cdec634 --- /dev/null +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/ConfigConst.cs @@ -0,0 +1,6 @@ +namespace Yi.Framework.Rbac.Domain.Shared.Consts; + +public class ConfigConst +{ + public const string Exist = "该配置已经存在"; +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/DeptConst.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/DeptConst.cs index 49344905..830f1b57 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/DeptConst.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/DeptConst.cs @@ -12,5 +12,6 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts public class DeptConst { + public const string Exist = "该部门已经存在"; } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/DictionaryConst.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/DictionaryConst.cs new file mode 100644 index 00000000..90831f4c --- /dev/null +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/DictionaryConst.cs @@ -0,0 +1,6 @@ +namespace Yi.Framework.Rbac.Domain.Shared.Consts; + +public class DictionaryConst +{ + public const string Exist = "该字典已经存在"; +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/MenuConst.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/MenuConst.cs index 77c687a9..82eb6f72 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/MenuConst.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/MenuConst.cs @@ -12,5 +12,6 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts public class MenuConst { + public const string Exist = "该菜单已经存在"; } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/PostConst.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/PostConst.cs index 900844e9..5c88d8d6 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/PostConst.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/PostConst.cs @@ -12,5 +12,6 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts public class PostConst { + public const string Exist = "该岗位已经存在"; } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/RoleConst.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/RoleConst.cs index 99fe5602..8cc4405d 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/RoleConst.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/RoleConst.cs @@ -12,5 +12,6 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts public class RoleConst { + public const string Exist = "该角色已经存在"; } } diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/UserConst.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/UserConst.cs index 4dab8fb6..4405a3f8 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/UserConst.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain.Shared/Consts/UserConst.cs @@ -16,7 +16,7 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts public const string Login_User_No_Exist = "登录失败!用户名不存在!"; public const string Login_Passworld_Error = "密码为空,添加失败!"; public const string Create_Passworld_Error = "密码格式错误,长度需大于等于6位"; - public const string User_Exist = "用户已经存在,创建失败!"; + public const string Exist = "用户已经存在,创建失败!"; public const string State_Is_State = "该用户已被禁用,请联系管理员进行恢复"; public const string No_Permission = "登录禁用!该用户分配无任何权限,无意义登录!"; public const string No_Role = "登录禁用!该用户分配无任何角色,无意义登录!"; diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Entities/StudentEntity.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Entities/StudentEntity.cs deleted file mode 100644 index d0f75f19..00000000 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Entities/StudentEntity.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Volo.Abp.Domain.Entities; - -namespace Yi.Framework.Rbac.Domain.Entities -{ - public class StudentEntity : Entity - { - - [SqlSugar.SugarColumn(IsPrimaryKey = true)] - public override Guid Id { get; protected set; } - public string Name { get; set; } - - } -} diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/EventHandlers/StudentEventHandler.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/EventHandlers/StudentEventHandler.cs deleted file mode 100644 index 4ded4f5d..00000000 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/EventHandlers/StudentEventHandler.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Volo.Abp.DependencyInjection; -using Volo.Abp.Domain.Entities.Events; -using Volo.Abp.EventBus; -using Yi.Framework.Rbac.Domain.Entities; - -namespace Yi.Framework.Rbac.Domain.EventHandlers -{ - public class StudentEventHandler : ILocalEventHandler>, ITransientDependency - { - public Task HandleEventAsync(EntityCreatedEventData eventData) - { - Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(eventData.Entity)); - return Task.CompletedTask; - } - } -} diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs index d3a50014..a4723df7 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Domain/Managers/UserManager.cs @@ -118,7 +118,7 @@ namespace Yi.Framework.Rbac.Domain.Managers var isExist = await _repository.IsAnyAsync(x => x.UserName == userEntity.UserName); if (isExist) { - throw new UserFriendlyException(UserConst.User_Exist); + throw new UserFriendlyException(UserConst.Exist); } var entity = await _repository.InsertReturnEntityAsync(userEntity); diff --git a/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/Account_Test.cs b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/Account_Test.cs index c500f597..8703f5df 100644 --- a/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/Account_Test.cs +++ b/Yi.Abp.Net8/test/Yi.Framework.Rbac.Test/System/Account_Test.cs @@ -55,7 +55,7 @@ namespace Yi.Framework.Rbac.Test.System } catch (UserFriendlyException ex) { - ex.Message.ShouldBe(UserConst.User_Exist); + ex.Message.ShouldBe(UserConst.Exist); } }