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" ;
/ * *
* 获 取 数 据 集 的 表 名
*
* @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 , String 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 ) ;
String rootId = rRoot . getStr ( "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 ) {
String dataset_group_id = record . getStr ( "id" ) ;
//数据集父名称
String parent_name = record . getStr ( "parent_name" ) ;
//数据集名称
String dataset_name = record . getStr ( "name" ) ;
//对应的表名
String table_name = getTableName ( 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 < String > getChildren ( String id ) {
List < String > 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 . getStr ( "id" ) ) ) ;
}
return list ;
}
/ * *
* 获 取 数 据 集 对 应 的 表
*
* @return
* /
public Page < Record > getDataSetContent ( String datasetId , int pageNumber , int pageSize ) {
String sql = "select * from t_dp_yx_dataset where dataset_group_id=?" ;
Record record = Db . findFirst ( sql , datasetId ) ;
if ( record = = null ) return null ;
String tableName = record . getStr ( "table_name" ) ;
Page < Record > p = Db . use ( DB_NAME ) . paginate ( pageNumber , pageSize ,
"SELECT *" , "from " + "`" + tableName + "`" ) ;
return p ;
}
/ * *
* 保 存 数 据 集 对 应 的 表
* /
public void saveDataSet ( String dataset_id , String 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" ) ;
}
/ * *
* 获 取 数 据 集 树 结 构
*
* @return
* /
public List < Record > getDatasetTree ( ) {
SqlPara sqlPara = Db . getSqlPara ( "YunXiao.datasetTree" ) ;
List < Record > list = Db . find ( sqlPara ) ;
return list ;
}
}