-- ===========================================
-- 班级操行分管理系统 - v1.0 初始数据库结构
-- 字符集: utf8mb4
--
-- 说明: 系统初始版本，创建核心表结构。
--       包含: semesters, subjects, students, users,
--             admin_roles, conduct_records, attendance_records
--
-- 兼容性: 使用 IF NOT EXISTS 实现幂等，phpMyAdmin 可直接执行
-- ===========================================

-- 学期表
CREATE TABLE IF NOT EXISTS `semesters` (
    `semester_id` INT PRIMARY KEY AUTO_INCREMENT,
    `semester_name` VARCHAR(100) NOT NULL COMMENT '学期名称',
    `start_date` DATE DEFAULT NULL COMMENT '学期开始日期',
    `end_date` DATE DEFAULT NULL COMMENT '学期结束日期',
    `is_active` TINYINT DEFAULT 0 COMMENT '是否为当前活跃学期',
    `is_archived` TINYINT DEFAULT 0 COMMENT '是否已归档',
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 科目表
CREATE TABLE IF NOT EXISTS `subjects` (
    `subject_id` INT PRIMARY KEY AUTO_INCREMENT COMMENT '科目ID',
    `subject_name` VARCHAR(50) NOT NULL COMMENT '科目名称',
    `is_active` TINYINT DEFAULT 1 COMMENT '是否启用',
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE KEY `uk_subject_name` (`subject_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 学生表
CREATE TABLE IF NOT EXISTS `students` (
    `student_id` INT PRIMARY KEY AUTO_INCREMENT,
    `student_no` VARCHAR(20) NOT NULL UNIQUE,
    `name` VARCHAR(50) NOT NULL,
    `total_points` INT DEFAULT 60,
    `parent_phone` VARCHAR(20) DEFAULT NULL,
    `status` TINYINT DEFAULT 1,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 用户表
CREATE TABLE IF NOT EXISTS `users` (
    `user_id` INT PRIMARY KEY AUTO_INCREMENT,
    `username` VARCHAR(50) NOT NULL UNIQUE,
    `password_hash` VARCHAR(64) NOT NULL,
    `real_name` VARCHAR(50) NOT NULL,
    `user_type` ENUM('student', 'parent', 'admin') NOT NULL,
    `student_id` INT DEFAULT NULL,
    `status` TINYINT DEFAULT 1,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`student_id`) REFERENCES `students`(`student_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 管理员角色表
CREATE TABLE IF NOT EXISTS `admin_roles` (
    `admin_role_id` INT PRIMARY KEY AUTO_INCREMENT,
    `user_id` INT NOT NULL,
    `role_type` ENUM('班主任', '班长', '学习委员', '考勤委员', '劳动委员', '志愿委员') NOT NULL,
    `subject_id` INT DEFAULT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`user_id`) REFERENCES `users`(`user_id`) ON DELETE CASCADE,
    FOREIGN KEY (`subject_id`) REFERENCES `subjects`(`subject_id`) ON DELETE CASCADE,
    UNIQUE KEY `uk_user_subject` (`user_id`, `subject_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 操行分记录表
CREATE TABLE IF NOT EXISTS `conduct_records` (
    `record_id` BIGINT PRIMARY KEY AUTO_INCREMENT,
    `student_id` INT NOT NULL,
    `points_change` INT NOT NULL,
    `reason` VARCHAR(255) NOT NULL,
    `recorder_id` INT NOT NULL,
    `recorder_name` VARCHAR(50) DEFAULT NULL,
    `related_id` INT DEFAULT NULL,
    `is_revoked` TINYINT DEFAULT 0,
    `revoked_by` INT DEFAULT NULL,
    `revoked_at` DATETIME DEFAULT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`student_id`) REFERENCES `students`(`student_id`) ON DELETE CASCADE,
    FOREIGN KEY (`recorder_id`) REFERENCES `users`(`user_id`),
    FOREIGN KEY (`revoked_by`) REFERENCES `users`(`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 考勤记录表
CREATE TABLE IF NOT EXISTS `attendance_records` (
    `attendance_id` INT PRIMARY KEY AUTO_INCREMENT,
    `student_id` INT NOT NULL,
    `date` DATE NOT NULL,
    `slot` ENUM('morning', 'afternoon', 'evening') DEFAULT 'morning' COMMENT '考勤时段',
    `status` ENUM('present', 'absent', 'late', 'leave') DEFAULT 'present',
    `reason` VARCHAR(255) DEFAULT NULL,
    `recorder_id` INT NOT NULL,
    `deduction_applied` TINYINT DEFAULT 0,
    `deduction_record_id` BIGINT DEFAULT NULL,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (`student_id`) REFERENCES `students`(`student_id`) ON DELETE CASCADE,
    FOREIGN KEY (`recorder_id`) REFERENCES `users`(`user_id`),
    FOREIGN KEY (`deduction_record_id`) REFERENCES `conduct_records`(`record_id`) ON DELETE SET NULL,
    UNIQUE KEY `uk_student_date_slot` (`student_id`, `date`, `slot`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 插入初始科目
INSERT IGNORE INTO `subjects` (`subject_name`) VALUES ('语文'), ('数学'), ('英语');
