Ver código fonte

优化收藏列表。优化已读信息

fangqing 2 semanas atrás
pai
commit
c170144d31

+ 36 - 33
ruoyi-generator/src/main/java/com/ruoyi/generator/controller/CommunityArticleController.java

@@ -1417,22 +1417,24 @@ public class CommunityArticleController extends BaseController {
         try {
             communityArticleVos = communityArticleService.selectUserLike(userId, pageNum, pageSize, searchType);
 
-            for (CommunityArticleVo communityArticleVo : communityArticleVos) {
-                //判断当前查询用户有没有喜欢该文章
-                CommunityLike like = communityLikeMapper.selectOne(
-                        new QueryWrapper<CommunityLike>()
-                                .eq("user_id", SecurityUtils.getUserId())
-                                .eq("article_id", communityArticleVo.getId()));
-
-                communityArticleVo.setLike(!Objects.isNull(like));
-
-                //判断是否已推荐
-                CommunityArticleRecommend recommend = recommendMapper.selectOne(new QueryWrapper<CommunityArticleRecommend>()
-                        .eq("user_id", SecurityUtils.getUserId())
-                        .eq("article_id", communityArticleVo.getId()).and((wrapper) -> {
-                            wrapper.ne("is_delete", true).or().isNull("is_delete");
-                        }));
-                communityArticleVo.setRecommend(!Objects.isNull(recommend));
+            if (communityArticleVos != null) {
+                for (CommunityArticleVo communityArticleVo : communityArticleVos) {
+                    //判断当前查询用户有没有喜欢该文章
+                    CommunityLike like = communityLikeMapper.selectOne(
+                            new QueryWrapper<CommunityLike>()
+                                    .eq("user_id", SecurityUtils.getUserId())
+                                    .eq("article_id", communityArticleVo.getId()));
+
+                    communityArticleVo.setLike(!Objects.isNull(like));
+
+                    //判断是否已推荐
+                    CommunityArticleRecommend recommend = recommendMapper.selectOne(new QueryWrapper<CommunityArticleRecommend>()
+                            .eq("user_id", SecurityUtils.getUserId())
+                            .eq("article_id", communityArticleVo.getId()).and((wrapper) -> {
+                                wrapper.ne("is_delete", true).or().isNull("is_delete");
+                            }));
+                    communityArticleVo.setRecommend(!Objects.isNull(recommend));
+                }
             }
         } catch (Exception e) {
             log.info(e.getMessage());
@@ -1456,23 +1458,24 @@ public class CommunityArticleController extends BaseController {
             }
             communityArticleVos = communityArticleService.selectUserCollect(userId, pageNum, pageSize, searchType);
 
-
-            for (CommunityArticleVo communityArticleVo : communityArticleVos) {
-                //判断当前查询用户有没有喜欢该文章
-                CommunityLike like = communityLikeMapper.selectOne(
-                        new QueryWrapper<CommunityLike>()
-                                .eq("user_id", SecurityUtils.getUserId())
-                                .eq("article_id", communityArticleVo.getId()));
-
-                communityArticleVo.setLike(!Objects.isNull(like));
-
-                //判断是否已推荐
-                CommunityArticleRecommend recommend = recommendMapper.selectOne(new QueryWrapper<CommunityArticleRecommend>()
-                        .eq("user_id", SecurityUtils.getUserId())
-                        .eq("article_id", communityArticleVo.getId()).and((wrapper) -> {
-                            wrapper.ne("is_delete", true).or().isNull("is_delete");
-                        }));
-                communityArticleVo.setRecommend(!Objects.isNull(recommend));
+            if (communityArticleVos != null) {
+                for (CommunityArticleVo communityArticleVo : communityArticleVos) {
+                    //判断当前查询用户有没有喜欢该文章
+                    CommunityLike like = communityLikeMapper.selectOne(
+                            new QueryWrapper<CommunityLike>()
+                                    .eq("user_id", SecurityUtils.getUserId())
+                                    .eq("article_id", communityArticleVo.getId()));
+
+                    communityArticleVo.setLike(!Objects.isNull(like));
+
+                    //判断是否已推荐
+                    CommunityArticleRecommend recommend = recommendMapper.selectOne(new QueryWrapper<CommunityArticleRecommend>()
+                            .eq("user_id", SecurityUtils.getUserId())
+                            .eq("article_id", communityArticleVo.getId()).and((wrapper) -> {
+                                wrapper.ne("is_delete", true).or().isNull("is_delete");
+                            }));
+                    communityArticleVo.setRecommend(!Objects.isNull(recommend));
+                }
             }
         } catch (Exception e) {
             log.info(e.getMessage());

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

@@ -595,8 +595,9 @@ public class CommunityCommentController extends BaseController {
         int offset = (pageNum - 1) * pageSize;
 
         List<CommentVo> commentInfoByUserId  = new ArrayList<>();
+        List<CommentVo> pagedList  = new ArrayList<>();
         try {
-            commentInfoByUserId = communityArticleCommentService.getCommentInfoByUserId(userId, offset, pageSize, searchType);
+            commentInfoByUserId = communityArticleCommentService.getCommentInfoByUserId2(userId,searchType);
             for (CommentVo commentVo : commentInfoByUserId) {
                 String type = String.valueOf(commentVo.getType());
                 String id = String.valueOf(commentVo.getId());
@@ -604,12 +605,20 @@ public class CommunityCommentController extends BaseController {
                     communityCommentController.updateMsgRead(type,id);
                 }
             }
+
+            int toIndex = Math.min(offset + pageSize, commentInfoByUserId.size());
+            // 检查是否越界,防止异常
+            if (offset >= commentInfoByUserId.size() || offset < 0) {
+                return success(new ArrayList<>()); // 返回空列表,避免异常
+            }
+            pagedList = commentInfoByUserId.subList(offset, toIndex);
+
         }catch (Exception e) {
             //e.printStackTrace();
             System.out.println(e.getMessage());
             throw new ProjectException();
         }
-        return success(commentInfoByUserId);
+        return success(pagedList);
     }
 
     /**
@@ -627,8 +636,9 @@ public class CommunityCommentController extends BaseController {
         int offset = (pageNum - 1) * pageSize;
 
         List<RecommendAndCollectVo> recommendAndCollectVos  = new ArrayList<>();
+        List<RecommendAndCollectVo> pagedList  = new ArrayList<>();
         try {
-            recommendAndCollectVos= communityArticleCollectMapper.selectRecommendAndCollectByUserId(userId, offset, pageSize, searchType);
+            recommendAndCollectVos= communityArticleCollectMapper.selectRecommendAndCollectByUserId2(userId, searchType);
 
             for (RecommendAndCollectVo recommendAndCollectVo : recommendAndCollectVos) {
                 String type = String.valueOf(recommendAndCollectVo.getType());
@@ -637,12 +647,19 @@ public class CommunityCommentController extends BaseController {
                     communityCommentController.updateMsgRead(type,id);
                 }
             }
+            int toIndex = Math.min(offset + pageSize, recommendAndCollectVos.size());
+            // 检查是否越界,防止异常
+            if (offset >= recommendAndCollectVos.size() || offset < 0) {
+                return success(new ArrayList<>()); // 返回空列表,避免异常
+            }
+            pagedList = recommendAndCollectVos.subList(offset, toIndex);
+
         }catch (Exception e) {
             //e.printStackTrace();
             System.out.println(e.getMessage());
             throw new ProjectException();
         }
-        return success(recommendAndCollectVos);
+        return success(pagedList);
     }
 
     /**
@@ -662,15 +679,16 @@ public class CommunityCommentController extends BaseController {
         try {
             //调用这个接口 刷新已读状态
             likeVos = communityUserLikeMapper.selectLikeByUserId(userId, offset, pageSize, searchType);
-            for (LikeVo likeVo : likeVos) {
-                if (!likeVo.isRead()) {
-                    userLikeService.update(new UpdateWrapper<CommunityUserLike>()
-                            .set("is_read", true)
-                            .set("update_time", DateUtils.parseDate(DateUtils.getTime()))
-                            .set("update_by", SecurityUtils.getUserId())
-                            .eq("id", likeVo.getId()));
-                }
-            }
+
+
+            userLikeService.update(new UpdateWrapper<CommunityUserLike>()
+                    .set("is_read", true)
+                    .set("update_time", DateUtils.parseDate(DateUtils.getTime()))
+                    .set("update_by", SecurityUtils.getUserId())
+                    .eq("like_user_id",userId)
+                    .eq("is_read", false));
+
+
         }catch (Exception e) {
             //e.printStackTrace();
             System.out.println(e.getMessage());