ChatSearchService.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using Dapper;
  4. using Datory;
  5. using GxPress.Common.Tools;
  6. using GxPress.EnumConst;
  7. using GxPress.Result.Chat;
  8. using GxPress.Service.Interface;
  9. using GxPress.Service.Interface.Chat;
  10. namespace GxPress.Service.Implement.Chat
  11. {
  12. public class ChatSearchService : IChatSearchService
  13. {
  14. private readonly IUserService userService;
  15. public ChatSearchService(IUserService userService)
  16. {
  17. this.userService = userService;
  18. }
  19. /// <summary>
  20. /// 搜索单聊
  21. /// </summary>
  22. /// <param name="keyWord"></param>
  23. /// <param name="userId"></param>
  24. /// <returns></returns>
  25. public async Task<IEnumerable<ChatSearchResult>> GetChatSearchAsync(string keyWord, int userId)
  26. {
  27. if (string.IsNullOrEmpty(keyWord))
  28. return new List<ChatSearchResult>();
  29. var chatConstValue = AllTypeConst.Wechat.GetHashCode();
  30. string sqlStr = string.Empty;
  31. if (!string.IsNullOrEmpty(keyWord))
  32. sqlStr += $" and c.Name like '%{keyWord}%'";
  33. var sql = $@"SELECT
  34. c.Id, c.Name,c.Phone as ImId,c.AvatarUrl,b.LastModifiedDate
  35. FROM
  36. tede_middle a
  37. INNER JOIN
  38. tede_often_contact b ON a.middleId = b.Id
  39. INNER JOIN
  40. tede_user c ON c.Id = b.ContactUserId
  41. WHERE
  42. a.FolderType = {chatConstValue} AND a.UserId = {userId}
  43. AND a.IsDelete = 0
  44. AND b.ChatType = 1 {sqlStr}";
  45. var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
  46. var connection = database.GetConnection();
  47. var result = await connection.QueryAsync<ChatSearchResult>(sql);
  48. foreach (var item in result)
  49. {
  50. item.TypeValue = 1;
  51. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  52. }
  53. return result;
  54. }
  55. /// <summary>
  56. /// 搜索信源文件夹
  57. /// </summary>
  58. /// <param name="keyWord"></param>
  59. /// <param name="userId"></param>
  60. /// <returns></returns>
  61. public async Task<IEnumerable<ChatSearchResult>> GetChatFolderNameSearchAsync(string keyWord, int userId)
  62. {
  63. if (string.IsNullOrEmpty(keyWord))
  64. return new List<ChatSearchResult>();
  65. var chatConstValue = AllTypeConst.Wechat.GetHashCode();
  66. string sqlStr = string.Empty;
  67. if (!string.IsNullOrEmpty(keyWord))
  68. sqlStr += $" and a.FolderName like '%{keyWord}%'";
  69. var sql = $@"SELECT
  70. a.Id,a.FolderName as Name,a.LastModifiedDate
  71. FROM
  72. tede_middle a
  73. WHERE
  74. a.FolderType = {chatConstValue} AND a.UserId = {userId}
  75. AND a.IsDelete = 0
  76. AND a.AttributeValue = 2 {sqlStr}";
  77. var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
  78. var connection = database.GetConnection();
  79. var result = await connection.QueryAsync<ChatSearchResult>(sql);
  80. foreach (var item in result)
  81. {
  82. item.TypeValue = 3;
  83. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  84. }
  85. return result;
  86. }
  87. /// <summary>
  88. /// 搜索信源群聊
  89. /// </summary>
  90. /// <param name="keyWord"></param>
  91. /// <param name="userId"></param>
  92. /// <returns></returns>
  93. public async Task<IEnumerable<ChatSearchResult>> GetGroupChatNameSearchAsync(string keyWord, int userId)
  94. {
  95. if (string.IsNullOrEmpty(keyWord))
  96. return new List<ChatSearchResult>();
  97. var chatConstValue = AllTypeConst.Wechat.GetHashCode();
  98. string sqlStr = string.Empty;
  99. if (!string.IsNullOrEmpty(keyWord))
  100. sqlStr += $" AND d.Name LIKE '%{keyWord}%'";
  101. var sql = $@"SELECT
  102. d.Id,
  103. d.Name,
  104. d.AvatarUrl,
  105. d.GroupChatImId AS ImId,
  106. d.CreatedDate AS LastModifiedDate
  107. FROM
  108. tede_middle a
  109. INNER JOIN
  110. tede_often_contact b ON a.middleId = b.Id
  111. INNER JOIN
  112. tede_group_chat d ON d.GroupChatImId = b.ContactUserId
  113. WHERE
  114. a.FolderType = {chatConstValue} AND a.UserId ={userId}
  115. AND a.IsDelete = 0
  116. AND b.ChatType = 2
  117. {sqlStr}
  118. ORDER BY d.CreatedDate DESC;";
  119. var database = new Database(DatabaseType.MySql, ConfigHelper.GetValue("Database:ConnectionString"));
  120. var connection = database.GetConnection();
  121. var result = await connection.QueryAsync<ChatSearchResult>(sql);
  122. foreach (var item in result)
  123. {
  124. item.TypeValue = 2;
  125. item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
  126. }
  127. return result;
  128. }
  129. /// <summary>
  130. /// 查询好友
  131. /// </summary>
  132. /// <param name="keyWord"></param>
  133. /// <param name="userId"></param>
  134. /// <returns></returns>
  135. public async Task<IEnumerable<ChatSearchResult>> GetFriendUserInfoResult(string keyWord, int userId)
  136. {
  137. var users = await userService.GetFriendUserInfoResult(userId, keyWord);
  138. var result = new List<ChatSearchResult>();
  139. foreach (var item in users)
  140. {
  141. result.Add(new ChatSearchResult
  142. {
  143. Id = item.Id,
  144. Name = item.Name,
  145. AvatarUrl = item.AvatarUrl,
  146. DepartmentName = item.DepartmentName,
  147. ImId = item.Phone
  148. });
  149. }
  150. return result;
  151. }
  152. }
  153. }