Browse Source

新增用户评价或者文章被回复的信息

fangzhen 6 months ago
parent
commit
caaaee7891

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

@@ -1,6 +1,7 @@
 package com.ruoyi.generator.controller;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
@@ -94,14 +95,16 @@ public class CommunityCommentController extends BaseController {
         List<CommunityArticleCommentVo> commentVos = new ArrayList<>();
         try {
             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;
 
             for (CommunityArticleComment record : records) {
-                if (record.getAddress() == null){
+                if (record.getAddress() == null) {
                     record.setAddress("未知");
                 }
 
@@ -111,6 +114,7 @@ public class CommunityCommentController extends BaseController {
             }
 
             Long userId = SecurityUtils.getUserId();
+            List<Long> commentIds = new ArrayList<>();
             for (CommunityArticleCommentVo vo : commentVos) {
                 Long commentUserId = vo.getUserId();
                 //获取评论的用户信息
@@ -118,14 +122,25 @@ 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());
+                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"));
+
+                //如果评价用户id与当前登录用户id相符,则标记为已读
+                if (vo.getUserId().equals(userId) && !vo.isRead()) {
+                    commentIds.add(vo.getId());
+                }
 
                 CommunityCommentReplyVo replyVo = null;
                 List<CommunityCommentReplyVo> replyVos = new ArrayList<>();
@@ -148,13 +163,16 @@ public class CommunityCommentController extends BaseController {
                 vo.setReplyCount(replies.isEmpty() ? 0 : replies.size() - 1);
                 vo.setReply(false);
             }
+
+            //更新为已读
+            if (!commentIds.isEmpty()) {
+                communityArticleCommentService.update(new UpdateWrapper<CommunityArticleComment>().set("is_read", true).in("id", commentIds));
+            }
         } catch (Exception e) {
             System.out.println(e.getMessage());
             throw new ProjectException();
         }
 
-
-
         return AjaxResult.success(commentVos);
     }
 
@@ -171,22 +189,42 @@ public class CommunityCommentController extends BaseController {
         }
         List<CommunityCommentReplyVo> replyVos = new ArrayList<>();
         try {
+            Long userId = SecurityUtils.getUserId();
             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");
-            }).orderByAsc("create_time")).getRecords();
-            CommunityCommentReplyVo replyVo = null;
+            List<CommunityCommentReply> replies = communityCommentReplyService.page(page, new QueryWrapper<CommunityCommentReply>()
+                    .eq("comment_id", commentId)
+                    .and((wrapper) -> {
+                        wrapper.ne("is_delete", true)
+                                .or()
+                                .isNull("is_delete");
+                    }).orderByAsc("create_time")).getRecords();
 
+            CommunityCommentReplyVo replyVo = null;
+            List<Long> replyIds = new ArrayList<>();
             for (final CommunityCommentReply reply : replies) {
                 replyVo = new CommunityCommentReplyVo();
-                if (reply.getAddress() == null){
+                if (reply.getAddress() == null) {
                     reply.setAddress("未知");
                 }
+
+                //标记当前登录的用户被回复的信息
+                if (reply.getUserId().equals(userId)) {
+                    List<CommunityCommentReply> communityCommentReplies = replies.stream()
+                            .filter(item -> item.getParentReplyId().equals(reply.getId()))
+                            .collect(Collectors.toList());
+                    for (CommunityCommentReply communityCommentReply : communityCommentReplies) {
+                        replyIds.add(communityCommentReply.getId());
+                    }
+                }
+
                 SysUser sysUser = sysUserService.selectUserById(reply.getUserId());
                 final Long parentReplyId = reply.getParentReplyId();
                 BeanUtils.copyProperties(reply, replyVo);
                 if (Objects.nonNull(parentReplyId) && !commentId.equalsIgnoreCase(Long.toString(parentReplyId))) {
-                    CommunityCommentReply communityCommentReply = replies.stream().filter(item -> item.getId().equals(parentReplyId)).collect(Collectors.toList()).get(0);
+                    List<CommunityCommentReply> communityCommentReplies = replies.stream()
+                            .filter(item -> item.getId().equals(parentReplyId))
+                            .collect(Collectors.toList());
+                    CommunityCommentReply communityCommentReply = communityCommentReplies.get(0);
                     SysUser sysUser1 = sysUserService.selectUserById(communityCommentReply.getUserId());
                     replyVo.setParentNickName(sysUser1.getNickName());
                     replyVo.setParentUserId(sysUser1.getUserId());
@@ -200,16 +238,19 @@ public class CommunityCommentController extends BaseController {
                         .eq("reply_id", replyVo.getId()));
                 replyVo.setLikeCount(commentLikes.size());
                 replyVo.setLike(commentLikes.stream().anyMatch(item -> SecurityUtils.getUserId().equals(item.getUserId())));
-
-
                 replyVos.add(replyVo);
             }
+
+            //更新回复的id为已读
+            if (!replyIds.isEmpty()) {
+                communityCommentReplyService.update(new UpdateWrapper<CommunityCommentReply>()
+                        .set("is_read", true)
+                        .in("id", replyIds));
+            }
         } catch (Exception e) {
             System.out.println(e.getMessage());
             throw new ProjectException();
         }
-
-
         return AjaxResult.success(replyVos);
     }
 
