CommentIndexServiceImpl.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package com.ruoyi.system.service.impl;
  2. import com.ruoyi.common.core.domain.AjaxResult;
  3. import com.ruoyi.common.core.redis.RedisCache;
  4. import com.ruoyi.common.utils.DateUtils;
  5. import com.ruoyi.common.utils.SecurityUtils;
  6. import com.ruoyi.common.utils.StringUtils;
  7. import com.ruoyi.system.domain.communityNews.CommentContent;
  8. import com.ruoyi.system.domain.communityNews.CommentIndex;
  9. import com.ruoyi.system.domain.communityNews.vo.CommentChildrenVo;
  10. import com.ruoyi.system.domain.communityNews.vo.CommentIndexShVo;
  11. import com.ruoyi.system.domain.communityNews.vo.CommentIndexVo;
  12. import com.ruoyi.system.mapper.CommentContentMapper;
  13. import com.ruoyi.system.mapper.CommentIndexMapper;
  14. import com.ruoyi.system.service.ICommentIndexService;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.transaction.annotation.Transactional;
  18. import javax.annotation.Resource;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import static com.ruoyi.common.constant.Constants.*;
  22. /**
  23. * 社区资讯评论Service业务层处理
  24. *
  25. * @author boman
  26. * @date 2025-02-25
  27. */
  28. @Service
  29. public class CommentIndexServiceImpl implements ICommentIndexService {
  30. @Autowired
  31. private CommentIndexMapper commentIndexMapper;
  32. @Autowired
  33. private CommentContentMapper commentContentMapper;
  34. @Resource
  35. private RedisCache redisCache;
  36. /**
  37. * 查询社区资讯评论
  38. *
  39. * @param commentId 社区资讯评论主键
  40. * @return 社区资讯评论
  41. */
  42. @Override
  43. public CommentIndex selectCommentIndexByCommentId(Long commentId) {
  44. return commentIndexMapper.selectCommentIndexByCommentId(commentId);
  45. }
  46. /**
  47. * 查询社区资讯评论列表
  48. *
  49. * @param commentIndex 社区资讯评论
  50. * @return 社区资讯评论
  51. */
  52. @Override
  53. public List<CommentIndex> selectCommentIndexList(CommentIndex commentIndex) {
  54. return commentIndexMapper.selectCommentIndexList(commentIndex);
  55. }
  56. /**
  57. * 查询社区资讯评论审核列表
  58. */
  59. @Override
  60. public List<CommentIndexShVo> commentSh(CommentIndexShVo commentIndexShVo) {
  61. return commentIndexMapper.commentSh(commentIndexShVo);
  62. }
  63. /**
  64. * 新增社区资讯评论
  65. *
  66. * @param commentIndex 社区资讯评论
  67. * @return 结果
  68. */
  69. @Override
  70. @Transactional(rollbackFor = Exception.class)
  71. public int insertCommentIndex(CommentIndex commentIndex) {
  72. int result = 1;
  73. //有社区资讯ID才能新增评论
  74. Long communityId = commentIndex.getCommunityId();
  75. if (communityId != null) {
  76. Long userId = commentIndex.getUserId();
  77. //需要插入评论内容表
  78. CommentContent commentContent = new CommentContent();
  79. commentContent.setUserId(userId);
  80. commentContent.setCommentContent(commentIndex.getCommentContent());
  81. commentContentMapper.insertCommentContent(commentContent);
  82. commentIndex.setCreateTime(DateUtils.getNowDate());
  83. commentIndex.setContentId(commentContent.getContentId());
  84. result = commentIndexMapper.insertCommentIndex(commentIndex);
  85. //再把评论内容表更新上资讯评论表id
  86. commentContent.setCommentId(commentIndex.getCommentId());
  87. commentContentMapper.updateCommentContent(commentContent);
  88. //记录该社区资讯评论的数量
  89. String communityCommentCountKey = COMMUNITY_COMMENT_COUNT + communityId;
  90. Integer communityCommentCount = redisCache.getCacheObject(communityCommentCountKey);
  91. if (communityCommentCount == null) {
  92. redisCache.setCacheObject(communityCommentCountKey, 1);
  93. } else {
  94. //评论数量+1
  95. redisCache.setCacheObject(communityCommentCountKey, communityCommentCount + 1);
  96. }
  97. }
  98. return result;
  99. }
  100. /**
  101. * 修改社区资讯评论
  102. *
  103. * @param commentIndex 社区资讯评论
  104. * @return 结果
  105. */
  106. @Override
  107. public int updateCommentIndex(CommentIndex commentIndex) {
  108. commentIndex.setUpdateTime(DateUtils.getNowDate());
  109. return commentIndexMapper.updateCommentIndex(commentIndex);
  110. }
  111. /**
  112. * 批量删除社区资讯评论
  113. *
  114. * @param commentIds 需要删除的社区资讯评论主键
  115. * @return 结果
  116. */
  117. @Override
  118. public int deleteCommentIndexByCommentIds(Long[] commentIds) {
  119. return commentIndexMapper.deleteCommentIndexByCommentIds(commentIds);
  120. }
  121. /**
  122. * 删除社区资讯评论信息
  123. *
  124. * @param commentId 社区资讯评论主键
  125. * @return 结果
  126. */
  127. @Override
  128. @Transactional(rollbackFor = Exception.class)
  129. public int deleteCommentIndexByCommentId(Long commentId) {
  130. //只能删除自己的评论
  131. CommentIndex commentIndex = commentIndexMapper.selectCommentIndexByCommentId(commentId);
  132. int result = 0;
  133. int communityCommentCount = 0;
  134. //查找出所有parent_id = commentId的commentId
  135. List<Long> commentIdList = new ArrayList<>();
  136. if (commentIndex != null && commentIndex.getUserId().equals(SecurityUtils.getUserId())) {
  137. //社区资讯id
  138. Long communityId = commentIndex.getCommunityId();
  139. result = commentIndexMapper.updateCommentIndexByCommentId(commentId);
  140. //删除评论内容表数据
  141. commentContentMapper.updateCommentContentByContentId(commentId);
  142. //如果是根评论删除,删除所有子评论
  143. if (1 == commentIndex.getIsRoot()) {
  144. //查找出所有parent_id = commentId的commentId
  145. commentIdList = commentIndexMapper.selectCommentIdListByCommentId(commentId);
  146. int i = commentIndexMapper.updateCommentIndexByParentId(commentId);
  147. result = result + i;
  148. //删除评论内容表子集数据
  149. commentContentMapper.deleteCommentContentByCommentId(commentId);
  150. }
  151. //删除该资讯的评论数量
  152. String communityCommentCountKey = COMMUNITY_COMMENT_COUNT + communityId;
  153. communityCommentCount = redisCache.getCacheObject(communityCommentCountKey);
  154. if (communityCommentCount != 0) {
  155. //删除所有的评论数量 = 根评论数量+子评论数量
  156. communityCommentCount = communityCommentCount - result;
  157. if (communityCommentCount < 0) {
  158. communityCommentCount = 0;
  159. }
  160. redisCache.setCacheObject(communityCommentCountKey, communityCommentCount);
  161. }
  162. //删除该资讯点赞的数量需要知道所有评论的id
  163. //首先获取该文章资讯的总点赞数量
  164. Integer likeCount = redisCache.getCacheObject(ONE_LIKE_COUNT + communityId);
  165. //再获取删除的评论的点赞数量
  166. Integer commentLikeCount = redisCache.getCacheObject(TWO_LIKE_COUNT + commentId);
  167. //所有子集的点赞数量
  168. int commentLikeChildrenCount = 0;
  169. if (commentIdList.size() > 0) {
  170. //说明删除了子集、获取所有子集的点赞数量
  171. for (Long commentIdChildren : commentIdList) {
  172. String commentLikeChildrenCountString = redisCache.getCacheObject(TWO_LIKE_COUNT + commentIdChildren);
  173. if (StringUtils.isNotBlank(commentLikeChildrenCountString)) {
  174. commentLikeChildrenCount = commentLikeChildrenCount + Integer.parseInt(commentLikeChildrenCountString);
  175. }
  176. }
  177. }
  178. //把删除后点赞数量写入到redis
  179. likeCount = likeCount - commentLikeCount - commentLikeChildrenCount;
  180. if (likeCount < 0){
  181. likeCount = 0;
  182. }
  183. redisCache.setCacheObject(ONE_LIKE_COUNT + communityId, likeCount);
  184. //todo 删除后还需要减少未读互动的数据
  185. }
  186. return result;
  187. }
  188. /**
  189. * 根据parent_id查询所有评论组装成前端所需要的子集数据结构 带分页
  190. *
  191. * @param commentIndex
  192. * @return
  193. */
  194. @Override
  195. public AjaxResult getParentComment(CommentIndex commentIndex) {
  196. Long userId = SecurityUtils.getUserId();
  197. List<CommentChildrenVo> allComment = commentIndexMapper.getParentComment(commentIndex);
  198. //要判断所有评论当前人员是否点赞过
  199. if (allComment != null && allComment.size() > 0) {
  200. //要判断所有评论当前人员是否点赞过
  201. //该用户点赞回复的key
  202. String myLikeTwoKey = TWO_MY_LIKE + userId;
  203. List<Long> communityIdList = redisCache.getCacheList(myLikeTwoKey);
  204. if (communityIdList != null && communityIdList.size() > 0) {
  205. for (CommentChildrenVo commentChildrenVo : allComment) {
  206. if (communityIdList.contains(commentChildrenVo.getId())) {
  207. commentChildrenVo.setIsLike("Y");
  208. }
  209. }
  210. }
  211. }
  212. return AjaxResult.success(allComment);
  213. }
  214. /**
  215. * 根据查询所有一级评论组带子集总数组装成前端所需要的数据结构 带分页
  216. *
  217. * @param commentIndex
  218. * @return
  219. */
  220. @Override
  221. public List<CommentIndexVo> getRootComment(CommentIndex commentIndex) {
  222. Long userId = SecurityUtils.getUserId();
  223. List<CommentIndexVo> rootComment = commentIndexMapper.getRootComment(commentIndex);
  224. if (rootComment != null && rootComment.size() > 0) {
  225. //要判断所有评论当前人员是否点赞过
  226. //该用户点赞回复的key
  227. String myLikeTwoKey = TWO_MY_LIKE + userId;
  228. List<Long> communityIdList = redisCache.getCacheList(myLikeTwoKey);
  229. if (communityIdList != null && communityIdList.size() > 0) {
  230. for (CommentIndexVo commentIndexVo : rootComment) {
  231. if (communityIdList.contains(commentIndexVo.getId())) {
  232. commentIndexVo.setIsLike("Y");
  233. }
  234. }
  235. }
  236. }
  237. return rootComment;
  238. }
  239. }