DepartmentUserRepository.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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>> GetUserIdsByUserIdAsync(int userId)
  49. {
  50. var departmentIds = await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.UserId), userId).Select(nameof(Entity.DepartmentUser.DepartmentId)));
  51. return await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), departmentIds).Select(nameof(Entity.DepartmentUser.UserId)));
  52. }
  53. /// <summary>
  54. /// 根据部门ID集合获取用户
  55. /// </summary>
  56. /// <param name="departmentId"></param>
  57. /// <returns></returns>
  58. public async Task<IEnumerable<int>> GetUserIdsAsync(IEnumerable<int> departmentIds)
  59. {
  60. return await _repository.GetAllAsync<int>(Q.WhereIn(nameof(Entity.DepartmentUser.DepartmentId), departmentIds).Select(nameof(Entity.DepartmentUser.UserId)));
  61. }
  62. /// <summary>
  63. /// 获取部门下面的所有用户
  64. /// </summary>
  65. /// <param name="departmentId"></param>
  66. /// <returns></returns>
  67. public async Task<IEnumerable<int>> GetUserIdsAsync()
  68. {
  69. return await _repository.GetAllAsync<int>(Q.Select(nameof(Entity.DepartmentUser.UserId)).GroupBy(nameof(Entity.DepartmentUser.UserId)));
  70. }
  71. /// <summary>
  72. /// 根据部门ID获取部门下面的所有用户
  73. /// </summary>
  74. /// <param name="departmentIds"></param>
  75. /// <returns></returns>
  76. public async Task<IEnumerable<int>> GetUserIdsByDepartmentIdsAsync(IEnumerable<int> departmentIds)
  77. {
  78. return await _repository.GetAllAsync<int>(Q.Select(nameof(Entity.DepartmentUser.UserId)).WhereIn(nameof(Entity.DepartmentUser.DepartmentId), departmentIds).GroupBy(nameof(Entity.DepartmentUser.UserId)));
  79. }
  80. /// <summary>
  81. /// 判断是否有部门
  82. /// </summary>
  83. /// <param name="userId"></param>
  84. /// <returns></returns>
  85. public async Task<bool> IsExistsAsync(int userId)
  86. {
  87. return await _repository.ExistsAsync(Q.Where(nameof(Entity.DepartmentUser.UserId), userId));
  88. }
  89. /// <summary>
  90. /// 根据用户D获取部门ID
  91. /// </summary>
  92. /// <param name="departmentId"></param>
  93. /// <returns></returns>
  94. public async Task<IEnumerable<int>> GetDepartmentIdsAsync(int userId)
  95. {
  96. return await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.UserId), userId).Select(nameof(Entity.DepartmentUser.DepartmentId)));
  97. }
  98. /// <summary>
  99. /// 添加部门成员
  100. /// </summary>
  101. /// <param name="request"></param>
  102. /// <returns></returns>
  103. public async Task<bool> AddDepartmentUserAsync(DepartmentUserInRequest request)
  104. {
  105. try
  106. {
  107. using (var transactionScope = new TransactionScope())
  108. {
  109. //删除
  110. await _repository.DeleteAsync(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), request.DepartmentId));
  111. foreach (var item in request.UserIds)
  112. {
  113. var entity = new Entity.DepartmentUser()
  114. {
  115. DepartmentId = request.DepartmentId,
  116. UserId = item
  117. };
  118. await _repository.InsertAsync(entity);
  119. }
  120. transactionScope.Complete();
  121. }
  122. }
  123. catch
  124. {
  125. return false;
  126. }
  127. return true;
  128. }
  129. /// <summary>
  130. /// 添加部门成员
  131. /// </summary>
  132. /// <param name="request"></param>
  133. /// <returns></returns>
  134. public async Task<bool> AddDepartmentUserAsync(IEnumerable<int> departmentIds, int userId)
  135. {
  136. try
  137. {
  138. using (var transactionScope = new TransactionScope())
  139. {
  140. //删除
  141. await _repository.DeleteAsync(Q.Where(nameof(Entity.DepartmentUser.UserId), userId));
  142. foreach (var item in departmentIds)
  143. {
  144. var entity = new Entity.DepartmentUser()
  145. {
  146. DepartmentId = item,
  147. UserId = userId
  148. };
  149. await _repository.InsertAsync(entity);
  150. }
  151. transactionScope.Complete();
  152. }
  153. }
  154. catch
  155. {
  156. return false;
  157. }
  158. return true;
  159. }
  160. /// <summary>
  161. /// 根据用户id获取所在部门
  162. /// </summary>
  163. /// <param name="userIds"></param>
  164. /// <returns></returns>
  165. public async Task<IEnumerable<Entity.DepartmentUser>> GetDepartmentUsersByUserIdsAsync(IEnumerable<int> userIds)
  166. {
  167. return await _repository.GetAllAsync(Q.WhereIn(nameof(Entity.DepartmentUser.UserId), userIds));
  168. }
  169. }
  170. }