-- 迁移：新建 dwd.dwd_goods_stock_summary 表
-- 日期：2026-02-20
-- 原因：为库存汇总数据创建 DWD 层明细表，支持按时间窗口增量加载
-- 关联任务：dataflow-field-completion Task 6.1

BEGIN;

CREATE TABLE IF NOT EXISTS dwd.dwd_goods_stock_summary (
    site_goods_id              BIGINT       NOT NULL,
    goods_name                 TEXT,
    goods_unit                 TEXT,
    goods_category_id          BIGINT,
    goods_category_second_id   BIGINT,
    category_name              TEXT,
    range_start_stock          NUMERIC(18,4),
    range_end_stock            NUMERIC(18,4),
    range_in                   NUMERIC(18,4),
    range_out                  NUMERIC(18,4),
    range_sale                 NUMERIC(18,4),
    range_sale_money           NUMERIC(18,2),
    range_inventory            NUMERIC(18,4),
    current_stock              NUMERIC(18,4),
    site_id                    BIGINT,
    tenant_id                  BIGINT,
    fetched_at                 TIMESTAMPTZ,
    PRIMARY KEY (site_goods_id, fetched_at)
);

COMMENT ON TABLE dwd.dwd_goods_stock_summary IS '库存汇总明细表（事实表）。来源：ods.goods_stock_summary。按时间窗口增量加载。';

COMMIT;
