-- Material and Population Report Tables (素材报告和人群报告表)

-- =============================================
-- 素材报告表 (material_report)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS material_report_id_seq;

CREATE TABLE IF NOT EXISTS material_report (
    id BIGINT NOT NULL DEFAULT nextval('material_report_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 '',
    ad_id VARCHAR(64) NOT NULL DEFAULT '',
    ad_name VARCHAR(255) NOT NULL DEFAULT '',
    material_hash VARCHAR(128) NOT NULL DEFAULT '',
    material_url VARCHAR(2048) NOT NULL DEFAULT '',
    material_type VARCHAR(32) 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_three_second_play_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    video_five_second_play_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 material_report IS '素材报告表';
COMMENT ON COLUMN material_report.id IS '主键ID';
COMMENT ON COLUMN material_report.tenant_id IS '租户ID';
COMMENT ON COLUMN material_report.creator IS '创建人';
COMMENT ON COLUMN material_report.created_at IS '创建时间';
COMMENT ON COLUMN material_report.updater IS '更新人';
COMMENT ON COLUMN material_report.updated_at IS '更新时间';
COMMENT ON COLUMN material_report.deleted_at IS '软删除时间';
COMMENT ON COLUMN material_report.cid IS '广告账户ID';
COMMENT ON COLUMN material_report.ad_id IS '创意ID';
COMMENT ON COLUMN material_report.ad_name IS '创意名称';
COMMENT ON COLUMN material_report.material_hash IS '素材哈希';
COMMENT ON COLUMN material_report.material_url IS '素材URL';
COMMENT ON COLUMN material_report.material_type IS '素材类型';
COMMENT ON COLUMN material_report.impression IS '展示数';
COMMENT ON COLUMN material_report.click IS '点击数';
COMMENT ON COLUMN material_report.cost IS '花费(元)';
COMMENT ON COLUMN material_report.ctr IS '点击率';
COMMENT ON COLUMN material_report.cpc IS '平均点击价格';
COMMENT ON COLUMN material_report.cpm IS '千次展现成本';
COMMENT ON COLUMN material_report.conversion IS '转化数';
COMMENT ON COLUMN material_report.conversion_rate IS '转化率';
COMMENT ON COLUMN material_report.cpp IS '单次转化成本';
COMMENT ON COLUMN material_report.video_play_count IS '视频播放次数';
COMMENT ON COLUMN material_report.video_play_uv IS '视频播放人数';
COMMENT ON COLUMN material_report.avg_video_play_duration IS '平均播放时长(秒)';
COMMENT ON COLUMN material_report.video_three_second_play_count IS '视频3秒播放次数';
COMMENT ON COLUMN material_report.video_five_second_play_count IS '视频5秒播放次数';
COMMENT ON COLUMN material_report.deep_convert_count IS '深度转化数';
COMMENT ON COLUMN material_report.deep_convert_rate IS '深度转化率';
COMMENT ON COLUMN material_report.deep_convert_cost IS '深度转化成本';
COMMENT ON COLUMN material_report.day IS '数据日期';
COMMENT ON COLUMN material_report.datasource IS '数据源';

CREATE INDEX idx_material_report_cid ON material_report(tenant_id, cid);
CREATE INDEX idx_material_report_ad_id ON material_report(tenant_id, ad_id);
CREATE INDEX idx_material_report_material_hash ON material_report(tenant_id, material_hash);
CREATE INDEX idx_material_report_day ON material_report(tenant_id, day);
CREATE INDEX idx_material_report_datasource ON material_report(tenant_id, datasource);

-- =============================================
-- 人群报告表 (population_report)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS population_report_id_seq;

CREATE TABLE IF NOT EXISTS population_report (
    id BIGINT NOT NULL DEFAULT nextval('population_report_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 '',
    audience_template_id VARCHAR(64) NOT NULL DEFAULT '',
    audience_template_name VARCHAR(255) NOT NULL DEFAULT '',
    audience_type VARCHAR(32) NOT NULL DEFAULT '',
    audience_source VARCHAR(32) 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_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 population_report IS '人群报告表';
COMMENT ON COLUMN population_report.id IS '主键ID';
COMMENT ON COLUMN population_report.tenant_id IS '租户ID';
COMMENT ON COLUMN population_report.creator IS '创建人';
COMMENT ON COLUMN population_report.created_at IS '创建时间';
COMMENT ON COLUMN population_report.updater IS '更新人';
COMMENT ON COLUMN population_report.updated_at IS '更新时间';
COMMENT ON COLUMN population_report.deleted_at IS '软删除时间';
COMMENT ON COLUMN population_report.cid IS '广告账户ID';
COMMENT ON COLUMN population_report.campaign_id IS '广告系列ID';
COMMENT ON COLUMN population_report.adgroup_id IS '广告组ID';
COMMENT ON COLUMN population_report.audience_template_id IS '人群包ID';
COMMENT ON COLUMN population_report.audience_template_name IS '人群包名称';
COMMENT ON COLUMN population_report.audience_type IS '人群类型';
COMMENT ON COLUMN population_report.audience_source IS '人群来源';
COMMENT ON COLUMN population_report.impression IS '展示数';
COMMENT ON COLUMN population_report.click IS '点击数';
COMMENT ON COLUMN population_report.cost IS '花费(元)';
COMMENT ON COLUMN population_report.ctr IS '点击率';
COMMENT ON COLUMN population_report.cpc IS '平均点击价格';
COMMENT ON COLUMN population_report.cpm IS '千次展现成本';
COMMENT ON COLUMN population_report.conversion IS '转化数';
COMMENT ON COLUMN population_report.conversion_rate IS '转化率';
COMMENT ON COLUMN population_report.cpp IS '单次转化成本';
COMMENT ON COLUMN population_report.video_play_count IS '视频播放次数';
COMMENT ON COLUMN population_report.video_play_uv IS '视频播放人数';
COMMENT ON COLUMN population_report.avg_video_play_duration IS '平均播放时长(秒)';
COMMENT ON COLUMN population_report.video_three_second_play_count IS '视频3秒播放次数';
COMMENT ON COLUMN population_report.video_five_second_play_count IS '视频5秒播放次数';
COMMENT ON COLUMN population_report.tweet_engage_count IS '推文参与数';
COMMENT ON COLUMN population_report.tweet_share_count IS '推文分享数';
COMMENT ON COLUMN population_report.tweet_comment_count IS '推文评论数';
COMMENT ON COLUMN population_report.tweet_like_count IS '推文点赞数';
COMMENT ON COLUMN population_report.tweet_repost_count IS '推文转发数';
COMMENT ON COLUMN population_report.game_download_count IS '游戏下载数';
COMMENT ON COLUMN population_report.app_download_count IS '应用下载数';
COMMENT ON COLUMN population_report.app_install_count IS '应用安装数';
COMMENT ON COLUMN population_report.onsite_leads_count IS '预注册数';
COMMENT ON COLUMN population_report.deep_convert_count IS '深度转化数';
COMMENT ON COLUMN population_report.deep_convert_rate IS '深度转化率';
COMMENT ON COLUMN population_report.deep_convert_cost IS '深度转化成本';
COMMENT ON COLUMN population_report.day IS '数据日期';
COMMENT ON COLUMN population_report.datasource IS '数据源';

CREATE INDEX idx_population_report_cid ON population_report(tenant_id, cid);
CREATE INDEX idx_population_report_campaign_id ON population_report(tenant_id, campaign_id);
CREATE INDEX idx_population_report_adgroup_id ON population_report(tenant_id, adgroup_id);
CREATE INDEX idx_population_report_audience_template_id ON population_report(tenant_id, audience_template_id);
CREATE INDEX idx_population_report_day ON population_report(tenant_id, day);
CREATE INDEX idx_population_report_datasource ON population_report(tenant_id, datasource);
