package com.mzl.flower.utils;
|
|
import com.mzl.flower.dto.response.film.CommentDTO;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.collections4.CollectionUtils;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.stream.Collectors;
|
|
/**
|
* 用于树转集合,集合转树场景
|
*
|
* @author fanghaowei
|
* @date
|
*/
|
@Slf4j
|
public class CommentTreeUtils {
|
/**
|
* 集合转树
|
*
|
* @param commentDTOS
|
* @return
|
*/
|
public static List<CommentDTO> toTree(List<CommentDTO> commentDTOS) {
|
Map<Integer, CommentDTO> commentDTOMap = commentDTOS.stream().collect(Collectors.toMap(CommentDTO::getId, e -> e));
|
List<CommentDTO> root = new ArrayList<>();
|
for (CommentDTO dto : commentDTOS) {
|
Integer parentId = dto.getParentId();
|
// 是根评论
|
if (parentId == 0) {
|
// 设置评论深度
|
dto.setDepth(0);
|
root.add(dto);
|
} else {
|
CommentDTO parent = commentDTOMap.get(parentId);
|
// 跳过子级无父级的评论
|
if (parent == null) {
|
continue;
|
}
|
List<CommentDTO> children = CollectionUtils.isEmpty(parent.getChild()) ? new ArrayList<>() : parent.getChild();
|
// 设置评论深度
|
dto.setDepth(parent.getDepth() + 1);
|
children.add(dto);
|
parent.setChild(children);
|
}
|
}
|
return root;
|
}
|
|
|
}
|