From 98a4849e1e34dacfd93efe63af424990d8935322 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Wed, 24 Jun 2026 21:28:11 +0800
Subject: [PATCH] fix(grid): 修复网格元素索引并发访问问题

---
 src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java |  109 ++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 93 insertions(+), 16 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java b/src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java
index 0b9ef82..661cdd2 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GridElement.java
@@ -87,6 +87,13 @@
     private String longTakeProfitOrderId;
     /** 空仓止盈订单 ID */
     private String shortTakeProfitOrderId;
+    /** 多仓止损订单 ID */
+    private String longStopLossOrderId;
+    /** 空仓止损订单 ID */
+    private String shortStopLossOrderId;
+
+    /** 索引重建锁,保证 refreshIndices() 与计数读取之间互斥,避免 clear→rebuild 窗口期读到 0 */
+    private static final Object INDEX_LOCK = new Object();
 
     /** 全局 ID 索引,由 {@link GateConfig#setGridElements(List)} 触发重建,O(1) 查找 */
     private static final Map<Integer, GridElement> INDEX = new ConcurrentHashMap<>();
@@ -100,6 +107,10 @@
     private static final Map<String, GridElement> LONG_TP_ORDER_ID_INDEX = new ConcurrentHashMap<>();
     /** 全局空仓止盈订单 ID 索引,由 {@link GateConfig#setGridElements(List)} 触发重建,O(1) 查找 */
     private static final Map<String, GridElement> SHORT_TP_ORDER_ID_INDEX = new ConcurrentHashMap<>();
