From d65679fefe52fc2bf2435825c65aec3a329e3b9e Mon Sep 17 00:00:00 2001
From: Junjie <fallin.jie@qq.com>
Date: 星期二, 22 四月 2025 14:40:21 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/asrs/task/NotifyScheduler.java |  149 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 149 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/zy/asrs/task/NotifyScheduler.java b/src/main/java/com/zy/asrs/task/NotifyScheduler.java
new file mode 100644
index 0000000..3dfc3a2
--- /dev/null
+++ b/src/main/java/com/zy/asrs/task/NotifyScheduler.java
@@ -0,0 +1,149 @@
+package com.zy.asrs.task;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.baomidou.mybatisplus.mapper.EntityWrapper;
+import com.zy.asrs.domain.dto.NotifyCustomDataDto;
+import com.zy.asrs.domain.dto.NotifyDto;
+import com.zy.asrs.entity.ApiLog;
+import com.zy.asrs.service.ApiLogService;
+import com.zy.asrs.utils.NotifyUtils;
+import com.zy.common.utils.HttpHandler;
+import com.zy.common.utils.RedisUtil;
+import com.zy.core.properties.SlaveProperties;
+import com.zy.system.entity.Config;
+import com.zy.system.service.ConfigService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+import java.util.List;
+
+@Component
+@Slf4j
+public class NotifyScheduler {
+
+    @Autowired
+    private RedisUtil redisUtil;
+    @Autowired
+    private NotifyUtils notifyUtils;
+    @Autowired
+    private SlaveProperties slaveProperties;
+    @Autowired
+    private ConfigService configService;
+    @Autowired
+    private ApiLogService apiLogService;
+
+    @Scheduled(cron = "0/3 * * * * ? ")
+    public synchronized void notifyTask(){
+        notifyMsg("task", 1);
+    }
+
+    private synchronized void notifyMsg(String notifyType, Integer device) {
+        Config notifyEnableConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyEnable"));
+        if(notifyEnableConfig == null){
+            return;
+        }
+        String notifyEnable = notifyEnableConfig.getValue();
+        if (!notifyEnable.equals("Y")) {
+            return;
+        }
+
+        Config notifyUriConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyUri"));
+        if(notifyUriConfig == null){
+            return;
+        }
+        String notifyUri = notifyUriConfig.getValue();
+
+        Config notifyUriPathConfig = configService.selectOne(new EntityWrapper<Config>().eq("code", "notifyUriPath"));
+        if(notifyUriPathConfig == null){
+            return;
+        }
+        String notifyUriPath = notifyUriPathConfig.getValue();
+
+        List<String> keys = notifyUtils.takeKeys(notifyType, device);
+        if(keys == null){
+            return;
+        }
+
+        if (keys.isEmpty()) {
+            return;
+        }
+
+        for (String key : keys) {
+            Object object = redisUtil.get(key);
+            if (object == null) {
+                continue;
+            }
+            NotifyDto notifyDto = (NotifyDto) object;
+
+            if (System.currentTimeMillis() - notifyDto.getLastRetryTime() < 1000 * notifyDto.getRetryTime()) {
+                continue;
+            }
+
+            ApiLog apiLog = new ApiLog();
+
+            try {
+                //瑙﹀彂閫氱煡
+                String response = null;
+                if (notifyDto.getSendCustomData()) {
+                    //鑷畾涔夋秷鎭牸寮�
+                    NotifyCustomDataDto customData = notifyDto.getCustomData();
+                    response = new HttpHandler.Builder()
+                            .setUri(customData.getUri())
+                            .setPath(customData.getPath())
+                            .setJson(customData.getData())
+                            .build()
+                            .doPost();
+
+                    apiLog.setUrl(customData.getUri() + customData.getPath());
+                    apiLog.setRequest(customData.getData());
+                    apiLog.setCreateTime(new Date());
+                }else {
+                    response = new HttpHandler.Builder()
+                            .setUri(notifyUri)
+                            .setPath(notifyUriPath)
+                            .setJson(JSON.toJSONString(notifyDto))
+                            .build()
+                            .doPost();
+
+                    apiLog.setUrl(notifyUri + notifyUriPath);
+                    apiLog.setRequest(JSON.toJSONString(notifyDto));
+                    apiLog.setCreateTime(new Date());
+                }
+
+                apiLog.setResponse(response);
+
+                JSONObject jsonObject = JSON.parseObject(response);
+                Integer code = jsonObject.getInteger("code");
+                if(code == 200){
+                    //閫氱煡鎴愬姛
+                    redisUtil.del(key);
+
+                    continue;
+                }
+            }catch (Exception e){
+                e.printStackTrace();
+            }finally {
+                //淇濆瓨璁板綍
+                apiLogService.insert(apiLog);
+            }
+
+            //閫氱煡澶辫触
+            int times = notifyDto.getRetryTimes() + 1;
+            if (times >= notifyDto.getFailTimes()) {
+                //瓒呰繃娆℃暟
+                redisUtil.del(key);
+                continue;
+            }
+
+            notifyDto.setLastRetryTime(System.currentTimeMillis());
+            notifyDto.setRetryTimes(times);
+            redisUtil.set(key, notifyDto);
+            continue;
+        }
+    }
+
+}

--
Gitblit v1.9.1