diff --git a/src/main/java/com/dsideal/base/BaseApplication.java b/src/main/java/com/dsideal/base/BaseApplication.java index 9a6ce8fe..546c688d 100644 --- a/src/main/java/com/dsideal/base/BaseApplication.java +++ b/src/main/java/com/dsideal/base/BaseApplication.java @@ -19,6 +19,7 @@ import com.dsideal.base.StudentYd.Controller.StudentYdController; import com.dsideal.base.Teacher.Controller.TeacherController; import com.dsideal.base.TeacherYd.Controller.TeacherYdController; import com.dsideal.base.Tools.Controller.excelConvertController; +import com.dsideal.base.YunXiao.Controller.YunXiaoController; import com.dsideal.base.Util.FileUtil; import com.dsideal.base.Util.LogBackLogFactory; import com.dsideal.base.Util.PkUtil; @@ -97,6 +98,8 @@ public class BaseApplication extends JFinalConfig { me.add("/dataease", DataEaseController.class); //资源管理 me.add("/res", ResourceController.class); + //云校 + me.add("/yx",YunXiaoController.class); } @Override diff --git a/src/main/java/com/dsideal/base/Tools/YunXiao/YunXiao_DataSetInit.java b/src/main/java/com/dsideal/base/Tools/YunXiao/YunXiao_DataSetInit.java new file mode 100644 index 00000000..025e3838 --- /dev/null +++ b/src/main/java/com/dsideal/base/Tools/YunXiao/YunXiao_DataSetInit.java @@ -0,0 +1,20 @@ +package com.dsideal.base.Tools.YunXiao; + +import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil; +import com.dsideal.base.YunXiao.Model.YunXiaoModel; + +import java.io.IOException; + +public class YunXiao_DataSetInit { + public static YunXiaoModel ym = new YunXiaoModel(); + + public static void main(String[] args) throws IOException { + LocalMysqlConnectUtil.Init(); + //1、添加到数据集表中 + ym.collectDataSet(false); + //2、加上主键 + ym.addPrimaryKey(); + //3、将所有非空列去掉不允许为空的限制 + ym.updateNotNullColumns(); + } +} diff --git a/src/main/java/com/dsideal/base/YunXiao/Controller/YunXiaoController.java b/src/main/java/com/dsideal/base/YunXiao/Controller/YunXiaoController.java new file mode 100644 index 00000000..fc79f27c --- /dev/null +++ b/src/main/java/com/dsideal/base/YunXiao/Controller/YunXiaoController.java @@ -0,0 +1,74 @@ +package com.dsideal.base.YunXiao.Controller; + +import com.dsideal.base.Interceptor.IsLoginInterface; +import com.dsideal.base.Interceptor.IsNumericInterface; +import com.dsideal.base.Util.CommonUtil; +import com.dsideal.base.Util.SqlInjectionUtils; +import com.dsideal.base.YunXiao.Model.YunXiaoModel; +import com.jfinal.aop.Before; +import com.jfinal.core.Controller; +import com.jfinal.ext.interceptor.GET; +import com.jfinal.ext.interceptor.POST; +import com.jfinal.kit.StrKit; +import com.jfinal.plugin.activerecord.Page; +import com.jfinal.plugin.activerecord.Record; +import io.github.yedaxia.apidocs.ApiDoc; + +import java.util.List; + +@ApiDoc +public class YunXiaoController extends Controller { + + YunXiaoModel ym = new YunXiaoModel(); + + /** + * 可以维护的数据集名称 + */ + @Before({GET.class}) + @IsLoginInterface({}) + public void getDataSet() { + List list = ym.getDataSet(); + renderJson(CommonUtil.renderJsonForLayUI(list)); + } + + + /** + * 获取数据集下的数据表 + * + * @param id 数据集id + * @param pageNumber 第几页 + * @param keyword 关键字 + * @param pageSize 每页多少条数据 + */ + @Before(GET.class) + @IsLoginInterface({}) + @IsNumericInterface({"id"}) + public void getDataSetContent(int id, String keyword, int pageNumber, int pageSize) { + if (StrKit.isBlank(keyword)) keyword = ""; + if (pageNumber == 0) pageNumber = 1; + if (pageSize == 0) pageSize = 20; + if (SqlInjectionUtils.hasSqlInjectionRisk(keyword)) { + renderJson("输入的查询关键字存在SQL注入攻击,无法执行!"); + return; + } + + Page pageList = ym.getDataSetContent(id, keyword, pageNumber, pageSize); + renderJson(CommonUtil.renderJsonForLayUI(pageList)); + } + + /** + * 保存数据集下的数据表 + * + * @param dataset_id 数据集id + * @param id 数据集下的数据表的id + * @param field 字段名 + * @param value 值 + */ + @Before(POST.class) + @IsLoginInterface({}) + public void saveDataSet(int dataset_id, int id, String field, String value) { + ym.saveDataSet(dataset_id, id, field, value); + renderJson(CommonUtil.returnMessageJson(true, "保存成功")); + } + +} diff --git a/src/main/java/com/dsideal/base/YunXiao/Model/YunXiaoModel.java b/src/main/java/com/dsideal/base/YunXiao/Model/YunXiaoModel.java new file mode 100644 index 00000000..30fadf5d --- /dev/null +++ b/src/main/java/com/dsideal/base/YunXiao/Model/YunXiaoModel.java @@ -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 getDataSet() { + String sql = "select t1.* from t_dp_yx_dataset as t1 where t1.b_use=1 order by t1.dataset_group_id"; + List 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 getNotNullColumns(String tableName) { + List columns = new ArrayList<>(); + String sql = "SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND IS_NULLABLE = 'NO'"; + List 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 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 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 tables = getExcelTable(); + for (Record table : tables) { + String tableName = table.getStr("TABLE_NAME"); + //获取非空列 + List 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 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 getChildren(long id) { + List list = new ArrayList<>(); + list.add(id); + String sql = "select * from data_visualization_info where pid=?"; + List 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 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 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"); + } +}