From af7cbae92ab8cd4dba5ac4415294c224f41a3b3f Mon Sep 17 00:00:00 2001
From: DELL <DELL@qq.com>
Date: 星期五, 28 十一月 2025 15:46:32 +0800
Subject: [PATCH] #

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

diff --git a/src/main/java/com/zy/core/task/DeviceLogScheduler.java b/src/main/java/com/zy/core/task/DeviceLogScheduler.java
index fccce85..a18a6bf 100644
--- a/src/main/java/com/zy/core/task/DeviceLogScheduler.java
+++ b/src/main/java/com/zy/core/task/DeviceLogScheduler.java
@@ -1,23 +1,41 @@
 package com.zy.core.task;
 
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.serializer.SerializerFeature;
 import com.zy.asrs.entity.DeviceDataLog;
 import com.zy.asrs.service.DeviceDataLogService;
 import com.zy.common.utils.RedisUtil;
 import com.zy.core.enums.RedisKeyType;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.text.SimpleDateFormat;
+import java.util.Comparator;
+import java.util.Date;
+import java.util.HashMap;
 import java.util.Set;
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 @Slf4j
 @Component
 public class DeviceLogScheduler {
 
+    @Value("${deviceLogStorage.type}")
+    private String storageType;
+    @Value("${deviceLogStorage.loggingPath}")
+    private String loggingPath;
     @Autowired
     private DeviceDataLogService deviceDataLogService;
     @Autowired
@@ -38,10 +56,99 @@
             }
         }
         if (!list.isEmpty()) {
-            if (deviceDataLogService.saveBatch(list)) {
-                redisUtil.del(keys.toArray(new String[0]));
+            if ("mysql".equals(storageType)) {
+                mysqlSave(keys, list);
+            }else if ("file".equals(storageType)) {
+                fileSave(keys, list);
+            }else {
+                log.error("鏈畾涔夌殑瀛樺偍绫诲瀷锛歿}", storageType);
             }
         }
     }
 
+    private void mysqlSave(Set<String> keys, List<DeviceDataLog> list) {
+        if (deviceDataLogService.saveBatch(list)) {
+            redisUtil.del(keys.toArray(new String[0]));
+        }
+    }
+
+    private void fileSave(Set<String> keys, List<DeviceDataLog> list) {
+        try {
+            Path baseDir = Paths.get(loggingPath);
+            Files.createDirectories(baseDir);
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
+            Map<String, Map<String, List<DeviceDataLog>>> group = new HashMap<>();
+            for (DeviceDataLog logItem : list) {
+                String typeName = logItem.getType();
+                String datePart = sdf.format(logItem.getCreateTime() == null ? new Date() : logItem.getCreateTime());
+                String prefix = typeName + "_" + String.valueOf(logItem.getDeviceNo()) + "_" + datePart + "_";
+                group.computeIfAbsent(datePart, k -> new HashMap<>())
+                        .computeIfAbsent(prefix, k -> new ArrayList<>())
+                        .add(logItem);
+            }
+            for (Map.Entry<String, Map<String, List<DeviceDataLog>>> dateEntry : group.entrySet()) {
+                Path dayDir = baseDir.resolve(dateEntry.getKey());
+                Files.createDirectories(dayDir);
+                for (Map.Entry<String, List<DeviceDataLog>> entry : dateEntry.getValue().entrySet()) {
+                    String prefix = entry.getKey();
+                    List<DeviceDataLog> logs = entry.getValue();
+                    logs.sort(Comparator.comparing(DeviceDataLog::getCreateTime, Comparator.nullsLast(Date::compareTo)));
+                    int index = findStartIndex(dayDir, prefix);
+                    Path current = dayDir.resolve(prefix + index + ".log");
+                    if (!Files.exists(current)) {
+                        Files.createFile(current);
+                    }
+                    long size = Files.size(current);
+                    long max = 1024L * 1024L;
+                    for (DeviceDataLog d : logs) {
+                        String json = JSON.toJSONStringWithDateFormat(d, "yyyy-MM-dd HH:mm:ss.SSS", SerializerFeature.WriteDateUseDateFormat);
+                        byte[] line = (json + System.lineSeparator()).getBytes(StandardCharsets.UTF_8);
+                        if (size + line.length > max) {
+                            index++;
+                            current = dayDir.resolve(prefix + index + ".log");
+                            Files.createFile(current);
+                            size = 0;
+                        }
+                        Files.write(current, line, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
+                        size += line.length;
+                    }
+                }
+            }
+            redisUtil.del(keys.toArray(new String[0]));
+        } catch (Exception e) {
+            log.error("璁惧鏃ュ織鏂囦欢瀛樺偍澶辫触", e);
+        }
+    }
+
+    private int findStartIndex(Path baseDir, String prefix) throws Exception {
+        List<Path> matched = Files.list(baseDir)
+                .filter(p -> {
+                    String n = p.getFileName().toString();
+                    return n.startsWith(prefix) && n.endsWith(".log");
+                })
+                .collect(Collectors.toList());
+        int maxIdx = 0;
+        for (Path p : matched) {
+            String name = p.getFileName().toString();
+            String suf = name.substring(prefix.length());
+            if (!suf.isEmpty()) {
+                try {
+                    int val = Integer.parseInt(suf.replace(".log", ""));
+                    if (val > maxIdx) {
+                        maxIdx = val;
+                    }
+                } catch (NumberFormatException ignored) {}
+            }
+        }
+        int candidate = maxIdx == 0 ? 1 : maxIdx;
+        Path path = baseDir.resolve(prefix + candidate + ".log");
+        if (Files.exists(path)) {
+            long size = Files.size(path);
+            if (size >= 1024L * 1024L) {
+                return candidate + 1;
+            }
+        }
+        return candidate;
+    }
+
 }

--
Gitblit v1.9.1