AddressBookGroupUserRepository.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 tede_user a
  71. inner join tede_address_book_group_user b on a.Id=b.UserId
  72. where b. AddressBookGroupId={request.AddressBookGroupId}";
  73. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  74. var database = new Database(databaseType, _connectionString);
  75. var connection = database.GetConnection();
  76. result.AddressBookGroupUsers = await connection.QueryAsync<AddressBookGroupUserModel>(sql);
  77. var userlist = result.AddressBookGroupUsers.ToList();
  78. //排除成员
  79. for (int i = 0; i < userlist.Count; i++)
  80. {
  81. if (!string.IsNullOrWhiteSpace(userlist[i].AvatarUrl))
  82. userlist[i].AvatarUrl = StringUtils.AddDomainMin(userlist[i].AvatarUrl);
  83. if (!request.IsShow)
  84. if (request.UserIds.Contains(userlist[i].UserId))
  85. userlist.Remove(userlist[i]);
  86. if (request.IsShow)
  87. if (request.UserIds.Contains(userlist[i].UserId))
  88. userlist[i].IsDisable = true;
  89. }
  90. result.AddressBookGroupUsers = userlist;
  91. result.AddressBookGroups =
  92. await _addressBookGroupRepository.GetAllAsync(Q.Where(nameof(AddressBookGroup.ParentId),
  93. request.AddressBookGroupId).Where(nameof(AddressBookGroup.UserId), userId).OrderByDesc(nameof(AddressBookGroup.Sort)));
  94. return result;
  95. }
  96. /// <summary>
  97. /// 添加
  98. /// </summary>
  99. /// <param name="request"></param>
  100. /// <returns></returns>
  101. public async Task<bool> InsertAsync(AddressBookGroupUserInRequest request)
  102. {
  103. //查询组是否存在
  104. var addressBookGroup = await _addressBookGroupRepository.GetAsync(request.AddressBookGroupId);
  105. if (addressBookGroup == null)
  106. throw new BusinessException("通讯录组不存在!");
  107. try
  108. {
  109. using (var trans = new TransactionScope())
  110. {
  111. foreach (var item in request.UserId)
  112. {
  113. var addressBookGroupUser = new AddressBookGroupUser
  114. {
  115. AddressBookGroupId = request.AddressBookGroupId,
  116. UserId = item
  117. };
  118. //查询用户是否是好友
  119. await _repository.InsertAsync(addressBookGroupUser);
  120. }
  121. trans.Complete();
  122. }
  123. }
  124. catch
  125. {
  126. return false;
  127. }
  128. return true;
  129. }
  130. /// <summary>
  131. /// 移动分组成员到分组下面
  132. /// </summary>
  133. /// <param name="request"></param>
  134. /// <returns></returns>
  135. public async Task<bool> AddressBookGroupUserMoveAsync(AddressBookGroupUserMoveRequest request)
  136. {
  137. var addressBookGroup = await _addressBookGroupRepository.GetAsync(request.AddressBookGroupId);
  138. if (addressBookGroup == null)
  139. throw new BusinessException("分组ID不存在");
  140. try
  141. {
  142. //事务
  143. using (var transactionScope = new TransactionScope())
  144. {
  145. //获取成员
  146. var addressBookGroupUsers = await _repository.GetAllAsync(Q.WhereIn(nameof(AddressBookGroupUser.Id), request.AddressBookGroupUserId));
  147. //获取小组成员
  148. var addressBookGroupUsersDto = await _repository.GetAllAsync(Q.Where(nameof(AddressBookGroupUser.AddressBookGroupId), request.AddressBookGroupId));
  149. foreach (var item in addressBookGroupUsers)
  150. {
  151. if (!addressBookGroupUsersDto.Any(n => n.UserId == item.UserId))
  152. {
  153. item.Id = 0;
  154. item.AddressBookGroupId = request.AddressBookGroupId;
  155. //添加
  156. await _repository.InsertAsync(item);
  157. }
  158. }
  159. await _repository.DeleteAsync(Q.WhereIn(nameof(AddressBookGroupUser.Id), request.AddressBookGroupUserId));
  160. transactionScope.Complete();
  161. }
  162. }
  163. catch (System.Exception ex)
  164. {
  165. throw new BusinessException(ex.Message);
  166. }
  167. return true;
  168. }
  169. public async Task<IEnumerable<AddressBookGroupUser>> GetAllAsync(Query query)
  170. {
  171. return await _repository.GetAllAsync(query);
  172. }
  173. }
  174. }