硬写入,无算法后期优化
sql按级别查询
WITH RECURSIVE TreeLevel AS (
SELECT id, parent_id, tree_name, 1 AS level
FROM sys_classification_maintenance
WHERE parent_id = 0
UNION ALL
SELECT c.id, c.parent_id, c.tree_name, p.level + 1 AS level
FROM sys_classification_maintenance c
INNER JOIN TreeLevel p ON c.parent_id = p.id
)
SELECT id, parent_id, tree_name, level
FROM TreeLevel
<if test="level != null and level != '' ">
WHERE level = #{level}
</if>
ORDER BY level, id;
// 1. 读取 Excel 文件
ExcelReader reader = cn.hutool.poi.excel.ExcelUtil.getReader("C:\\Users\\分类.xlsx");
List<List<Object>> rows = reader.read();
for (List<Object> row : rows) {
String level1 = row.get(0).toString();
List<SysClassificationMaintenance> list = sysClassificationMaintenanceService.dictFoodListForLevel("2");
SysClassificationMaintenance maintenance1 = list.stream().filter(e -> e.getTreeName().equals(level1)).findFirst().orElse(null);
SysClassificationMaintenance maintenance11 = null;
if(maintenance1 == null){
SysClassificationMaintenance maintenance = new SysClassificationMaintenance();
maintenance.setParentId(1l);
maintenance.setTreeName(level1);
sysClassificationMaintenanceService.insertSysClassificationMaintenance(maintenance);
List<SysClassificationMaintenance> list11 = sysClassificationMaintenanceService.dictFoodListForLevel("2");
maintenance11 = list11.stream().filter(e -> e.getTreeName().equals(level1)).findFirst().orElse(null);
}
SysClassificationMaintenance maintenance22 = null;
String level2 = row.get(1).toString();
List<SysClassificationMaintenance> list2 = sysClassificationMaintenanceService.dictFoodListForLevel("3");
SysClassificationMaintenance maintenance2 = list2.stream().filter(e -> e.getTreeName().equals(level2)).findFirst().orElse(null);
if(maintenance2 == null){
SysClassificationMaintenance maintenance = new SysClassificationMaintenance();
if(maintenance1 == null){
maintenance.setParentId(maintenance11.getId());
}else {
maintenance.setParentId(maintenance1.getId());
}
maintenance.setTreeName(level2);
sysClassificationMaintenanceService.insertSysClassificationMaintenance(maintenance);
List<SysClassificationMaintenance> list22 = sysClassificationMaintenanceService.dictFoodListForLevel("3");
maintenance22 = list22.stream().filter(e -> e.getTreeName().equals(level2)).findFirst().orElse(null);
}
SysClassificationMaintenance maintenance33 = null;
String level3 = row.get(2).toString();
List<SysClassificationMaintenance> list3 = sysClassificationMaintenanceService.dictFoodListForLevel("4");
SysClassificationMaintenance maintenance3 = list3.stream().filter(e -> e.getTreeName().equals(level3)).findFirst().orElse(null);
if(maintenance3 == null){
SysClassificationMaintenance maintenance = new SysClassificationMaintenance();
if(maintenance2 == null){
maintenance.setParentId(maintenance22.getId());
}else {
maintenance.setParentId(maintenance2.getId());
}
maintenance.setTreeName(level3);
sysClassificationMaintenanceService.insertSysClassificationMaintenance(maintenance);
List<SysClassificationMaintenance> list33 = sysClassificationMaintenanceService.dictFoodListForLevel("4");
maintenance33 = list33.stream().filter(e -> e.getTreeName().equals(level3)).findFirst().orElse(null);
}
String level4 = row.get(3).toString();
List<SysClassificationMaintenance> list4 = sysClassificationMaintenanceService.dictFoodListForLevel("5");
SysClassificationMaintenance maintenance4 = list4.stream().filter(e -> e.getTreeName().equals(level4)).findFirst().orElse(null);
if(maintenance4 == null){
SysClassificationMaintenance maintenance = new SysClassificationMaintenance();
if(maintenance3 == null){
maintenance.setParentId(maintenance33.getId());
}else {
maintenance.setParentId(maintenance3.getId());
}
maintenance.setTreeName(level4);
sysClassificationMaintenanceService.insertSysClassificationMaintenance(maintenance);
}
}
return null;
}