-- =============================================================================
-- etl_feiqiu / dws（汇总数据层）
-- 生成日期：2026-02-25
-- 来源：测试库（通过脚本自动导出）
-- =============================================================================

CREATE SCHEMA IF NOT EXISTS dws;

-- 序列
CREATE SEQUENCE IF NOT EXISTS dws.cfg_area_category_category_id_seq AS integer;
CREATE SEQUENCE IF NOT EXISTS dws.cfg_assistant_level_price_price_id_seq AS integer;
CREATE SEQUENCE IF NOT EXISTS dws.cfg_bonus_rules_rule_id_seq AS integer;
CREATE SEQUENCE IF NOT EXISTS dws.cfg_index_parameters_param_id_seq AS integer;
CREATE SEQUENCE IF NOT EXISTS dws.cfg_performance_tier_tier_id_seq AS integer;
CREATE SEQUENCE IF NOT EXISTS dws.cfg_skill_type_skill_type_id_seq AS integer;
CREATE SEQUENCE IF NOT EXISTS dws.dws_assistant_customer_stats_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_assistant_daily_detail_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_assistant_finance_analysis_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_assistant_monthly_summary_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_assistant_order_contribution_contribution_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_assistant_recharge_commission_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_assistant_salary_calc_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_finance_daily_summary_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_finance_discount_detail_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_finance_expense_summary_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_finance_income_structure_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_finance_recharge_summary_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_index_percentile_history_history_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_member_assistant_intimacy_intimacy_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_member_assistant_relation_index_relation_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_member_consumption_summary_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_member_newconv_index_newconv_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_member_recall_index_recall_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_member_spending_power_index_spi_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_member_visit_detail_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_member_winback_index_winback_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_ml_manual_order_alloc_alloc_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_ml_manual_order_source_source_id_seq AS bigint;
CREATE SEQUENCE IF NOT EXISTS dws.dws_platform_settlement_id_seq AS bigint;

