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.appeal.domain.dto.AppealDTO; 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.scheduling.annotation.Async; import org.springframework.stereotype.Component; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Date; 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 (DepartmentDTO departmentDTO : list) { List userInDepartment = getUserIdInDepartment(departmentDTO.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) .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; } /** * 发送工作通知 */ @Async("asycExecutor") public void sendWorkNotice(AppealDTO appealDTO) throws DtErrorException { DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String nowTime = LocalDateTime.now().format(formatter); boolean approved = appealDTO.getStatus() != null && appealDTO.getStatus() == 2; UserDTO userDTO = getUserById(appealDTO.getAppealUserId()); String resultLine = approved ? "- ✅ **审核通过**" : "- ❌ **审核不通过**"; String markdownText = String.format( "### AI考核-申诉审批完成\n\n" + "#### 任务信息\n" + "- 题目名称:%s\n" + "- 申诉理由:%s\n\n" + "#### 审批结果\n" + "%s\n\n" + "---\n" + "- 📅 审批时间:%s\n" + "- 👤 审批人:%s\n" + "- 💬 审批意见:%s", appealDTO.getQuestionContent(), appealDTO.getRemark(), resultLine, nowTime, userDTO.getName(), appealDTO.getReason() ); //发送工作通知 DtCorpConversationMessage corpConversationMessage = DtCorpConversationMessage.builder() .agentId(dtService.getDtConfigStorage().getAgentId()) .userIds(Lists.newArrayList(appealDTO.getUserId())) .msg(DtMessage.MARKDOWN() .content("AI考核-审批申诉结果") .text(markdownText) .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; } }