CREATE TABLE `tb_document_import_batch`
(
    `id`               bigint UNSIGNED NOT NULL COMMENT '主键',
    `knowledge_id`     bigint UNSIGNED NOT NULL COMMENT '知识库ID',
    `import_mode`      varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '导入模式',
    `status`           varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '批次状态',
    `total_count`      int NOT NULL DEFAULT 0 COMMENT '文件总数',
    `total_bytes`      bigint NOT NULL DEFAULT 0 COMMENT '文件总字节数',
    `completed_count`  int NOT NULL DEFAULT 0 COMMENT '完成数',
    `processing_count` int NOT NULL DEFAULT 0 COMMENT '处理中数量',
    `failed_count`     int NOT NULL DEFAULT 0 COMMENT '失败数',
    `pending_count`    int NOT NULL DEFAULT 0 COMMENT '等待数',
    `started_at`       datetime NULL DEFAULT NULL COMMENT '开始时间',
    `finished_at`      datetime NULL DEFAULT NULL COMMENT '结束时间',
    `created`          datetime NULL DEFAULT NULL COMMENT '创建时间',
    `created_by`       bigint UNSIGNED NULL DEFAULT NULL COMMENT '创建人',
    `modified`         datetime NULL DEFAULT NULL COMMENT '修改时间',
    `modified_by`      bigint UNSIGNED NULL DEFAULT NULL COMMENT '修改人',
    PRIMARY KEY (`id`) USING BTREE,
    INDEX `idx_document_import_batch_knowledge_status` (`knowledge_id`, `status`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '知识库文档批量导入批次' ROW_FORMAT = DYNAMIC;

CREATE TABLE `tb_document_import_batch_item`
(
    `id`               bigint UNSIGNED NOT NULL COMMENT '主键',
    `batch_id`         bigint UNSIGNED NOT NULL COMMENT '批次ID',
    `knowledge_id`     bigint UNSIGNED NOT NULL COMMENT '知识库ID',
    `document_id`      bigint UNSIGNED NULL DEFAULT NULL COMMENT '文档ID',
    `client_file_key`  varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '客户端文件键',
    `file_name`        varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '文件名',
    `relative_path`    varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '文件夹相对路径',
    `file_size`        bigint NOT NULL DEFAULT 0 COMMENT '文件大小',
    `file_path`        varchar(2048) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '存储路径',
    `stage`            varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '当前阶段',
    `status`           varchar(16) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '当前状态',
    `error_summary`    varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '错误摘要',
    `created`          datetime NULL DEFAULT NULL COMMENT '创建时间',
    `created_by`       bigint UNSIGNED NULL DEFAULT NULL COMMENT '创建人',
    `modified`         datetime NULL DEFAULT NULL COMMENT '修改时间',
    `modified_by`      bigint UNSIGNED NULL DEFAULT NULL COMMENT '修改人',
    PRIMARY KEY (`id`) USING BTREE,
    UNIQUE INDEX `uk_document_import_batch_item_key` (`batch_id`, `client_file_key`) USING BTREE,
    INDEX `idx_document_import_batch_item_batch_status` (`batch_id`, `status`) USING BTREE,
    INDEX `idx_document_import_batch_item_knowledge_key` (`knowledge_id`, `client_file_key`) USING BTREE,
    INDEX `idx_document_import_batch_item_document` (`document_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci COMMENT = '知识库文档批量导入文件项' ROW_FORMAT = DYNAMIC;

ALTER TABLE `tb_document_import_task`
    ADD COLUMN `batch_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '批次ID' AFTER `knowledge_id`,
    ADD COLUMN `batch_item_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '批次文件项ID' AFTER `batch_id`,
    ADD INDEX `idx_document_import_task_batch_phase_status` (`batch_id`, `phase`, `status`) USING BTREE,
    ADD INDEX `idx_document_import_task_batch_item` (`batch_item_id`) USING BTREE;
