From 12493116268785c43f534b3ab9bd45014b1229bb Mon Sep 17 00:00:00 2001
From: Junjie <DELL@qq.com>
Date: 星期五, 28 十一月 2025 16:08:29 +0800
Subject: [PATCH] #

---
 src/main/java/com/zy/core/task/DeviceLogScheduler.java |   41 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 41 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/zy/core/task/DeviceLogScheduler.java b/src/main/java/com/zy/core/task/DeviceLogScheduler.java
index a18a6bf..0f0d4f7 100644
--- a/src/main/java/com/zy/core/task/DeviceLogScheduler.java
+++ b/src/main/java/com/zy/core/task/DeviceLogScheduler.java
@@ -36,10 +36,23 @@
     private String storageType;
     @Value("${deviceLogStorage.loggingPath}")
     private String loggingPath;
+    @Value("${deviceLogStorage.expireDays}")
+    private Integer expireDays;
     @Autowired
     private DeviceDataLogService deviceDataLogService;
     @Autowired
     private RedisUtil redisUtil;
+
+    @Scheduled(cron = "0/3 * * * * ? ")
+    public void delDeviceLog() {
+        if ("mysql".equals(storageType)) {
+            deviceDataLogService.clearLog(expireDays == null ? 1 : expireDays);
+        }else if ("file".equals(storageType)) {
+            clearFileLog(expireDays == null ? 1 : expireDays);
+        }else {
+            log.error("鏈畾涔夌殑瀛樺偍绫诲瀷锛歿}", storageType);
+        }
+    }
 
     @Scheduled(cron = "0/3 * * * * ? ")
     public void execute() {
@@ -151,4 +164,32 @@
         return candidate;
     }
 
+    private void clearFileLog(int days) {
+        try {
+            Path baseDir = Paths.get(loggingPath);
+            if (!Files.exists(baseDir)) {
+                return;
+            }
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+            long cutoff = System.currentTimeMillis() - (long) days * 24 * 60 * 60 * 1000;
+            List<Path> dirs = Files.list(baseDir).filter(Files::isDirectory).collect(Collectors.toList());
+            for (Path dir : dirs) {
+                String name = dir.getFileName().toString();
+                if (name.length() == 8 && name.chars().allMatch(Character::isDigit)) {
+                    Date d = sdf.parse(name);
+                    if (d.getTime() < cutoff) {
+                        List<Path> all = Files.walk(dir).sorted(Comparator.reverseOrder()).collect(Collectors.toList());
+                        for (Path p : all) {
+                            try {
+                                Files.deleteIfExists(p);
+                            } catch (Exception ignored) {}
+                        }
+                    }
+                }
+            }
+        } catch (Exception e) {
+            log.error("璁惧鏃ュ織鏂囦欢娓呯悊澶辫触", e);
+        }
+    }
+
 }

--
Gitblit v1.9.1