|
|
@ -0,0 +1,259 @@
|
|
|
|
|
|
|
|
package com.dsideal.base.YunXiao.Model;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.jfinal.kit.Kv;
|
|
|
|
|
|
|
|
import com.jfinal.kit.StrKit;
|
|
|
|
|
|
|
|
import com.jfinal.plugin.activerecord.Db;
|
|
|
|
|
|
|
|
import com.jfinal.plugin.activerecord.Page;
|
|
|
|
|
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
|
|
|
import com.jfinal.plugin.activerecord.SqlPara;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class YunXiaoModel {
|
|
|
|
|
|
|
|
//DataEase数据库名称
|
|
|
|
|
|
|
|
public static String DB_NAME = "dataease";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取当前人员可以看到哪些数据集
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return 数据集列表
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public List<Record> getDataSet() {
|
|
|
|
|
|
|
|
String sql = "select t1.* from t_dp_yx_dataset as t1 where t1.b_use=1 order by t1.dataset_group_id";
|
|
|
|
|
|
|
|
List<Record> list = Db.find(sql);
|
|
|
|
|
|
|
|
for (Record record : list) {
|
|
|
|
|
|
|
|
String table_name = record.getStr("table_name");
|
|
|
|
|
|
|
|
sql = "select count(1) as c from `" + table_name + "`";
|
|
|
|
|
|
|
|
int cnt = Db.use(DB_NAME).queryInt(sql);
|
|
|
|
|
|
|
|
record.set("fill_count", cnt);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取数据集的表名
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @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);
|
|
|
|
|
|
|
|
Record record = Db.findFirst(sqlPara);
|
|
|
|
|
|
|
|
if (record == null) {
|
|
|
|
|
|
|
|
System.out.println("数据集不存在" + dataset_group_id);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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 dataset_group_id) {
|
|
|
|
|
|
|
|
String sql = "select count(1) from t_dp_yx_dataset where dataset_group_id=?";
|
|
|
|
|
|
|
|
if (Db.queryInt(sql, dataset_group_id) > 0) {
|
|
|
|
|
|
|
|
System.out.println("数据集已经存在,无需再次添加");
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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("dataset_group_id", dataset_group_id);
|
|
|
|
|
|
|
|
Db.save("t_dp_yx_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, YunXiaoModel.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, YunXiaoModel.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(YunXiaoModel.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(YunXiaoModel.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(YunXiaoModel.DB_NAME).update(sql);
|
|
|
|
|
|
|
|
System.out.println("去掉不允许为空的限制成功");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 将数据集添加到数据库中
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void collectDataSet(boolean clear) {
|
|
|
|
|
|
|
|
//1、获取树根
|
|
|
|
|
|
|
|
String sql = "select * from dataease.core_dataset_group where name='长春云校'";
|
|
|
|
|
|
|
|
Record rRoot = Db.findFirst(sql);
|
|
|
|
|
|
|
|
long rootId = rRoot.getLong("id");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (clear) {
|
|
|
|
|
|
|
|
//清空数据集表
|
|
|
|
|
|
|
|
sql = "truncate table t_dp_yx_dataset";
|
|
|
|
|
|
|
|
Db.update(sql);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//2、查询有哪些数据集
|
|
|
|
|
|
|
|
Kv kv = Kv.by("id", rootId);
|
|
|
|
|
|
|
|
kv.set("dataset", true);
|
|
|
|
|
|
|
|
SqlPara sqlPara = Db.getSqlPara("DataEase.getAllDataSet", kv);
|
|
|
|
|
|
|
|
List<Record> list = Db.find(sqlPara);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (Record record : list) {
|
|
|
|
|
|
|
|
long dataset_group_id = record.getLong("id");
|
|
|
|
|
|
|
|
//数据集父名称
|
|
|
|
|
|
|
|
String parent_name = record.getStr("parent_name");
|
|
|
|
|
|
|
|
//数据集名称
|
|
|
|
|
|
|
|
String dataset_name = record.getStr("name");
|
|
|
|
|
|
|
|
//对应的表名
|
|
|
|
|
|
|
|
String table_name = getTableName(String.valueOf(dataset_group_id));
|
|
|
|
|
|
|
|
if (!StrKit.isBlank(table_name)) {
|
|
|
|
|
|
|
|
//将这些数据集扫描到表中,然后标识这个数据集由谁来维护
|
|
|
|
|
|
|
|
collectDataSet(parent_name, table_name, dataset_name, dataset_group_id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取指定id的行政区划
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param id
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public Record getAreaById(String id) {
|
|
|
|
|
|
|
|
String sql = "select * from t_dm_area where id=?";
|
|
|
|
|
|
|
|
return Db.findFirst(sql, id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 递归获取所有子节点
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取数据集对应的表
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param id 数据集id
|
|
|
|
|
|
|
|
* @return
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public Page<Record> getDataSetContent(int id, String keyword, int pageNumber, int pageSize) {
|
|
|
|
|
|
|
|
Record record = Db.findById("t_dp_yx_dataset", "id", id);
|
|
|
|
|
|
|
|
String tableName = record.getStr("table_name");
|
|
|
|
|
|
|
|
Page<Record> p = Db.paginate(pageNumber, pageSize,
|
|
|
|
|
|
|
|
"SELECT *", "from " + DB_NAME + ".`" + tableName + "` where `行政区划` like '%" + keyword + "%'");
|
|
|
|
|
|
|
|
return p;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 保存数据集对应的表
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void saveDataSet(int dataset_id, int id, String field, String value) {
|
|
|
|
|
|
|
|
Record record = Db.findById("t_dp_yx_dataset", "id", dataset_id);
|
|
|
|
|
|
|
|
String tableName = record.getStr("table_name");
|
|
|
|
|
|
|
|
String sql = "update `" + tableName + "` set `" + field + "`=? where id=?";
|
|
|
|
|
|
|
|
Db.use(DB_NAME).update(sql, value, id);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getCityNameByAreaName(String areaName) {
|
|
|
|
|
|
|
|
String sql = "select id,parent_id from t_dm_area where area_name=?";
|
|
|
|
|
|
|
|
Record record = Db.findFirst(sql, areaName);
|
|
|
|
|
|
|
|
if (record == null) return null;
|
|
|
|
|
|
|
|
String parent_id = record.getStr("parent_id");
|
|
|
|
|
|
|
|
if (parent_id == null) return null;
|
|
|
|
|
|
|
|
return getAreaById(parent_id).getStr("area_name");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|