-- ============================================================
-- 通用报表公共包 - 系统表 DDL
-- 业务隔离、多业务支持、表/字段 100% 配置化
-- ============================================================

-- ----------------------------------------------------------
-- 1. 业务配置表 (business_config)
-- ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS report_business_config (
    id BIGSERIAL PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    business_code VARCHAR(64) NOT NULL,
    business_name VARCHAR(128) NOT NULL,
    description TEXT DEFAULT '',
    status VARCHAR(16) NOT NULL DEFAULT 'ACTIVE',
    config JSONB DEFAULT '{}',
    creator VARCHAR(64) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updater VARCHAR(64) DEFAULT '',
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE
);

COMMENT ON TABLE report_business_config IS '报表业务配置表';
COMMENT ON COLUMN report_business_config.tenant_id IS '租户ID';
COMMENT ON COLUMN report_business_config.business_code IS '业务编码(唯一标识)';
COMMENT ON COLUMN report_business_config.business_name IS '业务名称';
COMMENT ON COLUMN report_business_config.description IS '业务描述';
COMMENT ON COLUMN report_business_config.status IS '状态: ACTIVE启用/INACTIVE停用';
COMMENT ON COLUMN report_business_config.config IS '业务级配置(JSON)，如默认数据源等';

CREATE UNIQUE INDEX IF NOT EXISTS idx_business_code ON report_business_config (tenant_id, business_code);

-- ----------------------------------------------------------
-- 2. 报表配置表 (report_config)
-- ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS report_report_config (
    id BIGSERIAL PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    business_code VARCHAR(64) NOT NULL,
    report_code VARCHAR(64) NOT NULL,
    report_name VARCHAR(128) NOT NULL,
    description TEXT DEFAULT '',
    status VARCHAR(16) NOT NULL DEFAULT 'ACTIVE',
    stat_table_name VARCHAR(128) NOT NULL,
    stat_table_comment VARCHAR(256) DEFAULT '',
    date_field VARCHAR(64) DEFAULT 'stat_date',
    primary_keys JSONB DEFAULT '["id"]'::jsonb,
    conflict_keys JSONB DEFAULT '["stat_date"]'::jsonb,
    config JSONB DEFAULT '{}',
    creator VARCHAR(64) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updater VARCHAR(64) DEFAULT '',
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    CONSTRAINT uk_business_report_code UNIQUE (tenant_id, business_code, report_code)
);

COMMENT ON TABLE report_report_config IS '报表配置表';
COMMENT ON COLUMN report_report_config.tenant_id IS '租户ID';
COMMENT ON COLUMN report_report_config.business_code IS '所属业务编码';
COMMENT ON COLUMN report_report_config.report_code IS '报表编码(唯一标识)';
COMMENT ON COLUMN report_report_config.report_name IS '报表名称';
COMMENT ON COLUMN report_report_config.stat_table_name IS '统计宽表表名';
COMMENT ON COLUMN report_report_config.stat_table_comment IS '统计宽表注释';
COMMENT ON COLUMN report_report_config.date_field IS '日期字段名';
COMMENT ON COLUMN report_report_config.primary_keys IS '主键字段列表(JSON数组)';
COMMENT ON COLUMN report_report_config.conflict_keys IS '冲突键(用于ON CONFLICT upsert)';
COMMENT ON COLUMN report_report_config.config IS '报表级配置(JSON)';

CREATE INDEX IF NOT EXISTS idx_report_business_code ON report_report_config (tenant_id, business_code);

