From 7491d19c0412712080f23c389eb2f1bfa2924e09 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Tue, 19 May 2026 11:37:10 +0800
Subject: [PATCH] ``` fix(gate): 修复网格交易中订单状态更新处理逻辑

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java                   |   82 +++++++++++++++++++++++++++++++++++++++++
 src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/OrdersChannelHandler.java |    6 +-
 2 files changed, 85 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
index ddbe3b9..a1ecfc3 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateGridTradeService.java
@@ -635,6 +635,88 @@
         }
         log.info("[Gate] 条件单状态变更, id:{}, status:{}, reason:{}, order_type:{}",
                 orderId, status, reason, orderType);
+        if (!"finished".equals(status)) {
+            return;
+        }
+
+        /**
+         * 匹配止盈单止盈
+         */
+        GridElement byLongTakeProfitOrderId = GridElement.findByLongTakeProfitOrderId(orderId);
+        if (byLongTakeProfitOrderId != null){
+            longTakeProfitTraderIdParam(
+                    byLongTakeProfitOrderId,
+                    null,
+                    false
+            );
+            longEntryTraderIdParam(
+                    byLongTakeProfitOrderId,
+                    null,
+                    false
+            );
+        }
+        GridElement byShortTakeProfitOrderId = GridElement.findByShortTakeProfitOrderId(orderId);
+        if (byShortTakeProfitOrderId != null){
+            shortTakeProfitTraderIdParam(
+                    byShortTakeProfitOrderId,
+                    null,
+                    false
+            );
+            shortEntryTraderIdParam(
+                    byShortTakeProfitOrderId,
+                    null,
+                    false
+            );
+        }
+
+        /**
+         * 匹配挂单
+         */
+        GridElement longGridElement = GridElement.findByLongOrderId(orderId);
+        if (longGridElement != null) {
+            if (longGridElement.isHasLongOrder()){
+                if (longGridElement.getLongTakeProfitOrderId() == null){
+                    BigDecimal longTp = longGridElement.getLongTraderParam().getTakeProfitPrice();
+                    if (longTp != null) {
+                        executor.placeTakeProfit(longTp,
+                                FuturesPriceTrigger.RuleEnum.NUMBER_1,
+                                ORDER_TYPE_CLOSE_LONG,
+                                negate(config.getQuantity()),
+                                (profitId) -> {
+                                    longTakeProfitTraderIdParam(
+                                            longGridElement,
+                                            profitId,
+                                            true
+                                    );
+                                });
+                        log.info("[Gate] 多单成交匹配止盈, orderId:{}, 止盈价:{}, size:{}", orderId, longTp, negate(config.getQuantity()));
+                        return;
+                    }
+                }
+            }
+        }
+        GridElement shortGridElement = GridElement.findByShortOrderId(orderId);
+        if (shortGridElement != null) {
+            if (shortGridElement.isHasShortOrder()){
+                if (shortGridElement.getShortTakeProfitOrderId() == null){
+                    BigDecimal shortTp = shortGridElement.getShortTraderParam().getTakeProfitPrice();
+                    if (shortTp != null) {
+                        executor.placeTakeProfit(shortTp,
+                                FuturesPriceTrigger.RuleEnum.NUMBER_2,
+                                ORDER_TYPE_CLOSE_SHORT,
+                                config.getQuantity(),
+                                (profitId) -> {
+                                    shortTakeProfitTraderIdParam(
+                                            shortGridElement,
+                                            profitId,
+                                            true
+                                    );
+                                });
+                        log.info("[Gate] 空单成交匹配止盈, orderId:{}, 止盈价:{}, size:{}", orderId, shortTp, config.getQuantity());
+                    }
+                }
+            }
+        }
     }
 
     // ---- 网格队列处理 ----
diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/OrdersChannelHandler.java b/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/OrdersChannelHandler.java
index af93aff..851fb17 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/OrdersChannelHandler.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/wsHandler/handler/OrdersChannelHandler.java
@@ -61,9 +61,9 @@
                 log.info("[{}] 订单更新, id:{}, status:{}, finish_as:{}, price:{}, size:{},update_id:{}",
                         CHANNEL_NAME, orderId, status, finishAs,
                         order.get("price"), order.get("size"),updateId);
-                if (getGridTradeService() != null) {
-                    getGridTradeService().onOrderUpdate(orderId, status, finishAs);
-                }
+//                if (getGridTradeService() != null) {
+//                    getGridTradeService().onOrderUpdate(orderId, status, finishAs);
+//                }
             }
         } catch (Exception e) {
             log.error("[{}] 处理数据失败", CHANNEL_NAME, e);

--
Gitblit v1.9.1