TopicCommentRepository.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.Topic;
  11. using GxPress.Repository.Interface;
  12. using GxPress.Request.App.TopicComment;
  13. using GxPress.Result.App.TopicComment;
  14. using Microsoft.Extensions.Options;
  15. using Datory;
  16. using SqlKata;
  17. namespace GxPress.Repository.Implement
  18. {
  19. public class TopicCommentRepository : ITopicCommentRepository
  20. {
  21. private readonly Repository<TopicComment> _repository;
  22. private readonly Repository<TopicAnalyze> _topicAnalyzeRepository;
  23. private readonly Repository<Entity.Topic.Topic> _topicRepository;
  24. private readonly Repository<User> _userRepository;
  25. private readonly IMapper _mapper;
  26. public TopicCommentRepository(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<TopicComment>(database);
  31. _topicRepository = new Repository<Entity.Topic.Topic>(database);
  32. _userRepository = new Repository<User>(database);
  33. _topicAnalyzeRepository = new Repository<TopicAnalyze>(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> TopicCommentInAsync(TopicCommentInRequest request)
  45. {
  46. //查询文章是否存在
  47. var article = _topicRepository.ExistsAsync(Q.Where(nameof(Entity.Topic.Topic.Id), request.TopicId));
  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 topicComment = new TopicComment
  55. {
  56. TopicId = request.TopicId,
  57. Content = request.Content,
  58. UserId = request.UserId,
  59. Pid = request.Pid
  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. topicComment.IsUnderstand = true;
  69. }
  70. }
  71. return await _repository.InsertAsync(topicComment);
  72. }
  73. /// <summary>
  74. /// 分页列表
  75. /// </summary>
  76. /// <param name="request"></param>
  77. /// <returns></returns>
  78. public async Task<PagedList<TopicCommentPageResult>> GetPagedList(TopicCommentSearchPageRequest request)
  79. {
  80. var pagedList = new PagedList<TopicCommentPageResult>
  81. {
  82. Total = await GetCountAsync(request.TopicId)
  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="topicId"></param>
  93. /// <returns></returns>
  94. public async Task<int> GetCountAsync(int topicId)
  95. {
  96. return await _repository.CountAsync(Q.Where(nameof(TopicComment.TopicId), topicId)
  97. .Where(nameof(Comment.Pid), 0));
  98. }
  99. /// <summary>
  100. /// 获取分页数据
  101. /// </summary>
  102. /// <param name="request"></param>
  103. /// <returns></returns>
  104. public async Task<IEnumerable<TopicCommentPageResult>> GetPageListAsync(TopicCommentSearchPageRequest request)
  105. {
  106. List<TopicCommentPageResult> commentPageResults = new List<TopicCommentPageResult>();
  107. var query = new SqlKata.Query();
  108. query.Where(nameof(TopicComment.TopicId), request.TopicId).Where(nameof(TopicComment.Pid), 0);
  109. if (string.IsNullOrEmpty(request.Sort))
  110. query.OrderByDesc(nameof(TopicComment.CreatedDate));
  111. if (!string.IsNullOrEmpty(request.Sort) && request.Sort == "asc")
  112. query.OrderBy(nameof(TopicComment.CreatedDate));
  113. if (!string.IsNullOrEmpty(request.Sort) && request.Sort == "desc")
  114. query.OrderByDesc(nameof(TopicComment.CreatedDate));
  115. var commentList = await _repository.GetAllAsync(query.ForPage(request.Page, request.PerPage));
  116. var i = 1;
  117. foreach (var item in commentList)
  118. {
  119. //获取当前用户信息
  120. var user = _userRepository.GetAsync(item.UserId).Result;
  121. TopicCommentPageResult commentPageResult = new TopicCommentPageResult
  122. {
  123. TopicId = item.TopicId,
  124. AvatarUrl = StringUtils.AddDomainMin(user.AvatarUrl),
  125. Name = user.Name,
  126. Content = item.Content,
  127. CreatedTime = item.CreatedDate,
  128. LaudCount = item.Laud,
  129. UserId = item.UserId,
  130. //是否点赞
  131. IsLaud = await IsLaudCommentAsync(item.Id, item.UserId)
  132. };
  133. if (request.Sort == "asc")
  134. commentPageResult.FloorCount = $"第{((request.Page - 1) * request.PerPage) + i}楼";
  135. else
  136. commentPageResult.FloorCount = $"第{((request.Total - (request.Page - 1) * request.PerPage) - (i - 1))}楼";
  137. //计算点赞数量
  138. //计算被回复数据
  139. List<TopicCommentReplyResult> commentReplyResults = new List<TopicCommentReplyResult>();
  140. commentPageResult.TopicCommentReplyResults = await GetTopicCommentReplyResultByPid(item.Id, commentReplyResults);
  141. commentPageResult.Id = item.Id;
  142. commentPageResults.Add(commentPageResult);
  143. i++;
  144. }
  145. return commentPageResults;
  146. }
  147. /// <summary>
  148. /// 当前用户是否点赞该评论
  149. /// </summary>
  150. /// <param name="commentId"></param>
  151. /// <param name="userId"></param>
  152. /// <returns></returns>
  153. public async Task<bool> IsLaudCommentAsync(int commentId, int userId)
  154. {
  155. return await _topicAnalyzeRepository.CountAsync(Q.Where(nameof(TopicAnalyze.TopicCommentId), commentId)
  156. .Where(nameof(TopicAnalyze.UserId), userId).Where(nameof(TopicAnalyze.AnalyzeType), 2)) > 0;
  157. }
  158. /// <summary>
  159. /// 递归查询父级下面的所有回复
  160. /// </summary>
  161. public async Task<List<TopicCommentReplyResult>> GetTopicCommentReplyResultByPid(int pid,
  162. List<TopicCommentReplyResult> topicCommentReplyResults)
  163. {
  164. var result = _repository.GetAllAsync(Q.Where(nameof(TopicComment.Pid), pid)).Result.ToList();
  165. if (result.Count == 0)
  166. return topicCommentReplyResults;
  167. foreach (var item in result)
  168. {
  169. TopicCommentReplyResult topicCommentReplyResult = new TopicCommentReplyResult
  170. {
  171. Id = item.Id,
  172. Content = item.Content,
  173. CreatedTime = item.CreatedDate
  174. };
  175. var user = _userRepository.GetAsync(item.UserId).Result;
  176. topicCommentReplyResult.Name = user.Name;
  177. if (item.IsUnderstand)
  178. {
  179. var comment = _repository.GetAsync(Q.Where(nameof(Comment.Id), item.Pid)).Result;
  180. user = _userRepository.GetAsync(comment.UserId).Result;
  181. topicCommentReplyResult.ReplyName = user.Name;
  182. topicCommentReplyResult.ReplyUserId=user.Id;
  183. }
  184. topicCommentReplyResult.IsUnderstand = item.IsUnderstand;
  185. topicCommentReplyResult.UserId = item.UserId;
  186. topicCommentReplyResults.Add(topicCommentReplyResult);
  187. topicCommentReplyResults = await GetTopicCommentReplyResultByPid(item.Id, topicCommentReplyResults);
  188. }
  189. return topicCommentReplyResults;
  190. }
  191. /// <summary>
  192. /// 删除评论
  193. /// </summary>
  194. /// <param name="request"></param>
  195. /// <returns></returns>
  196. public async Task<bool> DeleteTopicCommentAsync(TopicCommentDeleteRequest request)
  197. {
  198. //查询评论数据
  199. var topicCommentEntity = _repository.GetAsync(Q.Where(nameof(TopicComment.TopicId), request.TopicId)
  200. .Where(nameof(TopicComment.Id), request.CommentId)).Result;
  201. if (topicCommentEntity.Id == 0)
  202. throw new BusinessException("该评论不存在!");
  203. //递归删除
  204. var commentList = new List<TopicComment> { topicCommentEntity };
  205. commentList = await GetTopicCommentByParentId(topicCommentEntity.Id, commentList);
  206. //删除
  207. foreach (var item in commentList)
  208. {
  209. await _repository.DeleteAsync(item.Id);
  210. }
  211. return true;
  212. }
  213. /// <summary>
  214. /// 递归删除
  215. /// </summary>
  216. /// <param name="id"></param>
  217. /// <param name="lists"></param>
  218. /// <returns></returns>
  219. public async Task<List<TopicComment>> GetTopicCommentByParentId(int id, List<TopicComment> lists)
  220. {
  221. var topicComment = await _repository.GetAsync(Q.Where(nameof(TopicComment.Pid), id));
  222. if (topicComment == null)
  223. return lists;
  224. lists.Add(topicComment);
  225. await GetTopicCommentByParentId(topicComment.Id, lists);
  226. return lists;
  227. }
  228. /// <summary>
  229. /// 获取评论数量
  230. /// </summary>
  231. /// <param name="topicId"></param>
  232. /// <returns></returns>
  233. public async Task<int> GetTopicCommentCountAsync(int topicId)
  234. {
  235. return await _repository.CountAsync(Q.Where(nameof(TopicComment.TopicId), topicId));
  236. }
  237. public async Task<int> CountAsync(Query query)
  238. {
  239. return await _repository.CountAsync(query);
  240. }
  241. }
  242. }