Просмотр исходного кода

尝试修复微信消息发送失败

fuyuchuan 1 месяц назад
Родитель
Сommit
cc2e563d41

+ 49 - 25
flow-im/flow-im-entity/src/main/java/com/flow/job/SendUskyJob.java

@@ -3,11 +3,11 @@ package com.flow.job;
 import com.flow.entity.Notify;
 import com.flow.entity.User;
 import com.flow.mapstruct.NotifyRowMapper;
-import com.flow.mapstruct.UserRowMapper;
 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;
@@ -16,6 +16,7 @@ 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;
@@ -34,7 +35,6 @@ import java.util.stream.Collectors;
 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/addMceNew";
     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";
 
@@ -44,6 +44,9 @@ public class SendUskyJob {
     @Autowired
     private RestTemplate restTemplate;
 
+    @Autowired
+    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
+
     // 每30秒执行一次
     @Scheduled(fixedRate = 30000)
     public void executeTask() {
@@ -71,19 +74,40 @@ public class SendUskyJob {
     public void sendUsky(List<Notify> unSend) {
         int successCount = 0;
         int failCount = 0;
+
+        // 构建消息发送请求体
+        JSONObject sendJson = new JSONObject();
+
         for (Notify send : unSend) {
             String subject = send.getSubject();
             String approvalResult = "";
+            Object approvalNode = send.getParams().get("approvalNode");
             switch (subject) {
                 case "申请已通过":
                     approvalResult = "审批通过";
+                    if (approvalNode != null) {
+                        sendJson.put("approvalNode", approvalNode);
+                    }
                     break;
                 case "新的审批任务":
+                    approvalResult = "审批中";
+                    if (approvalNode != null) {
+                        sendJson.put("approvalNode", approvalNode);
+                    }
+                    break;
                 case "催办":
                     approvalResult = "审批中";
+                    sendJson.put("approvalNode", extractContentInBrackets(send.getContent()));
                     break;
                 case "申请拒绝":
                     approvalResult = "审批驳回";
+                    if (approvalNode != null) {
+                        sendJson.put("approvalNode", approvalNode);
+                    }
+                    break;
+                case "审批提醒":
+                    approvalResult = "审批超时";
+                    sendJson.put("approvalNode", extractContentInBrackets(send.getContent()));
                     break;
             }
 
@@ -97,18 +121,16 @@ public class SendUskyJob {
                 continue;
             }
 
-            JSONObject sendJson = new JSONObject()
-                    .put("infoTypeName", "OA审批")
-                    .put("infoType", 3)
-                    .put("infoTitle", "OA审批")
-                    .put("infoContent", userNamesMap.get(send.getSender()) + send.getSubject())
-                    .put("userName", send.getSender())
-                    .put("userNames", Collections.singletonList(send.getReceiver()))
-                    .put("id", send.getId())
-                    .put("approvalResult", approvalResult)
-                    .put("processName", send.getSubject())
-                    .put("approvalNode", extractContentInBrackets(send.getContent()))
-                    .put("realName", userNamesMap.get(send.getSender()));
+            sendJson.put("infoTypeName", "OA审批");
+            sendJson.put("infoType", 3);
+            sendJson.put("infoTitle", "OA审批");
+            sendJson.put("infoContent", userNamesMap.get(send.getSender()) + 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", send.getContent());
+            sendJson.put("realName", userNamesMap.get(send.getSender()));
 
             String sendJsonString = sendJson.toString();
             log.info("准备发送 {} 的消息:{} ", send.getSender(), sendJsonString);
@@ -138,21 +160,23 @@ public class SendUskyJob {
 
     // 查询未发送消息的用户名与真实姓名
     public List<User> unSendUser(List<String> userNames) {
+        if (userNames == null || userNames.isEmpty()) {
+            return Collections.emptyList();
+        }
 
-        System.out.println(userNames);
-
-        // 构建IN子句的参数占位符
-        String inClause = userNames.stream()
-                .map(name -> "?")
-                .collect(Collectors.joining(", "));
+        // 构建IN子句的参数
+        Map<String, Object> params = new HashMap<>();
+        params.put("userNames", userNames);
 
-        System.out.println(inClause);
+        String sql = "SELECT username, name FROM sys_user WHERE username IN (:userNames)";
 
-        String sql = "SELECT username, name FROM sys_user WHERE username IN (" + inClause + ")";
-        return jdbcTemplate.query(
+        return namedParameterJdbcTemplate.query(
                 sql,
-                userNames.toArray(new Object[0]),
-                new UserRowMapper()
+                params,
+                (rs, rowNum) -> new User(
+                        rs.getString("username"),
+                        rs.getString("name")
+                )
         );
     }
 

+ 0 - 23
flow-system/flow-system-entity/src/main/java/com/flow/mapstruct/UserRowMapper.java

@@ -1,23 +0,0 @@
-package com.flow.mapstruct;
-
-import com.flow.entity.User;
-import org.springframework.jdbc.core.RowMapper;
-
-import java.sql.ResultSet;
-import java.sql.SQLException;
-
-/**
- *
- * @author fyc
- * @email yuchuan.fu@chinausky.com
- * @date 2025/5/22
- */
-public class UserRowMapper implements RowMapper<User> {
-    @Override
-    public User mapRow(ResultSet rs, int rowNum) throws SQLException {
-        return new User(
-                rs.getString("username"),
-                rs.getString("name")
-        );
-    }
-}

+ 12 - 0
flow-workflow/flow-workflow-biz/src/main/java/com/flow/listener/GlobalActivityEventListener.java

@@ -37,6 +37,7 @@ import org.springframework.transaction.support.TransactionSynchronization;
 import org.springframework.transaction.support.TransactionSynchronizationManager;
 
 import java.time.LocalDateTime;
+import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
 
@@ -132,6 +133,8 @@ public class GlobalActivityEventListener extends AbstractFlowableEngineEventList
             @Override
             public void afterCommit() {
                 Notify notify = new Notify();
+                Map<String, Object> params = new HashMap<>();
+
                 notify.setSubject("新的审批任务");
                 //notify.setContent(String.format("您有一条待处理的审批任务【%s】,请及时处理。", entity.getName()));
                 String instanceId = entity.getProcessInstanceId();
@@ -145,6 +148,9 @@ public class GlobalActivityEventListener extends AbstractFlowableEngineEventList
                 notify.setReceiver(entity.getAssignee());
                 notify.setReceivingTime(LocalDateTime.now());
                 notify.setUrl("/flow/todo");
+
+                params.put("approvalNode", entity.getName());
+                notify.setParams(params);
                 notifyService.notify(notify);
             }
         });
@@ -182,6 +188,11 @@ public class GlobalActivityEventListener extends AbstractFlowableEngineEventList
         FlowInstance flowInstance = FlowInstance.buildStatus(entity.getProcessInstanceId(), ProcessStatus.COMPLETED);
         flowInstanceService.updateById(flowInstance);
         redisService.del(event.getProcessInstanceId());
+
+        // 利用空闲参数设置审批节点
+        Map<String, Object> params = new HashMap<>();
+        params.put("approvalNode", entity.getName());
+
         // 消息通知
         TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
             @Override
@@ -200,6 +211,7 @@ public class GlobalActivityEventListener extends AbstractFlowableEngineEventList
                 notify.setReceiver(entity.getStartUserId());
                 notify.setReceivingTime(LocalDateTime.now());
                 notify.setUrl("/flow/instance");
+                notify.setParams(params);
                 notifyService.notify(notify);
             }
         });

+ 5 - 0
flow-workflow/flow-workflow-biz/src/main/java/com/flow/service/impl/FlowInstanceServiceImpl.java

@@ -215,6 +215,11 @@ public class FlowInstanceServiceImpl extends BaseServiceImpl<FlowInstanceDao, Fl
         notify.setReceiver(processInstance.getStartUserId());
         notify.setReceivingTime(LocalDateTime.now());
         notify.setUrl("/flow/instance");
+
+        Map<String, Object> params = new HashMap<>();
+        params.put("approvalNode", processInstance.getName());
+        notify.setParams(params);
+
         notifyService.notify(notify);
     }