|
|
|
@ -6,13 +6,10 @@ 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;
|
|
|
|
|
import com.dsideal.QingLong.Util.JSONUtil;
|
|
|
|
|
import com.jfinal.kit.JsonKit;
|
|
|
|
|
import com.jfinal.kit.Kv;
|
|
|
|
|
import com.jfinal.kit.PropKit;
|
|
|
|
@ -23,8 +20,8 @@ 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.text.DecimalFormat;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
|
|
|
|
@ -1812,55 +1809,61 @@ public class ZbdcModel {
|
|
|
|
|
* @param year
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public List<Record> getSummaryCgzb(int year) {
|
|
|
|
|
//TODO
|
|
|
|
|
return null;
|
|
|
|
|
// Kv kv = Kv.by("year", year);
|
|
|
|
|
// SqlPara sqlPara = Db.getSqlPara("Zbdc.getSummaryCgzb", kv);
|
|
|
|
|
// List<Record> list = Db.find(sqlPara);
|
|
|
|
|
// list = expandList(list);
|
|
|
|
|
// //共七个配备率,需要重新计算
|
|
|
|
|
// Map<String, Kv> wlPbl = calcPbl(year, new String[]{"初中物理", "高中物理"}); //key:市直,二道区,南关区 Kv:count,radio
|
|
|
|
|
// Map<String, Kv> hxPbl = calcPbl(year, new String[]{"初中化学", "高中化学"});
|
|
|
|
|
// Map<String, Kv> swPbl = calcPbl(year, new String[]{"初中生物", "高中生物"});
|
|
|
|
|
// Map<String, Kv> kxPbl = calcPbl(year, new String[]{"小学科学"});
|
|
|
|
|
// Map<String, Kv> yyPbl = calcPbl(year, new String[]{"小学音乐", "初中音乐", "高中音乐"});
|
|
|
|
|
// Map<String, Kv> tyPbl = calcPbl(year, new String[]{"小学体育", "初中体育", "高中体育"});
|
|
|
|
|
// Map<String, Kv> msPbl = calcPbl(year, new String[]{"小学美术", "初中美术", "高中美术"});
|
|
|
|
|
// //修改一下list
|
|
|
|
|
// for (Record record : list) {
|
|
|
|
|
// String area_name = record.getStr("area_name");
|
|
|
|
|
// if (wlPbl.containsKey(area_name)) {//物理配备率
|
|
|
|
|
// float value = wlPbl.get(area_name).getFloat("radio") / wlPbl.get(area_name).getInt("count");
|
|
|
|
|
// record.set("wl_pbl", String.format("%.2f%%", value * 100));
|
|
|
|
|
// }
|
|
|
|
|
// if (hxPbl.containsKey(area_name)) {//化学配备率
|
|
|
|
|
// float value = hxPbl.get(area_name).getFloat("radio") / hxPbl.get(area_name).getInt("count");
|
|
|
|
|
// record.set("hx_pbl", String.format("%.2f%%", value * 100));
|
|
|
|
|
// }
|
|
|
|
|
// if (swPbl.containsKey(area_name)) {//生物配备率
|
|
|
|
|
// float value = swPbl.get(area_name).getFloat("radio") / swPbl.get(area_name).getInt("count");
|
|
|
|
|
// record.set("sw_pbl", String.format("%.2f%%", value * 100));
|
|
|
|
|
// }
|
|
|
|
|
// if (kxPbl.containsKey(area_name)) {//小学科学配备率
|
|
|
|
|
// float value = kxPbl.get(area_name).getFloat("radio") / kxPbl.get(area_name).getInt("count");
|
|
|
|
|
// record.set("kx_pbl", String.format("%.2f%%", value * 100));
|
|
|
|
|
// }
|
|
|
|
|
// if (yyPbl.containsKey(area_name)) {//音乐配备率
|
|
|
|
|
// float value = yyPbl.get(area_name).getFloat("radio") / yyPbl.get(area_name).getInt("count");
|
|
|
|
|
// record.set("yy_pbl", String.format("%.2f%%", value * 100));
|
|
|
|
|
// }
|
|
|
|
|
// if (tyPbl.containsKey(area_name)) {//体育配备率
|
|
|
|
|
// float value = tyPbl.get(area_name).getFloat("radio") / tyPbl.get(area_name).getInt("count");
|
|
|
|
|
// record.set("ty_pbl", String.format("%.2f%%", value * 100));
|
|
|
|
|
// }
|
|
|
|
|
// if (msPbl.containsKey(area_name)) {//美术配备率
|
|
|
|
|
// float value = msPbl.get(area_name).getFloat("radio") / msPbl.get(area_name).getInt("count");
|
|
|
|
|
// record.set("ms_pbl", String.format("%.2f%%", value * 100));
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// addSummaryRow(list);
|
|
|
|
|
// return list;
|
|
|
|
|
public List<JSONObject> getSummaryCgzb(int year) {
|
|
|
|
|
//基于常规装备查询功能构建统计功能
|
|
|
|
|
Page<Record> list = getQueryCgzb(null, 0, year, "", 1, 9999);
|
|
|
|
|
String json = JsonKit.toJson(list.getList());
|
|
|
|
|
JSONArray ja = JSONArray.parseArray(json);
|
|
|
|
|
Map<String, JSONObject> map = new HashMap<>();
|
|
|
|
|
for (int i = 0; i < ja.size(); i++) {
|
|
|
|
|
JSONObject j2 = ja.getJSONObject(i);
|
|
|
|
|
String area_name = j2.getString("area_name");
|
|
|
|
|
//移除多余属性
|
|
|
|
|
j2.remove("id");
|
|
|
|
|
j2.remove("bureau_id");
|
|
|
|
|
j2.remove("bureau_name");
|
|
|
|
|
j2.remove("year");
|
|
|
|
|
j2.remove("school_type_name");
|
|
|
|
|
|
|
|
|
|
//处理一下配备率百分号
|
|
|
|
|
j2 = convertBfhToDouble(j2);
|
|
|
|
|
if (map.containsKey(area_name)) {//包含
|
|
|
|
|
JSONObject j1 = map.get(area_name);
|
|
|
|
|
j1 = convertBfhToDouble(j1);
|
|
|
|
|
//将jo中所有属性叠加到j2上去,并重新将j2放入到map中, 将j2的所有属性值累加到j1上
|
|
|
|
|
CommonUtil.mergeJsonObjects(j1, j2);
|
|
|
|
|
//放回去
|
|
|
|
|
j1.put("cnt", j1.getInteger("cnt") + 1);
|
|
|
|
|
map.put(area_name, j1);
|
|
|
|
|
} else {//不包含
|
|
|
|
|
j2.put("cnt", 1);
|
|
|
|
|
map.put(area_name, j2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//所有以pbl结尾的字段,都需要再除以cnt
|
|
|
|
|
for (String key : map.keySet()) {
|
|
|
|
|
JSONObject j = map.get(key);
|
|
|
|
|
for (String k : j.keySet()) {
|
|
|
|
|
if (k.endsWith("pbl")) {
|
|
|
|
|
// 计算百分比
|
|
|
|
|
String percentage = String.format("%.1f%%", j.getDouble(k) / j.getInteger("cnt"));
|
|
|
|
|
j.put(k, percentage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//以指定顺序的顺序输出
|
|
|
|
|
List<Record> L = getQueryXzqh();
|
|
|
|
|
List<JSONObject> res = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < L.size(); i++) {
|
|
|
|
|
Record r = L.get(i);
|
|
|
|
|
String area_name = r.getStr("area_name");
|
|
|
|
|
if (map.containsKey(area_name)) {
|
|
|
|
|
JSONObject j = map.get(area_name);
|
|
|
|
|
res.add(j);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//System.out.println(JsonKit.toJson(res));
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -2318,31 +2321,23 @@ public class ZbdcModel {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
/**
|
|
|
|
|
* 功能:将JSONObject对象中的百分比转换为double
|
|
|
|
|
*
|
|
|
|
|
* @param jo
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static JSONObject convertBfhToDouble(JSONObject jo) {
|
|
|
|
|
Set<String> keys = jo.keySet();
|
|
|
|
|
for (String key : keys) {
|
|
|
|
|
// 累加数值
|
|
|
|
|
Object v = jo.get(key);
|
|
|
|
|
if (v.toString().indexOf("%") > 0) {
|
|
|
|
|
v = v.toString().replace("%", "");
|
|
|
|
|
jo.put(key, (Double.parseDouble(v.toString())));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 如果不是数字也不是字符串,返回原值
|
|
|
|
|
return value;
|
|
|
|
|
return jo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws SQLException {
|
|
|
|
@ -2377,95 +2372,6 @@ public class ZbdcModel {
|
|
|
|
|
redis.start();
|
|
|
|
|
|
|
|
|
|
ZbdcModel zm = new ZbdcModel();
|
|
|
|
|
//基于常规装备查询功能构建统计功能
|
|
|
|
|
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 delSql = "drop table if exists " + table_name;
|
|
|
|
|
Db.update(delSql);
|
|
|
|
|
|
|
|
|
|
String createSql = JSONUtil.generateCreateTableStatement(table_name, jsonObject, 1);//生成正式表
|
|
|
|
|
//事务处理
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
record.set("id", UUID.randomUUID().toString().toUpperCase());
|
|
|
|
|
writeList.add(record);
|
|
|
|
|
}
|
|
|
|
|
if (!writeList.isEmpty()) {
|
|
|
|
|
Db.batchSave(table_name, writeList, 100);
|
|
|
|
|
conn.commit();//提交
|
|
|
|
|
}
|
|
|
|
|
//马上查询
|
|
|
|
|
//String sql = "select * from " + table_name;
|
|
|
|
|
//List<Record> l1 = Db.find(sql);
|
|
|
|
|
//System.out.println(l1);
|
|
|
|
|
|
|
|
|
|
Set<String> fields = new HashSet<>();
|
|
|
|
|
for (int i = 0; i < ja.size(); i++) {
|
|
|
|
|
JSONObject jo = ja.getJSONObject(i);
|
|
|
|
|
// 遍历JSON对象以收集所有字段
|
|
|
|
|
fields.addAll(jo.keySet());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder sqlBuilder = new StringBuilder();
|
|
|
|
|
sqlBuilder.append("SELECT area_name, ");
|
|
|
|
|
|
|
|
|
|
// 为每个字段添加SUM聚合函数,除了area_name, year, bureau_id, bureau_name
|
|
|
|
|
for (String field : fields) {
|
|
|
|
|
if (!field.equals("area_name") && !field.equals("year") &&
|
|
|
|
|
!field.equals("bureau_id") && !field.equals("bureau_name")) {
|
|
|
|
|
sqlBuilder.append("SUM(").append(field).append(") AS ").append(field).append(", ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 去除最后一个逗号
|
|
|
|
|
if (sqlBuilder.charAt(sqlBuilder.length() - 1) == ',') {
|
|
|
|
|
sqlBuilder.deleteCharAt(sqlBuilder.length() - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加统计个数的字段
|
|
|
|
|
sqlBuilder.append(" COUNT(1) AS cnt ")
|
|
|
|
|
.append("FROM t_zbdc_temp ")
|
|
|
|
|
.append("GROUP BY area_name;");
|
|
|
|
|
|
|
|
|
|
// 输出生成的SQL查询
|
|
|
|
|
System.out.println(sqlBuilder.toString());
|
|
|
|
|
|
|
|
|
|
} catch (Exception err) {
|
|
|
|
|
System.out.println(err);
|
|
|
|
|
if (null != conn) {
|
|
|
|
|
conn.rollback();//回滚
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|