-- DWS schema for aggregated / serving tables.
CREATE SCHEMA IF NOT EXISTS billiards_dws;

CREATE TABLE IF NOT EXISTS billiards_dws.dws_order_summary (
    site_id BIGINT NOT NULL,
    order_settle_id BIGINT NOT NULL,
    order_trade_no TEXT,
    order_date DATE,
    tenant_id BIGINT,
    member_id BIGINT,
    member_flag BOOLEAN,
    recharge_order_flag BOOLEAN,
    item_count INT,
    total_item_quantity NUMERIC,
    table_fee_amount NUMERIC,
    assistant_service_amount NUMERIC,
    goods_amount NUMERIC,
    group_amount NUMERIC,
    total_coupon_deduction NUMERIC,
    member_discount_amount NUMERIC,
    manual_discount_amount NUMERIC,
    order_original_amount NUMERIC,
    order_final_amount NUMERIC,
    stored_card_deduct NUMERIC,
    external_paid_amount NUMERIC,
    total_paid_amount NUMERIC,
    book_table_flow NUMERIC,
    book_assistant_flow NUMERIC,
    book_goods_flow NUMERIC,
    book_group_flow NUMERIC,
    book_order_flow NUMERIC,
    order_effective_consume_cash NUMERIC,
    order_effective_recharge_cash NUMERIC,
    order_effective_flow NUMERIC,
    refund_amount NUMERIC,
    net_income NUMERIC,
    created_at TIMESTAMPTZ DEFAULT now(),
    updated_at TIMESTAMPTZ DEFAULT now(),
    PRIMARY KEY (site_id, order_settle_id)
);

CREATE INDEX IF NOT EXISTS idx_dws_order_summary_order_date
    ON billiards_dws.dws_order_summary (order_date);

CREATE INDEX IF NOT EXISTS idx_dws_order_summary_tenant_date
    ON billiards_dws.dws_order_summary (tenant_id, order_date);

CREATE INDEX IF NOT EXISTS idx_dws_order_summary_member_date
    ON billiards_dws.dws_order_summary (member_id, order_date);

