ArticleRepository.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using AutoMapper;
  5. using GxPress.Common.AppOptions;
  6. using GxPress.Common.Page;
  7. using GxPress.Common.Tools;
  8. using GxPress.Entity;
  9. using GxPress.Entity.Article;
  10. using GxPress.Repository.Interface;
  11. using GxPress.Request.Admin.Article;
  12. using GxPress.Request.Article;
  13. using GxPress.Result.Article;
  14. using Microsoft.Extensions.Options;
  15. using Datory;
  16. namespace GxPress.Repository.Implement
  17. {
  18. public class ArticleRepository : IArticleRepository
  19. {
  20. private readonly Repository<Article> _repository;
  21. private readonly Repository<Comment> _commentRepository;
  22. private readonly Repository<ArticleGroup> _articleGroupRepository;
  23. private readonly Repository<Entity.Analyze.Analyze> _analyzeRepository;
  24. private readonly IMapper _mapper;
  25. public ArticleRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  26. {
  27. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  28. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  29. _repository = new Repository<Article>(database);
  30. _commentRepository = new Repository<Comment>(database);
  31. _articleGroupRepository = new Repository<ArticleGroup>(database);
  32. _analyzeRepository = new Repository<Entity.Analyze.Analyze>(database);
  33. _mapper = mapper;
  34. }
  35. public IDatabase Database => _repository.Database;
  36. public string TableName => _repository.TableName;
  37. public List<TableColumn> TableColumns => _repository.TableColumns;
  38. public async Task<bool> ExistsAsync(int id)
  39. {
  40. return await _repository.ExistsAsync(id);
  41. }
  42. public async Task<int> InsertAsync(Article process)
  43. {
  44. return await _repository.InsertAsync(process);
  45. }
  46. public async Task<PagedList<ArticleResult>> GetUserListAsync(ArticleSearchRequest request, IEnumerable<BlacklistArticle> blacklistArticles)
  47. {
  48. var pagedList = new PagedList<ArticleResult>();
  49. var query = Q.NewQuery();
  50. if (!string.IsNullOrEmpty(request.Keyword))
  51. {
  52. var like = $"%{request.Keyword}%";
  53. query.WhereLike(nameof(Article.Title), like)
  54. .OrWhereLike(nameof(Article.Author), like)
  55. .OrWhereLike(nameof(Article.Source), like);
  56. }
  57. if (request.GroupId > 0)
  58. query.Where(nameof(Article.GroupId), request.GroupId);
  59. //屏蔽文章
  60. foreach (var item in blacklistArticles)
  61. {
  62. if (item.BlacklistType == 1)
  63. {
  64. query.WhereNot(nameof(Article.Author), item.BlacklistValue);
  65. }
  66. if (item.BlacklistType == 2)
  67. {
  68. query.WhereNot(nameof(Article.Id), item.BlacklistValue);
  69. }
  70. }
  71. pagedList.Total = await _repository.CountAsync(query);
  72. query.ForPage(request.Page, request.PerPage);
  73. var list = await _repository.GetAllAsync(query.OrderByDesc(nameof(Article.IsTop), nameof(Article.Sort), nameof(Article.LastModifiedDate), nameof(Article.Id)));
  74. pagedList.Items = list.Select(article =>
  75. new ArticleResult
  76. {
  77. Id = article.Id,
  78. Title = article.Title,
  79. GroupId = article.GroupId,
  80. ImageUrls = StringUtils.StringCollectionToStringList(article.ImageUrls),
  81. Summary = article.Summary,
  82. Author = article.Author,
  83. Source = article.Source,
  84. ArticleBlocks = StringUtils.JsonDeserialize<IEnumerable<Block>>(article.Blocks),
  85. IsChecked = article.IsChecked,
  86. AddDate = article.AddDate,
  87. Sort = article.Sort,
  88. IsTop = article.IsTop,
  89. ReadCount = article.ReadCount,
  90. IsBlocks = article.IsBlocks
  91. }).ToList();
  92. foreach (var item in pagedList.Items)
  93. {
  94. //获取文章的评论数量
  95. var commentCount = await _commentRepository.CountAsync(Q.Where(nameof(Comment.ArticleId), item.Id).Where(nameof(Comment.TypeValue), 0).Where(nameof(Comment.Pid), 0));
  96. //获取文章的转发数量
  97. var retransmissionCunt = await _analyzeRepository.CountAsync(Q.Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 4)
  98. .Where(nameof(Entity.Analyze.Analyze.SourceId), item.Id));
  99. //获取文章的收藏数量
  100. var collectCount = await _analyzeRepository.CountAsync(Q.Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 3)
  101. .Where(nameof(Entity.Analyze.Analyze.SourceId), item.Id));
  102. //获取文章的点赞数量
  103. var praiseCount = await _analyzeRepository.CountAsync(Q.Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 1)
  104. .Where(nameof(Entity.Analyze.Analyze.SourceId), item.Id));
  105. item.CommentCount = commentCount;
  106. item.RetransmissionCount = retransmissionCunt;
  107. item.CollectCount = collectCount;
  108. item.PraiseCount = praiseCount;
  109. item.ReadCount = item.ReadCount;
  110. //是否转发
  111. item.IsRetransmission = await _analyzeRepository.CountAsync(
  112. Q.Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 4).Where(nameof(Entity.Analyze.Analyze.SourceId), item.Id)
  113. .Where(nameof(Entity.Analyze.Analyze.UserId), request.UserId)) > 0;
  114. //是否收藏
  115. item.IsCollect = await _analyzeRepository.CountAsync(
  116. Q.Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 3).Where(nameof(Entity.Analyze.Analyze.SourceId), item.Id)
  117. .Where(nameof(Entity.Analyze.Analyze.UserId), request.UserId)) > 0;
  118. //是否点赞
  119. item.IsPraise = await _analyzeRepository.CountAsync(
  120. Q.Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 1).Where(nameof(Entity.Analyze.Analyze.SourceId), item.Id)
  121. .Where(nameof(Entity.Analyze.Analyze.UserId), request.UserId)) > 0;
  122. }
  123. var pageListItems = pagedList.Items;
  124. foreach (var item in pageListItems)
  125. {
  126. var imageUrls = item.ImageUrls.ToList();
  127. for (int i = 0; i < imageUrls.Count(); i++)
  128. {
  129. imageUrls[i] = StringUtils.AddDomain(imageUrls[i]);
  130. }
  131. item.ImageUrls = imageUrls;
  132. foreach (var articleBlock in item.ArticleBlocks)
  133. {
  134. if (articleBlock.Data.File != null && !string.IsNullOrEmpty(articleBlock.Data.File.Url))
  135. articleBlock.Data.File.Url = StringUtils.AddDomain(articleBlock.Data.File.Url);
  136. }
  137. }
  138. return pagedList;
  139. }
  140. public async Task<Article> GetArticleAsync(int id)
  141. {
  142. var article = await _repository.GetAsync(id);
  143. if (article == null)
  144. {
  145. return null;
  146. }
  147. return article;
  148. }
  149. public async Task<Article> GetAsync(int id)
  150. {
  151. return await _repository.GetAsync(id);
  152. }
  153. public async Task<PagedList<ArticleResult>> GetListAsync(string keyword, PageParameter parameter)
  154. {
  155. var pagedList = new PagedList<ArticleResult>();
  156. var query = Q.NewQuery();
  157. if (!string.IsNullOrEmpty(keyword))
  158. {
  159. var like = $"%{keyword}%";
  160. query.WhereLike(nameof(Article.Title), like)
  161. .OrWhereLike(nameof(Article.Author), like)
  162. .OrWhereLike(nameof(Article.Source), like);
  163. }
  164. pagedList.Total = await _repository.CountAsync(query);
  165. query.ForPage(parameter.Page, parameter.PerPage);
  166. var list = await _repository.GetAllAsync(query.OrderByDesc(nameof(Article.IsTop), nameof(Article.Sort), nameof(Article.Id)));
  167. pagedList.Items = list.Select(article =>
  168. new ArticleResult
  169. {
  170. Id = article.Id,
  171. Title = article.Title,
  172. GroupId = article.GroupId,
  173. ImageUrls = StringUtils.StringCollectionToStringList(article.ImageUrls),
  174. Summary = article.Summary,
  175. Author = article.Author,
  176. Source = article.Source,
  177. ArticleBlocks = StringUtils.JsonDeserialize<IEnumerable<Block>>(article.Blocks),
  178. IsChecked = article.IsChecked,
  179. AddDate = article.AddDate,
  180. Sort = article.Sort,
  181. IsTop = article.IsTop,
  182. IsBlocks=article.IsBlocks
  183. });
  184. return pagedList;
  185. }
  186. public async Task<bool> DeleteAsync(int id)
  187. {
  188. return await _repository.DeleteAsync(id);
  189. }
  190. /// <summary>
  191. /// 克隆文章
  192. /// </summary>
  193. /// <param name="request"></param>
  194. /// <returns></returns>
  195. public async Task<bool> CloneArticleAsync(CloneArticleRequest request)
  196. {
  197. var article = await _repository.GetAsync(request.ArticleId);
  198. article.Title = request.ArticleTitle;
  199. return await _repository.InsertAsync(article) > 0;
  200. }
  201. public async Task<IEnumerable<Article>> GetAllAsync()
  202. {
  203. var query = Q.NewQuery();
  204. return await _repository.GetAllAsync(query.OrderByDesc(nameof(Article.IsTop), nameof(Article.Sort), nameof(Article.Id)));
  205. }
  206. public async Task<bool> UpdateAsync(Article article)
  207. {
  208. return await _repository.UpdateAsync(article);
  209. }
  210. public async Task<int?> MaxAsync(string sort)
  211. {
  212. return await _repository.MaxAsync(sort);
  213. }
  214. }
  215. }