ArticleService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Datory;
  5. using GxPress.Common.Tools;
  6. using GxPress.Entity;
  7. using GxPress.Repository.Interface;
  8. using GxPress.Repository.Interface.Collection;
  9. using GxPress.Result.Article;
  10. using GxPress.Service.Interface.Article;
  11. using GxPress.Service.Interface.Analyze;
  12. using GxPress.Request.Analyze;
  13. using GxPress.Service.Interface.Visit;
  14. using GxPress.Request.Article;
  15. using GxPress.Service.Interface.ArticleLabel;
  16. using GxPress.Entity.Article;
  17. namespace GxPress.Service.Implement.Article
  18. {
  19. /// <summary>
  20. /// 文章
  21. /// </summary>
  22. public class ArticleService : IArticleService
  23. {
  24. private readonly IUserRepository _userRepository;
  25. private readonly ICommentRepository _commentRepository;
  26. private readonly IArticleRepository _articleRepository;
  27. private readonly IArticleGroupRepository _articleGroupRepository;
  28. private readonly IMiddleRepository _middleRepository;
  29. private readonly ICollectionRepository _collectionRepository;
  30. private readonly IAnalyzeService _analyzeService;
  31. private readonly IVisitService _visitService;
  32. private readonly IArticleLabelService _articleLabelService;
  33. public ArticleService(IUserRepository userRepository,
  34. ICommentRepository commentRepository, IArticleRepository articleRepository, IArticleGroupRepository articleGroupRepository, ICollectionRepository collectionRepository, IMiddleRepository middleRepository, IAnalyzeService analyzeService, IVisitService visitService, IArticleLabelService articleLabelService)
  35. {
  36. _userRepository = userRepository;
  37. _commentRepository = commentRepository;
  38. _articleRepository = articleRepository;
  39. _articleGroupRepository = articleGroupRepository;
  40. _middleRepository = middleRepository;
  41. _collectionRepository = collectionRepository;
  42. _analyzeService = analyzeService;
  43. _visitService = visitService;
  44. _articleLabelService = articleLabelService;
  45. }
  46. /// <summary>
  47. /// 获取文章详情
  48. /// </summary>
  49. /// <param name="id"></param>
  50. /// <param name="userId"></param>
  51. /// <returns></returns>
  52. public async Task<ArticleResult> GetAsync(int id, int userId)
  53. {
  54. var article = await _articleRepository.GetAsync(id);
  55. if (article == null)
  56. return new ArticleResult();
  57. //增加阅读量
  58. //访问
  59. if (userId > 0)
  60. await _visitService.AddVisit(userId, 1, id);
  61. //获取文章类型名称
  62. var articleGroup = await _articleGroupRepository.GetAsync(article.GroupId);
  63. var groupName = articleGroup == null ? "" : articleGroup.Name;
  64. //获取文章的点赞数据
  65. var articleResult = new ArticleResult
  66. {
  67. Id = article.Id,
  68. Title = article.Title,
  69. GroupId = article.GroupId,
  70. ImageUrls = StringUtils.StringCollectionToStringList(article.ImageUrls),
  71. Summary = article.Summary,
  72. Author = article.Author,
  73. Source = article.Source,
  74. ArticleBlocks = StringUtils.JsonDeserialize<IEnumerable<Block>>(article.Blocks),
  75. ArticleContent = StringUtils.JsonDeserialize<ArticleContent>(article.ContentJson),
  76. IsChecked = article.IsChecked,
  77. AddDate = article.AddDate,
  78. Sort = article.Sort,
  79. GroupName = groupName,
  80. ReadCount = await _visitService.GetCountAsync(1, article.Id),
  81. IsTop = article.IsTop,
  82. CreatedDate = article.CreatedDate,
  83. IsBlocks = article.IsBlocks
  84. };
  85. //标签
  86. articleResult.ArticleInnerLabelsResult = await _articleLabelService.GetArticleLabelsAsync(id);
  87. //文章图片
  88. foreach (var block in articleResult.ArticleBlocks)
  89. {
  90. if (block.Type == "image")
  91. block.Data.File.Url = StringUtils.AddDomain(block.Data.File.Url);
  92. }
  93. //新版json
  94. if (articleResult.ArticleContent != null && articleResult.ArticleContent.type.Count() > 0)
  95. {
  96. foreach (var content in articleResult.ArticleContent.content)
  97. {
  98. foreach (var contentItem in content.content)
  99. {
  100. if (contentItem.type == "newImg")
  101. contentItem.attrs.src = StringUtils.AddDomain(contentItem.attrs.src);
  102. }
  103. }
  104. }
  105. var imageUrls = articleResult.ImageUrls.ToList();
  106. for (int i = 0; i < imageUrls.Count(); i++)
  107. imageUrls[i] = StringUtils.AddDomain(imageUrls[i]);
  108. articleResult.ImageUrls = imageUrls;
  109. var analyzeRequest = new Request.App.Analyze.AnalyzeRequest();
  110. analyzeRequest.TypeValue = 0;
  111. analyzeRequest.AnalyzeType = 1;
  112. analyzeRequest.SourceId = id;
  113. analyzeRequest.UserId = userId;
  114. //点赞数量
  115. articleResult.PraiseCount = await _analyzeService.CountAsync(analyzeRequest);
  116. //获取话题的评论数量
  117. var commentCount =
  118. await _commentRepository.CountAsync(Q.Where(nameof(Entity.Comment.ArticleId), id).Where(nameof(Entity.Comment.TypeValue), 0).Where(nameof(Entity.Comment.Pid), 0));
  119. articleResult.CommentCount = commentCount;
  120. //获取话题的转发数量
  121. analyzeRequest.AnalyzeType = 4;
  122. var retransmissionCount = await _analyzeService.CountAsync(analyzeRequest);
  123. articleResult.RetransmissionCount = retransmissionCount;
  124. articleResult.IsRetransmission = await _analyzeService.ExistsAsync(analyzeRequest);
  125. //获取话题的收藏数量
  126. analyzeRequest.AnalyzeType = 3;
  127. var collectCount = await _analyzeService.CountAsync(analyzeRequest);
  128. articleResult.CollectCount = collectCount;
  129. //是否收藏
  130. articleResult.IsCollect = await _analyzeService.ExistsAsync(analyzeRequest);
  131. //获取话题的点赞数量
  132. analyzeRequest.AnalyzeType = 1;
  133. var praiseCount = await _analyzeService.CountAsync(analyzeRequest);
  134. articleResult.PraiseCount = praiseCount;
  135. //是否点赞
  136. articleResult.IsPraise = await _analyzeService.ExistsAsync(analyzeRequest);
  137. var request = new PraisePageSearchRequest { SourceId = id, TypeValue = 0, Page = 1, PerPage = 3 };
  138. articleResult.ArticlePraisePagedList = await _analyzeService.GetPraisePageAsync(request);
  139. return articleResult;
  140. }
  141. /// <summary>
  142. /// 添加
  143. /// </summary>
  144. /// <param name="request"></param>
  145. /// <returns></returns>
  146. public async Task<ArticleResult> AddAsync(ArticleAddRequest request)
  147. {
  148. //文章图片
  149. var resultImageUrls = request.ImageUrls.ToList();
  150. for (int i = 0; i < resultImageUrls.Count; i++)
  151. resultImageUrls[i] = StringUtils.RemoveDomain(resultImageUrls[i]);
  152. request.ImageUrls = resultImageUrls;
  153. var isBlocks = false;
  154. //文章内容图片
  155. foreach (var block in request.Blocks)
  156. {
  157. if (block.Type == "image")
  158. {
  159. block.Data.File.Url = StringUtils.RemoveDomain(block.Data.File.Url);
  160. }
  161. isBlocks = true;
  162. }
  163. //新版json
  164. if (request.ArticleContent != null && request.ArticleContent.type.Count() > 0)
  165. {
  166. foreach (var content in request.ArticleContent.content)
  167. {
  168. foreach (var contentItem in content.content)
  169. {
  170. if (contentItem.type == "newImg")
  171. contentItem.attrs.src = StringUtils.RemoveDomain(contentItem.attrs.src);
  172. }
  173. }
  174. }
  175. var sort = request.Sort == 0 ? await _articleRepository.MaxAsync(nameof(Entity.Article.Article.Sort)) : request.Sort;
  176. var article = new Entity.Article.Article
  177. {
  178. Title = request.Title,
  179. GroupId = request.GroupId,
  180. ImageUrls = StringUtils.ObjectCollectionToString(request.ImageUrls),
  181. Summary = request.Summary,
  182. Author = request.Author,
  183. Source = request.Source,
  184. Blocks = StringUtils.JsonSerialize(request.Blocks),
  185. ContentJson = StringUtils.JsonSerialize(request.ArticleContent),
  186. IsChecked = request.IsChecked,
  187. AddDate = request.AddDate,
  188. Sort = sort ?? 1,
  189. IsTop = request.IsTop,
  190. IsBlocks = isBlocks
  191. };
  192. article.Id = await _articleRepository.InsertAsync(article);
  193. //添加文章标签
  194. await _articleLabelService.AddArticleInnerLabel(article.Id, request.ArticleLabelIds);
  195. var articleResult = 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. ArticleContent = StringUtils.JsonDeserialize<ArticleContent>(article.ContentJson),
  206. IsChecked = article.IsChecked,
  207. AddDate = article.AddDate,
  208. Sort = article.Sort,
  209. IsTop = article.IsTop
  210. };
  211. //文章图片
  212. resultImageUrls = articleResult.ImageUrls.ToList();
  213. for (int i = 0; i < resultImageUrls.Count; i++)
  214. {
  215. resultImageUrls[i] = StringUtils.AddDomain(resultImageUrls[i]);
  216. }
  217. articleResult.ImageUrls = resultImageUrls;
  218. //文章内容图片
  219. foreach (var block in articleResult.ArticleBlocks)
  220. {
  221. if (block.Type == "image")
  222. {
  223. block.Data.File.Url = StringUtils.AddDomain(block.Data.File.Url);
  224. }
  225. }
  226. //新版json
  227. if (articleResult.ArticleContent != null && articleResult.ArticleContent.type.Count() > 0)
  228. {
  229. foreach (var content in request.ArticleContent.content)
  230. {
  231. foreach (var contentItem in content.content)
  232. {
  233. if (contentItem.type == "newImg")
  234. contentItem.attrs.src = StringUtils.AddDomain(contentItem.attrs.src);
  235. }
  236. }
  237. }
  238. return articleResult;
  239. }
  240. public async Task<ArticleResult> EditAsync(ArticleEditRequest request)
  241. {
  242. //解析文章类容 图片地址
  243. foreach (var block in request.Blocks)
  244. {
  245. if (block.Type == "image")
  246. {
  247. block.Data.File.Url = StringUtils.RemoveDomain(block.Data.File.Url);
  248. }
  249. }
  250. //新版json
  251. if (request.ArticleContent != null && request.ArticleContent.type.Count() > 0)
  252. {
  253. foreach (var content in request.ArticleContent.content)
  254. {
  255. foreach (var contentItem in content.content)
  256. {
  257. if (contentItem.type == "newImg")
  258. contentItem.attrs.src = StringUtils.RemoveDomain(contentItem.attrs.src);
  259. }
  260. }
  261. }
  262. //
  263. var imageUrls = request.ImageUrls.ToList();
  264. for (int i = 0; i < imageUrls.Count; i++)
  265. {
  266. imageUrls[i] = StringUtils.RemoveDomain(imageUrls[i]);
  267. }
  268. request.ImageUrls = imageUrls;
  269. var article = new Entity.Article.Article
  270. {
  271. Id = request.Id,
  272. Title = request.Title,
  273. GroupId = request.GroupId,
  274. ImageUrls = StringUtils.ObjectCollectionToString(request.ImageUrls),
  275. Summary = request.Summary,
  276. Author = request.Author,
  277. Source = request.Source,
  278. Blocks = StringUtils.JsonSerialize(request.Blocks),
  279. ContentJson = StringUtils.JsonSerialize(request.ArticleContent),
  280. IsChecked = request.IsChecked,
  281. AddDate = request.AddDate,
  282. Sort = request.Sort,
  283. IsTop = request.IsTop
  284. };
  285. await _articleRepository.UpdateAsync(article);
  286. //添加文章标签
  287. await _articleLabelService.AddArticleInnerLabel(article.Id, request.ArticleLabelIds);
  288. var articleResult = new ArticleResult
  289. {
  290. Id = article.Id,
  291. Title = article.Title,
  292. GroupId = article.GroupId,
  293. ImageUrls = StringUtils.StringCollectionToStringList(article.ImageUrls),
  294. Summary = article.Summary,
  295. Author = article.Author,
  296. Source = article.Source,
  297. ArticleBlocks = StringUtils.JsonDeserialize<IEnumerable<Block>>(article.Blocks),
  298. IsChecked = article.IsChecked,
  299. AddDate = article.AddDate,
  300. Sort = article.Sort,
  301. IsTop = request.IsTop,
  302. ArticleContent=StringUtils.JsonDeserialize<ArticleContent>(article.ContentJson)
  303. };
  304. //文章图片
  305. var resultImageUrls = articleResult.ImageUrls.ToList();
  306. for (int i = 0; i < resultImageUrls.Count; i++)
  307. {
  308. resultImageUrls[i] = StringUtils.AddDomain(resultImageUrls[i]);
  309. }
  310. articleResult.ImageUrls = resultImageUrls;
  311. //文章内容图片
  312. foreach (var block in articleResult.ArticleBlocks)
  313. {
  314. if (block.Type == "image")
  315. {
  316. block.Data.File.Url = StringUtils.AddDomain(block.Data.File.Url);
  317. }
  318. }
  319. //新版json
  320. if (articleResult.ArticleContent != null && articleResult.ArticleContent.type.Count() > 0)
  321. {
  322. foreach (var content in request.ArticleContent.content)
  323. {
  324. foreach (var contentItem in content.content)
  325. {
  326. if (contentItem.type == "newImg")
  327. contentItem.attrs.src = StringUtils.AddDomain(contentItem.attrs.src);
  328. }
  329. }
  330. }
  331. return articleResult;
  332. }
  333. }
  334. }