CREATE TABLE IF NOT EXISTS tb_iot_card_import_task (
    id BIGSERIAL PRIMARY KEY,
    task_no VARCHAR(50) NOT NULL,
    status INT NOT NULL DEFAULT 1,
    carrier_id BIGINT NOT NULL,
    batch_no VARCHAR(100),
    file_name VARCHAR(255),
    total_count INT NOT NULL DEFAULT 0,
    success_count INT NOT NULL DEFAULT 0,
    skip_count INT NOT NULL DEFAULT 0,
    fail_count INT NOT NULL DEFAULT 0,
    skipped_items JSONB DEFAULT '[]',
    failed_items JSONB DEFAULT '[]',
    started_at TIMESTAMP,
    completed_at TIMESTAMP,
    error_message TEXT,
    shop_id BIGINT,
    created_at TIMESTAMP DEFAULT NOW(),
    updated_at TIMESTAMP DEFAULT NOW(),
    deleted_at TIMESTAMP,
    creator BIGINT NOT NULL DEFAULT 0,
    updater BIGINT NOT NULL DEFAULT 0
);

CREATE UNIQUE INDEX idx_import_task_no ON tb_iot_card_import_task(task_no) WHERE deleted_at IS NULL;
CREATE INDEX idx_import_task_carrier_id ON tb_iot_card_import_task(carrier_id);
CREATE INDEX idx_import_task_shop_id ON tb_iot_card_import_task(shop_id);
CREATE INDEX idx_import_task_status ON tb_iot_card_import_task(status);
CREATE INDEX idx_import_task_created_at ON tb_iot_card_import_task(created_at);

COMMENT ON TABLE tb_iot_card_import_task IS 'IoT 卡导入任务表';
COMMENT ON COLUMN tb_iot_card_import_task.task_no IS '任务编号(IMP-YYYYMMDD-XXXXXX)';
COMMENT ON COLUMN tb_iot_card_import_task.status IS '任务状态 1-待处理 2-处理中 3-已完成 4-失败';
COMMENT ON COLUMN tb_iot_card_import_task.carrier_id IS '运营商ID';
COMMENT ON COLUMN tb_iot_card_import_task.batch_no IS '批次号';
COMMENT ON COLUMN tb_iot_card_import_task.file_name IS '原始文件名';
COMMENT ON COLUMN tb_iot_card_import_task.total_count IS '总数';
COMMENT ON COLUMN tb_iot_card_import_task.success_count IS '成功数';
COMMENT ON COLUMN tb_iot_card_import_task.skip_count IS '跳过数';
COMMENT ON COLUMN tb_iot_card_import_task.fail_count IS '失败数';
COMMENT ON COLUMN tb_iot_card_import_task.skipped_items IS '跳过记录详情';
COMMENT ON COLUMN tb_iot_card_import_task.failed_items IS '失败记录详情';
COMMENT ON COLUMN tb_iot_card_import_task.started_at IS '开始处理时间';
COMMENT ON COLUMN tb_iot_card_import_task.completed_at IS '完成时间';
COMMENT ON COLUMN tb_iot_card_import_task.error_message IS '任务级错误信息';
COMMENT ON COLUMN tb_iot_card_import_task.shop_id IS '店铺ID(发起导入的店铺)';
COMMENT ON COLUMN tb_iot_card_import_task.creator IS '创建人ID';
COMMENT ON COLUMN tb_iot_card_import_task.updater IS '更新人ID';
