|
|
|
@ -0,0 +1,201 @@
|
|
|
|
|
package com.dsideal.base.Tools.Excel;
|
|
|
|
|
|
|
|
|
|
import ch.qos.logback.classic.Level;
|
|
|
|
|
import ch.qos.logback.classic.Logger;
|
|
|
|
|
import cn.idev.excel.*;
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
|
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.FileReader;
|
|
|
|
|
import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Db;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class School2019Map {
|
|
|
|
|
// 定义列配置类
|
|
|
|
|
private static class ColumnConfig {
|
|
|
|
|
int index; // Excel列索引
|
|
|
|
|
String name; // 列名
|
|
|
|
|
String type; // 数据类型
|
|
|
|
|
|
|
|
|
|
ColumnConfig(int index, String name, String type) {
|
|
|
|
|
this.index = index;
|
|
|
|
|
this.name = name;
|
|
|
|
|
this.type = type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws FileNotFoundException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 禁用 logback 的状态输出
|
|
|
|
|
System.setProperty("logback.statusListenerClass", "ch.qos.logback.core.status.NopStatusListener");
|
|
|
|
|
|
|
|
|
|
// 设置所有相关日志级别
|
|
|
|
|
Logger root = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
|
|
|
|
|
root.setLevel(Level.ERROR);
|
|
|
|
|
|
|
|
|
|
// 设置特定包的日志级别
|
|
|
|
|
String[] packages = {
|
|
|
|
|
"org.ehcache",
|
|
|
|
|
"org.ehcache.core",
|
|
|
|
|
"org.ehcache.impl",
|
|
|
|
|
"org.terracotta",
|
|
|
|
|
"ch.qos.logback",
|
|
|
|
|
"org.slf4j"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (String pkg : packages) {
|
|
|
|
|
Logger logger = (Logger) LoggerFactory.getLogger(pkg);
|
|
|
|
|
logger.setLevel(Level.ERROR);
|
|
|
|
|
logger.setAdditive(false);
|
|
|
|
|
}
|
|
|
|
|
//初始化数据库连接
|
|
|
|
|
LocalMysqlConnectUtil.Init();
|
|
|
|
|
|
|
|
|
|
// 读取配置文件
|
|
|
|
|
String configPath = "D:\\dsWork\\YunNanDsBase\\src\\main\\java\\com\\dsideal\\base\\Tools\\Excel\\SchoolCount.json";
|
|
|
|
|
JSONObject config = JSON.parseObject(new FileReader(configPath));
|
|
|
|
|
|
|
|
|
|
String filePath = "D:/dsWork/2025年收集的人口与教育数据库(20250328)/2015-2020年的数据/基础教育/2019.xlsx";
|
|
|
|
|
// 创建一个Map来存储所有处理结果
|
|
|
|
|
Map<String, Map<String, Map<String, Integer>>> allResults = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
// 循环处理每种学校类型
|
|
|
|
|
for (String schoolType : config.keySet()) {
|
|
|
|
|
// 获取当前学校类型的配置
|
|
|
|
|
JSONObject typeConfig = config.getJSONObject(schoolType);
|
|
|
|
|
Map<String, ColumnConfig> columnConfigs = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
// 转换配置为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")
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理当前学校类型的数据
|
|
|
|
|
String sheetName = "基教" + schoolType;
|
|
|
|
|
allResults.put(schoolType, processSheet(filePath, sheetName, 7, columnConfigs));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 保存统计结果到数据库
|
|
|
|
|
saveToDatabase(allResults, 2019);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 处理指定 Sheet 表的数据
|
|
|
|
|
*
|
|
|
|
|
* @param filePath 文件路径
|
|
|
|
|
* @param sheetName Sheet 表名称
|
|
|
|
|
* @param skipRows 跳过的行数
|
|
|
|
|
* @param columnConfigs 列配置
|
|
|
|
|
* @return 处理结果Map
|
|
|
|
|
*/
|
|
|
|
|
private static Map<String, Map<String, Integer>> processSheet(String filePath, String sheetName, int skipRows, Map<String, ColumnConfig> columnConfigs) {
|
|
|
|
|
// 从指定行开始读取,使用Map接收数据
|
|
|
|
|
List<Map<String, Object>> dataList = FastExcel.read(filePath)
|
|
|
|
|
.sheet(sheetName)
|
|
|
|
|
.headRowNumber(skipRows)
|
|
|
|
|
.doReadSync();
|
|
|
|
|
|
|
|
|
|
// 使用 Map 进行分组统计
|
|
|
|
|
Map<String, Map<String, Integer>> cityDistrictMap = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
for (Map<String, Object> data : dataList) {
|
|
|
|
|
try {
|
|
|
|
|
// 根据列配置获取数据
|
|
|
|
|
String city = getColumnValue(data, columnConfigs.get("city"));
|
|
|
|
|
String district = getColumnValue(data, columnConfigs.get("district"));
|
|
|
|
|
int count = getColumnValueAsInt(data, columnConfigs.get("schoolCount"));
|
|
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
// 如果市不存在,创建新的 Map
|
|
|
|
|
cityDistrictMap.putIfAbsent(city, new HashMap<>());
|
|
|
|
|
|
|
|
|
|
// 累加区对应的学校数量
|
|
|
|
|
cityDistrictMap.get(city).merge(district, count, Integer::sum);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.err.println("处理数据行时出错: " + data);
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
continue; // 继续处理下一行
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cityDistrictMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取列值
|
|
|
|
|
*/
|
|
|
|
|
private static String getColumnValue(Map<String, Object> data, ColumnConfig config) {
|
|
|
|
|
Object value = data.get(config.index);
|
|
|
|
|
return value != null ? value.toString() : "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取列值并转换为整数
|
|
|
|
|
*/
|
|
|
|
|
private static int getColumnValueAsInt(Map<String, Object> data, ColumnConfig config) {
|
|
|
|
|
Object value = data.get(config.index);
|
|
|
|
|
if (value == null) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (value instanceof Number) {
|
|
|
|
|
return ((Number) value).intValue();
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
return Integer.parseInt(value.toString());
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将统计结果保存到数据库
|
|
|
|
|
*
|
|
|
|
|
* @param allResults 所有处理结果
|
|
|
|
|
* @param year 年份
|
|
|
|
|
*/
|
|
|
|
|
private static void saveToDatabase(Map<String, Map<String, Map<String, Integer>>> allResults, int year) {
|
|
|
|
|
// 先删除该年份的旧数据
|
|
|
|
|
Db.delete("DELETE FROM t_yn_school_count WHERE year = ?", year);
|
|
|
|
|
|
|
|
|
|
// 遍历所有结果并保存
|
|
|
|
|
for (Map.Entry<String, Map<String, Map<String, Integer>>> schoolTypeEntry : allResults.entrySet()) {
|
|
|
|
|
String schoolType = schoolTypeEntry.getKey();
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<String, Map<String, Integer>> cityEntry : schoolTypeEntry.getValue().entrySet()) {
|
|
|
|
|
String city = cityEntry.getKey();
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<String, Integer> districtEntry : cityEntry.getValue().entrySet()) {
|
|
|
|
|
String district = districtEntry.getKey();
|
|
|
|
|
int count = districtEntry.getValue();
|
|
|
|
|
|
|
|
|
|
// 创建记录
|
|
|
|
|
Record record = new Record();
|
|
|
|
|
record.set("school_type", schoolType)
|
|
|
|
|
.set("city", city)
|
|
|
|
|
.set("district", district)
|
|
|
|
|
.set("school_count", count)
|
|
|
|
|
.set("year", year);
|
|
|
|
|
|
|
|
|
|
// 保存到数据库
|
|
|
|
|
Db.save("t_yn_school_count", record);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
System.out.println("数据已成功保存到数据库");
|
|
|
|
|
}
|
|
|
|
|
}
|