获取指定分类的上级分类
获取所有的指定分类的上级分类,不包含自己
public List<LecturerClassification> findParent(LecturerClassification lecturerClassification, List<LecturerClassification> parentList) {
LecturerClassification hhyContentTypeTree = lecturerClassificationRepository.findByIdAndDeletedIsFalse(lecturerClassification.getPid());
if (!ObjectUtils.isEmpty(hhyContentTypeTree)) {
parentList.add(hhyContentTypeTree);
if (hhyContentTypeTree.getPid() != 0) {
findParent(hhyContentTypeTree, parentList);
}
}
return parentList;
}
lecturerClassification 传入想获取上级的分类
parentList 一个空的list<指定的分类类型> 用于返回
获取指定分类的下级分类
获取所有的指定分类的下级分类,不包含自己
private List<LecturerClassification> findChildren(LecturerClassification hhyContentType,List<LecturerClassification> childrenList) {
List<LecturerClassification> hhyContentTypeTree = lecturerClassificationRepository.findOneByPidAndDeletedIsFalse(hhyContentType.getId());
if (!org.springframework.util.ObjectUtils.isEmpty(hhyContentTypeTree)) {
childrenList.addAll(hhyContentTypeTree);
hhyContentTypeTree.forEach(hhyContentType1 ->{
findChildren(hhyContentType1, childrenList);
});
}
return childrenList;
}
hhyContentType 传入想获取上级的分类
childrenList 一个空的list<指定的分类类型> 用于返回
获取树状结构
获取指定list的树状机构,赋予层级
private List<HhyLecturerClassificationVOV2> treeV2(List<LecturerClassification> hhyContentTypeList) {
List<HhyLecturerClassificationVOV2> hhyContentTypeAOList = new ArrayList<>();
int i = 1;
List<Long> collect = hhyContentTypeList.stream().map(LecturerClassification::getPid).collect(Collectors.toList());
Long min = ObjectUtils.isEmpty(collect) ? 0L : Collections.min(collect);
hhyContentTypeList.stream().filter(hhyContentType -> hhyContentType.getPid() == min)
.map(menu -> {
HhyLecturerClassificationVOV2 hhyContentTypeAO = new HhyLecturerClassificationVOV2();
BeanUtils.copyProperties(menu,hhyContentTypeAO);
hhyContentTypeAO.setLevel(i); //层级
List<HhyLecturerClassificationVOV2> hhyContentTypeAOChildren = setChildrenV2(hhyContentTypeAO, hhyContentTypeList, i);
hhyContentTypeAO.setChildrenContentType(hhyContentTypeAOChildren);
hhyContentTypeAOList.add(hhyContentTypeAO); //子集
return menu;
}).sorted(Comparator.comparing(LecturerClassification::getSort)).collect(Collectors.toList());
return hhyContentTypeAOList;
}
private List<HhyLecturerClassificationVOV2> setChildrenV2(HhyLecturerClassificationVOV2 hhyContentTypeAO,
List<LecturerClassification> all,
int level) {
List<HhyLecturerClassificationVOV2> hhyContentTypeAOList = new ArrayList<>();
int i = level + 1;
all.stream().filter(hhyContentType -> Objects.equals(hhyContentType.getPid(), hhyContentTypeAO.getId()))
.map(menu -> {
HhyLecturerClassificationVOV2 hhyContentTypeAO1 = new HhyLecturerClassificationVOV2();
BeanUtils.copyProperties(menu,hhyContentTypeAO1);
hhyContentTypeAO1.setLevel(i);
List<HhyLecturerClassificationVOV2> hhyContentTypeAOChildren = setChildren(hhyContentTypeAO1, all, i);
hhyContentTypeAO1.setChildrenContentType(hhyContentTypeAOChildren);
hhyContentTypeAOList.add(hhyContentTypeAO1);
return menu;
}).sorted(Comparator.comparing(LecturerClassification::getSort)).collect(Collectors.toList());
return hhyContentTypeAOList;
}
1.treeV2 的传入值
hhyContentTypeList 向组成树状形式的所有分类的lsit集合
2.setChildrenV2 的传入值
hhyContentTypeAO 传入的某个父类
all 向组成树状形式的所有分类的lsit集合
int 层级
获取树状图的层级关系
获取指定树状图的层级数List
// 获取树状图的层级关系
private List<Integer> maxDeep(List<HhyLecturerClassificationVOV2> tree, List<Integer> list) {
if (org.springframework.util.ObjectUtils.isEmpty(tree)) {
return null;
}
for (HhyLecturerClassificationVOV2 hhyContentTypeAO : tree) {
list.add(hhyContentTypeAO.getLevel());
maxDeep(hhyContentTypeAO.getChildrenContentType(),list);
}
return list;
}
tree 指定的树状图
list 用于返回层级lsit