1,java递归生成目录树 返回list<T>
递归方法返回List<T>类型
public List<ProductCategory> treeselect() {
// 获取数据库表中的所有数据
List<ProductCategory> dataList = productCategoryMapper.selectAll();
log.info("dataList--->>>" + dataList.toString());
List<ProductCategory> ProductCategoryList = new ArrayList<>();
// 先找到1级分类
for (ProductCategory ProductCategory : dataList) {
if (ProductCategory.getParentId() == 0) {
ProductCategoryList.add(ProductCategory);
}
}
log.info("ProductCategoryList--->>>" + ProductCategoryList.toString());
// 为一级菜单设置子菜单,getChild是递归调用的
for (ProductCategory productCategory : ProductCategoryList) {
productCategory.setChildren(getChilde(productCategory.getId(), dataList));
}
return ProductCategoryList;
}
递归方法获取子菜单
private List<ProductCategory> getChilde(Long id, List<ProductCategory> rootList) {
//子菜单
List<ProductCategory> childList = new ArrayList<>();
for (ProductCategory productCategory : rootList) {
// 遍历所有节点,将父菜单id与传过来的id比较
if (productCategory.getParentId().equals(id)) {
childList.add(productCategory);
}
}
// 把子菜单的子菜单再循环一遍
for (ProductCategory productCategory : childList) {
productCategory.setChildren(getChilde(productCategory.getId(), rootList));
}
// 递归退出条件
if (childList.size() == 0) {
return null;
}
return childList;
}
查询的数据结果
{
"code": "0",
"message": "成功",
"data": [
{
"id": "3771306d-b43f-4eea-b759-d06cccb0d74f",
"scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
"name": "技术",
"weight": 1,
"parentId": "top",
"childList": [
{
"id": "7007496f-e438-4be1-95cd-2460346adafc",
"scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
"name": "集成",
"weight": 27,
"parentId": "3771306d-b43f-4eea-b759-d06cccb0d74f",
"childList": [
"childList": [
{
"id": "7470fdbb-b7fa-4476-98aa-a54478dab960",
"scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
"name": "计划与调度",
"weight": 16,
"parentId": "93cebf9d-2c9f-4316-975a-c8a97822f5b1",
"variable": 24
},
{
"id": "79d3006b-19df-46f6-aa68-891e2dc05963",
"scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
"name": "安全环保",
"weight": 13,
"parentId": "93cebf9d-2c9f-4316-975a-c8a97822f5b1",
"variable": 27
},
{
"id": "b6ae80a7-9c75-466d-9025-82dc64e6321b",
"scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
"name": "生产作业",
"weight": 26,
"parentId": "93cebf9d-2c9f-4316-975a-c8a97822f5b1",
"variable": 25
}
]
"variable": 17
},
{
"id": "c109d13e-c1e3-4480-89eb-81f3f32ff2b5",
"scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
"name": "信息安全",
"weight": 27,
"parentId": "3771306d-b43f-4eea-b759-d06cccb0d74f",
"childList": null,
"variable": 18
},
{
"id": "f6075a8d-1301-4447-be78-06ee6bd013ac",
"scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
"name": "数据",
"weight": 46,
"parentId": "3771306d-b43f-4eea-b759-d06cccb0d74f",
"childList": null,
"variable": 16
}
],
"variable": 11
},
{
"id": "483d73d8-f5ce-4ff0-82d4-c17bafcc31f6",
"scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
"name": "人员",
"weight": 1,
"parentId": "top",
"childList": [
{
"id": "0046bad2-6e4b-4c3b-a4a5-92d3f39edd70",
"scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
"name": "人员技能",
"weight": 50,
"parentId": "483d73d8-f5ce-4ff0-82d4-c17bafcc31f6",
"childList": null,
"variable": 15
},
{
"id": "ad76e507-77c0-4846-b500-42f3675824af",
"scaleId": "22fe7a08-667f-478f-b194-17b2667eb602",
"name": "组织战略",
"weight": 50,
"parentId": "483d73d8-f5ce-4ff0-82d4-c17bafcc31f6",
"childList": null,
"variable": 14
}
],
"variable": 10
}
],
"timestamp": 1626937445890
}
2,java 求树中每一层的所有节点之和
递归方法的参数为List类型,返回值为void
/**
* 校验指标权值是否合法
* @param list
* @return
*/
public void checkList(List<DiagnosisMetric> list){
// 出口
if(list.size() == 0){
return;
}
// 校验权值和是否等于100
if(checkWeight(list) != 100){
String parentMetricName = getParentMetricName(list.get(0).getParentId());
log.info("【"+parentMetricName+"】的下级指标权重和不等于1!");
throw new BusinessRuntimeException(ResultStatus.DIAG_METRIC_WEIGHT_ERROR);
}
// 继续校验下一级指标
for(DiagnosisMetric parent : list){
List<DiagnosisMetric> childs = getChilds(parent);
checkList(childs);
}
}
/**
* 校验同一级指标权重是否合法
* @param metrics
* @return
*/
public Integer checkWeight(List<DiagnosisMetric> metrics){
int sum = 0;
if(metrics.size() > 0){
for(DiagnosisMetric metric : metrics){
sum += metric.getWeight();
}
if(sum != 100){
log.error("指标权重不等于100:{}", JSONObject.toJSONString(metrics));
}
return sum;
}
return sum;
}