123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- package com.ruoyi.system.service.impl;
- import com.ruoyi.common.core.domain.AjaxResult;
- import com.ruoyi.common.core.redis.RedisCache;
- import com.ruoyi.common.utils.DateUtils;
- import com.ruoyi.common.utils.SecurityUtils;
- import com.ruoyi.common.utils.StringUtils;
- import com.ruoyi.system.domain.communityNews.CommentContent;
- import com.ruoyi.system.domain.communityNews.CommentIndex;
- import com.ruoyi.system.domain.communityNews.vo.CommentChildrenVo;
- import com.ruoyi.system.domain.communityNews.vo.CommentIndexShVo;
- import com.ruoyi.system.domain.communityNews.vo.CommentIndexVo;
- import com.ruoyi.system.mapper.CommentContentMapper;
- import com.ruoyi.system.mapper.CommentIndexMapper;
- import com.ruoyi.system.service.ICommentIndexService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.util.ArrayList;
- import java.util.List;
- import static com.ruoyi.common.constant.Constants.*;
- /**
- * 社区资讯评论Service业务层处理
- *
- * @author boman
- * @date 2025-02-25
- */
- @Service
- public class CommentIndexServiceImpl implements ICommentIndexService {
- @Autowired
- private CommentIndexMapper commentIndexMapper;
- @Autowired
- private CommentContentMapper commentContentMapper;
- @Resource
- private RedisCache redisCache;
- /**
- * 查询社区资讯评论
- *
- * @param commentId 社区资讯评论主键
- * @return 社区资讯评论
- */
- @Override
- public CommentIndex selectCommentIndexByCommentId(Long commentId) {
- return commentIndexMapper.selectCommentIndexByCommentId(commentId);
- }
- /**
- * 查询社区资讯评论列表
- *
- * @param commentIndex 社区资讯评论
- * @return 社区资讯评论
- */
- @Override
- public List<CommentIndex> selectCommentIndexList(CommentIndex commentIndex) {
- return commentIndexMapper.selectCommentIndexList(commentIndex);
- }
- /**
- * 查询社区资讯评论审核列表
- */
- @Override
- public List<CommentIndexShVo> commentSh(CommentIndexShVo commentIndexShVo) {
- return commentIndexMapper.commentSh(commentIndexShVo);
- }
- /**
- * 新增社区资讯评论
- *
- * @param commentIndex 社区资讯评论
- * @return 结果
- */
- @Override
- @Transactional(rollbackFor = Exception.class)
- public int insertCommentIndex(CommentIndex commentIndex) {
- int result = 1;
- //有社区资讯ID才能新增评论
- Long communityId = commentIndex.getCommunityId();
- if (communityId != null) {
- Long userId = commentIndex.getUserId();
- //需要插入评论内容表
- CommentContent commentContent = new CommentContent();
- commentContent.setUserId(userId);
- commentContent.setCommentContent(commentIndex.getCommentContent());
- commentContentMapper.insertCommentContent(commentContent);
- commentIndex.setCreateTime(DateUtils.getNowDate());
- commentIndex.setContentId(commentContent.getContentId());
- result = commentIndexMapper.insertCommentIndex(commentIndex);
- //再把评论内容表更新上资讯评论表id
- commentContent.setCommentId(commentIndex.getCommentId());
- commentContentMapper.updateCommentContent(commentContent);
- //记录该社区资讯评论的数量
- String communityCommentCountKey = COMMUNITY_COMMENT_COUNT + communityId;
- Integer communityCommentCount = redisCache.getCacheObject(communityCommentCountKey);
- if (communityCommentCount == null) {
- redisCache.setCacheObject(communityCommentCountKey, 1);
- } else {
- //评论数量+1
- redisCache.setCacheObject(communityCommentCountKey, communityCommentCount + 1);
- }
- }
- return result;
- }
- /**
- * 修改社区资讯评论
- *
- * @param commentIndex 社区资讯评论
- * @return 结果
- */
- @Override
- public int updateCommentIndex(CommentIndex commentIndex) {
- commentIndex.setUpdateTime(DateUtils.getNowDate());
- return commentIndexMapper.updateCommentIndex(commentIndex);
- }
- /**
- * 批量删除社区资讯评论
- *
- * @param commentIds 需要删除的社区资讯评论主键
- * @return 结果
- */
- @Override
- public int deleteCommentIndexByCommentIds(Long[] commentIds) {
- return commentIndexMapper.deleteCommentIndexByCommentIds(commentIds);
- }
- /**
- * 删除社区资讯评论信息
- *
- * @param commentId 社区资讯评论主键
- * @return 结果
- */
- @Override
- @Transactional(rollbackFor = Exception.class)
- public int deleteCommentIndexByCommentId(Long commentId) {
- //只能删除自己的评论
- CommentIndex commentIndex = commentIndexMapper.selectCommentIndexByCommentId(commentId);
- int result = 0;
- int communityCommentCount = 0;
- //查找出所有parent_id = commentId的commentId
- List<Long> commentIdList = new ArrayList<>();
- if (commentIndex != null && commentIndex.getUserId().equals(SecurityUtils.getUserId())) {
- //社区资讯id
- Long communityId = commentIndex.getCommunityId();
- result = commentIndexMapper.updateCommentIndexByCommentId(commentId);
- //删除评论内容表数据
- commentContentMapper.updateCommentContentByContentId(commentId);
- //如果是根评论删除,删除所有子评论
- if (1 == commentIndex.getIsRoot()) {
- //查找出所有parent_id = commentId的commentId
- commentIdList = commentIndexMapper.selectCommentIdListByCommentId(commentId);
- int i = commentIndexMapper.updateCommentIndexByParentId(commentId);
- result = result + i;
- //删除评论内容表子集数据
- commentContentMapper.deleteCommentContentByCommentId(commentId);
- }
- //删除该资讯的评论数量
- String communityCommentCountKey = COMMUNITY_COMMENT_COUNT + communityId;
- communityCommentCount = redisCache.getCacheObject(communityCommentCountKey);
- if (communityCommentCount != 0) {
- //删除所有的评论数量 = 根评论数量+子评论数量
- communityCommentCount = communityCommentCount - result;
- if (communityCommentCount < 0) {
- communityCommentCount = 0;
- }
- redisCache.setCacheObject(communityCommentCountKey, communityCommentCount);
- }
- //删除该资讯点赞的数量需要知道所有评论的id
- //首先获取该文章资讯的总点赞数量
- Integer likeCount = redisCache.getCacheObject(ONE_LIKE_COUNT + communityId);
- //再获取删除的评论的点赞数量
- Integer commentLikeCount = redisCache.getCacheObject(TWO_LIKE_COUNT + commentId);
- //所有子集的点赞数量
- int commentLikeChildrenCount = 0;
- if (commentIdList.size() > 0) {
- //说明删除了子集、获取所有子集的点赞数量
- for (Long commentIdChildren : commentIdList) {
- String commentLikeChildrenCountString = redisCache.getCacheObject(TWO_LIKE_COUNT + commentIdChildren);
- if (StringUtils.isNotBlank(commentLikeChildrenCountString)) {
- commentLikeChildrenCount = commentLikeChildrenCount + Integer.parseInt(commentLikeChildrenCountString);
- }
- }
- }
- //把删除后点赞数量写入到redis
- likeCount = likeCount - commentLikeCount - commentLikeChildrenCount;
- if (likeCount < 0){
- likeCount = 0;
- }
- redisCache.setCacheObject(ONE_LIKE_COUNT + communityId, likeCount);
- //todo 删除后还需要减少未读互动的数据
- }
- return result;
- }
- /**
- * 根据parent_id查询所有评论组装成前端所需要的子集数据结构 带分页
- *
- * @param commentIndex
- * @return
- */
- @Override
- public AjaxResult getParentComment(CommentIndex commentIndex) {
- Long userId = SecurityUtils.getUserId();
- List<CommentChildrenVo> allComment = commentIndexMapper.getParentComment(commentIndex);
- //要判断所有评论当前人员是否点赞过
- if (allComment != null && allComment.size() > 0) {
- //要判断所有评论当前人员是否点赞过
- //该用户点赞回复的key
- String myLikeTwoKey = TWO_MY_LIKE + userId;
- List<Long> communityIdList = redisCache.getCacheList(myLikeTwoKey);
- if (communityIdList != null && communityIdList.size() > 0) {
- for (CommentChildrenVo commentChildrenVo : allComment) {
- if (communityIdList.contains(commentChildrenVo.getId())) {
- commentChildrenVo.setIsLike("Y");
- }
- }
- }
- }
- return AjaxResult.success(allComment);
- }
- /**
- * 根据查询所有一级评论组带子集总数组装成前端所需要的数据结构 带分页
- *
- * @param commentIndex
- * @return
- */
- @Override
- public List<CommentIndexVo> getRootComment(CommentIndex commentIndex) {
- Long userId = SecurityUtils.getUserId();
- List<CommentIndexVo> rootComment = commentIndexMapper.getRootComment(commentIndex);
- if (rootComment != null && rootComment.size() > 0) {
- //要判断所有评论当前人员是否点赞过
- //该用户点赞回复的key
- String myLikeTwoKey = TWO_MY_LIKE + userId;
- List<Long> communityIdList = redisCache.getCacheList(myLikeTwoKey);
- if (communityIdList != null && communityIdList.size() > 0) {
- for (CommentIndexVo commentIndexVo : rootComment) {
- if (communityIdList.contains(commentIndexVo.getId())) {
- commentIndexVo.setIsLike("Y");
- }
- }
- }
- }
- return rootComment;
- }
- }
|