+    /** 全局多仓止损订单 ID 索引 */
+    private static final Map<String, GridElement> LONG_SL_ORDER_ID_INDEX = new ConcurrentHashMap<>();
+    /** 全局空仓止损订单 ID 索引 */
+    private static final Map<String, GridElement> SHORT_SL_ORDER_ID_INDEX = new ConcurrentHashMap<>();
 
     /**
      * 根据 ID 快速查找网格元素(O(1))。
@@ -161,6 +172,30 @@
         return SHORT_TP_ORDER_ID_INDEX.get(orderId);
     }
 
+    /** @return 当前多仓止盈单数量(与 refreshIndices 互斥,避免清空窗口读到 0) */
+    public static int getLongTakeProfitCount() {
+        synchronized (INDEX_LOCK) { return LONG_TP_ORDER_ID_INDEX.size(); }
+    }
+
+    /** @return 当前空仓止盈单数量(与 refreshIndices 互斥,避免清空窗口读到 0) */
+    public static int getShortTakeProfitCount() {
+        synchronized (INDEX_LOCK) { return SHORT_TP_ORDER_ID_INDEX.size(); }
+    }
+
+    /**
+     * 根据多仓止损订单 ID 快速查找网格元素(O(1))。
+     */
+    public static GridElement findByLongStopLossOrderId(String orderId) {
+        return LONG_SL_ORDER_ID_INDEX.get(orderId);
+    }
+
+    /**
+     * 根据空仓止损订单 ID 快速查找网格元素(O(1))。
+     */
+    public static GridElement findByShortStopLossOrderId(String orderId) {
+        return SHORT_SL_ORDER_ID_INDEX.get(orderId);
+    }
+
     /**
      * 从列表中重建全局 ID 索引和价格索引。
      * 由 {@link GateConfig#setGridElements(List)} 在每次列表变更后调用。
@@ -172,6 +207,8 @@
         SHORT_ORDER_ID_INDEX.clear();
         LONG_TP_ORDER_ID_INDEX.clear();
         SHORT_TP_ORDER_ID_INDEX.clear();
+        LONG_SL_ORDER_ID_INDEX.clear();
+        SHORT_SL_ORDER_ID_INDEX.clear();
         for (GridElement e : elements) {
             INDEX.put(e.getId(), e);
             putDynamicIndices(e);
@@ -186,14 +223,19 @@
      * 使快速查找方法获取到最新数据。
      */
     public static void refreshIndices() {
-        PRICE_INDEX.clear();
-        LONG_ORDER_ID_INDEX.clear();
-        SHORT_ORDER_ID_INDEX.clear();
-        LONG_TP_ORDER_ID_INDEX.clear();
-        SHORT_TP_ORDER_ID_INDEX.clear();
-        for (GridElement e : INDEX.values()) {
-            putDynamicIndices(e);
+        synchronized (INDEX_LOCK) {
+            PRICE_INDEX.clear();
+            LONG_ORDER_ID_INDEX.clear();
+            SHORT_ORDER_ID_INDEX.clear();
+            LONG_TP_ORDER_ID_INDEX.clear();
+            SHORT_TP_ORDER_ID_INDEX.clear();
+            LONG_SL_ORDER_ID_INDEX.clear();
+            SHORT_SL_ORDER_ID_INDEX.clear();
+            for (GridElement e : INDEX.values()) {
+                putDynamicIndices(e);
+            }
         }
+        // logAll 在锁外执行,不阻塞计数查询
         logAll();
     }
 
@@ -205,9 +247,10 @@
         sorted.sort((a, b) -> Integer.compare(a.getId(), b.getId()));
         StringBuilder sb = new StringBuilder("\n========== 网格数据 ==========\n");
         for (GridElement e : sorted) {
-            if (e.isHasLongOrder() || e.isHasShortOrder()){
+            if (e.isHasLongOrder() || e.isHasShortOrder()
+                    || e.getLongStopLossOrderId() != null || e.getShortStopLossOrderId() != null){
                 sb.append(String.format(
-                        "  ID=%4d  价格=%s  up=%s  down=%s  多仓=%s(%s)  空仓=%s(%s)  多止盈=%s  空止盈=%s\n",
+                        "  ID=%4d  价格=%s  up=%s  down=%s  多仓=%s(%s)  空仓=%s(%s)  多止盈=%s  空止盈=%s  多止损=%s  空止损=%s\n",
                         e.getId(),
                         e.getGridPrice(),
                         e.getUpId(),
@@ -217,19 +260,23 @@
                         e.isHasShortOrder() ? "有" : "无",
                         e.getShortOrderId() != null ? e.getShortOrderId() : "-",
                         e.getLongTakeProfitOrderId() != null ? e.getLongTakeProfitOrderId() : "-",
-                        e.getShortTakeProfitOrderId() != null ? e.getShortTakeProfitOrderId() : "-"
+                        e.getShortTakeProfitOrderId() != null ? e.getShortTakeProfitOrderId() : "-",
+                        e.getLongStopLossOrderId() != null ? e.getLongStopLossOrderId() : "-",
+                        e.getShortStopLossOrderId() != null ? e.getShortStopLossOrderId() : "-"
                 ));
             }
         }
         sb.append(String.format(
                 "------------------------------------------------------------\n" +
-                "  索引统计: ID=%d  价格=%d  多仓订单ID=%d  空仓订单ID=%d  多止盈ID=%d  空止盈ID=%d\n",
+                "  索引统计: ID=%d  价格=%d  多仓订单ID=%d  空仓订单ID=%d  多止盈ID=%d  空止盈ID=%d  多止损ID=%d  空止损ID=%d\n",
                 INDEX.size(),
                 PRICE_INDEX.size(),
                 LONG_ORDER_ID_INDEX.size(),
                 SHORT_ORDER_ID_INDEX.size(),
                 LONG_TP_ORDER_ID_INDEX.size(),
-                SHORT_TP_ORDER_ID_INDEX.size()
+                SHORT_TP_ORDER_ID_INDEX.size(),
+                LONG_SL_ORDER_ID_INDEX.size(),
+                SHORT_SL_ORDER_ID_INDEX.size()
         ));
         sb.append(String.format("  多仓订单ID索引: %s\n", LONG_ORDER_ID_INDEX.keySet()));
         sb.append(String.format("  空仓订单ID索引: %s\n", SHORT_ORDER_ID_INDEX.keySet()));
@@ -245,10 +292,10 @@
      *
      * @return 已挂多仓条件单的 GridElement 列表
      */
-    public static List<GridElement> findAllLongOrders(BigDecimal currentPrice) {
+    public static List<GridElement> findAllShortOrders(BigDecimal currentPrice) {
         List<GridElement> result = new ArrayList<>();
         for (GridElement e : INDEX.values()) {
-            if (e.isHasShortOrder() && e.getGridPrice().compareTo(currentPrice) < 0) {
+            if (e.isHasShortOrder() && e.getGridPrice().compareTo(currentPrice) < 0 && e.getShortTakeProfitOrderId() == null) {
                 result.add(e);
             }
         }
@@ -261,10 +308,10 @@
      *
      * @return 已挂空仓条件单的 GridElement 列表
      */
-    public static List<GridElement> findAllShortOrders(BigDecimal currentPrice) {
+    public static List<GridElement> findAllLongOrders(BigDecimal currentPrice) {
         List<GridElement> result = new ArrayList<>();
         for (GridElement e : INDEX.values()) {
-            if (e.isHasLongOrder() && e.getGridPrice().compareTo(currentPrice) > 0) {
+            if (e.isHasLongOrder() && e.getGridPrice().compareTo(currentPrice) > 0 && e.getLongTakeProfitOrderId() == null) {
                 result.add(e);
             }
         }
@@ -284,6 +331,12 @@
         }
         if (e.getShortTakeProfitOrderId() != null) {
             SHORT_TP_ORDER_ID_INDEX.put(e.getShortTakeProfitOrderId(), e);
+        }
+        if (e.getLongStopLossOrderId() != null) {
+            LONG_SL_ORDER_ID_INDEX.put(e.getLongStopLossOrderId(), e);
+        }
+        if (e.getShortStopLossOrderId() != null) {
+            SHORT_SL_ORDER_ID_INDEX.put(e.getShortStopLossOrderId(), e);
         }
     }
 
@@ -314,6 +367,8 @@
         this.shortOrderId = builder.shortOrderId;
         this.longTakeProfitOrderId = builder.longTakeProfitOrderId;
         this.shortTakeProfitOrderId = builder.shortTakeProfitOrderId;
+        this.longStopLossOrderId = builder.longStopLossOrderId;
+        this.shortStopLossOrderId = builder.shortStopLossOrderId;
     }
 
     // ==================== 网格层级编号 ====================
@@ -400,6 +455,20 @@
     /** 设置空仓止盈订单 ID */
     public void setShortTakeProfitOrderId(String shortTakeProfitOrderId) { this.shortTakeProfitOrderId = shortTakeProfitOrderId; }
 
+    // ==================== 多仓止损订单 ID ====================
+
+    /** @return 多仓止损订单 ID */
+    public String getLongStopLossOrderId() { return longStopLossOrderId; }
+    /** 设置多仓止损订单 ID */
+    public void setLongStopLossOrderId(String longStopLossOrderId) { this.longStopLossOrderId = longStopLossOrderId; }
+
+    // ==================== 空仓止损订单 ID ====================
+
+    /** @return 空仓止损订单 ID */
+    public String getShortStopLossOrderId() { return shortStopLossOrderId; }
+    /** 设置空仓止损订单 ID */
+    public void setShortStopLossOrderId(String shortStopLossOrderId) { this.shortStopLossOrderId = shortStopLossOrderId; }
+
     public static Builder builder() {
         return new Builder();
     }
@@ -438,6 +507,10 @@
         private String longTakeProfitOrderId;
         /** 空仓止盈订单 ID */
         private String shortTakeProfitOrderId;
+        /** 多仓止损订单 ID */
+        private String longStopLossOrderId;
+        /** 空仓止损订单 ID */
+        private String shortStopLossOrderId;
 
         /** 设置网格层级编号 */
         public Builder id(int id) { this.id = id; return this; }
@@ -463,6 +536,10 @@
         public Builder longTakeProfitOrderId(String longTakeProfitOrderId) { this.longTakeProfitOrderId = longTakeProfitOrderId; return this; }
         /** 设置空仓止盈订单 ID */
         public Builder shortTakeProfitOrderId(String shortTakeProfitOrderId) { this.shortTakeProfitOrderId = shortTakeProfitOrderId; return this; }
+        /** 设置多仓止损订单 ID */
+        public Builder longStopLossOrderId(String longStopLossOrderId) { this.longStopLossOrderId = longStopLossOrderId; return this; }
+        /** 设置空仓止损订单 ID */
+        public Builder shortStopLossOrderId(String shortStopLossOrderId) { this.shortStopLossOrderId = shortStopLossOrderId; return this; }
 
         public GridElement build() {
             return new GridElement(this);

--
Gitblit v1.9.1