-- PostgreSQL 建表脚本 - 账户报告表

-- =============================================
-- 账户报告明细表 (cid_account_report_detail)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS cid_account_report_detail_id_seq START WITH 1 INCREMENT BY 1;

CREATE TABLE IF NOT EXISTS cid_account_report_detail (
    id BIGINT NOT NULL DEFAULT nextval('cid_account_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 '',
    report_date_str VARCHAR(50),
    account_id VARCHAR(100),
    account_name VARCHAR(200),
    cost_total DECIMAL(20,4),
    impression BIGINT,
    click BIGINT,
    ctr DECIMAL(10,6),
    cpc DECIMAL(20,4),
    cpm DECIMAL(20,4),
    conversion BIGINT,
    conversion_rate DECIMAL(10,6),
    cpa DECIMAL(20,4),
    datasource VARCHAR(64) NOT NULL DEFAULT '',
    PRIMARY KEY (id)
);

COMMENT ON TABLE cid_account_report_detail IS '账户报告明细表';
COMMENT ON COLUMN cid_account_report_detail.id IS '主键ID';
COMMENT ON COLUMN cid_account_report_detail.tenant_id IS '租户ID';
COMMENT ON COLUMN cid_account_report_detail.creator IS '创建人';
COMMENT ON COLUMN cid_account_report_detail.created_at IS '创建时间';
COMMENT ON COLUMN cid_account_report_detail.updater IS '更新人';
COMMENT ON COLUMN cid_account_report_detail.updated_at IS '更新时间';
COMMENT ON COLUMN cid_account_report_detail.deleted_at IS '软删除时间';
COMMENT ON COLUMN cid_account_report_detail.cid IS '广告账户ID';
COMMENT ON COLUMN cid_account_report_detail.report_date_str IS '报告日期';
COMMENT ON COLUMN cid_account_report_detail.account_id IS '账户ID';
COMMENT ON COLUMN cid_account_report_detail.account_name IS '账户名称';
COMMENT ON COLUMN cid_account_report_detail.cost_total IS '总花费';
COMMENT ON COLUMN cid_account_report_detail.impression IS '曝光数';
COMMENT ON COLUMN cid_account_report_detail.click IS '点击数';
COMMENT ON COLUMN cid_account_report_detail.ctr IS '点击率';
COMMENT ON COLUMN cid_account_report_detail.cpc IS '点击单价';
COMMENT ON COLUMN cid_account_report_detail.cpm IS '千次曝光成本';
COMMENT ON COLUMN cid_account_report_detail.conversion IS '转化数';
COMMENT ON COLUMN cid_account_report_detail.conversion_rate IS '转化率';
COMMENT ON COLUMN cid_account_report_detail.cpa IS '转化成本';
COMMENT ON COLUMN cid_account_report_detail.datasource IS '数据源';

CREATE INDEX idx_cid_account_report_detail_cid ON cid_account_report_detail(tenant_id, cid);
CREATE INDEX idx_cid_account_report_detail_date ON cid_account_report_detail(tenant_id, report_date_str);
CREATE INDEX idx_cid_account_report_detail_datasource ON cid_account_report_detail(tenant_id, datasource);

-- =============================================
-- 账户报告汇总表 (cid_account_report_sum)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS cid_account_report_sum_id_seq START WITH 1 INCREMENT BY 1;

CREATE TABLE IF NOT EXISTS cid_account_report_sum (
    id BIGINT NOT NULL DEFAULT nextval('cid_account_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 '',
    report_date_str VARCHAR(50),
    account_id VARCHAR(100),
    account_name VARCHAR(200),
    cost_total DECIMAL(20,4),
    impression BIGINT,
    click BIGINT,
    ctr DECIMAL(10,6),
    cpc DECIMAL(20,4),
    cpm DECIMAL(20,4),
    conversion BIGINT,
    conversion_rate DECIMAL(10,6),
    cpa DECIMAL(20,4),
    datasource VARCHAR(64) NOT NULL DEFAULT '',
    PRIMARY KEY (id)
);

COMMENT ON TABLE cid_account_report_sum IS '账户报告汇总表';
COMMENT ON COLUMN cid_account_report_sum.id IS '主键ID';
COMMENT ON COLUMN cid_account_report_sum.tenant_id IS '租户ID';
COMMENT ON COLUMN cid_account_report_sum.creator IS '创建人';
COMMENT ON COLUMN cid_account_report_sum.created_at IS '创建时间';
COMMENT ON COLUMN cid_account_report_sum.updater IS '更新人';
COMMENT ON COLUMN cid_account_report_sum.updated_at IS '更新时间';
COMMENT ON COLUMN cid_account_report_sum.deleted_at IS '软删除时间';
COMMENT ON COLUMN cid_account_report_sum.cid IS '广告账户ID';
COMMENT ON COLUMN cid_account_report_sum.report_date_str IS '报告日期';
COMMENT ON COLUMN cid_account_report_sum.account_id IS '账户ID';
COMMENT ON COLUMN cid_account_report_sum.account_name IS '账户名称';
COMMENT ON COLUMN cid_account_report_sum.cost_total IS '总花费';
COMMENT ON COLUMN cid_account_report_sum.impression IS '曝光数';
COMMENT ON COLUMN cid_account_report_sum.click IS '点击数';
COMMENT ON COLUMN cid_account_report_sum.ctr IS '点击率';
COMMENT ON COLUMN cid_account_report_sum.cpc IS '点击单价';
COMMENT ON COLUMN cid_account_report_sum.cpm IS '千次曝光成本';
COMMENT ON COLUMN cid_account_report_sum.conversion IS '转化数';
COMMENT ON COLUMN cid_account_report_sum.conversion_rate IS '转化率';
COMMENT ON COLUMN cid_account_report_sum.cpa IS '转化成本';
COMMENT ON COLUMN cid_account_report_sum.datasource IS '数据源';

CREATE INDEX idx_cid_account_report_sum_cid ON cid_account_report_sum(tenant_id, cid);
CREATE INDEX idx_cid_account_report_sum_date ON cid_account_report_sum(tenant_id, report_date_str);
CREATE INDEX idx_cid_account_report_sum_datasource ON cid_account_report_sum(tenant_id, datasource);
