From 5d0e6b7b45630f32100d23ca107a9c74df43db75 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Mon, 11 May 2026 11:58:04 +0800
Subject: [PATCH] refactor(gate): 移除账号标签配置和多账号支持功能

---
 src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java |  129 ++++++++++++++++++++++++------------------
 1 files changed, 73 insertions(+), 56 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java b/src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java
index f020544..93ffff4 100644
--- a/src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java
+++ b/src/main/java/com/xcong/excoin/modules/gateApi/GateWebSocketClientManager.java
@@ -1,83 +1,100 @@
 package com.xcong.excoin.modules.gateApi;
 
-import com.xcong.excoin.modules.okxNewPrice.OkxKlineWebSocketClient;
-import com.xcong.excoin.modules.okxNewPrice.OkxQuantWebSocketClient;
-import com.xcong.excoin.modules.okxNewPrice.celue.CaoZuoService;
-import com.xcong.excoin.modules.okxNewPrice.okxWs.enums.ExchangeInfoEnum;
-import com.xcong.excoin.modules.okxNewPrice.okxWs.wanggeList.WangGeListService;
-import com.xcong.excoin.utils.RedisUtils;
+import com.xcong.excoin.modules.gateApi.wsHandler.handler.CandlestickChannelHandler;
+import com.xcong.excoin.modules.gateApi.wsHandler.handler.PositionClosesChannelHandler;
+import com.xcong.excoin.modules.gateApi.wsHandler.handler.PositionsChannelHandler;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
-import java.util.Collection;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
+import java.math.BigDecimal;
 
 /**
- * 管理多个OKX WebSocket客户端实例,每个实例对应一个账号
+ * Gate 模块 Spring 入口,组装所有组件并管理生命周期。
+ *
+ * <h3>启动流程 ({@code @PostConstruct})</h3>
+ * <ol>
+ *   <li>构建 {@link GateConfig}(Builder 模式,含 API 密钥、合约、策略参数)</li>
+ *   <li>创建 {@link GateGridTradeService} → init():切持仓模式、清旧条件单、设杠杆</li>
+ *   <li>创建 {@link GateKlineWebSocketClient} → 注册 3 个 Handler → init():建立 WS 连接</li>
+ *   <li>gridTradeService.startGrid():激活策略,等待 K 线触发首次双开</li>
+ * </ol>
+ *
+ * <h3>销毁流程 ({@code @PreDestroy})</h3>
+ * <ol>
+ *   <li>gridTradeService.stopGrid():取消条件单 → 关闭交易线程池</li>
+ *   <li>wsClient.destroy():取消订阅 → 断开 WS → 关闭线程池</li>
+ * </ol>
+ *
+ * <h3>配置</h3>
+ * 当前在代码中硬编码测试网参数。切换到生产网只需改为 {@code .isProduction(true)}。
+ *
+ * @author Administrator
  */
 @Slf4j
 @Component
 public class GateWebSocketClientManager {
-    @Autowired
-    private CaoZuoService caoZuoService;
-    @Autowired
-    private RedisUtils redisUtils;
-    @Autowired
-    private WangGeListService wangGeListService;
 
-    private GateKlineWebSocketClient klinePriceClient;
+    /** WebSocket 连接管理器 */
+    private GateKlineWebSocketClient wsClient;
+    /** 网格交易策略服务 */
+    private GateGridTradeService gridTradeService;
+    /** 统一配置 */
+    private GateConfig config;
 
-
-    /**
-     * 初始化方法,在Spring Bean构造完成后执行
-     * 创建并初始化所有账号的WebSocket客户端实例
-     */
     @PostConstruct
     public void init() {
-        log.info("开始初始化OkxWebSocketClientManager");
-        
-        // 初始化价格WebSocket客户端
+        log.info("[管理器] 开始初始化...");
+
         try {
-            klinePriceClient = new GateKlineWebSocketClient(caoZuoService, this, wangGeListService);
-            klinePriceClient.init();
-            log.info("已初始化OkxNewPriceWebSocketClient");
+            config = GateConfig.builder()
+                    .apiKey("d90ca272391992b8e74f8f92cedb21ec")
+                    .apiSecret("1861e4f52de4bb53369ea3208d9ede38ece4777368030f96c77d27934c46c274")
+                    .contract("ETH_USDT")
+                    .leverage("100")
+                    .marginMode("CROSS")
+                    .positionMode("dual")
+                    .gridRate(new BigDecimal("0.0015"))
+                    .overallTp(new BigDecimal("5"))
+                    .maxLoss(new BigDecimal("15"))
+                    .quantity("1")
+                    .contractMultiplier(new BigDecimal("0.01"))
+                    .unrealizedPnlPriceMode(GateConfig.PnLPriceMode.LAST_PRICE)
+                    .isProduction(false)
+                    .reopenMaxRetries(3)
+                    .build();
+
+            gridTradeService = new GateGridTradeService(config);
+            gridTradeService.init();
+
+            wsClient = new GateKlineWebSocketClient(config.getWsUrl());
+            wsClient.addChannelHandler(new CandlestickChannelHandler(config.getContract(), gridTradeService));
+            wsClient.addChannelHandler(new PositionsChannelHandler(
+                    config.getApiKey(), config.getApiSecret(), config.getContract(), gridTradeService));
+            wsClient.addChannelHandler(new PositionClosesChannelHandler(
+                    config.getApiKey(), config.getApiSecret(), config.getContract(), gridTradeService));
+            wsClient.init();
+            log.info("[管理器] WS已连接, 已注册 3 个频道处理器");
+
+            gridTradeService.startGrid();
         } catch (Exception e) {
-            log.error("初始化OkxNewPriceWebSocketClient失败", e);
+            log.error("[管理器] 初始化失败", e);
         }
-        
     }
 
-    /**
-     * 销毁方法,在Spring Bean销毁前执行
-     * 关闭所有WebSocket客户端连接和相关资源
-     */
     @PreDestroy
     public void destroy() {
-        log.info("开始销毁OkxWebSocketClientManager");
-        
-        // 关闭价格WebSocket客户端
-        if (klinePriceClient != null) {
-            try {
-                klinePriceClient.destroy();
-                log.info("已销毁OkxNewPriceWebSocketClient");
-            } catch (Exception e) {
-                log.error("销毁OkxNewPriceWebSocketClient失败", e);
-            }
+        log.info("[管理器] 开始销毁...");
+        if (gridTradeService != null) {
+            gridTradeService.stopGrid();
         }
-        
-        log.info("OkxWebSocketClientManager销毁完成");
+        if (wsClient != null) {
+            wsClient.destroy();
+        }
+        log.info("[管理器] 销毁完成");
     }
 
-    /**
-     * 获取OkxNewPriceWebSocketClient实例
-     * @return 价格WebSocket客户端实例
-     */
-    public GateKlineWebSocketClient getKlineWebSocketClient() {
-        return klinePriceClient;
-    }
-}
\ No newline at end of file
+    public GateKlineWebSocketClient getKlineWebSocketClient() { return wsClient; }
+    public GateGridTradeService getGridTradeService() { return gridTradeService; }
+}

--
Gitblit v1.9.1