CommentRepository.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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.Repository.Interface;
  11. using GxPress.Request.Comment;
  12. using GxPress.Result.Comment;
  13. using Microsoft.Extensions.Options;
  14. using Datory;
  15. using SqlKata;
  16. namespace GxPress.Repository.Implement
  17. {
  18. public class CommentRepository : ICommentRepository
  19. {
  20. private readonly Repository<Comment> _repository;
  21. private readonly Repository<Entity.Analyze.Analyze> _analyzeRepository;
  22. private readonly Repository<Article> _articleRepository;
  23. private readonly Repository<User> _userRepository;
  24. private readonly IMapper _mapper;
  25. public CommentRepository(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<Comment>(database);
  30. _articleRepository = new Repository<Article>(database);
  31. _userRepository = new Repository<User>(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. /// <summary>
  39. /// 添加评论
  40. /// </summary>
  41. /// <param name="request"></param>
  42. /// <returns></returns>
  43. public async Task<int> CommentInAsync(CommentInRequest request)
  44. {
  45. //查询文章是否存在
  46. // var article = _articleRepository.ExistsAsync(Q.Where(nameof(Article.Id), request.ArticleId));
  47. // if (await article == false)
  48. // throw new BusinessException("该文章不存在");
  49. //查询用户
  50. var user = _userRepository.ExistsAsync(Q.Where(nameof(User.Id), request.UserId));
  51. if (await user == false)
  52. throw new BusinessException("该用户不存在");
  53. var comment = new Comment
  54. {
  55. ArticleId = request.ArticleId,
  56. Content = request.Content,
  57. UserId = request.UserId,
  58. Pid = request.Pid,
  59. TypeValue = request.TypeValue
  60. };
  61. //是否被回复
  62. if (request.Pid > 0)
  63. {
  64. //查询他的上级pid=0
  65. var commentEntity = await _repository.GetAsync(request.Pid);
  66. if (commentEntity.Pid > 0)
  67. {
  68. comment.IsUnderstand = true;
  69. }
  70. }
  71. return await _repository.InsertAsync(comment);
  72. }
  73. /// <summary>
  74. /// 分页列表
  75. /// </summary>
  76. /// <param name="request"></param>
  77. /// <returns></returns>
  78. public async Task<PagedList<CommentPageResult>> GetPagedList(CommentSearchPageRequest request)
  79. {
  80. var pagedList = new PagedList<CommentPageResult>
  81. {
  82. Total = await GetCountAsync(request)
  83. };
  84. request.Total = pagedList.Total;
  85. var list = await GetPageListAsync(request);
  86. pagedList.Items = list;
  87. return pagedList;
  88. }
  89. /// <summary>
  90. /// 获取总条数
  91. /// </summary>
  92. /// <param name="articleId"></param>
  93. /// <returns></returns>
  94. public async Task<int> GetCountAsync(CommentSearchPageRequest request)
  95. {
  96. return await _repository.CountAsync(Q.Where(nameof(Comment.ArticleId), request.ArticleId)
  97. .Where(nameof(Comment.Pid), 0).Where(nameof(Comment.TypeValue), request.TypeValue));
  98. }
  99. /// <summary>
  100. /// 获取分页数据
  101. /// </summary>
  102. /// <param name="request"></param>
  103. /// <returns></returns>
  104. public async Task<IEnumerable<CommentPageResult>> GetPageListAsync(CommentSearchPageRequest request)
  105. {
  106. List<CommentPageResult> commentPageResults = new List<CommentPageResult>();
  107. var query = new SqlKata.Query();
  108. query.Where(nameof(Comment.ArticleId), request.ArticleId)
  109. .Where(nameof(Comment.Pid), 0).Where(nameof(Comment.TypeValue), request.TypeValue);
  110. if (string.IsNullOrEmpty(request.Sort))
  111. query.OrderByDesc(nameof(Comment.CreatedDate));
  112. if (!string.IsNullOrEmpty(request.Sort) && request.Sort == "asc")
  113. query.OrderBy(nameof(Comment.CreatedDate));
  114. if (!string.IsNullOrEmpty(request.Sort) && request.Sort == "desc")
  115. query.OrderByDesc(nameof(Comment.CreatedDate));
  116. var commentList = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  117. var i = 1;
  118. foreach (var item in commentList)
  119. {
  120. //获取当前用户信息
  121. var user = await _userRepository.GetAsync(item.UserId);
  122. if (user == null)
  123. continue;
  124. CommentPageResult commentPageResult = new CommentPageResult
  125. {
  126. ArticleId = item.ArticleId,
  127. AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl),
  128. Name = user.Name,
  129. Content = item.Content,
  130. CreatedTime = item.CreatedDate,
  131. LaudCount =await LaudCommentAsync(item.Id),
  132. UserId = item.UserId,
  133. //是否点赞
  134. IsLaud = await IsLaudCommentAsync(item.Id, request.UserId)
  135. };
  136. if (request.Sort == "asc")
  137. commentPageResult.FloorCount = $"第{((request.Page - 1) * request.PerPage) + i}楼";
  138. else
  139. commentPageResult.FloorCount = $"第{((request.Total - (request.Page - 1) * request.PerPage) - (i - 1))}楼";
  140. //计算点赞数量
  141. //计算被回复数据
  142. List<CommentReplyResult> commentReplyResults = new List<CommentReplyResult>();
  143. commentPageResult.CommentReplyResults = await GetCommentReplyResultByPid(item.Id, commentReplyResults);
  144. commentPageResult.Id = item.Id;
  145. commentPageResults.Add(commentPageResult);
  146. i++;
  147. }
  148. return commentPageResults;
  149. }
  150. /// <summary>
  151. /// 当前用户是否点赞该评论
  152. /// </summary>
  153. /// <param name="commentId"></param>
  154. /// <param name="userId"></param>
  155. /// <returns></returns>
  156. public async Task<bool> IsLaudCommentAsync(int commentId, int userId)
  157. {
  158. return await _analyzeRepository.CountAsync(Q.Where(nameof(Entity.Analyze.Analyze.CommentId), commentId)
  159. .Where(nameof(Entity.Analyze.Analyze.UserId), userId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 2)) > 0;
  160. }
  161. /// <summary>
  162. /// 当前用户是否点赞该评论
  163. /// </summary>
  164. /// <param name="commentId"></param>
  165. /// <returns></returns>
  166. public async Task<int> LaudCommentAsync(int commentId)
  167. {
  168. return await _analyzeRepository.CountAsync(Q.Where(nameof(Entity.Analyze.Analyze.CommentId), commentId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 2));
  169. }
  170. /// <summary>
  171. /// 递归查询父级下面的所有回复
  172. /// </summary>
  173. public async Task<List<CommentReplyResult>> GetCommentReplyResultByPid(int pid,
  174. List<CommentReplyResult> commentReplyResults)
  175. {
  176. var result = _repository.GetAllAsync(Q.Where(nameof(Comment.Pid), pid)).Result.ToList();
  177. if (result.Count == 0)
  178. return commentReplyResults;
  179. foreach (var item in result)
  180. {
  181. CommentReplyResult commentReplyResult = new CommentReplyResult
  182. {
  183. Id = item.Id,
  184. Content = item.Content,
  185. CreatedTime = item.CreatedDate
  186. };
  187. var user = await _userRepository.GetAsync(item.UserId);
  188. if (user == null)
  189. continue;
  190. commentReplyResult.Name = user.Name;
  191. if (item.IsUnderstand)
  192. {
  193. var comment = await _repository.GetAsync(Q.Where(nameof(Comment.Id), item.Pid));
  194. if (comment == null)
  195. continue;
  196. user = _userRepository.GetAsync(comment.UserId).Result;
  197. commentReplyResult.ReplyName = user.Name;
  198. commentReplyResult.ReplyUserId = user.Id;
  199. }
  200. commentReplyResult.IsUnderstand = item.IsUnderstand;
  201. commentReplyResult.UserId = item.UserId;
  202. commentReplyResults.Add(commentReplyResult);
  203. commentReplyResults = await GetCommentReplyResultByPid(item.Id, commentReplyResults);
  204. }
  205. return commentReplyResults;
  206. }
  207. /// <summary>
  208. /// 删除评论
  209. /// </summary>
  210. /// <param name="request"></param>
  211. /// <returns></returns>
  212. public async Task<bool> DeleteCommentAsync(CommentDeleteRequest request)
  213. {
  214. //查询评论数据
  215. var commentEntity = await _repository.GetAsync(Q.Where(nameof(Comment.ArticleId), request.ArticleId)
  216. .Where(nameof(Comment.Id), request.CommentId));
  217. if (commentEntity.Id == 0)
  218. throw new BusinessException("该评论不存在!");
  219. //递归删除
  220. var commentList = new List<Comment> { commentEntity };
  221. commentList = await GetCommentByParentId(commentEntity.Id, commentList);
  222. //删除
  223. foreach (var item in commentList)
  224. {
  225. await _repository.DeleteAsync(item.Id);
  226. }
  227. return true;
  228. }
  229. /// <summary>
  230. /// 递归删除
  231. /// </summary>
  232. /// <param name="id"></param>
  233. /// <param name="lists"></param>
  234. /// <returns></returns>
  235. public async Task<List<Comment>> GetCommentByParentId(int id, List<Comment> lists)
  236. {
  237. var commentList = await _repository.GetAllAsync(Q.Where(nameof(Comment.Pid), id));
  238. if (!commentList.Any())
  239. return lists;
  240. foreach (var comment in commentList)
  241. {
  242. lists.Add(comment);
  243. await GetCommentByParentId(comment.Id, lists);
  244. }
  245. return lists;
  246. }
  247. /// <summary>
  248. /// 获取评论数量
  249. /// </summary>
  250. /// <param name="articleId"></param>
  251. /// <returns></returns>
  252. public async Task<int> GetCommentCountAsync(int articleId)
  253. {
  254. return await _repository.CountAsync(Q.Where(nameof(Comment.ArticleId), articleId));
  255. }
  256. public async Task<Comment> GetAsync(Query query)
  257. {
  258. return await _repository.GetAsync(query);
  259. }
  260. public async Task<bool> UpdateAsync(Comment comment)
  261. {
  262. return await _repository.UpdateAsync(comment);
  263. }
  264. public async Task<int> CountAsync(Query query)
  265. {
  266. return await _repository.CountAsync(query);
  267. }
  268. }
  269. }