-- CID数据库表结构 - PostgreSQL 版本

-- 应用管理表
CREATE TABLE IF NOT EXISTS cid_application (
                                               id BIGSERIAL PRIMARY KEY,
                                               tenant_id VARCHAR(64) DEFAULT '',
    name VARCHAR(255) NOT NULL,
    app_code VARCHAR(100) NOT NULL,
    type VARCHAR(50) NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'active',
    description TEXT,
    access_config JSONB,
    limit_config JSONB,
    callback_config JSONB,
    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 IF NOT EXISTS idx_cid_application_tenant ON cid_application(tenant_id);
CREATE INDEX IF NOT EXISTS idx_cid_application_type ON cid_application(type);
CREATE INDEX IF NOT EXISTS idx_cid_application_status ON cid_application(status);
CREATE INDEX IF NOT EXISTS idx_cid_application_name ON cid_application(name);
CREATE INDEX IF NOT EXISTS idx_cid_application_app_code ON cid_application(app_code);

-- 应用表注释
COMMENT ON TABLE cid_application IS '应用管理表';
COMMENT ON COLUMN cid_application.id IS '主键ID';
COMMENT ON COLUMN cid_application.tenant_id IS '租户ID';
COMMENT ON COLUMN cid_application.name IS '应用名称';
COMMENT ON COLUMN cid_application.app_code IS '应用编码(唯一标识)';
COMMENT ON COLUMN cid_application.type IS '应用类型';
COMMENT ON COLUMN cid_application.status IS '应用状态：active启用/inactive停用';
COMMENT ON COLUMN cid_application.description IS '应用描述';
COMMENT ON COLUMN cid_application.access_config IS '接入配置';
COMMENT ON COLUMN cid_application.limit_config IS '限流配置';
COMMENT ON COLUMN cid_application.callback_config IS '回调配置';
COMMENT ON COLUMN cid_application.created_at IS '创建时间';
COMMENT ON COLUMN cid_application.updated_at IS '更新时间';
COMMENT ON COLUMN cid_application.deleted_at IS '软删除时间戳，0表示未删除';

-- 平台管理表
CREATE TABLE IF NOT EXISTS cid_platform (
                                            id BIGSERIAL PRIMARY KEY,
                                            tenant_id VARCHAR(64) DEFAULT '',
    name VARCHAR(255) NOT NULL,
    type VARCHAR(50) NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'active',
    description TEXT,
    auth_config JSONB,
    limit_config JSONB,
    platform_config JSONB,
    created_at BIGINT NOT NULL,
    updated_at BIGINT NOT NULL,
    deleted_at BIGINT DEFAULT 0
    );

-- 平台表索引
CREATE INDEX IF NOT EXISTS idx_cid_platform_tenant ON cid_platform(tenant_id);
CREATE INDEX IF NOT EXISTS idx_cid_platform_type ON cid_platform(type);
CREATE INDEX IF NOT EXISTS idx_cid_platform_status ON cid_platform(status);
CREATE INDEX IF NOT EXISTS idx_cid_platform_name ON cid_platform(name);

-- 平台表注释
COMMENT ON TABLE cid_platform IS '平台管理表';
COMMENT ON COLUMN cid_platform.id IS '主键ID';
COMMENT ON COLUMN cid_platform.tenant_id IS '租户ID';
COMMENT ON COLUMN cid_platform.name IS '平台名称';
COMMENT ON COLUMN cid_platform.type IS '平台类型';
COMMENT ON COLUMN cid_platform.status IS '平台状态：active启用/inactive停用';
COMMENT ON COLUMN cid_platform.description IS '平台描述';
COMMENT ON COLUMN cid_platform.auth_config IS '认证配置';
COMMENT ON COLUMN cid_platform.limit_config IS '限流配置';
COMMENT ON COLUMN cid_platform.platform_config IS '平台专用配置';
COMMENT ON COLUMN cid_platform.created_at IS '创建时间';
COMMENT ON COLUMN cid_platform.updated_at IS '更新时间';
COMMENT ON COLUMN cid_platform.deleted_at IS '软删除时间戳，0表示未删除';

-- 接口管理表
CREATE TABLE IF NOT EXISTS cid_api_interface (
                                                 id BIGSERIAL PRIMARY KEY,
                                                 tenant_id VARCHAR(64) DEFAULT '',
    platform_id BIGINT NOT NULL,
    name VARCHAR(255) NOT NULL,
    code VARCHAR(100) NOT NULL,
    url VARCHAR(500) NOT NULL,
    method VARCHAR(10) NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'active',
    auth_type VARCHAR(50),
    request_config JSONB,
    response_config JSONB,
    limit_config JSONB,
    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 IF NOT EXISTS idx_cid_api_interface_tenant ON cid_api_interface(tenant_id);
CREATE INDEX IF NOT EXISTS idx_cid_api_interface_platform ON cid_api_interface(platform_id);
CREATE INDEX IF NOT EXISTS idx_cid_api_interface_code ON cid_api_interface(code);
CREATE INDEX IF NOT EXISTS idx_cid_api_interface_status ON cid_api_interface(status);
CREATE INDEX IF NOT EXISTS idx_cid_api_interface_name ON cid_api_interface(name);

-- 接口表注释
COMMENT ON TABLE cid_api_interface IS '接口管理表';
COMMENT ON COLUMN cid_api_interface.id IS '主键ID';
COMMENT ON COLUMN cid_api_interface.tenant_id IS '租户ID';
COMMENT ON COLUMN cid_api_interface.platform_id IS '所属平台ID';
COMMENT ON COLUMN cid_api_interface.name IS '接口名称';
COMMENT ON COLUMN cid_api_interface.code IS '接口编码';
COMMENT ON COLUMN cid_api_interface.url IS '接口地址';
COMMENT ON COLUMN cid_api_interface.method IS '请求方法：GET/POST/PUT/DELETE等';
COMMENT ON COLUMN cid_api_interface.status IS '接口状态：active启用/inactive停用';
COMMENT ON COLUMN cid_api_interface.auth_type IS '认证类型：oauth2/apikey/basic等';
COMMENT ON COLUMN cid_api_interface.request_config IS '请求配置';
COMMENT ON COLUMN cid_api_interface.response_config IS '响应配置';
COMMENT ON COLUMN cid_api_interface.limit_config IS '接口独立限流配置（可选，覆盖平台配置）';
COMMENT ON COLUMN cid_api_interface.created_at IS '创建时间';
COMMENT ON COLUMN cid_api_interface.updated_at IS '更新时间';
COMMENT ON COLUMN cid_api_interface.deleted_at IS '软删除时间戳，0表示未删除';

-- 数据获取日志表
CREATE TABLE IF NOT EXISTS cid_data_fetch_log (
                                                  id BIGSERIAL PRIMARY KEY,
                                                  tenant_id VARCHAR(64) DEFAULT '',
    platform_id BIGINT NOT NULL,
    interface_id BIGINT NOT NULL,
    request_id VARCHAR(100) NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'pending',
    start_time BIGINT NOT NULL,
    end_time BIGINT DEFAULT 0,
    duration INT DEFAULT 0,
    request_config JSONB,
    response_data TEXT,
    error_message TEXT,
    retry_count INT DEFAULT 0,
    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 IF NOT EXISTS idx_cid_data_fetch_log_tenant ON cid_data_fetch_log(tenant_id);
CREATE INDEX IF NOT EXISTS idx_cid_data_fetch_log_platform ON cid_data_fetch_log(platform_id);
CREATE INDEX IF NOT EXISTS idx_cid_data_fetch_log_interface ON cid_data_fetch_log(interface_id);
CREATE INDEX IF NOT EXISTS idx_cid_data_fetch_log_request_id ON cid_data_fetch_log(request_id);
CREATE INDEX IF NOT EXISTS idx_cid_data_fetch_log_status ON cid_data_fetch_log(status);
CREATE INDEX IF NOT EXISTS idx_cid_data_fetch_log_start_time ON cid_data_fetch_log(start_time);

-- 日志表注释
COMMENT ON TABLE cid_data_fetch_log IS '数据获取日志表';
COMMENT ON COLUMN cid_data_fetch_log.id IS '主键ID';
COMMENT ON COLUMN cid_data_fetch_log.tenant_id IS '租户ID';
COMMENT ON COLUMN cid_data_fetch_log.platform_id IS '平台ID';
COMMENT ON COLUMN cid_data_fetch_log.interface_id IS '接口ID';
COMMENT ON COLUMN cid_data_fetch_log.request_id IS '请求ID';
COMMENT ON COLUMN cid_data_fetch_log.status IS '执行状态：pending/running/success/failed/rate_limit';
COMMENT ON COLUMN cid_data_fetch_log.start_time IS '开始时间（时间戳）';
COMMENT ON COLUMN cid_data_fetch_log.end_time IS '结束时间（时间戳）';
COMMENT ON COLUMN cid_data_fetch_log.duration IS '执行时长（毫秒）';
COMMENT ON COLUMN cid_data_fetch_log.request_config IS '请求配置参数';
COMMENT ON COLUMN cid_data_fetch_log.response_data IS '响应数据（JSON）';
COMMENT ON COLUMN cid_data_fetch_log.error_message IS '错误信息';
COMMENT ON COLUMN cid_data_fetch_log.retry_count IS '重试次数';
COMMENT ON COLUMN cid_data_fetch_log.created_at IS '创建时间';
COMMENT ON COLUMN cid_data_fetch_log.updated_at IS '更新时间';
COMMENT ON COLUMN cid_data_fetch_log.deleted_at IS '软删除时间戳，0表示未删除';

-- 数据映射表
CREATE TABLE IF NOT EXISTS cid_data_mapping (
                                                id BIGSERIAL PRIMARY KEY,
                                                tenant_id VARCHAR(64) DEFAULT '',
    platform_id BIGINT NOT NULL,
    interface_id BIGINT NOT NULL,
    source_field VARCHAR(255) NOT NULL,
    target_field VARCHAR(255) NOT NULL,
    field_type VARCHAR(50) NOT NULL,
    default_value VARCHAR(500),
    transform_rule JSONB,
    priority INT DEFAULT 0,
    status VARCHAR(20) NOT NULL DEFAULT 'active',
    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 IF NOT EXISTS idx_cid_data_mapping_tenant ON cid_data_mapping(tenant_id);
CREATE INDEX IF NOT EXISTS idx_cid_data_mapping_platform ON cid_data_mapping(platform_id);
CREATE INDEX IF NOT EXISTS idx_cid_data_mapping_interface ON cid_data_mapping(interface_id);
CREATE INDEX IF NOT EXISTS idx_cid_data_mapping_source_field ON cid_data_mapping(source_field);
CREATE INDEX IF NOT EXISTS idx_cid_data_mapping_status ON cid_data_mapping(status);
CREATE INDEX IF NOT EXISTS idx_cid_data_mapping_priority ON cid_data_mapping(priority);

-- 映射表注释
COMMENT ON TABLE cid_data_mapping IS '数据映射表';
COMMENT ON COLUMN cid_data_mapping.id IS '主键ID';
COMMENT ON COLUMN cid_data_mapping.tenant_id IS '租户ID';
COMMENT ON COLUMN cid_data_mapping.platform_id IS '平台ID';
COMMENT ON COLUMN cid_data_mapping.interface_id IS '接口ID';
COMMENT ON COLUMN cid_data_mapping.source_field IS '源字段（接口返回字段）';
COMMENT ON COLUMN cid_data_mapping.target_field IS '目标字段（本地表字段）';
COMMENT ON COLUMN cid_data_mapping.field_type IS '字段类型：string/int/float/bool/array/object';
COMMENT ON COLUMN cid_data_mapping.default_value IS '默认值';
COMMENT ON COLUMN cid_data_mapping.transform_rule IS '转换规则';
COMMENT ON COLUMN cid_data_mapping.priority IS '优先级（数字越小优先级越高）';
COMMENT ON COLUMN cid_data_mapping.status IS '状态：active启用/inactive停用';
COMMENT ON COLUMN cid_data_mapping.created_at IS '创建时间';
COMMENT ON COLUMN cid_data_mapping.updated_at IS '更新时间';
COMMENT ON COLUMN cid_data_mapping.deleted_at IS '软删除时间戳，0表示未删除';
