-- CID数据库表结构

-- 应用管理表
CREATE TABLE IF NOT EXISTS cid_application (
    id BIGSERIAL PRIMARY KEY,
    tenant_id VARCHAR(64) DEFAULT '',
    name VARCHAR(255) NOT NULL COMMENT '应用名称',
    app_code VARCHAR(100) NOT NULL COMMENT '应用编码(唯一标识)',
    type VARCHAR(50) NOT NULL COMMENT '应用类型',
    status VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT '应用状态：active启用/inactive停用',
    description TEXT COMMENT '应用描述',
    access_config JSONB COMMENT '接入配置',
    limit_config JSONB COMMENT '限流配置',
    callback_config JSONB COMMENT '回调配置',
    created_at BIGINT NOT NULL,
    updated_at BIGINT NOT NULL,
    deleted_at BIGINT DEFAULT 0,
    CONSTRAINT uk_cid_application_app_code UNIQUE (app_code, deleted_at)
);

CREATE INDEX idx_cid_application_tenant ON cid_application(tenant_id);
CREATE INDEX idx_cid_application_type ON cid_application(type);
CREATE INDEX idx_cid_application_status ON cid_application(status);
CREATE INDEX idx_cid_application_name ON cid_application(name);
CREATE INDEX idx_cid_application_app_code ON cid_application(app_code);

-- 平台管理表
CREATE TABLE IF NOT EXISTS cid_platform (
    id BIGSERIAL PRIMARY KEY,
    tenant_id VARCHAR(64) DEFAULT '',
    name VARCHAR(255) NOT NULL COMMENT '平台名称',
    type VARCHAR(50) NOT NULL COMMENT '平台类型',
    status VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT '平台状态：active启用/inactive停用',
    description TEXT COMMENT '平台描述',
    auth_config JSONB COMMENT '认证配置',
    limit_config JSONB COMMENT '限流配置',
    platform_config JSONB COMMENT '平台专用配置',
    created_at BIGINT NOT NULL,
    updated_at BIGINT NOT NULL,
    deleted_at BIGINT DEFAULT 0
);

CREATE INDEX idx_cid_platform_tenant ON cid_platform(tenant_id);
CREATE INDEX idx_cid_platform_type ON cid_platform(type);
CREATE INDEX idx_cid_platform_status ON cid_platform(status);
CREATE INDEX idx_cid_platform_name ON cid_platform(name);

-- 接口管理表
CREATE TABLE IF NOT EXISTS cid_api_interface (
    id BIGSERIAL PRIMARY KEY,
    tenant_id VARCHAR(64) DEFAULT '',
    platform_id BIGINT NOT NULL COMMENT '所属平台ID',
    name VARCHAR(255) NOT NULL COMMENT '接口名称',
    code VARCHAR(100) NOT NULL COMMENT '接口编码',
    url VARCHAR(500) NOT NULL COMMENT '接口地址',
    method VARCHAR(10) NOT NULL COMMENT '请求方法：GET/POST/PUT/DELETE等',
    status VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT '接口状态：active启用/inactive停用',
    auth_type VARCHAR(50) COMMENT '认证类型：oauth2/apikey/basic等',
    request_config JSONB COMMENT '请求配置',
    response_config JSONB COMMENT '响应配置',
    limit_config JSONB COMMENT '接口独立限流配置（可选，覆盖平台配置）',
    created_at BIGINT NOT NULL,
    updated_at BIGINT NOT NULL,
    deleted_at BIGINT DEFAULT 0,
    CONSTRAINT fk_cid_api_interface_platform FOREIGN KEY (platform_id) REFERENCES cid_platform(id)
);

CREATE INDEX idx_cid_api_interface_tenant ON cid_api_interface(tenant_id);
CREATE INDEX idx_cid_api_interface_platform ON cid_api_interface(platform_id);
CREATE INDEX idx_cid_api_interface_code ON cid_api_interface(code);
CREATE INDEX idx_cid_api_interface_status ON cid_api_interface(status);
CREATE INDEX idx_cid_api_interface_name ON cid_api_interface(name);

