-- 实训任务默认数据与教学班任务配置迁移脚本
-- 适用场景：
-- 1. 测试环境/线上环境缺少 training_task 或 training_task_class_config 表；
-- 2. 需要补齐系统内置 23 个实训任务；
-- 3. 需要给已有教学班初始化一套班级维度的实训任务配置。
--
-- 说明：
-- - training_task 是管理员维护的系统默认实训任务。
-- - training_task_class_config 是各教学班自己的实训任务配置。
-- - 本脚本使用 INSERT IGNORE / ON DUPLICATE KEY no-op，避免覆盖已经编辑过的任务内容。

SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;

START TRANSACTION;

-- 兼容旧库：教学班功能依赖 school_class 的这些字段。
SET @db_name = DATABASE();

SET @sql = IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = @db_name AND TABLE_NAME = 'school_class' AND COLUMN_NAME = 'created_by') = 0,
  'ALTER TABLE school_class ADD COLUMN created_by varchar(64) NULL COMMENT ''班级创建人教师用户ID''',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = @db_name AND TABLE_NAME = 'school_class' AND COLUMN_NAME = 'class_type') = 0,
  'ALTER TABLE school_class ADD COLUMN class_type varchar(20) NOT NULL DEFAULT ''ADMIN'' COMMENT ''班级类型：ADMIN-行政班 TEACHING-教学班''',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = @db_name AND TABLE_NAME = 'school_class' AND COLUMN_NAME = 'data_source') = 0,
  'ALTER TABLE school_class ADD COLUMN data_source varchar(20) NOT NULL DEFAULT ''MANUAL'' COMMENT ''数据来源：MANUAL-手动创建 ZHIYUN-职云同步''',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = @db_name AND TABLE_NAME = 'school_class' AND COLUMN_NAME = 'source_class_ids') = 0,
  'ALTER TABLE school_class ADD COLUMN source_class_ids text NULL COMMENT ''教学班关联的行政班ID集合JSON''',
  'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

