-- Sync Task and Task Report Tables (同步任务日志和任务报告表)

-- =============================================
-- 同步任务日志表 (sync_task_log)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS sync_task_log_id_seq;

CREATE TABLE IF NOT EXISTS sync_task_log (
    id BIGINT NOT NULL DEFAULT nextval('sync_task_log_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,
    task_id BIGINT NOT NULL DEFAULT 0,
    report_type VARCHAR(64) NOT NULL DEFAULT '',
    report_date VARCHAR(10) NOT NULL DEFAULT '',
    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 '',
    datasource VARCHAR(64) NOT NULL DEFAULT '',
    sync_status VARCHAR(32) NOT NULL DEFAULT '',
    error_message TEXT NOT NULL DEFAULT '',
    start_time TIMESTAMP WITH TIME ZONE DEFAULT '0001-01-01 00:00:00+00:00',
    end_time TIMESTAMP WITH TIME ZONE DEFAULT '0001-01-01 00:00:00+00:00',
    PRIMARY KEY (id)
);

COMMENT ON TABLE sync_task_log IS '同步任务日志表';
COMMENT ON COLUMN sync_task_log.id IS '主键ID';
COMMENT ON COLUMN sync_task_log.tenant_id IS '租户ID';
COMMENT ON COLUMN sync_task_log.creator IS '创建人';
COMMENT ON COLUMN sync_task_log.created_at IS '创建时间';
COMMENT ON COLUMN sync_task_log.updater IS '更新人';
COMMENT ON COLUMN sync_task_log.updated_at IS '更新时间';
COMMENT ON COLUMN sync_task_log.deleted_at IS '软删除时间';
COMMENT ON COLUMN sync_task_log.task_id IS '调控任务ID';
COMMENT ON COLUMN sync_task_log.report_type IS '报告类型';
COMMENT ON COLUMN sync_task_log.report_date IS '报告日期';
COMMENT ON COLUMN sync_task_log.cid IS '广告账户ID';
COMMENT ON COLUMN sync_task_log.campaign_id IS '广告系列ID';
COMMENT ON COLUMN sync_task_log.adgroup_id IS '广告组ID';
COMMENT ON COLUMN sync_task_log.ad_id IS '创意ID';
COMMENT ON COLUMN sync_task_log.datasource IS '数据源';
COMMENT ON COLUMN sync_task_log.sync_status IS '同步状态';
COMMENT ON COLUMN sync_task_log.error_message IS '错误信息';
COMMENT ON COLUMN sync_task_log.start_time IS '开始时间';
COMMENT ON COLUMN sync_task_log.end_time IS '结束时间';

CREATE INDEX idx_sync_task_log_task_id ON sync_task_log(tenant_id, task_id);
CREATE INDEX idx_sync_task_log_report_type ON sync_task_log(tenant_id, report_type);
CREATE INDEX idx_sync_task_log_report_date ON sync_task_log(tenant_id, report_date);
CREATE INDEX idx_sync_task_log_cid ON sync_task_log(tenant_id, cid);
CREATE INDEX idx_sync_task_log_datasource ON sync_task_log(tenant_id, datasource);
CREATE INDEX idx_sync_task_log_sync_status ON sync_task_log(tenant_id, sync_status);
CREATE INDEX idx_sync_task_log_created_at ON sync_task_log(tenant_id, created_at);

-- =============================================
-- 调控任务报告表 (task_report)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS task_report_id_seq;

CREATE TABLE IF NOT EXISTS task_report (
    id BIGINT NOT NULL DEFAULT nextval('task_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,
    task_id BIGINT NOT NULL DEFAULT 0,
    datasource VARCHAR(64) NOT NULL DEFAULT '',
    report_date VARCHAR(10) NOT NULL DEFAULT '',
    total_count BIGINT NOT NULL DEFAULT 0,
    success_count BIGINT NOT NULL DEFAULT 0,
    fail_count BIGINT NOT NULL DEFAULT 0,
    skip_count BIGINT NOT NULL DEFAULT 0,
    total_cost DECIMAL(20,6) NOT NULL DEFAULT 0,
    total_impression DECIMAL(20,6) NOT NULL DEFAULT 0,
    total_click DECIMAL(20,6) NOT NULL DEFAULT 0,
    total_conversion DECIMAL(20,6) NOT NULL DEFAULT 0,
    avg_ctr DECIMAL(20,8) NOT NULL DEFAULT 0,
    avg_cpc DECIMAL(20,8) NOT NULL DEFAULT 0,
    avg_cpm DECIMAL(20,8) NOT NULL DEFAULT 0,
    avg_conversion_rate DECIMAL(20,8) NOT NULL DEFAULT 0,
    avg_cpp DECIMAL(20,8) NOT NULL DEFAULT 0,
    PRIMARY KEY (id)
);

COMMENT ON TABLE task_report IS '调控任务报告表';
COMMENT ON COLUMN task_report.id IS '主键ID';
COMMENT ON COLUMN task_report.tenant_id IS '租户ID';
COMMENT ON COLUMN task_report.creator IS '创建人';
COMMENT ON COLUMN task_report.created_at IS '创建时间';
COMMENT ON COLUMN task_report.updater IS '更新人';
COMMENT ON COLUMN task_report.updated_at IS '更新时间';
COMMENT ON COLUMN task_report.deleted_at IS '软删除时间';
COMMENT ON COLUMN task_report.task_id IS '调控任务ID';
COMMENT ON COLUMN task_report.datasource IS '数据源';
COMMENT ON COLUMN task_report.report_date IS '报告日期';
COMMENT ON COLUMN task_report.total_count IS '总数量';
COMMENT ON COLUMN task_report.success_count IS '成功数量';
COMMENT ON COLUMN task_report.fail_count IS '失败数量';
COMMENT ON COLUMN task_report.skip_count IS '跳过数量';
COMMENT ON COLUMN task_report.total_cost IS '总花费(元)';
COMMENT ON COLUMN task_report.total_impression IS '总展示数';
COMMENT ON COLUMN task_report.total_click IS '总点击数';
COMMENT ON COLUMN task_report.total_conversion IS '总转化数';
COMMENT ON COLUMN task_report.avg_ctr IS '平均点击率';
COMMENT ON COLUMN task_report.avg_cpc IS '平均点击价格';
COMMENT ON COLUMN task_report.avg_cpm IS '平均千次展现成本';
COMMENT ON COLUMN task_report.avg_conversion_rate IS '平均转化率';
COMMENT ON COLUMN task_report.avg_cpp IS '平均单次转化成本';

CREATE INDEX idx_task_report_task_id ON task_report(tenant_id, task_id);
CREATE INDEX idx_task_report_datasource ON task_report(tenant_id, datasource);
CREATE INDEX idx_task_report_report_date ON task_report(tenant_id, report_date);
CREATE UNIQUE INDEX idx_task_report_unique ON task_report(tenant_id, task_id, datasource, report_date);
