using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Entities; using Volo.Abp.Uow; using Yi.Framework.Ddd.Application; using Yi.Framework.Rbac.Application.Contracts.Dtos.Role; 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; namespace Yi.Framework.Rbac.Application.Services.System { /// /// Role服务实现 /// public class RoleService : YiCrudAppService, IRoleService { public RoleService(RoleManager roleManager, ISqlSugarRepository roleDeptRepository, ISqlSugarRepository userRoleRepository, ISqlSugarRepository repository) : base(repository) { (_roleManager, _roleDeptRepository, _userRoleRepository, _repository) = (roleManager, roleDeptRepository, userRoleRepository, repository); } private ISqlSugarRepository _repository; private RoleManager _roleManager { get; set; } private ISqlSugarRepository _roleDeptRepository; private ISqlSugarRepository _userRoleRepository; public async Task UpdateDataScopeAsync(UpdateDataScopeInput input) { //只有自定义的需要特殊处理 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(); 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!)) .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); return new PagedResultDto(total, await MapToGetListOutputDtosAsync(entities)); } /// /// 添加角色 /// /// /// public override async Task CreateAsync(RoleCreateInputVo input) { 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); var outputDto = await MapToGetOutputDtoAsync(entity); return outputDto; } /// /// 修改角色 /// /// /// /// public override async Task UpdateAsync(Guid id, RoleUpdateInputVo input) { 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); var dto = await MapToGetOutputDtoAsync(entity); return dto; } /// /// 更新状态 /// /// /// /// [Route("role/{id}/{state}")] public async Task UpdateStateAsync([FromRoute] Guid id, [FromRoute] bool state) { var entity = await _repository.GetByIdAsync(id); if (entity is null) { throw new ApplicationException("角色未存在"); } entity.State = state; await _repository.UpdateAsync(entity); return await MapToGetOutputDtoAsync(entity); } /// /// 获取角色下的用户 /// /// /// /// 是否在该角色下 /// [Route("role/auth-user/{roleId}/{isAllocated}")] public async Task> GetAuthUserByRoleIdAsync([FromRoute] Guid roleId, [FromRoute] bool isAllocated, [FromQuery] RoleAuthUserGetListInput input) { PagedResultDto output; //角色下已授权用户 if (isAllocated == true) { output = await GetAllocatedAuthUserByRoleIdAsync(roleId, input); } //角色下未授权用户 else { output = await GetNotAllocatedAuthUserByRoleIdAsync(roleId, input); } return output; } 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); return new PagedResultDto(total, output); } 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); var output = entities.Adapt>(); return new PagedResultDto(total, output); } /// /// 批量给用户授权 /// /// /// public async Task CreateAuthUserAsync([FromBody] RoleAuthUserCreateOrDeleteInput input) { var userRoleEntities = input.UserIds.Select(u => new UserRoleEntity { RoleId = input.RoleId, UserId = u }) .ToList(); await _userRoleRepository.InsertRangeAsync(userRoleEntities); } /// /// 批量取消授权 /// /// /// public async Task DeleteAuthUserAsync([FromBody] RoleAuthUserCreateOrDeleteInput input) { await _userRoleRepository._Db.Deleteable().Where(x => x.RoleId == input.RoleId) .Where(x => input.UserIds.Contains(x.UserId)) .ExecuteCommandAsync(); ; } } }