-- Storewide Report Tables (全店报告表)

-- =============================================
-- 全店报告明细表 (storewide_report_detail)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS storewide_report_detail_id_seq;

CREATE TABLE IF NOT EXISTS storewide_report_detail (
    id BIGINT NOT NULL DEFAULT nextval('storewide_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 '',
    primary_url VARCHAR(1024) 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,
    direct_conversion DECIMAL(20,6) NOT NULL DEFAULT 0,
    indirect_conversion DECIMAL(20,6) 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,
    roe DECIMAL(20,8) NOT NULL DEFAULT 0,
    gmv DECIMAL(20,6) NOT NULL DEFAULT 0,
    order_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    avg_order_price DECIMAL(20,8) NOT NULL DEFAULT 0,
    cart_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    favorite_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    page_view DECIMAL(20,6) NOT NULL DEFAULT 0,
    browse_duration DECIMAL(20,6) NOT NULL DEFAULT 0,
    avg_browse_duration DECIMAL(20,6) NOT NULL DEFAULT 0,
    day VARCHAR(10) NOT NULL DEFAULT '',
    datasource VARCHAR(64) NOT NULL DEFAULT '',
    PRIMARY KEY (id)
);

COMMENT ON TABLE storewide_report_detail IS '全店报告明细表';
COMMENT ON COLUMN storewide_report_detail.id IS '主键ID';
COMMENT ON COLUMN storewide_report_detail.tenant_id IS '租户ID';
COMMENT ON COLUMN storewide_report_detail.creator IS '创建人';
COMMENT ON COLUMN storewide_report_detail.created_at IS '创建时间';
COMMENT ON COLUMN storewide_report_detail.updater IS '更新人';
COMMENT ON COLUMN storewide_report_detail.updated_at IS '更新时间';
COMMENT ON COLUMN storewide_report_detail.deleted_at IS '软删除时间';
COMMENT ON COLUMN storewide_report_detail.cid IS '广告账户ID';
COMMENT ON COLUMN storewide_report_detail.campaign_id IS '广告系列ID';
COMMENT ON COLUMN storewide_report_detail.adgroup_id IS '广告组ID';
COMMENT ON COLUMN storewide_report_detail.ad_id IS '创意ID';
COMMENT ON COLUMN storewide_report_detail.ad_name IS '创意名称';
COMMENT ON COLUMN storewide_report_detail.primary_url IS '落地页URL';
COMMENT ON COLUMN storewide_report_detail.impression IS '展示数';
COMMENT ON COLUMN storewide_report_detail.click IS '点击数';
COMMENT ON COLUMN storewide_report_detail.cost IS '花费(元)';
COMMENT ON COLUMN storewide_report_detail.ctr IS '点击率';
COMMENT ON COLUMN storewide_report_detail.cpc IS '平均点击价格';
COMMENT ON COLUMN storewide_report_detail.cpm IS '千次展现成本';
COMMENT ON COLUMN storewide_report_detail.direct_conversion IS '直接转化数';
COMMENT ON COLUMN storewide_report_detail.indirect_conversion IS '间接转化数';
COMMENT ON COLUMN storewide_report_detail.conversion IS '总转化数';
COMMENT ON COLUMN storewide_report_detail.conversion_rate IS '转化率';
COMMENT ON COLUMN storewide_report_detail.cpp IS '单次转化成本';
COMMENT ON COLUMN storewide_report_detail.roe IS '投资回报率(ROE)';
COMMENT ON COLUMN storewide_report_detail.gmv IS 'GMV(元)';
COMMENT ON COLUMN storewide_report_detail.order_count IS '订单数';
COMMENT ON COLUMN storewide_report_detail.avg_order_price IS '平均客单价';
COMMENT ON COLUMN storewide_report_detail.cart_count IS '加购数';
COMMENT ON COLUMN storewide_report_detail.favorite_count IS '收藏数';
COMMENT ON COLUMN storewide_report_detail.page_view IS '页面浏览数';
COMMENT ON COLUMN storewide_report_detail.browse_duration IS '浏览时长';
COMMENT ON COLUMN storewide_report_detail.avg_browse_duration IS '平均浏览时长(秒)';
COMMENT ON COLUMN storewide_report_detail.day IS '数据日期';
COMMENT ON COLUMN storewide_report_detail.datasource IS '数据源';

CREATE INDEX idx_storewide_report_detail_cid ON storewide_report_detail(tenant_id, cid);
CREATE INDEX idx_storewide_report_detail_campaign_id ON storewide_report_detail(tenant_id, campaign_id);
CREATE INDEX idx_storewide_report_detail_ad_id ON storewide_report_detail(tenant_id, ad_id);
CREATE INDEX idx_storewide_report_detail_day ON storewide_report_detail(tenant_id, day);
CREATE INDEX idx_storewide_report_detail_datasource ON storewide_report_detail(tenant_id, datasource);

-- =============================================
-- 全店报告汇总表 (storewide_report_sum)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS storewide_report_sum_id_seq;

CREATE TABLE IF NOT EXISTS storewide_report_sum (
    id BIGINT NOT NULL DEFAULT nextval('storewide_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 '',
    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,
    direct_conversion DECIMAL(20,6) NOT NULL DEFAULT 0,
    indirect_conversion DECIMAL(20,6) 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,
    roe DECIMAL(20,8) NOT NULL DEFAULT 0,
    gmv DECIMAL(20,6) NOT NULL DEFAULT 0,
    order_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    avg_order_price DECIMAL(20,8) NOT NULL DEFAULT 0,
    cart_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    favorite_count DECIMAL(20,6) NOT NULL DEFAULT 0,
    page_view DECIMAL(20,6) NOT NULL DEFAULT 0,
    browse_duration DECIMAL(20,6) NOT NULL DEFAULT 0,
    avg_browse_duration DECIMAL(20,6) 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 storewide_report_sum IS '全店报告汇总表';
COMMENT ON COLUMN storewide_report_sum.id IS '主键ID';
COMMENT ON COLUMN storewide_report_sum.tenant_id IS '租户ID';
COMMENT ON COLUMN storewide_report_sum.creator IS '创建人';
COMMENT ON COLUMN storewide_report_sum.created_at IS '创建时间';
COMMENT ON COLUMN storewide_report_sum.updater IS '更新人';
COMMENT ON COLUMN storewide_report_sum.updated_at IS '更新时间';
COMMENT ON COLUMN storewide_report_sum.deleted_at IS '软删除时间';
COMMENT ON COLUMN storewide_report_sum.cid IS '广告账户ID';
COMMENT ON COLUMN storewide_report_sum.campaign_id IS '广告系列ID';
COMMENT ON COLUMN storewide_report_sum.adgroup_id IS '广告组ID';
COMMENT ON COLUMN storewide_report_sum.ad_id IS '创意ID';
COMMENT ON COLUMN storewide_report_sum.ad_name IS '创意名称';
COMMENT ON COLUMN storewide_report_sum.impression IS '展示数';
COMMENT ON COLUMN storewide_report_sum.click IS '点击数';
COMMENT ON COLUMN storewide_report_sum.cost IS '花费(元)';
COMMENT ON COLUMN storewide_report_sum.ctr IS '点击率';
COMMENT ON COLUMN storewide_report_sum.cpc IS '平均点击价格';
COMMENT ON COLUMN storewide_report_sum.cpm IS '千次展现成本';
COMMENT ON COLUMN storewide_report_sum.direct_conversion IS '直接转化数';
COMMENT ON COLUMN storewide_report_sum.indirect_conversion IS '间接转化数';
COMMENT ON COLUMN storewide_report_sum.conversion IS '总转化数';
COMMENT ON COLUMN storewide_report_sum.conversion_rate IS '转化率';
COMMENT ON COLUMN storewide_report_sum.cpp IS '单次转化成本';
COMMENT ON COLUMN storewide_report_sum.roe IS '投资回报率(ROE)';
COMMENT ON COLUMN storewide_report_sum.gmv IS 'GMV(元)';
COMMENT ON COLUMN storewide_report_sum.order_count IS '订单数';
COMMENT ON COLUMN storewide_report_sum.avg_order_price IS '平均客单价';
COMMENT ON COLUMN storewide_report_sum.cart_count IS '加购数';
COMMENT ON COLUMN storewide_report_sum.favorite_count IS '收藏数';
COMMENT ON COLUMN storewide_report_sum.page_view IS '页面浏览数';
COMMENT ON COLUMN storewide_report_sum.browse_duration IS '浏览时长';
COMMENT ON COLUMN storewide_report_sum.avg_browse_duration IS '平均浏览时长(秒)';
COMMENT ON COLUMN storewide_report_sum.hour IS '数据小时';
COMMENT ON COLUMN storewide_report_sum.day IS '数据日期';
COMMENT ON COLUMN storewide_report_sum.datasource IS '数据源';

CREATE INDEX idx_storewide_report_sum_cid ON storewide_report_sum(tenant_id, cid);
CREATE INDEX idx_storewide_report_sum_campaign_id ON storewide_report_sum(tenant_id, campaign_id);
CREATE INDEX idx_storewide_report_sum_ad_id ON storewide_report_sum(tenant_id, ad_id);
CREATE INDEX idx_storewide_report_sum_day ON storewide_report_sum(tenant_id, day);
CREATE INDEX idx_storewide_report_sum_hour ON storewide_report_sum(tenant_id, hour);
CREATE INDEX idx_storewide_report_sum_datasource ON storewide_report_sum(tenant_id, datasource);
CREATE UNIQUE INDEX idx_storewide_report_sum_unique ON storewide_report_sum(tenant_id, ad_id, hour, day, datasource);
