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.

200 lines
8.0 KiB

1 month ago
package com.project.ding.utils;
import cn.hutool.core.bean.BeanUtil;
1 month ago
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;
1 month ago
import com.google.gson.JsonObject;
import com.jayway.jsonpath.JsonPath;
import com.project.appeal.domain.dto.AppealDTO;
import com.project.ding.domain.dto.DepartmentDTO;
1 month ago
import com.project.ding.domain.dto.DingUserDTO;
import com.project.ding.domain.dto.UserDTO;
1 month ago
import io.vavr.control.Try;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
1 month ago
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
1 month ago
import java.util.ArrayList;
import java.util.Date;
1 month ago
import java.util.List;
import java.util.stream.Collectors;
1 month ago
@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;
1 month ago
}
1 month ago
public String getUserIdByCode(String code) {
return Try.of(() -> dtService.getOauth2Service().getUserInfo(code).getUserId())
.getOrElse("");
}
public List<DingUserDTO> getAllDingUserDTO() throws Exception {
List<DepartmentDTO> list = getAllDepartment();
1 month ago
List<DingUserDTO> userList = new ArrayList<>();
for (DepartmentDTO departmentDTO : list) {
List<DingUserDTO> userInDepartment = getUserIdInDepartment(departmentDTO.getId());
1 month ago
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)
.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;
}
1 month ago
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);
}
1 month ago
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(
1 month ago
JsonPath.read(responseContent, "$.result.list"),
new TypeReference<List<DingUserDTO>>() {
})).getOrElse(new ArrayList<>()));
// 游标获取,hasMore判断是否存在下一页
1 month ago
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;
}
/**
* 发送工作通知
*/
@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);
}
1 month ago
/**
* 获取发送工作通知
*/
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;
}
1 month ago
}