package com.cm.common.dingding.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cm.common.base.AbstractService;
import com.cm.common.dingding.manager.app.DingDingAppManager;
import com.cm.common.dingding.service.IDingDingAppDepartmentService;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2DepartmentListsubRequest;
import com.dingtalk.api.response.OapiV2DepartmentListsubResponse;
import com.taobao.api.ApiException;
import org.springframework.stereotype.Service;

/**
 * @ClassName: DingDingAppDepartmentServiceImpl
 * @Description: 钉钉部门管理，需要开放钉钉应用：权限管理 -> 通讯录管理 -> 通讯录部门信息读权限
 * @Author: wanggeng
 * @Date: 2021/7/21 9:42 上午
 * @Version: 1.0
 */
@Service
public class DingDingAppDepartmentServiceImpl extends AbstractService implements IDingDingAppDepartmentService {
    @Override
    public JSONObject list() throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
        OapiV2DepartmentListsubRequest oapiV2DepartmentListsubRequest = new OapiV2DepartmentListsubRequest();
        oapiV2DepartmentListsubRequest.setDeptId(1L);
        oapiV2DepartmentListsubRequest.setLanguage("zh_CN");
        OapiV2DepartmentListsubResponse rsp = client.execute(oapiV2DepartmentListsubRequest, DingDingAppManager.getInstance().getAccessToken());
        System.out.println(rsp.getBody());
        return JSON.parseObject(rsp.getBody());
    }
}
