ChatRecordRepository.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using System.Transactions;
  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.App.ChatRecord;
  12. using GxPress.Result.App.ChatRecord;
  13. using Microsoft.Extensions.Options;
  14. using Datory;
  15. using GxPress.Common.Page;
  16. namespace GxPress.Repository.Implement
  17. {
  18. public class ChatRecordRepository:IChatRecordRepository
  19. {
  20. private readonly Repository<ChatRecord> _repository;
  21. private readonly Repository<User> _userRepository;
  22. private readonly Repository<GroupChat> _groupChatRepository;
  23. private readonly Repository<GroupChatUser> _groupChartUseRepository;
  24. private readonly Repository<OftenContact> _oftenContactRepository;
  25. private readonly Repository<FileLibrary> _fileLibraryRepository;
  26. private readonly IMapper _mapper;
  27. public ChatRecordRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  28. {
  29. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  30. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  31. _repository = new Repository<ChatRecord>(database);
  32. _userRepository=new Repository<User>(database);
  33. _groupChatRepository=new Repository<GroupChat>(database);
  34. _groupChartUseRepository=new Repository<GroupChatUser>(database);
  35. _oftenContactRepository=new Repository<OftenContact>(database);
  36. _fileLibraryRepository=new Repository<FileLibrary>(database);
  37. _mapper = mapper;
  38. }
  39. public IDatabase Database => _repository.Database;
  40. public string TableName => _repository.TableName;
  41. public List<TableColumn> TableColumns => _repository.TableColumns;
  42. /// <summary>
  43. /// 插入聊天信息
  44. /// </summary>
  45. /// <param name="request"></param>
  46. /// <returns></returns>
  47. public async Task<bool> InsertAsync(ChatRecordInRequest request)
  48. {
  49. try
  50. {
  51. using (TransactionScope transactionScope = new TransactionScope())
  52. {
  53. var chatRecord = new ChatRecord
  54. {
  55. ChatType = request.ChatType,
  56. Content = request.Content,
  57. MsgType = request.MsgType,
  58. ReceiveUserId = request.ReceiveUserId,
  59. SendUserId = request.SendUserId
  60. };
  61. //修改界面显示
  62. if (request.ChatType == 1)
  63. {
  64. await _oftenContactRepository.UpdateAsync(Q.Set(nameof(OftenContact.IsDelete), false)
  65. .Where(nameof(OftenContact.UserId), request.SendUserId));
  66. await _oftenContactRepository.UpdateAsync(Q.Set(nameof(OftenContact.IsDelete), false)
  67. .Where(nameof(OftenContact.UserId), int.Parse(request.ReceiveUserId)));
  68. }
  69. else
  70. {
  71. await _oftenContactRepository.UpdateAsync(Q.Set(nameof(OftenContact.IsDelete), false)
  72. .Where(nameof(OftenContact.ContactUserId), request.ReceiveUserId).Where(nameof(OftenContact.IsDelete),true));
  73. }
  74. await _repository.InsertAsync(chatRecord);
  75. transactionScope.Complete();
  76. }
  77. }
  78. catch (Exception e)
  79. {
  80. throw new BusinessException(e.Message);
  81. }
  82. return true;
  83. }
  84. /// <summary>
  85. /// 搜索聊天记录
  86. /// </summary>
  87. /// <param name="request"></param>
  88. /// <returns></returns>
  89. public async Task<PagedList<ChatRecordSearchHistoryResult>> SearchChatRecordHistoryAsync(
  90. ChatRecordSearchHistoryRequest request)
  91. {
  92. var query = Q.NewQuery();
  93. if (!string.IsNullOrEmpty(request.KeyWord))
  94. {
  95. var key = $"%{request.KeyWord}%";
  96. query.WhereLike(nameof(ChatRecord.Content), key);
  97. query.Where(nameof(ChatRecord.MsgType), 1);
  98. }
  99. if (request.MsgType > 0)
  100. {
  101. query.Where(nameof(ChatRecord.MsgType), request.MsgType);
  102. }
  103. if (!string.IsNullOrEmpty(request.ReceiveUserId))
  104. {
  105. query.OrWhere(nameof(ChatRecord.ReceiveUserId), request.ReceiveUserId);
  106. query.OrWhere(nameof(ChatRecord.SendUserId), request.ReceiveUserId);
  107. }
  108. if (DateTime.TryParse(request.BeginTime, out var beginDateTime))
  109. {
  110. if (DateTime.TryParse(request.EndTime, out var endDateTime))
  111. {
  112. if (endDateTime > beginDateTime)
  113. {
  114. query.WhereDate(nameof(ChatRecord.CreatedDate), ">=", beginDateTime);
  115. query.WhereDate(nameof(ChatRecord.CreatedDate), "<=", endDateTime);
  116. }
  117. }
  118. }
  119. var result = new PagedList<ChatRecordSearchHistoryResult> {Total = await _repository.CountAsync(query)};
  120. var chatRecords = await _repository.GetAllAsync(query.OrderByDesc(nameof(ChatRecord.CreatedDate)).ForPage(request.Page,request.PerPage));
  121. var list=new List<ChatRecordSearchHistoryResult>();
  122. foreach (var chatRecord in chatRecords)
  123. {
  124. var chatRecordSearchHistory = new ChatRecordSearchHistoryResult
  125. {
  126. ChatType = chatRecord.ChatType,
  127. MsgType = chatRecord.MsgType,
  128. Content = chatRecord.Content,
  129. FileUrl = chatRecord.FileUrl,
  130. ReceiveUserId = chatRecord.ReceiveUserId
  131. };
  132. if (chatRecord.MsgType > 1)
  133. {
  134. if (!string.IsNullOrEmpty(chatRecord.Content))
  135. {
  136. var fileLibrary = await _fileLibraryRepository.GetAsync(int.Parse(chatRecord.Content));
  137. chatRecordSearchHistory.FileUrl = StringUtils.AddDomain(fileLibrary.FileUrl);
  138. }
  139. }
  140. //查询用户或者群
  141. if (chatRecord.ChatType == 1)
  142. {
  143. var user =await _userRepository.GetAsync(chatRecord.ReceiveUserId);
  144. if (user != null)
  145. {
  146. chatRecordSearchHistory.Name = user.Name;
  147. chatRecordSearchHistory.AvatarUrl = StringUtils.AddDomain(user.AvatarUrl);
  148. }
  149. }
  150. if (chatRecord.ChatType == 2)
  151. {
  152. var groupChat = await _groupChatRepository.GetAsync(chatRecord.ReceiveUserId);
  153. if (groupChat != null)
  154. {
  155. chatRecordSearchHistory.Name = groupChat.Name;
  156. chatRecordSearchHistory.AvatarUrl = StringUtils.AddDomain(groupChat.AvatarUrl);
  157. }
  158. }
  159. chatRecordSearchHistory.CreatedDate = chatRecord.CreatedDate;
  160. list.Add(chatRecordSearchHistory);
  161. }
  162. result.Items = list;
  163. return result;
  164. }
  165. public async Task<IEnumerable<ChatRecord>> GetAllAsync()
  166. {
  167. return await _repository.GetAllAsync();
  168. }
  169. }
  170. }