@@ -294,9 +335,8 @@ public class CommunityCommentController extends BaseController {
 
 
     /**
-     *
-     * @param articleId
-     * @return
+     * @param articleId 文章id
+     * @return 抽奖结果
      */
     @ApiOperation("评论抽奖")
     @GetMapping("/raffleComment")
@@ -313,13 +353,13 @@ public class CommunityCommentController extends BaseController {
                 .and((wrapper) -> {
                     wrapper.ne("is_delete", true).or().isNull("is_delete");
                 }));
-        if (communityArticle == null){
-            return  AjaxResult.error("非本人无法操作抽奖按钮!");
+        if (communityArticle == null) {
+            return AjaxResult.error("非本人无法操作抽奖按钮!");
         }
 
         CommunityCommentRaffleVo communityCommentRaffleVos = null;
         try {
-           communityCommentRaffleVos = communityArticleService.selectCommentRaffle(articleId);
+            communityCommentRaffleVos = communityArticleService.selectCommentRaffle(articleId);
 
         } catch (Exception e) {
             System.out.println(e.getMessage());
@@ -333,7 +373,7 @@ public class CommunityCommentController extends BaseController {
     @GetMapping("/queryComment")
     @Transactional
     //@Anonymous
-    public AjaxResult queryComment(Long articleId,String comment) {
+    public AjaxResult queryComment(Long articleId, String comment) {
         int pageNum = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
         int pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
         if (Objects.isNull(articleId)) {
@@ -344,7 +384,7 @@ public class CommunityCommentController extends BaseController {
         try {
             int offset = (pageNum - 1) * pageSize;
             //获取一二级评论
-            List<CommunityArticleComment> communityArticleComments = communityArticleCommentMapper.queryComment(articleId, comment,offset,pageSize);
+            List<CommunityArticleComment> communityArticleComments = communityArticleCommentMapper.queryComment(articleId, comment, offset, pageSize);
             //一二级评论合集Vo
             CommunitySearchCommentVo commentVo = null;
             for (CommunityArticleComment communityArticleComment : communityArticleComments) {
@@ -358,28 +398,36 @@ public class CommunityCommentController extends BaseController {
                 vo.setNickName(sysUserService.selectUserById(vo.getUserId()).getNickName());
                 vo.setCommentLikeCount(communityCommentLikeMapper.selectList(new QueryWrapper<CommunityCommentLike>().eq("comment_id", vo.getId())).size());
                 //设置当前用户是否点赞
-                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());
                 //如果是二级评论则执行这个接口
-                CommunityCommentReply communityCommentReply = communityCommentReplyMapper.selectOne(new QueryWrapper<CommunityCommentReply>().eq("id", vo.getId()).and((wrapper) -> {
-                    wrapper.ne("is_delete", true).or().isNull("is_delete");
-                }));
+                CommunityCommentReply communityCommentReply = communityCommentReplyMapper.selectOne(new QueryWrapper<CommunityCommentReply>()
+                        .eq("id", vo.getId())
+                        .and((wrapper) -> {
+                            wrapper.ne("is_delete", true).or().isNull("is_delete");
+                        }));
 
                 //如果二级评论不为空执行
-                if (!Objects.isNull(communityCommentReply)){
+                if (!Objects.isNull(communityCommentReply)) {
                     //点赞数量
                     List<CommunityCommentLike> commentLikeList = communityCommentLikeMapper.selectList(new QueryWrapper<CommunityCommentLike>()
                             .eq("reply_id", communityCommentReply.getId()));
                     vo.setCommentLikeCount(commentLikeList.size());
 
                     //二级评论是否点赞
-                    List<CommunityCommentLike> commentLikesReply = communityCommentLikeMapper.selectList(new QueryWrapper<CommunityCommentLike>().eq("reply_id", vo.getId()).eq("user_id", userId));
+                    List<CommunityCommentLike> commentLikesReply = communityCommentLikeMapper.selectList(new QueryWrapper<CommunityCommentLike>()
+                            .eq("reply_id", vo.getId())
+                            .eq("user_id", userId));
                     vo.setCommentLike(!commentLikesReply.isEmpty());
 
                     //找到一级评论获取 父评论 用户头像和ID
-                    CommunityArticleComment communityArticleComment = communityArticleCommentMapper.selectOne(new QueryWrapper<CommunityArticleComment>().eq("id", communityCommentReply.getCommentId()).and((wrapper) -> {
-                        wrapper.ne("is_delete", true).or().isNull("is_delete");
-                    }));
+                    CommunityArticleComment communityArticleComment = communityArticleCommentMapper.selectOne(new QueryWrapper<CommunityArticleComment>()
+                            .eq("id", communityCommentReply.getCommentId())
+                            .and((wrapper) -> {
+                                wrapper.ne("is_delete", true).or().isNull("is_delete");
+                            }));
                     SysUser sysUser1 = sysUserService.selectUserById(communityArticleComment.getUserId());
                     vo.setParentNickName(sysUser1.getNickName());
                     vo.setParentUserId(sysUser1.getUserId());
@@ -395,4 +443,15 @@ public class CommunityCommentController extends BaseController {
         return AjaxResult.success(commentVos);
     }
 
+    /**
+     * 获取当前登录用户的评论和回复
+     *
+     * @return 评论和回复
+     */
+    @GetMapping("/getComments")
+    @ApiOperation("获取当前登录用户的评论和回复")
+    public AjaxResult getCommentInfoByUserId() {
+        Long userId = SecurityUtils.getUserId();
+        return success(communityArticleCommentService.getCommentInfoByUserId(userId));
+    }
 }

+ 31 - 15
ruoyi-generator/src/main/java/com/ruoyi/generator/domain/Community/CommunityArticleComment.java

@@ -15,9 +15,10 @@ import java.io.Serializable;
 import java.util.Date;
 
 /**
-* 文章评价记录表
-* @TableName community_article_comment
-*/
+ * 文章评价记录表
+ *
+ * @TableName community_article_comment
+ */
 @Data
 @NoArgsConstructor
 @AllArgsConstructor
@@ -25,22 +26,22 @@ import java.util.Date;
 public class CommunityArticleComment implements Serializable {
 
     /**
-    * 评论id
-    */
-    @NotNull(message="[评论id]不能为空")
+     * 评论id
+     */
+    @NotNull(message = "[评论id]不能为空")
     @ApiModelProperty("评论id")
     @TableId("id")
     @JsonSerialize(using = ToStringSerializer.class)
     private Long id;
     /**
-    * 文章id
-    */
+     * 文章id
+     */
     @ApiModelProperty("文章id")
     @JsonSerialize(using = ToStringSerializer.class)
     private Long articleId;
     /**
-    * 评论人id
-    */
+     * 评论人id
+     */
     @ApiModelProperty("评论人id")
     private Long userId;
 
@@ -50,21 +51,36 @@ public class CommunityArticleComment implements Serializable {
     @ApiModelProperty("评论内容")
     private String content;
 
-    /** 创建者 */
+    /**
+     * 创建者
+     */
     private Long createBy;
 
-    /** 创建时间 */
+    /**
+     * 创建时间
+     */
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date createTime;
 
-    /** 更新者 */
+    /**
+     * 更新者
+     */
     private Long updateBy;
 
-    /** 更新时间 */
+    /**
+     * 更新时间
+     */
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
     private Date updateTime;
 
-    /** 是否删除 */
+    /**
+     * 是否已读
+     */
+    private boolean isRead;
+
+    /**
+     * 是否删除
+     */
     private boolean isDelete;
 
     /**

+ 6 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/domain/Community/CommunityCommentReply.java

@@ -77,6 +77,12 @@ public class CommunityCommentReply implements Serializable {
     @ApiModelProperty("更新人")
     private Long updateBy;
 
+    /**
+     * 是否已读
+     */
+    @ApiModelProperty("是否已读")
+    private boolean isRead;
+
     /**
      * 是否删除
      */

+ 7 - 2
ruoyi-generator/src/main/java/com/ruoyi/generator/mapper/community/CommunityArticleCommentMapper.java

@@ -2,7 +2,7 @@ package com.ruoyi.generator.mapper.community;
 
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 import com.ruoyi.generator.domain.Community.CommunityArticleComment;
-import io.swagger.models.auth.In;
+import com.ruoyi.generator.vo.CommentVo;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
 
@@ -50,5 +50,10 @@ public interface CommunityArticleCommentMapper extends BaseMapper<CommunityArtic
 
     List<CommunityArticleComment>  queryComment  (@Param("articleId")  Long articleId,@Param("comment")  String comment,@Param("offset")  int offset, @Param("limit") int limit);
 
-
+    /**
+     * 获取当前用户文章的所有评论信息
+     * @param userId 当前用户id
+     * @return 该用户的文章信息
+     */
+    List<CommentVo> getCommentListByUserId(@Param("userId") Long userId);
 }

+ 17 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/service/CommunityArticleCommentServiceImpl.java

@@ -3,9 +3,26 @@ package com.ruoyi.generator.service;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.generator.domain.Community.CommunityArticleComment;
 import com.ruoyi.generator.mapper.community.CommunityArticleCommentMapper;
+import com.ruoyi.generator.vo.CommentVo;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
+import java.util.List;
+
 @Service
 public class CommunityArticleCommentServiceImpl extends ServiceImpl<CommunityArticleCommentMapper, CommunityArticleComment> implements ICommunityArticleCommentService {
 
+    @Resource
+    private CommunityArticleCommentMapper communityArticleCommentMapper;
+
+    /**
+     * 获取当前用户文章的所有评论信息
+     *
+     * @param userId 当前用户id
+     * @return 该用户的文章信息
+     */
+    @Override
+    public List<CommentVo> getCommentInfoByUserId(long userId) {
+        return communityArticleCommentMapper.getCommentListByUserId(userId);
+    }
 }

+ 11 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/service/ICommunityArticleCommentService.java

@@ -2,6 +2,17 @@ package com.ruoyi.generator.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.ruoyi.generator.domain.Community.CommunityArticleComment;
+import com.ruoyi.generator.vo.CommentVo;
+
+import java.util.List;
 
 public interface ICommunityArticleCommentService extends IService<CommunityArticleComment> {
+
+    /**
+     * 获取当前用户文章的所有评论信息
+     *
+     * @param userId 当前用户id
+     * @return 该用户的文章信息
+     */
+    List<CommentVo> getCommentInfoByUserId(long userId);
 }

+ 26 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/vo/CommentVo.java

@@ -0,0 +1,26 @@
+package com.ruoyi.generator.vo;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.Date;
+
+/**
+ * 当前登录用户文章下的评论信息
+ */
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+public class CommentVo {
+    private Long id;
+    private int type;
+    private Long articleId;
+    private String avatar;
+    private String nickName;
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
+    private Date createTime;
+    private String content;
+    private boolean isRead;
+}

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

@@ -88,6 +88,12 @@ public class CommunityArticleCommentVo extends BaseEntity implements Serializabl
     @ApiModelProperty("评论下回复剩余总数")
     private int replyCount;
 
+    /**
+     * 是否已读
+     */
+    @ApiModelProperty("是否已读")
+    private boolean isRead;
+
     /**
      * 是否是回复
      */

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

@@ -72,6 +72,11 @@ public class CommunityCommentReplyVo implements Serializable {
      */
     @ApiModelProperty("用户头像")
     private String avatar;
+    /**
+     * 是否是已读
+     */
+    @ApiModelProperty("是否是已读")
+    private boolean isRead;
     /**
      * 是否是回复
      */

+ 56 - 0
ruoyi-generator/src/main/resources/mapper/community/CommunityArticleCommentMapper.xml

@@ -194,4 +194,60 @@
     </select>
 
 
+    <select id="getCommentListByUserId" resultType="com.ruoyi.generator.vo.CommentVo">
+        select id,
+               article_id,
+               type,
+               avatar,
+               nick_name,
+               create_time,
+               content,
+               is_read
+        from (select cac.id,
+                     cac.article_id,
+                     '0'                              as type,
+                     (select su.avatar
+                      from sys_user su
+                      where su.user_id = cac.user_id) as avatar,
+                     (select su.nick_name
+                      from sys_user su
+                      where su.user_id = cac.user_id) as nick_name,
+                     cac.create_time,
+                     cac.content,
+                     cac.is_read
+              from community_article ca
+                       left join community_article_comment cac on
+                  ca.id = cac.article_id
+              where ca.user_id = #{userId}
+                and cac.id is not null
+                and (ca.is_delete is null
+                  or ca.is_delete != 1)
+                and (cac.is_delete is null
+                  or cac.is_delete != 1)
+
+              union all
+
+              select ccr.id                   as id,
+                     cac.article_id,
+                     '1'                              as type,
+                     (select su.avatar
+                      from sys_user su
+                      where su.user_id = ccr.user_id) as avatar,
+                     (select su.nick_name
+                      from sys_user su
+                      where su.user_id = ccr.user_id) as nick_name,
+                     ccr.create_time,
+                     ccr.content,
+                     ccr.is_read
+              from community_article_comment cac
+                       left join community_comment_reply ccr on
+                  cac.id = ccr.comment_id
+              where cac.user_id = #{userId}
+                and ccr.id is not NULL
+                and (cac.is_delete is null
+                  or cac.is_delete != 1)
+                and (ccr.is_delete is null
+                  or ccr.is_delete != 1)) as comments
+        order by create_time desc
+    </select>
 </mapper>