This commit is contained in:
2025-12-01 22:37:00 +08:00
parent 25ed2391e9
commit 6fe44b3c81
44 changed files with 2934 additions and 339 deletions

View File

@@ -0,0 +1,76 @@
package com.jeesite.modules.app.dao;
import java.io.Serializable;
import java.time.LocalDateTime;
import lombok.Data;
/**
* <p>
* 数据表字段信息表
* </p>
*
* @author gaoxq
* @since 2025-11-17
*/
@Data
public class DataTableField implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 创建时间
*/
private LocalDateTime createTime;
/**
* 字段唯一标识(主键)
*/
private String fieldId;
/**
* 关联的数据表ID外键关联data_table_info.table_id
*/
private String tableId;
/**
* 数据来源
*/
private String dataSource;
/**
* 数据表名称
*/
private String dataName;
/**
* 字段序号(表示字段在表中的顺序)
*/
private Integer fieldOrder;
/**
* 字段类型int、varchar、datetime等
*/
private String fieldType;
/**
* 字段名称
*/
private String fieldName;
/**
* 字段长度如varchar(50)中的50数值型可表示精度
*/
private Long fieldLength;
/**
* 字段说明
*/
private String fieldRemark;
/**
* 分区日期
*/
private String ds;
}

View File

@@ -0,0 +1,81 @@
package com.jeesite.modules.app.dao;
import lombok.Data;
import java.io.Serializable;
import java.math.BigDecimal;
/**
* <p>
* 数据表基础信息表
* </p>
*
* @author gaoxq
* @since 2025-11-17
*/
@Data
public class DataTableInfo implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 创建时间
*/
private String createTime;
/**
* 数据表唯一标识(主键)
*/
private String tableId;
/**
* 数据表名称
*/
private String dataName;
/**
* 数据表描述
*/
private String tableComment;
/**
* 数据表大小单位MB
*/
private BigDecimal tableSize;
/**
* 数据来源(如:业务系统、文件导入等)
*/
private String dataSource;
/**
* 创建人
*/
private String creator;
/**
* 记录条数
*/
private Long dataRows;
/**
* 更新时间
*/
private String updateTime;
/**
* 备注信息
*/
private String remarks;
/**
* 数据标识
*/
private String dbId;
/**
* 分区日期
*/
private String ds;
}

View File

@@ -0,0 +1,23 @@
package com.jeesite.modules.app.dao;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class TableTree implements Serializable {
DataTableInfo tableInfo;
List<DataTableField> tableFields;
public TableTree() {
}
public TableTree(DataTableInfo tableInfo, List<DataTableField> tableFields) {
this.tableInfo = tableInfo;
this.tableFields = tableFields;
}
}

View File

@@ -63,16 +63,4 @@ public class DateUtils {
int quarter = (month - 1) / 3 + 1;
return String.format("%d-Q%d", date.getYear(), quarter);
}
public static String dsValue() {
LocalDate currentDate = LocalDate.now();
// 格式化日期为yyyymmdd
return currentDate.format(DATE_FORMATTER);
}
public static String dsValueDaysAgo(long days) {
LocalDate targetDate = LocalDate.now().minusDays(days);
return targetDate.format(DATE_FORMATTER);
}
}

View File

@@ -0,0 +1,48 @@
package com.jeesite.modules.app.utils;
import java.util.Random;
public class KeyUtil {
public static String ObjKey(int length, int type) {
Random random = new Random();
StringBuffer key = new StringBuffer();
if (type == 1) {
String str = "0123456789";
for (int i = 0; i < length; ++i) {
//从62个的数字或字母中选择
int number = random.nextInt(10);
//将产生的数字通过length次承载到key中
key.append(str.charAt(number));
}
return key.toString();
} else if (type == 2) {
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for (int i = 0; i < length; ++i) {
//从62个的数字或字母中选择
int number = random.nextInt(36);
//将产生的数字通过length次承载到key中
key.append(str.charAt(number));
}
return key.toString();
} else if (type == 3) {
String str = "abcdefghijklmnopqrstuvwxyz0123456789";
for (int i = 0; i < length; ++i) {
//从62个的数字或字母中选择
int number = random.nextInt(36);
//将产生的数字通过length次承载到key中
key.append(str.charAt(number));
}
return key.toString();
} else {
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (int i = 0; i < length; ++i) {
//从62个的数字或字母中选择
int number = random.nextInt(62);
//将产生的数字通过length次承载到key中
key.append(str.charAt(number));
}
return key.toString();
}
}
}

