CommentRepository.cs 13 KB

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