using Furion.DatabaseAccessor; using SqlSugar; using Yi.Framework.Infrastructure.Ddd.Dtos; using Yi.Framework.Infrastructure.Ddd.Repositories; using Yi.Framework.Infrastructure.Ddd.Services; using Yi.Framework.Infrastructure.Helper; using Yi.Furion.Application.Rbac.Domain; using Yi.Furion.Core.Rbac.Dtos.Role; using Yi.Furion.Core.Rbac.Entities; using Yi.Furion.Core.Rbac.Enums; namespace Yi.Furion.Application.Rbac.Services.Impl { /// /// Role服务实现 /// public class RoleService : CrudAppService, IRoleService, ITransient, IDynamicApiController { public RoleService(RoleManager roleManager, IRepository roleDeptRepository) => (_roleManager, _roleDeptRepository) = (roleManager, roleDeptRepository); private RoleManager _roleManager { get; set; } private IRepository _roleDeptRepository; public async Task> GetDeptIdsAsync(long roleId) { var entities = await _roleDeptRepository.GetListAsync(x => x.RoleId == roleId); return entities.Select(x => x.DeptId).ToList(); } [UnitOfWork] public async Task UpdateDataScpoceAsync(UpdateDataScpoceInput input) { //只有自定义的需要特殊处理 if (input.DataScope == DataScopeEnum.CUSTOM) { await _roleDeptRepository.DeleteAsync(x => x.RoleId == input.RoleId); var insertEntities = input.DeptIds.Select(x => new RoleDeptEntity {Id=SnowflakeHelper.NextId, DeptId = x, RoleId = input.RoleId }).ToList(); await _roleDeptRepository.InsertRangeAsync(insertEntities); } await _repository._Db.Updateable(new RoleEntity() { Id = input.RoleId, DataScope = input.DataScope }).UpdateColumns(x => x.DataScope).ExecuteCommandAsync(); } public override async Task> GetListAsync(RoleGetListInputVo input) { var entity = await MapToEntityAsync(input); RefAsync total = 0; var entities = await _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.PageNum, input.PageSize, total); return new PagedResultDto(total, await MapToGetListOutputDtosAsync(entities)); } /// /// 添加角色 /// /// /// [UnitOfWork] public override async Task CreateAsync(RoleCreateInputVo input) { RoleGetOutputDto outputDto; //using (var uow = _unitOfWorkManager.CreateContext()) //{ var entity = await MapToEntityAsync(input); await _repository.InsertAsync(entity); outputDto = await MapToGetOutputDtoAsync(entity); await _roleManager.GiveRoleSetMenuAsync(new List { entity.Id }, input.MenuIds); // uow.Commit(); //} return outputDto; } /// /// 修改角色 /// /// /// /// [UnitOfWork] public override async Task UpdateAsync(long id, RoleUpdateInputVo input) { var dto = new RoleGetOutputDto(); //using (var uow = _unitOfWorkManager.CreateContext()) //{ var entity = await _repository.GetByIdAsync(id); await MapToEntityAsync(input, entity); await _repository.UpdateAsync(entity); await _roleManager.GiveRoleSetMenuAsync(new List { id }, input.MenuIds); dto = await MapToGetOutputDtoAsync(entity); // uow.Commit(); //} return dto; } /// /// 更新状态 /// /// /// /// [Route("/api/role/{id}/{state}")] public async Task UpdateStateAsync([FromRoute] long 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); } } }