@ -1,7 +1,9 @@
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 ;
@ -81,14 +83,13 @@ public class DataEaseModel {
}
/ * *
* 获 取 指 定 大 屏 中 的 地 图 配 置 信 息
*
* @param bigScreenId 大 屏 ID
* @return
* /
public List < Record > getMap ( long bigScreenId ) {
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 ;
@ -100,7 +101,7 @@ public class DataEaseModel {
* @param cityName 城 市 名 称
* @return 城 市 编 码
* /
public String getCityCode ( String cityName ) {
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" ) ;
}
@ -111,7 +112,7 @@ public class DataEaseModel {
* @param dataVisualizationName 大 屏 名 称
* @param cityName 城 市 名 称
* /
public void updateCity ( String dataVisualizationName , String 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 ) ;
@ -131,4 +132,155 @@ public class DataEaseModel {
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 ) ;
}
}
/ * *
* 获 取 表 中 不 允 许 为 空 的 列 名
*
* @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 ) ;
}
}
}