View File

@@ -0,0 +1,205 @@
package com.jeesite.modules.app.utils;
import com.jeesite.modules.app.dao.DataTableField;
import com.jeesite.modules.app.dao.DataTableInfo;
import com.jeesite.modules.app.dao.TableTree;
import com.jeesite.modules.biz.entity.BizDbConfig;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MysqlUtils {
private static final LoggerUtils logger = LoggerUtils.getInstance();
// 需要排除的系统数据库
private static final List<String> SYSTEM_DATABASES = Arrays.asList(
"information_schema", "mysql", "performance_schema", "sys", "test"
);
// 提取字段长度的正则表达式如varchar(50) -> 50
private static final Pattern LENGTH_PATTERN = Pattern.compile("\\((\\d+)\\)");
/**
* 封装获取MySQL数据库连接
*/
private static Connection getConnection(String ip, Integer port, String username, String password) throws Exception {
String driver = "com.mysql.cj.jdbc.Driver";
String jdbcUrl = String.format(
"jdbc:mysql://%s:%d/information_schema?useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true",
ip, port
);
Class.forName(driver); // 加载驱动
return DriverManager.getConnection(jdbcUrl, username, password);
}
/**
* 获取指定MySQL连接的所有非系统库表结构信息
*
* @return 数据库名 -> 表信息列表(包含字段)的映射
* @throws Exception 连接或查询异常
*/
public static Map<String, List<DataTableInfo>> getMysqlSchemaInfo(Connection conn) throws Exception {
Map<String, List<DataTableInfo>> result = new HashMap<>();
// 1. 获取所有非系统数据库
List<String> databases = getNonSystemDatabases(conn);
logger.info("获取到非系统数据库数量:", databases.size());
// 2. 遍历数据库,获取表和字段信息
for (String dbName : databases) {
List<DataTableInfo> tableInfos = getTablesByDatabase(conn, dbName);
result.put(dbName, tableInfos);
}
return result;
}
/**
* 获取所有非系统数据库
*/
private static List<String> getNonSystemDatabases(Connection conn) throws SQLException {
List<String> databases = new ArrayList<>();
String sql = "SELECT SCHEMA_NAME FROM SCHEMATA WHERE SCHEMA_NAME NOT IN ("
+ String.join(",", Collections.nCopies(SYSTEM_DATABASES.size(), "?")) + ")";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
for (int i = 0; i < SYSTEM_DATABASES.size(); i++) {
ps.setString(i + 1, SYSTEM_DATABASES.get(i));
}
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
databases.add(rs.getString("SCHEMA_NAME"));
}
}
}
return databases;
}
/**
* 获取指定数据库下的所有表信息(包含字段)
*/
private static List<DataTableInfo> getTablesByDatabase(Connection conn, String dbName) throws SQLException {
List<DataTableInfo> tableInfos = new ArrayList<>();
String tableSql = "SELECT " +
"TABLE_NAME, TABLE_COMMENT, CREATE_TIME, UPDATE_TIME, " +
"DATA_LENGTH, INDEX_LENGTH, TABLE_ROWS " +
"FROM TABLES WHERE TABLE_SCHEMA = ?";
try (PreparedStatement tablePs = conn.prepareStatement(tableSql)) {
tablePs.setString(1, dbName);
try (ResultSet tableRs = tablePs.executeQuery()) {
while (tableRs.next()) {
DataTableInfo tableInfo = buildDataTableInfo(tableRs, dbName);
List<DataTableField> fields = getFieldsByTable(conn, dbName, tableInfo.getDataName());
fields.forEach(field -> field.setTableId(tableInfo.getTableId()));
tableInfos.add(tableInfo);
}
}
}
return tableInfos;
}
/**
* 构建DataTableInfo实体
*/
private static DataTableInfo buildDataTableInfo(ResultSet tableRs, String dbName) throws SQLException {
DataTableInfo tableInfo = new DataTableInfo();
tableInfo.setTableId(vId.getUid());
tableInfo.setDataName(tableRs.getString("TABLE_NAME"));
tableInfo.setTableComment(tableRs.getString("TABLE_COMMENT"));
long dataLength = tableRs.getLong("DATA_LENGTH");
long indexLength = tableRs.getLong("INDEX_LENGTH");
BigDecimal tableSize = BigDecimal.valueOf((dataLength + indexLength) / 1024.0 / 1024.0)
.setScale(2, RoundingMode.HALF_UP);
tableInfo.setTableSize(tableSize);
tableInfo.setDataSource(dbName);
tableInfo.setDataRows(tableRs.getLong("TABLE_ROWS"));
String createDate = tableRs.getString("CREATE_TIME");
tableInfo.setCreateTime(createDate != null ? createDate : vDate.getNow());
String updateDate = tableRs.getString("UPDATE_TIME");
tableInfo.setUpdateTime(updateDate != null ? updateDate : vDate.getNow());
tableInfo.setDs(vDate.dsValue());
return tableInfo;
}
/**
* 获取指定表的字段信息
*/
private static List<DataTableField> getFieldsByTable(Connection conn, String dbName, String tableName) throws SQLException {
List<DataTableField> fields = new ArrayList<>();
String fieldSql = "SELECT " +
"TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,DATA_TYPE, COLUMN_TYPE, COLUMN_COMMENT, " +
"ORDINAL_POSITION, CHARACTER_MAXIMUM_LENGTH " +
"FROM COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? " +
"ORDER BY ORDINAL_POSITION";
try (PreparedStatement fieldPs = conn.prepareStatement(fieldSql)) {
fieldPs.setString(1, dbName);
fieldPs.setString(2, tableName);
try (ResultSet fieldRs = fieldPs.executeQuery()) {
while (fieldRs.next()) {
fields.add(buildDataTableField(fieldRs));
}
}
}
return fields;
}
/**
* 构建DataTableField实体
*/
private static DataTableField buildDataTableField(ResultSet fieldRs) throws SQLException {
DataTableField field = new DataTableField();
field.setFieldId(vId.getUid());
field.setDataSource(fieldRs.getString("TABLE_SCHEMA"));
field.setDataName(fieldRs.getString("TABLE_NAME"));
field.setFieldName(fieldRs.getString("COLUMN_NAME"));
field.setFieldType(fieldRs.getString("DATA_TYPE"));
field.setFieldOrder(fieldRs.getInt("ORDINAL_POSITION"));
field.setFieldRemark(fieldRs.getString("COLUMN_COMMENT"));
Long length = fieldRs.getLong("CHARACTER_MAXIMUM_LENGTH");
if (length == 0 || fieldRs.wasNull()) {
length = extractLengthFromType(fieldRs.getString("COLUMN_TYPE"));
}
field.setFieldLength(length);
field.setCreateTime(LocalDateTime.now());
field.setDs(vDate.dsValue());
return field;
}
/**
* 从字段类型中提取长度如int(11) -> 11
*/
private static Long extractLengthFromType(String fieldType) {
if (fieldType == null) return null;
Matcher matcher = LENGTH_PATTERN.matcher(fieldType);
if (matcher.find()) {
try {
return Long.parseLong(matcher.group(1));
} catch (NumberFormatException e) {
logger.warn("提取字段长度失败,类型", fieldType, e);
}
}
return null;
}
public static List<TableTree> getTableTrees(BizDbConfig dbConfig) {
List<TableTree> tableTrees = new ArrayList<>();
try {
Connection conn = getConnection(dbConfig.getDbIp(), dbConfig.getDbPort(), dbConfig.getDbUsername(), dbConfig.getDbPassword());
Map<String, List<DataTableInfo>> schemaInfo = MysqlUtils.getMysqlSchemaInfo(conn);
for (Map.Entry<String, List<DataTableInfo>> entry : schemaInfo.entrySet()) {
for (DataTableInfo tableInfo : entry.getValue()) {
tableInfo.setDbId(dbConfig.getId());
List<DataTableField> dataTableFields = getFieldsByTable(conn, entry.getKey(), tableInfo.getDataName());
dataTableFields.stream().forEach(tableField -> tableField.setTableId(tableInfo.getTableId()));
tableTrees.add(new TableTree(tableInfo, dataTableFields));
}
}
} catch (Exception e) {
logger.error(e.getMessage());
}
return tableTrees;
}
}

