ArticleRepository.cs 10 KB

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