#AI
zhou zhou
4 小时以前 51877df13075ad10ef51107f15bcd21f1661febe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.vincent.rsf.server.ai.config;
 
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.List;
 
@Component
public class AiSchemaGuard {
 
    private static final List<String> REQUIRED_TABLES = Arrays.asList(
            "sys_ai_chat_session",
            "sys_ai_chat_message",
            "sys_ai_prompt_template",
            "sys_ai_prompt_publish_log",
            "sys_ai_diagnosis_record",
            "sys_ai_diagnosis_plan",
            "sys_ai_call_log",
            "sys_ai_mcp_mount",
            "sys_ai_model_route",
            "sys_ai_diagnostic_tool_config"
    );
 
    @Resource
    private DataSource dataSource;
 
    @PostConstruct
    public void validate() {
        try (Connection connection = dataSource.getConnection()) {
            for (String table : REQUIRED_TABLES) {
                if (!tableExists(connection, table)) {
                    throw new IllegalStateException("AI feature table missing: " + table + ",请先执行 AI 迁移脚本");
                }
            }
        } catch (IllegalStateException e) {
            throw e;
        } catch (Exception e) {
            throw new IllegalStateException("AI feature schema validation failed: " + e.getMessage(), e);
        }
    }
 
    private boolean tableExists(Connection connection, String tableName) throws Exception {
        try (ResultSet resultSet = connection.getMetaData().getTables(connection.getCatalog(), null, tableName, null)) {
            if (resultSet.next()) {
                return true;
            }
        }
        try (ResultSet resultSet = connection.getMetaData().getTables(connection.getCatalog(), null, tableName.toUpperCase(), null)) {
            if (resultSet.next()) {
                return true;
            }
        }
        try (ResultSet resultSet = connection.getMetaData().getTables(connection.getCatalog(), null, tableName.toLowerCase(), null)) {
            return resultSet.next();
        }
    }
 
}