public class ThreeUtils {
/**
* 树结构转换处理-每次递归查询全部下级以及下级的子集
*
* @param menuList 需要处理的数据集
* @param threeResult 返回对象
* @param parentId 父级ID
* @param dataTreating 逻辑处理
* @param <T>
*/
public static <T extends Three, E extends Three> void treeListAncestral(List<T> menuList, E threeResult, Long parentId, ThreeFunctional<T, E> dataTreating) {
if (ObjectUtil.isEmpty(menuList)) {
return;
}
Map<Long, List<T>> collect = menuList.stream().filter(menu -> menu != null && menu.getParentId() != null).collect(Collectors.groupingBy(Three::getParentId));
//根据父级ID查询下级
List<T> threes = collect.get(parentId);
if (ObjectUtil.isEmpty(threes)) {
return;
}
ArrayList<T> groupT = threes.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(T::getId))), ArrayList::new));
for (T three : groupT) {
/*根据本次循环ID查询所有下级以及子集*/
List<T> ancestralThreeList = menuList.stream().filter(m -> StringUtil.leftPad(String.valueOf(m.getId()), String.valueOf(m.getId()).length()).equals(String.valueOf(three.getId()))).collect(Collectors.toList());
if (ObjectUtil.isEmpty(ancestralThreeList)) {
continue;
}
//树处理逻辑
E e = dataTreating.dataTreating(ancestralThreeList, threeResult);
if (null == e) {
return;
}
/*创建下级返回对象类,并且存入当前上级的下级集合中*/
List<E> itemList = threeResult.getItemList();
if (null == itemList) {
itemList = new ArrayList<>();
}
itemList.add(e);
threeResult.setItemList(itemList);
//执行递归处理
treeListAncestral(menuList, e, three.getId(), dataTreating);
}
}
/**
* 树结构转换处理-每次递归查询下级
*
* @param menuList 树处理集合
* @param threeResult 返回对象
* @param parentId 父级ID
* @param dataTreating 逻辑处理
* @param <T>
*/
public static <T extends Three, E extends Three> void treeList(List<T> menuList, E threeResult, Long parentId, ThreeFunctional<T, E> dataTreating) {
if (ObjectUtil.isEmpty(menuList)) {
return;
}
Map<Long, List<T>> collect = menuList.stream().filter(menu -> menu != null && menu.getParentId() != null).collect(Collectors.groupingBy(Three::getParentId));
//根据父级ID查询下级
List<T> threes = collect.get(parentId);
if (ObjectUtil.isEmpty(threes)) {
return;
}
ArrayList<T> groupT = threes.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(T::getId))), ArrayList::new));
for (T three : groupT) {
/*根据本次循环ID查询所有下级*/
List<T> ancestralThreeList = menuList.stream().filter(menu -> menu != null && menu.getParentId() != null && menu.getParentId().equals(three.getId())).collect(Collectors.toList());
if (ObjectUtil.isEmpty(ancestralThreeList)) {
continue;
}
//树处理逻辑
E e = dataTreating.dataTreating(ancestralThreeList, threeResult);
if (null == e) {
return;
}
/*创建下级返回对象类,并且存入当前上级的下级集合中*/
List<E> itemList = threeResult.getItemList();
if (null == itemList) {
itemList = new ArrayList<>();
}
itemList.add(e);
threeResult.setItemList(itemList);
//执行递归处理
treeList(menuList, e, three.getId(), dataTreating);
}
}
}
树处理函数接口
java复制代码
@FunctionalInterface
public interface ThreeFunctional<T, E> {
/**
* 树处理逻辑
*
* @param threeList 处理后的树结构
* @param data 处理后的树类结果对象
* 这里返回创建一个新的空对象即可
*/
E dataTreating(List<T> threeList, E data);
}
树实体类
java复制代码
@Data
public class Three<E> implements Serializable {
private static final long serialVersionUID = 2883288479291688378L;
private Long id;
private String name;
private Long parentId;
//数据结果集
private List<E> ItemList;
}
请求响应实体类
java复制代码
@Data
@ApiModel("返回VO")
public class CoveredCountyEnteringVO extends Three<CoveredCountyEnteringVO> implements Serializable {
private static final long serialVersionUID = -1571960669989120842L;
private Integer monitorBabyNumber;
private Integer standardBabyNumber;
private BigDecimal percentOfPass;
}
@Data
@ApiModel("响应DTO")
public class CoveredCountyEnteringDTO extends Three<CoveredCountyEnteringDTO> implements Serializable {
private static final long serialVersionUID = -1571960669989120842L;
private String medicalQuestion1;
private String medicalQuestion3;
private String medicalQuestion7;
}