GroupChatUserRepository.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using AutoMapper;
  6. using GxPress.Common.AppOptions;
  7. using GxPress.Common.Exceptions;
  8. using GxPress.Common.Tools;
  9. using GxPress.Entity;
  10. using GxPress.Repository.Interface;
  11. using GxPress.Request.GroupChatUser;
  12. using GxPress.Result.GroupChatUser;
  13. using Microsoft.Extensions.Options;
  14. using Datory;
  15. using GroupChatUser = GxPress.Entity.GroupChatUser;
  16. using Dapper;
  17. namespace GxPress.Repository.Implement
  18. {
  19. public class GroupChatUserRepository : IGroupChatUserRepository
  20. {
  21. private readonly Repository<GroupChatUser> _repository;
  22. private readonly Repository<GroupChat> _groupChatRepository;
  23. private readonly Repository<User> _userRepository;
  24. private readonly Repository<OftenContact> _oftenContactRepository;
  25. private readonly IMapper _mapper;
  26. private readonly Repository<Entity.Middle.Middle> _middleRepository;
  27. private readonly string _connectionString;
  28. private readonly string _databaseTypeStr;
  29. public GroupChatUserRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  30. {
  31. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  32. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  33. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  34. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  35. _repository = new Repository<GroupChatUser>(database);
  36. _groupChatRepository = new Repository<GroupChat>(database);
  37. _userRepository = new Repository<User>(database);
  38. _oftenContactRepository = new Repository<OftenContact>(database);
  39. _middleRepository = new Repository<Entity.Middle.Middle>(database);
  40. _mapper = mapper;
  41. }
  42. public IDatabase Database => _repository.Database;
  43. public string TableName => _repository.TableName;
  44. public List<TableColumn> TableColumns => _repository.TableColumns;
  45. /// <summary>
  46. /// 添加
  47. /// </summary>
  48. /// <param name="request"></param>
  49. /// <returns></returns>
  50. public async Task<bool> InsertAsync(GroupChatUserInRequest request)
  51. {
  52. var user = await _userRepository.GetAsync(request.UserId);
  53. if (user == null)
  54. throw new BusinessException("用户不存在");
  55. var groupChat = await _groupChatRepository.GetAsync(request.GroupChatId);
  56. if (groupChat == null)
  57. throw new BusinessException("群不存在");
  58. //查询用户是否存在
  59. var groupChatUser = await _repository.GetAsync(Q
  60. .Where(nameof(GroupChatUser.GroupChatId), request.GroupChatId)
  61. .Where(nameof(GroupChatUser.UserId), request.UserId));
  62. if (groupChatUser == null)
  63. throw new BusinessException("用户已经添加过了");
  64. var groupChatUserEntity = new GroupChatUser
  65. {
  66. GroupChatId = request.GroupChatId,
  67. UserId = request.UserId,
  68. ImId = request.GroupChartImId
  69. };
  70. //添加用户
  71. var oftenContact = new OftenContact
  72. {
  73. ChatType = 2,
  74. ContactUserId = request.GroupChartImId,
  75. IndividualGroupId = 0,
  76. UserId = request.UserId
  77. };
  78. await _oftenContactRepository.InsertAsync(oftenContact);
  79. return await _repository.InsertAsync(groupChatUserEntity) > 0;
  80. }
  81. /// <summary>
  82. /// 添加多个
  83. /// </summary>
  84. /// <param name="groupChatUsers"></param>
  85. /// <returns></returns>
  86. public async Task<bool> InsertAsync(List<GroupChatUser> groupChatUsers)
  87. {
  88. if (groupChatUsers.Count == 0 || groupChatUsers == null)
  89. return false;
  90. string sql = $@"INSERT INTO `ccpph`.`tede_group_chat_user`(`Guid`,`CreatedDate`,`LastModifiedDate`,`GroupChatId`,`UserId`,`IsDisturb`,`IsTop`,`ImId`,`IsAdmin`,`GroupChatImId`)VALUES";
  91. var guId = Guid.NewGuid().ToString();
  92. var nowTime = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
  93. foreach (var item in groupChatUsers)
  94. {
  95. var IsDisturb = item.IsDisturb ? 1 : 0;
  96. var IsTop = item.IsTop ? 1 : 0;
  97. var IsAdmin = item.IsAdmin ? 1 : 0;
  98. sql += $@"('{guId}','{nowTime}','{nowTime}','{item.GroupChatId}',{item.UserId}, {IsDisturb},{IsTop},'{item.ImId}',{IsAdmin},'{item.GroupChatImId}'),";
  99. }
  100. sql = sql.Remove(sql.Length - 1, 1);
  101. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  102. var database = new Database(databaseType, _connectionString);
  103. var connection = database.GetConnection();
  104. return await connection.ExecuteAsync(sql) > 0;
  105. }
  106. /// <summary>
  107. /// 修改数据
  108. /// </summary>
  109. /// <param name="request"></param>
  110. /// <returns></returns>
  111. public async Task<bool> UpdateAsync(GroupChatUserUpRequest request)
  112. {
  113. throw new NotImplementedException();
  114. }
  115. /// <summary>
  116. /// 删除用户
  117. /// </summary>
  118. /// <param name="id"></param>
  119. /// <returns></returns>
  120. public async Task<bool> DeleteAsync(int id)
  121. {
  122. return await _repository.DeleteAsync(id);
  123. }
  124. /// <summary>
  125. /// 根据群ID获取群用户
  126. /// </summary>
  127. /// <param name="request"></param>
  128. /// <returns></returns>
  129. public async Task<IEnumerable<GroupChatUserByGroupChatIdResult>> GetGroupChatUserByGroupChatIdAsync(GroupChatUserSearchRequest request)
  130. {
  131. var sql = $"SELECT a.*,b.Name,b.AvatarUrl FROM ccpph.tede_group_chat_user a inner join ccpph.tede_user b on a.UserId=b.Id where a.GroupChatImId='{request.GroupChatImId}'";
  132. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  133. var database = new Database(databaseType, _connectionString);
  134. var connection = database.GetConnection();
  135. var items = await connection
  136. .QueryAsync<GroupChatUserByGroupChatIdResult, User, GroupChatUserByGroupChatIdResult>(sql,
  137. (groupChatUserByGroupChatIdResult, user) =>
  138. {
  139. groupChatUserByGroupChatIdResult.UserName = user.Name;
  140. groupChatUserByGroupChatIdResult.AvatarUrl = user != null ? StringUtils.AddDomainMin(user.AvatarUrl) : "";
  141. return groupChatUserByGroupChatIdResult;
  142. },
  143. splitOn: "Name");
  144. return items;
  145. }
  146. /// <summary>
  147. /// 是否免打扰
  148. /// </summary>
  149. /// <param name="request"></param>
  150. /// <returns></returns>
  151. public async Task<bool> UpIsDisturbAsync(GroupChatUserIsDisturbRequest request)
  152. {
  153. //获取群
  154. var groupChat = await _groupChatRepository.GetAsync(request.GroupChatId);
  155. if (groupChat == null)
  156. throw new BusinessException("群已经解散");
  157. var groupChatUser = await _repository.GetAsync(Q.Where(nameof(GroupChatUser.GroupChatId), request.GroupChatId).Where(nameof(GroupChatUser.UserId), request.UserId));
  158. if (groupChatUser == null)
  159. throw new BusinessException("用户不在群里面");
  160. groupChatUser.IsDisturb = !groupChatUser.IsDisturb;
  161. return await _repository.UpdateAsync(groupChatUser);
  162. }
  163. /// <summary>
  164. /// 是否置顶
  165. /// </summary>
  166. /// <param name="request"></param>
  167. /// <returns></returns>
  168. public async Task<bool> UpIsTopAsync(GroupChatUserIsTopRequest request)
  169. {
  170. //获取群
  171. var groupChat = await _groupChatRepository.GetAsync(request.GroupChatId);
  172. if (groupChat == null)
  173. throw new BusinessException("群已经解散");
  174. var oftenContact = await _oftenContactRepository.GetAsync(Q.Where(nameof(Entity.OftenContact.UserId), request.UserId).Where(nameof(Entity.OftenContact.ContactUserId), groupChat.GroupChatImId));
  175. if (oftenContact == null)
  176. return false;
  177. return await _middleRepository.UpdateAsync(Q.Set(nameof(Entity.Middle.Middle.IsTop), request.IsTop).Where(nameof(Entity.Middle.Middle.UserId), request.UserId).Where(nameof(Entity.Middle.Middle.MiddleId), oftenContact.Id).Where(nameof(Entity.Middle.Middle.FolderType), 10)) > 0;
  178. }
  179. /// <summary>
  180. /// 获取
  181. /// </summary>
  182. /// <param name="id"></param>
  183. /// <returns></returns>
  184. public async Task<GroupChatUser> FindGroupChatUserByIdAsync(int id)
  185. {
  186. return await _repository.GetAsync(id);
  187. }
  188. /// <summary>
  189. /// 根据群IMID获取用户
  190. /// </summary>
  191. /// <param name="imId"></param>
  192. /// <returns></returns>
  193. public async Task<IEnumerable<GroupChatUser>> FindGroupChatUserByImId(string imId)
  194. {
  195. var groupChat = await _groupChatRepository.GetAsync(Q.Where(nameof(GroupChat.GroupChatImId), imId));
  196. return await _repository.GetAllAsync(Q.Where(nameof(GroupChatUser.GroupChatId), groupChat.Id).Where(nameof(GroupChatUser.IsAdmin), false));
  197. }
  198. /// <summary>
  199. /// 是否免打扰
  200. /// </summary>
  201. /// <param name="request"></param>
  202. /// <returns></returns>
  203. public async Task<bool> IsDisturbAsync(GroupChatUserIsDisturbRequest request)
  204. {
  205. //获取群
  206. var groupChat = await _groupChatRepository.GetAsync(request.GroupChatId);
  207. if (groupChat == null)
  208. throw new BusinessException("群已经解散");
  209. var groupChatUser = await _repository.GetAsync(Q.Where(nameof(GroupChatUser.GroupChatId), request.GroupChatId).Where(nameof(GroupChatUser.UserId), request.UserId));
  210. if (groupChatUser == null)
  211. return false;
  212. return groupChatUser.IsDisturb;
  213. }
  214. public async Task<IEnumerable<GroupChatUser>> GetAllAsync(SqlKata.Query query)
  215. {
  216. return await _repository.GetAllAsync(query);
  217. }
  218. public async Task<int> InsertAsync(GroupChatUser groupChatUser)
  219. {
  220. return await _repository.InsertAsync(groupChatUser);
  221. }
  222. }
  223. }