diff --git a/src/main/java/com/dsideal/base/Tools/JkyNewData/Data/SchoolCount/2019.json b/src/main/java/com/dsideal/base/Tools/JkyNewData/Data/SchoolCount/2019.json deleted file mode 100644 index 2c8035a0..00000000 --- a/src/main/java/com/dsideal/base/Tools/JkyNewData/Data/SchoolCount/2019.json +++ /dev/null @@ -1,166 +0,0 @@ -{ - "基教幼儿园": { - "schoolName": { - "index": 1, - "name": "学校名称", - "type": "String" - }, - "schoolType": { - "index": 3, - "name": "学校类型", - "type": "String" - }, - "schoolCount": { - "index": 5, - "name": "学校数量", - "type": "Integer" - }, - "city": { - "index": 208, - "name": "市", - "type": "String" - }, - "district": { - "index": 209, - "name": "区", - "type": "String" - } - }, - "基教小学": { - "schoolName": { - "index": 1, - "name": "学校名称", - "type": "String" - }, - "schoolType": { - "index": 3, - "name": "学校类型", - "type": "String" - }, - "schoolCount": { - "index": 5, - "name": "学校数量", - "type": "Integer" - }, - "city": { - "index": 350, - "name": "市", - "type": "String" - }, - "district": { - "index": 351, - "name": "区", - "type": "String" - } - }, - "基教初中": { - "schoolName": { - "index": 1, - "name": "学校名称", - "type": "String" - }, - "schoolType": { - "index": 3, - "name": "学校类型", - "type": "String" - }, - "schoolCount": { - "index": 5, - "name": "学校数量", - "type": "Integer" - }, - "city": { - "index": 374, - "name": "市", - "type": "String" - }, - "district": { - "index": 375, - "name": "区", - "type": "String" - } - }, - "基教高中": { - "schoolName": { - "index": 1, - "name": "学校名称", - "type": "String" - }, - "schoolType": { - "index": 3, - "name": "学校类型", - "type": "String" - }, - "schoolCount": { - "index": 5, - "name": "学校数量", - "type": "Integer" - }, - "city": { - "index": 320, - "name": "市", - "type": "String" - }, - "district": { - "index": 321, - "name": "区", - "type": "String" - } - }, - - "基教特教": { - "schoolName": { - "index": 1, - "name": "学校名称", - "type": "String" - }, - "schoolType": { - "index": 3, - "name": "学校类型", - "type": "String" - }, - "schoolCount": { - "index": 5, - "name": "学校数量", - "type": "Integer" - }, - "city": { - "index": 193, - "name": "市", - "type": "String" - }, - "district": { - "index": 194, - "name": "区", - "type": "String" - } - }, - - "基教工读": { - "schoolName": { - "index": 1, - "name": "学校名称", - "type": "String" - }, - "schoolType": { - "index": 3, - "name": "学校类型", - "type": "String" - }, - "schoolCount": { - "index": 5, - "name": "学校数量", - "type": "Integer" - }, - "city": { - "index": 32, - "name": "市", - "type": "String" - }, - "district": { - "index": 33, - "name": "区", - "type": "String" - } - } -} diff --git a/src/main/java/com/dsideal/base/Tools/JkyNewData/ExcelSheetChecker.java b/src/main/java/com/dsideal/base/Tools/JkyNewData/SchoolCountConfig.java similarity index 56% rename from src/main/java/com/dsideal/base/Tools/JkyNewData/ExcelSheetChecker.java rename to src/main/java/com/dsideal/base/Tools/JkyNewData/SchoolCountConfig.java index 59f4add4..cf5b2199 100644 --- a/src/main/java/com/dsideal/base/Tools/JkyNewData/ExcelSheetChecker.java +++ b/src/main/java/com/dsideal/base/Tools/JkyNewData/SchoolCountConfig.java @@ -5,15 +5,20 @@ import cn.idev.excel.FastExcel; import cn.idev.excel.read.builder.ExcelReaderBuilder; import cn.idev.excel.read.metadata.ReadSheet; import com.dsideal.base.Tools.JkyNewData.Util.CloseLogUtil; +import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil; +import com.jfinal.plugin.activerecord.Db; +import com.jfinal.plugin.activerecord.Record; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Map; -public class ExcelSheetChecker { +public class SchoolCountConfig { public static void main(String[] args) { CloseLogUtil.Init(); + // 二、初始化数据库连接 + LocalMysqlConnectUtil.Init(); // Excel文件基础路径 String basePath = "D:/dsWork/2025年收集的人口与教育数据库(20250328)/2015-2020年的数据/基础教育/"; @@ -54,6 +59,7 @@ public class ExcelSheetChecker { // 标记是否找到昆明市 boolean found = false; + int cityColumnIndex = -1; // 遍历每一行数据 for (int rowIndex = 0; rowIndex < dataList.size(); rowIndex++) { @@ -69,9 +75,12 @@ public class ExcelSheetChecker { found = true; // 获取列标识 String columnKey = String.valueOf(entry.getKey()); + // 从列标识中提取数字部分作为索引 + cityColumnIndex = Integer.parseInt(columnKey.replaceAll("[^0-9]", "")); System.out.println("在 " + year + " 年的Sheet '" + sheetName + "' 中:"); System.out.println(" 昆明市首次出现在第 " + (rowIndex + 8) + " 行(跳过7行后)"); - System.out.println(" 列标识: " + columnKey); + System.out.println(" 城市列索引: " + cityColumnIndex); + System.out.println(" 区县列索引: " + (cityColumnIndex + 1)); break; } } @@ -82,7 +91,10 @@ public class ExcelSheetChecker { } } - if (!found) { + if (found) { + // 生成配置并插入数据库 + generateAndInsertConfig(year, sheetName, cityColumnIndex); + } else { System.out.println("在 " + year + " 年的Sheet '" + sheetName + "' 中没有找到昆明市"); } } @@ -93,4 +105,50 @@ public class ExcelSheetChecker { } System.out.println("\n所有年份处理完成"); } + + private static void generateAndInsertConfig(int year, String sheetName, int cityColumnIndex) { + // 基础配置(除了城市和区县列,其他配置与2019年相同) + String[][] baseConfig = { + {"schoolName", "1", "学校名称", "String"}, + {"schoolType", "3", "学校类型", "String"}, + {"schoolCount", "5", "学校数量", "Integer"} + }; + + // 先删除该年份该sheet的旧配置 + Db.delete("DELETE FROM t_yn_school_count_config WHERE year = ? AND sheet_name = ?", year, sheetName); + + // 插入基础配置 + for (String[] config : baseConfig) { + Record record = new Record(); + record.set("year", year) + .set("sheet_name", sheetName) + .set("field_name", config[0]) + .set("field_index", Integer.parseInt(config[1])) + .set("field_type", config[3]) + .set("field_desc", config[2]); + Db.save("t_yn_school_count_config", record); + } + + // 插入城市配置 + Record cityRecord = new Record(); + cityRecord.set("year", year) + .set("sheet_name", sheetName) + .set("field_name", "city") + .set("field_index", cityColumnIndex) + .set("field_type", "String") + .set("field_desc", "市"); + Db.save("t_yn_school_count_config", cityRecord); + + // 插入区县配置 + Record districtRecord = new Record(); + districtRecord.set("year", year) + .set("sheet_name", sheetName) + .set("field_name", "district") + .set("field_index", cityColumnIndex + 1) + .set("field_type", "String") + .set("field_desc", "区"); + Db.save("t_yn_school_count_config", districtRecord); + + System.out.println("已生成并插入 " + year + " 年 " + sheetName + " 的配置"); + } } \ No newline at end of file diff --git a/src/main/java/com/dsideal/base/Tools/JkyNewData/SchoolCountSummary.java b/src/main/java/com/dsideal/base/Tools/JkyNewData/SchoolCountSummary.java index 0ace0ec4..00dbb44f 100644 --- a/src/main/java/com/dsideal/base/Tools/JkyNewData/SchoolCountSummary.java +++ b/src/main/java/com/dsideal/base/Tools/JkyNewData/SchoolCountSummary.java @@ -18,12 +18,12 @@ import com.jfinal.plugin.activerecord.Record; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.TreeMap; import static com.dsideal.base.Tools.JkyNewData.Util.CommonUtil.log; public class SchoolCountSummary { // 配置文件 - public static final String configDir = "D:/dsWork/YunNanDsBase/src/main/java/com/dsideal/base/Tools/JkyNewData/Data/SchoolCount/"; public static final String excelPath = "D:/dsWork/2025年收集的人口与教育数据库(20250328)/2015-2020年的数据/基础教育"; @@ -38,53 +38,62 @@ public class SchoolCountSummary { String sql = "truncate table t_yn_school_count"; Db.update(sql); - // 四、获取所有配置文件 - File dir = new File(configDir); - File[] configFiles = dir.listFiles((d, name) -> name.matches("\\d{4}\\.json")); - - if (configFiles != null) { - for (File configFile : configFiles) { - // 从文件名中提取年份 - String year = configFile.getName().substring(0, 4); - log(String.format("开始处理年份:%s", year)); - - // 读取配置文件 - JSONObject config = JSON.parseObject(new FileReader(configFile)); - String filePath = String.format(excelPath + "/%s.xlsx", year); - - // 创建一个Map来存储所有处理结果 - Map>> allResults = new HashMap<>(); - - // 循环处理每种学校类型 - for (String schoolType : config.keySet()) { - log(String.format("开始处理学校类型:%s", schoolType)); - // 获取当前学校类型的配置 - JSONObject typeConfig = config.getJSONObject(schoolType); - Map columnConfigs = new HashMap<>(); - - //表头的行数,发现全都是前7行是表头 - int skipRows = 7; - // 转换配置为ColumnConfig对象 - for (String key : typeConfig.keySet()) { - JSONObject colConfig = typeConfig.getJSONObject(key); - columnConfigs.put(key, new ColumnConfig( - colConfig.getIntValue("index"), - colConfig.getString("name"), - colConfig.getString("type") - )); - } - - // 处理当前学校类型的数据 - allResults.put(schoolType, processSheet(filePath, schoolType, skipRows, columnConfigs)); - log(String.format("处理学校类型:%s完成", schoolType)); - } - log(String.format("处理年份:%s完成", year)); + // 四、获取所有年份的配置 + List yearRecords = Db.find("SELECT DISTINCT year FROM t_yn_school_count_config ORDER BY year"); + + for (Record yearRecord : yearRecords) { + int year = yearRecord.getInt("year"); + log(String.format("开始处理年份:%s", year)); + + // 读取该年份的所有配置 + List configRecords = Db.find( + "SELECT * FROM t_yn_school_count_config WHERE year = ? ORDER BY sheet_name, field_name", + year + ); + + // 按sheet_name分组配置 + Map> sheetConfigs = new HashMap<>(); + for (Record config : configRecords) { + String sheetName = config.getStr("sheet_name"); + String fieldName = config.getStr("field_name"); + + // 创建ColumnConfig对象 + ColumnConfig columnConfig = new ColumnConfig( + config.getInt("field_index"), + config.getStr("field_desc"), + config.getStr("field_type") + ); + + // 将配置添加到对应的sheet配置中 + sheetConfigs.computeIfAbsent(sheetName, k -> new HashMap<>()) + .put(fieldName, columnConfig); + } + + String filePath = String.format(excelPath + "/%s.xlsx", year); + // 创建一个Map来存储所有处理结果 + Map>> allResults = new HashMap<>(); + + // 循环处理每种学校类型 + for (Map.Entry> entry : sheetConfigs.entrySet()) { + String schoolType = entry.getKey(); + Map columnConfigs = entry.getValue(); + + log(String.format("开始处理学校类型:%s", schoolType)); - // 保存统计结果到数据库 - log(String.format("开始保存年份:%s到数据库", year)); - saveToDatabase(allResults, Integer.parseInt(year)); - log(String.format("保存年份:%s到数据库完成", year)); + //表头的行数,发现全都是前7行是表头 + int skipRows = 7; + + // 处理当前学校类型的数据 + allResults.put(schoolType, processSheet(filePath, schoolType, skipRows, columnConfigs)); + log(String.format("处理学校类型:%s完成", schoolType)); } + + log(String.format("处理年份:%s完成", year)); + + // 保存统计结果到数据库 + log(String.format("开始保存年份:%s到数据库", year)); + saveToDatabase(allResults, year); + log(String.format("保存年份:%s到数据库完成", year)); } log("所有年份处理完成"); } @@ -143,14 +152,21 @@ public class SchoolCountSummary { // 先删除该年份的旧数据 Db.delete("DELETE FROM t_yn_school_count WHERE year = ?", year); + // 使用TreeMap来保持排序 + Map>> sortedResults = new TreeMap<>(allResults); + // 遍历所有结果并保存 - for (Map.Entry>> schoolTypeEntry : allResults.entrySet()) { + for (Map.Entry>> schoolTypeEntry : sortedResults.entrySet()) { String schoolType = schoolTypeEntry.getKey(); + // 对城市进行排序 + Map> sortedCities = new TreeMap<>(schoolTypeEntry.getValue()); - for (Map.Entry> cityEntry : schoolTypeEntry.getValue().entrySet()) { + for (Map.Entry> cityEntry : sortedCities.entrySet()) { String city = cityEntry.getKey(); + // 对区县进行排序 + Map sortedDistricts = new TreeMap<>(cityEntry.getValue()); - for (Map.Entry districtEntry : cityEntry.getValue().entrySet()) { + for (Map.Entry districtEntry : sortedDistricts.entrySet()) { String district = districtEntry.getKey(); int count = districtEntry.getValue();