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.
331 lines
12 KiB
331 lines
12 KiB
package com.dsideal.base.DataEase.Model;
|
|
|
|
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 java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
|
|
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
|
|
* @return
|
|
*/
|
|
public List<Record> getDataSetTableContent(int dataset_id) {
|
|
Record record = getTableName(dataset_id);
|
|
if (record == null) return null;
|
|
String sql = "select * from `" + record.getStr("table_name") + "`";
|
|
List<Record> list = Db.use(DB_NAME).find(sql);
|
|
return list;
|
|
}
|
|
|
|
/**
|
|
* 根据数据集id获取表名
|
|
*
|
|
* @param dataset_id 数据集id
|
|
* @return 表对象
|
|
*/
|
|
public Record getTableName(int dataset_id) {
|
|
String sql = "select * from t_dp_dataset where id=?";
|
|
return Db.findFirst(sql, dataset_id);
|
|
}
|
|
|
|
/**
|
|
* 保存数据集对应的表
|
|
*
|
|
* @param dataset_id 数据集id
|
|
* @param ja json数组
|
|
*/
|
|
public void saveDataSetTable(int identity_id, int dataset_id, String xmqh, JSONArray ja) {
|
|
String tableName = getTableName(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 dataease.core_chart_view where scene_id=? and type='map'";
|
|
List<Record> list = Db.find(sql, bigScreenId);
|
|
return list;
|
|
}
|
|
|
|
/**
|
|
* 获取城市编码
|
|
*
|
|
* @param cityName 城市名称
|
|
* @return 城市编码
|
|
*/
|
|
public String getCityCode(String cityName) {
|
|
String sql = "select area_code from t_city_code where area_name=?";
|
|
return Db.findFirst(sql, cityName).getStr("area_code");
|
|
}
|
|
|
|
/**
|
|
* 修改大屏的城市地图为指定的城市
|
|
*
|
|
* @param dataVisualizationName 大屏名称
|
|
* @param cityName 城市名称
|
|
*/
|
|
public void updateCity(String dataVisualizationName, String cityName) {
|
|
//取出大屏的ID值
|
|
String sql = "select * from dataease.data_visualization_info where name =?";
|
|
Record dataVisualizationInfo = Db.findFirst(sql, dataVisualizationName);
|
|
long bigScreenId = dataVisualizationInfo.getLong("id");
|
|
// 配置的内容
|
|
List<Record> list = getMap(bigScreenId);
|
|
for (Record record : list) {
|
|
long id = record.getLong("id");
|
|
com.alibaba.fastjson.JSONObject jo = com.alibaba.fastjson.JSONObject.parseObject(record.getStr("custom_attr"));
|
|
//获取城市编码
|
|
String area_code = getCityCode(cityName);
|
|
//修改城市编码
|
|
jo.getJSONObject("map").put("id", area_code);
|
|
jo.getJSONObject("map").put("level", "city");
|
|
//写到数据库
|
|
String jsonString = jo.toJSONString();
|
|
Db.update("update dataease.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");
|
|
}
|
|
|
|
|
|
/**
|
|
* 将数据集填充到数据库表中,用于配置此数据集让谁来维护
|
|
*
|
|
* @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;
|
|
}
|
|
|
|
// 检查表是否存在主键
|
|
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);
|
|
}
|
|
}
|