/**
* 分页查询评论数据
* @param current
* @param limit
* @param postId
* @param type 0 只看楼主
* 1 正序 楼主创建时间早的在前面
* @return
*/
@Override
public Page<PostRemark> getRemarkPage(Long current, Long limit, String postId, Integer type) {
Page<PostRemark> page=new Page<>(current,limit);
if(type==1){
//全部帖子集合
LambdaQueryWrapper<PostRemark> queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.eq(PostRemark::getPostId,postId);
queryWrapper.orderByDesc(PostRemark::getGmtCreate);
List<PostRemark> currentPostlists = this.list(queryWrapper);
//封装子集
int a = 0;
int b = 0;
for (PostRemark one:currentPostlists){
//存值的变量
String currentID=one.getId();
a++;
System.out.println("当前下标"+a+"->当前id"+currentID);
while(!this.getById(currentID).getParentId().equals("0")){
//找父类
PostRemark currentRemark = this.getById(currentID);
PostRemark currentParentRemark = this.getById(currentRemark.getParentId());
if(currentParentRemark!=null){
//父类添加孩子
if(currentParentRemark.getChildren()==null){
List<PostRemark> children = new ArrayList<>();
currentParentRemark.setChildren(children);
}
List<PostRemark> children = currentParentRemark.getChildren();
children.add(currentRemark);
currentParentRemark.setChildren(children);
//当前父亲再变成孩子
currentID=currentParentRemark.getId();
b++;
System.out.println("子级下标:"+b);
}else {
break;
}
}
}
//返回数据返回一级评论集合
List<PostRemark> firstRemarkLists=new ArrayList<>();
for(PostRemark first:currentPostlists){
if(first.getParentId().equals("0")){
firstRemarkLists.add(first);
}
}
return page.setRecords(firstRemarkLists);
}else if(type==0){
//全部帖子集合
LambdaQueryWrapper<PostRemark> queryWrapper=new LambdaQueryWrapper<>();
Post byId = postService.getById(postId);
queryWrapper.eq(PostRemark::getPostId,postId);
queryWrapper.orderByDesc(PostRemark::getGmtCreate);
//queryWrapper.eq(PostRemark::getCreatorId,byId.getCreatedBy());
List<PostRemark> currentPostlists = this.list(queryWrapper);
//封装子集
for (PostRemark one:currentPostlists){
//存值的变量
PostRemark temp;
temp=one;
while(temp.getParentId()!="0"){
//找父类
PostRemark currentParentRemark = this.getById(temp.getParentId());
//孩子集合
List<PostRemark> childs=new ArrayList<>();
childs.add(temp);
//父类找孩子
currentParentRemark.setChildren(childs);
//当前父亲再变成孩子
temp=currentParentRemark;
}
}
//返回数据返回一级评论集合
List<PostRemark> firstRemarkLists=new ArrayList<>();
for(PostRemark first:currentPostlists){
if(first.getParentId().equals("0")){
firstRemarkLists.add(first);
}
}
return page.setRecords(firstRemarkLists);
}
throw new NktException(404,"参数检验不合格");
}