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.
607 lines
21 KiB
607 lines
21 KiB
package com.dsideal.base.DataEase.Model;
|
|
|
|
import com.dsideal.base.BaseApplication;
|
|
import com.dsideal.base.Util.ExcelCommonUtil;
|
|
import com.jfinal.kit.Kv;
|
|
import com.jfinal.kit.StrKit;
|
|
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.FileInputStream;
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
public class DataEaseModel {
|
|
//DataEase数据库名称
|
|
public static String DB_NAME = "dataease";
|
|
|
|
/**
|
|
* 获取当前人员可以看到哪些数据集
|
|
*
|
|
* @param identity_id 身份id
|
|
* @return 数据集列表
|
|
*/
|
|
public List<Record> getDataSetByIdentityId(int identity_id, String area_name) {
|
|
String sql = "select t1.* from t_dp_dataset as t1 where t1.owner_id=? order by t1.dataease_id";
|
|
List<Record> list = Db.find(sql, identity_id);
|
|
if (!StrKit.isBlank(area_name)) {
|
|
for (Record record : list) {
|
|
//这个数据集,当前的区域,已经填写了多少条数据
|
|
String table_name = record.getStr("table_name");
|
|
sql = "select count(1) as c from `" + table_name + "`";
|
|
if (identity_id > 1) {
|
|
sql += " where `行政区划`='" + area_name + "'";
|
|
}
|
|
int cnt = Db.use(DB_NAME).queryInt(sql);
|
|
record.set("fill_count", cnt);
|
|
}
|
|
}
|
|
return list;
|
|
}
|
|
|
|
|
|
/**
|
|
* 保存数据集对应的表
|
|
*
|
|
* @param dataset_id 数据集id
|
|
*/
|
|
public void saveDataSetTable(int identity_id, int dataset_id, String parent_area_name, String area_name, List<ExcelRow> listExcelRow) {
|
|
//根据数据集id获取表名
|
|
String tableName = getDataSetById(dataset_id).getStr("table_name");
|
|
// 清除数据
|
|
if (identity_id > 1) {
|
|
String sql = "delete from `" + tableName + "` where `行政区划`=?";
|
|
Db.use(DB_NAME).update(sql, area_name);
|
|
} else {
|
|
String sql = "delete from `" + tableName + "`";
|
|
Db.use(DB_NAME).update(sql);
|
|
}
|
|
|
|
List<Record> list = new ArrayList<>();
|
|
//数据行
|
|
for (int i = 1; i < listExcelRow.size(); i++) {
|
|
ExcelRow row = listExcelRow.get(i);
|
|
Record record = new Record();
|
|
for (int j = 0; j < row.getData().size(); j++) {
|
|
String value = row.getData().get(j);
|
|
//第一行是表头
|
|
String colName = listExcelRow.getFirst().getData().get(j);
|
|
if (colName.equals("行政区划")) {
|
|
record.set(colName, area_name);
|
|
} else if (colName.equals("上级行政区划")) {
|
|
record.set(colName, parent_area_name);
|
|
} else {
|
|
if (StrKit.isBlank(value.trim()) && colName.equals("数值")) {
|
|
record.set(colName, 0);
|
|
} else if (isInteger(value)) {
|
|
record.set(colName, Integer.parseInt(value));
|
|
} else if (isDouble(value)) {
|
|
record.set(colName, Double.parseDouble(value));
|
|
} else {
|
|
record.set(colName, value);
|
|
}
|
|
}
|
|
}
|
|
list.add(record);
|
|
}
|
|
Db.use(DB_NAME).batchSave(tableName, list, 100);
|
|
}
|
|
|
|
public boolean isInteger(String str) {
|
|
try {
|
|
Integer.parseInt(str);
|
|
return true;
|
|
} catch (NumberFormatException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public boolean isDouble(String str) {
|
|
try {
|
|
Double.parseDouble(str);
|
|
return true;
|
|
} catch (NumberFormatException e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取指定大屏中的地图配置信息
|
|
*
|
|
* @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 int updateCity(String dataVisualizationName, String cityName) {
|
|
//取出大屏的ID值
|
|
//云南省教育决策支持系统
|
|
String sql = "select * from data_visualization_info where name =?";
|
|
List<Record> L1 = Db.use(DB_NAME).find(sql, dataVisualizationName);
|
|
if (L1.size() > 1) {
|
|
return -1;
|
|
}
|
|
Record dataVisualizationInfo = L1.getFirst();
|
|
long bigScreenId = dataVisualizationInfo.getLong("id");
|
|
//这个名称确实是存在,但它是不是我想要的这个云南省项目中的大屏呢
|
|
Set<Long> set = getFamilyNodes();
|
|
if (!set.contains(bigScreenId)) {
|
|
return -2;
|
|
}
|
|
|
|
// 配置的内容
|
|
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);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取数据集的表名
|
|
*
|
|
* @param dataset_group_id 数据集的id
|
|
* @return 表名
|
|
*/
|
|
public String getTableName(String 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) {
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* 获取表中不允许为空的列名
|
|
*
|
|
* @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");
|
|
|
|
//清空数据集表
|
|
String sql = "truncate table t_dp_dataset";
|
|
Db.update(sql);
|
|
|
|
//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(String.valueOf(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 id
|
|
* @return
|
|
*/
|
|
public Record getAreaById(String id) {
|
|
String sql = "select * from t_dm_area where id=?";
|
|
return Db.findFirst(sql, id);
|
|
}
|
|
|
|
/**
|
|
* 指定父级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);
|
|
}
|
|
|
|
public void fillDefaultXzqh(int identity_id, String tableName) {
|
|
//补全默认行政区划
|
|
if (identity_id == 2) {
|
|
String sql = "update `" + tableName + "` set `行政区划`='昆明市',`上级行政区划`='云南省' where `行政区划` is null or `上级行政区划` is null";
|
|
Db.use(DB_NAME).update(sql);
|
|
}
|
|
if (identity_id == 3) {
|
|
String sql = "update `" + tableName + "` set `行政区划`='寻甸县',`上级行政区划`='昆明市' where `行政区划` is null or `上级行政区划` is null";
|
|
Db.use(DB_NAME).update(sql);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 导出Excel
|
|
*
|
|
* @param identity_id
|
|
* @param tableName
|
|
* @param exportPath
|
|
* @param area_name
|
|
* @throws IOException
|
|
*/
|
|
public String exportExcel(int identity_id, String tableName, String exportPath, String area_name) throws IOException {
|
|
//填充默认的行政区划
|
|
fillDefaultXzqh(identity_id, tableName);
|
|
//对此表中的数据进行直接导出EXCEL
|
|
String sql = "select * from `" + tableName + "`";
|
|
if (identity_id > 1) {
|
|
sql = sql + "where `行政区划`='" + area_name + "'";
|
|
}
|
|
//获取一下表有哪些列
|
|
List<String> columnNames = getColumns(tableName);
|
|
|
|
List<Record> tableData = Db.use(DataEaseModel.DB_NAME).find(sql);
|
|
String excelFileName = UUID.randomUUID().toString().toUpperCase() + ".xlsx";
|
|
String filePath = exportPath + excelFileName;
|
|
//导出
|
|
ExcelCommonUtil.writeExcel(columnNames, tableData, filePath, true);
|
|
return filePath;
|
|
}
|
|
|
|
/**
|
|
* 从Excel中获取列名
|
|
*
|
|
* @param filePath
|
|
* @return
|
|
*/
|
|
public List<String> getColumnNamesFromExcel(String filePath) {
|
|
List<String> columnNames = new ArrayList<>();
|
|
try (FileInputStream fis = new FileInputStream(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");
|
|
}
|
|
|
|
/**
|
|
* 获取云南教科院项目的根节点
|
|
*
|
|
* @return
|
|
*/
|
|
public Record getVisuallizationRoot() {
|
|
String rootName = BaseApplication.PropKit.get("dataEase.dataVisualizationRootName");
|
|
String sql = "select * from data_visualization_info where name =?";
|
|
return Db.use(DB_NAME).findFirst(sql, rootName);
|
|
}
|
|
|
|
/**
|
|
* 递归获取所有子节点
|
|
*
|
|
* @param id 节点id
|
|
* @return
|
|
*/
|
|
public List<Long> getChildren(long id) {
|
|
List<Long> list = new ArrayList<>();
|
|
list.add(id);
|
|
String sql = "select * from data_visualization_info where pid=?";
|
|
List<Record> children = Db.use(DB_NAME).find(sql, id);
|
|
for (Record r : children) {
|
|
list.addAll(getChildren(r.getLong("id")));
|
|
}
|
|
return list;
|
|
}
|
|
|
|
/**
|
|
* 获取这个项目中有多少个节点,都是什么号
|
|
*
|
|
* @return
|
|
*/
|
|
public Set<Long> getFamilyNodes() {
|
|
long rootId = getVisuallizationRoot().getLong("id");
|
|
return new HashSet<>(getChildren(rootId));
|
|
}
|
|
|
|
/**
|
|
* 获取指定名称省下面的所有县区
|
|
*
|
|
* @param provinceName
|
|
* @return
|
|
*/
|
|
public List<Record> getProvinceArea(String provinceName) {
|
|
String provinceId = getAreaByName(provinceName).getStr("id");
|
|
String sql = "select * from ds_db.t_dm_area where province_id=? and level_id=5";
|
|
return Db.find(sql, provinceId);
|
|
}
|
|
|
|
/**
|
|
* 获取数据集对应的表
|
|
*
|
|
* @param id 数据集id
|
|
* @return
|
|
*/
|
|
public List<Record> getDataSetContent(int id) {
|
|
Record record = Db.findById("t_dp_dataset", "id", id);
|
|
String tableName = record.getStr("table_name");
|
|
String sql = "select * from `" + tableName + "`";
|
|
return Db.use(DB_NAME).find(sql);
|
|
}
|
|
|
|
/**
|
|
* 保存数据集对应的表
|
|
*
|
|
* @param id 数据集id
|
|
* @param ja json数组
|
|
*/
|
|
public void saveDataSet(int id, String area_name, JSONArray ja) {
|
|
Record record = Db.findById("t_dp_dataset", "id", id);
|
|
String tableName = record.getStr("table_name");
|
|
|
|
String sql = "delete from `" + tableName + "` where `行政区划`=?";
|
|
Db.use(DB_NAME).update(sql, area_name);
|
|
|
|
List<Record> list = new ArrayList<>();
|
|
for (int i = 0; i < ja.size(); i++) {
|
|
JSONObject jsonObject = ja.getJSONObject(i);
|
|
//遍历jo的每一个属性
|
|
// 或者使用keySet和for-each循环遍历
|
|
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(DB_NAME).batchSave(tableName, list, 100);
|
|
}
|
|
}
|