SysRoleUserService.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using YBEE.EQM.Core;
  2. namespace YBEE.EQM.Application;
  3. /// <summary>
  4. /// 角色成员管理服务
  5. /// </summary>
  6. public class SysRoleUserService : ISysRoleUserService, ITransient
  7. {
  8. private readonly IRepository<SysRoleUser> _roleUserRep;
  9. private readonly ISysRoleService _roleService;
  10. private readonly ISysRoleMenuService _roleMenuService;
  11. private readonly ICacheService _cacheService;
  12. public SysRoleUserService(IRepository<SysRoleUser> roleUserRep, ISysRoleService roleService, ISysRoleMenuService roleMenuService, ICacheService cacheService)
  13. {
  14. _roleUserRep = roleUserRep;
  15. _roleService = roleService;
  16. _roleMenuService = roleMenuService;
  17. _cacheService = cacheService;
  18. }
  19. /// <summary>
  20. /// 获取用户的角色Id集合
  21. /// </summary>
  22. /// <param name="userId"></param>
  23. /// <param name="checkRoleStatus"></param>
  24. /// <returns></returns>x\
  25. public async Task<List<int>> GetUserRoleIdList(int userId, bool checkRoleStatus = true)
  26. {
  27. return await _roleUserRep
  28. // 检查role状态,跳过全局tenantId&delete过滤器,超级管理员使用
  29. .Where(!checkRoleStatus, u => u.SysRole.Status == CommonStatus.ENABLE && !u.SysRole.IsDeleted, ignoreQueryFilters: true)
  30. // 当不是超级管理员的时候检查role状态和全局tenantId&delete过滤器
  31. .Where(checkRoleStatus, u => u.SysRole.Status == CommonStatus.ENABLE)
  32. .Where(u => u.SysUserId == userId)
  33. .Select(u => u.SysRoleId)
  34. .ToListAsync();
  35. }
  36. /// <summary>
  37. /// 获取用户拥有角色列表
  38. /// </summary>
  39. /// <param name="userId"></param>
  40. /// <param name="checkRoleStatus"></param>
  41. /// <returns></returns>
  42. public async Task<List<SysRoleOutput>> GetUserRoleList(int userId, bool checkRoleStatus = true)
  43. {
  44. var ids = await GetUserRoleIdList(userId, checkRoleStatus);
  45. return await _roleService.GetListByIds(ids);
  46. }
  47. /// <summary>
  48. /// 获取登录用户拥有角色列表
  49. /// </summary>
  50. /// <param name="userId"></param>
  51. /// <param name="checkRoleStatus"></param>
  52. /// <returns></returns>
  53. public async Task<List<SysRoleOutput>> GetLoginUserRoleList(int userId, bool checkRoleStatus = true)
  54. {
  55. var items = await _cacheService.GetRoleDataScope(userId);
  56. if (items == null || items.Count < 1)
  57. {
  58. var ids = await GetUserRoleIdList(userId, checkRoleStatus);
  59. items = await _roleService.GetListByIds(ids);
  60. // 缓存结果
  61. await _cacheService.SetRoleDataScope(userId, items);
  62. }
  63. return items;
  64. }
  65. /// <summary>
  66. /// 添加用户到角色
  67. /// </summary>
  68. /// <param name="input"></param>
  69. /// <returns></returns>
  70. public async Task AddRoleUser(SysRoleUserInput input)
  71. {
  72. var roleUserIds = await _roleUserRep.Where(t => t.SysRoleId == input.SysRoleId).Select(t => t.SysUserId).ToListAsync();
  73. var addUserIds = from it in input.SysUserIdList where !(from rid in roleUserIds select rid).Contains(it) select it;
  74. var roles = addUserIds.Select(u => new SysRoleUser
  75. {
  76. SysRoleId = input.SysRoleId,
  77. SysUserId = u,
  78. }).ToList();
  79. await _roleUserRep.InsertAsync(roles);
  80. }
  81. /// <summary>
  82. /// 移出角色中的用户
  83. /// </summary>
  84. /// <param name="input"></param>
  85. /// <returns></returns>
  86. public async Task RemoveRoleUser(SysRoleUserInput input)
  87. {
  88. var roleUsers = await _roleUserRep.Where(t => t.SysRoleId == input.SysRoleId && input.SysUserIdList.Contains(t.SysUserId)).ToListAsync();
  89. await _roleUserRep.DeleteNowAsync(roleUsers);
  90. }
  91. /// <summary>
  92. /// 判断用户是否为超级管理员
  93. /// </summary>
  94. /// <param name="userId"></param>
  95. /// <returns></returns>
  96. public async Task<bool> IsSuperAdmin(int userId)
  97. {
  98. var roles = await GetUserRoleList(userId);
  99. return roles.Any(t => t.RoleType == RoleType.SUPER_ADMIN);
  100. }
  101. /// <summary>
  102. /// 获取指定角色中的用户列表
  103. /// </summary>
  104. /// <param name="roleId"></param>
  105. /// <returns></returns>
  106. public async Task<List<SysUserOutput>> GetInRoleUserList(int roleId)
  107. {
  108. var items = await _roleUserRep.DetachedEntities
  109. .Include(t => t.SysUser)
  110. .Where(t => t.SysRoleId == roleId)
  111. .OrderByDescending(u => u.Id)
  112. .Select(t => t.SysUser)
  113. .ProjectToType<SysUserOutput>()
  114. .ToListAsync();
  115. foreach (var item in items)
  116. {
  117. item.SysRoles = await GetUserRoleList(item.Id);
  118. }
  119. return items;
  120. }
  121. /// <summary>
  122. /// 分页查询角色用户列表
  123. /// </summary>
  124. /// <param name="input"></param>
  125. /// <returns></returns>
  126. public async Task<PageResult<SysUserOutput>> QueryInRoleUserPageList(SysRoleUserPageInput input)
  127. {
  128. var ret = await _roleUserRep.DetachedEntities
  129. .Include(t => t.SysUser)
  130. .Where(t => t.SysRoleId == input.SysRoleId)
  131. .Where(input.SysOrgId.HasValue, t => t.SysUser.SysOrgId == input.SysOrgId)
  132. .OrderByDescending(u => u.Id)
  133. .Select(t => t.SysUser)
  134. .ProjectToType<SysUserOutput>()
  135. .ToADPagedListAsync(input.PageIndex, input.PageSize);
  136. foreach (var item in ret.Items)
  137. {
  138. item.SysRoles = await GetUserRoleList(item.Id);
  139. }
  140. return ret;
  141. }
  142. /// <summary>
  143. /// 获取未在指定角色中的用户列表
  144. /// </summary>
  145. /// <param name="roleId"></param>
  146. /// <returns></returns>
  147. public async Task<List<SysUserOutput>> GetNotInRoleUserList(int roleId)
  148. {
  149. var roleUsers = await _roleUserRep.DetachedEntities.Where(t => t.SysRoleId == roleId).Select(t => t.SysUserId).ToListAsync();
  150. var items = await _roleUserRep.Change<SysUser>().DetachedEntities
  151. .Where(t => t.Id > 1 && !roleUsers.Contains(t.Id))
  152. .OrderByDescending(u => u.Name)
  153. .ProjectToType<SysUserOutput>()
  154. .ToListAsync();
  155. return items;
  156. }
  157. /// <summary>
  158. /// 根据角色Id删除对应的用户-角色表关联信息
  159. /// </summary>
  160. /// <param name="roleId"></param>
  161. /// <returns></returns>
  162. public async Task DeleteRoleUserByRoleId(int roleId)
  163. {
  164. var userRoles = await _roleUserRep.Where(u => u.SysRoleId == roleId).ToListAsync();
  165. await _roleUserRep.DeleteAsync(userRoles);
  166. }
  167. /// <summary>
  168. /// 根据用户Id删除对应的用户-角色表关联信息
  169. /// </summary>
  170. /// <param name="userId"></param>
  171. /// <returns></returns>
  172. public async Task DeleteUserRoleListByUserId(int userId)
  173. {
  174. var surList = await _roleUserRep.AsQueryable(m => m.SysUserId == userId, false).ToListAsync();
  175. await _roleUserRep.DeleteAsync(surList);
  176. }
  177. /// <summary>
  178. /// 通过权限标识获取人员列表
  179. /// </summary>
  180. /// <param name="permission">权限标识</param>
  181. /// <returns></returns>
  182. public async Task<List<SysUserLiteOutput>> GetUserListByPermission(string permission)
  183. {
  184. var roleIds = await _roleMenuService.GetRoleIdListByMenuPermissionCode(permission);
  185. var items = await _roleUserRep.DetachedEntities
  186. .Include(t => t.SysRole)
  187. .Where(t => roleIds.Contains(t.SysRoleId) && t.SysUser.Status == CommonStatus.ENABLE && t.SysUser.IsDeleted == false)
  188. .OrderByDescending(u => u.Id)
  189. .Select(t => t.SysUser)
  190. .ProjectToType<SysUserLiteOutput>()
  191. .ToListAsync();
  192. return items;
  193. }
  194. }