-- Creative Report Tables (创意报告表)

-- =============================================
-- 创意报告明细表 (creative_report_detail)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS creative_report_detail_id_seq;

CREATE TABLE IF NOT EXISTS creative_report_detail (
    id BIGINT NOT NULL DEFAULT nextval('creative_report_detail_id_seq'::regclass),
    tenant_id BIGINT NOT NULL DEFAULT 0,
    creator VARCHAR(64) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT '0001-01-01 00:00:00+00:00',
    updater VARCHAR(64) DEFAULT '',
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT '0001-01-01 00:00:00+00:00',
    deleted_at TIMESTAMP WITH TIME ZONE,
    cid VARCHAR(64) NOT NULL DEFAULT '',
    campaign_id VARCHAR(64) NOT NULL DEFAULT '',
    adgroup_id VARCHAR(64) NOT NULL DEFAULT '',
    ad_id VARCHAR(64) NOT NULL DEFAULT '',
    ad_name VARCHAR(255) NOT NULL DEFAULT '',
    render_mode VARCHAR(32) NOT NULL DEFAULT '',
    inventory_type VARCHAR(32) NOT NULL DEFAULT '',
    promotion_target_id VARCHAR(64) NOT NULL DEFAULT '',
    material_hash VARCHAR(128) NOT NULL DEFAULT '',
    impression DECIMAL(20,6) NOT NULL DEFAULT 0,
    click DECIMAL(20,6) NOT NULL DEFAULT 0,
    cost DECIMAL(20,6) NOT NULL DEFAULT 0,
    ctr DECIMAL(20,8) NOT NULL DEFAULT 0,
    cpc DECIMAL(20,8) NOT NULL DEFAULT 0,
    cpm DECIMAL(20,8) NOT NULL DEFAULT 0,
    conversion DECIMAL(20,6) NOT NULL DEFAULT 0,
    conversion_rate DECIMAL(20,8) NOT NULL DEFAULT 0,
    cpp DECIMAL(20,8) NOT NULL DEFAULT 0,
    primary_url VARCHAR(1024) NOT NULL DEFAULT '',
    video_play_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    video_play_uv DECIMAL(20,6) NOT NULL DEFAULT 0,
    avg_video_play_duration DECIMAL(20,6) NOT NULL DEFAULT 0,
    video_interaction_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    video_three_second_play_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    video_five_second_play_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    tweet_engage_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    tweet_share_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    tweet_comment_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    tweet_like_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    tweet_repost_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    game_download_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    app_download_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    app_install_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    onsite_leads_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    deep_convert_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    deep_convert_rate DECIMAL(20,8) NOT NULL DEFAULT 0,
    deep_convert_cost DECIMAL(20,8) NOT NULL DEFAULT 0,
    day VARCHAR(10) NOT NULL DEFAULT '',
    datasource VARCHAR(64) NOT NULL DEFAULT '',
    PRIMARY KEY (id)
);

