USE medical_info_system;

CREATE TABLE IF NOT EXISTS rpa_task (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    inquiry_id BIGINT NOT NULL COMMENT '关联的查询请求ID',
    source VARCHAR(32) NOT NULL COMMENT '数据源: CNKI, WANFANG',
    status VARCHAR(32) NOT NULL DEFAULT 'PENDING' COMMENT '任务状态: PENDING, RUNNING, COMPLETED, FAILED',
    query_expression VARCHAR(2000) NOT NULL COMMENT '检索表达式',
    remark VARCHAR(1024) COMMENT '备注信息（如失败原因）',
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    started_at DATETIME COMMENT '开始执行时间',
    finished_at DATETIME COMMENT '完成时间',
    INDEX idx_inquiry_id (inquiry_id),
    INDEX idx_source (source),
    INDEX idx_status (status),
    INDEX idx_created_at (created_at),
    INDEX idx_source_status (source, status),
    FOREIGN KEY (inquiry_id) REFERENCES inquiry_requests(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
COMMENT='RPA自动化检索任务表';