Răsfoiți Sursa

调整启动时逻辑

ximinghao 2 luni în urmă
părinte
comite
80d8104d0b

+ 0 - 1
src/main/java/com/skyversation/xjcy/service/DataCountService.java

@@ -58,7 +58,6 @@ public class DataCountService {
     @Resource
     private AuthService authService;
 
-    @PostConstruct
     public void tryCount() {
         long now = System.currentTimeMillis();
         long last = lastStartTime.get();

+ 20 - 23
src/main/java/com/skyversation/xjcy/service/SerialNumberGenerator.java

@@ -44,8 +44,6 @@ public class SerialNumberGenerator {
 
     // 存储每个前缀的当前状态
     private final Map<Prefix, SerialState> stateMap = new ConcurrentHashMap<>();
-    // 存储每个前缀的初始化状态
-    private final Map<Prefix, Boolean> initializedMap = new ConcurrentHashMap<>();
     // 用于初始化时的同步锁
     private final Map<Prefix, ReentrantLock> initLocks = new EnumMap<>(Prefix.class);
 
@@ -59,7 +57,6 @@ public class SerialNumberGenerator {
         }
     }
 
-    @PostConstruct
     public void autoInit() {
         String token;
         try {
@@ -87,10 +84,6 @@ public class SerialNumberGenerator {
      * @throws IllegalStateException 如果前缀未初始化
      */
     public String generate(Prefix prefix) {
-        // 检查是否已初始化
-        if (!isInitialized(prefix)) {
-            throw new IllegalStateException("Prefix " + prefix + " not initialized. Call initPrefix() first.");
-        }
 
         // 获取或创建当前状态
         SerialState currentState = getOrCreateState(prefix);
@@ -121,24 +114,13 @@ public class SerialNumberGenerator {
         ReentrantLock lock = initLocks.get(prefix);
         lock.lock();
         try {
-            if (isInitialized(prefix)) {
-                throw new IllegalStateException("Prefix " + prefix + " already initialized");
-            }
 
             stateMap.put(prefix, new SerialState(today, new AtomicInteger(initialValue - 1)));
-            initializedMap.put(prefix, true);
         } finally {
             lock.unlock();
         }
     }
 
-    /**
-     * 检查前缀是否已初始化
-     */
-    public boolean isInitialized(Prefix prefix) {
-        return initializedMap.getOrDefault(prefix, false);
-    }
-
     // 获取或创建当前状态(处理日期切换)
     private SerialState getOrCreateState(Prefix prefix) {
         String today = currentDate();
@@ -155,12 +137,27 @@ public class SerialNumberGenerator {
         try {
             // 双重检查锁定模式
             currentState = stateMap.get(prefix);
-            if (currentState == null || !currentState.date.equals(today)) {
-                // 创建新的状态(计数器从0开始)
-                currentState = new SerialState(today, new AtomicInteger(0));
-                stateMap.put(prefix, currentState);
+            if (currentState == null) {
+                // 首次初始化:从DMS查询当天最大流水号
+                String token = authService.getTokenOfServiceAccount();
+                int max = dmsService.getLargestCode(token, prefix.getColumId(), prefix.getCodeColumn(), today);
+                // max为当天已使用的最大序号(无记录时为0),计数器设为max
+                SerialState newState = new SerialState(today, new AtomicInteger(max));
+                stateMap.put(prefix, newState);
+                return newState;
+            } else if (!currentState.date.equals(today)) {
+                // 日期切换:重置计数器为0
+                SerialState newState = new SerialState(today, new AtomicInteger(0));
+                stateMap.put(prefix, newState);
+                // 已初始化标志保持不变
+                return newState;
+            } else {
+                // 其他线程已更新,直接返回
+                return currentState;
             }
-            return currentState;
+        } catch (Exception e) {
+            System.out.println(prefix + "流水号初始化失败");
+            throw new RuntimeException(e);
         } finally {
             lock.unlock();
         }