-- 考试信息管理系统数据库初始化脚本
-- 字符集：utf8mb4
-- 存储引擎：InnoDB

CREATE DATABASE IF NOT EXISTS exam_registration CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE exam_registration;

-- 用户表
CREATE TABLE IF NOT EXISTS `user` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `username` VARCHAR(50) NOT NULL COMMENT '用户名',
  `password` VARCHAR(100) NOT NULL COMMENT '密码（加密）',
  `email` VARCHAR(100) DEFAULT NULL COMMENT '邮箱',
  `phone` VARCHAR(20) DEFAULT NULL COMMENT '手机号',
  `real_name` VARCHAR(50) DEFAULT NULL COMMENT '真实姓名',
  `id_card` VARCHAR(20) DEFAULT NULL COMMENT '身份证号',
  `role` VARCHAR(20) DEFAULT 'user' COMMENT '角色：admin, user',
  `status` TINYINT DEFAULT 1 COMMENT '状态：1-正常，0-禁用',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` DATETIME DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_username` (`username`),
  KEY `idx_email` (`email`),
  KEY `idx_phone` (`phone`),
  KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表';

-- 考试表
CREATE TABLE IF NOT EXISTS `exam` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` VARCHAR(200) NOT NULL COMMENT '考试名称',
  `code` VARCHAR(50) NOT NULL COMMENT '考试代码',
  `description` TEXT COMMENT '考试描述',
  `start_time` DATETIME NOT NULL COMMENT '考试开始时间',
  `end_time` DATETIME NOT NULL COMMENT '考试结束时间',
  `registration_start` DATETIME NOT NULL COMMENT '报名开始时间',
  `registration_end` DATETIME NOT NULL COMMENT '报名截止时间',
  `max_candidates` INT DEFAULT 0 COMMENT '最大考生数（0 表示不限制）',
  `exam_fee` DECIMAL(10,2) DEFAULT 0.00 COMMENT '考试费用',
  `exam_location` VARCHAR(200) DEFAULT NULL COMMENT '考试地点',
  `subject` VARCHAR(100) DEFAULT NULL COMMENT '考试科目',
  `status` TINYINT DEFAULT 1 COMMENT '状态：1-未开始，2-进行中，3-已结束',
  `creator_id` BIGINT UNSIGNED DEFAULT NULL COMMENT '创建者 ID',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` DATETIME DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_code` (`code`),
  KEY `idx_status` (`status`),
  KEY `idx_creator_id` (`creator_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='考试表';

-- 报名表
CREATE TABLE IF NOT EXISTS `exam_registration` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户 ID',
  `exam_id` BIGINT UNSIGNED NOT NULL COMMENT '考试 ID',
  `status` TINYINT DEFAULT 0 COMMENT '状态：0-待审核，1-已通过，2-已拒绝，3-已取消',
  `payment_status` TINYINT DEFAULT 0 COMMENT '支付状态：0-未支付，1-已支付',
  `payment_time` DATETIME DEFAULT NULL COMMENT '支付时间',
  `audit_time` DATETIME DEFAULT NULL COMMENT '审核时间',
  `audit_comment` VARCHAR(500) DEFAULT NULL COMMENT '审核意见',
  `ticket_number` VARCHAR(50) DEFAULT NULL COMMENT '准考证号',
  `exam_seat` VARCHAR(20) DEFAULT NULL COMMENT '考场座位',
  `remark` VARCHAR(500) DEFAULT NULL COMMENT '备注',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` DATETIME DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_exam` (`user_id`, `exam_id`),
  UNIQUE KEY `uk_ticket_number` (`ticket_number`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_exam_id` (`exam_id`),
  KEY `idx_status` (`status`),
  KEY `idx_payment_status` (`payment_status`),
  CONSTRAINT `fk_reg_user` FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_reg_exam` FOREIGN KEY (`exam_id`) REFERENCES `exam`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='报名表';

-- 考试通知表
CREATE TABLE IF NOT EXISTS `exam_notice` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `exam_id` BIGINT UNSIGNED NOT NULL COMMENT '考试 ID',
  `title` VARCHAR(200) NOT NULL COMMENT '通知标题',
  `content` TEXT NOT NULL COMMENT '通知内容',
  `type` TINYINT DEFAULT 1 COMMENT '类型：1-普通，2-重要，3-紧急',
  `publish_time` DATETIME DEFAULT NULL COMMENT '发布时间',
  `publisher_id` BIGINT UNSIGNED DEFAULT NULL COMMENT '发布者 ID',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` DATETIME DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_exam_id` (`exam_id`),
  KEY `idx_publisher_id` (`publisher_id`),
  CONSTRAINT `fk_notice_exam` FOREIGN KEY (`exam_id`) REFERENCES `exam`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='考试通知表';

-- 考试成绩表
CREATE TABLE IF NOT EXISTS `exam_score` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` BIGINT UNSIGNED NOT NULL COMMENT '用户 ID',
  `exam_id` BIGINT UNSIGNED NOT NULL COMMENT '考试 ID',
  `score` DECIMAL(5,2) DEFAULT NULL COMMENT '分数',
  `total_score` DECIMAL(5,2) DEFAULT NULL COMMENT '总分',
  `pass` BOOLEAN DEFAULT FALSE COMMENT '是否及格',
  `rank` INT DEFAULT 0 COMMENT '排名',
  `remark` VARCHAR(500) DEFAULT NULL COMMENT '备注',
  `published` BOOLEAN DEFAULT FALSE COMMENT '是否已发布',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  `updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `deleted_at` DATETIME DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_user_exam` (`user_id`, `exam_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_exam_id` (`exam_id`),
  KEY `idx_published` (`published`),
  CONSTRAINT `fk_score_user` FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_score_exam` FOREIGN KEY (`exam_id`) REFERENCES `exam`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='考试成绩表';

-- 插入默认管理员账号（密码：admin123）
INSERT INTO `user` (`username`, `password`, `email`, `role`, `status`) 
VALUES ('admin', '$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgcfl7p92ldGxad68LJZdL17lhWy', 'admin@example.com', 'admin', 1);
