-- 创建PLC测试任务表
CREATE TABLE IF NOT EXISTS `plc_test_task` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `project_id` varchar(50) NOT NULL COMMENT '关联的项目ID（外键）',
  `module` varchar(50) NOT NULL COMMENT '模块名称',
  `operation_mode` varchar(20) NOT NULL COMMENT '操作模式：MANUAL|AUTOMATIC',
  `selected_fields` json COMMENT '选中的字段列表（JSON格式）',
  `status` varchar(20) NOT NULL DEFAULT 'PENDING' COMMENT '任务状态：PENDING|RUNNING|SUCCESS|FAILED|STOPPED',
  `start_time` datetime COMMENT '任务开始时间',
  `end_time` datetime COMMENT '任务结束时间',
  `duration` int(11) COMMENT '耗时（毫秒）',
  `result` json COMMENT '测试结果数据（JSON格式）',
  `error_message` varchar(255) COMMENT '错误信息',
  `created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_project_id` (`project_id`),
  KEY `idx_status` (`status`),
  KEY `idx_created_at` (`created_at`),
  CONSTRAINT `fk_project_id` FOREIGN KEY (`project_id`) 
    REFERENCES `plc_address_mapping` (`project_id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='PLC测试任务表';
