package com.reading.platform;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.ActiveProfiles;

import java.util.List;
import java.util.Map;

/**
 * 数据库表结构检查工具
 */
@SpringBootTest
@ActiveProfiles("dev")
public class DatabaseInspectTool {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 检查 V20 迁移涉及的表和列
     */
    @Test
    public void checkV20Tables() {
        System.out.println("=== 检查 V20 迁移涉及的表和列 ===\n");

        String[] tables = {"student_records", "lesson_feedbacks", "lessons", "student_class_history", "class_teacher", "students"};

        for (String table : tables) {
            System.out.println("表：" + table);
            try {
                List<Map<String, Object>> columns = jdbcTemplate.queryForList(
                    "SHOW COLUMNS FROM " + table
                );
                System.out.println("  列名:");
                for (Map<String, Object> col : columns) {
                    System.out.println("    - " + col.get("Field") + " (" + col.get("Type") + ")");
                }
            } catch (Exception e) {
                System.out.println("  表不存在：" + e.getMessage());
            }
            System.out.println();
        }
    }

    /**
     * 清理 flyway_schema_history 表中的 V20 记录
     */
    @Test
    public void cleanFlywayHistory() {
        System.out.println("=== 清理 Flyway V20 记录 ===");
        int deleted = jdbcTemplate.update("DELETE FROM flyway_schema_history WHERE version = '20'");
        System.out.println("已删除 " + deleted + " 条记录");
    }
}
