数据库中的数据是平铺的,一行行的,但是api要查询出来的数据要求是一棵树的结构,
怎么把平铺的数据转换成树状结构呢?
public List findCarbonRepo(Integer type){
// 1. 先查出所有数据。 baseFindList 方法就是查询数据库,返回一个list对象、CarbonRepo 就是数据库中某张表的数据实体
List list = this.baseFindList(new CarbonRepo().setType(type).setActiveFlag(true));
List collect = list.stream()
// 2. 找出所有顶级(规定 2位code 为顶级)
.filter(o -> o.getCode().length() == 2)
// 3.给当前父级的 childList 设置子
.peek(o -> {
o.setChildList(getChildList(o, list));
o.setKey(o.getId());
})
// 4.收集
.collect(Collectors.toList());
return collect;
}
// 通过递归的方式设置每个对象下的子属性。数据库中表的设计是通过code,parent
// 根据当前父类 找出子类, 并通过递归找出子类的子类
private List getChildList(CarbonRepo carbonRepo, List list) {
return list.stream()
//筛选出父节点code == parentCode 的所有对象 => list
.filter(o -> StringUtil.equals(carbonRepo.getCode(), o.getParentCode()))
.peek(o -> {
o.setChildList(getChildList(o, list));
o.setKey(o.getId());
})
.collect(Collectors.toList());
}
CREATE TABLE `data_carbon_repo` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
`code` varchar(32) NOT NULL COMMENT '编码',
`name` varchar(32) NOT NULL COMMENT '名称',
`parent_code` varchar(32) NOT NULL COMMENT '父编码',
`type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '不同用途区分',
`active_flag` tinyint(4) NOT NULL DEFAULT '1' COMMENT '删除标识 1: 未删除 0:已删除',
`creator` bigint(20) DEFAULT NULL COMMENT '创建人id',
`modifier` bigint(20) DEFAULT NULL COMMENT '修改人id',
`create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`modify_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=344 DEFAULT CHARSET=utf8 COMMENT='材料库配置表'
{
"code": 100,
"message": "成功",
"data": [
{
"id": 279,
"code": "01",
"name": "国标数据",
"parentCode": "0",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [
{
"id": 280,
"code": "0101",
"name": "材料碳排放计算标准",
"parentCode": "01",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [
{
"id": 281,
"code": "010101",
"name": "水泥混凝土类",
"parentCode": "0101",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 281
},
{
"id": 282,
"code": "010102",
"name": "建材制品类",
"parentCode": "0101",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 282
},
{
"id": 283,
"code": "010103",
"name": "钢铁金属类",
"parentCode": "0101",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 283
},
{
"id": 284,
"code": "010104",
"name": "玻璃及门窗类",
"parentCode": "0101",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 284
},
{
"id": 285,
"code": "010105",
"name": "其他",
"parentCode": "0101",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 285
}
],
"key": 280
}
],
"key": 279
},
{
"id": 286,
"code": "02",
"name": "省标数据",
"parentCode": "0",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [
{
"id": 287,
"code": "0201",
"name": "江苏省民用建筑碳排放规则(征求意见稿)",
"parentCode": "02",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [
{
"id": 288,
"code": "020101",
"name": "水泥及其制品类",
"parentCode": "0201",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 288
},
{
"id": 289,
"code": "020102",
"name": "地方材料及建材制品类",
"parentCode": "0201",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 289
},
{
"id": 290,
"code": "020103",
"name": "黑色及有色金属类",
"parentCode": "0201",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 290
},
{
"id": 291,
"code": "020104",
"name": "玻璃及门窗",
"parentCode": "0201",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 291
},
{
"id": 292,
"code": "020105",
"name": "化学制品",
"parentCode": "0201",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 292
},
{
"id": 293,
"code": "020106",
"name": "自来水",
"parentCode": "0201",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 293
},
{
"id": 294,
"code": "020107",
"name": "装饰面层",
"parentCode": "0201",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 294
},
{
"id": 295,
"code": "020108",
"name": "油漆、涂料类",
"parentCode": "0201",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 295
},
{
"id": 296,
"code": "020109",
"name": "预制构件",
"parentCode": "0201",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 296
},
{
"id": 297,
"code": "020110",
"name": "环保建材",
"parentCode": "0201",
"type": 2,
"activeFlag": 1,
"creator": null,
"modifier": null,
"createTime": "2023-08-18 16:40:46",
"modifyTime": "2023-08-18 16:40:46",
"childList": [],
"key": 297
}
],
"key": 287
}
],
"key": 286
}
],
"success": true
}