表头
工具类
public class ThreeUtils {
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));
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) {
List<T> ancestralThreeList = menuList.stream().filter(m -> StringUtils.left(String.valueOf(m.getId()), String.valueOf(three.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;
}
e.setId(three.getId());
e.setName(three.getName());
e.setParentId(three.getParentId());
List<E> itemList = threeResult.getItemList();
if (null == itemList) {
itemList = new ArrayList<>();
}
itemList.add(e);
threeResult.setItemList(itemList);
treeListAncestral(menuList, e, three.getId(), dataTreating);
}
}
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));
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) {
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;
}
e.setId(three.getId());
e.setName(three.getName());
e.setParentId(three.getParentId());
List<E> itemList = threeResult.getItemList();
if (null == itemList) {
itemList = new ArrayList<>();
}
itemList.add(e);
threeResult.setItemList(itemList);
treeListAncestral(menuList, e, three.getId(), dataTreating);
}
}
}
树处理函数接口
@FunctionalInterface
public interface ThreeFunctional<T, E> {
E dataTreating(List<T> threeList, E data);
}
树实体类
@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;
}
请求响应实体类
@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;
}
调用
List<CoveredCountyEnteringDTO> questionnaireList = cniQuestionnaireMapper.selectByAreaCode(reportFormsDTO);
CoveredCountyEnteringVO countyEnteringVO = new CoveredCountyEnteringVO();
ThreeUtils.treeListAncestral(questionnaireList, countyEnteringVO, reportFormsDTO.getAreaCode(), (threeList, data) -> {
if (ObjectUtil.isEmpty(threeList)) {
return null;
}
threeList = threeList.stream().filter(three -> null != three.getCniQuestionnaireId()).collect(Collectors.toList());
Long monitorBabyNumber = threeList.stream().count();
Stream<CoveredCountyEnteringDTO> standardBabyNumber = threeList.stream().filter(coveredCountyEnteringDTO -> null != coveredCountyEnteringDTO
&& StringUtils.isNotBlank(coveredCountyEnteringDTO.getMedicalQuestion1())
&& StringUtils.isNotBlank(coveredCountyEnteringDTO.getMedicalQuestion3())
&& StringUtils.isNotBlank(coveredCountyEnteringDTO.getMedicalQuestion7()));
CoveredCountyEnteringVO resultCoveredCountVO = new CoveredCountyEnteringVO();
resultCoveredCountVO.setMonitorBabyNumber(null != monitorBabyNumber ? Integer.valueOf(monitorBabyNumber + "") : 0);
resultCoveredCountVO.setStandardBabyNumber(ObjectUtil.isNotEmpty(standardBabyNumber) ? Integer.valueOf(standardBabyNumber.count() + "") : 0);
resultCoveredCountVO.setPercentOfPass(BigDecimalUtils.rate(resultCoveredCountVO.getStandardBabyNumber() + "", resultCoveredCountVO.getMonitorBabyNumber() + ""));
return resultCoveredCountVO;
});