-- 表
CREATE TABLE dws.cfg_area_category (
    category_id integer DEFAULT nextval('dws.cfg_area_category_category_id_seq'::regclass) NOT NULL,
    source_area_name character varying(100) NOT NULL,
    category_code character varying(20) NOT NULL,
    category_name character varying(50) NOT NULL,
    match_type character varying(10) DEFAULT 'EXACT'::character varying NOT NULL,
    match_priority integer DEFAULT 100 NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    description text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.cfg_assistant_level_price (
    price_id integer DEFAULT nextval('dws.cfg_assistant_level_price_price_id_seq'::regclass) NOT NULL,
    level_code integer NOT NULL,
    level_name character varying(20) NOT NULL,
    base_course_price numeric(10,2) NOT NULL,
    bonus_course_price numeric(10,2) NOT NULL,
    effective_from date DEFAULT '2000-01-01'::date NOT NULL,
    effective_to date DEFAULT '9999-12-31'::date NOT NULL,
    description text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.cfg_bonus_rules (
    rule_id integer DEFAULT nextval('dws.cfg_bonus_rules_rule_id_seq'::regclass) NOT NULL,
    rule_type character varying(20) NOT NULL,
    rule_code character varying(30) NOT NULL,
    rule_name character varying(50) NOT NULL,
    threshold_hours numeric(10,2),
    rank_position integer,
    bonus_amount numeric(12,2) NOT NULL,
    is_cumulative boolean DEFAULT false NOT NULL,
    priority integer DEFAULT 0 NOT NULL,
    effective_from date DEFAULT '2000-01-01'::date NOT NULL,
    effective_to date DEFAULT '9999-12-31'::date NOT NULL,
    description text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.cfg_index_parameters (
    param_id integer DEFAULT nextval('dws.cfg_index_parameters_param_id_seq'::regclass) NOT NULL,
    index_type character varying(50) NOT NULL,
    param_name character varying(100) NOT NULL,
    param_value numeric(14,6) NOT NULL,
    description text,
    effective_from date DEFAULT CURRENT_DATE NOT NULL,
    effective_to date,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.cfg_performance_tier (
    tier_id integer DEFAULT nextval('dws.cfg_performance_tier_tier_id_seq'::regclass) NOT NULL,
    tier_code character varying(20) NOT NULL,
    tier_name character varying(50) NOT NULL,
    tier_level integer NOT NULL,
    min_hours numeric(10,2) NOT NULL,
    max_hours numeric(10,2),
    base_deduction numeric(10,2) DEFAULT 0 NOT NULL,
    bonus_deduction_ratio numeric(5,4) DEFAULT 0 NOT NULL,
    vacation_days integer DEFAULT 0 NOT NULL,
    vacation_unlimited boolean DEFAULT false NOT NULL,
    is_new_hire_tier boolean DEFAULT false NOT NULL,
    effective_from date DEFAULT '2000-01-01'::date NOT NULL,
    effective_to date DEFAULT '9999-12-31'::date NOT NULL,
    description text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.cfg_skill_type (
    skill_type_id integer DEFAULT nextval('dws.cfg_skill_type_skill_type_id_seq'::regclass) NOT NULL,
    skill_id bigint NOT NULL,
    skill_name character varying(50),
    course_type_code character varying(10) NOT NULL,
    course_type_name character varying(20) NOT NULL,
    is_active boolean DEFAULT true NOT NULL,
    description text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_assistant_customer_stats (
    id bigint DEFAULT nextval('dws.dws_assistant_customer_stats_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    assistant_id bigint NOT NULL,
    assistant_nickname character varying(50),
    member_id bigint NOT NULL,
    member_nickname character varying(100),
    member_mobile character varying(20),
    stat_date date NOT NULL,
    first_service_date date,
    last_service_date date,
    total_service_count integer DEFAULT 0 NOT NULL,
    total_service_hours numeric(10,2) DEFAULT 0 NOT NULL,
    total_service_amount numeric(12,2) DEFAULT 0 NOT NULL,
    service_count_7d integer DEFAULT 0 NOT NULL,
    service_count_10d integer DEFAULT 0 NOT NULL,
    service_count_15d integer DEFAULT 0 NOT NULL,
    service_count_30d integer DEFAULT 0 NOT NULL,
    service_count_60d integer DEFAULT 0 NOT NULL,
    service_count_90d integer DEFAULT 0 NOT NULL,
    service_hours_7d numeric(10,2) DEFAULT 0 NOT NULL,
    service_hours_10d numeric(10,2) DEFAULT 0 NOT NULL,
    service_hours_15d numeric(10,2) DEFAULT 0 NOT NULL,
    service_hours_30d numeric(10,2) DEFAULT 0 NOT NULL,
    service_hours_60d numeric(10,2) DEFAULT 0 NOT NULL,
    service_hours_90d numeric(10,2) DEFAULT 0 NOT NULL,
    service_amount_7d numeric(12,2) DEFAULT 0 NOT NULL,
    service_amount_10d numeric(12,2) DEFAULT 0 NOT NULL,
    service_amount_15d numeric(12,2) DEFAULT 0 NOT NULL,
    service_amount_30d numeric(12,2) DEFAULT 0 NOT NULL,
    service_amount_60d numeric(12,2) DEFAULT 0 NOT NULL,
    service_amount_90d numeric(12,2) DEFAULT 0 NOT NULL,
    days_since_last integer,
    is_active_7d boolean DEFAULT false NOT NULL,
    is_active_30d boolean DEFAULT false NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_assistant_daily_detail (
    id bigint DEFAULT nextval('dws.dws_assistant_daily_detail_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    assistant_id bigint NOT NULL,
    assistant_nickname character varying(50),
    stat_date date NOT NULL,
    assistant_level_code integer,
    assistant_level_name character varying(20),
    total_service_count integer DEFAULT 0 NOT NULL,
    base_service_count integer DEFAULT 0 NOT NULL,
    bonus_service_count integer DEFAULT 0 NOT NULL,
    room_service_count integer DEFAULT 0 NOT NULL,
    total_seconds integer DEFAULT 0 NOT NULL,
    base_seconds integer DEFAULT 0 NOT NULL,
    bonus_seconds integer DEFAULT 0 NOT NULL,
    room_seconds integer DEFAULT 0 NOT NULL,
    total_hours numeric(10,2) DEFAULT 0 NOT NULL,
    base_hours numeric(10,2) DEFAULT 0 NOT NULL,
    bonus_hours numeric(10,2) DEFAULT 0 NOT NULL,
    room_hours numeric(10,2) DEFAULT 0 NOT NULL,
    total_ledger_amount numeric(12,2) DEFAULT 0 NOT NULL,
    base_ledger_amount numeric(12,2) DEFAULT 0 NOT NULL,
    bonus_ledger_amount numeric(12,2) DEFAULT 0 NOT NULL,
    room_ledger_amount numeric(12,2) DEFAULT 0 NOT NULL,
    unique_customers integer DEFAULT 0 NOT NULL,
    unique_tables integer DEFAULT 0 NOT NULL,
    trashed_seconds integer DEFAULT 0 NOT NULL,
    trashed_count integer DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    penalty_minutes numeric(10,2) DEFAULT 0,
    penalty_reason text,
    is_exempt boolean DEFAULT false,
    per_hour_contribution numeric(14,2)
);

CREATE TABLE dws.dws_assistant_finance_analysis (
    id bigint DEFAULT nextval('dws.dws_assistant_finance_analysis_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    stat_date date NOT NULL,
    assistant_id bigint NOT NULL,
    assistant_nickname character varying(50),
    revenue_total numeric(14,2) DEFAULT 0 NOT NULL,
    revenue_base numeric(14,2) DEFAULT 0 NOT NULL,
    revenue_bonus numeric(14,2) DEFAULT 0 NOT NULL,
    revenue_room numeric(14,2) DEFAULT 0 NOT NULL,
    cost_daily numeric(14,2) DEFAULT 0 NOT NULL,
    gross_profit numeric(14,2) DEFAULT 0 NOT NULL,
    gross_margin numeric(5,4) DEFAULT 0 NOT NULL,
    service_count integer DEFAULT 0 NOT NULL,
    service_hours numeric(10,2) DEFAULT 0 NOT NULL,
    room_service_count integer DEFAULT 0 NOT NULL,
    room_service_hours numeric(10,2) DEFAULT 0 NOT NULL,
    unique_customers integer DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_assistant_monthly_summary (
    id bigint DEFAULT nextval('dws.dws_assistant_monthly_summary_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    assistant_id bigint NOT NULL,
    assistant_nickname character varying(50),
    stat_month date NOT NULL,
    assistant_level_code integer,
    assistant_level_name character varying(20),
    hire_date date,
    is_new_hire boolean DEFAULT false NOT NULL,
    work_days integer DEFAULT 0 NOT NULL,
    total_service_count integer DEFAULT 0 NOT NULL,
    base_service_count integer DEFAULT 0 NOT NULL,
    bonus_service_count integer DEFAULT 0 NOT NULL,
    room_service_count integer DEFAULT 0 NOT NULL,
    total_hours numeric(10,2) DEFAULT 0 NOT NULL,
    base_hours numeric(10,2) DEFAULT 0 NOT NULL,
    bonus_hours numeric(10,2) DEFAULT 0 NOT NULL,
    room_hours numeric(10,2) DEFAULT 0 NOT NULL,
    effective_hours numeric(10,2) DEFAULT 0 NOT NULL,
    trashed_hours numeric(10,2) DEFAULT 0 NOT NULL,
    total_ledger_amount numeric(12,2) DEFAULT 0 NOT NULL,
    base_ledger_amount numeric(12,2) DEFAULT 0 NOT NULL,
    bonus_ledger_amount numeric(12,2) DEFAULT 0 NOT NULL,
    room_ledger_amount numeric(12,2) DEFAULT 0 NOT NULL,
    unique_customers integer DEFAULT 0 NOT NULL,
    unique_tables integer DEFAULT 0 NOT NULL,
    avg_service_seconds numeric(10,2) DEFAULT 0 NOT NULL,
    tier_id integer,
    tier_code character varying(20),
    tier_name character varying(50),
    rank_by_hours integer,
    rank_with_ties integer,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_assistant_order_contribution (
    contribution_id bigint DEFAULT nextval('dws.dws_assistant_order_contribution_contribution_id_seq'::regclass) NOT NULL,
    site_id integer NOT NULL,
    tenant_id integer NOT NULL,
    assistant_id bigint NOT NULL,
    assistant_nickname character varying(100),
    stat_date date NOT NULL,
    order_gross_revenue numeric(14,2) DEFAULT 0,
    order_net_revenue numeric(14,2) DEFAULT 0,
    time_weighted_revenue numeric(14,2) DEFAULT 0,
    time_weighted_net_revenue numeric(14,2) DEFAULT 0,
    order_count integer DEFAULT 0,
    total_service_seconds integer DEFAULT 0,
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now()
);

CREATE TABLE dws.dws_assistant_recharge_commission (
    id bigint DEFAULT nextval('dws.dws_assistant_recharge_commission_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    assistant_id bigint NOT NULL,
    assistant_nickname character varying(50),
    commission_month date NOT NULL,
    recharge_order_id bigint,
    recharge_order_no character varying(50),
    recharge_amount numeric(12,2) DEFAULT 0 NOT NULL,
    commission_amount numeric(12,2) DEFAULT 0 NOT NULL,
    commission_ratio numeric(5,4),
    import_batch_no character varying(50),
    import_file_name character varying(200),
    import_time timestamp with time zone,
    import_user character varying(50),
    remark text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_assistant_salary_calc (
    id bigint DEFAULT nextval('dws.dws_assistant_salary_calc_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    assistant_id bigint NOT NULL,
    assistant_nickname character varying(50),
    salary_month date NOT NULL,
    assistant_level_code integer,
    assistant_level_name character varying(20),
    hire_date date,
    is_new_hire boolean DEFAULT false NOT NULL,
    effective_hours numeric(10,2) DEFAULT 0 NOT NULL,
    base_hours numeric(10,2) DEFAULT 0 NOT NULL,
    bonus_hours numeric(10,2) DEFAULT 0 NOT NULL,
    room_hours numeric(10,2) DEFAULT 0 NOT NULL,
    tier_id integer,
    tier_code character varying(20),
    tier_name character varying(50),
    rank_with_ties integer,
    base_course_price numeric(10,2) DEFAULT 0 NOT NULL,
    bonus_course_price numeric(10,2) DEFAULT 0 NOT NULL,
    base_deduction numeric(10,2) DEFAULT 0 NOT NULL,
    bonus_deduction_ratio numeric(5,4) DEFAULT 0 NOT NULL,
    base_income numeric(12,2) DEFAULT 0 NOT NULL,
    bonus_income numeric(12,2) DEFAULT 0 NOT NULL,
    room_income numeric(12,2) DEFAULT 0 NOT NULL,
    total_course_income numeric(12,2) DEFAULT 0 NOT NULL,
    sprint_bonus numeric(12,2) DEFAULT 0 NOT NULL,
    top_rank_bonus numeric(12,2) DEFAULT 0 NOT NULL,
    recharge_commission numeric(12,2) DEFAULT 0 NOT NULL,
    other_bonus numeric(12,2) DEFAULT 0 NOT NULL,
    total_bonus numeric(12,2) DEFAULT 0 NOT NULL,
    gross_salary numeric(12,2) DEFAULT 0 NOT NULL,
    vacation_days integer DEFAULT 0 NOT NULL,
    vacation_unlimited boolean DEFAULT false NOT NULL,
    calc_notes text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_finance_daily_summary (
    id bigint DEFAULT nextval('dws.dws_finance_daily_summary_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    stat_date date NOT NULL,
    gross_amount numeric(14,2) DEFAULT 0 NOT NULL,
    table_fee_amount numeric(14,2) DEFAULT 0 NOT NULL,
    goods_amount numeric(14,2) DEFAULT 0 NOT NULL,
    assistant_pd_amount numeric(14,2) DEFAULT 0 NOT NULL,
    assistant_cx_amount numeric(14,2) DEFAULT 0 NOT NULL,
    discount_total numeric(14,2) DEFAULT 0 NOT NULL,
    discount_groupbuy numeric(14,2) DEFAULT 0 NOT NULL,
    discount_vip numeric(14,2) DEFAULT 0 NOT NULL,
    discount_gift_card numeric(14,2) DEFAULT 0 NOT NULL,
    discount_manual numeric(14,2) DEFAULT 0 NOT NULL,
    discount_rounding numeric(14,2) DEFAULT 0 NOT NULL,
    discount_other numeric(14,2) DEFAULT 0 NOT NULL,
    confirmed_income numeric(14,2) DEFAULT 0 NOT NULL,
    cash_inflow_total numeric(14,2) DEFAULT 0 NOT NULL,
    cash_pay_amount numeric(14,2) DEFAULT 0 NOT NULL,
    groupbuy_pay_amount numeric(14,2) DEFAULT 0 NOT NULL,
    platform_settlement_amount numeric(14,2) DEFAULT 0 NOT NULL,
    platform_fee_amount numeric(14,2) DEFAULT 0 NOT NULL,
    recharge_cash_inflow numeric(14,2) DEFAULT 0 NOT NULL,
    card_consume_total numeric(14,2) DEFAULT 0 NOT NULL,
    cash_card_consume numeric(14,2) DEFAULT 0 NOT NULL,
    gift_card_consume numeric(14,2) DEFAULT 0 NOT NULL,
    cash_outflow_total numeric(14,2) DEFAULT 0 NOT NULL,
    cash_balance_change numeric(14,2) DEFAULT 0 NOT NULL,
    recharge_count integer DEFAULT 0 NOT NULL,
    recharge_total numeric(14,2) DEFAULT 0 NOT NULL,
    recharge_cash numeric(14,2) DEFAULT 0 NOT NULL,
    recharge_gift numeric(14,2) DEFAULT 0 NOT NULL,
    first_recharge_count integer DEFAULT 0 NOT NULL,
    first_recharge_amount numeric(14,2) DEFAULT 0 NOT NULL,
    renewal_count integer DEFAULT 0 NOT NULL,
    renewal_amount numeric(14,2) DEFAULT 0 NOT NULL,
    order_count integer DEFAULT 0 NOT NULL,
    member_order_count integer DEFAULT 0 NOT NULL,
    guest_order_count integer DEFAULT 0 NOT NULL,
    avg_order_amount numeric(12,2) DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_finance_discount_detail (
    id bigint DEFAULT nextval('dws.dws_finance_discount_detail_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    stat_date date NOT NULL,
    discount_type_code character varying(30) NOT NULL,
    discount_type_name character varying(50) NOT NULL,
    discount_amount numeric(14,2) DEFAULT 0 NOT NULL,
    discount_ratio numeric(5,4) DEFAULT 0 NOT NULL,
    usage_count integer DEFAULT 0 NOT NULL,
    affected_orders integer DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_finance_expense_summary (
    id bigint DEFAULT nextval('dws.dws_finance_expense_summary_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    expense_month date NOT NULL,
    expense_type_code character varying(30) NOT NULL,
    expense_type_name character varying(50) NOT NULL,
    expense_category character varying(20),
    expense_amount numeric(14,2) DEFAULT 0 NOT NULL,
    expense_detail text,
    import_batch_no character varying(50),
    import_file_name character varying(200),
    import_time timestamp with time zone,
    import_user character varying(50),
    remark text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_finance_income_structure (
    id bigint DEFAULT nextval('dws.dws_finance_income_structure_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    stat_date date NOT NULL,
    structure_type character varying(20) NOT NULL,
    category_code character varying(30) NOT NULL,
    category_name character varying(50) NOT NULL,
    income_amount numeric(14,2) DEFAULT 0 NOT NULL,
    income_ratio numeric(5,4) DEFAULT 0 NOT NULL,
    order_count integer DEFAULT 0 NOT NULL,
    duration_minutes integer DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_finance_recharge_summary (
    id bigint DEFAULT nextval('dws.dws_finance_recharge_summary_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    stat_date date NOT NULL,
    recharge_count integer DEFAULT 0 NOT NULL,
    recharge_total numeric(14,2) DEFAULT 0 NOT NULL,
    recharge_cash numeric(14,2) DEFAULT 0 NOT NULL,
    recharge_gift numeric(14,2) DEFAULT 0 NOT NULL,
    first_recharge_count integer DEFAULT 0 NOT NULL,
    first_recharge_cash numeric(14,2) DEFAULT 0 NOT NULL,
    first_recharge_gift numeric(14,2) DEFAULT 0 NOT NULL,
    first_recharge_total numeric(14,2) DEFAULT 0 NOT NULL,
    renewal_count integer DEFAULT 0 NOT NULL,
    renewal_cash numeric(14,2) DEFAULT 0 NOT NULL,
    renewal_gift numeric(14,2) DEFAULT 0 NOT NULL,
    renewal_total numeric(14,2) DEFAULT 0 NOT NULL,
    recharge_member_count integer DEFAULT 0 NOT NULL,
    new_member_count integer DEFAULT 0 NOT NULL,
    total_card_balance numeric(14,2) DEFAULT 0 NOT NULL,
    cash_card_balance numeric(14,2) DEFAULT 0 NOT NULL,
    gift_card_balance numeric(14,2) DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_goods_stock_daily_summary (
    site_id bigint NOT NULL,
    tenant_id bigint,
    stat_date date NOT NULL,
    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,
    range_end_stock numeric,
    range_in numeric,
    range_out numeric,
    range_sale numeric,
    range_sale_money numeric(12,2),
    range_inventory numeric,
    current_stock numeric,
    stat_period text DEFAULT 'daily'::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_goods_stock_monthly_summary (
    site_id bigint NOT NULL,
    tenant_id bigint,
    stat_date date NOT NULL,
    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,
    range_end_stock numeric,
    range_in numeric,
    range_out numeric,
    range_sale numeric,
    range_sale_money numeric(12,2),
    range_inventory numeric,
    current_stock numeric,
    stat_period text DEFAULT 'monthly'::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_goods_stock_weekly_summary (
    site_id bigint NOT NULL,
    tenant_id bigint,
    stat_date date NOT NULL,
    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,
    range_end_stock numeric,
    range_in numeric,
    range_out numeric,
    range_sale numeric,
    range_sale_money numeric(12,2),
    range_inventory numeric,
    current_stock numeric,
    stat_period text DEFAULT 'weekly'::text NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_index_percentile_history (
    history_id bigint DEFAULT nextval('dws.dws_index_percentile_history_history_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    index_type character varying(50) NOT NULL,
    calc_time timestamp with time zone NOT NULL,
    percentile_5 numeric(14,6),
    percentile_95 numeric(14,6),
    percentile_5_smoothed numeric(14,6),
    percentile_95_smoothed numeric(14,6),
    record_count integer,
    min_raw_score numeric(14,6),
    max_raw_score numeric(14,6),
    avg_raw_score numeric(14,6),
    created_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_member_assistant_intimacy (
    intimacy_id bigint DEFAULT nextval('dws.dws_member_assistant_intimacy_intimacy_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    member_id bigint NOT NULL,
    assistant_id bigint NOT NULL,
    session_count integer DEFAULT 0 NOT NULL,
    total_duration_minutes integer DEFAULT 0 NOT NULL,
    basic_session_count integer DEFAULT 0 NOT NULL,
    incentive_session_count integer DEFAULT 0 NOT NULL,
    days_since_last_session integer,
    attributed_recharge_count integer DEFAULT 0 NOT NULL,
    attributed_recharge_amount numeric(14,2) DEFAULT 0 NOT NULL,
    score_frequency numeric(10,4),
    score_recency numeric(10,4),
    score_recharge numeric(10,4),
    score_duration numeric(10,4),
    burst_multiplier numeric(6,4),
    raw_score numeric(14,6),
    display_score numeric(4,2),
    calc_time timestamp with time zone DEFAULT now() NOT NULL,
    calc_version integer DEFAULT 1 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_member_assistant_relation_index (
    relation_id bigint DEFAULT nextval('dws.dws_member_assistant_relation_index_relation_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    member_id bigint NOT NULL,
    assistant_id bigint NOT NULL,
    session_count integer DEFAULT 0 NOT NULL,
    total_duration_minutes integer DEFAULT 0 NOT NULL,
    basic_session_count integer DEFAULT 0 NOT NULL,
    incentive_session_count integer DEFAULT 0 NOT NULL,
    days_since_last_session integer,
    rs_f numeric(14,6) DEFAULT 0 NOT NULL,
    rs_d numeric(14,6) DEFAULT 0 NOT NULL,
    rs_r numeric(14,6) DEFAULT 0 NOT NULL,
    rs_raw numeric(14,6) DEFAULT 0 NOT NULL,
    rs_display numeric(4,2) DEFAULT 0 NOT NULL,
    os_share numeric(10,6) DEFAULT 0 NOT NULL,
    os_label character varying(20) DEFAULT 'POOL'::character varying NOT NULL,
    os_rank integer,
    ms_f_short numeric(14,6) DEFAULT 0 NOT NULL,
    ms_f_long numeric(14,6) DEFAULT 0 NOT NULL,
    ms_raw numeric(14,6) DEFAULT 0 NOT NULL,
    ms_display numeric(4,2) DEFAULT 0 NOT NULL,
    ml_order_count integer DEFAULT 0 NOT NULL,
    ml_allocated_amount numeric(14,2) DEFAULT 0 NOT NULL,
    ml_raw numeric(14,6) DEFAULT 0 NOT NULL,
    ml_display numeric(4,2) DEFAULT 0 NOT NULL,
    calc_time timestamp with time zone DEFAULT now() NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_member_consumption_summary (
    id bigint DEFAULT nextval('dws.dws_member_consumption_summary_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    member_id bigint NOT NULL,
    stat_date date NOT NULL,
    member_nickname character varying(100),
    member_mobile character varying(20),
    card_grade_name character varying(50),
    register_date date,
    first_consume_date date,
    last_consume_date date,
    total_visit_count integer DEFAULT 0 NOT NULL,
    total_consume_amount numeric(14,2) DEFAULT 0 NOT NULL,
    total_recharge_amount numeric(14,2) DEFAULT 0 NOT NULL,
    total_table_fee numeric(14,2) DEFAULT 0 NOT NULL,
    total_goods_amount numeric(14,2) DEFAULT 0 NOT NULL,
    total_assistant_amount numeric(14,2) DEFAULT 0 NOT NULL,
    visit_count_7d integer DEFAULT 0 NOT NULL,
    visit_count_10d integer DEFAULT 0 NOT NULL,
    visit_count_15d integer DEFAULT 0 NOT NULL,
    visit_count_30d integer DEFAULT 0 NOT NULL,
    visit_count_60d integer DEFAULT 0 NOT NULL,
    visit_count_90d integer DEFAULT 0 NOT NULL,
    [REDACTED] numeric(14,2) DEFAULT 0 NOT NULL,
    [REDACTED] numeric(14,2) DEFAULT 0 NOT NULL,
    [REDACTED] numeric(14,2) DEFAULT 0 NOT NULL,
    [REDACTED] numeric(14,2) DEFAULT 0 NOT NULL,
    [REDACTED] numeric(14,2) DEFAULT 0 NOT NULL,
    [REDACTED] numeric(14,2) DEFAULT 0 NOT NULL,
    cash_card_balance numeric(14,2) DEFAULT 0 NOT NULL,
    gift_card_balance numeric(14,2) DEFAULT 0 NOT NULL,
    total_card_balance numeric(14,2) DEFAULT 0 NOT NULL,
    days_since_last integer,
    is_active_7d boolean DEFAULT false NOT NULL,
    is_active_30d boolean DEFAULT false NOT NULL,
    is_active_90d boolean DEFAULT false NOT NULL,
    customer_tier character varying(20),
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    recharge_count_30d integer DEFAULT 0,
    [REDACTED] integer DEFAULT 0,
    [REDACTED] integer DEFAULT 0,
    [REDACTED] numeric(14,2) DEFAULT 0,
    [REDACTED] numeric(14,2) DEFAULT 0,
    [REDACTED] numeric(14,2) DEFAULT 0,
    avg_ticket_amount numeric(14,2) DEFAULT 0
);

CREATE TABLE dws.dws_member_newconv_index (
    newconv_id bigint DEFAULT nextval('dws.dws_member_newconv_index_newconv_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    member_id bigint NOT NULL,
    status character varying(30),
    segment character varying(10),
    member_create_time timestamp with time zone,
    first_visit_time timestamp with time zone,
    last_visit_time timestamp with time zone,
    last_recharge_time timestamp with time zone,
    t_v numeric(6,2),
    t_r numeric(6,2),
    t_a numeric(6,2),
    visits_14d integer DEFAULT 0 NOT NULL,
    visits_60d integer DEFAULT 0 NOT NULL,
    visits_total integer DEFAULT 0 NOT NULL,
    spend_30d numeric(14,2) DEFAULT 0 NOT NULL,
    spend_180d numeric(14,2) DEFAULT 0 NOT NULL,
    sv_balance numeric(14,2) DEFAULT 0 NOT NULL,
    [REDACTED] numeric(14,2) DEFAULT 0 NOT NULL,
    interval_count integer DEFAULT 0 NOT NULL,
    need_new numeric(10,4),
    salvage_new numeric(10,4),
    recharge_new numeric(10,4),
    value_new numeric(10,4),
    welcome_new numeric(10,4),
    raw_score_welcome numeric(14,6),
    raw_score_convert numeric(14,6),
    raw_score numeric(14,6),
    display_score_welcome numeric(4,2),
    display_score_convert numeric(4,2),
    display_score numeric(4,2),
    last_wechat_touch_time timestamp with time zone,
    calc_time timestamp with time zone DEFAULT now() NOT NULL,
    calc_version integer DEFAULT 1 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_member_recall_index (
    recall_id bigint DEFAULT nextval('dws.dws_member_recall_index_recall_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    member_id bigint NOT NULL,
    days_since_last_visit integer,
    visit_interval_median numeric(10,2),
    visit_interval_mad numeric(10,2),
    days_since_first_visit integer,
    days_since_last_recharge integer,
    visits_last_14_days integer DEFAULT 0 NOT NULL,
    visits_last_60_days integer DEFAULT 0 NOT NULL,
    score_overdue numeric(10,4),
    score_new_bonus numeric(10,4),
    score_recharge_bonus numeric(10,4),
    score_hot_drop numeric(10,4),
    raw_score numeric(14,6),
    display_score numeric(4,2),
    calc_time timestamp with time zone DEFAULT now() NOT NULL,
    calc_version integer DEFAULT 1 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_member_spending_power_index (
    spi_id bigint DEFAULT nextval('dws.dws_member_spending_power_index_spi_id_seq'::regclass) NOT NULL,
    site_id integer NOT NULL,
    member_id bigint NOT NULL,
    spend_30 numeric(14,2) DEFAULT 0,
    spend_90 numeric(14,2) DEFAULT 0,
    recharge_90 numeric(14,2) DEFAULT 0,
    orders_30 integer DEFAULT 0,
    orders_90 integer DEFAULT 0,
    visit_days_30 integer DEFAULT 0,
    visit_days_90 integer DEFAULT 0,
    avg_ticket_90 numeric(14,2) DEFAULT 0,
    active_weeks_90 integer DEFAULT 0,
    [REDACTED] numeric(14,2) DEFAULT 0,
    score_level_raw numeric(10,4) DEFAULT 0,
    score_speed_raw numeric(10,4) DEFAULT 0,
    score_stability_raw numeric(10,4) DEFAULT 0,
    score_level_display numeric(5,2) DEFAULT 0,
    score_speed_display numeric(5,2) DEFAULT 0,
    score_stability_display numeric(5,2) DEFAULT 0,
    raw_score numeric(10,4) DEFAULT 0,
    display_score numeric(5,2) DEFAULT 0,
    calc_time timestamp with time zone DEFAULT now(),
    created_at timestamp with time zone DEFAULT now(),
    updated_at timestamp with time zone DEFAULT now()
);

CREATE TABLE dws.dws_member_visit_detail (
    id bigint DEFAULT nextval('dws.dws_member_visit_detail_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    member_id bigint NOT NULL,
    order_settle_id bigint NOT NULL,
    visit_date date NOT NULL,
    visit_time timestamp with time zone,
    member_nickname character varying(100),
    member_mobile character varying(20),
    member_birthday date,
    table_id bigint,
    table_name character varying(50),
    area_name character varying(50),
    area_category character varying(20),
    table_fee numeric(12,2) DEFAULT 0 NOT NULL,
    goods_amount numeric(12,2) DEFAULT 0 NOT NULL,
    assistant_amount numeric(12,2) DEFAULT 0 NOT NULL,
    total_consume numeric(12,2) DEFAULT 0 NOT NULL,
    total_discount numeric(12,2) DEFAULT 0 NOT NULL,
    actual_pay numeric(12,2) DEFAULT 0 NOT NULL,
    cash_pay numeric(12,2) DEFAULT 0 NOT NULL,
    cash_card_pay numeric(12,2) DEFAULT 0 NOT NULL,
    gift_card_pay numeric(12,2) DEFAULT 0 NOT NULL,
    groupbuy_pay numeric(12,2) DEFAULT 0 NOT NULL,
    table_duration_min integer DEFAULT 0 NOT NULL,
    assistant_duration_min integer DEFAULT 0 NOT NULL,
    assistant_services jsonb,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_member_winback_index (
    winback_id bigint DEFAULT nextval('dws.dws_member_winback_index_winback_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    member_id bigint NOT NULL,
    status character varying(30),
    segment character varying(10),
    member_create_time timestamp with time zone,
    first_visit_time timestamp with time zone,
    last_visit_time timestamp with time zone,
    last_recharge_time timestamp with time zone,
    t_v numeric(6,2),
    t_r numeric(6,2),
    t_a numeric(6,2),
    visits_14d integer DEFAULT 0 NOT NULL,
    visits_60d integer DEFAULT 0 NOT NULL,
    visits_total integer DEFAULT 0 NOT NULL,
    spend_30d numeric(14,2) DEFAULT 0 NOT NULL,
    spend_180d numeric(14,2) DEFAULT 0 NOT NULL,
    sv_balance numeric(14,2) DEFAULT 0 NOT NULL,
    [REDACTED] numeric(14,2) DEFAULT 0 NOT NULL,
    interval_count integer DEFAULT 0 NOT NULL,
    overdue_old numeric(10,4),
    drop_old numeric(10,4),
    recharge_old numeric(10,4),
    value_old numeric(10,4),
    raw_score numeric(14,6),
    display_score numeric(4,2),
    last_wechat_touch_time timestamp with time zone,
    calc_time timestamp with time zone DEFAULT now() NOT NULL,
    calc_version integer DEFAULT 1 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL,
    overdue_cdf_p numeric(10,4),
    ideal_interval_days numeric(10,2),
    ideal_next_visit_date date
);

CREATE TABLE dws.dws_ml_manual_order_alloc (
    alloc_id bigint DEFAULT nextval('dws.dws_ml_manual_order_alloc_alloc_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    biz_date date NOT NULL,
    external_id character varying(128) NOT NULL,
    member_id bigint DEFAULT 0 NOT NULL,
    pay_time timestamp with time zone NOT NULL,
    order_amount numeric(14,2) DEFAULT 0 NOT NULL,
    assistant_id bigint NOT NULL,
    assistant_name character varying(128),
    share_ratio numeric(14,8) DEFAULT 0 NOT NULL,
    allocated_amount numeric(14,2) DEFAULT 0 NOT NULL,
    currency character varying(16) DEFAULT 'CNY'::character varying NOT NULL,
    import_scope_key character varying(128) NOT NULL,
    import_batch_no character varying(64) NOT NULL,
    import_file_name character varying(255) NOT NULL,
    import_time timestamp with time zone DEFAULT now() NOT NULL,
    import_user character varying(64),
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_ml_manual_order_source (
    source_id bigint DEFAULT nextval('dws.dws_ml_manual_order_source_source_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    biz_date date NOT NULL,
    external_id character varying(128) NOT NULL,
    member_id bigint DEFAULT 0 NOT NULL,
    pay_time timestamp with time zone NOT NULL,
    order_amount numeric(14,2) DEFAULT 0 NOT NULL,
    currency character varying(16) DEFAULT 'CNY'::character varying NOT NULL,
    assistant_id_1 bigint,
    assistant_name_1 character varying(128),
    assistant_id_2 bigint,
    assistant_name_2 character varying(128),
    assistant_id_3 bigint,
    assistant_name_3 character varying(128),
    assistant_id_4 bigint,
    assistant_name_4 character varying(128),
    assistant_id_5 bigint,
    assistant_name_5 character varying(128),
    import_batch_no character varying(64) NOT NULL,
    import_file_name character varying(255) NOT NULL,
    import_scope_key character varying(128) NOT NULL,
    import_time timestamp with time zone DEFAULT now() NOT NULL,
    import_user character varying(64),
    row_no integer NOT NULL,
    remark text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_order_summary (
    site_id bigint NOT NULL,
    order_settle_id bigint NOT NULL,
    order_trade_no character varying(64),
    order_date date NOT NULL,
    tenant_id bigint NOT NULL,
    member_id bigint,
    member_flag boolean DEFAULT false NOT NULL,
    recharge_order_flag boolean DEFAULT false NOT NULL,
    item_count integer DEFAULT 0 NOT NULL,
    total_item_quantity integer DEFAULT 0 NOT NULL,
    table_fee_amount numeric(14,2) DEFAULT 0 NOT NULL,
    assistant_service_amount numeric(14,2) DEFAULT 0 NOT NULL,
    goods_amount numeric(14,2) DEFAULT 0 NOT NULL,
    group_amount numeric(14,2) DEFAULT 0 NOT NULL,
    total_coupon_deduction numeric(14,2) DEFAULT 0 NOT NULL,
    member_discount_amount numeric(14,2) DEFAULT 0 NOT NULL,
    manual_discount_amount numeric(14,2) DEFAULT 0 NOT NULL,
    order_original_amount numeric(14,2) DEFAULT 0 NOT NULL,
    order_final_amount numeric(14,2) DEFAULT 0 NOT NULL,
    stored_card_deduct numeric(14,2) DEFAULT 0 NOT NULL,
    external_paid_amount numeric(14,2) DEFAULT 0 NOT NULL,
    total_paid_amount numeric(14,2) DEFAULT 0 NOT NULL,
    book_table_flow numeric(14,2) DEFAULT 0 NOT NULL,
    book_assistant_flow numeric(14,2) DEFAULT 0 NOT NULL,
    book_goods_flow numeric(14,2) DEFAULT 0 NOT NULL,
    book_group_flow numeric(14,2) DEFAULT 0 NOT NULL,
    book_order_flow numeric(14,2) DEFAULT 0 NOT NULL,
    order_effective_consume_cash numeric(14,2) DEFAULT 0 NOT NULL,
    order_effective_recharge_cash numeric(14,2) DEFAULT 0 NOT NULL,
    order_effective_flow numeric(14,2) DEFAULT 0 NOT NULL,
    refund_amount numeric(14,2) DEFAULT 0 NOT NULL,
    net_income numeric(14,2) DEFAULT 0 NOT NULL,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

CREATE TABLE dws.dws_platform_settlement (
    id bigint DEFAULT nextval('dws.dws_platform_settlement_id_seq'::regclass) NOT NULL,
    site_id bigint NOT NULL,
    tenant_id bigint NOT NULL,
    settlement_date date NOT NULL,
    platform_type character varying(30) NOT NULL,
    platform_name character varying(50),
    platform_order_no character varying(100),
    order_settle_id bigint,
    settlement_amount numeric(14,2) DEFAULT 0 NOT NULL,
    commission_amount numeric(14,2) DEFAULT 0 NOT NULL,
    service_fee numeric(14,2) DEFAULT 0 NOT NULL,
    gross_amount numeric(14,2) DEFAULT 0 NOT NULL,
    import_batch_no character varying(50),
    import_file_name character varying(200),
    import_time timestamp with time zone,
    import_user character varying(50),
    remark text,
    created_at timestamp with time zone DEFAULT now() NOT NULL,
    updated_at timestamp with time zone DEFAULT now() NOT NULL
);

-- 约束（主键 / 唯一 / 外键）
ALTER TABLE dws.cfg_area_category ADD CONSTRAINT cfg_area_category_pkey PRIMARY KEY (category_id);
ALTER TABLE dws.cfg_area_category ADD CONSTRAINT uk_cfg_area_category UNIQUE (source_area_name);
ALTER TABLE dws.cfg_assistant_level_price ADD CONSTRAINT cfg_assistant_level_price_pkey PRIMARY KEY (price_id);
ALTER TABLE dws.cfg_assistant_level_price ADD CONSTRAINT uk_cfg_assistant_level_price UNIQUE (level_code, effective_from);
ALTER TABLE dws.cfg_bonus_rules ADD CONSTRAINT cfg_bonus_rules_pkey PRIMARY KEY (rule_id);
ALTER TABLE dws.cfg_bonus_rules ADD CONSTRAINT uk_cfg_bonus_rules UNIQUE (rule_type, rule_code, effective_from);
ALTER TABLE dws.cfg_index_parameters ADD CONSTRAINT cfg_index_parameters_pkey PRIMARY KEY (param_id);
ALTER TABLE dws.cfg_index_parameters ADD CONSTRAINT uk_cfg_index_parameters UNIQUE (index_type, param_name, effective_from);
ALTER TABLE dws.cfg_performance_tier ADD CONSTRAINT cfg_performance_tier_pkey PRIMARY KEY (tier_id);
ALTER TABLE dws.cfg_performance_tier ADD CONSTRAINT uk_cfg_performance_tier UNIQUE (tier_code, effective_from);
ALTER TABLE dws.cfg_skill_type ADD CONSTRAINT cfg_skill_type_pkey PRIMARY KEY (skill_type_id);
ALTER TABLE dws.cfg_skill_type ADD CONSTRAINT uk_cfg_skill_type UNIQUE (skill_id);
ALTER TABLE dws.dws_assistant_customer_stats ADD CONSTRAINT dws_assistant_customer_stats_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_assistant_customer_stats ADD CONSTRAINT uk_dws_assistant_customer UNIQUE (site_id, assistant_id, member_id, stat_date);
ALTER TABLE dws.dws_assistant_daily_detail ADD CONSTRAINT dws_assistant_daily_detail_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_assistant_daily_detail ADD CONSTRAINT uk_dws_assistant_daily UNIQUE (site_id, assistant_id, stat_date);
ALTER TABLE dws.dws_assistant_finance_analysis ADD CONSTRAINT dws_assistant_finance_analysis_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_assistant_finance_analysis ADD CONSTRAINT uk_dws_assistant_finance UNIQUE (site_id, stat_date, assistant_id);
ALTER TABLE dws.dws_assistant_monthly_summary ADD CONSTRAINT dws_assistant_monthly_summary_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_assistant_monthly_summary ADD CONSTRAINT uk_dws_assistant_monthly UNIQUE (site_id, assistant_id, stat_month, assistant_level_code);
ALTER TABLE dws.dws_assistant_order_contribution ADD CONSTRAINT dws_assistant_order_contribution_pkey PRIMARY KEY (contribution_id);
ALTER TABLE dws.dws_assistant_recharge_commission ADD CONSTRAINT dws_assistant_recharge_commission_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_assistant_salary_calc ADD CONSTRAINT dws_assistant_salary_calc_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_assistant_salary_calc ADD CONSTRAINT uk_dws_assistant_salary UNIQUE (site_id, assistant_id, salary_month, assistant_level_code);
ALTER TABLE dws.dws_finance_daily_summary ADD CONSTRAINT dws_finance_daily_summary_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_finance_daily_summary ADD CONSTRAINT uk_dws_finance_daily UNIQUE (site_id, stat_date);
ALTER TABLE dws.dws_finance_discount_detail ADD CONSTRAINT dws_finance_discount_detail_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_finance_discount_detail ADD CONSTRAINT uk_dws_finance_discount_detail UNIQUE (site_id, stat_date, discount_type_code);
ALTER TABLE dws.dws_finance_expense_summary ADD CONSTRAINT dws_finance_expense_summary_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_finance_expense_summary ADD CONSTRAINT uk_dws_finance_expense UNIQUE (site_id, expense_month, expense_type_code, import_batch_no);
ALTER TABLE dws.dws_finance_income_structure ADD CONSTRAINT dws_finance_income_structure_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_finance_income_structure ADD CONSTRAINT uk_dws_finance_income_structure UNIQUE (site_id, stat_date, structure_type, category_code);
ALTER TABLE dws.dws_finance_recharge_summary ADD CONSTRAINT dws_finance_recharge_summary_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_finance_recharge_summary ADD CONSTRAINT uk_dws_finance_recharge UNIQUE (site_id, stat_date);
ALTER TABLE dws.dws_goods_stock_daily_summary ADD CONSTRAINT dws_goods_stock_daily_summary_pkey PRIMARY KEY (site_id, stat_date, site_goods_id);
ALTER TABLE dws.dws_goods_stock_monthly_summary ADD CONSTRAINT dws_goods_stock_monthly_summary_pkey PRIMARY KEY (site_id, stat_date, site_goods_id);
ALTER TABLE dws.dws_goods_stock_weekly_summary ADD CONSTRAINT dws_goods_stock_weekly_summary_pkey PRIMARY KEY (site_id, stat_date, site_goods_id);
ALTER TABLE dws.dws_index_percentile_history ADD CONSTRAINT dws_index_percentile_history_pkey PRIMARY KEY (history_id);
ALTER TABLE dws.dws_index_percentile_history ADD CONSTRAINT uk_dws_index_percentile_history UNIQUE (site_id, index_type, calc_time);
ALTER TABLE dws.dws_member_assistant_intimacy ADD CONSTRAINT dws_member_assistant_intimacy_pkey PRIMARY KEY (intimacy_id);
ALTER TABLE dws.dws_member_assistant_intimacy ADD CONSTRAINT uk_dws_member_assistant_intimacy UNIQUE (site_id, member_id, assistant_id);
ALTER TABLE dws.dws_member_assistant_relation_index ADD CONSTRAINT dws_member_assistant_relation_index_pkey PRIMARY KEY (relation_id);
ALTER TABLE dws.dws_member_assistant_relation_index ADD CONSTRAINT uk_dws_member_assistant_relation_index UNIQUE (site_id, member_id, assistant_id);
ALTER TABLE dws.dws_member_consumption_summary ADD CONSTRAINT dws_member_consumption_summary_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_member_consumption_summary ADD CONSTRAINT uk_dws_member_consumption UNIQUE (site_id, member_id, stat_date);
ALTER TABLE dws.dws_member_newconv_index ADD CONSTRAINT dws_member_newconv_index_pkey PRIMARY KEY (newconv_id);
ALTER TABLE dws.dws_member_newconv_index ADD CONSTRAINT uk_dws_member_newconv UNIQUE (site_id, member_id);
ALTER TABLE dws.dws_member_recall_index ADD CONSTRAINT dws_member_recall_index_pkey PRIMARY KEY (recall_id);
ALTER TABLE dws.dws_member_recall_index ADD CONSTRAINT uk_dws_member_recall UNIQUE (site_id, member_id);
ALTER TABLE dws.dws_member_spending_power_index ADD CONSTRAINT dws_member_spending_power_index_pkey PRIMARY KEY (spi_id);
ALTER TABLE dws.dws_member_visit_detail ADD CONSTRAINT dws_member_visit_detail_pkey PRIMARY KEY (id);
ALTER TABLE dws.dws_member_visit_detail ADD CONSTRAINT uk_dws_member_visit UNIQUE (site_id, member_id, order_settle_id);
ALTER TABLE dws.dws_member_winback_index ADD CONSTRAINT dws_member_winback_index_pkey PRIMARY KEY (winback_id);
ALTER TABLE dws.dws_member_winback_index ADD CONSTRAINT uk_dws_member_winback UNIQUE (site_id, member_id);
ALTER TABLE dws.dws_ml_manual_order_alloc ADD CONSTRAINT dws_ml_manual_order_alloc_pkey PRIMARY KEY (alloc_id);
ALTER TABLE dws.dws_ml_manual_order_alloc ADD CONSTRAINT uk_dws_ml_manual_order_alloc UNIQUE (site_id, external_id, assistant_id);
ALTER TABLE dws.dws_ml_manual_order_source ADD CONSTRAINT dws_ml_manual_order_source_pkey PRIMARY KEY (source_id);
ALTER TABLE dws.dws_ml_manual_order_source ADD CONSTRAINT uk_dws_ml_manual_order_source UNIQUE (site_id, external_id, import_scope_key, row_no);
ALTER TABLE dws.dws_order_summary ADD CONSTRAINT [REDACTED]PRIMARY KEY (site_id, order_settle_id);
ALTER TABLE dws.dws_platform_settlement ADD CONSTRAINT dws_platform_settlement_pkey PRIMARY KEY (id);

-- 索引
CREATE INDEX idx_cfg_area_category_code ON dws.cfg_area_category USING btree (category_code);
CREATE INDEX idx_cfg_assistant_level_price_effective ON dws.cfg_assistant_level_price USING btree (effective_from, effective_to);
CREATE INDEX idx_cfg_bonus_rules_effective ON dws.cfg_bonus_rules USING btree (effective_from, effective_to);
CREATE INDEX idx_cfg_bonus_rules_type ON dws.cfg_bonus_rules USING btree (rule_type);
CREATE INDEX idx_cfg_index_params_effective ON dws.cfg_index_parameters USING btree (effective_from, effective_to);
CREATE INDEX idx_cfg_index_params_type ON dws.cfg_index_parameters USING btree (index_type);
CREATE INDEX idx_cfg_performance_tier_effective ON dws.cfg_performance_tier USING btree (effective_from, effective_to);
CREATE INDEX idx_cfg_skill_type_course ON dws.cfg_skill_type USING btree (course_type_code);
CREATE INDEX idx_dws_assistant_customer_asst ON dws.dws_assistant_customer_stats USING btree (assistant_id, stat_date);
CREATE INDEX idx_dws_assistant_customer_date ON dws.dws_assistant_customer_stats USING btree (stat_date);
CREATE INDEX idx_dws_assistant_customer_member ON dws.dws_assistant_customer_stats USING btree (member_id, stat_date);
CREATE INDEX idx_dws_assistant_daily_asst_date ON dws.dws_assistant_daily_detail USING btree (assistant_id, stat_date);
CREATE INDEX idx_dws_assistant_daily_date ON dws.dws_assistant_daily_detail USING btree (stat_date);
CREATE INDEX idx_dws_assistant_daily_site_date ON dws.dws_assistant_daily_detail USING btree (site_id, stat_date);
CREATE INDEX idx_dws_assistant_finance_asst ON dws.dws_assistant_finance_analysis USING btree (assistant_id, stat_date);
CREATE INDEX idx_dws_assistant_finance_date ON dws.dws_assistant_finance_analysis USING btree (stat_date);
CREATE INDEX idx_dws_assistant_monthly_asst ON dws.dws_assistant_monthly_summary USING btree (assistant_id, stat_month);
CREATE INDEX idx_dws_assistant_monthly_month ON dws.dws_assistant_monthly_summary USING btree (stat_month);
CREATE INDEX idx_dws_assistant_monthly_tier ON dws.dws_assistant_monthly_summary USING btree (tier_code);
CREATE UNIQUE INDEX idx_aoc_site_assistant_date ON dws.dws_assistant_order_contribution USING btree (site_id, assistant_id, stat_date);
CREATE INDEX idx_aoc_stat_date ON dws.dws_assistant_order_contribution USING btree (site_id, stat_date);
CREATE INDEX idx_dws_assistant_commission_asst ON dws.dws_assistant_recharge_commission USING btree (assistant_id, commission_month);
CREATE INDEX idx_dws_assistant_commission_batch ON dws.dws_assistant_recharge_commission USING btree (import_batch_no);
CREATE INDEX idx_dws_assistant_commission_month ON dws.dws_assistant_recharge_commission USING btree (commission_month);
CREATE INDEX idx_dws_assistant_salary_asst ON dws.dws_assistant_salary_calc USING btree (assistant_id, salary_month);
CREATE INDEX idx_dws_assistant_salary_month ON dws.dws_assistant_salary_calc USING btree (salary_month);
CREATE INDEX idx_dws_finance_daily_date ON dws.dws_finance_daily_summary USING btree (stat_date);
CREATE INDEX idx_dws_finance_daily_site ON dws.dws_finance_daily_summary USING btree (site_id, stat_date);
CREATE INDEX idx_dws_finance_discount_date ON dws.dws_finance_discount_detail USING btree (stat_date);
CREATE INDEX idx_dws_finance_discount_type ON dws.dws_finance_discount_detail USING btree (discount_type_code);
CREATE INDEX idx_dws_finance_expense_batch ON dws.dws_finance_expense_summary USING btree (import_batch_no);
CREATE INDEX idx_dws_finance_expense_month ON dws.dws_finance_expense_summary USING btree (expense_month);
CREATE INDEX idx_dws_finance_expense_type ON dws.dws_finance_expense_summary USING btree (expense_type_code);
CREATE INDEX idx_dws_finance_income_date ON dws.dws_finance_income_structure USING btree (stat_date);
CREATE INDEX idx_dws_finance_income_type ON dws.dws_finance_income_structure USING btree (structure_type, category_code);
CREATE INDEX idx_dws_finance_recharge_date ON dws.dws_finance_recharge_summary USING btree (stat_date);
CREATE INDEX idx_dws_goods_stock_daily_date ON dws.dws_goods_stock_daily_summary USING btree (stat_date);
CREATE INDEX idx_dws_goods_stock_daily_goods ON dws.dws_goods_stock_daily_summary USING btree (site_goods_id, stat_date);
CREATE INDEX idx_dws_goods_stock_daily_site ON dws.dws_goods_stock_daily_summary USING btree (site_id, stat_date);
CREATE INDEX idx_dws_goods_stock_monthly_date ON dws.dws_goods_stock_monthly_summary USING btree (stat_date);
CREATE INDEX idx_dws_goods_stock_monthly_goods ON dws.dws_goods_stock_monthly_summary USING btree (site_goods_id, stat_date);
CREATE INDEX idx_dws_goods_stock_monthly_site ON dws.dws_goods_stock_monthly_summary USING btree (site_id, stat_date);
CREATE INDEX idx_dws_goods_stock_weekly_date ON dws.dws_goods_stock_weekly_summary USING btree (stat_date);
CREATE INDEX idx_dws_goods_stock_weekly_goods ON dws.dws_goods_stock_weekly_summary USING btree (site_goods_id, stat_date);
CREATE INDEX idx_dws_goods_stock_weekly_site ON dws.dws_goods_stock_weekly_summary USING btree (site_id, stat_date);
CREATE INDEX idx_dws_percentile_history ON dws.dws_index_percentile_history USING btree (site_id, index_type, calc_time DESC);
CREATE INDEX idx_dws_intimacy_assistant ON dws.dws_member_assistant_intimacy USING btree (site_id, assistant_id, display_score DESC);
CREATE INDEX idx_dws_intimacy_member ON dws.dws_member_assistant_intimacy USING btree (site_id, member_id, display_score DESC);
CREATE INDEX idx_dws_relation_assistant ON dws.dws_member_assistant_relation_index USING btree (site_id, assistant_id, rs_display DESC);
CREATE INDEX idx_dws_relation_calc_time ON dws.dws_member_assistant_relation_index USING btree (calc_time);
CREATE INDEX idx_dws_relation_member ON dws.dws_member_assistant_relation_index USING btree (site_id, member_id, os_share DESC);
CREATE INDEX idx_dws_member_consumption_date ON dws.dws_member_consumption_summary USING btree (stat_date);
CREATE INDEX idx_dws_member_consumption_member ON dws.dws_member_consumption_summary USING btree (member_id, stat_date);
CREATE INDEX idx_dws_member_consumption_tier ON dws.dws_member_consumption_summary USING btree (customer_tier);
CREATE INDEX idx_dws_newconv_display ON dws.dws_member_newconv_index USING btree (site_id, display_score DESC);
CREATE INDEX idx_dws_recall_display ON dws.dws_member_recall_index USING btree (site_id, display_score DESC);
CREATE INDEX idx_spi_display_score ON dws.dws_member_spending_power_index USING btree (site_id, display_score DESC);
CREATE UNIQUE INDEX idx_spi_site_member ON dws.dws_member_spending_power_index USING btree (site_id, member_id);
CREATE INDEX idx_dws_member_visit_date ON dws.dws_member_visit_detail USING btree (visit_date);
CREATE INDEX idx_dws_member_visit_member ON dws.dws_member_visit_detail USING btree (member_id, visit_date);
CREATE INDEX idx_dws_member_visit_order ON dws.dws_member_visit_detail USING btree (order_settle_id);
CREATE INDEX idx_dws_winback_display ON dws.dws_member_winback_index USING btree (site_id, display_score DESC);
CREATE INDEX idx_dws_ml_alloc_member_assistant ON dws.dws_ml_manual_order_alloc USING btree (site_id, member_id, assistant_id);
CREATE INDEX idx_dws_ml_alloc_scope ON dws.dws_ml_manual_order_alloc USING btree (site_id, biz_date);
CREATE INDEX idx_dws_ml_source_external ON dws.dws_ml_manual_order_source USING btree (site_id, external_id);
CREATE INDEX idx_dws_ml_source_scope ON dws.dws_ml_manual_order_source USING btree (site_id, biz_date);
CREATE INDEX idx_dws_order_summary_member ON dws.dws_order_summary USING btree (member_id, order_date);
CREATE INDEX idx_dws_order_summary_site_date ON dws.dws_order_summary USING btree (site_id, order_date);
CREATE INDEX idx_dws_order_summary_trade_no ON dws.dws_order_summary USING btree (order_trade_no);
CREATE INDEX idx_dws_platform_settlement_batch ON dws.dws_platform_settlement USING btree (import_batch_no);
CREATE INDEX idx_dws_platform_settlement_date ON dws.dws_platform_settlement USING btree (settlement_date);
CREATE INDEX idx_dws_platform_settlement_order ON dws.dws_platform_settlement USING btree (order_settle_id);
CREATE INDEX idx_dws_platform_settlement_platform ON dws.dws_platform_settlement USING btree (platform_type);
CREATE INDEX idx_mv_assistant_daily_l1 ON dws.mv_dws_assistant_daily_detail_l1 USING btree (site_id, stat_date, assistant_id);
CREATE INDEX idx_mv_assistant_daily_l2 ON dws.mv_dws_assistant_daily_detail_l2 USING btree (site_id, stat_date, assistant_id);
CREATE INDEX idx_mv_assistant_daily_l3 ON dws.mv_dws_assistant_daily_detail_l3 USING btree (site_id, stat_date, assistant_id);
CREATE INDEX idx_mv_assistant_daily_l4 ON dws.mv_dws_assistant_daily_detail_l4 USING btree (site_id, stat_date, assistant_id);
CREATE INDEX idx_mv_finance_daily_l1 ON dws.mv_dws_finance_daily_summary_l1 USING btree (site_id, stat_date);
CREATE INDEX idx_mv_finance_daily_l2 ON dws.mv_dws_finance_daily_summary_l2 USING btree (site_id, stat_date);
CREATE INDEX idx_mv_finance_daily_l3 ON dws.mv_dws_finance_daily_summary_l3 USING btree (site_id, stat_date);
CREATE INDEX idx_mv_finance_daily_l4 ON dws.mv_dws_finance_daily_summary_l4 USING btree (site_id, stat_date);

-- 视图
CREATE OR REPLACE VIEW dws.v_member_recall_priority AS
SELECT dws_member_winback_index.site_id,
    dws_member_winback_index.tenant_id,
    dws_member_winback_index.member_id,
    'WBI'::character varying(10) AS index_type,
    dws_member_winback_index.status,
    dws_member_winback_index.segment,
    dws_member_winback_index.member_create_time,
    dws_member_winback_index.first_visit_time,
    dws_member_winback_index.last_visit_time,
    dws_member_winback_index.last_recharge_time,
    dws_member_winback_index.t_v,
    dws_member_winback_index.t_r,
    dws_member_winback_index.t_a,
    dws_member_winback_index.visits_14d,
    dws_member_winback_index.visits_60d,
    dws_member_winback_index.visits_total,
    dws_member_winback_index.spend_30d,
    dws_member_winback_index.spend_180d,
    dws_member_winback_index.sv_balance,
    dws_member_winback_index.recharge_60d_amt,
    NULL::numeric(10,4) AS need_new,
    NULL::numeric(10,4) AS salvage_new,
    NULL::numeric(10,4) AS recharge_new,
    NULL::numeric(10,4) AS value_new,
    NULL::numeric(10,4) AS welcome_new,
    NULL::numeric(14,6) AS raw_score_welcome,
    NULL::numeric(14,6) AS raw_score_convert,
    dws_member_winback_index.raw_score,
    NULL::numeric(4,2) AS display_score_welcome,
    NULL::numeric(4,2) AS display_score_convert,
    dws_member_winback_index.display_score,
    dws_member_winback_index.last_wechat_touch_time,
    dws_member_winback_index.calc_time
   FROM dws.dws_member_winback_index
UNION ALL
 SELECT dws_member_newconv_index.site_id,
    dws_member_newconv_index.tenant_id,
    dws_member_newconv_index.member_id,
    'NCI'::character varying(10) AS index_type,
    dws_member_newconv_index.status,
    dws_member_newconv_index.segment,
    dws_member_newconv_index.member_create_time,
    dws_member_newconv_index.first_visit_time,
    dws_member_newconv_index.last_visit_time,
    dws_member_newconv_index.last_recharge_time,
    dws_member_newconv_index.t_v,
    dws_member_newconv_index.t_r,
    dws_member_newconv_index.t_a,
    dws_member_newconv_index.visits_14d,
    dws_member_newconv_index.visits_60d,
    dws_member_newconv_index.visits_total,
    dws_member_newconv_index.spend_30d,
    dws_member_newconv_index.spend_180d,
    dws_member_newconv_index.sv_balance,
    dws_member_newconv_index.recharge_60d_amt,
    dws_member_newconv_index.need_new,
    dws_member_newconv_index.salvage_new,
    dws_member_newconv_index.recharge_new,
    dws_member_newconv_index.value_new,
    dws_member_newconv_index.welcome_new,
    dws_member_newconv_index.raw_score_welcome,
    dws_member_newconv_index.raw_score_convert,
    dws_member_newconv_index.raw_score,
    dws_member_newconv_index.display_score_welcome,
    dws_member_newconv_index.display_score_convert,
    dws_member_newconv_index.display_score,
    dws_member_newconv_index.last_wechat_touch_time,
    dws_member_newconv_index.calc_time
   FROM dws.dws_member_newconv_index;
;

-- 物化视图
CREATE MATERIALIZED VIEW dws.mv_dws_assistant_daily_detail_l1 AS
SELECT id,
    site_id,
    tenant_id,
    assistant_id,
    assistant_nickname,
    stat_date,
    assistant_level_code,
    assistant_level_name,
    total_service_count,
    base_service_count,
    bonus_service_count,
    room_service_count,
    total_seconds,
    base_seconds,
    bonus_seconds,
    room_seconds,
    total_hours,
    base_hours,
    bonus_hours,
    room_hours,
    total_ledger_amount,
    base_ledger_amount,
    bonus_ledger_amount,
    room_ledger_amount,
    unique_customers,
    unique_tables,
    trashed_seconds,
    trashed_count,
    created_at,
    updated_at
   FROM dws.dws_assistant_daily_detail
  WHERE (stat_date >= (CURRENT_DATE - '1 day'::interval));
;

CREATE MATERIALIZED VIEW dws.mv_dws_assistant_daily_detail_l2 AS
SELECT id,
    site_id,
    tenant_id,
    assistant_id,
    assistant_nickname,
    stat_date,
    assistant_level_code,
    assistant_level_name,
    total_service_count,
    base_service_count,
    bonus_service_count,
    room_service_count,
    total_seconds,
    base_seconds,
    bonus_seconds,
    room_seconds,
    total_hours,
    base_hours,
    bonus_hours,
    room_hours,
    total_ledger_amount,
    base_ledger_amount,
    bonus_ledger_amount,
    room_ledger_amount,
    unique_customers,
    unique_tables,
    trashed_seconds,
    trashed_count,
    created_at,
    updated_at
   FROM dws.dws_assistant_daily_detail
  WHERE (stat_date >= (CURRENT_DATE - '30 days'::interval));
;

CREATE MATERIALIZED VIEW dws.mv_dws_assistant_daily_detail_l3 AS
SELECT id,
    site_id,
    tenant_id,
    assistant_id,
    assistant_nickname,
    stat_date,
    assistant_level_code,
    assistant_level_name,
    total_service_count,
    base_service_count,
    bonus_service_count,
    room_service_count,
    total_seconds,
    base_seconds,
    bonus_seconds,
    room_seconds,
    total_hours,
    base_hours,
    bonus_hours,
    room_hours,
    total_ledger_amount,
    base_ledger_amount,
    bonus_ledger_amount,
    room_ledger_amount,
    unique_customers,
    unique_tables,
    trashed_seconds,
    trashed_count,
    created_at,
    updated_at
   FROM dws.dws_assistant_daily_detail
  WHERE (stat_date >= (CURRENT_DATE - '90 days'::interval));
;

CREATE MATERIALIZED VIEW dws.mv_dws_assistant_daily_detail_l4 AS
SELECT id,
    site_id,
    tenant_id,
    assistant_id,
    assistant_nickname,
    stat_date,
    assistant_level_code,
    assistant_level_name,
    total_service_count,
    base_service_count,
    bonus_service_count,
    room_service_count,
    total_seconds,
    base_seconds,
    bonus_seconds,
    room_seconds,
    total_hours,
    base_hours,
    bonus_hours,
    room_hours,
    total_ledger_amount,
    base_ledger_amount,
    bonus_ledger_amount,
    room_ledger_amount,
    unique_customers,
    unique_tables,
    trashed_seconds,
    trashed_count,
    created_at,
    updated_at
   FROM dws.dws_assistant_daily_detail
  WHERE ((stat_date >= (date_trunc('month'::text, (CURRENT_DATE)::timestamp with time zone) - '6 mons'::interval)) AND (stat_date < date_trunc('month'::text, (CURRENT_DATE)::timestamp with time zone)));
;

CREATE MATERIALIZED VIEW dws.mv_dws_finance_daily_summary_l1 AS
SELECT id,
    site_id,
    tenant_id,
    stat_date,
    gross_amount,
    table_fee_amount,
    goods_amount,
    assistant_pd_amount,
    assistant_cx_amount,
    discount_total,
    discount_groupbuy,
    discount_vip,
    discount_gift_card,
    discount_manual,
    discount_rounding,
    discount_other,
    confirmed_income,
    cash_inflow_total,
    cash_pay_amount,
    groupbuy_pay_amount,
    platform_settlement_amount,
    platform_fee_amount,
    recharge_cash_inflow,
    card_consume_total,
    cash_card_consume,
    gift_card_consume,
    cash_outflow_total,
    cash_balance_change,
    recharge_count,
    recharge_total,
    recharge_cash,
    recharge_gift,
    first_recharge_count,
    first_recharge_amount,
    renewal_count,
    renewal_amount,
    order_count,
    member_order_count,
    guest_order_count,
    avg_order_amount,
    created_at,
    updated_at
   FROM dws.dws_finance_daily_summary
  WHERE (stat_date >= (CURRENT_DATE - '1 day'::interval));
;

CREATE MATERIALIZED VIEW dws.mv_dws_finance_daily_summary_l2 AS
SELECT id,
    site_id,
    tenant_id,
    stat_date,
    gross_amount,
    table_fee_amount,
    goods_amount,
    assistant_pd_amount,
    assistant_cx_amount,
    discount_total,
    discount_groupbuy,
    discount_vip,
    discount_gift_card,
    discount_manual,
    discount_rounding,
    discount_other,
    confirmed_income,
    cash_inflow_total,
    cash_pay_amount,
    groupbuy_pay_amount,
    platform_settlement_amount,
    platform_fee_amount,
    recharge_cash_inflow,
    card_consume_total,
    cash_card_consume,
    gift_card_consume,
    cash_outflow_total,
    cash_balance_change,
    recharge_count,
    recharge_total,
    recharge_cash,
    recharge_gift,
    first_recharge_count,
    first_recharge_amount,
    renewal_count,
    renewal_amount,
    order_count,
    member_order_count,
    guest_order_count,
    avg_order_amount,
    created_at,
    updated_at
   FROM dws.dws_finance_daily_summary
  WHERE (stat_date >= (CURRENT_DATE - '30 days'::interval));
;

CREATE MATERIALIZED VIEW dws.mv_dws_finance_daily_summary_l3 AS
SELECT id,
    site_id,
    tenant_id,
    stat_date,
    gross_amount,
    table_fee_amount,
    goods_amount,
    assistant_pd_amount,
    assistant_cx_amount,
    discount_total,
    discount_groupbuy,
    discount_vip,
    discount_gift_card,
    discount_manual,
    discount_rounding,
    discount_other,
    confirmed_income,
    cash_inflow_total,
    cash_pay_amount,
    groupbuy_pay_amount,
    platform_settlement_amount,
    platform_fee_amount,
    recharge_cash_inflow,
    card_consume_total,
    cash_card_consume,
    gift_card_consume,
    cash_outflow_total,
    cash_balance_change,
    recharge_count,
    recharge_total,
    recharge_cash,
    recharge_gift,
    first_recharge_count,
    first_recharge_amount,
    renewal_count,
    renewal_amount,
    order_count,
    member_order_count,
    guest_order_count,
    avg_order_amount,
    created_at,
    updated_at
   FROM dws.dws_finance_daily_summary
  WHERE (stat_date >= (CURRENT_DATE - '90 days'::interval));
;

CREATE MATERIALIZED VIEW dws.mv_dws_finance_daily_summary_l4 AS
SELECT id,
    site_id,
    tenant_id,
    stat_date,
    gross_amount,
    table_fee_amount,
    goods_amount,
    assistant_pd_amount,
    assistant_cx_amount,
    discount_total,
    discount_groupbuy,
    discount_vip,
    discount_gift_card,
    discount_manual,
    discount_rounding,
    discount_other,
    confirmed_income,
    cash_inflow_total,
    cash_pay_amount,
    groupbuy_pay_amount,
    platform_settlement_amount,
    platform_fee_amount,
    recharge_cash_inflow,
    card_consume_total,
    cash_card_consume,
    gift_card_consume,
    cash_outflow_total,
    cash_balance_change,
    recharge_count,
    recharge_total,
    recharge_cash,
    recharge_gift,
    first_recharge_count,
    first_recharge_amount,
    renewal_count,
    renewal_amount,
    order_count,
    member_order_count,
    guest_order_count,
    avg_order_amount,
    created_at,
    updated_at
   FROM dws.dws_finance_daily_summary
  WHERE ((stat_date >= (date_trunc('month'::text, (CURRENT_DATE)::timestamp with time zone) - '6 mons'::interval)) AND (stat_date < date_trunc('month'::text, (CURRENT_DATE)::timestamp with time zone)));
;

-- 物化视图索引
CREATE INDEX idx_mv_assistant_daily_l1 ON dws.mv_dws_assistant_daily_detail_l1 USING btree (site_id, stat_date, assistant_id);
CREATE INDEX idx_mv_assistant_daily_l2 ON dws.mv_dws_assistant_daily_detail_l2 USING btree (site_id, stat_date, assistant_id);
CREATE INDEX idx_mv_assistant_daily_l3 ON dws.mv_dws_assistant_daily_detail_l3 USING btree (site_id, stat_date, assistant_id);
CREATE INDEX idx_mv_assistant_daily_l4 ON dws.mv_dws_assistant_daily_detail_l4 USING btree (site_id, stat_date, assistant_id);
CREATE INDEX idx_mv_finance_daily_l1 ON dws.mv_dws_finance_daily_summary_l1 USING btree (site_id, stat_date);
CREATE INDEX idx_mv_finance_daily_l2 ON dws.mv_dws_finance_daily_summary_l2 USING btree (site_id, stat_date);
CREATE INDEX idx_mv_finance_daily_l3 ON dws.mv_dws_finance_daily_summary_l3 USING btree (site_id, stat_date);
CREATE INDEX idx_mv_finance_daily_l4 ON dws.mv_dws_finance_daily_summary_l4 USING btree (site_id, stat_date);

