BEGIN;

CREATE TABLE tb_card_observation_effect (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMPTZ,
    creator BIGINT NOT NULL DEFAULT 0,
    updater BIGINT NOT NULL DEFAULT 0,
    event_id VARCHAR(180) NOT NULL,
    effect_type VARCHAR(50) NOT NULL,
    status INTEGER NOT NULL DEFAULT 0,
    CONSTRAINT chk_card_observation_effect_status CHECK (status IN (0, 1, 2, 3, 4))
);

COMMENT ON TABLE tb_card_observation_effect IS '卡观测事件副作用幂等处理进度';
COMMENT ON COLUMN tb_card_observation_effect.event_id IS 'Outbox事件ID';
COMMENT ON COLUMN tb_card_observation_effect.effect_type IS '副作用类型';
COMMENT ON COLUMN tb_card_observation_effect.status IS '处理状态 0-待处理 1-处理中或结果未知 2-日流量已记录 3-已扣减 4-已完成';

CREATE UNIQUE INDEX uk_card_observation_effect_event
    ON tb_card_observation_effect (event_id)
    WHERE deleted_at IS NULL;

COMMIT;
