-- 迁移：新建 dwd.dwd_goods_stock_movement 表
-- 日期：2026-02-20
-- 原因：为库存变动流水创建 DWD 层明细表，按 create_time 增量加载
-- 关联任务：dataflow-field-completion Task 6.2

BEGIN;

CREATE TABLE IF NOT EXISTS dwd.dwd_goods_stock_movement (
    site_goods_stock_id        BIGINT       NOT NULL,
    tenant_id                  BIGINT,
    site_id                    BIGINT,
    site_goods_id              BIGINT,
    goods_name                 TEXT,
    goods_category_id          BIGINT,
    goods_second_category_id   BIGINT,
    unit                       TEXT,
    price                      NUMERIC(18,4),
    stock_type                 INTEGER,
    change_num                 NUMERIC(18,4),
    start_num                  NUMERIC(18,4),
    end_num                    NUMERIC(18,4),
    change_num_a               NUMERIC(18,4),
    start_num_a                NUMERIC(18,4),
    end_num_a                  NUMERIC(18,4),
    remark                     TEXT,
    operator_name              TEXT,
    create_time                TIMESTAMPTZ,
    fetched_at                 TIMESTAMPTZ,
    PRIMARY KEY (site_goods_stock_id)
);

COMMENT ON TABLE dwd.dwd_goods_stock_movement IS '库存变动流水表（事实表）。来源：ods.goods_stock_movements。按 create_time 增量加载。';

COMMIT;
