评论排序、分页

This commit is contained in:
wuchunlei
2024-11-28 11:09:42 +08:00
parent 6e04aafa7a
commit 041005360a

View File

@@ -1,6 +1,7 @@
package com.peanut.modules.bookAbroad.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.common.utils.R;
import com.peanut.common.utils.ShiroUtils;
import com.peanut.modules.bookAbroad.service.BookAbroadCommentLikeService;
@@ -41,23 +42,30 @@ public class BookAbroadController {
//评论树
@RequestMapping("/getBookAbroadCommentTree")
public R getBookAbroadCommentTree(@RequestBody Map<String,Object> params) {
int current = Integer.parseInt(params.get("current").toString())-1;
int limit = Integer.parseInt(params.get("limit").toString());
List<BookAbroadComment> comments = bookAbroadCommentService.list(new LambdaQueryWrapper<BookAbroadComment>()
.eq(BookAbroadComment::getBookId,params.get("bookId")));
List<BookAbroadComment> commentsTree = comments.stream().filter((bookAbroadComment) ->
bookAbroadComment.getPid() == 0
).map((comment)->{
comment.setIsLike(bookAbroadCommentLikeService.count(new LambdaQueryWrapper<BookAbroadCommentLike>()
.eq(BookAbroadCommentLike::getCommentId,comment.getId())
.eq(BookAbroadCommentLike::getUserId,ShiroUtils.getUId())));
comment.setLikeCount(bookAbroadCommentLikeService.count(new LambdaQueryWrapper<BookAbroadCommentLike>()
.eq(BookAbroadCommentLike::getCommentId,comment.getId())));
comment.setUserEntity(userService.getOne(new LambdaQueryWrapper<MyUserEntity>()
.select(MyUserEntity::getId,MyUserEntity::getName,MyUserEntity::getNickname,MyUserEntity::getAvatar)
.eq(MyUserEntity::getId,comment.getUserId())));
comment.setChildren(getLabelChildrens(comment,comments));
return comment;
}).collect(Collectors.toList());
return R.ok().put("commentsTree",commentsTree);
.eq(BookAbroadComment::getBookId,params.get("bookId"))
.orderByDesc(BookAbroadComment::getId));
int commentsCount = comments.stream().filter((bookAbroadComment) -> bookAbroadComment.getPid() == 0).collect(Collectors.toList()).size();
List<BookAbroadComment> commentsTree = comments.stream()
.filter((bookAbroadComment) -> bookAbroadComment.getPid() == 0)
.map((comment)->{
comment.setIsLike(bookAbroadCommentLikeService.count(new LambdaQueryWrapper<BookAbroadCommentLike>()
.eq(BookAbroadCommentLike::getCommentId,comment.getId())
.eq(BookAbroadCommentLike::getUserId,ShiroUtils.getUId())));
comment.setLikeCount(bookAbroadCommentLikeService.count(new LambdaQueryWrapper<BookAbroadCommentLike>()
.eq(BookAbroadCommentLike::getCommentId,comment.getId())));
comment.setUserEntity(userService.getOne(new LambdaQueryWrapper<MyUserEntity>()
.select(MyUserEntity::getId,MyUserEntity::getName,MyUserEntity::getNickname,MyUserEntity::getAvatar)
.eq(MyUserEntity::getId,comment.getUserId())));
comment.setChildren(getLabelChildrens(comment,comments));
return comment; })
.sorted((comment1,comment2)->{
return comment2.getLikeCount() - comment1.getLikeCount();})
.collect(Collectors.toList())
.subList(current*limit,((current*limit)+limit)<commentsCount?((current*limit)+limit):commentsCount);
return R.ok().put("commentsCount",commentsCount).put("commentsTree",commentsTree);
}
private List<BookAbroadComment> getLabelChildrens(BookAbroadComment root,List<BookAbroadComment> all){
@@ -67,6 +75,8 @@ public class BookAbroadController {
bookAbroadComment.setIsLike(bookAbroadCommentLikeService.count(new LambdaQueryWrapper<BookAbroadCommentLike>()
.eq(BookAbroadCommentLike::getCommentId,bookAbroadComment.getId())
.eq(BookAbroadCommentLike::getUserId,ShiroUtils.getUId())));
bookAbroadComment.setLikeCount(bookAbroadCommentLikeService.count(new LambdaQueryWrapper<BookAbroadCommentLike>()
.eq(BookAbroadCommentLike::getCommentId,bookAbroadComment.getId())));
bookAbroadComment.setUserEntity(userService.getOne(new LambdaQueryWrapper<MyUserEntity>()
.select(MyUserEntity::getId,MyUserEntity::getName,MyUserEntity::getNickname,MyUserEntity::getAvatar)
.eq(MyUserEntity::getId,bookAbroadComment.getUserId())));
@@ -97,7 +107,9 @@ public class BookAbroadController {
//取消点赞
@RequestMapping("/delBookAbroadCommentLike")
public R delBookAbroadCommentLike(@RequestBody Map<String,Object> params){
bookAbroadCommentLikeService.removeById(params.get("commentLikeId").toString());
bookAbroadCommentLikeService.remove(new LambdaQueryWrapper<BookAbroadCommentLike>()
.eq(BookAbroadCommentLike::getCommentId,params.get("commentId"))
.eq(BookAbroadCommentLike::getUserId,ShiroUtils.getUId()));
return R.ok();
}