-- PostgreSQL 建表脚本 - dict 包

-- =============================================
-- 数据源平台配置表 (api_datasource_platform)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS api_datasource_platform_id_seq START WITH 1 INCREMENT BY 1;

CREATE TABLE IF NOT EXISTS api_datasource_platform (
    id BIGINT NOT NULL DEFAULT nextval('api_datasource_platform_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,
    platform_code VARCHAR(100) NOT NULL,
    platform_name VARCHAR(200) NOT NULL,
    description TEXT,
    status VARCHAR(50) DEFAULT 'ACTIVE',
    api_base_url VARCHAR(500),
    auth_type VARCHAR(50) DEFAULT 'API_KEY',
    token VARCHAR(500),
    api_key VARCHAR(500),
    client_id VARCHAR(200),
    client_secret VARCHAR(500),
    rate_limit_per_minute INT DEFAULT 60,
    rate_limit_per_hour INT DEFAULT 3600,
    concurrency_limit INT DEFAULT 10,
    request_timeout_ms INT DEFAULT 30000,
    max_retries INT DEFAULT 3,
    retry_delay_ms INT DEFAULT 1000,
    version INT DEFAULT 0,
    PRIMARY KEY (id)
);

COMMENT ON TABLE api_datasource_platform IS '数据源平台配置表';
COMMENT ON COLUMN api_datasource_platform.id IS '主键ID';
COMMENT ON COLUMN api_datasource_platform.tenant_id IS '租户ID';
COMMENT ON COLUMN api_datasource_platform.creator IS '创建人';
COMMENT ON COLUMN api_datasource_platform.created_at IS '创建时间';
COMMENT ON COLUMN api_datasource_platform.updater IS '更新人';
COMMENT ON COLUMN api_datasource_platform.updated_at IS '更新时间';
COMMENT ON COLUMN api_datasource_platform.deleted_at IS '软删除时间';
COMMENT ON COLUMN api_datasource_platform.platform_code IS '平台编码，唯一标识';
COMMENT ON COLUMN api_datasource_platform.platform_name IS '平台名称';
COMMENT ON COLUMN api_datasource_platform.description IS '平台描述';
COMMENT ON COLUMN api_datasource_platform.status IS '状态: ACTIVE启用, INACTIVE停用';
COMMENT ON COLUMN api_datasource_platform.api_base_url IS 'API基础地址';
COMMENT ON COLUMN api_datasource_platform.auth_type IS '认证类型: TOKEN/API_KEY/OAUTH2/BASIC';
COMMENT ON COLUMN api_datasource_platform.token IS '认证token/密钥';
COMMENT ON COLUMN api_datasource_platform.api_key IS 'API Key';
COMMENT ON COLUMN api_datasource_platform.client_id IS 'OAuth2 Client ID';
COMMENT ON COLUMN api_datasource_platform.client_secret IS 'OAuth2 Client Secret';
COMMENT ON COLUMN api_datasource_platform.rate_limit_per_minute IS '每分钟请求限制';
COMMENT ON COLUMN api_datasource_platform.rate_limit_per_hour IS '每小时请求限制';
COMMENT ON COLUMN api_datasource_platform.concurrency_limit IS '并发连接限制';
COMMENT ON COLUMN api_datasource_platform.request_timeout_ms IS '请求超时时间(毫秒)';
COMMENT ON COLUMN api_datasource_platform.max_retries IS '最大重试次数';
COMMENT ON COLUMN api_datasource_platform.retry_delay_ms IS '重试延迟(毫秒)';
COMMENT ON COLUMN api_datasource_platform.version IS '版本号(乐观锁)';

CREATE UNIQUE INDEX idx_api_datasource_platform_code ON api_datasource_platform(tenant_id, platform_code);

-- =============================================
-- 字段映射配置表 (api_field_mapping_config)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS api_field_mapping_config_id_seq START WITH 1 INCREMENT BY 1;

CREATE TABLE IF NOT EXISTS api_field_mapping_config (
    id BIGINT NOT NULL DEFAULT nextval('api_field_mapping_config_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,
    config_name VARCHAR(200) NOT NULL,
    vendor_name VARCHAR(100),
    api_name VARCHAR(200),
    api_version VARCHAR(50),
    source_field VARCHAR(200),
    source_field_type VARCHAR(100),
    source_field_desc VARCHAR(500),
    target_field VARCHAR(200),
    target_field_type VARCHAR(100),
    target_field_desc VARCHAR(500),
    transform_type VARCHAR(100),
    transform_params JSONB,
    validation_rules JSONB,
    default_value VARCHAR(500),
    is_required BOOLEAN DEFAULT FALSE,
    is_active BOOLEAN DEFAULT TRUE,
    priority INT DEFAULT 0,
    business_domain VARCHAR(100),
    field_group VARCHAR(100),
    config_version INT DEFAULT 1,
    effective_date TIMESTAMP WITH TIME ZONE,
    expiry_date TIMESTAMP WITH TIME ZONE,
    PRIMARY KEY (id)
);

COMMENT ON TABLE api_field_mapping_config IS '字段映射配置表';
COMMENT ON COLUMN api_field_mapping_config.id IS '主键ID';
COMMENT ON COLUMN api_field_mapping_config.tenant_id IS '租户ID';
COMMENT ON COLUMN api_field_mapping_config.creator IS '创建人';
COMMENT ON COLUMN api_field_mapping_config.created_at IS '创建时间';
COMMENT ON COLUMN api_field_mapping_config.updater IS '更新人';
COMMENT ON COLUMN api_field_mapping_config.updated_at IS '更新时间';
COMMENT ON COLUMN api_field_mapping_config.deleted_at IS '软删除时间';
COMMENT ON COLUMN api_field_mapping_config.config_name IS '配置名称';
COMMENT ON COLUMN api_field_mapping_config.vendor_name IS '厂商名称';
COMMENT ON COLUMN api_field_mapping_config.api_name IS '接口名称';
COMMENT ON COLUMN api_field_mapping_config.api_version IS '接口版本';
COMMENT ON COLUMN api_field_mapping_config.source_field IS '源字段名';
COMMENT ON COLUMN api_field_mapping_config.source_field_type IS '源字段数据类型';
COMMENT ON COLUMN api_field_mapping_config.source_field_desc IS '源字段描述';
COMMENT ON COLUMN api_field_mapping_config.target_field IS '目标字段名';
COMMENT ON COLUMN api_field_mapping_config.target_field_type IS '目标数据类型';
COMMENT ON COLUMN api_field_mapping_config.target_field_desc IS '字段描述';
COMMENT ON COLUMN api_field_mapping_config.transform_type IS '转换类型';
COMMENT ON COLUMN api_field_mapping_config.transform_params IS '转换参数';
COMMENT ON COLUMN api_field_mapping_config.validation_rules IS '验证规则';
COMMENT ON COLUMN api_field_mapping_config.default_value IS '默认值';
COMMENT ON COLUMN api_field_mapping_config.is_required IS '是否必填';
COMMENT ON COLUMN api_field_mapping_config.is_active IS '是否启用';
COMMENT ON COLUMN api_field_mapping_config.priority IS '优先级';
COMMENT ON COLUMN api_field_mapping_config.business_domain IS '业务域';
COMMENT ON COLUMN api_field_mapping_config.field_group IS '字段分组';
COMMENT ON COLUMN api_field_mapping_config.config_version IS '配置版本号';
COMMENT ON COLUMN api_field_mapping_config.effective_date IS '生效时间';
COMMENT ON COLUMN api_field_mapping_config.expiry_date IS '失效时间';

CREATE INDEX idx_api_field_mapping_config_vendor ON api_field_mapping_config(tenant_id, vendor_name, api_name);
CREATE INDEX idx_api_field_mapping_config_active ON api_field_mapping_config(tenant_id, is_active);

-- =============================================
-- API接口管理表 (api_interface)
-- =============================================
CREATE SEQUENCE IF NOT EXISTS api_interface_id_seq START WITH 1 INCREMENT BY 1;

CREATE TABLE IF NOT EXISTS api_interface (
    id BIGINT NOT NULL DEFAULT nextval('api_interface_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,
    platform_id BIGINT NOT NULL,
    name VARCHAR(200) NOT NULL,
    code VARCHAR(100) NOT NULL,
    url VARCHAR(500) NOT NULL,
    method VARCHAR(20) NOT NULL DEFAULT 'GET',
    status VARCHAR(50) DEFAULT 'active',
    auth_type VARCHAR(50) DEFAULT 'api_key',
    request_config JSONB,
    response_config JSONB,
    limit_config JSONB,
    PRIMARY KEY (id)
);

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

CREATE UNIQUE INDEX idx_api_interface_code ON api_interface(tenant_id, code);
CREATE INDEX idx_api_interface_platform_id ON api_interface(tenant_id, platform_id);
CREATE INDEX idx_api_interface_status ON api_interface(tenant_id, status);
