SysRoleService.cs 4.1 KB

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