-- ----------------------------------------------------------
-- 3. 报表字段配置表 (report_field_config)
-- 维度、指标、筛选字段 100% 配置化
-- ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS report_field_config (
    id BIGSERIAL PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    business_code VARCHAR(64) NOT NULL,
    report_code VARCHAR(64) NOT NULL,
    field_code VARCHAR(64) NOT NULL,
    field_name VARCHAR(128) NOT NULL,
    field_type VARCHAR(32) NOT NULL,
    data_type VARCHAR(32) NOT NULL DEFAULT 'STRING',
    field_role VARCHAR(32) NOT NULL,
    is_aggregatable BOOLEAN DEFAULT FALSE,
    is_filterable BOOLEAN DEFAULT TRUE,
    is_queryable BOOLEAN DEFAULT TRUE,
    is_sortable BOOLEAN DEFAULT TRUE,
    default_aggregate VARCHAR(32) DEFAULT '',
    valid_aggregates JSONB DEFAULT '[]'::jsonb,
    filter_operators JSONB DEFAULT '["=","!=",">","<",">=","<=","IN","LIKE","BETWEEN"]'::jsonb,
    expression VARCHAR(512) DEFAULT '',
    expression_type VARCHAR(32) DEFAULT '',
    format_pattern VARCHAR(64) DEFAULT '',
    unit VARCHAR(32) DEFAULT '',
    dict_code VARCHAR(64) DEFAULT '',
    sort_order INT DEFAULT 0,
    group_name VARCHAR(64) DEFAULT '',
    status VARCHAR(16) NOT NULL DEFAULT 'ACTIVE',
    creator VARCHAR(64) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updater VARCHAR(64) DEFAULT '',
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    CONSTRAINT uk_business_report_field_code UNIQUE (tenant_id, business_code, report_code, field_code)
);

COMMENT ON TABLE report_field_config IS '报表字段配置表(维度/指标/筛选)';
COMMENT ON COLUMN report_field_config.tenant_id IS '租户ID';
COMMENT ON COLUMN report_field_config.business_code IS '所属业务编码';
COMMENT ON COLUMN report_field_config.report_code IS '所属报表编码';
COMMENT ON COLUMN report_field_config.field_code IS '字段编码(唯一)';
COMMENT ON COLUMN report_field_config.field_name IS '字段显示名称';
COMMENT ON COLUMN report_field_config.field_type IS '字段类型: STRING/INT/FLOAT/DATE/DATETIME/JSONB';
COMMENT ON COLUMN report_field_config.data_type IS '数据类型: DIMENSION维度/INDICATOR指标/FILTER筛选';
COMMENT ON COLUMN report_field_config.field_role IS '字段角色: DIMENSION维度/INDICATOR指标/FILTER筛选/FILTER_ONLY仅筛选';
COMMENT ON COLUMN report_field_config.is_aggregatable IS '是否可统计(sum/count/avg)';
COMMENT ON COLUMN report_field_config.is_filterable IS '是否可筛选';
COMMENT ON COLUMN report_field_config.is_queryable IS '是否可查询';
COMMENT ON COLUMN report_field_config.is_sortable IS '是否可排序';
COMMENT ON COLUMN report_field_config.default_aggregate IS '默认聚合方式: SUM/COUNT/AVG/MAX/MIN';
COMMENT ON COLUMN report_field_config.valid_aggregates IS '支持的聚合方式(JSON数组)';
COMMENT ON COLUMN report_field_config.filter_operators IS '支持的筛选操作符(JSON数组)';
COMMENT ON COLUMN report_field_config.expression IS '计算表达式(如退款率=退款金额/销售额*100)';
COMMENT ON COLUMN report_field_config.expression_type IS '表达式类型: CALCULATED衍生/ORIGINAL原始';
COMMENT ON COLUMN report_field_config.format_pattern IS '格式化模式(如 #,##0.00%)';
COMMENT ON COLUMN report_field_config.unit IS '单位(如元/件/%)';
COMMENT ON COLUMN report_field_config.dict_code IS '字典编码(用于下拉选项)';
COMMENT ON COLUMN report_field_config.sort_order IS '排序顺序';
COMMENT ON COLUMN report_field_config.group_name IS '字段分组名称';

CREATE INDEX IF NOT EXISTS idx_field_business_report ON report_field_config (business_code, report_code);
CREATE INDEX IF NOT EXISTS idx_field_data_type ON report_field_config (data_type);
CREATE INDEX IF NOT EXISTS idx_field_field_role ON report_field_config (field_role);

