SendUskyJob.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package com.flow.job;
  2. import com.flow.entity.Notify;
  3. import com.flow.entity.User;
  4. import com.flow.enums.NotifyEnum;
  5. import com.flow.mapstruct.NotifyRowMapper;
  6. import dm.jdbc.filter.stat.json.JSONObject;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.jdbc.core.JdbcTemplate;
  10. import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
  11. import org.springframework.scheduling.annotation.Async;
  12. import org.springframework.scheduling.annotation.Scheduled;
  13. import org.springframework.stereotype.Component;
  14. import org.springframework.web.client.RestTemplate;
  15. import java.time.LocalDateTime;
  16. import java.time.format.DateTimeFormatter;
  17. import java.util.Collections;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23. import java.util.stream.Collectors;
  24. /**
  25. *
  26. * @author fyc
  27. * @email yuchuan.fu@chinausky.com
  28. * @date 2025/5/14
  29. */
  30. @Component
  31. @Slf4j
  32. public class SendUskyJob {
  33. private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  34. private static final String USKY_URL = "http://172.16.66.143:9886/mceReceive/addMceNew";
  35. private static final String USKY_TEST_URL = "http://192.168.10.165:13200/offline-api/system/mceReceive/addMceNew";
  36. @Autowired
  37. private JdbcTemplate jdbcTemplate;
  38. @Autowired
  39. private RestTemplate restTemplate;
  40. @Autowired
  41. private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
  42. // 每30秒执行一次
  43. @Scheduled(fixedRate = 30000)
  44. public void executeTask() {
  45. // 记录任务开始时间
  46. LocalDateTime startTime = LocalDateTime.now();
  47. log.info("执行消息同步定时任务,开始时间:" + startTime.format(FORMATTER));
  48. // 一小时内的消息
  49. List<Notify> notifies = unSendUsky(startTime.minusHours(1), startTime);
  50. int notifySize = notifies.size();
  51. log.info("需要同步的消息数量为:" + notifySize);
  52. if (notifySize > 0) {
  53. sendUsky(notifies);
  54. }
  55. // 记录任务结束时间
  56. LocalDateTime endTime = LocalDateTime.now();
  57. log.info("执行消息同步定时任务,结束时间:" + endTime.format(FORMATTER));
  58. }
  59. // 发送消息
  60. @Async
  61. public void sendUsky(List<Notify> unSend) {
  62. int successCount = 0;
  63. int failCount = 0;
  64. // 构建消息发送请求体
  65. JSONObject sendJson = new JSONObject();
  66. for (Notify send : unSend) {
  67. String subject = send.getSubject().split("-")[0];
  68. String processName = send.getSubject().split("-")[1];
  69. String approvalResult = "";
  70. NotifyEnum sendType = send.getType();
  71. log.info("审批单:{}", processName);
  72. log.info("审批节点:{}", send.getContent());
  73. switch (subject) {
  74. case "申请已通过":
  75. approvalResult = "审批通过";
  76. break;
  77. case "新的审批任务":
  78. case "催办":
  79. approvalResult = "审批中";
  80. break;
  81. case "申请拒绝":
  82. approvalResult = "审批驳回";
  83. break;
  84. case "审批提醒":
  85. approvalResult = "审批超时";
  86. break;
  87. }
  88. List<String> userNames = unSend.stream().map(Notify::getSender).collect(Collectors.toList());
  89. log.info("查询到的未发送用户: {}", userNames);
  90. List<User> unSendUsers = unSendUser(userNames);
  91. log.info("查询到未发送的用户信息: {}", unSendUsers);
  92. Map<String, String> userNamesMap = unSendUsers.stream()
  93. .filter(user -> user.getUsername() != null && user.getName() != null)
  94. .collect(Collectors.toMap(
  95. User::getUsername, User::getName,
  96. (existingValue, newValue) -> existingValue
  97. ));
  98. log.info("查询到的用户映射: {}", userNamesMap);
  99. if (userNamesMap.containsKey(send.getSender())) {
  100. sendJson.put("infoTypeName", "OA审批");
  101. sendJson.put("infoType", 3);
  102. sendJson.put("infoTitle", "OA审批");
  103. sendJson.put("infoContent", send.getSubject());
  104. sendJson.put("userName", send.getSender());
  105. sendJson.put("userNames", Collections.singletonList(send.getReceiver()));
  106. sendJson.put("id", send.getId());
  107. sendJson.put("approvalResult", approvalResult);
  108. sendJson.put("processName", processName);
  109. sendJson.put("approvalNode", send.getContent());
  110. sendJson.put("realName", userNamesMap.get(send.getSender()));
  111. if ("system".equals(sendType.getType())) {
  112. sendJson.put("oaType", "me");
  113. } else {
  114. sendJson.put("oaType", "todo");
  115. }
  116. String sendJsonString = sendJson.toString();
  117. log.info("准备发送 {} 的消息:{} ", send.getSender(), sendJsonString);
  118. try {
  119. // restTemplate.postForObject(USKY_TEST_URL, sendJsonString, String.class);
  120. restTemplate.postForObject(USKY_URL, sendJsonString, String.class);
  121. log.info("消息发送成功,ID: " + send.getId());
  122. successCount++;
  123. } catch (Exception e) {
  124. log.error("消息发送失败,ID: " + send.getId(), e);
  125. failCount++;
  126. }
  127. } else {
  128. log.error("用户 {} 的真实姓名未找到", send.getSender());
  129. }
  130. }
  131. log.info("消息发送完成,成功数量:" + successCount + ",失败数量:" + failCount);
  132. // 更新消息发送状态
  133. updateNotify(unSend);
  134. }
  135. // 查询未发送的消息
  136. public List<Notify> unSendUsky(LocalDateTime startTime, LocalDateTime endTime) {
  137. String sql = "SELECT * FROM sys_notify WHERE is_send_usky = 0 AND receiving_time BETWEEN ? AND ?";
  138. return jdbcTemplate.query(
  139. sql,
  140. new Object[]{startTime, endTime},
  141. new NotifyRowMapper()
  142. );
  143. }
  144. // 查询未发送消息的用户名与真实姓名
  145. public List<User> unSendUser(List<String> userNames) {
  146. if (userNames == null || userNames.isEmpty()) {
  147. return Collections.emptyList();
  148. }
  149. // 构建IN子句的参数
  150. Map<String, Object> params = new HashMap<>();
  151. params.put("userNames", userNames);
  152. String sql = "SELECT username, name FROM sys_user WHERE username IN (:userNames)";
  153. return namedParameterJdbcTemplate.query(
  154. sql,
  155. params,
  156. (rs, rowNum) -> new User(
  157. rs.getString("username"),
  158. rs.getString("name")
  159. )
  160. );
  161. }
  162. // 更新已发送消息同步状态
  163. public void updateNotify(List<Notify> unSend) {
  164. for (Notify send : unSend) {
  165. String sql = "UPDATE sys_notify SET is_send_usky = 1 WHERE id = ?";
  166. jdbcTemplate.update(sql, send.getId());
  167. }
  168. log.info("更新消息状态完成,共更新 {} 条消息", unSend.size());
  169. }
  170. // 提取【】内的内容
  171. public static String extractContentInBrackets(String content) {
  172. // 定义正则表达式,匹配【】内的内容
  173. String regex = "【(.*?)】";
  174. Pattern pattern = Pattern.compile(regex);
  175. Matcher matcher = pattern.matcher(content);
  176. if (matcher.find()) {
  177. // 返回匹配到的第一个结果
  178. return matcher.group(1);
  179. } else {
  180. // 如果没有匹配到,返回空字符串或其他默认值
  181. return "";
  182. }
  183. }
  184. }