package com.kingdee.eas.custom.beisen.synchronouspos.osf;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.kingdee.bos.Context;
import com.kingdee.bos.rabbitmq.guava.Lists;
import com.kingdee.eas.custom.beisen.utils.BeiSenUtils;
import com.kingdee.eas.custom.beisen.utils.Helper;

import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * description: StaffManageRunnable <br>
 * date: 2025/8/12 9:24 <br>
 * author: lhbj <br>
 * version: 1.0 <br>
 */
public class StaffManageProcess{

    public Map<String, List<Map<String,Object>>> orgProcess(Context context, List<Map<String,Object>> orgListMap) throws IOException, URISyntaxException {
        Map<String,List<Map<String,Object>>> result = Maps.newHashMap();
        List<Map<String,Object>> list = Lists.newArrayList();
        List<Map<String,Object>> listMap = Lists.newArrayList();
        result.put("list",list);
        result.put("listMap",listMap);
        BeiSenUtils b = new BeiSenUtils(context);
        Helper helper = b.helper;
        String token = b.getAccessToken();
        for(Map pos : orgListMap) {
            // 若访问令牌不为空且开始时间和结束时间不为空
            if (!com.kingdee.util.StringUtils.isEmpty(token)) {
                // 创建请求头的 Map
                Map<String, String> header = new HashMap<String, String>();
                // 设置请求头的 Content-Type
                header.put("Content-Type", "application/json");
                // 设置请求头的 Authorization
                header.put("Authorization", "Bearer " + token);
                String url = "https://openapi.italent.cn/UserFrameworkApiV3/api/v1/departments/Put?originalId=" + URLEncoder.encode((String) pos.get("id"), "UTF-8");

                JSONObject requestBody = new JSONObject(pos);
                System.out.println("url：" + url);
                System.out.println("requestBody：" + requestBody);
                JSONObject responseJson = helper.getURL(url, header, requestBody, "PUT", requestBody.getString("posId"), "更新", "北森");
                System.out.println("responseJson：" + responseJson);
                listMap.add(responseJson);
                list.add(pos);
            }

        }
        return result;
    }

    public Map<String,List<Map<String,Object>>> posProcess(Context context,List<Map<String,Object>> posListMap) throws IOException, URISyntaxException {
        Map<String,List<Map<String,Object>>> result = Maps.newHashMap();
        List<Map<String,Object>> list = Lists.newArrayList();
        List<Map<String,Object>> listMap = Lists.newArrayList();
        result.put("list",list);
        result.put("listMap",listMap);
        BeiSenUtils b = new BeiSenUtils(context);
        Helper helper = b.helper;
        String token = b.getAccessToken();
        for(Map pos : posListMap) {
            // 若访问令牌不为空且开始时间和结束时间不为空
            if (!com.kingdee.util.StringUtils.isEmpty(token)) {
                // 创建请求头的 Map
                Map<String, String> header = new HashMap<String, String>();
                // 设置请求头的 Content-Type
                header.put("Content-Type", "application/json");
                // 设置请求头的 Authorization
                header.put("Authorization", "Bearer " + token);
                String url = "https://openapi.italent.cn/RecruitV6/api/v1/RecruitOnBoarding/UpdatePost";
                JSONObject requestBody = new JSONObject(pos);
                System.out.println("url："+url);
                System.out.println("requestBody："+requestBody);
                JSONObject responseJson = helper.getURL(url,header, requestBody, "PUT",requestBody.getString("posId"),"更新","北森");
                System.out.println("responseJson："+responseJson);
                listMap.add(responseJson);
                list.add(pos);
            }
        }
        return result;
    }
}
