-- =============================================================
-- Docker 爬虫任务日志：建表 + 菜单 + crawler 调度迁移
-- 表前缀：la_
-- 执行时机：Docker crawler 容器部署并验证可写日志后执行。
-- =============================================================

CREATE TABLE IF NOT EXISTS `la_crawler_task_log` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `task_key` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '任务标识',
  `task_name` VARCHAR(100) NOT NULL DEFAULT '' COMMENT '任务名称',
  `action` VARCHAR(64) NOT NULL DEFAULT '' COMMENT '爬虫动作',
  `cron_expression` VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'cron规则',
  `source` VARCHAR(32) NOT NULL DEFAULT 'docker' COMMENT '来源',
  `container_id` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '容器ID',
  `status` TINYINT UNSIGNED NOT NULL DEFAULT 0 COMMENT '0执行中 1成功 2失败 3跳过',
  `trigger_type` TINYINT UNSIGNED NOT NULL DEFAULT 1 COMMENT '1自动 2手动',
  `output` TEXT NULL COMMENT '执行输出',
  `error_message` TEXT NULL COMMENT '错误信息',
  `elapsed` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT '耗时秒',
  `started_at` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '开始时间',
  `finished_at` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT '结束时间',
  `create_time` INT UNSIGNED NOT NULL DEFAULT 0,
  `update_time` INT UNSIGNED NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_action_status` (`action`, `status`),
  KEY `idx_create_time` (`create_time`),
  KEY `idx_task_key` (`task_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Docker爬虫任务执行日志';

SET @system_maintenance_id := (
  SELECT `id` FROM `la_system_menu`
  WHERE `name` = '系统维护'
  ORDER BY `id` ASC
  LIMIT 1
);

INSERT INTO `la_system_menu`
  (`pid`, `type`, `name`, `icon`, `paths`, `perms`, `component`, `selected`, `params`, `sort`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
SELECT
  @system_maintenance_id, 'C', '爬虫任务日志', '', 'crawler_task_log', 'crawler.taskLog/lists',
  'crawler/task_log/index', '', '', 210, 0, 1, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
FROM DUAL
WHERE @system_maintenance_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM `la_system_menu` WHERE `perms` = 'crawler.taskLog/lists'
  );

SET @crawler_task_log_menu_id := (
  SELECT `id` FROM `la_system_menu`
  WHERE `perms` = 'crawler.taskLog/lists'
  ORDER BY `id` ASC
  LIMIT 1
);

INSERT INTO `la_system_menu`
  (`pid`, `type`, `name`, `icon`, `paths`, `perms`, `component`, `selected`, `params`, `sort`, `is_cache`, `is_show`, `is_disable`, `create_time`, `update_time`)
SELECT
  @crawler_task_log_menu_id, 'A', '任务日志详情', '', '', 'crawler.taskLog/detail',
  '', '', '', 0, 0, 0, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
FROM DUAL
WHERE @crawler_task_log_menu_id IS NOT NULL
  AND NOT EXISTS (
    SELECT 1 FROM `la_system_menu` WHERE `perms` = 'crawler.taskLog/detail'
  );

-- Docker crawler 验证上线后，停用旧 PHP crontab 中的 Python crawler 调度，避免双跑。
UPDATE `la_dev_crontab`
SET
  `status` = 2,
  `error` = 'crawler任务已迁移到Docker容器调度，请查看管理端爬虫任务日志',
  `remark` = CASE
    WHEN COALESCE(`remark`, '') LIKE '%已迁移到Docker容器调度%' THEN `remark`
    WHEN COALESCE(`remark`, '') = '' THEN '已迁移到Docker容器调度'
    ELSE CONCAT(COALESCE(`remark`, ''), '\n已迁移到Docker容器调度')
  END,
  `update_time` = UNIX_TIMESTAMP()
WHERE `command` IN ('crawler', 'truth_social', 'fifa_worldcup_news')
  AND `status` = 1;
