-- ===========================================
-- 班级操行分管理系统 - v1.8 → v2.0 升级脚本
-- 字符集: utf8mb4
--
-- 变更内容:
--   1. 添加性能索引: conduct_records.semester_id
--   2. 添加性能索引: attendance_records.semester_id
--   3. 添加性能索引: conduct_records.student_id
--   4. 数据迁移: recorder_name 从 username 更新为 real_name
--   5. students 表添加 dormitory_number 列
--   6. students 表添加 points_updated_at 列
--
-- 兼容性: 使用存储过程实现幂等，phpMyAdmin 可直接执行
-- ===========================================

-- ===========================================
-- 升级步骤 1: 添加 conduct_records.semester_id 索引
-- ===========================================

DROP PROCEDURE IF EXISTS `upgrade_step`;
DELIMITER $$
CREATE PROCEDURE `upgrade_step`()
BEGIN
    IF NOT EXISTS (
        SELECT * FROM INFORMATION_SCHEMA.STATISTICS 
        WHERE TABLE_SCHEMA = DATABASE() 
          AND TABLE_NAME = 'conduct_records' 
          AND INDEX_NAME = 'idx_conduct_semester'
    ) THEN
        CREATE INDEX `idx_conduct_semester` ON `conduct_records`(`semester_id`);
    END IF;
END$$
DELIMITER ;

CALL `upgrade_step`();
DROP PROCEDURE IF EXISTS `upgrade_step`;

-- ===========================================
-- 升级步骤 2: 添加 attendance_records.semester_id 索引
-- ===========================================

DROP PROCEDURE IF EXISTS `upgrade_step`;
DELIMITER $$
CREATE PROCEDURE `upgrade_step`()
BEGIN
    IF NOT EXISTS (
        SELECT * FROM INFORMATION_SCHEMA.STATISTICS 
        WHERE TABLE_SCHEMA = DATABASE() 
          AND TABLE_NAME = 'attendance_records' 
          AND INDEX_NAME = 'idx_attendance_semester'
    ) THEN
        CREATE INDEX `idx_attendance_semester` ON `attendance_records`(`semester_id`);
    END IF;
END$$
DELIMITER ;

CALL `upgrade_step`();
DROP PROCEDURE IF EXISTS `upgrade_step`;

-- ===========================================
-- 升级步骤 3: 添加 conduct_records.student_id 索引
-- ===========================================

DROP PROCEDURE IF EXISTS `upgrade_step`;
DELIMITER $$
CREATE PROCEDURE `upgrade_step`()
BEGIN
    IF NOT EXISTS (
        SELECT * FROM INFORMATION_SCHEMA.STATISTICS 
        WHERE TABLE_SCHEMA = DATABASE() 
          AND TABLE_NAME = 'conduct_records' 
          AND INDEX_NAME = 'idx_conduct_student'
    ) THEN
        CREATE INDEX `idx_conduct_student` ON `conduct_records`(`student_id`);
    END IF;
END$$
DELIMITER ;

CALL `upgrade_step`();
DROP PROCEDURE IF EXISTS `upgrade_step`;

-- ===========================================
-- 升级步骤 4: 数据迁移 - recorder_name 从 username 更新为 real_name
-- ===========================================

DROP PROCEDURE IF EXISTS `upgrade_step`;
DELIMITER $$
CREATE PROCEDURE `upgrade_step`()
BEGIN
    UPDATE `conduct_records` cr
    INNER JOIN `users` u ON cr.recorder_id = u.user_id
    SET cr.recorder_name = u.real_name
    WHERE cr.recorder_name = u.username;
END$$
DELIMITER ;

CALL `upgrade_step`();
DROP PROCEDURE IF EXISTS `upgrade_step`;

-- ===========================================
-- 升级步骤 5: students 表添加 dormitory_number 列
-- ===========================================

DROP PROCEDURE IF EXISTS `upgrade_step`;
DELIMITER $$
CREATE PROCEDURE `upgrade_step`()
BEGIN
    IF NOT EXISTS (
        SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
        WHERE TABLE_SCHEMA = DATABASE() 
          AND TABLE_NAME = 'students' 
          AND COLUMN_NAME = 'dormitory_number'
    ) THEN
        ALTER TABLE `students` 
        ADD COLUMN `dormitory_number` VARCHAR(20) DEFAULT NULL COMMENT '宿舍号' 
        AFTER `parent_phone`;
    END IF;
END$$
DELIMITER ;

CALL `upgrade_step`();
DROP PROCEDURE IF EXISTS `upgrade_step`;

-- ===========================================
-- 升级步骤 6: students 表添加 points_updated_at 列
-- ===========================================

DROP PROCEDURE IF EXISTS `upgrade_step`;
DELIMITER $$
CREATE PROCEDURE `upgrade_step`()
BEGIN
    IF NOT EXISTS (
        SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
        WHERE TABLE_SCHEMA = DATABASE() 
          AND TABLE_NAME = 'students' 
          AND COLUMN_NAME = 'points_updated_at'
    ) THEN
        ALTER TABLE `students` 
        ADD COLUMN `points_updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT '分数最后更新时间';
    END IF;
END$$
DELIMITER ;

CALL `upgrade_step`();
DROP PROCEDURE IF EXISTS `upgrade_step`;
