From 04a1828f4cca58545725aef8fb6eb6312d3d1e83 Mon Sep 17 00:00:00 2001
From: Administrator <15274802129@163.com>
Date: Thu, 08 Oct 2026 11:57:19 +0800
Subject: [PATCH] feat(stations): 添加每轮止损次数统计功能

---
 src/main/java/com/xcong/excoin/modules/station/service/impl/StationServiceImpl.java |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/station/service/impl/StationServiceImpl.java b/src/main/java/com/xcong/excoin/modules/station/service/impl/StationServiceImpl.java
index 9cba72e..6b29015 100644
--- a/src/main/java/com/xcong/excoin/modules/station/service/impl/StationServiceImpl.java
+++ b/src/main/java/com/xcong/excoin/modules/station/service/impl/StationServiceImpl.java
@@ -18,7 +18,10 @@
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Date;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -166,6 +169,51 @@
     }
 
     @Override
+    public List<Map<String, Object>> getRoundStopLossCount(String apiKeyMd5) {
+        // selectByApiKeyMd5 按 event_time DESC 返回(新→旧),反转为升序(旧→新)
+        List<StrategyEventLog> events = strategyEventLogDao.selectByApiKeyMd5(apiKeyMd5);
+        List<StrategyEventLog> asc = new ArrayList<>(events);
+        Collections.reverse(asc);
+
+        List<Map<String, Object>> rounds = new ArrayList<>();
+        int longCount = 0;
+        int shortCount = 0;
+
+        for (StrategyEventLog e : asc) {
+            String type = e.getEventType();
+            if ("STOP_LOSS_TRIGGERED".equals(type)) {
+                JSONObject p = JSON.parseObject(e.getPayloadJson());
+                String direction = p.getString("direction");
+                if ("long".equals(direction)) {
+                    longCount++;
+                } else if ("short".equals(direction)) {
+                    shortCount++;
+                }
+            } else if ("ROUND_COMPLETE".equals(type)) {
+                // 轮次结束(重置前):快照当前累计的止损次数,然后清零进入下一轮
+                JSONObject p = JSON.parseObject(e.getPayloadJson());
+                Map<String, Object> row = new LinkedHashMap<>();
+                row.put("roundNum", p.getIntValue("roundNum"));
+                row.put("longLossCount", longCount);
+                row.put("shortLossCount", shortCount);
+                rounds.add(row);
+                longCount = 0;
+                shortCount = 0;
+            }
+        }
+
+        // 当前进行中的轮次(尚未 ROUND_COMPLETE):若已有止损则追加
+        if (longCount > 0 || shortCount > 0) {
+            Map<String, Object> row = new LinkedHashMap<>();
+            row.put("roundNum", null);
+            row.put("longLossCount", longCount);
+            row.put("shortLossCount", shortCount);
+            rounds.add(row);
+        }
+        return rounds;
+    }
+
+    @Override
     public StrategyStatus getStrategyStatus(String apiKeyMd5) {
         return strategyStatusDao.selectByApiKeyMd5(apiKeyMd5);
     }

--
Gitblit v1.9.1