You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.9 KiB

package com.project.ding.utils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.tingyugetc520.ali.dingtalk.api.DtService;
import com.github.tingyugetc520.ali.dingtalk.bean.department.DtDepart;
import com.google.gson.JsonObject;
import com.jayway.jsonpath.JsonPath;
import com.project.ding.domain.dto.DingUserDTO;
import io.vavr.control.Try;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class DingUtil {
@Autowired
private DtService dtService;
public List<DtDepart> getAllDepartment() throws Exception {
return dtService.getDepartmentService().list(null , true);
}
public List<DingUserDTO> getAllUser() throws Exception {
List<DtDepart> list = getAllDepartment();
List<DingUserDTO> userList = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
List<DingUserDTO> userInDepartment = getUserIdInDepartment(list.get(i).getId());
userList.addAll(userInDepartment);
}
return userList;
}
public List<DingUserDTO> getUserIdInDepartment(Long id) throws Exception {
List<DingUserDTO> res = new ArrayList<>();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("dept_id" , id);
jsonObject.addProperty("cursor" , 0);
jsonObject.addProperty("size" , 100);
String url = dtService.getDtConfigStorage().getApiUrl("/topapi/v2/user/list");
String responseContent = dtService.post(url, jsonObject);
res.addAll(Try.of(() -> new ObjectMapper().convertValue(
JsonPath.read(responseContent, "$.result.list"),
new TypeReference<List<DingUserDTO>>() {})).getOrElse(new ArrayList<>()));
Boolean hasMore = Try.of(() -> new ObjectMapper().convertValue(
JsonPath.read(responseContent, "$.result.has_more"),
new TypeReference<Boolean>() {})).getOrElse(Boolean.FALSE);
while (hasMore) {
jsonObject.addProperty("cursor" , new ObjectMapper().convertValue(
JsonPath.read(responseContent, "$.result.next_cursor"),
new TypeReference<Long>() {}));
String nextResponseContent = dtService.post(url, jsonObject);
res.addAll(Try.of(() -> new ObjectMapper().convertValue(
JsonPath.read(nextResponseContent, "$.result.list"),
new TypeReference<List<DingUserDTO>>() {})).getOrElse(new ArrayList<>()));
hasMore = Try.of(() -> new ObjectMapper().convertValue(
JsonPath.read(nextResponseContent, "$.result.has_more"),
new TypeReference<Boolean>() {})).getOrElse(Boolean.FALSE);
}
return res;
}
}