main
黄海 1 year ago
parent 142a1ace1f
commit 50e57e1076

@ -0,0 +1,34 @@
package com.dsideal.QingLong.Util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class JsonToSql {
public static String generateCreateTableStatement(String tableName, JSONObject jsonObject) {
StringBuilder createTableBuilder = new StringBuilder("CREATE TEMPORARY TABLE " + tableName + " (\n");
for (String key : jsonObject.keySet()) {
Object value = jsonObject.get(key);
String sqlType = getSqlType(value);
createTableBuilder.append(" ").append(key).append(" ").append(sqlType);
createTableBuilder.append(",\n");
}
createTableBuilder.setLength(createTableBuilder.length() - 2); // 删除最后一个逗号和换行符
createTableBuilder.append("\n);");
return createTableBuilder.toString();
}
private static String getSqlType(Object value) {
if (value instanceof Integer) {
return "INTEGER";
} else if (value instanceof String) {
return "VARCHAR(255)";
} else if (value instanceof Double || value instanceof Float) {
return "DOUBLE PRECISION";
} else if (value instanceof Boolean) {
return "BOOLEAN";
} else {
return "VARCHAR(255)"; // 默认类型
}
}
}

@ -6,6 +6,9 @@ import java.math.RoundingMode;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dsideal.QingLong.Start;
import com.dsideal.QingLong.Util.CommonUtil;
@ -19,9 +22,13 @@ import com.jfinal.plugin.activerecord.dialect.PostgreSqlDialect;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.text.SimpleDateFormat;
import static com.dsideal.QingLong.Util.JsonToSql.generateCreateTableStatement;
public class ZbdcModel {
/**
* ID
@ -1481,12 +1488,21 @@ public class ZbdcModel {
}
//配备情况
List<Record> listPb = calcPbl(year);
Map<String, Float> mapPb = new HashMap<>();
Map<String, Float> mapPbl = new HashMap<>();//配备率
Map<String, Integer> mapNowCnt = new HashMap<>();//现有品种数量
Map<String, Integer> mapNeedCnt = new HashMap<>();//应配置品种数量
for (Record record : listPb) {
String bureau_id = record.getStr("bureau_id");
String col = record.getStr("col"); //summary_yypbl
float radio = record.getFloat("radio");
mapPb.put(bureau_id + "_" + col, radio);
int nowcnt = record.getInt("nowcnt");
int needcnt = record.getInt("needcnt");
//配置率
mapPbl.put(bureau_id + "_" + col, radio);
//已配置品种数量
mapNowCnt.put(bureau_id + "_" + col, nowcnt);
//应配置品牌数量
mapNeedCnt.put(bureau_id + "_" + col, needcnt);
}
for (Record record : p.getList()) {
@ -1585,34 +1601,35 @@ public class ZbdcModel {
record.set("summary_qdljtrmj", record.getFloat("cz_qdljtrmj") + record.getFloat("gz_qdljtrmj"));
record.set("summary_lsjtrshshu", record.getInt("cz_lsjtrshshu") + record.getInt("gz_lsjtrshshu"));
record.set("summary_lsjtrmj", record.getFloat("cz_lsjtrmj") + record.getFloat("gz_lsjtrmj"));
//配备率
record.set("xx_kxpbl", mapPb.get(bureau_id + "_" + "xx_kxpbl"));//小学科学 配备率
record.set("xx_yypbl", mapPb.get(bureau_id + "_" + "xx_yypbl"));//小学音乐 配备率
record.set("cz_yypbl", mapPb.get(bureau_id + "_" + "cz_yypbl"));//初中音乐 配备率
record.set("gz_yypbl", mapPb.get(bureau_id + "_" + "gz_yypbl"));//高中音乐 配备率
record.set("summary_yypbl", mapPb.get(bureau_id + "_" + "summary_yypbl"));//汇总 配备率
record.set("xx_typbl", mapPb.get(bureau_id + "_" + "xx_typbl"));//小学体育 配备率
record.set("cz_typbl", mapPb.get(bureau_id + "_" + "cz_typbl"));//初中体育 配备率
record.set("gz_typbl", mapPb.get(bureau_id + "_" + "gz_typbl"));//高中体育 配备率
record.set("summary_typbl", mapPb.get(bureau_id + "_" + "summary_typbl"));//汇总 配备率
record.set("xx_mspbl", mapPb.get(bureau_id + "_" + "xx_mspbl"));//小学美术 配备率
record.set("cz_mspbl", mapPb.get(bureau_id + "_" + "cz_mspbl"));//初中美术 配备率
record.set("gz_mspbl", mapPb.get(bureau_id + "_" + "gz_mspbl"));//高中美术 配备率
record.set("summary_mspbl", mapPb.get(bureau_id + "_" + "summary_mspbl"));//汇总 配备率
record.set("cz_wlpbl", mapPb.get(bureau_id + "_" + "cz_wlpbl"));//初中物理 配备率
record.set("gz_wlpbl", mapPb.get(bureau_id + "_" + "gz_wlpbl"));//高中物理 配备率
record.set("summary_wlpbl", mapPb.get(bureau_id + "_" + "summary_wlpbl"));//汇总 配备率
record.set("cz_hxpbl", mapPb.get(bureau_id + "_" + "cz_hxpbl"));//初中化学 配备率
record.set("gz_hxpbl", mapPb.get(bureau_id + "_" + "gz_hxpbl"));//高中化学 配备率
record.set("summary_hxpbl", mapPb.get(bureau_id + "_" + "summary_hxpbl"));//汇总 配备率
record.set("cz_swpbl", mapPb.get(bureau_id + "_" + "cz_swpbl"));//初中生物 配备率
record.set("gz_swpbl", mapPb.get(bureau_id + "_" + "gz_swpbl"));//高中生物 配备率
record.set("summary_swpbl", mapPb.get(bureau_id + "_" + "summary_swpbl"));//汇总 配备率
//配备率 cz_swyppz cz_swypjs
record.set("xx_kxpbl", mapPbl.get(bureau_id + "_" + "xx_kxpbl"));//小学科学 配备率
record.set("xx_yypbl", mapPbl.get(bureau_id + "_" + "xx_yypbl"));//小学音乐 配备率
record.set("cz_yypbl", mapPbl.get(bureau_id + "_" + "cz_yypbl"));//初中音乐 配备率
record.set("gz_yypbl", mapPbl.get(bureau_id + "_" + "gz_yypbl"));//高中音乐 配备率
record.set("summary_yypbl", mapPbl.get(bureau_id + "_" + "summary_yypbl"));//汇总 配备率
record.set("xx_typbl", mapPbl.get(bureau_id + "_" + "xx_typbl"));//小学体育 配备率
record.set("cz_typbl", mapPbl.get(bureau_id + "_" + "cz_typbl"));//初中体育 配备率
record.set("gz_typbl", mapPbl.get(bureau_id + "_" + "gz_typbl"));//高中体育 配备率
record.set("summary_typbl", mapPbl.get(bureau_id + "_" + "summary_typbl"));//汇总 配备率
record.set("xx_mspbl", mapPbl.get(bureau_id + "_" + "xx_mspbl"));//小学美术 配备率
record.set("cz_mspbl", mapPbl.get(bureau_id + "_" + "cz_mspbl"));//初中美术 配备率
record.set("gz_mspbl", mapPbl.get(bureau_id + "_" + "gz_mspbl"));//高中美术 配备率
record.set("summary_mspbl", mapPbl.get(bureau_id + "_" + "summary_mspbl"));//汇总 配备率
record.set("cz_wlpbl", mapPbl.get(bureau_id + "_" + "cz_wlpbl"));//初中物理 配备率
record.set("gz_wlpbl", mapPbl.get(bureau_id + "_" + "gz_wlpbl"));//高中物理 配备率
record.set("summary_wlpbl", mapPbl.get(bureau_id + "_" + "summary_wlpbl"));//汇总 配备率
record.set("cz_hxpbl", mapPbl.get(bureau_id + "_" + "cz_hxpbl"));//初中化学 配备率
record.set("gz_hxpbl", mapPbl.get(bureau_id + "_" + "gz_hxpbl"));//高中化学 配备率
record.set("summary_hxpbl", mapPbl.get(bureau_id + "_" + "summary_hxpbl"));//汇总 配备率
record.set("cz_swpbl", mapPbl.get(bureau_id + "_" + "cz_swpbl"));//初中生物 配备率
record.set("gz_swpbl", mapPbl.get(bureau_id + "_" + "gz_swpbl"));//高中生物 配备率
record.set("summary_swpbl", mapPbl.get(bureau_id + "_" + "summary_swpbl"));//汇总 配备率
//如果配备率为空则设为0,什么情况下为空呢?比如用户就没有配置
for (Map.Entry<String, Object> entry : record.getColumns().entrySet()) {
@ -2304,7 +2321,37 @@ public class ZbdcModel {
}
}
public static void main(String[] args) {
private static Object convertValue(Object value) {
if (value.toString().equals("0.0")) return 0;
// 如果值已经是数字类型,则直接返回
if (value instanceof Number) {
return value;
}
// 如果值是字符串,尝试转换为数字
if (value instanceof String) {
String stringValue = (String) value;
try {
// 尝试去除百分号并转换为浮点数
if (stringValue.endsWith("%")) {
String numberStr = stringValue.substring(0, stringValue.length() - 1);
return Double.parseDouble(numberStr) / 100.0;
}
// 尝试转换为整数
return Integer.parseInt(stringValue);
} catch (NumberFormatException e) {
// 如果转换失败,返回原始字符串
return stringValue;
}
}
// 如果不是数字也不是字符串,返回原值
return value;
}
public static void main(String[] args) throws SQLException {
//告之配置文件位置
PropKit.use("application.properties");
HikariCpPlugin hp = new HikariCpPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"),
@ -2336,8 +2383,57 @@ public class ZbdcModel {
redis.start();
ZbdcModel zm = new ZbdcModel();
List<Record> list = zm.calcPbl(2024);
String json = JsonKit.toJson(list);
System.out.println(json);
//基于常规装备查询功能构建统计功能
Page<Record> list = zm.getQueryCgzb(null, 211, 2024, "", 1, 9999);
String json = JsonKit.toJson(list.getList());
JSONArray ja = JSONArray.parseArray(json);
if (!ja.isEmpty()) {
String table_name = "t_zbdc_temp";
//取第一个对象做为标准的建表对象
JSONObject jsonObject = ja.getJSONObject(0);
//能转成数字的,则转换为数字类型
for (String key : jsonObject.keySet()) {
Object value = jsonObject.get(key);
Object convertedValue = convertValue(value);
jsonObject.put(key, convertedValue);
}
String createSql = generateCreateTableStatement(table_name, jsonObject);
//事务处理
Connection conn = null;
try {
conn = DbKit.getConfig().getDataSource().getConnection();
DbKit.getConfig().setThreadLocalConnection(conn);
conn.setAutoCommit(false);//手工提交
//创建表
Db.update(createSql);
conn.commit();//提交
// 构建批量写入的集合
List<Record> writeList = new ArrayList<>();
for (int i = 0; i < ja.size(); i++) {
JSONObject jo = ja.getJSONObject(i);
// 构建Record对象
Record record = new Record();
for (String key : jo.keySet()) {
Object value = jo.get(key);
Object convertedValue = convertValue(value);
record.set(key, convertedValue);
}
writeList.add(record);
}
if (!writeList.isEmpty()) {
Db.batchSave("t_zbdc_temp", writeList, 1000);
}
//马上查询
String sql = "select * from t_zbdc_temp";
List<Record> l1 = Db.find(sql);
FileUtil.writeUtf8String(JsonKit.toJson(l1), "c://1.json");
} catch (Exception err) {
System.out.println(err.toString());
if (null != conn) {
conn.rollback();//回滚
}
}
}
}
}
Loading…
Cancel
Save