|
|
|
|
package com.project.ding.utils;
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
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.DepartmentDTO;
|
|
|
|
|
import com.project.ding.domain.dto.DingUserDTO;
|
|
|
|
|
import com.project.ding.domain.dto.UserDTO;
|
|
|
|
|
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;
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
public class DingUtil {
|
|
|
|
|
@Autowired
|
|
|
|
|
private DtService dtService;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public List<DepartmentDTO> getAllDepartment() throws Exception {
|
|
|
|
|
List<DtDepart> list = dtService.getDepartmentService().list(null, true);
|
|
|
|
|
List<DepartmentDTO> res = new ArrayList<>();
|
|
|
|
|
for (DtDepart dtDepart : list) {
|
|
|
|
|
DepartmentDTO departmentDTO = new DepartmentDTO();
|
|
|
|
|
BeanUtil.copyProperties(dtDepart , departmentDTO);
|
|
|
|
|
res.add(departmentDTO);
|
|
|
|
|
}
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String getUserIdByCode(String code) {
|
|
|
|
|
return Try.of(() -> dtService.getOauth2Service().getUserInfo(code).getUserId())
|
|
|
|
|
.getOrElse("");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<DingUserDTO> getAllDingUserDTO() throws Exception {
|
|
|
|
|
List<DepartmentDTO> 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<UserDTO> getAllUserDTO() throws Exception {
|
|
|
|
|
List<DepartmentDTO> list = getAllDepartment();
|
|
|
|
|
|
|
|
|
|
List<UserDTO> userList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
|
List<DingUserDTO> userInDepartment = getUserIdInDepartment(list.get(i).getId());
|
|
|
|
|
userList.addAll(userInDepartment.stream()
|
|
|
|
|
.map(UserDTO::fromDingUserDTO)
|
|
|
|
|
.collect(Collectors.toList()));
|
|
|
|
|
}
|
|
|
|
|
return userList.stream()
|
|
|
|
|
.collect(Collectors.toMap(
|
|
|
|
|
UserDTO::getId,
|
|
|
|
|
u -> u,
|
|
|
|
|
this::mergeUser
|
|
|
|
|
))
|
|
|
|
|
.values()
|
|
|
|
|
.stream()
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 定义合并规则:处理权限“或”逻辑
|
|
|
|
|
*/
|
|
|
|
|
private UserDTO mergeUser(UserDTO u1, UserDTO u2) {
|
|
|
|
|
// 权限合并:只要其中一个是 true,结果就是 true
|
|
|
|
|
u1.setLeader(Boolean.logicalOr(Boolean.TRUE.equals(u1.getLeader()), Boolean.TRUE.equals(u2.getLeader())));
|
|
|
|
|
return u1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public UserDTO getUserById(String id) {
|
|
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
|
|
jsonObject.addProperty("userid" , id);
|
|
|
|
|
String url = dtService.getDtConfigStorage().getApiUrl("/topapi/v2/user/get");
|
|
|
|
|
String responseContent = Try.of(() -> dtService.post(url, jsonObject)).getOrElse("");
|
|
|
|
|
DingUserDTO dingUserDTO = Try.of(() -> new ObjectMapper().convertValue(
|
|
|
|
|
JsonPath.read(responseContent, "$.result"),
|
|
|
|
|
new TypeReference<DingUserDTO>() {
|
|
|
|
|
})).getOrNull();
|
|
|
|
|
|
|
|
|
|
return UserDTO.fromDingUserDTO(dingUserDTO);
|
|
|
|
|
}
|
|
|
|
|
public List<DingUserDTO> getUserIdInDepartment(Long id) throws Exception {
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
List<DingUserDTO> res = new ArrayList<>(Try.of(() -> new ObjectMapper().convertValue(
|
|
|
|
|
JsonPath.read(responseContent, "$.result.list"),
|
|
|
|
|
new TypeReference<List<DingUserDTO>>() {
|
|
|
|
|
})).getOrElse(new ArrayList<>()));
|
|
|
|
|
// 游标获取,hasMore判断是否存在下一页
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|