-- 数据获取日志表
CREATE TABLE IF NOT EXISTS cid_data_fetch_log (
    id BIGSERIAL PRIMARY KEY,
    tenant_id VARCHAR(64) DEFAULT '',
    platform_id BIGINT NOT NULL COMMENT '平台ID',
    interface_id BIGINT NOT NULL COMMENT '接口ID',
    request_id VARCHAR(100) NOT NULL COMMENT '请求ID',
    status VARCHAR(20) NOT NULL DEFAULT 'pending' COMMENT '执行状态：pending/running/success/failed/rate_limit',
    start_time BIGINT NOT NULL COMMENT '开始时间（时间戳）',
    end_time BIGINT DEFAULT 0 COMMENT '结束时间（时间戳）',
    duration INT DEFAULT 0 COMMENT '执行时长（毫秒）',
    request_config JSONB COMMENT '请求配置参数',
    response_data TEXT COMMENT '响应数据（JSON）',
    error_message TEXT COMMENT '错误信息',
    retry_count INT DEFAULT 0 COMMENT '重试次数',
    created_at BIGINT NOT NULL,
    updated_at BIGINT NOT NULL,
    deleted_at BIGINT DEFAULT 0,
    CONSTRAINT fk_cid_data_fetch_log_platform FOREIGN KEY (platform_id) REFERENCES cid_platform(id),
    CONSTRAINT fk_cid_data_fetch_log_interface FOREIGN KEY (interface_id) REFERENCES cid_api_interface(id)
);

CREATE INDEX idx_cid_data_fetch_log_tenant ON cid_data_fetch_log(tenant_id);
CREATE INDEX idx_cid_data_fetch_log_platform ON cid_data_fetch_log(platform_id);
CREATE INDEX idx_cid_data_fetch_log_interface ON cid_data_fetch_log(interface_id);
CREATE INDEX idx_cid_data_fetch_log_request_id ON cid_data_fetch_log(request_id);
CREATE INDEX idx_cid_data_fetch_log_status ON cid_data_fetch_log(status);
CREATE INDEX idx_cid_data_fetch_log_start_time ON cid_data_fetch_log(start_time);

-- 数据映射表
CREATE TABLE IF NOT EXISTS cid_data_mapping (
    id BIGSERIAL PRIMARY KEY,
    tenant_id VARCHAR(64) DEFAULT '',
    platform_id BIGINT NOT NULL COMMENT '平台ID',
    interface_id BIGINT NOT NULL COMMENT '接口ID',
    source_field VARCHAR(255) NOT NULL COMMENT '源字段（接口返回字段）',
    target_field VARCHAR(255) NOT NULL COMMENT '目标字段（本地表字段）',
    field_type VARCHAR(50) NOT NULL COMMENT '字段类型：string/int/float/bool/array/object',
    default_value VARCHAR(500) COMMENT '默认值',
    transform_rule JSONB COMMENT '转换规则',
    priority INT DEFAULT 0 COMMENT '优先级（数字越小优先级越高）',
    status VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT '状态：active启用/inactive停用',
    created_at BIGINT NOT NULL,
    updated_at BIGINT NOT NULL,
    deleted_at BIGINT DEFAULT 0,
    CONSTRAINT fk_cid_data_mapping_platform FOREIGN KEY (platform_id) REFERENCES cid_platform(id),
    CONSTRAINT fk_cid_data_mapping_interface FOREIGN KEY (interface_id) REFERENCES cid_api_interface(id),
    CONSTRAINT uk_cid_data_mapping_interface_target UNIQUE (interface_id, target_field, deleted_at)
);

CREATE INDEX idx_cid_data_mapping_tenant ON cid_data_mapping(tenant_id);
CREATE INDEX idx_cid_data_mapping_platform ON cid_data_mapping(platform_id);
CREATE INDEX idx_cid_data_mapping_interface ON cid_data_mapping(interface_id);
CREATE INDEX idx_cid_data_mapping_source_field ON cid_data_mapping(source_field);
CREATE INDEX idx_cid_data_mapping_status ON cid_data_mapping(status);
CREATE INDEX idx_cid_data_mapping_priority ON cid_data_mapping(priority);
