SysRoleMenuService.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using YBEE.EQM.Core;
  2. namespace YBEE.EQM.Application
  3. {
  4. /// <summary>
  5. /// 角色菜单服务
  6. /// </summary>
  7. public class SysRoleMenuService : ISysRoleMenuService, ITransient
  8. {
  9. private readonly IRepository<SysRoleMenu> _roleMenuRep;
  10. private readonly IRepository<SysMenu> _menuRep;
  11. private readonly ICacheService _cacheService;
  12. public SysRoleMenuService(IRepository<SysRoleMenu> roleMenuRep, IRepository<SysMenu> menuRep, ICacheService cacheService)
  13. {
  14. _roleMenuRep = roleMenuRep;
  15. _menuRep = menuRep;
  16. _cacheService = cacheService;
  17. }
  18. /// <summary>
  19. /// 授权角色菜单
  20. /// </summary>
  21. /// <param name="input"></param>
  22. /// <returns></returns>
  23. public async Task GrantRoleMenu(GrantSysRoleMenuInput input)
  24. {
  25. var roleMenus = await _roleMenuRep.DetachedEntities.Where(u => u.SysRoleId == input.RoleId).ToListAsync();
  26. await _roleMenuRep.DeleteAsync(roleMenus);
  27. // 确保对按钮菜单的父级菜单能正确的授权,发现前端构造的数据会丢弃了按钮菜单的父级菜单
  28. var roleMenuPidsList = await _menuRep.DetachedEntities.Where(u => input.RoleMenuScopeList.Select(t => t.MenuId).Contains(u.Id))
  29. .Select(u => u.Pids).ToListAsync();
  30. var roleMenuSplitPids = roleMenuPidsList
  31. .SelectMany(u => u.Split(',', StringSplitOptions.RemoveEmptyEntries)
  32. .Select(v => int.Parse(v[1..^1]))
  33. .Where(v => v != 0))
  34. .Union(input.RoleMenuScopeList.Select(t => t.MenuId));
  35. var menus = roleMenuSplitPids.Select(u => new SysRoleMenu
  36. {
  37. SysRoleId = input.RoleId,
  38. SysMenuId = u,
  39. DataScopeType = input.RoleMenuScopeList.FirstOrDefault(t => t.MenuId == u)?.DataScopeType ?? DataScopeType.SELF
  40. }).ToList();
  41. await _roleMenuRep.InsertAsync(menus);
  42. // 清除缓存
  43. await _cacheService.DelByPatternAsync(CommonConst.CACHE_KEY_MENU);
  44. await _cacheService.DelByPatternAsync(CommonConst.CACHE_KEY_PERMISSION);
  45. }
  46. /// <summary>
  47. /// 根据菜单Id集合删除对应的角色-菜单表信息
  48. /// </summary>
  49. /// <param name="menuIdList"></param>
  50. /// <returns></returns>
  51. public async Task DeleteRoleMenuListByMenuIdList(List<int> menuIdList)
  52. {
  53. var roleMenus = await _roleMenuRep.DetachedEntities.Where(u => menuIdList.Contains(u.SysMenuId)).ToListAsync();
  54. await _roleMenuRep.DeleteAsync(roleMenus);
  55. }
  56. /// <summary>
  57. /// 根据角色Id删除对应的角色-菜单表关联信息
  58. /// </summary>
  59. /// <param name="roleId"></param>
  60. /// <returns></returns>
  61. public async Task DeleteRoleMenuListByRoleId(int roleId)
  62. {
  63. var roleMenus = await _roleMenuRep.DetachedEntities.Where(u => u.SysRoleId == roleId).ToListAsync();
  64. await _roleMenuRep.DeleteAsync(roleMenus);
  65. }
  66. /// <summary>
  67. /// 获取角色的菜单Id集合
  68. /// </summary>
  69. /// <param name="roleIdList"></param>
  70. /// <returns></returns>
  71. public async Task<List<int>> GetRoleMenuIdList(List<int> roleIdList)
  72. {
  73. return await _roleMenuRep.DetachedEntities
  74. .Where(u => roleIdList.Contains(u.SysRoleId))
  75. .Select(u => u.SysMenuId).ToListAsync();
  76. }
  77. /// <summary>
  78. /// 获取角色对象菜单列表
  79. /// </summary>
  80. /// <param name="roleId"></param>
  81. /// <returns></returns>
  82. public async Task<List<SysRoleMenuOutput>> GetRoleMenuList(int roleId)
  83. {
  84. return await _roleMenuRep.DetachedEntities.Include(t => t.SysMenu).Where(t => t.SysRoleId == roleId).ProjectToType<SysRoleMenuOutput>().ToListAsync();
  85. }
  86. /// <summary>
  87. /// 获取指定权限标识的角色ID列表
  88. /// </summary>
  89. /// <param name="permissionCode"></param>
  90. /// <returns></returns>
  91. public async Task<List<int>> GetRoleIdListByMenuPermissionCode(string permissionCode)
  92. {
  93. return await _roleMenuRep.DetachedEntities.Where(t => t.SysMenu.Permission == permissionCode).Select(t => t.SysRoleId).Distinct().ToListAsync();
  94. }
  95. }
  96. }