AddressBookGroupUserRepository.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Exceptions;
  7. using GxPress.Common.Tools;
  8. using GxPress.Entity;
  9. using GxPress.Repository.Interface;
  10. using GxPress.Request.AddressBookGroupUser;
  11. using GxPress.Result.AddressBookGroupUser;
  12. using Microsoft.Extensions.Options;
  13. using Datory;
  14. using SqlKata;
  15. using System.Transactions;
  16. using Dapper;
  17. namespace GxPress.Repository.Implement
  18. {
  19. public class AddressBookGroupUserRepository : IAddressBookGroupUserRepository
  20. {
  21. private readonly Repository<AddressBookGroupUser> _repository;
  22. private readonly Repository<AddressBookGroup> _addressBookGroupRepository;
  23. private readonly Repository<User> _userRepository;
  24. private readonly IMapper _mapper;
  25. private readonly string _connectionString;
  26. private readonly string _databaseTypeStr;
  27. public AddressBookGroupUserRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  28. {
  29. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  30. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  31. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  32. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  33. _repository = new Repository<AddressBookGroupUser>(database);
  34. _userRepository = new Repository<User>(database);
  35. _addressBookGroupRepository = new Repository<AddressBookGroup>(database);
  36. _mapper = mapper;
  37. }
  38. public IDatabase Database => _repository.Database;
  39. public string TableName => _repository.TableName;
  40. public List<TableColumn> TableColumns => _repository.TableColumns;
  41. /// <summary>
  42. /// 删除
  43. /// </summary>
  44. /// <param name="id"></param>
  45. /// <returns></returns>
  46. public async Task<bool> DeleteAsync(int id)
  47. {
  48. return await _repository.DeleteAsync(id);
  49. }
  50. /// <summary>
  51. /// 批量删除通讯录自建分组用户
  52. /// </summary>
  53. /// <param name="ids"></param>
  54. /// <returns></returns>
  55. public async Task<bool> DeletesAsync(List<int> ids)
  56. {
  57. return await _repository.DeleteAsync(Q.WhereIn(nameof(Entity.AddressBookGroupUser.Id), ids)) > 0;
  58. }
  59. /// <summary>
  60. /// 查询通讯录好友
  61. /// </summary>
  62. /// <param name="request"></param>
  63. /// <param name="userId"></param>
  64. /// <returns></returns>
  65. public async Task<AddressBookGroupUserListResult> GetAddressBookGroupUserListAsync(
  66. AddressBookGroupUserSearchRequest request, int userId)
  67. {
  68. var result = new AddressBookGroupUserListResult();
  69. //sql
  70. var sql = $"select b.Id,b.AddressBookGroupId,a.Id as UserId,a.Name as UserName,a.AvatarUrl,a.ImId from ccpph.tede_user a inner join ccpph.tede_address_book_group_user b on a.Id=b.UserId where b. AddressBookGroupId={request.AddressBookGroupId}";
  71. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  72. var database = new Database(databaseType, _connectionString);
  73. var connection = database.GetConnection();
  74. result.AddressBookGroupUsers = await connection.QueryAsync<AddressBookGroupUserModel>(sql);
  75. var userlist = result.AddressBookGroupUsers.ToList();
  76. //排除成员
  77. for (int i = 0; i < userlist.Count; i++)
  78. {
  79. if (!string.IsNullOrWhiteSpace(userlist[i].AvatarUrl))
  80. userlist[i].AvatarUrl = StringUtils.AddDomainMin(userlist[i].AvatarUrl);
  81. if (!request.IsShow)
  82. if (request.UserIds.Contains(userlist[i].UserId))
  83. userlist.Remove(userlist[i]);
  84. if (request.IsShow)
  85. if (request.UserIds.Contains(userlist[i].UserId))
  86. userlist[i].IsDisable = true;
  87. }
  88. result.AddressBookGroupUsers = userlist;
  89. result.AddressBookGroups =
  90. await _addressBookGroupRepository.GetAllAsync(Q.Where(nameof(AddressBookGroup.ParentId),
  91. request.AddressBookGroupId).Where(nameof(AddressBookGroup.UserId), userId));
  92. return result;
  93. }
  94. /// <summary>
  95. /// 添加
  96. /// </summary>
  97. /// <param name="request"></param>
  98. /// <returns></returns>
  99. public async Task<bool> InsertAsync(AddressBookGroupUserInRequest request)
  100. {
  101. //查询组是否存在
  102. var addressBookGroup = await _addressBookGroupRepository.GetAsync(request.AddressBookGroupId);
  103. if (addressBookGroup == null)
  104. throw new BusinessException("通讯录组不存在!");
  105. var user = await _userRepository.GetAsync(request.UserId);
  106. if (user == null)
  107. throw new BusinessException("用户不存在!");
  108. var addressBookGroupUserEntity = await _repository.GetAsync(Q
  109. .Where(nameof(AddressBookGroupUser.UserId), request.UserId)
  110. .Where(nameof(AddressBookGroupUser.AddressBookGroupId), request.AddressBookGroupId));
  111. if (addressBookGroupUserEntity != null)
  112. throw new BusinessException("用户已经存在!");
  113. var addressBookGroupUser = new AddressBookGroupUser
  114. {
  115. AddressBookGroupId = request.AddressBookGroupId,
  116. UserId = request.UserId
  117. };
  118. return await _repository.InsertAsync(addressBookGroupUser) > 0;
  119. }
  120. /// <summary>
  121. /// 移动分组成员到分组下面
  122. /// </summary>
  123. /// <param name="request"></param>
  124. /// <returns></returns>
  125. public async Task<bool> AddressBookGroupUserMoveAsync(AddressBookGroupUserMoveRequest request)
  126. {
  127. var addressBookGroup = await _addressBookGroupRepository.GetAsync(request.AddressBookGroupId);
  128. if (addressBookGroup == null)
  129. throw new BusinessException("分组ID不存在");
  130. try
  131. {
  132. //事务
  133. using (var transactionScope = new TransactionScope())
  134. {
  135. //获取成员
  136. var addressBookGroupUsers = await _repository.GetAllAsync(Q.WhereIn(nameof(AddressBookGroupUser.Id), request.AddressBookGroupUserId));
  137. //获取小组成员
  138. var addressBookGroupUsersDto = await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), request.AddressBookGroupId));
  139. foreach (var item in addressBookGroupUsers)
  140. {
  141. if (!addressBookGroupUsersDto.Any(n => n.UserId == item.UserId))
  142. {
  143. item.Id = 0;
  144. item.AddressBookGroupId = request.AddressBookGroupId;
  145. //添加
  146. await _repository.InsertAsync(item);
  147. }
  148. }
  149. await _repository.DeleteAsync(Q.WhereIn(nameof(AddressBookGroupUser.Id), request.AddressBookGroupUserId));
  150. transactionScope.Complete();
  151. }
  152. }
  153. catch (System.Exception ex)
  154. {
  155. throw new BusinessException(ex.Message);
  156. }
  157. return true;
  158. }
  159. public async Task<IEnumerable<AddressBookGroupUser>> GetAllAsync(Query query)
  160. {
  161. return await _repository.GetAllAsync(query);
  162. }
  163. }
  164. }