Explorar o código

修改优化点赞回复和评论逻辑

fangzhen hai 6 meses
pai
achega
6549a5d6ac

+ 21 - 12
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/CommunityArticleController.java

@@ -13,6 +13,7 @@ import com.ruoyi.generator.mapper.community.*;
 import com.ruoyi.generator.service.ICommunityArticleRecommendService;
 import com.ruoyi.generator.service.ICommunityArticleService;
 import com.ruoyi.generator.service.ICommunityCollectionService;
+import com.ruoyi.generator.service.ICommunityCommentReplyService;
 import com.ruoyi.generator.vo.CommunityArticleVo;
 import com.ruoyi.generator.vo.CommunityCircleVo;
 import com.ruoyi.generator.vo.CommunityCollectionArticleVo;
@@ -68,6 +69,9 @@ public class CommunityArticleController extends BaseController {
     @Autowired
     private ICommunityArticleRecommendService communityArticleRecommendService;
 
+    @Autowired
+    private ICommunityCommentReplyService commentReplyService;
+
     /**
      * 获取文章列表信息
      */
@@ -276,22 +280,32 @@ public class CommunityArticleController extends BaseController {
         if (Objects.isNull(commentLike.getCommentId())) {
             return AjaxResult.error("评论或用户信息异常!");
         }
+
         Long userId = SecurityUtils.getLoginUser().getUserId();
-        boolean isSuccess = true;
         String message = "";
+        //评论点赞
         //查询该评论是否已点赞
-        CommunityCommentLike communityCommentLike = communityCommentLikeMapper
-                .selectOne(new QueryWrapper<CommunityCommentLike>()
-                        .eq("comment_id", commentLike.getCommentId())
-                        .eq("user_id", userId));
+        QueryWrapper<CommunityCommentLike> queryWrapper = null;
+        if (commentLike.isReply()) {
+            //回复的点赞
+            queryWrapper = new QueryWrapper<CommunityCommentLike>()
+                    .eq("reply_id", commentLike.getCommentId())
+                    .eq("user_id", userId);
+            commentLike.setReplyId(commentLike.getCommentId());
+            commentLike.setCommentId(null);
+        } else {
+            //评论的点赞
+            queryWrapper = new QueryWrapper<CommunityCommentLike>()
+                    .eq("comment_id", commentLike.getCommentId())
+                    .eq("user_id", userId);
+        }
 
+        CommunityCommentLike communityCommentLike = communityCommentLikeMapper.selectOne(queryWrapper);
         if (Objects.isNull(communityCommentLike) || Objects.isNull(communityCommentLike.getId())) {
             //新增点赞
             commentLike.setCreateBy(userId);
             commentLike.setUserId(userId);
-            commentLike.setUpdateBy(userId);
             commentLike.setCreateTime(DateUtils.parseDate(DateUtils.getTime()));
-            commentLike.setUpdateTime(DateUtils.parseDate(DateUtils.getTime()));
             communityCommentLikeMapper.insert(commentLike);
             message = "感谢点赞";
         } else {
@@ -299,11 +313,6 @@ public class CommunityArticleController extends BaseController {
             communityCommentLikeMapper.deleteById(communityCommentLike);
             message = "取消点赞";
         }
-
-        if (!isSuccess) {
-            message = "操作失败!";
-            return AjaxResult.error(message);
-        }
         return AjaxResult.success(message);
     }
 

+ 29 - 29
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/CommunityCommentController.java

@@ -68,10 +68,9 @@ public class CommunityCommentController extends BaseController {
             return AjaxResult.error("参数异常!");
         }
         Page<CommunityArticleComment> page = new Page<>(currentPage, limit);
-        List<CommunityArticleComment> records = communityArticleCommentService.page(page, new QueryWrapper<CommunityArticleComment>()
-                .eq("article_id", articleId).and((wrapper) -> {
-                    wrapper.ne("is_delete", true).or().isNull("is_delete");
-                }).orderByDesc("create_time")).getRecords();
+        List<CommunityArticleComment> records = communityArticleCommentService.page(page, new QueryWrapper<CommunityArticleComment>().eq("article_id", articleId).and((wrapper) -> {
+            wrapper.ne("is_delete", true).or().isNull("is_delete");
+        }).orderByDesc("create_time")).getRecords();
 
         CommunityArticleCommentVo commentVo = null;
         List<CommunityArticleCommentVo> commentVos = new ArrayList<>();
@@ -89,18 +88,14 @@ public class CommunityCommentController extends BaseController {
             vo.setNickName(sysUser.getNickName());
             vo.setAvatar(sysUser.getAvatar());
             //当前登录用户是否已点赞
-            List<CommunityCommentLike> commentLikes = communityCommentLikeMapper.selectList(new QueryWrapper<CommunityCommentLike>()
-                    .eq("comment_id", vo.getId())
-                    .eq("user_id", userId));
+            List<CommunityCommentLike> commentLikes = communityCommentLikeMapper.selectList(new QueryWrapper<CommunityCommentLike>().eq("comment_id", vo.getId()).eq("user_id", userId));
             vo.setCommentLike(!commentLikes.isEmpty());
             //该评论的点赞数量
             vo.setCommentLikeCount(communityCommentLikeMapper.selectList(new QueryWrapper<CommunityCommentLike>().eq("comment_id", vo.getId())).size());
             //设置首条评论
-            List<CommunityCommentReply> replies = communityCommentReplyService.list(new QueryWrapper<CommunityCommentReply>()
-                    .eq("comment_id", vo.getId())
-                    .and((wrapper) -> {
-                        wrapper.ne("is_delete", true).or().isNull("is_delete");
-                    }).orderByAsc("create_time"));
+            List<CommunityCommentReply> replies = communityCommentReplyService.list(new QueryWrapper<CommunityCommentReply>().eq("comment_id", vo.getId()).and((wrapper) -> {
+                wrapper.ne("is_delete", true).or().isNull("is_delete");
+            }).orderByAsc("create_time"));
 
             CommunityCommentReplyVo replyVo = null;
             List<CommunityCommentReplyVo> replyVos = new ArrayList<>();
@@ -108,12 +103,19 @@ public class CommunityCommentController extends BaseController {
                 replyVo = new CommunityCommentReplyVo();
                 BeanUtils.copyProperties(reply, replyVo);
                 replyVo.setNickName(sysUserService.selectUserById(replyVo.getUserId()).getNickName());
+                replyVo.setReply(true);
                 replyVos.add(replyVo);
+                //设置点赞数量和当前登录用户是否已点赞
+                List<CommunityCommentLike> commentLikeList = communityCommentLikeMapper.selectList(new QueryWrapper<CommunityCommentLike>()
+                        .eq("reply_id", replyVo.getId()));
+                replyVo.setLikeCount(commentLikeList.size());
+                replyVo.setLike(commentLikeList.stream().anyMatch(item -> SecurityUtils.getUserId().equals(item.getUserId())));
                 break;
             }
             vo.setFirstReply(replyVos);
             //设置还剩多少评论
             vo.setReplyCount(replies.isEmpty() ? 0 : replies.size() - 1);
+            vo.setReply(false);
         }
 
         return AjaxResult.success(commentVos);
@@ -131,12 +133,9 @@ public class CommunityCommentController extends BaseController {
             return AjaxResult.error("参数异常!");
         }
         Page<CommunityCommentReply> page = new Page<>(currentPage, limit);
-        List<CommunityCommentReply> replies = communityCommentReplyService
-                .page(page, new QueryWrapper<CommunityCommentReply>()
-                        .eq("comment_id", commentId).and((wrapper) -> {
-                            wrapper.ne("is_delete", true).or().isNull("is_delete");
-                        })
-                        .orderByDesc("create_time")).getRecords();
+        List<CommunityCommentReply> replies = communityCommentReplyService.page(page, new QueryWrapper<CommunityCommentReply>().eq("comment_id", commentId).and((wrapper) -> {
+            wrapper.ne("is_delete", true).or().isNull("is_delete");
+        }).orderByDesc("create_time")).getRecords();
         CommunityCommentReplyVo replyVo = null;
         List<CommunityCommentReplyVo> replyVos = new ArrayList<>();
         for (final CommunityCommentReply reply : replies) {
@@ -144,10 +143,8 @@ public class CommunityCommentController extends BaseController {
             SysUser sysUser = sysUserService.selectUserById(reply.getUserId());
             final Long parentReplyId = reply.getParentReplyId();
             BeanUtils.copyProperties(reply, replyVo);
-            if (Objects.nonNull(parentReplyId)) {
-                CommunityCommentReply communityCommentReply = replies.stream()
-                        .filter(item -> item.getId().equals(parentReplyId))
-                        .collect(Collectors.toList()).get(0);
+            if (Objects.nonNull(parentReplyId) && !commentId.equalsIgnoreCase(Long.toString(parentReplyId))) {
+                CommunityCommentReply communityCommentReply = replies.stream().filter(item -> item.getId().equals(parentReplyId)).collect(Collectors.toList()).get(0);
                 SysUser sysUser1 = sysUserService.selectUserById(communityCommentReply.getUserId());
                 replyVo.setParentNickName(sysUser1.getNickName());
                 replyVo.setParentUserId(sysUser1.getUserId());
@@ -155,6 +152,12 @@ public class CommunityCommentController extends BaseController {
             }
             replyVo.setNickName(sysUser.getNickName());
             replyVo.setAvatar(sysUser.getAvatar());
+            replyVo.setReply(true);
+            //设置点赞数量和当前登录用户是否已点赞
+            List<CommunityCommentLike> commentLikes = communityCommentLikeMapper.selectList(new QueryWrapper<CommunityCommentLike>()
+                    .eq("reply_id", replyVo.getId()));
+            replyVo.setLikeCount(commentLikes.size());
+            replyVo.setLike(commentLikes.stream().anyMatch(item -> SecurityUtils.getUserId().equals(item.getUserId())));
             replyVos.add(replyVo);
         }
         return AjaxResult.success(replyVos);
@@ -169,8 +172,7 @@ public class CommunityCommentController extends BaseController {
     @Transactional
     //@Anonymous
     public AjaxResult reply(@RequestBody CommunityCommentReply communityCommentReply) {
-        if (Objects.isNull(communityCommentReply.getCommentId())
-                || Objects.isNull(communityCommentReply.getContent())) {
+        if (Objects.isNull(communityCommentReply.getCommentId()) || Objects.isNull(communityCommentReply.getContent())) {
             return AjaxResult.error("参数异常!");
         }
 
@@ -195,11 +197,9 @@ public class CommunityCommentController extends BaseController {
             return AjaxResult.error("参数异常!");
         }
 
-        CommunityCommentReply communityCommentReply = communityCommentReplyService.getOne(new QueryWrapper<CommunityCommentReply>()
-                .eq("id", replyId)
-                .and((wrapper) -> {
-                    wrapper.ne("is_delete", true).or().isNull("is_delete");
-                }));
+        CommunityCommentReply communityCommentReply = communityCommentReplyService.getOne(new QueryWrapper<CommunityCommentReply>().eq("id", replyId).and((wrapper) -> {
+            wrapper.ne("is_delete", true).or().isNull("is_delete");
+        }));
         if (Objects.isNull(communityCommentReply)) {
             return AjaxResult.error("未找到该回复!");
         }

+ 12 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/domain/Community/CommunityCommentLike.java

@@ -1,5 +1,6 @@
 package com.ruoyi.generator.domain.Community;
 
+import com.baomidou.mybatisplus.annotation.TableField;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
@@ -33,11 +34,22 @@ public class CommunityCommentLike implements Serializable {
     */
     @ApiModelProperty("评价id")
     private Long commentId;
+    /**
+     * 回复id
+     */
+    @ApiModelProperty("回复id")
+    private Long replyId;
     /**
     * 点赞用户id
     */
     @ApiModelProperty("点赞用户id")
     private Long userId;
+    /**
+     * 是否是回复
+     */
+    @ApiModelProperty("是否是回复")
+    @TableField(exist = false)
+    private boolean isReply;
     /**
     * 创建人
     */

+ 6 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/vo/CommunityArticleCommentVo.java

@@ -86,4 +86,10 @@ public class CommunityArticleCommentVo extends BaseEntity implements Serializabl
      */
     @ApiModelProperty("评论下回复剩余总数")
     private int replyCount;
+
+    /**
+     * 是否是回复
+     */
+    @ApiModelProperty("是否是回复")
+    private boolean isReply;
 }

+ 15 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/vo/CommunityCommentReplyVo.java

@@ -70,6 +70,21 @@ public class CommunityCommentReplyVo implements Serializable {
      */
     @ApiModelProperty("用户头像")
     private String avatar;
+    /**
+     * 是否是回复
+     */
+    @ApiModelProperty("是否是回复")
+    private boolean isReply;
+    /**
+     * 点赞数量
+     */
+    @ApiModelProperty("点赞数量")
+    private int likeCount;
+    /**
+     * 当前用户是否点赞
+     */
+    @ApiModelProperty("当前用户是否点赞")
+    private boolean isLike;
     /**
     * 回复内容
     */