-- 营收统计缓存表（定时汇总，加速 Dashboard 查询）
CREATE TABLE IF NOT EXISTS rev_revenue_daily (
    id              BIGSERIAL PRIMARY KEY,
    stat_date       DATE NOT NULL,
    area            VARCHAR(50),
    customer_type   VARCHAR(20),
    total_bills     INT DEFAULT 0,
    total_amount    DECIMAL(14,2) DEFAULT 0,
    paid_amount     DECIMAL(14,2) DEFAULT 0,
    overdue_amount  DECIMAL(14,2) DEFAULT 0,
    new_customers   INT DEFAULT 0,
    created_at      TIMESTAMPTZ DEFAULT NOW(),
    UNIQUE(stat_date, area, customer_type)
);

CREATE INDEX IF NOT EXISTS idx_rev_daily_date ON rev_revenue_daily(stat_date);
CREATE INDEX IF NOT EXISTS idx_rev_daily_area ON rev_revenue_daily(area);