COMMENT ON TABLE creative_report_detail IS '创意报告明细表';
COMMENT ON COLUMN creative_report_detail.id IS '主键ID';
COMMENT ON COLUMN creative_report_detail.tenant_id IS '租户ID';
COMMENT ON COLUMN creative_report_detail.creator IS '创建人';
COMMENT ON COLUMN creative_report_detail.created_at IS '创建时间';
COMMENT ON COLUMN creative_report_detail.updater IS '更新人';
COMMENT ON COLUMN creative_report_detail.updated_at IS '更新时间';
COMMENT ON COLUMN creative_report_detail.deleted_at IS '软删除时间';
COMMENT ON COLUMN creative_report_detail.cid IS '广告账户ID';
COMMENT ON COLUMN creative_report_detail.campaign_id IS '广告系列ID';
COMMENT ON COLUMN creative_report_detail.adgroup_id IS '广告组ID';
COMMENT ON COLUMN creative_report_detail.ad_id IS '创意ID';
COMMENT ON COLUMN creative_report_detail.ad_name IS '创意名称';
COMMENT ON COLUMN creative_report_detail.render_mode IS '创意类型';
COMMENT ON COLUMN creative_report_detail.inventory_type IS '投放位置';
COMMENT ON COLUMN creative_report_detail.promotion_target_id IS '推广目标ID';
COMMENT ON COLUMN creative_report_detail.material_hash IS '素材哈希';
COMMENT ON COLUMN creative_report_detail.impression IS '展示数';
COMMENT ON COLUMN creative_report_detail.click IS '点击数';
COMMENT ON COLUMN creative_report_detail.cost IS '花费(元)';
COMMENT ON COLUMN creative_report_detail.ctr IS '点击率';
COMMENT ON COLUMN creative_report_detail.cpc IS '平均点击价格';
COMMENT ON COLUMN creative_report_detail.cpm IS '千次展现成本';
COMMENT ON COLUMN creative_report_detail.conversion IS '转化数';
COMMENT ON COLUMN creative_report_detail.conversion_rate IS '转化率';
COMMENT ON COLUMN creative_report_detail.cpp IS '单次转化成本';
COMMENT ON COLUMN creative_report_detail.primary_url IS '落地页URL';
COMMENT ON COLUMN creative_report_detail.video_play_count IS '视频播放次数';
COMMENT ON COLUMN creative_report_detail.video_play_uv IS '视频播放人数';
COMMENT ON COLUMN creative_report_detail.avg_video_play_duration IS '平均播放时长(秒)';
COMMENT ON COLUMN creative_report_detail.video_interaction_count IS '视频互动数';
COMMENT ON COLUMN creative_report_detail.video_three_second_play_count IS '视频3秒播放次数';
COMMENT ON COLUMN creative_report_detail.video_five_second_play_count IS '视频5秒播放次数';
COMMENT ON COLUMN creative_report_detail.tweet_engage_count IS '推文参与数';
COMMENT ON COLUMN creative_report_detail.tweet_share_count IS '推文分享数';
COMMENT ON COLUMN creative_report_detail.tweet_comment_count IS '推文评论数';
COMMENT ON COLUMN creative_report_detail.tweet_like_count IS '推文点赞数';
COMMENT ON COLUMN creative_report_detail.tweet_repost_count IS '推文转发数';
COMMENT ON COLUMN creative_report_detail.game_download_count IS '游戏下载数';
COMMENT ON COLUMN creative_report_detail.app_download_count IS '应用下载数';
COMMENT ON COLUMN creative_report_detail.app_install_count IS '应用安装数';
COMMENT ON COLUMN creative_report_detail.onsite_leads_count IS '预注册数';
COMMENT ON COLUMN creative_report_detail.deep_convert_count IS '深度转化数';
COMMENT ON COLUMN creative_report_detail.deep_convert_rate IS '深度转化率';
COMMENT ON COLUMN creative_report_detail.deep_convert_cost IS '深度转化成本';
COMMENT ON COLUMN creative_report_detail.day IS '数据日期';
COMMENT ON COLUMN creative_report_detail.datasource IS '数据源';

CREATE INDEX idx_creative_report_detail_cid ON creative_report_detail(tenant_id, cid);
CREATE INDEX idx_creative_report_detail_campaign_id ON creative_report_detail(tenant_id, campaign_id);
CREATE INDEX idx_creative_report_detail_ad_id ON creative_report_detail(tenant_id, ad_id);
CREATE INDEX idx_creative_report_detail_day ON creative_report_detail(tenant_id, day);
CREATE INDEX idx_creative_report_detail_datasource ON creative_report_detail(tenant_id, datasource);
CREATE INDEX idx_creative_report_detail_material_hash ON creative_report_detail(tenant_id, material_hash);

-- =============================================
-- 创意报告汇总表 (creative_report_sum)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS creative_report_sum_id_seq;

CREATE TABLE IF NOT EXISTS creative_report_sum (
    id BIGINT NOT NULL DEFAULT nextval('creative_report_sum_id_seq'::regclass),
    tenant_id BIGINT NOT NULL DEFAULT 0,
    creator VARCHAR(64) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT '0001-01-01 00:00:00+00:00',
    updater VARCHAR(64) DEFAULT '',
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT '0001-01-01 00:00:00+00:00',
    deleted_at TIMESTAMP WITH TIME ZONE,
    cid VARCHAR(64) NOT NULL DEFAULT '',
    campaign_id VARCHAR(64) NOT NULL DEFAULT '',
    adgroup_id VARCHAR(64) NOT NULL DEFAULT '',
    ad_id VARCHAR(64) NOT NULL DEFAULT '',
    ad_name VARCHAR(255) NOT NULL DEFAULT '',
    render_mode VARCHAR(32) NOT NULL DEFAULT '',
    inventory_type VARCHAR(32) NOT NULL DEFAULT '',
    promotion_target_id VARCHAR(64) NOT NULL DEFAULT '',
    impression DECIMAL(20,6) NOT NULL DEFAULT 0,
    click DECIMAL(20,6) NOT NULL DEFAULT 0,
    cost DECIMAL(20,6) NOT NULL DEFAULT 0,
    ctr DECIMAL(20,8) NOT NULL DEFAULT 0,
    cpc DECIMAL(20,8) NOT NULL DEFAULT 0,
    cpm DECIMAL(20,8) NOT NULL DEFAULT 0,
    conversion DECIMAL(20,6) NOT NULL DEFAULT 0,
    conversion_rate DECIMAL(20,8) NOT NULL DEFAULT 0,
    cpp DECIMAL(20,8) NOT NULL DEFAULT 0,
    video_play_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    video_play_uv DECIMAL(20,6) NOT NULL DEFAULT 0,
    avg_video_play_duration DECIMAL(20,6) NOT NULL DEFAULT 0,
    video_interaction_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    game_download_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    app_download_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    app_install_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    onsite_leads_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    deep_convert_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    deep_convert_rate DECIMAL(20,8) NOT NULL DEFAULT 0,
    deep_convert_cost DECIMAL(20,8) NOT NULL DEFAULT 0,
    hour VARCHAR(2) NOT NULL DEFAULT '',
    day VARCHAR(10) NOT NULL DEFAULT '',
    datasource VARCHAR(64) NOT NULL DEFAULT '',
    PRIMARY KEY (id)
);

