-- 创建统一导出主任务表与分片任务表

CREATE TABLE IF NOT EXISTS tb_export_task (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
    deleted_at TIMESTAMP,

    creator BIGINT NOT NULL DEFAULT 0,
    updater BIGINT NOT NULL DEFAULT 0,

    task_no VARCHAR(50) NOT NULL,
    scene VARCHAR(50) NOT NULL,
    format VARCHAR(20) NOT NULL,
    status INT NOT NULL DEFAULT 1,
    progress INT NOT NULL DEFAULT 0,

    total_rows INT NOT NULL DEFAULT 0,
    processed_rows INT NOT NULL DEFAULT 0,
    total_shards INT NOT NULL DEFAULT 0,
    success_shards INT NOT NULL DEFAULT 0,
    failed_shards INT NOT NULL DEFAULT 0,

    cancel_requested BOOLEAN NOT NULL DEFAULT FALSE,

    query_json JSONB NOT NULL DEFAULT '{}'::jsonb,
    scope_shop_ids JSONB NOT NULL DEFAULT '[]'::jsonb,

    creator_user_id BIGINT NOT NULL,
    creator_user_type INT NOT NULL,
    creator_shop_id BIGINT,
    creator_enterprise_id BIGINT,

    file_key VARCHAR(500) NOT NULL DEFAULT '',
    file_size BIGINT NOT NULL DEFAULT 0,
    error_message TEXT,

    started_at TIMESTAMP,
    completed_at TIMESTAMP
);

CREATE TABLE IF NOT EXISTS tb_export_shard_task (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMP NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
    deleted_at TIMESTAMP,

    creator BIGINT NOT NULL DEFAULT 0,
    updater BIGINT NOT NULL DEFAULT 0,

    task_id BIGINT NOT NULL,
    shard_no INT NOT NULL,
    status INT NOT NULL DEFAULT 1,

    cursor_start BIGINT NOT NULL DEFAULT 0,
    cursor_end BIGINT NOT NULL DEFAULT 0,

    row_count INT NOT NULL DEFAULT 0,
    processed_rows INT NOT NULL DEFAULT 0,

    file_key VARCHAR(500) NOT NULL DEFAULT '',
    file_size BIGINT NOT NULL DEFAULT 0,
    error_message TEXT,

    started_at TIMESTAMP,
    completed_at TIMESTAMP
);

