SysRoleService.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using Furion.DatabaseAccessor.Extensions;
  2. using YBEE.EQM.Core;
  3. namespace YBEE.EQM.Application;
  4. /// <summary>
  5. /// 角色服务
  6. /// </summary>
  7. public class SysRoleService : ISysRoleService, ITransient
  8. {
  9. private readonly IRepository<SysRole> _roleRep;
  10. private readonly IRepository<SysRoleUser> _roleUserRep;
  11. private readonly ISysRoleMenuService _roleMenuService;
  12. private readonly ICacheService _cacheService;
  13. public SysRoleService(IRepository<SysRole> roleRep, IRepository<SysRoleUser> roleUserRep, ISysRoleMenuService roleMenuService, ICacheService cacheService)
  14. {
  15. _roleRep = roleRep;
  16. _roleUserRep = roleUserRep;
  17. _roleMenuService = roleMenuService;
  18. _cacheService = cacheService;
  19. }
  20. /// <summary>
  21. /// 添加角色
  22. /// </summary>
  23. /// <param name="input"></param>
  24. /// <returns></returns>
  25. public async Task Add(AddSysRoleInput input)
  26. {
  27. var isExist = await _roleRep.DetachedEntities.AnyAsync(u => u.Name == input.Name);
  28. if (isExist)
  29. {
  30. throw Oops.Oh(ErrorCode.E2004);
  31. }
  32. var role = input.Adapt<SysRole>();
  33. role.RoleType = RoleType.NORMAL;
  34. await role.InsertNowAsync();
  35. }
  36. /// <summary>
  37. /// 删除角色
  38. /// </summary>
  39. /// <param name="input"></param>
  40. /// <returns></returns>
  41. public async Task Del(DeleteSysRoleInput input)
  42. {
  43. var sysRole = await _roleRep.FirstOrDefaultAsync(u => u.Id == input.Id);
  44. if (sysRole.RoleType != RoleType.NORMAL)
  45. {
  46. throw Oops.Oh(ErrorCode.E8201);
  47. }
  48. await sysRole.DeleteNowAsync();
  49. //级联删除该角色对应的用户-角色表关联信息
  50. var userRoles = await _roleUserRep.Where(u => u.SysRoleId == sysRole.Id).ToListAsync();
  51. await _roleUserRep.DeleteAsync(userRoles);
  52. //级联删除该角色对应的角色-菜单表关联信息
  53. await _roleMenuService.DeleteRoleMenuListByRoleId(sysRole.Id);
  54. }
  55. /// <summary>
  56. /// 更新角色
  57. /// </summary>
  58. /// <param name="input"></param>
  59. /// <returns></returns>
  60. public async Task Update(UpdateSysRoleInput input)
  61. {
  62. var role = await _roleRep.DetachedEntities.FirstOrDefaultAsync(u => u.Id == input.Id);
  63. if (role.RoleType != RoleType.NORMAL)
  64. {
  65. throw Oops.Oh(ErrorCode.E8202);
  66. }
  67. var isExist = await _roleRep.DetachedEntities.AnyAsync(u => u.Name == input.Name && u.Id != input.Id);
  68. if (isExist)
  69. {
  70. throw Oops.Oh(ErrorCode.E2004);
  71. }
  72. var sysRole = input.Adapt<SysRole>();
  73. await sysRole.UpdateExcludeNowAsync(new[] { nameof(sysRole.RoleDataScope) });
  74. }
  75. /// <summary>
  76. /// 设置角色数据权限范围
  77. /// </summary>
  78. /// <returns></returns>
  79. public async Task SetDataScope()
  80. {
  81. // TODO:
  82. // 清空缓存,DelByPatternAsync,CommonConst.CACHE_KEY_PERMISSION
  83. }
  84. /// <summary>
  85. /// 获取当前用户角色数据权限范围
  86. /// </summary>
  87. /// <returns></returns>
  88. public async Task<RoleDataScope> GetCurrentUserDataScope()
  89. {
  90. var roles = await _cacheService.GetRoleDataScope(CurrentSysUserInfo.SysUserId);
  91. if (roles == null)
  92. {
  93. return new();
  94. }
  95. RoleDataScope roleDataScope = new();
  96. foreach (var role in roles)
  97. {
  98. roleDataScope.EducationStages.AddRange(role.RoleDataScope.EducationStages);
  99. roleDataScope.SysOrgIds.AddRange(role.RoleDataScope.SysOrgIds);
  100. }
  101. roleDataScope.EducationStages = roleDataScope.EducationStages.Distinct().ToList();
  102. roleDataScope.SysOrgIds = roleDataScope.SysOrgIds.Distinct().ToList();
  103. return roleDataScope;
  104. }
  105. /// <summary>
  106. /// 获取所有角色
  107. /// </summary>
  108. /// <returns></returns>
  109. public async Task<List<SysRoleOutput>> GetAllList()
  110. {
  111. return await _roleRep.DetachedEntities.OrderBy(u => u.RoleType).ProjectToType<SysRoleOutput>().ToListAsync();
  112. }
  113. /// <summary>
  114. /// 根据角色ID列表获取角色列表
  115. /// </summary>
  116. /// <param name="ids"></param>
  117. /// <returns></returns>
  118. public async Task<List<SysRoleOutput>> GetListByIds(List<int> ids)
  119. {
  120. return await _roleRep.DetachedEntities.Where(t => ids.Contains(t.Id)).ProjectToType<SysRoleOutput>().ToListAsync();
  121. }
  122. }