java中收集父节点下的所有子节点
递归算法,收集父节点下所有的子节点数据
/**
* 收集子节点ID
*
* @param categoryId 类别id
* @return {@link Set}<{@link Long}>
*/
public Set<Long> selectCategoryAndChildrenById(Long categoryId) {
Set<Long> categorySet = Sets.newHashSet();
findChildCategory(categorySet, categoryId);
Set<Long> categoryIdList = Sets.newHashSet();
if (Func.isNotEmpty(categoryId)) {
for (Long id : categorySet) {
categoryIdList.add(id);
}
}
return categoryIdList;
}
/**
* 递归算法,算出子节点
*
* @param categorySet 类别设置
* @param categoryId 类别id
* @return {@link Set}<{@link Long}>
*/
private Set<Long> findChildCategory(Set<Long> categorySet, Long categoryId) {
ConquasInspectionType inspectionType = this.getById(categoryId);
if (Func.isNotEmpty(inspectionType)) {
categorySet.add(inspectionType.getId());
}
QueryWrapper<ConquasInspectionType> conquasInspectionTypeQueryWrapper = new QueryWrapper<>();
conquasInspectionTypeQueryWrapper.eq("parent_id", categoryId);
List<ConquasInspectionType> inspectionTypeList = this.list(conquasInspectionTypeQueryWrapper);
for (ConquasInspectionType conquasInspectionType : inspectionTypeList) {
findChildCategory(categorySet, conquasInspectionType.getId());
}
return categorySet;
}