-- 主任务索引
CREATE UNIQUE INDEX IF NOT EXISTS idx_export_task_no
    ON tb_export_task(task_no)
    WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_export_task_scene_status_created_at
    ON tb_export_task(scene, status, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_export_task_creator_user_id
    ON tb_export_task(creator_user_id);
CREATE INDEX IF NOT EXISTS idx_export_task_creator_shop_id
    ON tb_export_task(creator_shop_id);
CREATE INDEX IF NOT EXISTS idx_export_task_creator_enterprise_id
    ON tb_export_task(creator_enterprise_id);
CREATE INDEX IF NOT EXISTS idx_export_task_cancel_requested
    ON tb_export_task(cancel_requested);
CREATE INDEX IF NOT EXISTS idx_export_task_deleted_at
    ON tb_export_task(deleted_at);

-- 分片任务索引
CREATE UNIQUE INDEX IF NOT EXISTS idx_export_shard_task_task_shard_no
    ON tb_export_shard_task(task_id, shard_no)
    WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_export_shard_task_task_status
    ON tb_export_shard_task(task_id, status);
CREATE INDEX IF NOT EXISTS idx_export_shard_task_deleted_at
    ON tb_export_shard_task(deleted_at);
CREATE INDEX IF NOT EXISTS idx_export_shard_task_created_at
    ON tb_export_shard_task(created_at DESC);

-- 表注释
COMMENT ON TABLE tb_export_task IS '统一导出主任务表';
COMMENT ON TABLE tb_export_shard_task IS '统一导出分片任务表';

-- 主任务字段注释
COMMENT ON COLUMN tb_export_task.id IS '主键ID';
COMMENT ON COLUMN tb_export_task.created_at IS '创建时间';
COMMENT ON COLUMN tb_export_task.updated_at IS '更新时间';
COMMENT ON COLUMN tb_export_task.deleted_at IS '删除时间（软删除）';
COMMENT ON COLUMN tb_export_task.creator IS '创建人ID';
COMMENT ON COLUMN tb_export_task.updater IS '更新人ID';
COMMENT ON COLUMN tb_export_task.task_no IS '任务编号(EXP-YYYYMMDD-XXXXXX)';
COMMENT ON COLUMN tb_export_task.scene IS '导出场景(device/iot_card)';
COMMENT ON COLUMN tb_export_task.format IS '导出格式(xlsx/csv)';
COMMENT ON COLUMN tb_export_task.status IS '任务状态 1-待处理 2-处理中 3-已完成 4-已失败 5-已取消';
COMMENT ON COLUMN tb_export_task.progress IS '任务进度(0-100)';
COMMENT ON COLUMN tb_export_task.total_rows IS '总行数';
COMMENT ON COLUMN tb_export_task.processed_rows IS '已处理行数';
COMMENT ON COLUMN tb_export_task.total_shards IS '总分片数';
COMMENT ON COLUMN tb_export_task.success_shards IS '成功分片数';
COMMENT ON COLUMN tb_export_task.failed_shards IS '失败分片数';
COMMENT ON COLUMN tb_export_task.cancel_requested IS '是否请求取消';
COMMENT ON COLUMN tb_export_task.query_json IS '导出查询参数(JSON)';
COMMENT ON COLUMN tb_export_task.scope_shop_ids IS '导出时的数据权限店铺范围快照';
COMMENT ON COLUMN tb_export_task.creator_user_id IS '创建人用户ID';
COMMENT ON COLUMN tb_export_task.creator_user_type IS '创建人用户类型 2-平台 3-代理 4-企业';
COMMENT ON COLUMN tb_export_task.creator_shop_id IS '创建人店铺ID快照';
COMMENT ON COLUMN tb_export_task.creator_enterprise_id IS '创建人企业ID快照';
COMMENT ON COLUMN tb_export_task.file_key IS '最终产物文件Key';
COMMENT ON COLUMN tb_export_task.file_size IS '最终产物文件大小(字节)';
COMMENT ON COLUMN tb_export_task.error_message IS '错误信息';
COMMENT ON COLUMN tb_export_task.started_at IS '开始处理时间';
COMMENT ON COLUMN tb_export_task.completed_at IS '完成时间';

-- 分片任务字段注释
COMMENT ON COLUMN tb_export_shard_task.id IS '主键ID';
COMMENT ON COLUMN tb_export_shard_task.created_at IS '创建时间';
COMMENT ON COLUMN tb_export_shard_task.updated_at IS '更新时间';
COMMENT ON COLUMN tb_export_shard_task.deleted_at IS '删除时间（软删除）';
COMMENT ON COLUMN tb_export_shard_task.creator IS '创建人ID';
COMMENT ON COLUMN tb_export_shard_task.updater IS '更新人ID';
COMMENT ON COLUMN tb_export_shard_task.task_id IS '主任务ID';
COMMENT ON COLUMN tb_export_shard_task.shard_no IS '分片序号(从1开始)';
COMMENT ON COLUMN tb_export_shard_task.status IS '分片状态 1-待处理 2-处理中 3-已成功 4-已失败 5-已取消';
COMMENT ON COLUMN tb_export_shard_task.cursor_start IS '分片游标起点(不含)';
COMMENT ON COLUMN tb_export_shard_task.cursor_end IS '分片游标终点(含)';
COMMENT ON COLUMN tb_export_shard_task.row_count IS '分片行数';
COMMENT ON COLUMN tb_export_shard_task.processed_rows IS '分片已处理行数';
COMMENT ON COLUMN tb_export_shard_task.file_key IS '分片产物文件Key';
COMMENT ON COLUMN tb_export_shard_task.file_size IS '分片产物文件大小(字节)';
COMMENT ON COLUMN tb_export_shard_task.error_message IS '分片错误信息';
COMMENT ON COLUMN tb_export_shard_task.started_at IS '开始处理时间';
COMMENT ON COLUMN tb_export_shard_task.completed_at IS '完成时间';
