public Map<String, Object> findAll(HttpServletRequest request) {
Map<String, Object> rs = new HashMap<String, Object>();
List<Map<String, Object>> articleTypeList = articleTypeDao.findAllMap();
List<Map<String, Object>> loadChildArticleType = this.loadChildArticleType(articleTypeList, 1);
return rs;
}
/**
* 递归聚合
*/
private List<Map<String, Object>> loadChildArticleType(List<Map<String, Object>> allList, int level) {
if (allList == null || allList.isEmpty() || level == 0) {
return allList;
}
List<Map<String, Object>> mainList = new ArrayList<>();
List<Map<String, Object>> childList = new ArrayList<>();
for (Map<String, Object> item : allList) {
if (toInteger(item.get("level")) == level) {
mainList.add(item);
} else if (toInteger(item.get("level")) == level + 1) {
childList.add(item);
}
}
allList.removeAll(mainList);
if (!childList.isEmpty()) {
childList = this.loadChildArticleType(allList, toInteger(childList.get(0).get("level")));
}
for (Map<String, Object> main : mainList) {
for (Map<String, Object> child : childList) {
if (child.get("upperId").equals(main.get("id"))) {
List<Map<String, Object>> typeList;
if (main.get("childTypeList") == null) {
typeList = new ArrayList<>();
} else {
typeList = (List<Map<String, Object>>) main.get("childTypeList");
}
typeList.add(child);
main.put("childTypeList", typeList);
}
}
}
return mainList;
}
实体字段
private Long id; 主键
private Long upperId; 上级id
private int level; 第几极