DepartmentUserRepository.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using AutoMapper;
  4. using Datory;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Tools;
  7. using GxPress.Repository.Interface.DepartmentUser;
  8. using Microsoft.Extensions.Caching.Distributed;
  9. using Microsoft.Extensions.Options;
  10. using System.Transactions;
  11. using GxPress.Request.Department;
  12. namespace GxPress.Repository.Implement.DepartmentUser
  13. {
  14. public class DepartmentUserRepository : IDepartmentUserRepository
  15. {
  16. private readonly Repository<Entity.DepartmentUser> _repository;
  17. private readonly IMapper _mapper;
  18. private readonly IDistributedCache _cache;
  19. private readonly string _connectionString;
  20. private readonly string _databaseTypeStr;
  21. public DepartmentUserRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
  22. {
  23. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  24. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  25. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  26. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  27. _repository = new Repository<Entity.DepartmentUser>(database);
  28. _mapper = mapper;
  29. _cache = cache;
  30. }
  31. public IDatabase Database => _repository.Database;
  32. public string TableName => _repository.TableName;
  33. public List<TableColumn> TableColumns => _repository.TableColumns;
  34. /// <summary>
  35. /// 根据部门ID获取用户
  36. /// </summary>
  37. /// <param name="departmentId"></param>
  38. /// <returns></returns>
  39. public async Task<IEnumerable<int>> GetUserIdsAsync(int departmentId)
  40. {
  41. return await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), departmentId).Select(nameof(Entity.DepartmentUser.UserId)));
  42. }
  43. /// <summary>
  44. /// 根据部门ID集合获取用户
  45. /// </summary>
  46. /// <param name="departmentId"></param>
  47. /// <returns></returns>
  48. public async Task<IEnumerable<int>> GetUserIdsAsync(IEnumerable<int> departmentIds)
  49. {
  50. return await _repository.GetAllAsync<int>(Q.WhereIn(nameof(Entity.DepartmentUser.DepartmentId), departmentIds).Select(nameof(Entity.DepartmentUser.UserId)));
  51. }
  52. /// <summary>
  53. /// 获取部门下面的所有用户
  54. /// </summary>
  55. /// <param name="departmentId"></param>
  56. /// <returns></returns>
  57. public async Task<IEnumerable<int>> GetUserIdsAsync()
  58. {
  59. return await _repository.GetAllAsync<int>(Q.Select(nameof(Entity.DepartmentUser.UserId)).GroupBy(nameof(Entity.DepartmentUser.UserId)));
  60. }
  61. /// <summary>
  62. /// 根据部门ID获取部门下面的所有用户
  63. /// </summary>
  64. /// <param name="departmentIds"></param>
  65. /// <returns></returns>
  66. public async Task<IEnumerable<int>> GetUserIdsByDepartmentIdsAsync(IEnumerable<int> departmentIds)
  67. {
  68. return await _repository.GetAllAsync<int>(Q.Select(nameof(Entity.DepartmentUser.UserId)).WhereIn(nameof(Entity.DepartmentUser.DepartmentId), departmentIds).GroupBy(nameof(Entity.DepartmentUser.UserId)));
  69. }
  70. /// <summary>
  71. /// 判断是否有部门
  72. /// </summary>
  73. /// <param name="userId"></param>
  74. /// <returns></returns>
  75. public async Task<bool> IsExistsAsync(int userId)
  76. {
  77. return await _repository.ExistsAsync(Q.Where(nameof(Entity.DepartmentUser.UserId), userId));
  78. }
  79. /// <summary>
  80. /// 根据用户D获取部门ID
  81. /// </summary>
  82. /// <param name="departmentId"></param>
  83. /// <returns></returns>
  84. public async Task<IEnumerable<int>> GetDepartmentIdsAsync(int userId)
  85. {
  86. return await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.UserId), userId).Select(nameof(Entity.DepartmentUser.DepartmentId)));
  87. }
  88. /// <summary>
  89. /// 添加部门成员
  90. /// </summary>
  91. /// <param name="request"></param>
  92. /// <returns></returns>
  93. public async Task<bool> AddDepartmentUserAsync(DepartmentUserInRequest request)
  94. {
  95. try
  96. {
  97. using (var transactionScope = new TransactionScope())
  98. {
  99. //删除
  100. await _repository.DeleteAsync(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), request.DepartmentId));
  101. foreach (var item in request.UserIds)
  102. {
  103. var entity = new Entity.DepartmentUser()
  104. {
  105. DepartmentId = request.DepartmentId,
  106. UserId = item
  107. };
  108. await _repository.InsertAsync(entity);
  109. }
  110. transactionScope.Complete();
  111. }
  112. }
  113. catch
  114. {
  115. return false;
  116. }
  117. return true;
  118. }
  119. /// <summary>
  120. /// 添加部门成员
  121. /// </summary>
  122. /// <param name="request"></param>
  123. /// <returns></returns>
  124. public async Task<bool> AddDepartmentUserAsync(IEnumerable<int> departmentIds, int userId)
  125. {
  126. try
  127. {
  128. using (var transactionScope = new TransactionScope())
  129. {
  130. //删除
  131. await _repository.DeleteAsync(Q.Where(nameof(Entity.DepartmentUser.UserId), userId));
  132. foreach (var item in departmentIds)
  133. {
  134. var entity = new Entity.DepartmentUser()
  135. {
  136. DepartmentId = item,
  137. UserId = userId
  138. };
  139. await _repository.InsertAsync(entity);
  140. }
  141. transactionScope.Complete();
  142. }
  143. }
  144. catch
  145. {
  146. return false;
  147. }
  148. return true;
  149. }
  150. /// <summary>
  151. /// 根据用户id获取所在部门
  152. /// </summary>
  153. /// <param name="userIds"></param>
  154. /// <returns></returns>
  155. public async Task<IEnumerable<Entity.DepartmentUser>> GetDepartmentUsersByUserIdsAsync(IEnumerable<int> userIds)
  156. {
  157. return await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.DepartmentUser.UserId), userIds));
  158. }
  159. }
  160. }