DepartmentUserRepository.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /// 添加部门成员
  45. /// </summary>
  46. /// <param name="request"></param>
  47. /// <returns></returns>
  48. public async Task<bool> AddDepartmentUserAsync(DepartmentUserInRequest request)
  49. {
  50. try
  51. {
  52. using (var transactionScope = new TransactionScope())
  53. {
  54. //删除
  55. await _repository.DeleteAsync(Q.Where(nameof(Entity.DepartmentUser.DepartmentId), request.DepartmentId));
  56. foreach (var item in request.UserIds)
  57. {
  58. var entity = new Entity.DepartmentUser()
  59. {
  60. DepartmentId = request.DepartmentId,
  61. UserId = item
  62. };
  63. await _repository.InsertAsync(entity);
  64. }
  65. transactionScope.Complete();
  66. }
  67. }
  68. catch
  69. {
  70. return false;
  71. }
  72. return true;
  73. }
  74. /// <summary>
  75. /// 添加部门成员
  76. /// </summary>
  77. /// <param name="request"></param>
  78. /// <returns></returns>
  79. public async Task<bool> AddDepartmentUserAsync(IEnumerable<int> departmentIds, int userId)
  80. {
  81. try
  82. {
  83. using (var transactionScope = new TransactionScope())
  84. {
  85. //删除
  86. await _repository.DeleteAsync(Q.Where(nameof(Entity.DepartmentUser.UserId), userId));
  87. foreach (var item in departmentIds)
  88. {
  89. var entity = new Entity.DepartmentUser()
  90. {
  91. DepartmentId = item,
  92. UserId = userId
  93. };
  94. await _repository.InsertAsync(entity);
  95. }
  96. transactionScope.Complete();
  97. }
  98. }
  99. catch
  100. {
  101. return false;
  102. }
  103. return true;
  104. }
  105. }
  106. }