-- PostgreSQL 建表脚本 - 广告系列报告表

-- =============================================
-- 广告系列报告明细表 (campaign_report_detail)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS campaign_report_detail_id_seq START WITH 1 INCREMENT BY 1;

CREATE TABLE IF NOT EXISTS campaign_report_detail (
    id BIGINT NOT NULL DEFAULT nextval('campaign_report_detail_id_seq'::regclass),
    tenant_id BIGINT NOT NULL DEFAULT 0,
    creator VARCHAR(100) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(100) DEFAULT '',
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP WITH TIME ZONE,
    cid VARCHAR(64) NOT NULL DEFAULT '',
    datasource VARCHAR(64) NOT NULL DEFAULT '',
    t0_order_payment_amt DECIMAL(20,4),
    creative_material_type VARCHAR(100),
    live_name VARCHAR(200),
    author_id VARCHAR(100),
    pic_url VARCHAR(500),
    pic_name VARCHAR(200),
    pic_id VARCHAR(100),
    cover_url VARCHAR(500),
    cover_id BIGINT,
    item_order_conversion_ratio DECIMAL(10,6),
    item_card_click_ratio DECIMAL(10,6),
    item_card_clk_cnt BIGINT,
    live_play_cnt_cost DECIMAL(20,4),
    ad_merchant_follow_cost DECIMAL(20,4),
    ad_merchant_follow BIGINT,
    net_t0_order_cnt BIGINT,
    net_t0_roi DECIMAL(10,6),
    net_t0_gmv DECIMAL(20,4),
    photo_name VARCHAR(200),
    photo_id_str VARCHAR(100),
    photo_id VARCHAR(100),
    mod_price_segment VARCHAR(100),
    age_segment VARCHAR(100),
    province VARCHAR(100),
    gender VARCHAR(20),
    ad_photo_played_five_ratio DECIMAL(10,6),
    ad_photo_played_three_ratio DECIMAL(10,6),
    order_submit_roi DECIMAL(10,6),
    order_submit_amt BIGINT,
    event_order_submit_cost DECIMAL(20,4),
    event_order_submit BIGINT,
    event_order_paid_roi DECIMAL(10,6),
    event_app_invoked BIGINT,
    event_add_shopping_cart BIGINT,
    conversion_num_cost DECIMAL(20,4),
    ad_effective_play_num BIGINT,
    ad_item_click BIGINT,
    merchant_product_id VARCHAR(100),
    cost_total DECIMAL(20,4),
    ad_show BIGINT,
    ad_show1k_cost DECIMAL(20,4),
    impression BIGINT,
    photo_click BIGINT,
    photo_click_ratio DECIMAL(10,6),
    click BIGINT,
    actionbar_click BIGINT,
    actionbar_click_cost DECIMAL(20,4),
    esp_click_ratio DECIMAL(10,6),
    action_ratio DECIMAL(10,6),
    ad_item_click_count BIGINT,
    esp_live_played_seconds BIGINT,
    played_three_seconds BIGINT,
    play3s_ratio DECIMAL(10,6),
    played_five_seconds BIGINT,
    play5s_ratio DECIMAL(10,6),
    played_end BIGINT,
    play_end_ratio DECIMAL(10,6),
    share BIGINT,
    comment BIGINT,
    likes BIGINT,
    report BIGINT,
    block BIGINT,
    item_negative BIGINT,
    live_share BIGINT,
    live_comment BIGINT,
    live_reward BIGINT,
    effective_play_count BIGINT,
    effective_play_ratio DECIMAL(10,6),
    conversion_num BIGINT,
    conversion_cost_esp DECIMAL(20,4),
    roi DECIMAL(10,6),
    gmv DECIMAL(20,4),
    t0_gmv DECIMAL(20,4),
    t1_gmv DECIMAL(20,4),
    t7_gmv DECIMAL(20,4),
    t15_gmv DECIMAL(20,4),
    t30_gmv DECIMAL(20,4),
    t0_roi DECIMAL(10,6),
    t1_roi DECIMAL(10,6),
    t7_roi DECIMAL(10,6),
    t15_roi DECIMAL(10,6),
    t30_roi DECIMAL(10,6),
    paied_order BIGINT,
    order_ratio DECIMAL(10,6),
    t0_order_cnt BIGINT,
    t0_order_cnt_cost DECIMAL(20,4),
    t0_order_cnt_ratio DECIMAL(10,6),
    t1_order_cnt BIGINT,
    t7_order_cnt BIGINT,
    t15_order_cnt BIGINT,
    t30_order_cnt BIGINT,
    merchant_reco_fans BIGINT,
    t1_retention DECIMAL(10,4),
    t7_retention DECIMAL(10,4),
    t15_retention DECIMAL(10,4),
    t30_retention DECIMAL(10,4),
    t1_retention_ratio DECIMAL(10,6),
    t7_retention_ratio DECIMAL(10,6),
    t15_retention_ratio DECIMAL(10,6),
    t30_retention_ratio DECIMAL(10,6),
    reservation_success BIGINT,
    reservation_cost DECIMAL(20,4),
    standard_live_played_started BIGINT,
    ad_live_play_cnt BIGINT,
    ad_live_play_cnt_cost DECIMAL(20,4),
    live_audience_cost DECIMAL(20,4),
    live_event_goods_view BIGINT,
    goods_click_ratio DECIMAL(10,6),
    direct_attr_plat_new_buyer_cnt BIGINT,
    t30_attr_plat_total_buyer_cnt BIGINT,
    direct_attr_seller_new_buyer_cnt BIGINT,
    t30_attr_seller_total_buyer_cnt BIGINT,
    t3_gmv DECIMAL(20,4),
    t3_order_cnt BIGINT,
    t3_roi DECIMAL(10,6),
    t7_indirect_order_amt DECIMAL(20,4),
    t7_indirect_order_cnt BIGINT,
    fans_t0_gmv_per_fans DECIMAL(20,4),
    fans_t3_gmv_per_fans DECIMAL(20,4),
    fans_t7_gmv_per_fans DECIMAL(20,4),
    fans_t15_gmv_per_fans DECIMAL(20,4),
    fans_t30_gmv_per_fans DECIMAL(20,4),
    reco_fans_cost DECIMAL(20,4),
    qcpx_whitebox_direct_order_payment_amt DECIMAL(20,4),
    qcpx_whitebox_direct_order_cnt BIGINT,
    fans_t0_gmv DECIMAL(20,4),
    fans_t1_gmv DECIMAL(20,4),
    fans_t7_gmv DECIMAL(20,4),
    fans_t15_gmv DECIMAL(20,4),
    fans_t30_gmv DECIMAL(20,4),
    fans_t0_roi DECIMAL(10,6),
    fans_t1_roi DECIMAL(10,6),
    fans_t7_roi DECIMAL(10,6),
    fans_t15_roi DECIMAL(10,6),
    fans_t30_roi DECIMAL(10,6),
    t0_shop_new_buyer_order_payment_amt DECIMAL(20,4),
    t1_shop_new_buyer_order_payment_amt DECIMAL(20,4),
    t3_shop_new_buyer_order_payment_amt DECIMAL(20,4),
    t7_shop_new_buyer_order_payment_amt DECIMAL(20,4),
    t15_shop_new_buyer_order_payment_amt DECIMAL(20,4),
    t30_shop_new_buyer_order_payment_amt DECIMAL(20,4),
    t0_shop_new_buyer_order_cnt BIGINT,
    t1_shop_new_buyer_order_cnt BIGINT,
    t3_shop_new_buyer_order_cnt BIGINT,
    t7_shop_new_buyer_order_cnt BIGINT,
    t15_shop_new_buyer_order_cnt BIGINT,
    t30_shop_new_buyer_order_cnt BIGINT,
    t1_new_buyer_repurchase_ratio DECIMAL(10,6),
    t3_new_buyer_repurchase_ratio DECIMAL(10,6),
    t7_new_buyer_repurchase_ratio DECIMAL(10,6),
    t15_new_buyer_repurchase_ratio DECIMAL(10,6),
    t30_new_buyer_repurchase_ratio DECIMAL(10,6),
    t0_shop_new_buyer_roi DECIMAL(10,6),
    t1_shop_new_buyer_roi DECIMAL(10,6),
    t3_shop_new_buyer_roi DECIMAL(10,6),
    t7_shop_new_buyer_roi DECIMAL(10,6),
    t15_shop_new_buyer_roi DECIMAL(10,6),
    t30_shop_new_buyer_roi DECIMAL(10,6),
    create_card_order_cnt BIGINT,
    forward_ts_create_card_order_cnt BIGINT,
    create_card_order_cost DECIMAL(20,4),
    forward_ts_create_card_order_cost DECIMAL(20,4),
    activate_card_order_cnt BIGINT,
    forward_ts_activate_card_order_cnt BIGINT,
    activate_card_order_cost DECIMAL(20,4),
    forward_ts_activate_card_order_cost DECIMAL(20,4),
    create_card_order_ratio DECIMAL(10,6),
    forward_ts_create_card_order_ratio DECIMAL(10,6),
    activate_card_order_cnt_ratio DECIMAL(10,6),
    forward_ts_activate_card_order_ratio DECIMAL(10,6),
    live_play_cnt BIGINT,
    item_entrance_clk_cnt BIGINT,
    show_cnt BIGINT,
    report_date_str VARCHAR(50),
    campaign_id BIGINT,
    campaign_name VARCHAR(200),
    unit_id BIGINT,
    unit_name VARCHAR(200),
    creative_id BIGINT,
    creative_name VARCHAR(200),
    cid_actual_roi_after_subsidy DECIMAL(10,6),
    cid_coupon_amount BIGINT,
    cid_coupon_callback_paid_refund_amount BIGINT,
    cid_voucher_cost DECIMAL(20,4),
    PRIMARY KEY (id)
);

