瀏覽代碼

对接消息中心

fuyuchuan 5 天之前
父節點
當前提交
8ab23f76cb

+ 11 - 0
flow-app/src/main/java/com/flow/FlowApplication.java

@@ -3,11 +3,22 @@ package com.flow;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.web.client.RestTemplateBuilder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.annotation.EnableScheduling;
+import org.springframework.web.client.RestTemplate;
 
 @MapperScan(basePackages = "com.flow.dao")
 @SpringBootApplication
+@EnableScheduling
+@EnableAsync
 public class FlowApplication {
     public static void main(String[] args) {
         SpringApplication.run(FlowApplication.class, args);
     }
+    @Bean
+    public RestTemplate restTemplate(RestTemplateBuilder builder) {
+        return builder.build();
+    }
 }

+ 12 - 0
flow-im/flow-im-entity/pom.xml

@@ -18,4 +18,16 @@
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
 
+    <dependencies>
+        <!--HTTP-->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+            <dependency>
+        <groupId>org.springframework.boot</groupId>
+        <artifactId>spring-boot-starter-webflux</artifactId>
+    </dependency>
+    </dependencies>
+
 </project>

+ 11 - 0
flow-im/flow-im-entity/src/main/java/com/flow/entity/Notify.java

@@ -35,4 +35,15 @@ public class Notify {
     private String url;
     @TableField(typeHandler = JacksonTypeHandler.class)
     private Map<String, Object> params;
+
+    public Notify(Long id, NotifyEnum type, LocalDateTime receivingTime, String subject, String content, String sender, String url, String receiver) {
+        this.id = id;
+        this.subject = subject;
+        this.content = content;
+        this.sender = sender;
+        this.receiver = receiver;
+        this.type = type;
+        this.receivingTime = receivingTime;
+        this.url = url;
+    }
 }

+ 111 - 0
flow-im/flow-im-entity/src/main/java/com/flow/job/SendUskyJob.java

@@ -0,0 +1,111 @@
+package com.flow.job;
+
+import com.flow.entity.Notify;
+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.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.Arrays;
+import java.util.List;
+
+/**
+ *
+ * @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 = "https://gateway.usky.cn/prod-api/system/mceReceive/addMce";
+    private static final String USKY_TEST_URL = "http://192.168.10.165:13200/offline-api/system/mceReceive/addMce";
+
+    @Autowired
+    private JdbcTemplate jdbcTemplate;
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    // 每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 (!notifies.isEmpty()) {
+            sendUsky(notifies);
+            updateNotify(notifies);
+        }
+
+        // 记录任务结束时间
+        LocalDateTime endTime = LocalDateTime.now();
+        log.info("执行消息同步定时任务,结束时间:" + endTime.format(FORMATTER));
+    }
+
+    // 查询未发送的消息
+    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()
+        );
+    }
+
+    // 发送消息
+    @Async
+    public void sendUsky(List<Notify> unSend) {
+        int successCount = 0;
+        int failCount = 0;
+        for (Notify send : unSend) {
+            JSONObject sendJson = new JSONObject()
+                    .put("infoTypeName", "message_type")
+                    .put("infoType", 3)
+                    .put("infoTitle", "OA审批")
+                    .put("infoContent", send.getContent() + "。 url:" + send.getUrl())
+                    .put("userName", send.getSender())
+                    .put("userNames", Arrays.asList(send.getReceiver()))
+                    .put("id", send.getId());
+
+            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++;
+            }
+        }
+        log.info("消息发送完成,成功数量:" + successCount + ",失败数量:" + failCount);
+    }
+
+    // 更新已发送消息同步状态
+    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());
+    }
+}

+ 38 - 0
flow-im/flow-im-entity/src/main/java/com/flow/mapstruct/NotifyRowMapper.java

@@ -0,0 +1,38 @@
+package com.flow.mapstruct;
+
+import com.flow.common.core.exception.BaseException;
+import com.flow.entity.Notify;
+import com.flow.enums.NotifyEnum;
+import org.springframework.jdbc.core.RowMapper;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.time.LocalDateTime;
+
+/**
+ *
+ * @author fyc
+ * @email yuchuan.fu@chinausky.com
+ * @date 2025/5/15
+ */
+public class NotifyRowMapper implements RowMapper<Notify> {
+    @Override
+    public Notify mapRow(ResultSet rs, int rowNum) throws SQLException {
+        Long id = rs.getLong("id");
+        String typeStr = rs.getString("type");
+        LocalDateTime receivingTime = rs.getTimestamp("receiving_time").toLocalDateTime();
+        String subject = rs.getString("subject");
+        String content = rs.getString("content");
+        String sender = rs.getString("sender");
+        String receiver = rs.getString("receiver");
+        String url = rs.getString("url");
+
+
+        NotifyEnum type = NotifyEnum.match(typeStr);
+        if (type == null) {
+            throw new BaseException("未知的type值:" + typeStr);
+        }
+
+        return new Notify(id, type, receivingTime, subject, content, sender, url, receiver);
+    }
+}