-- ----------------------------------------------------------
-- 4. 数据抽取配置表 (extract_config)
-- 100% 配置化，不硬编码任何抽取逻辑
-- ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS report_extract_config (
    id BIGSERIAL PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    business_code VARCHAR(64) NOT NULL,
    report_code VARCHAR(64) NOT NULL,
    extract_code VARCHAR(64) NOT NULL,
    extract_name VARCHAR(128) NOT NULL,
    source_table_name VARCHAR(128) NOT NULL,
    source_table_alias VARCHAR(64) DEFAULT '',
    target_table_name VARCHAR(128) NOT NULL,
    is_enabled BOOLEAN DEFAULT TRUE,
    extract_type VARCHAR(32) NOT NULL DEFAULT 'FULL',
    extract_mode VARCHAR(32) NOT NULL DEFAULT 'DIRECT',
    extract_key_field VARCHAR(64) DEFAULT '',
    extract_key_format VARCHAR(64) DEFAULT '',
    group_by_fields JSONB DEFAULT '[]'::jsonb,
    filter_expression TEXT DEFAULT '',
    join_configs JSONB DEFAULT '[]'::jsonb,
    field_mappings JSONB DEFAULT '[]'::jsonb,
    transform_rules JSONB DEFAULT '[]'::jsonb,
    batch_size INT DEFAULT 1000,
    status VARCHAR(16) NOT NULL DEFAULT 'ACTIVE',
    creator VARCHAR(64) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updater VARCHAR(64) DEFAULT '',
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    CONSTRAINT uk_business_report_extract_code UNIQUE (tenant_id, business_code, report_code, extract_code)
);

COMMENT ON TABLE report_extract_config IS '数据抽取配置表';
COMMENT ON COLUMN report_extract_config.tenant_id IS '租户ID';
COMMENT ON COLUMN report_extract_config.business_code IS '所属业务编码';
COMMENT ON COLUMN report_extract_config.report_code IS '所属报表编码';
COMMENT ON COLUMN report_extract_config.extract_code IS '抽取配置编码';
COMMENT ON COLUMN report_extract_config.extract_name IS '抽取配置名称';
COMMENT ON COLUMN report_extract_config.source_table_name IS '源表表名';
COMMENT ON COLUMN report_extract_config.source_table_alias IS '源表别名';
COMMENT ON COLUMN report_extract_config.target_table_name IS '目标表表名(统计宽表)';
COMMENT ON COLUMN report_extract_config.extract_type IS '抽取类型: FULL全量/INCREMENTAL增量';
COMMENT ON COLUMN report_extract_config.extract_mode IS '抽取模式: DIRECT逐行抽取/AGGREGATE按GROUP BY聚合';
COMMENT ON COLUMN report_extract_config.extract_key_field IS '增量抽取关键字段(如updated_at)';
COMMENT ON COLUMN report_extract_config.extract_key_format IS '关键字段格式(如yyyy-MM-dd HH:mm:ss)';
COMMENT ON COLUMN report_extract_config.group_by_fields IS 'AGGREGATE模式下的GROUP BY字段(JSON数组)';
COMMENT ON COLUMN report_extract_config.filter_expression IS '抽取条件表达式';
COMMENT ON COLUMN report_extract_config.join_configs IS '关联配置(JSON数组)';
COMMENT ON COLUMN report_extract_config.field_mappings IS '字段映射配置(JSON数组)';
COMMENT ON COLUMN report_extract_config.transform_rules IS '转换规则(JSON数组)';
COMMENT ON COLUMN report_extract_config.batch_size IS '批次大小';

CREATE INDEX IF NOT EXISTS idx_extract_business_report ON report_extract_config (business_code, report_code);

