From 720e0926fa1c94b952c26e111206c5d6e1ed5ba2 Mon Sep 17 00:00:00 2001
From: lsh <lsh@163.com>
Date: 星期二, 21 四月 2026 15:59:49 +0800
Subject: [PATCH] Merge branch 'master' of http://47.97.1.152:5880/r/zy-wcs-master

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

diff --git a/src/main/java/com/zy/core/task/DeviceLogScheduler.java b/src/main/java/com/zy/core/task/DeviceLogScheduler.java
index 6c36495..c448830 100644
--- a/src/main/java/com/zy/core/task/DeviceLogScheduler.java
+++ b/src/main/java/com/zy/core/task/DeviceLogScheduler.java
@@ -7,6 +7,7 @@
 import com.zy.asrs.service.DeviceDataLogService;
 import com.zy.common.utils.RedisUtil;
 import com.zy.core.enums.RedisKeyType;
+import com.zy.core.enums.SlaveType;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -18,21 +19,21 @@
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
-import java.nio.file.StandardOpenOption;
 import java.nio.file.SimpleFileVisitor;
+import java.nio.file.StandardOpenOption;
 import java.nio.file.attribute.BasicFileAttributes;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.LinkedHashSet;
-import java.util.Set;
 import java.util.List;
-import java.util.ArrayList;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.locks.ReentrantLock;
-import java.util.stream.Stream;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 @Slf4j
 @Component
@@ -59,7 +60,7 @@
     public void delDeviceLog() {
         if ("mysql".equals(storageType)) {
             deviceDataLogService.clearLog(expireDays == null ? 1 : expireDays);
-        }else if ("file".equals(storageType)) {
+        } else if ("file".equals(storageType)) {
             if (!FILE_OP_LOCK.tryLock()) {
                 return;
             }
@@ -68,7 +69,7 @@
             } finally {
                 FILE_OP_LOCK.unlock();
             }
-        }else {
+        } else {
             log.error("鏈畾涔夌殑瀛樺偍绫诲瀷锛歿}", storageType);
         }
     }
@@ -90,7 +91,7 @@
         if (!list.isEmpty()) {
             if ("mysql".equals(storageType)) {
                 mysqlSave(keys, list);
-            }else if ("file".equals(storageType)) {
+            } else if ("file".equals(storageType)) {
                 if (!FILE_OP_LOCK.tryLock()) {
                     return;
                 }
@@ -99,7 +100,7 @@
                 } finally {
                     FILE_OP_LOCK.unlock();
                 }
-            }else {
+            } else {
                 log.error("鏈畾涔夌殑瀛樺偍绫诲瀷锛歿}", storageType);
             }
         }
@@ -143,9 +144,11 @@
             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 + "_";
+                String prefix = buildFilePrefix(logItem, datePart);
+                if (prefix == null) {
+                    continue;
+                }
                 group.computeIfAbsent(datePart, k -> new HashMap<>())
                         .computeIfAbsent(prefix, k -> new ArrayList<>())
                         .add(logItem);
@@ -154,11 +157,23 @@
                 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();
+                    if (logs == null || logs.isEmpty()) {
+                        continue;
+                    }
+                    DeviceDataLog firstLog = logs.get(0);
+                    Path deviceDir = resolveDeviceDir(dayDir, firstLog);
+                    if (deviceDir == null) {
+                        continue;
+                    }
+                    Files.createDirectories(deviceDir);
+                    String prefix = buildFilePrefix(firstLog, dateEntry.getKey());
+                    if (prefix == null) {
+                        continue;
+                    }
                     logs.sort(Comparator.comparing(DeviceDataLog::getCreateTime, Comparator.nullsLast(Date::compareTo)));
-                    int index = findStartIndex(dayDir, prefix);
-                    Path current = dayDir.resolve(prefix + index + ".log");
+                    int index = findStartIndex(deviceDir, prefix);
+                    Path current = deviceDir.resolve(prefix + index + ".log");
                     if (!Files.exists(current)) {
                         Files.createFile(current);
                     }
@@ -169,7 +184,7 @@
                         byte[] line = (json + System.lineSeparator()).getBytes(StandardCharsets.UTF_8);
                         if (size + line.length > max) {
                             index++;
-                            current = dayDir.resolve(prefix + index + ".log");
+                            current = deviceDir.resolve(prefix + index + ".log");
                             if (!Files.exists(current)) {
                                 Files.createFile(current);
                             }
@@ -184,6 +199,27 @@
         } catch (Exception e) {
             log.error("璁惧鏃ュ織鏂囦欢瀛樺偍澶辫触", e);
         }
+    }
+
+    private String buildFilePrefix(DeviceDataLog logItem, String datePart) {
+        if (logItem == null || logItem.getType() == null || logItem.getDeviceNo() == null) {
+            return null;
+        }
+        if (String.valueOf(SlaveType.Devp).equals(logItem.getType())) {
+            if (logItem.getStationId() == null) {
+                log.warn("璺宠繃缂哄皯绔欑偣鍙风殑杈撻�佽澶囨棩蹇�, deviceNo={}, createTime={}", logItem.getDeviceNo(), logItem.getCreateTime());
+                return null;
+            }
+            return logItem.getType() + "_" + logItem.getDeviceNo() + "_station_" + logItem.getStationId() + "_" + datePart + "_";
+        }
+        return logItem.getType() + "_" + logItem.getDeviceNo() + "_" + datePart + "_";
+    }
+
+    private Path resolveDeviceDir(Path dayDir, DeviceDataLog logItem) {
+        if (dayDir == null || logItem == null || logItem.getType() == null || logItem.getDeviceNo() == null) {
+            return null;
+        }
+        return dayDir.resolve(logItem.getType()).resolve(String.valueOf(logItem.getDeviceNo()));
     }
 
     private int findStartIndex(Path baseDir, String prefix) throws Exception {
@@ -206,7 +242,8 @@
                     if (val > maxIdx) {
                         maxIdx = val;
                     }
-                } catch (NumberFormatException ignored) {}
+                } catch (NumberFormatException ignored) {
+                }
             }
         }
         int candidate = maxIdx == 0 ? 1 : maxIdx;
@@ -242,7 +279,8 @@
                             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                                 try {
                                     Files.deleteIfExists(file);
-                                } catch (Exception ignored) {}
+                                } catch (Exception ignored) {
+                                }
                                 return FileVisitResult.CONTINUE;
                             }
 
@@ -250,7 +288,8 @@
                             public FileVisitResult postVisitDirectory(Path dir, java.io.IOException exc) {
                                 try {
                                     Files.deleteIfExists(dir);
-                                } catch (Exception ignored) {}
+                                } catch (Exception ignored) {
+                                }
                                 return FileVisitResult.CONTINUE;
                             }
                         });

--
Gitblit v1.9.1