package com.fjhx.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiServiceGetCorpTokenRequest;
import com.dingtalk.api.request.OapiV2DepartmentGetRequest;
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiServiceGetCorpTokenResponse;
import com.dingtalk.api.response.OapiV2DepartmentGetResponse;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.taobao.api.ApiException;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

public class DingUtil {

    private static final String accessKey = "suitepgu4shlatgllyiqp";
    private static final String accessSecret = "52-_2aKc3WGd8qVX5FRFmR6MnkeAOi6seUKYxjMWBCeiXZJ0l01_YaDfABIP-hmk";

//    public static void main(String[] args) throws ApiException {
//        OapiServiceGetCorpTokenResponse corpToken = getCorpToken("ding53d823e205604918a39a90f97fcb1e09",
//                "v8v4H910CdecMa53IHETdVtGAWgyKO01cXf2LVQT1BhsFcXjzTbOpmW6toLyTecS7jEFuOrAzFvyzNdlpREw6l");
//
//        if (corpToken.getErrcode().equals(0L)) {
//            String accessToken = corpToken.getAccessToken();
//
//
//            getDeptList(accessToken, 1L);
//
//        }
//    }

    /**
     * 获取token
     */
    public static OapiServiceGetCorpTokenResponse getCorpToken(String authCorpid, String suiteTicket) throws ApiException {

        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_corp_token");
        OapiServiceGetCorpTokenRequest req = new OapiServiceGetCorpTokenRequest();
        req.setAuthCorpid(authCorpid);

        return client.execute(req, accessKey, accessSecret, suiteTicket);
    }

    public static void getDeptList(String accessToken, Long deptId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
        OapiV2DepartmentListsubRequest req = new OapiV2DepartmentListsubRequest();
        req.setDeptId(deptId);
        OapiV2DepartmentListsubResponse rsp = client.execute(req, accessToken);

        List<OapiV2DepartmentListsubResponse.DeptBaseResponse> result = rsp.getResult();

        for (OapiV2DepartmentListsubResponse.DeptBaseResponse deptBaseResponse : result) {
            getDeptList(accessToken, deptBaseResponse.getDeptId());
            System.out.println("listsubData:" + JSONObject.toJSONString(deptBaseResponse, SerializerFeature.WriteMapNullValue));

            getDeptDetails(accessToken, deptBaseResponse.getDeptId());
        }

    }

    public static void getDeptDetails(String accessToken, Long deptId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/get");
        OapiV2DepartmentGetRequest req = new OapiV2DepartmentGetRequest();
        req.setDeptId(deptId);
        OapiV2DepartmentGetResponse rsp = client.execute(req, accessToken);
        System.err.println("departmentGetName:" + JSONObject.toJSONString(rsp.getResult(),SerializerFeature.WriteMapNullValue));
    }


    public static void getUser(String accessToken, String userId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
        OapiV2UserGetRequest req = new OapiV2UserGetRequest();
        req.setUserid(userId);
        OapiV2UserGetResponse rsp = client.execute(req, accessToken);
        System.out.println(rsp.getBody());
    }

}
