|
|
@@ -0,0 +1,379 @@
|
|
|
+package com.zxyj.config;
|
|
|
+
|
|
|
+import com.zxyj.mapper.*;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
|
|
+import org.springframework.dao.DataAccessException;
|
|
|
+import org.springframework.dao.InvalidDataAccessApiUsageException;
|
|
|
+import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.concurrent.ConcurrentHashMap;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 运行时数据库结构自动同步处理器
|
|
|
+ * 当SQL报错"字段不存在"时,自动检测并添加字段
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class SchemaSyncHandler {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private JdbcTemplate jdbcTemplate;
|
|
|
+
|
|
|
+ // 已处理过的表缓存,避免重复检查
|
|
|
+ private final Set<String> processedTables = ConcurrentHashMap.newKeySet();
|
|
|
+
|
|
|
+ // 字段定义映射表:表名 -> (字段名 -> MySQL类型定义)
|
|
|
+ private static final Map<String, Map<String, String>> SCHEMA_DEFINITIONS = Map.of(
|
|
|
+ "rewards", Map.of(
|
|
|
+ "title", "VARCHAR(100) NOT NULL DEFAULT ''",
|
|
|
+ "points_required", "INT NOT NULL DEFAULT 0",
|
|
|
+ "category", "VARCHAR(32)",
|
|
|
+ "status", "ENUM('available','pending','exchanged','rejected') DEFAULT 'available'",
|
|
|
+ "exchange_approved", "TINYINT",
|
|
|
+ "exchanged_at", "DATETIME",
|
|
|
+ "is_template", "TINYINT DEFAULT 0",
|
|
|
+ "child_id", "BIGINT NULL",
|
|
|
+ "family_id", "BIGINT NOT NULL"
|
|
|
+ ),
|
|
|
+ "tasks", Map.of(
|
|
|
+ "title", "VARCHAR(200) NOT NULL",
|
|
|
+ "description", "VARCHAR(500)",
|
|
|
+ "category", "VARCHAR(32)",
|
|
|
+ "points", "TINYINT DEFAULT 2",
|
|
|
+ "deadline", "DATETIME",
|
|
|
+ "child_id", "BIGINT NULL",
|
|
|
+ "family_id", "BIGINT NOT NULL",
|
|
|
+ "creator_id", "BIGINT NOT NULL",
|
|
|
+ "status", "ENUM('pending','completed','overdue','cancelled') DEFAULT 'pending'",
|
|
|
+ "executor_type", "ENUM('child','parent') DEFAULT 'child'",
|
|
|
+ "executor_id", "BIGINT",
|
|
|
+ "repeat_type", "ENUM('none','daily','weekly') DEFAULT 'none'",
|
|
|
+ "need_review", "TINYINT DEFAULT 0",
|
|
|
+ "review_by_category", "TINYINT DEFAULT 0",
|
|
|
+ "bonus_rule", "VARCHAR(500)",
|
|
|
+ "points_expire_days", "INT DEFAULT 0",
|
|
|
+ "template_id", "BIGINT",
|
|
|
+ "is_template", "TINYINT DEFAULT 0",
|
|
|
+ "task_type", "VARCHAR(20)",
|
|
|
+ "frequency", "VARCHAR(20)",
|
|
|
+ "max_frequency", "INT",
|
|
|
+ "complete_types", "VARCHAR(100)",
|
|
|
+ "review_type", "VARCHAR(20)",
|
|
|
+ "earliest_start", "DATETIME",
|
|
|
+ "latest_end", "DATETIME"
|
|
|
+ ),
|
|
|
+ "users", Map.of(
|
|
|
+ "openid", "VARCHAR(64) NOT NULL",
|
|
|
+ "unionid", "VARCHAR(64)",
|
|
|
+ "family_id", "BIGINT NOT NULL",
|
|
|
+ "role", "ENUM('parent','child','teacher') NOT NULL",
|
|
|
+ "nickname", "VARCHAR(50)",
|
|
|
+ "avatar", "VARCHAR(255)",
|
|
|
+ "password", "VARCHAR(128)",
|
|
|
+ "roles", "VARCHAR(100)",
|
|
|
+ "total_points", "INT DEFAULT 0",
|
|
|
+ "id_card", "VARCHAR(18)",
|
|
|
+ "real_name", "VARCHAR(50)",
|
|
|
+ "phone", "VARCHAR(11)",
|
|
|
+ "birthday", "DATE",
|
|
|
+ "gender", "ENUM('male','female')",
|
|
|
+ "teacher_no", "VARCHAR(50)",
|
|
|
+ "teacher_photo", "VARCHAR(500)",
|
|
|
+ "certificate_image", "VARCHAR(500)",
|
|
|
+ "teacher_status", "VARCHAR(20) DEFAULT 'pending'",
|
|
|
+ "teacher_family_ids", "VARCHAR(500)"
|
|
|
+ ),
|
|
|
+ "children", Map.of(
|
|
|
+ "user_id", "BIGINT NOT NULL",
|
|
|
+ "family_id", "BIGINT NOT NULL",
|
|
|
+ "nickname", "VARCHAR(50) NOT NULL",
|
|
|
+ "age", "TINYINT NOT NULL",
|
|
|
+ "dan_level", "VARCHAR(2)",
|
|
|
+ "penalty_enabled", "TINYINT DEFAULT 1",
|
|
|
+ "theme", "VARCHAR(32) DEFAULT 'default'",
|
|
|
+ "total_points", "INT DEFAULT 0",
|
|
|
+ "streak_days", "INT DEFAULT 0",
|
|
|
+ "birthday", "DATE",
|
|
|
+ "gender", "ENUM('male','female')",
|
|
|
+ "phone", "VARCHAR(11)",
|
|
|
+ "id_card", "VARCHAR(18)",
|
|
|
+ "focus_max_daily", "TINYINT DEFAULT 3",
|
|
|
+ "focus_remaining", "TINYINT DEFAULT 3",
|
|
|
+ "focus_reset_date", "DATE"
|
|
|
+ ),
|
|
|
+ "families", Map.of(
|
|
|
+ "name", "VARCHAR(100)",
|
|
|
+ "invite_code", "VARCHAR(8) UNIQUE",
|
|
|
+ "street_id", "BIGINT",
|
|
|
+ "province", "VARCHAR(50)",
|
|
|
+ "city", "VARCHAR(50)",
|
|
|
+ "district", "VARCHAR(50)",
|
|
|
+ "street", "VARCHAR(100)",
|
|
|
+ "teacher_id", "BIGINT"
|
|
|
+ ),
|
|
|
+ "task_templates", Map.of(
|
|
|
+ "dan_level", "VARCHAR(2) NOT NULL",
|
|
|
+ "title", "VARCHAR(200) NOT NULL",
|
|
|
+ "points", "TINYINT DEFAULT 2",
|
|
|
+ "duration_minutes", "TINYINT",
|
|
|
+ "category", "VARCHAR(32)",
|
|
|
+ "is_default", "TINYINT DEFAULT 1",
|
|
|
+ "family_id", "BIGINT"
|
|
|
+ ),
|
|
|
+ "admin_task_templates", Map.of(
|
|
|
+ "title", "VARCHAR(200) NOT NULL",
|
|
|
+ "description", "TEXT",
|
|
|
+ "points", "INT DEFAULT 10",
|
|
|
+ "category", "VARCHAR(32)",
|
|
|
+ "need_review", "TINYINT DEFAULT 0",
|
|
|
+ "review_by_category", "TINYINT DEFAULT 0",
|
|
|
+ "recommended_age", "INT",
|
|
|
+ "difficulty", "ENUM('easy','medium','hard') DEFAULT 'medium'",
|
|
|
+ "is_active", "TINYINT DEFAULT 1",
|
|
|
+ "teacher_id", "BIGINT",
|
|
|
+ "complete_types", "VARCHAR(100)",
|
|
|
+ "duration", "INT",
|
|
|
+ "review_type", "VARCHAR(20)",
|
|
|
+ "task_type", "VARCHAR(20)",
|
|
|
+ "frequency", "VARCHAR(20)",
|
|
|
+ "max_frequency", "INT"
|
|
|
+ ),
|
|
|
+ "points_log", Map.of(
|
|
|
+ "child_id", "BIGINT NOT NULL",
|
|
|
+ "task_id", "BIGINT",
|
|
|
+ "amount", "INT NOT NULL",
|
|
|
+ "type", "ENUM('earn','spend','bonus','penalty','adjust','reset','refund') NOT NULL",
|
|
|
+ "description", "VARCHAR(255)",
|
|
|
+ "expire_at", "DATE",
|
|
|
+ "expired", "TINYINT DEFAULT 0"
|
|
|
+ ),
|
|
|
+ "user_operation_logs", Map.of(
|
|
|
+ "user_id", "BIGINT",
|
|
|
+ "nickname", "VARCHAR(50)",
|
|
|
+ "operation_type", "VARCHAR(20) NOT NULL",
|
|
|
+ "description", "VARCHAR(500)",
|
|
|
+ "request_path", "VARCHAR(200)",
|
|
|
+ "request_method", "VARCHAR(10)",
|
|
|
+ "request_params", "TEXT",
|
|
|
+ "response_status", "INT",
|
|
|
+ "client_ip", "VARCHAR(50)",
|
|
|
+ "user_agent", "VARCHAR(500)"
|
|
|
+ )
|
|
|
+ );
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理字段不存在的异常,自动添加字段后重试
|
|
|
+ * @param exception 原始SQL异常
|
|
|
+ * @param operation 要重试的操作
|
|
|
+ * @return 重试结果,如果成功返回null,失败返回异常消息
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public String handleMissingColumn(DataAccessException exception, Runnable operation) {
|
|
|
+ String message = exception.getMessage();
|
|
|
+
|
|
|
+ // 匹配 "Unknown column 'xxx' in 'field list'" 或 "Field 'xxx' doesn't have a default value"
|
|
|
+ Pattern pattern = Pattern.compile("Unknown column '(\\w+)' in '(?:field list|\\w+)'", Pattern.CASE_INSENSITIVE);
|
|
|
+ Matcher matcher = pattern.matcher(message);
|
|
|
+
|
|
|
+ // 尝试提取字段名
|
|
|
+ if (!matcher.find()) {
|
|
|
+ // 也可能是 "Field 'xxx' doesn't have a default value" 情况
|
|
|
+ pattern = Pattern.compile("Field '(\\w+)' doesn't have a default value", Pattern.CASE_INSENSITIVE);
|
|
|
+ matcher = pattern.matcher(message);
|
|
|
+ if (!matcher.find()) {
|
|
|
+ log.warn("无法解析字段缺失错误: {}", message);
|
|
|
+ return "无法解析字段缺失错误";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ String fieldName = matcher.group(1);
|
|
|
+
|
|
|
+ // 尝试提取表名 - 从异常消息或SQL语句中提取
|
|
|
+ String tableName = extractTableName(message);
|
|
|
+ if (tableName == null) {
|
|
|
+ log.warn("无法从错误消息中提取表名: {}", message);
|
|
|
+ return "无法提取表名";
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("检测到字段缺失: {}.{}", tableName, fieldName);
|
|
|
+
|
|
|
+ // 检查并添加字段
|
|
|
+ boolean success = addMissingColumn(tableName, fieldName);
|
|
|
+
|
|
|
+ if (!success) {
|
|
|
+ return "无法自动添加字段: " + tableName + "." + fieldName;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 重试操作
|
|
|
+ try {
|
|
|
+ operation.run();
|
|
|
+ log.info("字段自动同步成功,重试操作完成: {}.{}", tableName, fieldName);
|
|
|
+ return null;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("重试操作仍然失败: {}", e.getMessage());
|
|
|
+ return "重��失败: " + e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从异常消息中提取表名
|
|
|
+ */
|
|
|
+ private String extractTableName(String message) {
|
|
|
+ // 优先尝试从表名中提取 (tableName.field)
|
|
|
+ Pattern p = Pattern.compile("\\b(\\w+)\\.(\\w+)\\b", Pattern.CASE_INSENSITIVE);
|
|
|
+ Matcher m = p.matcher(message);
|
|
|
+ if (m.find()) {
|
|
|
+ return m.group(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 从 INSERT/UPDATE/INTO tableName 中提取
|
|
|
+ p = Pattern.compile("(?:INSERT\\s+INTO|UPDATE|INTO)\\s+(\\w+)", Pattern.CASE_INSENSITIVE);
|
|
|
+ m = p.matcher(message);
|
|
|
+ if (m.find()) {
|
|
|
+ return m.group(1);
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加缺失的列
|
|
|
+ */
|
|
|
+ private boolean addMissingColumn(String tableName, String fieldName) {
|
|
|
+ tableName = tableName.toLowerCase();
|
|
|
+
|
|
|
+ // 检查是否已经处理过
|
|
|
+ String cacheKey = tableName + "." + fieldName;
|
|
|
+ if (processedTables.contains(cacheKey)) {
|
|
|
+ log.info("字段已在缓存中,跳过: {}", cacheKey);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查表定义中是否存在该字段
|
|
|
+ Map<String, String> fields = SCHEMA_DEFINITIONS.get(tableName);
|
|
|
+ if (fields == null) {
|
|
|
+ log.warn("未找到表 {} 的字段定义", tableName);
|
|
|
+ return syncFromDatabase(tableName, fieldName);
|
|
|
+ }
|
|
|
+
|
|
|
+ String columnDef = fields.get(fieldName.toLowerCase());
|
|
|
+ if (columnDef == null) {
|
|
|
+ log.warn("表 {} 中未定义字段 {}", tableName, fieldName);
|
|
|
+ return syncFromDatabase(tableName, fieldName);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 检查字段是否已存在
|
|
|
+ boolean exists = checkColumnExists(tableName, fieldName);
|
|
|
+ if (!exists) {
|
|
|
+ String sql = String.format("ALTER TABLE %s ADD COLUMN %s %s", tableName, fieldName, columnDef);
|
|
|
+ jdbcTemplate.execute(sql);
|
|
|
+ log.info("已自动添加字段: {}.{}", tableName, fieldName);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 加入缓存
|
|
|
+ processedTables.add(cacheKey);
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("自动添加字段失败: {}.{} - {}", tableName, fieldName, e.getMessage());
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从数据库同步字段信息(当本地定义缺失时)
|
|
|
+ */
|
|
|
+ private boolean syncFromDatabase(String tableName, String fieldName) {
|
|
|
+ try {
|
|
|
+ // 查询 INFORMATION_SCHEMA 获取字段信息
|
|
|
+ String sql = "SELECT COLUMN_TYPE, IS_NULLABLE, COLUMN_DEFAULT " +
|
|
|
+ "FROM INFORMATION_SCHEMA.COLUMNS " +
|
|
|
+ "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?";
|
|
|
+
|
|
|
+ Map<String, Object> colInfo = jdbcTemplate.queryForMap(sql, tableName, fieldName + "_backup"); // 尝试查找相似字段
|
|
|
+
|
|
|
+ log.info("从数据库同步字段信息: {}.{}", tableName, fieldName);
|
|
|
+
|
|
|
+ // 简单地标记为已处理
|
|
|
+ processedTables.add(tableName + "." + fieldName);
|
|
|
+ return true;
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 字段不在数据库中,尝试创建
|
|
|
+ log.warn("无法找到字段 {} 的定义,将尝试通用处理", fieldName);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查字段是否已存在
|
|
|
+ */
|
|
|
+ private boolean checkColumnExists(String tableName, String fieldName) {
|
|
|
+ try {
|
|
|
+ String sql = "SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS " +
|
|
|
+ "WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?";
|
|
|
+ Integer count = jdbcTemplate.queryForObject(sql, Integer.class, tableName, fieldName);
|
|
|
+ return count != null && count > 0;
|
|
|
+ } catch (Exception e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 运行时同步整个表的结构
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ public void syncTableSchema(String tableName) {
|
|
|
+ tableName = tableName.toLowerCase();
|
|
|
+ Map<String, String> fields = SCHEMA_DEFINITIONS.get(tableName);
|
|
|
+ if (fields == null) {
|
|
|
+ log.warn("未找到表 {} 的定义,跳过同步", tableName);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("开始同步表 {} 的结构...", tableName);
|
|
|
+
|
|
|
+ for (Map.Entry<String, String> entry : fields.entrySet()) {
|
|
|
+ String fieldName = entry.getKey();
|
|
|
+ String columnDef = entry.getValue();
|
|
|
+
|
|
|
+ try {
|
|
|
+ boolean exists = checkColumnExists(tableName, fieldName);
|
|
|
+ if (!exists) {
|
|
|
+ String sql = String.format("ALTER TABLE %s ADD COLUMN %s %s", tableName, fieldName, columnDef);
|
|
|
+ jdbcTemplate.execute(sql);
|
|
|
+ log.info("已添加字段: {}.{}", tableName, fieldName);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("同步字段失败: {}.{} - {}", tableName, fieldName, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("表 {} 结构同步完成", tableName);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步所有已定义的表
|
|
|
+ */
|
|
|
+ public void syncAllTables() {
|
|
|
+ log.info("开始同步所有表结构...");
|
|
|
+
|
|
|
+ for (String tableName : SCHEMA_DEFINITIONS.keySet()) {
|
|
|
+ try {
|
|
|
+ syncTableSchema(tableName);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("同步表 {} 失败: {}", tableName, e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ log.info("所有表结构同步完成");
|
|
|
+ }
|
|
|
+}
|