-- ----------------------------------------------------------
-- 5. 抽取记录表 (extract_log) - 用于幂等和追踪
-- ----------------------------------------------------------
CREATE TABLE IF NOT EXISTS report_extract_log (
    id BIGSERIAL PRIMARY KEY,
    tenant_id BIGINT NOT NULL DEFAULT 0,
    business_code VARCHAR(64) NOT NULL,
    report_code VARCHAR(64) NOT NULL,
    extract_code VARCHAR(64) NOT NULL,
    stat_date VARCHAR(16) NOT NULL,
    extract_type VARCHAR(32) NOT NULL,
    status VARCHAR(16) NOT NULL DEFAULT 'RUNNING',
    total_count INT DEFAULT 0,
    success_count INT DEFAULT 0,
    fail_count INT DEFAULT 0,
    start_time TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    end_time TIMESTAMP WITH TIME ZONE,
    error_message TEXT DEFAULT '',
    executor VARCHAR(64) DEFAULT '',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),

    CONSTRAINT uk_extract_keys UNIQUE (tenant_id, business_code, report_code, extract_code, stat_date)
);

COMMENT ON TABLE report_extract_log IS '抽取记录表(幂等追踪)';
COMMENT ON COLUMN report_extract_log.tenant_id IS '租户ID';
COMMENT ON COLUMN report_extract_log.business_code IS '业务编码';
COMMENT ON COLUMN report_extract_log.report_code IS '报表编码';
COMMENT ON COLUMN report_extract_log.extract_code IS '抽取配置编码';
COMMENT ON COLUMN report_extract_log.stat_date IS '统计日期(yyyy-MM-dd)';
COMMENT ON COLUMN report_extract_log.extract_type IS '抽取类型: FULL/INCREMENTAL';
COMMENT ON COLUMN report_extract_log.status IS '状态: RUNNING运行中/SUCCESS成功/FAILED失败';
COMMENT ON COLUMN report_extract_log.total_count IS '总记录数';
COMMENT ON COLUMN report_extract_log.success_count IS '成功记录数';
COMMENT ON COLUMN report_extract_log.fail_count IS '失败记录数';
CREATE INDEX IF NOT EXISTS idx_extract_log_business_report ON report_extract_log (business_code, report_code);
CREATE INDEX IF NOT EXISTS idx_extract_log_stat_date ON report_extract_log (stat_date);

-- ============================================================
-- 字段映射配置结构说明 (field_mappings JSONB)
-- ============================================================
-- [
--   {
--     "source_field": "order_id",
--     "target_field": "order_id",
--     "field_type": "STRING",
--     "default_value": "",
--     "transform_rule": {
--       "type": "DIRECT/MAPPING/CALCULATE",
--       "expression": ""
--     }
--   },
--   {
--     "source_field": "sale_amount",
--     "target_field": "sale_amount_yuan",
--     "field_type": "FLOAT",
--     "default_value": 0,
--     "transform_rule": {
--       "type": "CALCULATE",
--       "expression": "source_value / 100"
--     }
--   }
-- ]

-- ============================================================
-- 转换规则配置结构说明 (transform_rules JSONB)
-- ============================================================
-- [
--   {
--     "rule_code": "status_mapping",
--     "rule_type": "MAPPING",
--     "source_field": "order_status",
--     "target_field": "status_name",
--     "mapping": {
--       "PENDING": "待支付",
--       "PAID": "已支付",
--       "SHIPPED": "已发货",
--       "COMPLETED": "已完成",
--       "REFUNDED": "已退款"
--     }
--   },
--   {
--     "rule_code": "date_format",
--     "rule_type": "FORMAT",
--     "source_field": "create_time",
--     "target_field": "stat_date",
--     "format": "yyyy-MM-dd"
--   }
-- ]

-- ============================================================
-- 关联配置结构说明 (join_configs JSONB)
-- ============================================================
-- [
--   {
--     "join_table": "product_info",
--     "join_alias": "p",
--     "join_type": "LEFT",
--     "join_condition": "s.product_id = p.id",
--     "field_mappings": [
--       {"source_field": "p.category_name", "target_field": "category_name"}
--     ]
--   }
-- ]
