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.github.tingyugetc520.ali.dingtalk.bean.message.DtCorpConversationMessage; import com.github.tingyugetc520.ali.dingtalk.bean.message.DtMessage; import com.github.tingyugetc520.ali.dingtalk.error.DtErrorException; import com.google.common.collect.Lists; 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 getAllDepartment() throws Exception { List list = dtService.getDepartmentService().list(null, true); List 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 getAllDingUserDTO() throws Exception { List list = getAllDepartment(); List userList = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { List userInDepartment = getUserIdInDepartment(list.get(i).getId()); userList.addAll(userInDepartment); } return userList; } public List getAllUserDTO() throws Exception { List list = getAllDepartment(); List userList = new ArrayList<>(); for (int i = 0; i < list.size(); i++) { List 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() { })).getOrNull(); return UserDTO.fromDingUserDTO(dingUserDTO); } public List 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 res = new ArrayList<>(Try.of(() -> new ObjectMapper().convertValue( JsonPath.read(responseContent, "$.result.list"), new TypeReference>() { })).getOrElse(new ArrayList<>())); // 游标获取,hasMore判断是否存在下一页 Boolean hasMore = Try.of(() -> new ObjectMapper().convertValue( JsonPath.read(responseContent, "$.result.has_more"), new TypeReference() {})).getOrElse(Boolean.FALSE); while (hasMore) { jsonObject.addProperty("cursor" , new ObjectMapper().convertValue( JsonPath.read(responseContent, "$.result.next_cursor"), new TypeReference() {})); String nextResponseContent = dtService.post(url, jsonObject); res.addAll(Try.of(() -> new ObjectMapper().convertValue( JsonPath.read(nextResponseContent, "$.result.list"), new TypeReference>() {})).getOrElse(new ArrayList<>())); hasMore = Try.of(() -> new ObjectMapper().convertValue( JsonPath.read(nextResponseContent, "$.result.has_more"), new TypeReference() {})).getOrElse(Boolean.FALSE); } return res; } /** * 发送工作通知 */ public void sendWorkNotice(String userId,String message) throws DtErrorException { DtCorpConversationMessage corpConversationMessage = DtCorpConversationMessage.builder() .agentId(dtService.getDtConfigStorage().getAgentId()) .userIds(Lists.newArrayList(userId)) .msg(DtMessage.MARKDOWN() .content("AI考核-审批申诉结果") .text(String.format("### AI考核-申诉审批完成\n" + "\n" + "#### 任务信息\n" + "- 考试任务:%d\n" + "- 题目名称:%d\n" + "\n" + "#### 审批结果\n" + "- ✅ 审核通过\n" + "\n" + "---\n" + "- \uD83D\uDCC5 审批时间:%d\n" + "- \uD83D\uDC64 审批人:%d\n" + "- \uD83D\uDCAC 审批意见:%d","任务1","题目1","时间","人","意见")) .build()) .build(); // dtService.getCorpConversationMsgService().send(corpConversationMessage); } /** * 获取发送工作通知 */ public String getSendResult(String taskId){ JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("task_id" , taskId); jsonObject.addProperty("agent_id" , dtService.getDtConfigStorage().getAgentId()); String apiUrl = dtService.getDtConfigStorage().getApiUrl("/topapi/message/corpconversation/getsendresult"); String res = Try.of(() -> dtService.post(apiUrl, jsonObject)).getOrElse(""); return res; } }