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 ) ;
}
}