CREATE TABLE IF NOT EXISTS training_task (
  id varchar(64) NOT NULL COMMENT '主键ID',
  task_key varchar(128) NOT NULL COMMENT '学生端页面任务标识',
  project_name varchar(128) NOT NULL COMMENT '所属项目',
  task_name varchar(128) NOT NULL COMMENT '实训任务名称',
  knowledge text NULL COMMENT '相关知识',
  background text NULL COMMENT '实训背景',
  objectives text NULL COMMENT '实训目标JSON数组',
  requirements text NULL COMMENT '实训要求',
  steps text NULL COMMENT '步骤名称JSON数组',
  material_name varchar(255) NULL COMMENT '资料名称',
  material_url varchar(500) NULL COMMENT '资料地址',
  sort int DEFAULT 0 COMMENT '排序',
  enabled bit(1) DEFAULT b'1' COMMENT '是否启用',
  created_by varchar(64) NULL COMMENT '创建人',
  create_time datetime NULL COMMENT '创建时间',
  update_time datetime NULL COMMENT '更新时间',
  PRIMARY KEY (id),
  UNIQUE KEY uk_training_task_key (task_key),
  KEY idx_training_task_project (project_name),
  KEY idx_training_task_sort (sort)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='实训任务配置';

CREATE TABLE IF NOT EXISTS training_task_class_config (
  id varchar(64) NOT NULL,
  teaching_class_id varchar(64) NOT NULL,
  task_key varchar(128) NOT NULL,
  project_name varchar(128) NOT NULL,
  task_name varchar(128) NOT NULL,
  knowledge text NULL,
  background text NULL,
  objectives text NULL,
  requirements text NULL,
  steps text NULL,
  material_name varchar(255) NULL,
  material_url varchar(500) NULL,
  sort int DEFAULT 0,
  enabled bit(1) DEFAULT b'1',
  created_by varchar(64) NULL,
  create_time datetime NULL,
  update_time datetime NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uk_training_task_class_key (teaching_class_id, task_key),
  KEY idx_training_task_class_enabled_sort (teaching_class_id, enabled, sort),
  KEY idx_training_task_class_created_by (created_by)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='教学班实训任务配置';

CREATE TEMPORARY TABLE tmp_training_task_seed (
  sort int NOT NULL,
  project_name varchar(128) NOT NULL,
  task_key varchar(128) NOT NULL,
  task_name varchar(128) NOT NULL,
  PRIMARY KEY (task_key)
) ENGINE=Memory DEFAULT CHARSET=utf8mb4;

INSERT INTO tmp_training_task_seed (sort, project_name, task_key, task_name) VALUES
(1, '互联网产品开发基础认知', 'new-product-survey', '新产品调查与分析'),
(2, '互联网产品开发基础认知', 'product-development-factors', '产品开发关键因素'),
(3, '互联网产品开发基础认知', 'product-development-process', '产品开发主要流程'),
(4, '市场洞察与需求分析', 'industry-demand-analysis', '行业需求分析'),
(5, '市场洞察与需求分析', 'competitive-environment-analysis', '竞争环境分析'),
(6, '市场洞察与需求分析', 'market-opportunity-selection', '市场机会识别与选品'),
(7, '市场洞察与需求分析', 'business-feasibility', '商业可行性论证'),
(8, '市场洞察与需求分析', 'target-user-profile', '目标用户画像'),
(9, '市场洞察与需求分析', 'consumer-behavior', '消费者需求与行为'),
(10, '市场洞察与需求分析', 'consumer-scenario', '消费场景解构'),
(11, '市场洞察与需求分析', 'feature-conversion', '需求转化为功能'),
(12, '市场洞察与需求分析', 'product-value-positioning', '产品价值主张及定位'),
(13, '产品规划与设计', 'product-structure', '产品架构设计'),
(14, '产品规划与设计', 'after-sales-service', '售后服务流程设计'),
(15, '产品规划与设计', 'requirements-doc', '产品需求说明书'),
(16, '产品开发与测试验证', 'hardware-cost', '智能硬件成本估算'),
(17, '产品开发与测试验证', 'supplier-evaluation', '供应渠道评估'),
(18, '产品开发与测试验证', 'purchase-demand-forecast', '采购需求预测'),
(19, '产品开发与测试验证', 'product-pricing', '产品定价'),
(20, '产品上线与运营推广', 'category-optimization', '品类结构优化'),
(21, '产品上线与运营推广', 'release-channel-evaluation', '产品发布渠道评估'),
(22, '产品上线与运营推广', 'promotion-effect-optimization', '投放效果评估与优化'),
(23, '产品上线与运营推广', 'product-diagnosis-iteration', '产品问题诊断与迭代');

-- 补齐管理员默认任务；已有 task_key 不覆盖，避免覆盖管理员已编辑内容。
INSERT INTO training_task (
  id, task_key, project_name, task_name, knowledge, background, objectives,
  requirements, steps, material_name, material_url, sort, enabled, created_by, create_time, update_time
)
SELECT
  UUID(),
  seed.task_key,
  seed.project_name,
  seed.task_name,
  CONCAT(seed.task_name, '任务分析由教师在实训任务管理中维护。'),
  CONCAT(seed.task_name, '围绕真实业务场景展开，要求学生完成调研、分析与成果输出。'),
  CONCAT('["理解', seed.task_name, '的核心方法","能够按步骤完成实训成果"]'),
  '按任务步骤完成信息收集、分析记录和结论输出，提交内容需要结构清晰、依据充分。',
  '["任务理解","资料收集","分析整理","成果提交"]',
  '',
  '',
  seed.sort,
  b'1',
  'system',
  NOW(),
  NOW()
FROM tmp_training_task_seed seed
ON DUPLICATE KEY UPDATE training_task.id = training_task.id;

-- 给已有教学班补齐班级维度任务；已有班级任务配置不覆盖，避免覆盖教师已编辑内容。
INSERT IGNORE INTO training_task_class_config (
  id, teaching_class_id, task_key, project_name, task_name, knowledge, background,
  objectives, requirements, steps, material_name, material_url, sort, enabled,
  created_by, create_time, update_time
)
SELECT
  UUID(),
  sc.school_class_id,
  tt.task_key,
  tt.project_name,
  tt.task_name,
  tt.knowledge,
  tt.background,
  tt.objectives,
  tt.requirements,
  tt.steps,
  tt.material_name,
  tt.material_url,
  tt.sort,
  tt.enabled,
  sc.created_by,
  NOW(),
  NOW()
FROM school_class sc
JOIN training_task tt ON 1 = 1
WHERE sc.class_type = 'TEACHING'
ORDER BY sc.school_class_id, tt.sort;

DROP TEMPORARY TABLE IF EXISTS tmp_training_task_seed;

COMMIT;

-- 执行后校验
SELECT 'training_task_count' AS check_name, COUNT(*) AS count_value FROM training_task;
SELECT 'training_task_class_config_count' AS check_name, COUNT(*) AS count_value FROM training_task_class_config;
SELECT 'teaching_class_count' AS check_name, COUNT(*) AS count_value FROM school_class WHERE class_type = 'TEACHING';
SELECT teaching_class_id, COUNT(*) AS task_count
FROM training_task_class_config
GROUP BY teaching_class_id
ORDER BY teaching_class_id;
