123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- package com.flow.job;
- import com.flow.entity.Notify;
- import com.flow.entity.User;
- import com.flow.enums.NotifyEnum;
- import com.flow.mapstruct.NotifyRowMapper;
- import dm.jdbc.filter.stat.json.JSONObject;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
- import org.springframework.scheduling.annotation.Async;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- import org.springframework.web.client.RestTemplate;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- /**
- *
- * @author fyc
- * @email yuchuan.fu@chinausky.com
- * @date 2025/5/14
- */
- @Component
- @Slf4j
- public class SendUskyJob {
- private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- private static final String USKY_URL = "http://172.16.66.143:9886/mceReceive/addMceNew";
- private static final String USKY_TEST_URL = "http://192.168.10.165:13200/offline-api/system/mceReceive/addMceNew";
- @Autowired
- private JdbcTemplate jdbcTemplate;
- @Autowired
- private RestTemplate restTemplate;
- @Autowired
- private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
- // 每30秒执行一次
- @Scheduled(fixedRate = 30000)
- public void executeTask() {
- // 记录任务开始时间
- LocalDateTime startTime = LocalDateTime.now();
- log.info("执行消息同步定时任务,开始时间:" + startTime.format(FORMATTER));
- // 一小时内的消息
- List<Notify> notifies = unSendUsky(startTime.minusHours(1), startTime);
- int notifySize = notifies.size();
- log.info("需要同步的消息数量为:" + notifySize);
- if (notifySize > 0) {
- sendUsky(notifies);
- }
- // 记录任务结束时间
- LocalDateTime endTime = LocalDateTime.now();
- log.info("执行消息同步定时任务,结束时间:" + endTime.format(FORMATTER));
- }
- // 发送消息
- @Async
- public void sendUsky(List<Notify> unSend) {
- int successCount = 0;
- int failCount = 0;
- // 构建消息发送请求体
- JSONObject sendJson = new JSONObject();
- for (Notify send : unSend) {
- String subject = send.getSubject().split("-")[0];
- String processName = send.getSubject().split("-")[1];
- String approvalResult = "";
- NotifyEnum sendType = send.getType();
- log.info("审批单:{}", processName);
- log.info("审批节点:{}", send.getContent());
- switch (subject) {
- case "申请已通过":
- approvalResult = "审批通过";
- break;
- case "新的审批任务":
- case "催办":
- approvalResult = "审批中";
- break;
- case "申请拒绝":
- approvalResult = "审批驳回";
- break;
- case "审批提醒":
- approvalResult = "审批超时";
- break;
- }
- List<String> userNames = unSend.stream().map(Notify::getSender).collect(Collectors.toList());
- log.info("查询到的未发送用户: {}", userNames);
- List<User> unSendUsers = unSendUser(userNames);
- log.info("查询到未发送的用户信息: {}", unSendUsers);
- Map<String, String> userNamesMap = unSendUsers.stream()
- .filter(user -> user.getUsername() != null && user.getName() != null)
- .collect(Collectors.toMap(
- User::getUsername, User::getName,
- (existingValue, newValue) -> existingValue
- ));
- log.info("查询到的用户映射: {}", userNamesMap);
- if (userNamesMap.containsKey(send.getSender())) {
- sendJson.put("infoTypeName", "OA审批");
- sendJson.put("infoType", 3);
- sendJson.put("infoTitle", "OA审批");
- sendJson.put("infoContent", send.getSubject());
- sendJson.put("userName", send.getSender());
- sendJson.put("userNames", Collections.singletonList(send.getReceiver()));
- sendJson.put("id", send.getId());
- sendJson.put("approvalResult", approvalResult);
- sendJson.put("processName", processName);
- sendJson.put("approvalNode", send.getContent());
- sendJson.put("realName", userNamesMap.get(send.getSender()));
- if ("system".equals(sendType.getType())) {
- sendJson.put("oaType", "me");
- } else {
- sendJson.put("oaType", "todo");
- }
- String sendJsonString = sendJson.toString();
- log.info("准备发送 {} 的消息:{} ", send.getSender(), sendJsonString);
- try {
- // restTemplate.postForObject(USKY_TEST_URL, sendJsonString, String.class);
- restTemplate.postForObject(USKY_URL, sendJsonString, String.class);
- log.info("消息发送成功,ID: " + send.getId());
- successCount++;
- } catch (Exception e) {
- log.error("消息发送失败,ID: " + send.getId(), e);
- failCount++;
- }
- } else {
- log.error("用户 {} 的真实姓名未找到", send.getSender());
- }
- }
- log.info("消息发送完成,成功数量:" + successCount + ",失败数量:" + failCount);
- // 更新消息发送状态
- updateNotify(unSend);
- }
- // 查询未发送的消息
- public List<Notify> unSendUsky(LocalDateTime startTime, LocalDateTime endTime) {
- String sql = "SELECT * FROM sys_notify WHERE is_send_usky = 0 AND receiving_time BETWEEN ? AND ?";
- return jdbcTemplate.query(
- sql,
- new Object[]{startTime, endTime},
- new NotifyRowMapper()
- );
- }
- // 查询未发送消息的用户名与真实姓名
- public List<User> unSendUser(List<String> userNames) {
- if (userNames == null || userNames.isEmpty()) {
- return Collections.emptyList();
- }
- // 构建IN子句的参数
- Map<String, Object> params = new HashMap<>();
- params.put("userNames", userNames);
- String sql = "SELECT username, name FROM sys_user WHERE username IN (:userNames)";
- return namedParameterJdbcTemplate.query(
- sql,
- params,
- (rs, rowNum) -> new User(
- rs.getString("username"),
- rs.getString("name")
- )
- );
- }
- // 更新已发送消息同步状态
- public void updateNotify(List<Notify> unSend) {
- for (Notify send : unSend) {
- String sql = "UPDATE sys_notify SET is_send_usky = 1 WHERE id = ?";
- jdbcTemplate.update(sql, send.getId());
- }
- log.info("更新消息状态完成,共更新 {} 条消息", unSend.size());
- }
- // 提取【】内的内容
- public static String extractContentInBrackets(String content) {
- // 定义正则表达式,匹配【】内的内容
- String regex = "【(.*?)】";
- Pattern pattern = Pattern.compile(regex);
- Matcher matcher = pattern.matcher(content);
- if (matcher.find()) {
- // 返回匹配到的第一个结果
- return matcher.group(1);
- } else {
- // 如果没有匹配到,返回空字符串或其他默认值
- return "";
- }
- }
- }
|