-- =============================================================================
-- etl_feiqiu / core（跨平台统一维度/事实层）
-- 生成日期：2026-04-06
-- 来源：测试库（通过脚本自动导出）
--
-- 定位：屏蔽 ODS/DWD 多数据源差异，输出标准化维度和事实。
-- DWS/app 层只依赖 core，不直接查 DWD。
-- 当前数据源：飞球（dwd.*），后续可接入美团、抖音等。
-- =============================================================================

CREATE SCHEMA IF NOT EXISTS core;

-- 表
CREATE TABLE core.dim_assistant (
    assistant_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    site_id bigint NOT NULL,
    real_name text NOT NULL,
    nickname text,
    mobile text,
    level integer,
    assistant_status integer,
    leave_status integer
);

CREATE TABLE core.dim_goods_category (
    category_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    category_name text NOT NULL,
    parent_id bigint,
    level integer
);

CREATE TABLE core.dim_member (
    member_id bigint NOT NULL,
    system_member_id bigint,
    tenant_id bigint NOT NULL,
    register_site_id bigint NOT NULL,
    mobile text,
    nickname text,
    member_card_grade_name text,
    status integer
);

CREATE TABLE core.dim_site (
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    shop_name text NOT NULL,
    site_label text,
    shop_status integer,
    site_id_alias bigint
);

CREATE TABLE core.dim_table (
    table_id bigint NOT NULL,
    site_id bigint NOT NULL,
    table_name text NOT NULL,
    site_table_area_name text,
    table_price numeric(18,2)
);

CREATE TABLE core.fact_payment (
    payment_id bigint NOT NULL,
    site_id bigint NOT NULL,
    order_settle_id bigint,
    pay_type integer,
    pay_amount numeric(18,2),
    pay_time timestamp with time zone,
    status integer
);

CREATE TABLE core.fact_settlement (
    order_settle_id bigint NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    order_trade_no bigint,
    member_id bigint,
    total_amount numeric(18,2),
    actual_amount numeric(18,2),
    discount_amount numeric(18,2),
    pay_status integer,
    settle_time timestamp with time zone,
    created_at timestamp with time zone,
    updated_at timestamp with time zone
);

-- 约束（主键 / 唯一 / 外键）
ALTER TABLE core.dim_assistant ADD CONSTRAINT dim_assistant_pkey PRIMARY KEY (assistant_id);
ALTER TABLE core.dim_goods_category ADD CONSTRAINT dim_goods_category_pkey PRIMARY KEY (category_id);
ALTER TABLE core.dim_member ADD CONSTRAINT dim_member_pkey PRIMARY KEY (member_id);
ALTER TABLE core.dim_site ADD CONSTRAINT dim_site_pkey PRIMARY KEY (site_id);
ALTER TABLE core.dim_table ADD CONSTRAINT dim_table_pkey PRIMARY KEY (table_id);
ALTER TABLE core.fact_payment ADD CONSTRAINT fact_payment_pkey PRIMARY KEY (payment_id);
ALTER TABLE core.fact_settlement ADD CONSTRAINT fact_settlement_pkey PRIMARY KEY (order_settle_id);

-- 索引
CREATE INDEX idx_core_assistant_site ON core.dim_assistant USING btree (site_id);
CREATE INDEX idx_core_member_site ON core.dim_member USING btree (register_site_id);
CREATE INDEX idx_core_table_site ON core.dim_table USING btree (site_id);
CREATE INDEX idx_core_payment_site ON core.fact_payment USING btree (site_id);
CREATE INDEX idx_core_settlement_site ON core.fact_settlement USING btree (site_id);
CREATE INDEX idx_core_settlement_time ON core.fact_settlement USING btree (settle_time);

