MissiveRepository.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using Dapper;
  6. using Datory;
  7. using GxPress.Common.AppOptions;
  8. using GxPress.Common.Page;
  9. using GxPress.Common.Tools;
  10. using GxPress.Entity;
  11. using GxPress.EnumConst;
  12. using GxPress.Repository.Interface.Missive;
  13. using GxPress.Request.App.Missive;
  14. using GxPress.Result.App.Missive;
  15. using GxPress.Result.Job;
  16. using Microsoft.Extensions.Caching.Distributed;
  17. using Microsoft.Extensions.Options;
  18. using SqlKata;
  19. namespace GxPress.Repository.Implement.Missive
  20. {
  21. public class MissiveRepository : IMissiveRepository
  22. {
  23. private readonly Repository<Entity.Missive.Missive> _repository;
  24. private readonly IMapper _mapper;
  25. private readonly IDistributedCache _cache;
  26. private readonly string _connectionString;
  27. private readonly string _databaseTypeStr;
  28. public MissiveRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper, IDistributedCache cache)
  29. {
  30. _databaseTypeStr = dbOptionsAccessor.CurrentValue.DatabaseType;
  31. _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
  32. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  33. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  34. _repository = new Repository<Entity.Missive.Missive>(database);
  35. _mapper = mapper;
  36. _cache = cache;
  37. }
  38. public IDatabase Database => _repository.Database;
  39. public string TableName => _repository.TableName;
  40. public List<TableColumn> TableColumns => _repository.TableColumns;
  41. public async Task<int> InsertAsync(Entity.Missive.Missive missive)
  42. {
  43. return await _repository.InsertAsync(missive);
  44. }
  45. public async Task<Entity.Missive.Missive> GetAsync(int id)
  46. {
  47. return await _repository.GetAsync(id);
  48. }
  49. public async Task<Entity.Missive.Missive> GetAsync(SqlKata.Query query)
  50. {
  51. return await _repository.GetAsync(query);
  52. }
  53. public async Task<bool> UpdateAsync(Entity.Missive.Missive missive)
  54. {
  55. return await _repository.UpdateAsync(missive);
  56. }
  57. public async Task<bool> DeleteAsync(Query query)
  58. {
  59. return await _repository.DeleteAsync(query) > 0;
  60. }
  61. public async Task<bool> UpdateAsync(Query query)
  62. {
  63. return await _repository.UpdateAsync(query) > 0;
  64. }
  65. public async Task<IEnumerable<Entity.Missive.Missive>> GetAllAsync(Query query)
  66. {
  67. return await _repository.GetAllAsync(query);
  68. }
  69. /// <summary>
  70. /// 获取站内信
  71. /// </summary>
  72. /// <param name="request"></param>
  73. /// <returns></returns>
  74. public async Task<PagedList<MissiveSearchResult>> GetMissiveSearchResultAsync(MissiveSearchRequest request)
  75. {
  76. var sb = "";
  77. if (!string.IsNullOrEmpty(request.Key))
  78. sb += $@" AND a.AttributeValue != 2 AND (b.Title LIKE '%{request.Key}%'
  79. OR b.Content LIKE '%{request.Key}%'
  80. OR c.Name LIKE '%{request.Key}%'
  81. OR a.UserId IN (SELECT
  82. Id
  83. FROM
  84. tede_user
  85. WHERE
  86. Id IN (SELECT
  87. UserId
  88. FROM
  89. tede_addressee
  90. WHERE SourceType={AllTypeConst.Missive.GetHashCode()} and
  91. SourceId = a.MiddleId)
  92. AND Name LIKE '%{request.Key}%')
  93. OR a.CreatedDate LIKE '%{request.Key}%')";
  94. if (request.FolderId > 0)
  95. sb += $" AND a.ParentId = {request.FolderId}";
  96. if (request.SearchType == 1)
  97. sb += $" AND a.ParentId = {request.FolderId} AND a.UserId = {request.UserId} and a.IsDelete=0 and a.NoticeAddresseeType!=2";
  98. else if (request.SearchType == 2)
  99. sb += $" AND a.UserId = {request.UserId} and a.IsAdmin=1 and a.IsDelete=0 and a.NoticeAddresseeType!=2";
  100. else if (request.SearchType == 3)
  101. sb += $" AND a.UserId = {request.UserId} and a.IsAdmin=1 and a.NoticeAddresseeType=2 and a.IsDelete=0";
  102. else if (request.SearchType == 4)
  103. sb += $" AND a.UserId = {request.UserId} and a.IsDelete=1 AND a.AttributeValue=1";
  104. else if (request.SearchType == 5)
  105. sb += $" AND a.UserId = {request.UserId} and b.MissiveType=2 and a.IsDelete=0";
  106. else
  107. sb += $" AND a.UserId = {request.UserId} and a.IsDelete=0";
  108. var missiveTypeValue = GxPress.EnumConst.AllTypeConst.Missive.GetHashCode();
  109. var sql = $@"SELECT
  110. a.*,(SELECT GROUP_CONCAT(SourceName Separator'、') from tede_user_middle where MiddleType=30 and DataSourceId=a.MiddleId) as EnjoyUser,
  111. (SELECT
  112. COUNT(1)
  113. FROM
  114. tede_middle
  115. WHERE
  116. ParentId = a.Id and IsDelete=0 and NoticeAddresseeType=1) as FileCount,
  117. (SELECT
  118. COUNT(1)
  119. FROM
  120. tede_addressee
  121. WHERE
  122. SourceType={missiveTypeValue} and
  123. SourceId = a.MiddleId AND IsRead = 1) AS ReadCount,
  124. (SELECT
  125. COUNT(1)
  126. FROM
  127. tede_addressee
  128. WHERE SourceType={missiveTypeValue} and
  129. SourceId = a.MiddleId ) AS AllCount,
  130. (SELECT
  131. COUNT(1)
  132. FROM
  133. tede_addressee
  134. WHERE
  135. SourceType={missiveTypeValue} and
  136. SourceId = a.MiddleId AND IsRead = 1
  137. AND UserId = {request.UserId}
  138. LIMIT 0 , 1) AS IsRead, b.Title, b.UserName, b.MissiveType,b.SendUserId,b.Content,b.SourceId
  139. FROM
  140. tede_middle a
  141. left JOIN
  142. tede_missive b ON a.MiddleId = b.Id INNER JOIN
  143. tede_user c ON c.Id = a.UserId
  144. WHERE
  145. a.FolderType = 5 {sb} order by a.IsTop desc,a.LastModifiedDate desc,a.AttributeValue desc limit {(request.Page - 1) * request.PerPage},{request.PerPage}";
  146. var sqlCount = $@"SELECT
  147. count(1)
  148. FROM
  149. tede_middle a
  150. left JOIN
  151. tede_missive b ON a.MiddleId = b.Id INNER JOIN
  152. tede_user c ON c.Id = a.UserId
  153. WHERE
  154. a.FolderType = {missiveTypeValue} {sb} ";
  155. var result = new PagedList<MissiveSearchResult>();
  156. var databaseType = _databaseTypeStr.ToEnum(DatabaseType.MySql);
  157. var database = new Database(databaseType, _connectionString);
  158. var connection = database.GetConnection();
  159. result.Items =
  160. await connection
  161. .QueryAsync<MissiveSearchResult, Entity.Missive.Missive, MissiveSearchResult>(sql,
  162. (missiveSearchResult, missive) =>
  163. {
  164. missiveSearchResult.Title = missive != null ? missive.Title : "";
  165. missiveSearchResult.MissiveType = missive?.MissiveType ?? 0;
  166. missiveSearchResult.Content = missive != null ? missive.Content : "[]";
  167. missiveSearchResult.SourceId = missive != null ? missive.SourceId : 0;
  168. missiveSearchResult.SendUserId = missive != null ? missive.SendUserId : 0;
  169. if (missive != null) missiveSearchResult.SendName = missive.UserName;
  170. return missiveSearchResult;
  171. }, null, splitOn: "Title");
  172. result.Total = await connection.ExecuteScalarAsync<int>(sqlCount);
  173. return result;
  174. }
  175. public async Task<List<JobMissiveResult>> ElasticSearchMissive()
  176. {
  177. var sql = "select a.Id,a.UserId,a.GuId,a.CreatedDate,b.Id,b.Title,b.Content,b.CreatedDate,c.Name,c.AvatarUrl from tede_middle a inner join tede_Missive b on a.MiddleId=b.Id inner join tede_user c on c.Id=b.UserId where a.FolderType=5 and a.IsUpload=0 order by a.CreatedDate desc limit 0,100";
  178. var databaseType = StringUtils.ToEnum<DatabaseType>(_databaseTypeStr, DatabaseType.MySql);
  179. var database = new Database(databaseType, _connectionString);
  180. var connection = database.GetConnection();
  181. var items = await connection
  182. .QueryAsync<JobMissiveResult, Entity.Missive.Missive, User, JobMissiveResult>(sql,
  183. (jobMissiveResult, missive, user) =>
  184. {
  185. jobMissiveResult.MiddleId = jobMissiveResult.Id;
  186. jobMissiveResult.Id = missive.Id;
  187. jobMissiveResult.Title = missive != null ? missive.Title : "";
  188. jobMissiveResult.Content = missive != null ? missive.Content : "";
  189. jobMissiveResult.AvatarUrl = user != null ? user.AvatarUrl : "";
  190. jobMissiveResult.Name = user != null ? user.Name : "";
  191. return jobMissiveResult;
  192. },
  193. splitOn: "Id,Id,Name");
  194. return items.ToList();
  195. }
  196. }
  197. }