-- 创建学员状态字典类型
INSERT INTO `sys_dict_type` (`dict_name`, `dict_type`, `status`, `create_by`, `create_time`, `remark`) 
VALUES ('学员状态', 'study_student_status', '0', 'admin', NOW(), '学员状态列表（在押、释放、外出、假释等）')
ON DUPLICATE KEY UPDATE `dict_name` = '学员状态', `remark` = '学员状态列表（在押、释放、外出、假释等）';

-- 创建学员状态字典数据
-- 在押
INSERT INTO `sys_dict_data` (`dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `create_time`, `remark`) 
VALUES (1, '在押', 'incarcerated', 'study_student_status', '', 'primary', 'Y', '0', 'admin', NOW(), '在押状态')
ON DUPLICATE KEY UPDATE `dict_label` = '在押', `dict_sort` = 1, `is_default` = 'Y', `status` = '0';

-- 释放
INSERT INTO `sys_dict_data` (`dict_sort`, `dict_label`, `dict_value`, `dict_type`, `css_class`, `list_class`, `is_default`, `status`, `create_by`, `create_time`, `remark`) 
VALUES (2, '释放', 'released', 'study_student_status', '', 'success', 'N', '0', 'admin', NOW(), '释放状态')
ON DUPLICATE KEY UPDATE `dict_label` = '释放', `dict_sort` = 2, `is_default` = 'N', `status` = '0';

-- 为 sys_user 表添加学员状态字段（如果字段不存在）
SET @exist := (SELECT COUNT(*) FROM information_schema.COLUMNS 
               WHERE TABLE_SCHEMA = DATABASE() 
               AND TABLE_NAME = 'sys_user' 
               AND COLUMN_NAME = 'student_status');
SET @sqlstmt := IF(@exist = 0, 
    'ALTER TABLE `sys_user` ADD COLUMN `student_status` VARCHAR(50) DEFAULT NULL COMMENT ''学员状态（incarcerated-在押, released-释放, out-外出, parole-假释等）'' AFTER `entry_date`',
    'SELECT ''student_status字段已存在'' as result');
PREPARE stmt FROM @sqlstmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- 创建学员变更记录表
DROP TABLE IF EXISTS `study_student_change_log`;
CREATE TABLE `study_student_change_log` (
  `id` bigint NOT NULL AUTO_INCREMENT COMMENT '记录ID',
  `student_id` bigint NOT NULL COMMENT '学员ID（sys_user表的user_id）',
  `change_type` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '变更类型（status_change-状态变更, field_change-字段变更）',
  `field_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '字段名称（仅字段变更时使用）',
  `old_value` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '旧值',
  `new_value` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '新值',
  `old_label` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '旧值显示标签（用于状态等需要翻译的值）',
  `new_label` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '新值显示标签（用于状态等需要翻译的值）',
  `change_desc` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '变更描述',
  `operator_id` bigint DEFAULT NULL COMMENT '操作人ID',
  `operator_name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '操作人名称',
  `change_time` datetime NOT NULL COMMENT '变更时间',
  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `remark` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT '备注',
  PRIMARY KEY (`id`),
  KEY `idx_student_id` (`student_id`),
  KEY `idx_change_type` (`change_type`),
  KEY `idx_change_time` (`change_time`),
  KEY `idx_field_name` (`field_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='学员变更记录表';