COMMENT ON TABLE creative_report_sum IS '创意报告汇总表';
COMMENT ON COLUMN creative_report_sum.id IS '主键ID';
COMMENT ON COLUMN creative_report_sum.tenant_id IS '租户ID';
COMMENT ON COLUMN creative_report_sum.creator IS '创建人';
COMMENT ON COLUMN creative_report_sum.created_at IS '创建时间';
COMMENT ON COLUMN creative_report_sum.updater IS '更新人';
COMMENT ON COLUMN creative_report_sum.updated_at IS '更新时间';
COMMENT ON COLUMN creative_report_sum.deleted_at IS '软删除时间';
COMMENT ON COLUMN creative_report_sum.cid IS '广告账户ID';
COMMENT ON COLUMN creative_report_sum.campaign_id IS '广告系列ID';
COMMENT ON COLUMN creative_report_sum.adgroup_id IS '广告组ID';
COMMENT ON COLUMN creative_report_sum.ad_id IS '创意ID';
COMMENT ON COLUMN creative_report_sum.ad_name IS '创意名称';
COMMENT ON COLUMN creative_report_sum.render_mode IS '创意类型';
COMMENT ON COLUMN creative_report_sum.inventory_type IS '投放位置';
COMMENT ON COLUMN creative_report_sum.promotion_target_id IS '推广目标ID';
COMMENT ON COLUMN creative_report_sum.impression IS '展示数';
COMMENT ON COLUMN creative_report_sum.click IS '点击数';
COMMENT ON COLUMN creative_report_sum.cost IS '花费(元)';
COMMENT ON COLUMN creative_report_sum.ctr IS '点击率';
COMMENT ON COLUMN creative_report_sum.cpc IS '平均点击价格';
COMMENT ON COLUMN creative_report_sum.cpm IS '千次展现成本';
COMMENT ON COLUMN creative_report_sum.conversion IS '转化数';
COMMENT ON COLUMN creative_report_sum.conversion_rate IS '转化率';
COMMENT ON COLUMN creative_report_sum.cpp IS '单次转化成本';
COMMENT ON COLUMN creative_report_sum.video_play_count IS '视频播放次数';
COMMENT ON COLUMN creative_report_sum.video_play_uv IS '视频播放人数';
COMMENT ON COLUMN creative_report_sum.avg_video_play_duration IS '平均播放时长(秒)';
COMMENT ON COLUMN creative_report_sum.video_interaction_count IS '视频互动数';
COMMENT ON COLUMN creative_report_sum.game_download_count IS '游戏下载数';
COMMENT ON COLUMN creative_report_sum.app_download_count IS '应用下载数';
COMMENT ON COLUMN creative_report_sum.app_install_count IS '应用安装数';
COMMENT ON COLUMN creative_report_sum.onsite_leads_count IS '预注册数';
COMMENT ON COLUMN creative_report_sum.deep_convert_count IS '深度转化数';
COMMENT ON COLUMN creative_report_sum.deep_convert_rate IS '深度转化率';
COMMENT ON COLUMN creative_report_sum.deep_convert_cost IS '深度转化成本';
COMMENT ON COLUMN creative_report_sum.hour IS '数据小时';
COMMENT ON COLUMN creative_report_sum.day IS '数据日期';
COMMENT ON COLUMN creative_report_sum.datasource IS '数据源';

CREATE INDEX idx_creative_report_sum_cid ON creative_report_sum(tenant_id, cid);
CREATE INDEX idx_creative_report_sum_campaign_id ON creative_report_sum(tenant_id, campaign_id);
CREATE INDEX idx_creative_report_sum_ad_id ON creative_report_sum(tenant_id, ad_id);
CREATE INDEX idx_creative_report_sum_day ON creative_report_sum(tenant_id, day);
CREATE INDEX idx_creative_report_sum_hour ON creative_report_sum(tenant_id, hour);
CREATE INDEX idx_creative_report_sum_datasource ON creative_report_sum(tenant_id, datasource);
CREATE UNIQUE INDEX idx_creative_report_sum_unique ON creative_report_sum(tenant_id, ad_id, hour, day, datasource);
