-- ===========================================
-- 班级操行分管理系统 - v2.0.1 → v2.1 升级脚本
-- 主要内容：添加缺失的数据库索引，优化查询性能
-- ===========================================

DELIMITER $$

-- conduct_records 表：添加 student_id + created_at 联合索引（学生端查询历史记录）
CREATE PROCEDURE upgrade_add_conduct_student_created_idx()
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
        WHERE TABLE_SCHEMA = DATABASE()
        AND TABLE_NAME = 'conduct_records'
        AND INDEX_NAME = 'idx_student_created'
    ) THEN
        ALTER TABLE `conduct_records` ADD INDEX `idx_student_created` (`student_id`, `created_at`);
    END IF;
END$$

-- conduct_records 表：添加 recorder_id 索引（班干查询自己记录的）
CREATE PROCEDURE upgrade_add_conduct_recorder_idx()
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
        WHERE TABLE_SCHEMA = DATABASE()
        AND TABLE_NAME = 'conduct_records'
        AND INDEX_NAME = 'idx_recorder_id'
    ) THEN
        ALTER TABLE `conduct_records` ADD INDEX `idx_recorder_id` (`recorder_id`);
    END IF;
END$$

-- attendance_records 表：添加 date 索引（按日期查询考勤记录）
CREATE PROCEDURE upgrade_add_attendance_date_idx()
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
        WHERE TABLE_SCHEMA = DATABASE()
        AND TABLE_NAME = 'attendance_records'
        AND INDEX_NAME = 'idx_date'
    ) THEN
        ALTER TABLE `attendance_records` ADD INDEX `idx_date` (`date`);
    END IF;
END$$

-- login_logs 表：添加 username + created_at 联合索引（查询登录历史）
CREATE PROCEDURE upgrade_add_login_username_created_idx()
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
        WHERE TABLE_SCHEMA = DATABASE()
        AND TABLE_NAME = 'login_logs'
        AND INDEX_NAME = 'idx_username_created'
    ) THEN
        ALTER TABLE `login_logs` ADD INDEX `idx_username_created` (`username`, `created_at`);
    END IF;
END$$

-- operation_logs 表：添加 operator_id + created_at 联合索引（查询操作历史）
CREATE PROCEDURE upgrade_add_operation_operator_created_idx()
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
        WHERE TABLE_SCHEMA = DATABASE()
        AND TABLE_NAME = 'operation_logs'
        AND INDEX_NAME = 'idx_operator_created'
    ) THEN
        ALTER TABLE `operation_logs` ADD INDEX `idx_operator_created` (`operator_id`, `created_at`);
    END IF;
END$$

-- semester_archives 表：添加 semester_id 索引（查询学期归档数据）
CREATE PROCEDURE upgrade_add_archive_semester_idx()
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM INFORMATION_SCHEMA.STATISTICS
        WHERE TABLE_SCHEMA = DATABASE()
        AND TABLE_NAME = 'semester_archives'
        AND INDEX_NAME = 'idx_semester_id'
    ) THEN
        ALTER TABLE `semester_archives` ADD INDEX `idx_semester_id` (`semester_id`);
    END IF;
END$$

DELIMITER ;

-- 执行所有升级存储过程
CALL upgrade_add_conduct_student_created_idx();
CALL upgrade_add_conduct_recorder_idx();
CALL upgrade_add_attendance_date_idx();
CALL upgrade_add_login_username_created_idx();
CALL upgrade_add_operation_operator_created_idx();
CALL upgrade_add_archive_semester_idx();

-- 清理存储过程
DROP PROCEDURE IF EXISTS upgrade_add_conduct_student_created_idx;
DROP PROCEDURE IF EXISTS upgrade_add_conduct_recorder_idx;
DROP PROCEDURE IF EXISTS upgrade_add_attendance_date_idx;
DROP PROCEDURE IF EXISTS upgrade_add_login_username_created_idx;
DROP PROCEDURE IF EXISTS upgrade_add_operation_operator_created_idx;
DROP PROCEDURE IF EXISTS upgrade_add_archive_semester_idx;
