namespace Admin.NET.Core.Service; /// /// 系统角色服务 /// [ApiDescriptionSettings(Order = 480)] public class SysRoleService : IDynamicApiController, ITransient { private readonly UserManager _userManager; private readonly SqlSugarRepository _sysRoleRep; private readonly SysCacheService _sysCacheService; private readonly SysRoleOrgService _sysRoleOrgService; private readonly SysRoleMenuService _sysRoleMenuService; private readonly SysOrgService _sysOrgService; private readonly SysUserRoleService _sysUserRoleService; public SysRoleService(UserManager userManager, SqlSugarRepository sysRoleRep, SysCacheService sysCacheService, SysRoleOrgService sysRoleOrgService, SysRoleMenuService sysRoleMenuService, SysOrgService sysOrgService, SysUserRoleService sysUserRoleService) { _userManager = userManager; _sysRoleRep = sysRoleRep; _sysCacheService = sysCacheService; _sysRoleOrgService = sysRoleOrgService; _sysRoleMenuService = sysRoleMenuService; _sysOrgService = sysOrgService; _sysUserRoleService = sysUserRoleService; } /// /// 获取角色分页列表 /// /// /// [DisplayName("获取角色分页列表")] public async Task> Page(PageRoleInput input) { return await _sysRoleRep.AsQueryable() .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name)) .WhereIF(!string.IsNullOrWhiteSpace(input.Code), u => u.Code.Contains(input.Code)) .OrderBy(u => u.OrderNo) .ToPagedListAsync(input.Page, input.PageSize); } /// /// 获取角色列表 /// /// [DisplayName("获取角色列表")] public async Task> GetList() { return await _sysRoleRep.AsQueryable().OrderBy(u => u.OrderNo).Select().ToListAsync(); } /// /// 增加角色 /// /// /// [ApiDescriptionSettings(Name = "Add"), HttpPost] [DisplayName("增加角色")] public async Task AddRole(AddRoleInput input) { var isExist = await _sysRoleRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code); if (isExist) throw Oops.Oh(ErrorCodeEnum.D1006); var newRole = await _sysRoleRep.AsInsertable(input.Adapt()).ExecuteReturnEntityAsync(); input.Id = newRole.Id; await UpdateRoleMenu(input); } /// /// 更新角色菜单权限 /// /// /// private async Task UpdateRoleMenu(AddRoleInput input) { if (input.MenuIdList == null || input.MenuIdList.Count < 1) return; await GrantMenu(new RoleMenuInput() { Id = input.Id, MenuIdList = input.MenuIdList }); } /// /// 更新角色 /// /// /// [ApiDescriptionSettings(Name = "Update"), HttpPost] [DisplayName("更新角色")] public async Task UpdateRole(UpdateRoleInput input) { var isExist = await _sysRoleRep.IsAnyAsync(u => u.Name == input.Name && u.Code == input.Code && u.Id != input.Id); if (isExist) throw Oops.Oh(ErrorCodeEnum.D1006); await _sysRoleRep.AsUpdateable(input.Adapt()).IgnoreColumns(true) .IgnoreColumns(u => new { u.DataScope }).ExecuteCommandAsync(); await UpdateRoleMenu(input); } /// /// 删除角色 /// /// /// [UnitOfWork] [ApiDescriptionSettings(Name = "Delete"), HttpPost] [DisplayName("删除角色")] public async Task DeleteRole(DeleteRoleInput input) { var sysRole = await _sysRoleRep.GetFirstAsync(u => u.Id == input.Id); if (sysRole.Code == CommonConst.SysAdminRole) throw Oops.Oh(ErrorCodeEnum.D1019); await _sysRoleRep.DeleteAsync(sysRole); // 级联删除角色机构数据 await _sysRoleOrgService.DeleteRoleOrgByRoleId(sysRole.Id); // 级联删除用户角色数据 await _sysUserRoleService.DeleteUserRoleByRoleId(sysRole.Id); // 级联删除角色菜单数据 await _sysRoleMenuService.DeleteRoleMenuByRoleId(sysRole.Id); } /// /// 授权角色菜单 /// /// /// [DisplayName("授权角色菜单")] public async Task GrantMenu(RoleMenuInput input) { await _sysRoleMenuService.GrantRoleMenu(input); } /// /// 授权角色数据范围 /// /// /// [DisplayName("授权角色数据范围")] public async Task GrantDataScope(RoleOrgInput input) { // 删除所有用户机构缓存 _sysCacheService.RemoveByPrefixKey(CacheConst.KeyOrgIdList); var role = await _sysRoleRep.GetFirstAsync(u => u.Id == input.Id); var dataScope = input.DataScope; if (!_userManager.SuperAdmin) { // 非超级管理员没有全部数据范围权限 if (dataScope == (int)DataScopeEnum.All) throw Oops.Oh(ErrorCodeEnum.D1016); // 若数据范围自定义,则判断授权数据范围是否有权限 if (dataScope == (int)DataScopeEnum.Define) { var grantOrgIdList = input.OrgIdList; if (grantOrgIdList.Count > 0) { var orgIdList = await _sysOrgService.GetUserOrgIdList(); if (orgIdList.Count < 1) throw Oops.Oh(ErrorCodeEnum.D1016); else if (!grantOrgIdList.All(u => orgIdList.Any(c => c == u))) throw Oops.Oh(ErrorCodeEnum.D1016); } } } role.DataScope = (DataScopeEnum)dataScope; await _sysRoleRep.AsUpdateable(role).UpdateColumns(u => new { u.DataScope }).ExecuteCommandAsync(); await _sysRoleOrgService.GrantRoleOrg(input); } /// /// 根据角色Id获取菜单Id集合 /// /// /// [DisplayName("根据角色Id获取菜单Id集合")] public async Task> GetOwnMenuList([FromQuery] RoleInput input) { return await _sysRoleMenuService.GetRoleMenuIdList(new List { input.Id }); } /// /// 根据角色Id获取机构Id集合 /// /// /// [DisplayName("根据角色Id获取机构Id集合")] public async Task> GetOwnOrgList([FromQuery] RoleInput input) { return await _sysRoleOrgService.GetRoleOrgIdList(new List { input.Id }); } /// /// 设置角色状态 /// /// /// [DisplayName("设置角色状态")] public async Task SetStatus(RoleInput input) { if (!Enum.IsDefined(typeof(StatusEnum), input.Status)) throw Oops.Oh(ErrorCodeEnum.D3005); return await _sysRoleRep.AsUpdateable() .SetColumns(u => u.Status == input.Status) .Where(u => u.Id == input.Id) .ExecuteCommandAsync(); } }