-- =============================================
-- 钉钉智能薪酬平台初始化数据
-- 
-- 认证方式：app-id + signature 头部
-- 签名算法：MD5(request_body_string + app_secret).toUpperCase()
-- 
-- 执行后需在 auth_config 中配置 app_id 和 app_secret：
--   UPDATE api_datasource_platform SET auth_config = jsonb_set(
--     jsonb_set(auth_config, '{app_id}', '"你的app-id"'),
--     '{app_secret}', '"你的app-secret"'
--   ) WHERE platform_code = 'dingtalk_salary';
-- =============================================

-- 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_salary', '钉钉智能薪酬', '钉钉智能薪酬数据同步（app-id + signature 头部认证）', 'ACTIVE',
    'https://salary.eapps.dingtalkcloud.com', 'APP_SIGNATURE',
    '{
        "app_id": "",
        "app_secret": "",
        "sign_algorithm": "md5_upper_body",
        "app_id_header": "app-id",
        "sign_header": "signature"
    }'::jsonb,
    60, 3600, 3, 30000, 3, 1000
);

-- 2. 部门列表（无分页，单次请求返回全部部门）
-- 无请求入参，直接 POST 即可
-- 响应：{"code":"200","success":true,"data":[{"name":"研发部","value":"93639012","ext":"-1"},...]}
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_salary'),
    '公司部门列表', 'salary_dept_list',
    '/oapi/salary/dept/list', 'POST', 'active', 'inherit',
    '{}'::jsonb,
    '{
        "success_field": "code",
        "success_value": 200,
        "message_field": "msg",
        "list_path": "data"
    }'::jsonb,
    '{
        "table_name": "dingtalk_salary_dept",
        "columns": [
            {"name": "name", "type": "VARCHAR(300)", "comment": "部门名称"},
            {"name": "value", "type": "VARCHAR(100)", "comment": "部门ID"},
            {"name": "ext", "type": "VARCHAR(100)", "comment": "父部门ID，-1为根部门"}
        ],
        "conflict_keys": ["value"]
    }'::jsonb
);

-- 3. 按部门导出当月薪资报表（prefetch→salary_dept_list）
-- 先通过 dept/list 获取全量部门列表，再对每个部门调用此接口
-- deptId 通过 prefetch 注入，statisticsMonth 通过 row_inject 注入到每行
-- 使用前请将 statisticsMonth 更新为目标月份，如 "2026-06"
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_salary'),
    '按部门薪资报表', 'salary_statistics',
    '/oapi/salary/statistics/dept', 'POST', 'active', 'inherit',
    '{
        "statisticsMonth": "2026-06",
        "row_inject": ["statisticsMonth"],
        "prefetch": {
            "url": "/oapi/salary/dept/list",
            "method": "POST",
            "response_path": "data",
            "target_param": "deptId",
            "value_field": "value"
        }
    }'::jsonb,
    '{
        "success_field": "code",
        "success_value": 200,
        "message_field": "msg",
        "list_path": "data"
    }'::jsonb,
    '{
        "table_name": "dingtalk_salary_statistics",
        "columns": [
            {"name": "deptId", "type": "BIGINT", "comment": "部门ID"},
            {"name": "statisticsMonth", "type": "VARCHAR(7)", "comment": "薪资月(yyyy-MM)"},
            {"name": "name", "type": "VARCHAR(200)", "comment": "报表项名称"},
            {"name": "value", "type": "VARCHAR(100)", "comment": "报表项数据"}
        ],
        "conflict_keys": ["deptId", "statisticsMonth", "name"]
    }'::jsonb
);

-- 4. 人力成本报表（无分页，单次请求返回指定月份的人力成本汇总）
-- calBizId 为空时默认为当前月份，格式为 yyyyMM + "M"，如 "202606M"
-- 使用前可将 calBizId 更新为目标月份
-- 响应：data.sumStatisticsData 包含各成本项（itemId/itemName/itemDesc/sValue/value）
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_salary'),
    '人力成本报表', 'salary_statistic_report',
    '/oapi/salary/statistic/report/data', 'POST', 'active', 'inherit',
    '{
        "calBizId": "202606M"
    }'::jsonb,
    '{
        "success_field": "code",
        "success_value": 200,
        "message_field": "msg",
        "list_path": "data",
        "single_record": true
    }'::jsonb,
    '{
        "table_name": "dingtalk_salary_statistic_report",
        "columns": [
            {"name": "calBizId", "type": "VARCHAR(20)", "comment": "薪资月份"},
            {"name": "sumStatisticsData", "type": "JSONB", "comment": "人力成本报表数据"}
        ],
        "conflict_keys": ["calBizId"]
    }'::jsonb
);

-- 5. 按薪资组获取人力成本报表（无分页，单次请求返回指定薪资组的人力成本汇总）
-- salaryGroupName 为必填，使用前需更新为实际的薪资组名称
-- calBizId 为空时默认为当前月份，格式为 yyyyMM + "M"
-- row_inject 会将请求中的 salaryGroupName 注入到每行，便于区分不同薪资组的报表
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_salary'),
    '按薪资组人力成本报表', 'salary_statistic_report_group',
    '/oapi/salary/statistic/report/groupData', 'POST', 'active', 'inherit',
    '{
        "calBizId": "202606M",
        "salaryGroupName": "",
        "row_inject": ["salaryGroupName"]
    }'::jsonb,
    '{
        "success_field": "code",
        "success_value": 200,
        "message_field": "msg",
        "list_path": "data",
        "single_record": true
    }'::jsonb,
    '{
        "table_name": "dingtalk_salary_statistic_report_group",
        "columns": [
            {"name": "calBizId", "type": "VARCHAR(20)", "comment": "薪资月份"},
            {"name": "salaryGroupName", "type": "VARCHAR(200)", "comment": "薪资组名称"},
            {"name": "sumStatisticsData", "type": "JSONB", "comment": "人力成本报表数据"}0
        ],
        "conflict_keys": ["calBizId", "salaryGroupName"]
    }'::jsonb
);
