MissiveRepository.cs 10 KB

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