private List getTreeDataLoop(long l) {
//返回数据 一级类型,下面挂了子子孙孙类型
List result = new ArrayList<>();
//1 获取所有的类型
List productTypes = productTypeMapper.selectList(null);
//2)遍历所有的类型
Map<Long,ProductType> productTypesDto = new HashMap<>();
for (ProductType productType : productTypes) {
productTypesDto.put(productType.getId(), productType);
}
for (ProductType productType : productTypes) {
Long pid = productType.getPid();
// ①如果没有父亲就是一级类型 放入返回列表中
if (pid.longValue() == 0){
result.add(productType);
}else{
//方案2:通过Map建立id和类型直接关系,以后通过pid直接获取父亲 10+10
ProductType parent = productTypesDto.get(pid);
parent.getChildren().add(productType);
}
}
return result;
}