package com.engine.custom.yunxuetang.util;

import com.alibaba.fastjson.JSONObject;
import com.engine.custom.yunxuetang.bean.DeptRequestBody;
import com.engine.custom.yunxuetang.bean.JobRequestBody;
import com.engine.custom.yunxuetang.bean.JobTypeRequestBody;
import com.engine.custom.yunxuetang.bean.ResponseData;
import okhttp3.OkHttpClient;

public  class SyncUtil {
    private static OkHttpClient client =new OkHttpClient();
    public static final String tokenUrl = "https://openapi-adel.yunxuetang.cn/token";
    public static final String appId = "9eqdtfi6t2f";
    public static final String appSecret = "OWZhZWM0MGVjZWFj";
    public static final String syncDeptsUrl = "https://openapi-adel.yunxuetang.cn/v1/udp/public/depts/sync";
    public static final String syncDelDeptsUrl = "https://openapi-adel.yunxuetang.cn/v1/udp/public/depts/${thirdId}/del";
    public static final String syncJobUrl = "https://openapi-adel.yunxuetang.cn/v1/udp/public/positions/sync";
    public static final String syncJobTypeUrl = "https://openapi-adel.yunxuetang.cn/v1/udp/public/positioncatalogs/sync";
    private  String cachedToken;
    private  long tokenExpiryTime;

    public ResponseData syncDept(DeptRequestBody deptRequestBody) {
        // String accessToken = HttpClientUtil.getAccessToken(tokenUrl, appId, appSecret);
        // System.out.println(accessToken);
        //同步部门
        // DeptRequestBody deptRequestBody = new DeptRequestBody();
        // deptRequestBody.setName("研发一部子部门1");
        // deptRequestBody.setThirdId("2");
        // deptRequestBody.setCode("");
        // deptRequestBody.setParentThirdId("1");
        String bodyStr = JSONObject.toJSONString(deptRequestBody);
        fetchToken();
        String responseData = HttpClientUtil.doPost(syncDeptsUrl, bodyStr, cachedToken);
        ResponseData jsonObject = JSONObject.parseObject(responseData,ResponseData.class);
        System.out.println(jsonObject);
        return jsonObject;

    }

    public  ResponseData syncDelDept( String id) {
        fetchToken();
        String responseData = HttpClientUtil.doPost(syncDelDeptsUrl.replace("${thirdId}",id), "", cachedToken);
        ResponseData jsonObject = JSONObject.parseObject(responseData,ResponseData.class);
        System.out.println(jsonObject);
        return jsonObject;
    }




    public void fetchToken() {
        // 如果缓存中的令牌为空或即将过期，重新获取
        if (cachedToken == null || System.currentTimeMillis() > tokenExpiryTime - (5 * 60 * 1000)) {
            cachedToken = HttpClientUtil.getAccessToken(tokenUrl, appId, appSecret);
            // 设置令牌的过期时间，这里是30分钟后
            tokenExpiryTime = System.currentTimeMillis() + (30 * 60 * 1000);
        }
    }

    public ResponseData syncJobType(JobTypeRequestBody job) {
        fetchToken();
        String bodyStr = JSONObject.toJSONString(job);
        fetchToken();
        System.out.println(bodyStr);
        String responseData = HttpClientUtil.doPost(syncJobTypeUrl, bodyStr, cachedToken);
        ResponseData jsonObject = JSONObject.parseObject(responseData,ResponseData.class);
        System.out.println(jsonObject);
        return jsonObject;
    }

    public ResponseData syncJobs(JobRequestBody job) {
        fetchToken();
        String bodyStr = JSONObject.toJSONString(job);
        fetchToken();
        System.out.println(bodyStr);
        String responseData = HttpClientUtil.doPost(syncJobUrl, bodyStr, cachedToken);
        ResponseData jsonObject = JSONObject.parseObject(responseData,ResponseData.class);
        System.out.println(jsonObject);
        return jsonObject;
    }
}
