-- 合同批量下载任务表
-- [2026-02-05] 新增：用于记录和追踪合同批量下载任务

-- CreateEnum
CREATE TYPE "BatchDownloadStatus" AS ENUM ('PENDING', 'PROCESSING', 'COMPLETED', 'FAILED', 'CANCELLED');

-- CreateTable
CREATE TABLE "contract_batch_download_tasks" (
    "id" BIGSERIAL NOT NULL,
    "task_no" VARCHAR(50) NOT NULL,
    "status" "BatchDownloadStatus" NOT NULL DEFAULT 'PENDING',
    "total_contracts" INTEGER NOT NULL DEFAULT 0,
    "downloaded_count" INTEGER NOT NULL DEFAULT 0,
    "failed_count" INTEGER NOT NULL DEFAULT 0,
    "progress" INTEGER NOT NULL DEFAULT 0,
    "last_processed_order_no" VARCHAR(50),
    "result_file_url" VARCHAR(500),
    "result_file_size" BIGINT,
    "errors" JSONB,
    "operator_id" VARCHAR(50) NOT NULL,
    "filters" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "started_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),
    "expires_at" TIMESTAMP(3),

    CONSTRAINT "contract_batch_download_tasks_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "contract_batch_download_tasks_task_no_key" ON "contract_batch_download_tasks"("task_no");

-- CreateIndex
CREATE INDEX "contract_batch_download_tasks_status_idx" ON "contract_batch_download_tasks"("status");

-- CreateIndex
CREATE INDEX "contract_batch_download_tasks_operator_id_idx" ON "contract_batch_download_tasks"("operator_id");

-- CreateIndex
CREATE INDEX "contract_batch_download_tasks_created_at_idx" ON "contract_batch_download_tasks"("created_at");
