SendUskyJob.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package com.flow.job;
  2. import com.flow.entity.Notify;
  3. import com.flow.mapstruct.NotifyRowMapper;
  4. import dm.jdbc.filter.stat.json.JSONObject;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.jdbc.core.JdbcTemplate;
  8. import org.springframework.scheduling.annotation.Async;
  9. import org.springframework.scheduling.annotation.Scheduled;
  10. import org.springframework.stereotype.Component;
  11. import org.springframework.web.client.RestTemplate;
  12. import java.time.LocalDateTime;
  13. import java.time.format.DateTimeFormatter;
  14. import java.util.Arrays;
  15. import java.util.List;
  16. /**
  17. *
  18. * @author fyc
  19. * @email yuchuan.fu@chinausky.com
  20. * @date 2025/5/14
  21. */
  22. @Component
  23. @Slf4j
  24. public class SendUskyJob {
  25. private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  26. private static final String USKY_URL = "https://gateway.usky.cn/prod-api/system/mceReceive/addMce";
  27. private static final String USKY_TEST_URL = "http://192.168.10.165:13200/offline-api/system/mceReceive/addMce";
  28. @Autowired
  29. private JdbcTemplate jdbcTemplate;
  30. @Autowired
  31. private RestTemplate restTemplate;
  32. // 每30秒执行一次
  33. @Scheduled(fixedRate = 30000)
  34. public void executeTask() {
  35. // 记录任务开始时间
  36. LocalDateTime startTime = LocalDateTime.now();
  37. log.info("执行消息同步定时任务,开始时间:" + startTime.format(FORMATTER));
  38. // 一小时内的消息
  39. List<Notify> notifies = unSendUsky(startTime.minusHours(1), startTime);
  40. int notifySize = notifies.size();
  41. log.info("需要同步的消息数量为:" + notifySize);
  42. if (!notifies.isEmpty()) {
  43. sendUsky(notifies);
  44. updateNotify(notifies);
  45. }
  46. // 记录任务结束时间
  47. LocalDateTime endTime = LocalDateTime.now();
  48. log.info("执行消息同步定时任务,结束时间:" + endTime.format(FORMATTER));
  49. }
  50. // 查询未发送的消息
  51. public List<Notify> unSendUsky(LocalDateTime startTime, LocalDateTime endTime) {
  52. String sql = "SELECT * FROM sys_notify WHERE is_send_usky = 0 AND receiving_time BETWEEN ? AND ?";
  53. return jdbcTemplate.query(
  54. sql,
  55. new Object[]{startTime, endTime},
  56. new NotifyRowMapper()
  57. );
  58. }
  59. // 发送消息
  60. @Async
  61. public void sendUsky(List<Notify> unSend) {
  62. int successCount = 0;
  63. int failCount = 0;
  64. for (Notify send : unSend) {
  65. JSONObject sendJson = new JSONObject()
  66. .put("infoTypeName", "message_type")
  67. .put("infoType", 3)
  68. .put("infoTitle", "OA审批")
  69. .put("infoContent", send.getContent() + "。 url:" + send.getUrl())
  70. .put("userName", send.getSender())
  71. .put("userNames", Arrays.asList(send.getReceiver()))
  72. .put("id", send.getId());
  73. String sendJsonString = sendJson.toString();
  74. log.info("准备发送 {} 的消息:{} ", send.getSender(), sendJsonString);
  75. try {
  76. // restTemplate.postForObject(USKY_TEST_URL, sendJsonString, String.class);
  77. restTemplate.postForObject(USKY_URL, sendJsonString, String.class);
  78. log.info("消息发送成功,ID: " + send.getId());
  79. successCount++;
  80. } catch (Exception e) {
  81. log.error("消息发送失败,ID: " + send.getId(), e);
  82. failCount++;
  83. }
  84. }
  85. log.info("消息发送完成,成功数量:" + successCount + ",失败数量:" + failCount);
  86. }
  87. // 更新已发送消息同步状态
  88. public void updateNotify(List<Notify> unSend) {
  89. for (Notify send : unSend) {
  90. String sql = "UPDATE sys_notify SET is_send_usky = 1 WHERE id = ?";
  91. jdbcTemplate.update(sql, send.getId());
  92. }
  93. log.info("更新消息状态完成,共更新 {} 条消息", unSend.size());
  94. }
  95. }