View File

@@ -0,0 +1,97 @@
package com.jeesite.modules.app.utils;
import com.jeesite.modules.app.dao.DataTableField;
import com.jeesite.modules.app.dao.DataTableInfo;
import java.util.List;
public class SqlUtils {
/**
* 生成CREATE TABLE语句
*
* @param tableInfo 数据表基础信息
* @param fieldList 字段列表
* @return CREATE TABLE语句
*/
public static String CreateTableSql(DataTableInfo tableInfo, List<DataTableField> fieldList) {
StringBuilder sb = new StringBuilder();
// 表定义开始
sb.append("CREATE TABLE ").append(tableInfo.getDataName()).append(" (\n");
// 拼接字段定义
for (int i = 0; i < fieldList.size(); i++) {
DataTableField field = fieldList.get(i);
sb.append(" ").append(field.getFieldName()).append(" ");
// 处理字段类型和长度
if (field.getFieldLength() != null && field.getFieldLength() > 0) {
sb.append(field.getFieldType()).append("(").append(field.getFieldLength()).append(") ");
} else {
sb.append(field.getFieldType()).append(" ");
}
// 处理主键简单判断fieldId为主键字段
if ("field_id".equals(field.getFieldName()) || "table_id".equals(field.getFieldName())) {
sb.append("NOT NULL AUTO_INCREMENT ");
} else {
sb.append("DEFAULT NULL ");
}
// 字段注释
if (field.getFieldRemark() != null && !field.getFieldRemark().isEmpty()) {
sb.append("COMMENT '").append(field.getFieldRemark()).append("'");
}
// 最后一个字段不加逗号
if (i != fieldList.size() - 1) {
sb.append(",");
}
sb.append("\n");
}
// 表引擎和字符集
sb.append(") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ");
// 表注释
if (tableInfo.getTableComment() != null && !tableInfo.getTableComment().isEmpty()) {
sb.append("COMMENT\t'").append(tableInfo.getTableComment()).append("'");
}
sb.append(";");
return sb.toString();
}
/**
* 生成带注释的SELECT语句
*
* @param tableInfo 数据表基础信息
* @param fieldList 字段列表
* @return 带注释的SELECT语句
*/
public static String SelectSqlComments(DataTableInfo tableInfo, List<DataTableField> fieldList) {
StringBuilder sb = new StringBuilder();
// 表注释
sb.append("-- 表名:").append(tableInfo.getDataName()).append("\n");
sb.append("-- 描述:").append(tableInfo.getTableComment() != null ? tableInfo.getTableComment() : "").append("\n");
sb.append("-- 数据来源:").append(tableInfo.getDataSource() != null ? tableInfo.getDataSource() : "未知").append("\n");
// SELECT语句开始
sb.append("SELECT\n");
// 拼接字段(带注释)
for (int i = 0; i < fieldList.size(); i++) {
DataTableField field = fieldList.get(i);
// 拼接字段名
sb.append(" ").append(field.getFieldName());
// 非最后一个字段加逗号(先处理逗号)
if (i != fieldList.size() - 1) {
sb.append(",");
}
// 处理字段注释(后加注释)
if (field.getFieldRemark() != null && !field.getFieldRemark().isEmpty()) {
sb.append(" -- ").append(field.getFieldRemark());
}
// 换行
sb.append("\n");
}
// 表名
sb.append("FROM ").append(tableInfo.getDataName()).append("\n;");
return sb.toString();
}
}

