/*
 MIP Query Task Table
 用于存储查询词任务，抓取需要自动点击的网址
 
 Date: 2026-01-19
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for ai_mip_query_task
-- ----------------------------
DROP TABLE IF EXISTS `ai_mip_query_task`;
CREATE TABLE `ai_mip_query_task` (
  `id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  `query_word` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '查询词/关键词',
  `query_type` enum('keyword','phrase','long_tail') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'keyword' COMMENT '查询类型：关键词/短语/长尾词',
  `task_date` char(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '任务日期，格式：YYYYMMDD',
  `threshold_max` int NOT NULL DEFAULT 100 COMMENT '最大抓取数量阈值',
  `current_count` int NOT NULL DEFAULT 0 COMMENT '当前已抓取数量',
  `status` enum('ready','doing','failed','finished','closed') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT 'ready' COMMENT '任务状态：准备中/执行中/失败/完成/已关闭',
  `priority` tinyint NOT NULL DEFAULT 5 COMMENT '优先级（1-10，数字越小优先级越高）',
  `category` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '分类标签（如：医疗、教育、法律等）',
  `source_platform` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT 'baidu' COMMENT '来源平台：baidu/sogou/360等',
  `crawl_url_count` int NOT NULL DEFAULT 0 COMMENT '已爬取URL数量',
  `valid_url_count` int NOT NULL DEFAULT 0 COMMENT '有效URL数量（带广告）',
  `error_message` text CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL COMMENT '错误信息',
  `started_at` timestamp NULL DEFAULT NULL COMMENT '开始执行时间',
  `finished_at` timestamp NULL DEFAULT NULL COMMENT '完成时间',
  `closed_at` timestamp NULL DEFAULT NULL COMMENT '达到阈值关闭时间',
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `created_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT 'system' COMMENT '创建人',
  `remark` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注信息',
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `uniq_query_date`(`query_word`(191) ASC, `task_date` ASC) USING BTREE COMMENT '同一查询词每天只有一个任务',
  INDEX `idx_date_status`(`task_date` ASC, `status` ASC) USING BTREE COMMENT '按日期和状态查询',
  INDEX `idx_status_priority`(`status` ASC, `priority` ASC) USING BTREE COMMENT '按状态和优先级查询',
  INDEX `idx_category`(`category` ASC) USING BTREE COMMENT '按分类查询',
  INDEX `idx_threshold`(`threshold_max` ASC, `current_count` ASC) USING BTREE COMMENT '阈值监控',
  INDEX `idx_closed`(`closed_at` ASC) USING BTREE COMMENT '关闭时间索引'
) ENGINE = InnoDB 
  AUTO_INCREMENT = 1 
  CHARACTER SET = utf8mb4 
  COLLATE = utf8mb4_general_ci 
  COMMENT = 'MIP查询任务表 - 用于存储查询词抓取网址任务' 
  ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- 示例数据
-- ----------------------------
INSERT INTO `ai_mip_query_task` 
  (`query_word`, `query_type`, `task_date`, `threshold_max`, `priority`, `category`, `source_platform`, `remark`) 
VALUES 
  ('糖尿病治疗', 'keyword', '20260119', 50, 3, '医疗', 'baidu', '医疗类关键词测试'),
  ('在线教育平台', 'phrase', '20260119', 30, 5, '教育', 'baidu', '教育类短语测试'),
  ('法律咨询免费在线', 'long_tail', '20260119', 20, 7, '法律', 'baidu', '法律类长尾词测试');

SET FOREIGN_KEY_CHECKS = 1;
