-- =============================================
-- 钉钉智能人事平台初始化数据
-- 注意：此平台使用 x-acs-dingtalk-access-token Header 认证
-- API 基础地址为 https://api.dingtalk.com（与 oapi 不同）
-- 
-- 执行后需配置 token（OAUTH2 读取 token 列，非 api_key）：
--   UPDATE api_datasource_platform SET token = '你的hrm access_token'
--   WHERE platform_code = 'dingtalk_hrm';
-- =============================================

-- 1. 创建钉钉智能人事平台
INSERT INTO api_datasource_platform (
    tenant_id, creator, created_at, updater, updated_at,
    platform_code, platform_name, description, status,
    api_base_url, auth_type,
    auth_config,
    rate_limit_per_minute, rate_limit_per_hour,
    concurrency_limit, request_timeout_ms, max_retries, retry_delay_ms
) VALUES (
    1, 'admin', NOW(), 'admin', NOW(),
    'dingtalk_hrm', '钉钉智能人事', '钉钉智能人事数据同步（header 认证）', 'ACTIVE',
    'https://api.dingtalk.com', 'OAUTH2',
    '{
        "token_in_query": false,
        "header_name": "x-acs-dingtalk-access-token",
        "header_format": "{token}"
    }'::jsonb,
    60, 3600, 5, 30000, 3, 1000
);

-- 2. 企业职位列表（游标分页，hasMore 判结束）
-- 响应：list（职位数组），nextToken（游标），hasMore（是否还有更多）
-- 注意：此接口成功时无 errcode 字段，parseRespExt 在找不到 success_field 时自动跳过检查
INSERT INTO api_interface (
    tenant_id, creator, created_at, updater, updated_at,
    platform_id, name, code, url, method, status, auth_type,
    request_config, response_config, table_definition
) VALUES (
    1, 'admin', NOW(), 'admin', NOW(),
    (SELECT id FROM api_datasource_platform WHERE platform_code = 'dingtalk_hrm'),
    '企业职位列表', 'position_list',
    '/v1.0/hrm/positions/query', 'POST', 'active', 'inherit',
    '{
        "cursor_pagination": true,
        "page_param": "nextToken",
        "page_size_param": "maxResults",
        "pageSize": 200,
        "initial_cursor": 0,
        "parameters_location": "query"
    }'::jsonb,
    '{
        "list_path": "list",
        "cursor_field": "nextToken",
        "has_more_field": "hasMore"
    }'::jsonb,
    '{
        "table_name": "dingtalk_position",
        "columns": [
            {"name": "positionId", "type": "VARCHAR(100)", "comment": "职位ID"},
            {"name": "positionName", "type": "VARCHAR(300)", "comment": "职位名称"},
            {"name": "positionCategoryId", "type": "VARCHAR(100)", "comment": "职位类别ID"},
            {"name": "jobId", "type": "VARCHAR(100)", "comment": "所属职务ID"},
            {"name": "positionDes", "type": "TEXT", "comment": "职位描述"},
            {"name": "rankIdList", "type": "JSONB", "comment": "职级ID列表"},
            {"name": "status", "type": "INT", "comment": "职位状态 0启用 1停用"}
        ],
        "conflict_keys": ["positionId"]
    }'::jsonb
);