View File

@@ -0,0 +1,114 @@
package com.jeesite.modules.app.utils;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;
public class vDate {
private static final DateTimeFormatter DEFAULT_FMT =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat DEFAULT_SDF =
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z", Locale.ENGLISH);
private static final DateTimeFormatter DAY_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE; // yyyy-MM-dd
private static final DateTimeFormatter MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM"); // yyyy-MM
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
public static String toStr(LocalDateTime dateTime) {
return dateTime.format(DEFAULT_FMT);
}
/**
* LocalDateTime → String自定义格式
*/
public static String toStr(LocalDateTime dateTime, String pattern) {
return dateTime.format(DateTimeFormatter.ofPattern(pattern));
}
public static String getNow() {
Date date = new Date();
return DEFAULT_SDF.format(date);
}
/**
* 获取更新时间
*/
public static Date getUpdateTime(boolean isNew) {
if (isNew) {
return null;
}
return new Date();
}
public static String dsValue() {
LocalDate currentDate = LocalDate.now();
// 格式化日期为yyyymmdd
return currentDate.format(DATE_FORMATTER);
}
public static String dsValueDaysAgo(long days) {
LocalDate targetDate = LocalDate.now().minusDays(days);
return targetDate.format(DATE_FORMATTER);
}
public static String getDockerCreate(String createAt) {
try {
String cleaned = createAt.replace(" CST", "");
// 定义格式
Date date = sdf.parse(cleaned);
return DEFAULT_SDF.format(date);
} catch (Exception e) {
System.out.println(e.getMessage());
return getNow();
}
}
public static String getRunTimes(long totalSeconds) {
if (totalSeconds < 0) {
throw new IllegalArgumentException("输入的秒数不能为负数");
}
// 定义时间单位换算关系
long secondsPerMinute = 60;
long secondsPerHour = secondsPerMinute * 60; // 3600秒/小时
long secondsPerDay = secondsPerHour * 24; // 86400秒/天
// 计算各单位数值
long days = totalSeconds / secondsPerDay;
long remainingSecondsAfterDays = totalSeconds % secondsPerDay;
long hours = remainingSecondsAfterDays / secondsPerHour;
long remainingSecondsAfterHours = remainingSecondsAfterDays % secondsPerHour;
long minutes = remainingSecondsAfterHours / secondsPerMinute;
long seconds = remainingSecondsAfterHours % secondsPerMinute;
// 拼接结果字符串
StringBuilder result = new StringBuilder();
if (days > 0) {
result.append(days).append("");
}
if (hours > 0) {
result.append(hours).append("小时");
}
if (minutes > 0) {
result.append(minutes).append("");
}
// 确保至少显示秒数即使其他单位都为0
result.append(seconds).append("");
return result.toString();
}
}

View File

@@ -0,0 +1,35 @@
package com.jeesite.modules.app.utils;
import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class vId {
private static final SecureRandom RAND = new SecureRandom();
private static final DateTimeFormatter DF = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
private vId() {}
/**
*
* 时间戳唯一编号
*/
public static String getUid() {
// 17 位时间
String tm = LocalDateTime.now().format(DF);
// 25 位随机数字(高位补零)
long rand = Math.abs(RAND.nextLong()) % (long) Math.pow(10, 15);
return tm + String.format("%015d", rand);
}
public static String getCid() {
// 17 位时间
String tm = LocalDateTime.now().format(DF);
// 25 位随机数字(高位补零)
long rand = Math.abs(RAND.nextLong()) % (long) Math.pow(3, 6);
return tm + String.format("%04d", rand);
}
}

View File

@@ -2,7 +2,6 @@ package com.jeesite.modules.app.utils;
import com.jeesite.common.codec.AesUtils;
import java.util.Date;
public class vo {
@@ -39,16 +38,4 @@ public class vo {
return s;
}
}
/**
* 获取更新时间
*/
public static Date getUpdateTime(boolean isNew) {
if (isNew) {
return null;
}
return new Date();
}
}