ArticleRepository.cs 12 KB

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