You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
433 lines
15 KiB
433 lines
15 KiB
package com.dsideal.base.DataEase.Model;
|
|
|
|
import com.dsideal.base.Util.ExcelCommonUtil;
|
|
import com.jfinal.kit.Kv;
|
|
import com.jfinal.plugin.activerecord.Db;
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
import com.jfinal.plugin.activerecord.SqlPara;
|
|
import net.sf.json.JSONArray;
|
|
import net.sf.json.JSONObject;
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
public class DataEaseModel {
|
|
//DataEase数据库名称
|
|
public static final String DB_NAME = "dataease";
|
|
|
|
/**
|
|
* 获取当前人员可以看到哪些数据集
|
|
*
|
|
* @param identity_id 身份id
|
|
* @return 数据集列表
|
|
*/
|
|
public List<Record> getDataSetByIdentityId(int identity_id) {
|
|
String sql = "select * from t_dp_dataset where owner_id=? order by dataease_id desc";
|
|
return Db.find(sql, identity_id);
|
|
}
|
|
|
|
|
|
/**
|
|
* 保存数据集对应的表
|
|
*
|
|
* @param dataset_id 数据集id
|
|
* @param ja json数组
|
|
*/
|
|
public void saveDataSetTable(int identity_id, int dataset_id, String xmqh, JSONArray ja) {
|
|
String tableName = getDataSetById(dataset_id).getStr("table_name");
|
|
if (identity_id > 1) {
|
|
String sql = "delete from dataease.`" + tableName + "` where `行政区划`=?";
|
|
Db.update(sql, xmqh);
|
|
} else {
|
|
String sql = "delete from dataease.`" + tableName + "`";
|
|
Db.update(sql);
|
|
}
|
|
List<Record> list = new ArrayList<>();
|
|
for (int i = 0; i < ja.size(); i++) {
|
|
JSONObject jsonObject = ja.getJSONObject(i);
|
|
//遍历jo的每一个属性
|
|
// 或者使用keySet和for-each循环遍历
|
|
Record record = new Record();
|
|
for (Object key : jsonObject.keySet()) {
|
|
Object value = jsonObject.get(key);
|
|
if (value.equals("null")) value = null;
|
|
record.set(key.toString(), value);
|
|
}
|
|
list.add(record);
|
|
}
|
|
Db.use("dataease").batchSave(tableName, list, 100);
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取指定大屏中的地图配置信息
|
|
*
|
|
* @param bigScreenId 大屏ID
|
|
* @return
|
|
*/
|
|
public List<Record> getMap(long bigScreenId) {
|
|
String sql = "select id,custom_attr from core_chart_view where scene_id=? and type like '%map%'";
|
|
return Db.use(DB_NAME).find(sql, bigScreenId);
|
|
}
|
|
|
|
/**
|
|
* 获取城市编码
|
|
*
|
|
* @param cityName 城市名称
|
|
* @return 城市编码
|
|
*/
|
|
public String getCityCode(String cityName) {
|
|
String full_name = getFullAreaName(cityName);
|
|
String sql = "select area_code from t_city_code where area_name=?";
|
|
return Db.findFirst(sql, full_name).getStr("area_code");
|
|
}
|
|
|
|
/**
|
|
* 修改大屏的城市地图为指定的城市
|
|
*
|
|
* @param dataVisualizationName 大屏名称
|
|
* @param cityName 城市名称
|
|
*/
|
|
public void updateCity(String dataVisualizationName, String cityName) {
|
|
//取出大屏的ID值
|
|
//云南省教育决策支持系统
|
|
String sql = "select * from data_visualization_info where name =?";
|
|
Record dataVisualizationInfo = Db.use(DB_NAME).findFirst(sql, dataVisualizationName);
|
|
long bigScreenId = dataVisualizationInfo.getLong("id");
|
|
// 配置的内容
|
|
List<Record> list = getMap(bigScreenId);
|
|
|
|
for (Record record : list) {
|
|
long id = record.getLong("id");
|
|
JSONObject jo = JSONObject.fromObject(record.getStr("custom_attr"));
|
|
//获取城市编码
|
|
String area_code = getCityCode(cityName);
|
|
//修改前
|
|
System.out.println("修改前=" + jo.getJSONObject("map"));
|
|
|
|
//修改城市编码
|
|
jo.getJSONObject("map").put("id", area_code);
|
|
jo.getJSONObject("map").put("level", "city");
|
|
|
|
System.out.println("修改后=" + jo.getJSONObject("map"));
|
|
//写到数据库
|
|
String jsonString = jo.toString();
|
|
Db.use(DB_NAME).update("update core_chart_view set custom_attr=? where id=?", jsonString, id);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取数据集的表名
|
|
*
|
|
* @param dataset_group_id 数据集的id
|
|
* @return 表名
|
|
*/
|
|
public String getTableName(long dataset_group_id) {
|
|
Kv kv = Kv.by("dataset_group_id", dataset_group_id);
|
|
SqlPara sqlPara = Db.getSqlPara("DataEase.getTableName", kv);
|
|
return Db.findFirst(sqlPara).getStr("table_name");
|
|
}
|
|
|
|
/**
|
|
* 根据数据集id获取表名
|
|
*
|
|
* @param dataset_id 数据集id
|
|
* @return 表对象
|
|
*/
|
|
public Record getDataSetById(int dataset_id) {
|
|
String sql = "select * from t_dp_dataset where id=?";
|
|
return Db.findFirst(sql, dataset_id);
|
|
}
|
|
|
|
/**
|
|
* 将数据集填充到数据库表中,用于配置此数据集让谁来维护
|
|
*
|
|
* @param parent_name 数据集的父名称
|
|
* @param table_name 表名
|
|
* @param dataset_name 数据集名
|
|
*/
|
|
public void collectDataSet(String parent_name, String table_name, String dataset_name, long dataease_id) {
|
|
String sql = "select count(1) from t_dp_dataset where table_name=?";
|
|
if (Db.queryInt(sql, table_name) == 0) {
|
|
Record record = new Record();
|
|
record.set("parent_name", parent_name.replace("-", ""));
|
|
record.set("table_name", table_name);
|
|
record.set("dataset_name", dataset_name);
|
|
if (parent_name.contains("省")) {
|
|
record.set("owner_id", 1);
|
|
} else if (parent_name.contains("市") || parent_name.contains("州")) {
|
|
record.set("owner_id", 2);
|
|
} else if (parent_name.contains("县")) {
|
|
record.set("owner_id", 3);
|
|
}
|
|
record.set("dataease_id", dataease_id);
|
|
Db.save("t_dp_dataset", "id", record);
|
|
System.out.println("添加数据集成功,parent_name=" + parent_name + ",table_name=" + table_name + ",dataset_name=" + dataset_name);
|
|
} else {
|
|
System.out.println("数据集已存在,跳过添加");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取表中不允许为空的列名
|
|
*
|
|
* @param tableName 表名
|
|
* @return 列名列表
|
|
*/
|
|
public List<String> getNotNullColumns(String tableName) {
|
|
List<String> columns = new ArrayList<>();
|
|
String sql = "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND IS_NULLABLE = 'NO'";
|
|
List<Record> results = Db.find(sql, DataEaseModel.DB_NAME, tableName);
|
|
for (Record result : results) {
|
|
columns.add(result.get("COLUMN_NAME").toString());
|
|
}
|
|
return columns;
|
|
}
|
|
|
|
/**
|
|
* 指定列中有哪些列
|
|
*
|
|
* @param tableName
|
|
* @return
|
|
*/
|
|
public List<String> getColumns(String tableName) {
|
|
String sql = "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_DEFAULT, IS_NULLABLE, COLUMN_TYPE, CHARACTER_MAXIMUM_LENGTH, NUMERIC_PRECISION, DATETIME_PRECISION, COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?";
|
|
List<Record> list = Db.find(sql, DataEaseModel.DB_NAME, tableName);
|
|
return list.stream().map(record -> record.getStr("COLUMN_NAME")).collect(Collectors.toList());
|
|
}
|
|
|
|
// 检查表是否存在主键
|
|
public boolean hasPrimaryKey(String tableName) {
|
|
String sql = "SELECT COUNT(*) as c FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND CONSTRAINT_TYPE = 'PRIMARY KEY'";
|
|
return Db.use("dataease").queryInt(sql, DataEaseModel.DB_NAME, tableName) > 0;
|
|
}
|
|
|
|
// 添加主键列,并设置为主键
|
|
public void addPrimaryKey(String tableName) {
|
|
// 添加 id 列
|
|
String sql = "ALTER TABLE `" + tableName + "` ADD COLUMN `id` int(11) primary key auto_increment first";
|
|
Db.use(DataEaseModel.DB_NAME).update(sql);
|
|
}
|
|
|
|
/**
|
|
* 获取所有以 excel_ 开头的表
|
|
*
|
|
* @return
|
|
*/
|
|
public List<Record> getExcelTable() {
|
|
// 查询所有以 excel_ 开头的表
|
|
String sql = "SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'dataease' AND TABLE_NAME LIKE 'excel\\_%'";
|
|
return Db.use(DataEaseModel.DB_NAME).find(sql);
|
|
}
|
|
|
|
/**
|
|
* 添加主键
|
|
*/
|
|
public void addPrimaryKey() {
|
|
// 查询所有以 excel_ 开头的表
|
|
List<Record> tables = getExcelTable();
|
|
|
|
for (Record table : tables) {
|
|
String tableName = table.getStr("TABLE_NAME");
|
|
//没有主键的表,添加上主键
|
|
if (!hasPrimaryKey(tableName)) {
|
|
System.out.println("表" + tableName + "没有主键,正在添加主键...");
|
|
addPrimaryKey(tableName);
|
|
System.out.println("添加主键成功");
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 将Excel表中不允许为空的列改为允许为空
|
|
*/
|
|
public void updateNotNullColumns() {
|
|
// 查询所有以 excel_ 开头的表
|
|
List<Record> tables = getExcelTable();
|
|
|
|
for (Record table : tables) {
|
|
String tableName = table.getStr("TABLE_NAME");
|
|
//获取非空列
|
|
List<String> cols = getNotNullColumns(tableName);
|
|
|
|
for (String col : cols) {
|
|
if (!col.equals("id")) {
|
|
System.out.println("列" + col + "非空,正在去掉不允许为空的限制...");
|
|
//去掉不允许为空的限制
|
|
String sql = "ALTER TABLE `" + tableName + "` MODIFY `" + col + "` VARCHAR(255) NULL";
|
|
Db.use(DataEaseModel.DB_NAME).update(sql);
|
|
System.out.println("去掉不允许为空的限制成功");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 将数据集添加到数据库中
|
|
*/
|
|
public void collectDataSet() {
|
|
//1、获取树根
|
|
SqlPara sqlPara = Db.getSqlPara("DataEase.getTreeRoot");
|
|
Record rRoot = Db.findFirst(sqlPara);
|
|
long rootId = rRoot.getLong("id");
|
|
|
|
//2、查询有哪些数据集
|
|
Kv kv = Kv.by("id", rootId);
|
|
kv.set("dataset", true);
|
|
sqlPara = Db.getSqlPara("DataEase.getAllDataSet", kv);
|
|
List<Record> list = Db.find(sqlPara);
|
|
|
|
for (Record record : list) {
|
|
long dataease_id = record.getLong("id");
|
|
//数据集父名称
|
|
String parent_name = record.getStr("parent_name");
|
|
//数据集名称
|
|
String dataset_name = record.getStr("name");
|
|
//对应的表名
|
|
String table_name = getTableName(dataease_id);
|
|
//将这些数据集扫描到表中,然后标识这个数据集由谁来维护
|
|
collectDataSet(parent_name, table_name, dataset_name, dataease_id);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取指定省份下的所有市
|
|
*
|
|
* @param area_name
|
|
* @return
|
|
*/
|
|
public Record getAreaByName(String area_name) {
|
|
String sql = "select * from t_dm_area where area_name=?";
|
|
return Db.findFirst(sql, area_name);
|
|
}
|
|
|
|
/**
|
|
* 指定父级id获取所有子级
|
|
*
|
|
* @param parent_id 父级id
|
|
* @return
|
|
*/
|
|
public List<Record> getAreaList(String parent_id) {
|
|
String sql = "select * from t_dm_area where parent_id=?";
|
|
return Db.find(sql, parent_id);
|
|
}
|
|
|
|
/**
|
|
* 指定表格是不是有指定名称的列
|
|
*
|
|
* @param tableName 表名
|
|
* @return
|
|
*/
|
|
public boolean hasNoColumnName(String tableName, String columnName) {
|
|
String sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = 'dataease' AND TABLE_NAME = ? AND COLUMN_NAME = ?";
|
|
return Db.find(sql, tableName, columnName).isEmpty();
|
|
}
|
|
|
|
/**
|
|
* 添加上指定名称的列
|
|
*
|
|
* @param tableName 表名
|
|
* @param columnName 列名
|
|
*/
|
|
public void addColumn(String tableName, String columnName) {
|
|
String sql = "ALTER TABLE `" + tableName + "` ADD COLUMN `" + columnName + "` varchar(255) NULL";
|
|
Db.use(DataEaseModel.DB_NAME).update(sql);
|
|
}
|
|
|
|
/**
|
|
* 导出Excel
|
|
*
|
|
* @param identity_id
|
|
* @param tableName
|
|
* @param dataSetName
|
|
* @param exportPath
|
|
* @param area_name
|
|
* @throws IOException
|
|
*/
|
|
public String exportExcel(int identity_id, String tableName, String dataSetName, String exportPath, String area_name) throws IOException {
|
|
//对此表中的数据进行直接导出EXCEL
|
|
String sql = "select * from `" + tableName + "`";
|
|
if (identity_id > 1) {
|
|
sql = sql + "where `行政区划`='" + area_name + "'";
|
|
}
|
|
List<Record> tableData = Db.use(DataEaseModel.DB_NAME).find(sql);
|
|
String excelFileName = UUID.randomUUID().toString().toUpperCase() + ".xlsx";
|
|
String filePath = exportPath + excelFileName;
|
|
//导出
|
|
ExcelCommonUtil.writeExcel(tableData, filePath, true);
|
|
return filePath;
|
|
}
|
|
|
|
/**
|
|
* 从Excel中获取列名
|
|
*
|
|
* @param filePath
|
|
* @return
|
|
*/
|
|
public static List<String> getColumnNamesFromExcel(String filePath) {
|
|
List<String> columnNames = new ArrayList<>();
|
|
try (FileInputStream fis = new FileInputStream(new File(filePath));
|
|
Workbook workbook = new XSSFWorkbook(fis)) {
|
|
|
|
Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表
|
|
Row firstRow = sheet.getRow(0); // 假设第一行包含列名
|
|
|
|
if (firstRow != null) {
|
|
for (Cell cell : firstRow) {
|
|
columnNames.add(cell.getStringCellValue());
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return columnNames;
|
|
}
|
|
|
|
/**
|
|
* 记录最后一次操作的是哪个区域
|
|
*
|
|
* @param area_name 区域名称
|
|
*/
|
|
public void writeLastArea(String area_name) {
|
|
String sql = "update t_dataease_last_area set area_name=? where id=1";
|
|
Db.update(sql, area_name);
|
|
}
|
|
|
|
/**
|
|
* 获取完整的区域名称
|
|
*
|
|
* @param area_name
|
|
* @return
|
|
*/
|
|
public String getFullAreaName(String area_name) {
|
|
String sql = "select * from t_dm_area where full_name = ?";
|
|
Record record = Db.findFirst(sql, area_name);
|
|
if (record != null) return area_name;
|
|
sql = "select * from t_dm_area where area_name=?";
|
|
record = Db.findFirst(sql, area_name);
|
|
return record.getStr("full_name");
|
|
}
|
|
|
|
/**
|
|
* 获取最后一次操作的是哪个区域
|
|
*
|
|
* @return
|
|
*/
|
|
public String getLastArea() {
|
|
String sql = "select area_name from t_dataease_last_area where id=1";
|
|
return Db.findFirst(sql).getStr("area_name");
|
|
}
|
|
}
|