-- 学习闭环:训练题快照/反馈、岗位题库、入职考试(MySQL 8.x)。 -- 可重复执行;先于 aihr_20260717_practice_five_position_catalog_mysql8.sql 执行。 SET @aihr_schema = DATABASE(); SET @content_version_exists = ( SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = @aihr_schema AND table_name = 'aihr_practice_assignment' AND column_name = 'content_version' ); SET @content_version_ddl = IF(@content_version_exists = 0, 'ALTER TABLE aihr_practice_assignment ADD COLUMN content_version varchar(30) DEFAULT ''v1'' COMMENT ''派发内容版本''', 'SELECT 1'); PREPARE aihr_content_version_stmt FROM @content_version_ddl; EXECUTE aihr_content_version_stmt; DEALLOCATE PREPARE aihr_content_version_stmt; SET @question_snapshot_exists = ( SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = @aihr_schema AND table_name = 'aihr_practice_assignment' AND column_name = 'question_snapshot' ); SET @question_snapshot_ddl = IF(@question_snapshot_exists = 0, 'ALTER TABLE aihr_practice_assignment ADD COLUMN question_snapshot varchar(1000) DEFAULT NULL COMMENT ''派发时题目快照'' AFTER content_version', 'SELECT 1'); PREPARE aihr_question_snapshot_stmt FROM @question_snapshot_ddl; EXECUTE aihr_question_snapshot_stmt; DEALLOCATE PREPARE aihr_question_snapshot_stmt; SET @reference_snapshot_exists = ( SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = @aihr_schema AND table_name = 'aihr_practice_assignment' AND column_name = 'reference_answer_snapshot' ); SET @reference_snapshot_ddl = IF(@reference_snapshot_exists = 0, 'ALTER TABLE aihr_practice_assignment ADD COLUMN reference_answer_snapshot varchar(2000) DEFAULT NULL COMMENT ''派发时参考答案快照'' AFTER question_snapshot', 'SELECT 1'); PREPARE aihr_reference_snapshot_stmt FROM @reference_snapshot_ddl; EXECUTE aihr_reference_snapshot_stmt; DEALLOCATE PREPARE aihr_reference_snapshot_stmt; SET @content_hash_exists = ( SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = @aihr_schema AND table_name = 'aihr_practice_assignment' AND column_name = 'content_hash' ); SET @content_hash_ddl = IF(@content_hash_exists = 0, 'ALTER TABLE aihr_practice_assignment ADD COLUMN content_hash char(64) DEFAULT NULL COMMENT ''版本化内容哈希'' AFTER reference_answer_snapshot', 'SELECT 1'); PREPARE aihr_content_hash_stmt FROM @content_hash_ddl; EXECUTE aihr_content_hash_stmt; DEALLOCATE PREPARE aihr_content_hash_stmt; SET @content_hash_index_exists = ( SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = @aihr_schema AND table_name = 'aihr_practice_assignment' AND index_name = 'idx_aihr_assignment_recent_content' ); SET @content_hash_index_ddl = IF(@content_hash_index_exists = 0, 'ALTER TABLE aihr_practice_assignment ADD KEY idx_aihr_assignment_recent_content (tenant_id, ext_party_id, source, content_hash, create_time)', 'SELECT 1'); PREPARE aihr_content_hash_index_stmt FROM @content_hash_index_ddl; EXECUTE aihr_content_hash_index_stmt; DEALLOCATE PREPARE aihr_content_hash_index_stmt; CREATE TABLE IF NOT EXISTS `aihr_learning_question` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键', `tenant_id` varchar(20) NOT NULL DEFAULT '000000' COMMENT '租户编号', `question_code` varchar(120) NOT NULL COMMENT '题目编码', `scenario_code` varchar(80) NOT NULL COMMENT '场景编码', `position_code` varchar(80) NOT NULL COMMENT '岗位编码', `question_type` varchar(20) NOT NULL DEFAULT 'daily' COMMENT 'daily/special', `question_text` varchar(1000) NOT NULL COMMENT '题干', `reference_answer` varchar(2000) NOT NULL COMMENT '参考答案', `content_version` varchar(30) NOT NULL DEFAULT 'v1' COMMENT '内容版本', `content_hash` char(64) NOT NULL COMMENT '题目内容哈希', `enabled` tinyint NOT NULL DEFAULT 1 COMMENT '是否启用', `sort_order` int NOT NULL DEFAULT 0 COMMENT '排序', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_time` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`), UNIQUE KEY `uk_aihr_learning_question` (`tenant_id`, `question_code`), KEY `idx_aihr_learning_question_matrix` (`tenant_id`, `position_code`, `question_type`, `enabled`, `sort_order`), KEY `idx_aihr_learning_question_hash` (`tenant_id`, `content_hash`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='岗位场景学习题库'; CREATE TABLE IF NOT EXISTS `aihr_practice_question_feedback` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键', `tenant_id` varchar(20) NOT NULL DEFAULT '000000' COMMENT '租户编号', `assignment_id` bigint NOT NULL COMMENT '训练题派发ID', `ext_party_id` varchar(100) NOT NULL COMMENT '员工身份', `verdict` varchar(20) NOT NULL COMMENT 'useful/not_useful', `comment` varchar(500) DEFAULT NULL COMMENT '反馈说明', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_time` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`), UNIQUE KEY `uk_aihr_question_feedback` (`tenant_id`, `assignment_id`, `ext_party_id`), KEY `idx_aihr_question_feedback_verdict` (`tenant_id`, `verdict`, `update_time`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='每日题和专项题有用性反馈'; CREATE TABLE IF NOT EXISTS `aihr_onboard_exam` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键', `tenant_id` varchar(20) NOT NULL DEFAULT '000000' COMMENT '租户编号', `title` varchar(200) NOT NULL COMMENT '考试名称', `project_code` varchar(100) NOT NULL DEFAULT '' COMMENT '适用项目,空为全部', `position_code` varchar(80) NOT NULL DEFAULT '' COMMENT '适用岗位,空为全部', `status` varchar(20) NOT NULL DEFAULT 'DRAFT' COMMENT 'DRAFT/PUBLISHED', `total_score` int NOT NULL DEFAULT 100 COMMENT '总分', `pass_score` int NOT NULL DEFAULT 80 COMMENT '及格分', `due_date` date DEFAULT NULL COMMENT '截止日期', `created_by` varchar(100) NOT NULL COMMENT '创建人身份', `publish_request_key` varchar(100) DEFAULT NULL COMMENT '发布幂等键', `published_time` datetime DEFAULT NULL COMMENT '发布时间', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_time` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`), UNIQUE KEY `uk_aihr_onboard_exam_publish_request` (`tenant_id`, `created_by`, `publish_request_key`), KEY `idx_aihr_onboard_exam_scope` (`tenant_id`, `status`, `project_code`, `position_code`, `due_date`), KEY `idx_aihr_onboard_exam_owner` (`tenant_id`, `created_by`, `id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='主管创建的岗前/入职考试'; SET @publish_request_exists = ( SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = @aihr_schema AND table_name = 'aihr_onboard_exam' AND column_name = 'publish_request_key' ); SET @publish_request_ddl = IF(@publish_request_exists = 0, 'ALTER TABLE aihr_onboard_exam ADD COLUMN publish_request_key varchar(100) DEFAULT NULL COMMENT ''发布幂等键'' AFTER created_by', 'SELECT 1'); PREPARE aihr_publish_request_stmt FROM @publish_request_ddl; EXECUTE aihr_publish_request_stmt; DEALLOCATE PREPARE aihr_publish_request_stmt; SET @publish_request_index_exists = ( SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = @aihr_schema AND table_name = 'aihr_onboard_exam' AND index_name = 'uk_aihr_onboard_exam_publish_request' ); SET @publish_request_index_ddl = IF(@publish_request_index_exists = 0, 'ALTER TABLE aihr_onboard_exam ADD UNIQUE KEY uk_aihr_onboard_exam_publish_request (tenant_id, created_by, publish_request_key)', 'SELECT 1'); PREPARE aihr_publish_request_index_stmt FROM @publish_request_index_ddl; EXECUTE aihr_publish_request_index_stmt; DEALLOCATE PREPARE aihr_publish_request_index_stmt; CREATE TABLE IF NOT EXISTS `aihr_onboard_exam_target` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键', `tenant_id` varchar(20) NOT NULL DEFAULT '000000' COMMENT '租户编号', `exam_id` bigint NOT NULL COMMENT '考试ID', `ext_party_id` varchar(100) NOT NULL COMMENT '真实员工外部身份', `trainee_name` varchar(100) NOT NULL COMMENT '派发时员工姓名快照', `project_code` varchar(100) NOT NULL DEFAULT '' COMMENT '派发时项目快照', `position_name` varchar(100) NOT NULL DEFAULT '' COMMENT '派发时岗位快照', `create_time` datetime DEFAULT NULL COMMENT '创建时间', PRIMARY KEY (`id`), UNIQUE KEY `uk_aihr_onboard_exam_target` (`tenant_id`, `exam_id`, `ext_party_id`), KEY `idx_aihr_onboard_exam_target_party` (`tenant_id`, `ext_party_id`, `exam_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='入职考试定向人员(最多100人)'; CREATE TABLE IF NOT EXISTS `aihr_onboard_exam_question` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键', `tenant_id` varchar(20) NOT NULL DEFAULT '000000' COMMENT '租户编号', `exam_id` bigint NOT NULL COMMENT '考试ID', `question_type` varchar(20) NOT NULL COMMENT 'single/multiple/judge/short', `stem` varchar(1000) NOT NULL COMMENT '题干', `options_json` text COMMENT '选项JSON', `correct_answers_json` text NOT NULL COMMENT '正确答案或关键词JSON', `score` int NOT NULL COMMENT '分值', `explanation` varchar(1000) DEFAULT NULL COMMENT '解析', `sort_order` int NOT NULL DEFAULT 0 COMMENT '排序', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_time` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`), KEY `idx_aihr_onboard_exam_question` (`tenant_id`, `exam_id`, `sort_order`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='入职考试题目'; CREATE TABLE IF NOT EXISTS `aihr_onboard_exam_attempt` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键', `tenant_id` varchar(20) NOT NULL DEFAULT '000000' COMMENT '租户编号', `exam_id` bigint NOT NULL COMMENT '考试ID', `ext_party_id` varchar(100) NOT NULL COMMENT '员工身份', `request_key` varchar(100) NOT NULL COMMENT '提交幂等键', `status` varchar(20) NOT NULL DEFAULT 'SUBMITTED' COMMENT '提交状态', `score` int NOT NULL DEFAULT 0 COMMENT '得分', `passed` tinyint NOT NULL DEFAULT 0 COMMENT '是否通过', `submitted_time` datetime NOT NULL COMMENT '提交时间', `create_time` datetime DEFAULT NULL COMMENT '创建时间', `update_time` datetime DEFAULT NULL COMMENT '更新时间', PRIMARY KEY (`id`), UNIQUE KEY `uk_aihr_onboard_exam_attempt_request` (`tenant_id`, `exam_id`, `ext_party_id`, `request_key`), KEY `idx_aihr_onboard_exam_attempt_result` (`tenant_id`, `exam_id`, `score`, `submitted_time`), KEY `idx_aihr_onboard_exam_attempt_party` (`tenant_id`, `ext_party_id`, `submitted_time`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='入职考试作答记录'; CREATE TABLE IF NOT EXISTS `aihr_onboard_exam_answer` ( `id` bigint NOT NULL AUTO_INCREMENT COMMENT '主键', `tenant_id` varchar(20) NOT NULL DEFAULT '000000' COMMENT '租户编号', `attempt_id` bigint NOT NULL COMMENT '作答记录ID', `question_id` bigint NOT NULL COMMENT '题目ID', `answer_json` text COMMENT '员工答案JSON', `correct` tinyint NOT NULL DEFAULT 0 COMMENT '是否正确', `earned_score` int NOT NULL DEFAULT 0 COMMENT '本题得分', `create_time` datetime DEFAULT NULL COMMENT '创建时间', PRIMARY KEY (`id`), UNIQUE KEY `uk_aihr_onboard_exam_answer` (`tenant_id`, `attempt_id`, `question_id`), KEY `idx_aihr_onboard_exam_answer_question` (`tenant_id`, `question_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='入职考试逐题答案';