DepartmentUserRepository.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. /// 判断是否有部门
  63. /// </summary>
  64. /// <param name="userId"></param>
  65. /// <returns></returns>
  66. public async Task<bool> IsExistsAsync(int userId)
  67. {
  68. return await _repository.ExistsAsync(Q.Where(nameof(Entity.DepartmentUser.UserId), userId));
  69. }
  70. /// <summary>
  71. /// 根据用户D获取部门ID
  72. /// </summary>
  73. /// <param name="departmentId"></param>
  74. /// <returns></returns>
  75. public async Task<IEnumerable<int>> GetDepartmentIdsAsync(int userId)
  76. {
  77. return await _repository.GetAllAsync<int>(Q.Where(nameof(Entity.DepartmentUser.UserId), userId).Select(nameof(Entity.DepartmentUser.DepartmentId)));
  78. }
  79. /// <summary>
  80. /// 添加部门成员
  81. /// </summary>
  82. /// <param name="request"></param>
  83. /// <returns></returns>
  84. public async Task<bool> AddDepartmentUserAsync(DepartmentUserInRequest request)
  85. {
  86. try
  87. {
  88. using (var transactionScope = new TransactionScope())
  89. {
  90. //删除
  91. await _repository.DeleteAsync(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), request.DepartmentId));
  92. foreach (var item in request.UserIds)
  93. {
  94. var entity = new Entity.DepartmentUser()
  95. {
  96. DepartmentId = request.DepartmentId,
  97. UserId = item
  98. };
  99. await _repository.InsertAsync(entity);
  100. }
  101. transactionScope.Complete();
  102. }
  103. }
  104. catch
  105. {
  106. return false;
  107. }
  108. return true;
  109. }
  110. /// <summary>
  111. /// 添加部门成员
  112. /// </summary>
  113. /// <param name="request"></param>
  114. /// <returns></returns>
  115. public async Task<bool> AddDepartmentUserAsync(IEnumerable<int> departmentIds, int userId)
  116. {
  117. try
  118. {
  119. using (var transactionScope = new TransactionScope())
  120. {
  121. //删除
  122. await _repository.DeleteAsync(Q.Where(nameof(Entity.DepartmentUser.UserId), userId));
  123. foreach (var item in departmentIds)
  124. {
  125. var entity = new Entity.DepartmentUser()
  126. {
  127. DepartmentId = item,
  128. UserId = userId
  129. };
  130. await _repository.InsertAsync(entity);
  131. }
  132. transactionScope.Complete();
  133. }
  134. }
  135. catch
  136. {
  137. return false;
  138. }
  139. return true;
  140. }
  141. }
  142. }