SysUserRoleService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using Attribute;
  2. using Dto;
  3. using Extensions;
  4. using Model;
  5. using Model.Base;
  6. using Repository;
  7. using Service;
  8. namespace Services
  9. {
  10. /// <summary>
  11. /// 用户角色
  12. /// </summary>
  13. [AppService(ServiceType = typeof(ISysUserRoleService), ServiceLifetime = LifeTime.Transient)]
  14. public class SysUserRoleService : BaseService<SysUserRole>, ISysUserRoleService
  15. {
  16. /// <summary>
  17. /// 通过角色ID查询角色使用数量
  18. /// </summary>
  19. /// <param name="roleId"></param>
  20. /// <returns></returns>
  21. public int CountUserRoleByRoleId(long roleId)
  22. {
  23. return Count(it => it.RoleId == roleId);
  24. }
  25. /// <summary>
  26. /// 删除用户角色
  27. /// </summary>
  28. /// <param name="userId"></param>
  29. /// <returns></returns>
  30. public int DeleteUserRoleByUserId(int userId)
  31. {
  32. return Delete(it => it.UserId == userId) ? 1 : 0;
  33. }
  34. /// <summary>
  35. /// 批量删除角色对应用户
  36. /// </summary>
  37. /// <param name="roleId"></param>
  38. /// <param name="userIds"></param>
  39. /// <returns></returns>
  40. public int DeleteRoleUserByUserIds(long roleId, List<long> userIds)
  41. {
  42. return Delete(it => it.RoleId == roleId && userIds.Contains(it.UserId)) ? 1 : 0;
  43. }
  44. /// <summary>
  45. /// 添加用户角色
  46. /// </summary>
  47. /// <param name="sysUserRoles"></param>
  48. /// <returns></returns>
  49. public int AddUserRole(List<SysUserRole> sysUserRoles)
  50. {
  51. return Insert(sysUserRoles);
  52. }
  53. /// <summary>
  54. /// 获取用户数据根据角色id
  55. /// </summary>
  56. /// <param name="roleId"></param>
  57. /// <returns></returns>
  58. public List<SysUser> GetSysUsersByRoleId(long roleId)
  59. {
  60. return Context.Queryable<SysUserRole, SysUser>((t1, u) => new JoinQueryInfos(
  61. JoinType.Left, t1.UserId == u.userId))
  62. .WithCache(60 * 10)
  63. .Where((t1, u) => t1.RoleId == roleId && u.DelFlag == "0")
  64. .Select((t1, u) => u)
  65. .ToList();
  66. }
  67. /// <summary>
  68. /// 获取用户数据根据角色id
  69. /// </summary>
  70. /// <param name="roleUserQueryDto"></param>
  71. /// <returns></returns>
  72. public PagedInfo<SysUser> GetSysUsersByRoleId(RoleUserQueryDto roleUserQueryDto)
  73. {
  74. var query = Context.Queryable<SysUserRole, SysUser>((t1, u) => new JoinQueryInfos(
  75. JoinType.Left, t1.UserId == u.userId))
  76. .Where((t1, u) => t1.RoleId == roleUserQueryDto.RoleId && u.DelFlag == "0");
  77. if (!string.IsNullOrEmpty(roleUserQueryDto.UserName))
  78. {
  79. query = query.Where((t1, u) => u.username.Contains(roleUserQueryDto.UserName));
  80. }
  81. return query.Select((t1, u) => u).ToPage(roleUserQueryDto);
  82. }
  83. /// <summary>
  84. /// 获取尚未指派的用户数据根据角色id
  85. /// </summary>
  86. /// <param name="roleUserQueryDto"></param>
  87. /// <returns></returns>
  88. public PagedInfo<SysUser> GetExcludedSysUsersByRoleId(RoleUserQueryDto roleUserQueryDto)
  89. {
  90. var query = Context.Queryable<SysUser>()
  91. .Where(it => it.DelFlag == "0")
  92. .Where(it => SqlFunc.Subqueryable<SysUserRole>().Where(s => s.UserId == it.userId && s.RoleId == roleUserQueryDto.RoleId).NotAny())
  93. .WhereIF(roleUserQueryDto.UserName.IsNotEmpty(), it => it.username.Contains(roleUserQueryDto.UserName));
  94. return query.ToPage(roleUserQueryDto);
  95. }
  96. /// <summary>
  97. /// 新增用户角色信息
  98. /// </summary>
  99. /// <param name="user"></param>
  100. /// <returns></returns>
  101. public int InsertUserRole(SysUser user)
  102. {
  103. //if(user.RoleIds == null) return 0;
  104. List<SysUserRole> userRoles = new();
  105. foreach (var item in user.RoleIds)
  106. {
  107. userRoles.Add(new SysUserRole() { RoleId = item, UserId = user.userId });
  108. }
  109. return userRoles.Count > 0 ? AddUserRole(userRoles) : 0;
  110. }
  111. /// <summary>
  112. /// 新增加角色用户
  113. /// </summary>
  114. /// <param name="roleUsersCreateDto"></param>
  115. /// <returns></returns>
  116. public int InsertRoleUser(RoleUsersCreateDto roleUsersCreateDto)
  117. {
  118. List<SysUserRole> userRoles = new();
  119. foreach (var item in roleUsersCreateDto.UserIds)
  120. {
  121. userRoles.Add(new SysUserRole() { RoleId = roleUsersCreateDto.RoleId, UserId = item });
  122. }
  123. return userRoles.Count > 0 ? AddUserRole(userRoles) : 0;
  124. }
  125. }
  126. }