GroupChatUserRepository.cs 13 KB

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