COMMENT ON TABLE campaign_report_detail IS '广告系列报告明细表';

CREATE INDEX idx_campaign_report_detail_cid ON campaign_report_detail(tenant_id, cid);
CREATE INDEX idx_campaign_report_detail_campaign_id ON campaign_report_detail(tenant_id, campaign_id);
CREATE INDEX idx_campaign_report_detail_date ON campaign_report_detail(tenant_id, report_date_str);
CREATE INDEX idx_campaign_report_detail_datasource ON campaign_report_detail(tenant_id, datasource);

-- =============================================
-- 广告系列报告汇总表 (campaign_report_sum)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS campaign_report_sum_id_seq START WITH 1 INCREMENT BY 1;

CREATE TABLE IF NOT EXISTS campaign_report_sum (
    id BIGINT NOT NULL DEFAULT nextval('campaign_report_sum_id_seq'::regclass),
    tenant_id BIGINT NOT NULL DEFAULT 0,
    creator VARCHAR(100) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updater VARCHAR(100) DEFAULT '',
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP WITH TIME ZONE,
    cid VARCHAR(64) NOT NULL DEFAULT '',
    datasource VARCHAR(64) NOT NULL DEFAULT '',
    t0_order_payment_amt DECIMAL(20,4),
    creative_material_type VARCHAR(100),
    live_name VARCHAR(200),
    author_id VARCHAR(100),
    pic_url VARCHAR(500),
    pic_name VARCHAR(200),
    pic_id VARCHAR(100),
    cover_url VARCHAR(500),
    cover_id BIGINT,
    item_order_conversion_ratio DECIMAL(10,6),
    item_card_click_ratio DECIMAL(10,6),
    item_card_clk_cnt BIGINT,
    live_play_cnt_cost DECIMAL(20,4),
    ad_merchant_follow_cost DECIMAL(20,4),
    ad_merchant_follow BIGINT,
    net_t0_order_cnt BIGINT,
    net_t0_roi DECIMAL(10,6),
    net_t0_gmv DECIMAL(20,4),
    photo_name VARCHAR(200),
    photo_id_str VARCHAR(100),
    photo_id VARCHAR(100),
    mod_price_segment VARCHAR(100),
    age_segment VARCHAR(100),
    province VARCHAR(100),
    gender VARCHAR(20),
    ad_photo_played_five_ratio DECIMAL(10,6),
    ad_photo_played_three_ratio DECIMAL(10,6),
    order_submit_roi DECIMAL(10,6),
    order_submit_amt BIGINT,
    event_order_submit_cost DECIMAL(20,4),
    event_order_submit BIGINT,
    event_order_paid_roi DECIMAL(10,6),
    event_app_invoked BIGINT,
    event_add_shopping_cart BIGINT,
    conversion_num_cost DECIMAL(20,4),
    ad_effective_play_num BIGINT,
    ad_item_click BIGINT,
    merchant_product_id VARCHAR(100),
    cost_total DECIMAL(20,4),
    ad_show BIGINT,
    ad_show1k_cost DECIMAL(20,4),
    impression BIGINT,
    photo_click BIGINT,
    photo_click_ratio DECIMAL(10,6),
    click BIGINT,
    actionbar_click BIGINT,
    actionbar_click_cost DECIMAL(20,4),
    esp_click_ratio DECIMAL(10,6),
    action_ratio DECIMAL(10,6),
    ad_item_click_count BIGINT,
    esp_live_played_seconds BIGINT,
    played_three_seconds BIGINT,
    play3s_ratio DECIMAL(10,6),
    played_five_seconds BIGINT,
    play5s_ratio DECIMAL(10,6),
    played_end BIGINT,
    play_end_ratio DECIMAL(10,6),
    share BIGINT,
    comment BIGINT,
    likes BIGINT,
    report BIGINT,
    block BIGINT,
    item_negative BIGINT,
    live_share BIGINT,
    live_comment BIGINT,
    live_reward BIGINT,
    effective_play_count BIGINT,
    effective_play_ratio DECIMAL(10,6),
    conversion_num BIGINT,
    conversion_cost_esp DECIMAL(20,4),
    roi DECIMAL(10,6),
    gmv DECIMAL(20,4),
    t0_gmv DECIMAL(20,4),
    t1_gmv DECIMAL(20,4),
    t7_gmv DECIMAL(20,4),
    t15_gmv DECIMAL(20,4),
    t30_gmv DECIMAL(20,4),
    t0_roi DECIMAL(10,6),
    t1_roi DECIMAL(10,6),
    t7_roi DECIMAL(10,6),
    t15_roi DECIMAL(10,6),
    t30_roi DECIMAL(10,6),
    paied_order BIGINT,
    order_ratio DECIMAL(10,6),
    t0_order_cnt BIGINT,
    t0_order_cnt_cost DECIMAL(20,4),
    t0_order_cnt_ratio DECIMAL(10,6),
    t1_order_cnt BIGINT,
    t7_order_cnt BIGINT,
    t15_order_cnt BIGINT,
    t30_order_cnt BIGINT,
    merchant_reco_fans BIGINT,
    t1_retention DECIMAL(10,4),
    t7_retention DECIMAL(10,4),
    t15_retention DECIMAL(10,4),
    t30_retention DECIMAL(10,4),
    t1_retention_ratio DECIMAL(10,6),
    t7_retention_ratio DECIMAL(10,6),
    t15_retention_ratio DECIMAL(10,6),
    t30_retention_ratio DECIMAL(10,6),
    reservation_success BIGINT,
    reservation_cost DECIMAL(20,4),
    standard_live_played_started BIGINT,
    ad_live_play_cnt BIGINT,
    ad_live_play_cnt_cost DECIMAL(20,4),
    live_audience_cost DECIMAL(20,4),
    live_event_goods_view BIGINT,
    goods_click_ratio DECIMAL(10,6),
    direct_attr_plat_new_buyer_cnt BIGINT,
    t30_attr_plat_total_buyer_cnt BIGINT,
    direct_attr_seller_new_buyer_cnt BIGINT,
    t30_attr_seller_total_buyer_cnt BIGINT,
    t3_gmv DECIMAL(20,4),
    t3_order_cnt BIGINT,
    t3_roi DECIMAL(10,6),
    t7_indirect_order_amt DECIMAL(20,4),
    t7_indirect_order_cnt BIGINT,
    fans_t0_gmv_per_fans DECIMAL(20,4),
    fans_t3_gmv_per_fans DECIMAL(20,4),
    fans_t7_gmv_per_fans DECIMAL(20,4),
    fans_t15_gmv_per_fans DECIMAL(20,4),
    fans_t30_gmv_per_fans DECIMAL(20,4),
    reco_fans_cost DECIMAL(20,4),
    qcpx_whitebox_direct_order_payment_amt DECIMAL(20,4),
    qcpx_whitebox_direct_order_cnt BIGINT,
    fans_t0_gmv DECIMAL(20,4),
    fans_t1_gmv DECIMAL(20,4),
    fans_t7_gmv DECIMAL(20,4),
    fans_t15_gmv DECIMAL(20,4),
    fans_t30_gmv DECIMAL(20,4),
    fans_t0_roi DECIMAL(10,6),
    fans_t1_roi DECIMAL(10,6),
    fans_t7_roi DECIMAL(10,6),
    fans_t15_roi DECIMAL(10,6),
    fans_t30_roi DECIMAL(10,6),
    t0_shop_new_buyer_order_payment_amt DECIMAL(20,4),
    t1_shop_new_buyer_order_payment_amt DECIMAL(20,4),
    t3_shop_new_buyer_order_payment_amt DECIMAL(20,4),
    t7_shop_new_buyer_order_payment_amt DECIMAL(20,4),
    t15_shop_new_buyer_order_payment_amt DECIMAL(20,4),
    t30_shop_new_buyer_order_payment_amt DECIMAL(20,4),
    t0_shop_new_buyer_order_cnt BIGINT,
    t1_shop_new_buyer_order_cnt BIGINT,
    t3_shop_new_buyer_order_cnt BIGINT,
    t7_shop_new_buyer_order_cnt BIGINT,
    t15_shop_new_buyer_order_cnt BIGINT,
    t30_shop_new_buyer_order_cnt BIGINT,
    t1_new_buyer_repurchase_ratio DECIMAL(10,6),
    t3_new_buyer_repurchase_ratio DECIMAL(10,6),
    t7_new_buyer_repurchase_ratio DECIMAL(10,6),
    t15_new_buyer_repurchase_ratio DECIMAL(10,6),
    t30_new_buyer_repurchase_ratio DECIMAL(10,6),
    t0_shop_new_buyer_roi DECIMAL(10,6),
    t1_shop_new_buyer_roi DECIMAL(10,6),
    t3_shop_new_buyer_roi DECIMAL(10,6),
    t7_shop_new_buyer_roi DECIMAL(10,6),
    t15_shop_new_buyer_roi DECIMAL(10,6),
    t30_shop_new_buyer_roi DECIMAL(10,6),
    create_card_order_cnt BIGINT,
    forward_ts_create_card_order_cnt BIGINT,
    create_card_order_cost DECIMAL(20,4),
    forward_ts_create_card_order_cost DECIMAL(20,4),
    activate_card_order_cnt BIGINT,
    forward_ts_activate_card_order_cnt BIGINT,
    activate_card_order_cost DECIMAL(20,4),
    forward_ts_activate_card_order_cost DECIMAL(20,4),
    create_card_order_ratio DECIMAL(10,6),
    forward_ts_create_card_order_ratio DECIMAL(10,6),
    activate_card_order_cnt_ratio DECIMAL(10,6),
    forward_ts_activate_card_order_ratio DECIMAL(10,6),
    live_play_cnt BIGINT,
    item_entrance_clk_cnt BIGINT,
    show_cnt BIGINT,
    report_date_str VARCHAR(50),
    campaign_id BIGINT,
    campaign_name VARCHAR(200),
    unit_id BIGINT,
    unit_name VARCHAR(200),
    creative_id BIGINT,
    creative_name VARCHAR(200),
    cid_actual_roi_after_subsidy DECIMAL(10,6),
    cid_coupon_amount BIGINT,
    cid_coupon_callback_paid_refund_amount BIGINT,
    cid_voucher_cost DECIMAL(20,4),
    PRIMARY KEY (id)
);

COMMENT ON TABLE campaign_report_sum IS '广告系列报告汇总表';

CREATE INDEX idx_campaign_report_sum_cid ON campaign_report_sum(tenant_id, cid);
CREATE INDEX idx_campaign_report_sum_campaign_id ON campaign_report_sum(tenant_id, campaign_id);
CREATE INDEX idx_campaign_report_sum_date ON campaign_report_sum(tenant_id, report_date_str);
CREATE INDEX idx_campaign_report_sum_datasource ON campaign_report_sum(tenant_id, datasource);
