CommentRepository.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. };
  63. //是否被回复
  64. if (request.Pid > 0)
  65. {
  66. //查询他的上级pid=0
  67. var commentEntity = await _repository.GetAsync(request.Pid);
  68. if (commentEntity.Pid > 0)
  69. {
  70. comment.IsUnderstand = true;
  71. }
  72. }
  73. return await _repository.InsertAsync(comment);
  74. }
  75. /// <summary>
  76. /// 分页列表
  77. /// </summary>
  78. /// <param name="request"></param>
  79. /// <returns></returns>
  80. public async Task<PagedList<CommentPageResult>> GetPagedList(CommentSearchPageRequest request)
  81. {
  82. var pagedList = new PagedList<CommentPageResult>
  83. {
  84. Total = await GetCountAsync(request)
  85. };
  86. request.Total = pagedList.Total;
  87. var list = await GetPageListAsync(request);
  88. pagedList.Items = list;
  89. return pagedList;
  90. }
  91. /// <summary>
  92. /// 后台评论分页列表
  93. /// </summary>
  94. /// <param name="request"></param>
  95. /// <returns></returns>
  96. public async Task<PagedList<CommentResult>> GetAllAsync(CommentSearchRequest request)
  97. {
  98. var result = new PagedList<CommentResult>();
  99. var query = Q.NewQuery();
  100. if (!string.IsNullOrEmpty(request.KeyWord))
  101. {
  102. query.OrWhereLike(nameof(Comment.Content), $"%{request.KeyWord}%");
  103. query.OrWhereLike(nameof(Comment.Title), $"%{request.KeyWord}%");
  104. }
  105. result.Total = await _repository.CountAsync(query);
  106. var commentResult = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  107. result.Items = commentResult.Select(n => _mapper.Map<CommentResult>(n));
  108. return result;
  109. }
  110. /// <summary>
  111. /// 获取总条数
  112. /// </summary>
  113. /// <param name="articleId"></param>
  114. /// <returns></returns>
  115. public async Task<int> GetCountAsync(CommentSearchPageRequest request)
  116. {
  117. return await _repository.CountAsync(Q.Where(nameof(Comment.ArticleId), request.ArticleId)
  118. .Where(nameof(Comment.Pid), 0).Where(nameof(Comment.TypeValue), request.TypeValue));
  119. }
  120. /// <summary>
  121. /// 获取分页数据
  122. /// </summary>
  123. /// <param name="request"></param>
  124. /// <returns></returns>
  125. public async Task<IEnumerable<CommentPageResult>> GetPageListAsync(CommentSearchPageRequest request)
  126. {
  127. List<CommentPageResult> commentPageResults = new List<CommentPageResult>();
  128. var query = new SqlKata.Query();
  129. query.Where(nameof(Comment.ArticleId), request.ArticleId)
  130. .Where(nameof(Comment.Pid), 0).Where(nameof(Comment.TypeValue), request.TypeValue);
  131. if (string.IsNullOrEmpty(request.Sort))
  132. query.OrderByDesc(nameof(Comment.CreatedDate));
  133. if (!string.IsNullOrEmpty(request.Sort) && request.Sort == "asc")
  134. query.OrderBy(nameof(Comment.CreatedDate));
  135. if (!string.IsNullOrEmpty(request.Sort) && request.Sort == "desc")
  136. query.OrderByDesc(nameof(Comment.CreatedDate));
  137. var commentList = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  138. var i = 1;
  139. foreach (var item in commentList)
  140. {
  141. //获取当前用户信息
  142. var user = await _userRepository.GetAsync(item.UserId);
  143. if (user == null)
  144. continue;
  145. CommentPageResult commentPageResult = new CommentPageResult
  146. {
  147. ArticleId = item.ArticleId,
  148. AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl),
  149. Name = user.Name,
  150. Content = item.Content,
  151. CreatedTime = item.CreatedDate,
  152. LaudCount = await LaudCommentAsync(item.Id),
  153. UserId = item.UserId,
  154. //是否点赞
  155. IsLaud = await IsLaudCommentAsync(item.Id, item.UserId),
  156. Score = item.Score
  157. };
  158. if (request.Sort == "asc")
  159. commentPageResult.FloorCount = $"第{((request.Page - 1) * request.PerPage) + i}楼";
  160. else
  161. commentPageResult.FloorCount = $"第{((request.Total - (request.Page - 1) * request.PerPage) - (i - 1))}楼";
  162. //计算点赞数量
  163. //计算被回复数据
  164. List<CommentReplyResult> commentReplyResults = new List<CommentReplyResult>();
  165. commentPageResult.CommentReplyResults = await GetCommentReplyResultByPid(item.Id, commentReplyResults);
  166. commentPageResult.Id = item.Id;
  167. commentPageResults.Add(commentPageResult);
  168. i++;
  169. }
  170. return commentPageResults;
  171. }
  172. /// <summary>
  173. /// 当前用户是否点赞该评论
  174. /// </summary>
  175. /// <param name="commentId"></param>
  176. /// <param name="userId"></param>
  177. /// <returns></returns>
  178. public async Task<bool> IsLaudCommentAsync(int commentId, int userId)
  179. {
  180. return await _analyzeRepository.CountAsync(Q.Where(nameof(Entity.Analyze.Analyze.CommentId), commentId)
  181. .Where(nameof(Entity.Analyze.Analyze.UserId), userId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 2)) > 0;
  182. }
  183. /// <summary>
  184. /// 当前用户是否点赞该评论
  185. /// </summary>
  186. /// <param name="commentId"></param>
  187. /// <returns></returns>
  188. public async Task<int> LaudCommentAsync(int commentId)
  189. {
  190. return await _analyzeRepository.CountAsync(Q.Where(nameof(Entity.Analyze.Analyze.CommentId), commentId).Where(nameof(Entity.Analyze.Analyze.AnalyzeType), 2));
  191. }
  192. /// <summary>
  193. /// 递归查询父级下面的所有回复
  194. /// </summary>
  195. public async Task<List<CommentReplyResult>> GetCommentReplyResultByPid(int pid,
  196. List<CommentReplyResult> commentReplyResults)
  197. {
  198. var result = _repository.GetAllAsync(Q.Where(nameof(Comment.Pid), pid)).Result.ToList();
  199. if (result.Count == 0)
  200. return commentReplyResults;
  201. foreach (var item in result)
  202. {
  203. CommentReplyResult commentReplyResult = new CommentReplyResult
  204. {
  205. Id = item.Id,
  206. Content = item.Content,
  207. CreatedTime = item.CreatedDate
  208. };
  209. var user = await _userRepository.GetAsync(item.UserId);
  210. if (user == null)
  211. continue;
  212. commentReplyResult.Name = user.Name;
  213. if (item.IsUnderstand)
  214. {
  215. var comment = await _repository.GetAsync(Q.Where(nameof(Comment.Id), item.Pid));
  216. if (comment == null)
  217. continue;
  218. user = _userRepository.GetAsync(comment.UserId).Result;
  219. commentReplyResult.ReplyName = user.Name;
  220. commentReplyResult.ReplyUserId = user.Id;
  221. }
  222. commentReplyResult.IsUnderstand = item.IsUnderstand;
  223. commentReplyResult.UserId = item.UserId;
  224. commentReplyResults.Add(commentReplyResult);
  225. commentReplyResults = await GetCommentReplyResultByPid(item.Id, commentReplyResults);
  226. }
  227. return commentReplyResults;
  228. }
  229. /// <summary>
  230. /// 删除评论
  231. /// </summary>
  232. /// <param name="request"></param>
  233. /// <returns></returns>
  234. public async Task<bool> DeleteCommentAsync(CommentDeleteRequest request)
  235. {
  236. //查询评论数据
  237. var commentEntity = await _repository.GetAsync(Q.Where(nameof(Comment.ArticleId), request.ArticleId)
  238. .Where(nameof(Comment.Id), request.CommentId));
  239. if (commentEntity.Id == 0)
  240. throw new BusinessException("该评论不存在!");
  241. //递归删除
  242. var commentList = new List<Comment> { commentEntity };
  243. commentList = await GetCommentByParentId(commentEntity.Id, commentList);
  244. //删除
  245. foreach (var item in commentList)
  246. {
  247. await _repository.DeleteAsync(item.Id);
  248. }
  249. return true;
  250. }
  251. /// <summary>
  252. /// 递归删除
  253. /// </summary>
  254. /// <param name="id"></param>
  255. /// <param name="lists"></param>
  256. /// <returns></returns>
  257. public async Task<List<Comment>> GetCommentByParentId(int id, List<Comment> lists)
  258. {
  259. var commentList = await _repository.GetAllAsync(Q.Where(nameof(Comment.Pid), id));
  260. if (!commentList.Any())
  261. return lists;
  262. foreach (var comment in commentList)
  263. {
  264. lists.Add(comment);
  265. await GetCommentByParentId(comment.Id, lists);
  266. }
  267. return lists;
  268. }
  269. /// <summary>
  270. /// 获取评论数量
  271. /// </summary>
  272. /// <param name="articleId"></param>
  273. /// <returns></returns>
  274. public async Task<int> GetCommentCountAsync(int articleId)
  275. {
  276. return await _repository.CountAsync(Q.Where(nameof(Comment.ArticleId), articleId));
  277. }
  278. public async Task<Comment> GetAsync(Query query)
  279. {
  280. return await _repository.GetAsync(query);
  281. }
  282. public async Task<bool> UpdateAsync(Comment comment)
  283. {
  284. return await _repository.UpdateAsync(comment);
  285. }
  286. public async Task<int> CountAsync(Query query)
  287. {
  288. return await _repository.CountAsync(query);
  289. }
  290. /// <summary>
  291. /// 修改评论
  292. /// </summary>
  293. /// <param name="request"></param>
  294. /// <returns></returns>
  295. public async Task<bool> UpdateCommentAsync(CommentUpdateRequest request)
  296. {
  297. //判断类容是否为空
  298. if (string.IsNullOrWhiteSpace(request.Content))
  299. throw new Common.Exceptions.BusinessException("评论不能为空!");
  300. //查询评论
  301. var comment = await _repository.GetAsync(request.CommentId);
  302. if (comment == null)
  303. throw new Common.Exceptions.BusinessException("该评论不存在!");
  304. if (comment.UserId != request.UserId)
  305. throw new Common.Exceptions.BusinessException("该评论不属于该用户!");
  306. //修改
  307. comment.Content = request.Content;
  308. if (request.Score > 0)
  309. comment.Score = request.Score;
  310. return await UpdateAsync(comment);
  311. }
  312. }
  313. }