CommentRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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.Exceptions;
  7. using GxPress.Common.Page;
  8. using GxPress.Common.Tools;
  9. using GxPress.Entity;
  10. using GxPress.Entity.Article;
  11. using GxPress.Repository.Interface;
  12. using GxPress.Request.Comment;
  13. using GxPress.Result.Comment;
  14. using Microsoft.Extensions.Options;
  15. using Datory;
  16. using SqlKata;
  17. namespace GxPress.Repository.Implement
  18. {
  19. public class CommentRepository : ICommentRepository
  20. {
  21. private readonly Repository<Comment> _repository;
  22. private readonly Repository<Entity.Analyze.Analyze> _analyzeRepository;
  23. private readonly Repository<Article> _articleRepository;
  24. private readonly Repository<User> _userRepository;
  25. private readonly IMapper _mapper;
  26. public CommentRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
  27. {
  28. var databaseType = StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
  29. var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
  30. _repository = new Repository<Comment>(database);
  31. _articleRepository = new Repository<Article>(database);
  32. _userRepository = new Repository<User>(database);
  33. _analyzeRepository = new Repository<Entity.Analyze.Analyze>(database);
  34. _mapper = mapper;
  35. }
  36. public IDatabase Database => _repository.Database;
  37. public string TableName => _repository.TableName;
  38. public List<TableColumn> TableColumns => _repository.TableColumns;
  39. /// <summary>
  40. /// 添加评论
  41. /// </summary>
  42. /// <param name="request"></param>
  43. /// <returns></returns>
  44. public async Task<int> CommentInAsync(CommentInRequest request)
  45. {
  46. //查询文章是否存在
  47. // var article = _articleRepository.ExistsAsync(Q.Where(nameof(Article.Id), request.ArticleId));
  48. // if (await article == false)
  49. // throw new BusinessException("该文章不存在");
  50. //查询用户
  51. var user = _userRepository.ExistsAsync(Q.Where(nameof(User.Id), request.UserId));
  52. if (await user == false)
  53. throw new BusinessException("该用户不存在");
  54. var comment = new Comment
  55. {
  56. ArticleId = request.ArticleId,
  57. Content = request.Content,
  58. UserId = request.UserId,
  59. Pid = request.Pid,
  60. TypeValue = request.TypeValue,
  61. Score = request.Score,
  62. JsonContent = request.JsonContent,
  63. HtmlContent = request.HtmlContent
  64. };
  65. //是否被回复
  66. if (request.Pid > 0)
  67. {
  68. //查询他的上级pid=0
  69. var commentEntity = await _repository.GetAsync(request.Pid);
  70. if (commentEntity.Pid > 0)
  71. {
  72. comment.IsUnderstand = true;
  73. }
  74. }
  75. return await _repository.InsertAsync(comment);
  76. }
  77. /// <summary>
  78. /// 分页列表
  79. /// </summary>
  80. /// <param name="request"></param>
  81. /// <returns></returns>
  82. public async Task<PagedList<CommentPageResult>> GetPagedList(CommentSearchPageRequest request)
  83. {
  84. var pagedList = new PagedList<CommentPageResult>
  85. {
  86. Total = await GetCountAsync(request)
  87. };
  88. request.Total = pagedList.Total;
  89. var list = await GetPageListAsync(request);
  90. pagedList.Items = list;
  91. return pagedList;
  92. }
  93. /// <summary>
  94. /// 后台评论分页列表
  95. /// </summary>
  96. /// <param name="request"></param>
  97. /// <returns></returns>
  98. public async Task<PagedList<CommentResult>> GetAllAsync(CommentSearchRequest request)
  99. {
  100. var result = new PagedList<CommentResult>();
  101. var query = Q.NewQuery();
  102. if (!string.IsNullOrEmpty(request.KeyWord))
  103. {
  104. query.Where(n => n.OrWhereLike(nameof(Comment.Content), $"%{request.KeyWord}%").
  105. OrWhereLike(nameof(Comment.Title), $"%{request.KeyWord}%"));
  106. }
  107. result.Total = await _repository.CountAsync(query);
  108. var commentResult = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  109. result.Items = commentResult.Select(n => _mapper.Map<CommentResult>(n));
  110. return result;
  111. }
  112. /// <summary>
  113. /// 获取总条数
  114. /// </summary>
  115. /// <param name="articleId"></param>
  116. /// <returns></returns>
  117. public async Task<int> GetCountAsync(CommentSearchPageRequest request)
  118. {
  119. return await _repository.CountAsync(Q.Where(nameof(Comment.ArticleId), request.ArticleId)
  120. .Where(nameof(Comment.Pid), 0).Where(nameof(Comment.TypeValue), request.TypeValue));
  121. }
  122. /// <summary>
  123. /// 获取分页数据
  124. /// </summary>
  125. /// <param name="request"></param>
  126. /// <returns></returns>
  127. public async Task<IEnumerable<CommentPageResult>> GetPageListAsync(CommentSearchPageRequest request)
  128. {
  129. List<CommentPageResult> commentPageResults = new List<CommentPageResult>();
  130. var query = new SqlKata.Query();
  131. query.Where(nameof(Comment.ArticleId), request.ArticleId)
  132. .Where(nameof(Comment.Pid), 0).Where(nameof(Comment.TypeValue), request.TypeValue);
  133. if (string.IsNullOrEmpty(request.Sort))
  134. query.OrderByDesc(nameof(Comment.CreatedDate));
  135. if (!string.IsNullOrEmpty(request.Sort) && request.Sort == "asc")
  136. query.OrderBy(nameof(Comment.CreatedDate));
  137. if (!string.IsNullOrEmpty(request.Sort) && request.Sort == "desc")
  138. query.OrderByDesc(nameof(Comment.CreatedDate));
  139. var commentList = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  140. var i = 1;
  141. foreach (var item in commentList)
  142. {
  143. //获取当前用户信息
  144. var user = await _userRepository.GetAsync(item.UserId);
  145. if (user == null)
  146. continue;
  147. CommentPageResult commentPageResult = new CommentPageResult
  148. {
  149. ArticleId = item.ArticleId,
  150. AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl),
  151. Name = user.Name,
  152. Content = item.Content,
  153. CreatedTime = item.CreatedDate,
  154. LaudCount = await LaudCommentAsync(item.Id),
  155. UserId = item.UserId,
  156. JsonContent = item.JsonContent,
  157. HtmlContent = item.HtmlContent,
  158. //是否点赞
  159. IsLaud = await IsLaudCommentAsync(item.Id, item.UserId),
  160. Score = item.Score
  161. };
  162. if (request.Sort == "asc")
  163. commentPageResult.FloorCount = $"第{((request.Page - 1) * request.PerPage) + i}楼";
  164. else
  165. commentPageResult.FloorCount = $"第{((request.Total - (request.Page - 1) * request.PerPage) - (i - 1))}楼";
  166. //计算点赞数量
  167. //计算被回复数据
  168. List<CommentReplyResult> commentReplyResults = new List<CommentReplyResult>();
  169. commentPageResult.CommentReplyResults = await GetCommentReplyResultByPid(item.Id, commentReplyResults);
  170. commentPageResult.Id = item.Id;
  171. commentPageResults.Add(commentPageResult);
  172. i++;
  173. }
  174. return commentPageResults;
  175. }
  176. /// <summary>
  177. /// 当前用户是否点赞该评论
  178. /// </summary>
  179. /// <param name="commentId"></param>
  180. /// <param name="userId"></param>
  181. /// <returns></returns>
  182. public async Task<bool> IsLaudCommentAsync(int commentId, int userId)
  183. {
  184. return await _analyzeRepository.CountAsync(Q.Where(nameof(Entity.Analyze.Analyze.CommentId), commentId)
  185. .Where(nameof(Entity.Analyze.Analyze.UserId), userId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 2)) > 0;
  186. }
  187. /// <summary>
  188. /// 当前用户是否点赞该评论
  189. /// </summary>
  190. /// <param name="commentId"></param>
  191. /// <returns></returns>
  192. public async Task<int> LaudCommentAsync(int commentId)
  193. {
  194. return await _analyzeRepository.CountAsync(Q.Where(nameof(Entity.Analyze.Analyze.CommentId), commentId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 2));
  195. }
  196. /// <summary>
  197. /// 递归查询父级下面的所有回复
  198. /// </summary>
  199. public async Task<List<CommentReplyResult>> GetCommentReplyResultByPid(int pid,
  200. List<CommentReplyResult> commentReplyResults)
  201. {
  202. var result = _repository.GetAllAsync(Q.Where(nameof(Comment.Pid), pid)).Result.ToList();
  203. if (result.Count == 0)
  204. return commentReplyResults;
  205. foreach (var item in result)
  206. {
  207. CommentReplyResult commentReplyResult = new CommentReplyResult
  208. {
  209. Id = item.Id,
  210. Content = item.Content,
  211. CreatedTime = item.CreatedDate,
  212. JsonContent = item.JsonContent,
  213. HtmlContent = item.HtmlContent
  214. };
  215. var user = await _userRepository.GetAsync(item.UserId);
  216. if (user == null)
  217. continue;
  218. commentReplyResult.Name = user.Name;
  219. if (item.IsUnderstand)
  220. {
  221. var comment = await _repository.GetAsync(Q.Where(nameof(Comment.Id), item.Pid));
  222. if (comment == null)
  223. continue;
  224. user = _userRepository.GetAsync(comment.UserId).Result;
  225. commentReplyResult.ReplyName = user.Name;
  226. commentReplyResult.ReplyUserId = user.Id;
  227. }
  228. commentReplyResult.IsUnderstand = item.IsUnderstand;
  229. commentReplyResult.UserId = item.UserId;
  230. commentReplyResults.Add(commentReplyResult);
  231. commentReplyResults = await GetCommentReplyResultByPid(item.Id, commentReplyResults);
  232. }
  233. return commentReplyResults;
  234. }
  235. /// <summary>
  236. /// 删除评论
  237. /// </summary>
  238. /// <param name="request"></param>
  239. /// <returns></returns>
  240. public async Task<bool> DeleteCommentAsync(CommentDeleteRequest request)
  241. {
  242. //查询评论数据
  243. var commentEntity = await _repository.GetAsync(Q.Where(nameof(Comment.ArticleId), request.ArticleId)
  244. .Where(nameof(Comment.Id), request.CommentId));
  245. if (commentEntity.Id == 0)
  246. throw new BusinessException("该评论不存在!");
  247. //递归删除
  248. var commentList = new List<Comment> { commentEntity };
  249. commentList = await GetCommentByParentId(commentEntity.Id, commentList);
  250. //删除
  251. foreach (var item in commentList)
  252. {
  253. await _repository.DeleteAsync(item.Id);
  254. }
  255. return true;
  256. }
  257. /// <summary>
  258. /// 递归删除
  259. /// </summary>
  260. /// <param name="id"></param>
  261. /// <param name="lists"></param>
  262. /// <returns></returns>
  263. public async Task<List<Comment>> GetCommentByParentId(int id, List<Comment> lists)
  264. {
  265. var commentList = await _repository.GetAllAsync(Q.Where(nameof(Comment.Pid), id));
  266. if (!commentList.Any())
  267. return lists;
  268. foreach (var comment in commentList)
  269. {
  270. lists.Add(comment);
  271. await GetCommentByParentId(comment.Id, lists);
  272. }
  273. return lists;
  274. }
  275. /// <summary>
  276. /// 获取评论数量
  277. /// </summary>
  278. /// <param name="articleId"></param>
  279. /// <returns></returns>
  280. public async Task<int> GetCommentCountAsync(int articleId)
  281. {
  282. return await _repository.CountAsync(Q.Where(nameof(Comment.ArticleId), articleId));
  283. }
  284. public async Task<Comment> GetAsync(Query query)
  285. {
  286. return await _repository.GetAsync(query);
  287. }
  288. public async Task<bool> UpdateAsync(Comment comment)
  289. {
  290. return await _repository.UpdateAsync(comment);
  291. }
  292. public async Task<int> CountAsync(Query query)
  293. {
  294. return await _repository.CountAsync(query);
  295. }
  296. /// <summary>
  297. /// 修改评论
  298. /// </summary>
  299. /// <param name="request"></param>
  300. /// <returns></returns>
  301. public async Task<bool> UpdateCommentAsync(CommentUpdateRequest request)
  302. {
  303. //判断类容是否为空
  304. if (string.IsNullOrWhiteSpace(request.Content) && string.IsNullOrWhiteSpace(request.JsonContent) && string.IsNullOrWhiteSpace(request.HtmlContent))
  305. throw new Common.Exceptions.BusinessException("评论不能为空!");
  306. //查询评论
  307. var comment = await _repository.GetAsync(request.CommentId);
  308. if (comment == null)
  309. throw new Common.Exceptions.BusinessException("该评论不存在!");
  310. if (comment.UserId != request.UserId)
  311. throw new Common.Exceptions.BusinessException("该评论不属于该用户!");
  312. //修改
  313. if (!string.IsNullOrWhiteSpace(request.Content))
  314. comment.Content = request.Content;
  315. if (!string.IsNullOrWhiteSpace(request.HtmlContent))
  316. comment.HtmlContent = request.HtmlContent;
  317. if (!string.IsNullOrWhiteSpace(request.JsonContent))
  318. comment.JsonContent = request.JsonContent;
  319. comment.Content = request.Content;
  320. if (request.Score > 0)
  321. comment.Score = request.Score;
  322. return await UpdateAsync(comment);
  323. }
  324. }
  325. }