From 31e6203a5bb778ad9d1c599171606c89c8edd3a3 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Thu, 21 May 2026 15:59:47 +0800
Subject: [PATCH] 1
---
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/DataDBServiceImpl.java | 86 +++++++++++++++++++++++++++++++++++++++++-
1 files changed, 83 insertions(+), 3 deletions(-)
diff --git a/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/DataDBServiceImpl.java b/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/DataDBServiceImpl.java
index e5aee0b..d4d05b8 100644
--- a/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/DataDBServiceImpl.java
+++ b/trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/DataDBServiceImpl.java
@@ -2,8 +2,10 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yami.trading.bean.data.domain.Kline;
import com.yami.trading.bean.data.domain.Realtime;
+import com.yami.trading.bean.data.dto.BeforeClose;
import com.yami.trading.bean.item.domain.Item;
import com.yami.trading.common.config.RequestDataHelper;
import com.yami.trading.common.constants.Constants;
@@ -26,6 +28,7 @@
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.*;
+import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Service
@@ -109,11 +112,11 @@
// 1. 确定时间戳单位(假设ts是毫秒级,若为秒级需用ofEpochSecond())
Instant instant = Instant.ofEpochMilli(ts);
- // 2. 将时间戳转换为当地时区的日期(指定时区更准确,如Asia/Shanghai)
- LocalDate tsDate = instant.atZone(ZoneId.of("Asia/Shanghai")).toLocalDate();
+ // 2. 将时间戳转换为当地时区的日期(指定时区更准确,如America/New_York)
+ LocalDate tsDate = instant.atZone(ZoneId.of("America/New_York")).toLocalDate();
// 3. 获取“昨天的日期”(当前日期减1天)
- LocalDate yesterday = LocalDate.now(ZoneId.of("Asia/Shanghai")).minusDays(1);
+ LocalDate yesterday = LocalDate.now(ZoneId.of("America/New_York")).minusDays(1);
// 4. 判断是否为昨天
boolean isYesterday = tsDate.equals(yesterday);
@@ -139,6 +142,83 @@
return realtime;
}
+ public BeforeClose getBeforeClose(String symbol, String line, Long ts, Realtime realtime) {
+ BeforeClose beforeClose = (BeforeClose) redisTemplate.opsForValue().get(RedisKeys.REAL_TIME_BEFORE_CLOSE + symbol + line);
+ //超出时间重新计算
+ if (beforeClose == null || ts > beforeClose.getTs()) {
+ long currentTimeStamp = System.currentTimeMillis();
+ RequestDataHelper.set("symbol", symbol);
+ QueryWrapper<Realtime> queryWrapper = new QueryWrapper<Realtime>()
+ .eq("symbol", symbol)
+ .ge("ts", ts)
+ .le("ts", currentTimeStamp)
+ .select("MAX(close) as maxClose", "MIN(close) as minClose");
+ Map<String, Object> resultMap = realtimeService.getMap(queryWrapper);
+ RequestDataHelper.clear();
+ beforeClose = new BeforeClose();
+ BigDecimal maxClose = extractAggregateValue(resultMap, "maxClose");
+ BigDecimal minClose = extractAggregateValue(resultMap, "minClose");
+ mergeRealtimeIntoBeforeClose(beforeClose, maxClose, minClose, realtime);
+ beforeClose.setTs(ts);
+ redisTemplate.opsForValue().set(RedisKeys.REAL_TIME_BEFORE_CLOSE + symbol + line, beforeClose);
+ } else if (realtime != null) {
+ mergeRealtimeIntoBeforeClose(beforeClose, beforeClose.getMaxClose(), beforeClose.getMinClose(), realtime);
+ redisTemplate.opsForValue().set(RedisKeys.REAL_TIME_BEFORE_CLOSE + symbol + line, beforeClose);
+ }
+ return beforeClose;
+ }
+
+ private void mergeRealtimeIntoBeforeClose(BeforeClose beforeClose, BigDecimal maxClose, BigDecimal minClose, Realtime realtime) {
+ BigDecimal max = maxClose;
+ BigDecimal min = minClose;
+ if (realtime != null && realtime.getClose() != null) {
+ BigDecimal latest = realtime.getClose();
+ if (max == null || max.compareTo(BigDecimal.ZERO) <= 0) {
+ max = latest;
+ } else {
+ max = max.max(latest);
+ }
+ if (min == null || min.compareTo(BigDecimal.ZERO) <= 0) {
+ min = latest;
+ } else {
+ min = min.min(latest);
+ }
+ }
+ beforeClose.setMaxClose(max == null ? BigDecimal.ZERO : max);
+ beforeClose.setMinClose(min == null ? BigDecimal.ZERO : min);
+ }
+
+ private BigDecimal extractAggregateValue(Map<String, Object> resultMap, String key) {
+ if (resultMap == null || resultMap.isEmpty()) {
+ return null;
+ }
+ if (resultMap.containsKey(key)) {
+ return convertToBigDecimal(resultMap.get(key));
+ }
+ for (Map.Entry<String, Object> entry : resultMap.entrySet()) {
+ if (entry.getKey() != null && entry.getKey().equalsIgnoreCase(key)) {
+ return convertToBigDecimal(entry.getValue());
+ }
+ }
+ return null;
+ }
+
+ // 辅助方法:统一转换为BigDecimal,避免类型错误
+ private BigDecimal convertToBigDecimal(Object value) {
+ if (value == null) {
+ return BigDecimal.ZERO;
+ }
+ if (value instanceof BigDecimal) {
+ return (BigDecimal) value;
+ }
+ try {
+ return new BigDecimal(value.toString());
+ } catch (NumberFormatException e) {
+ log.error("转换数值为BigDecimal失败:value={}", value, e);
+ return BigDecimal.ZERO;
+ }
+ }
+
@Override
public void cacheBefore24Hour(String symbol) {
// 计算“24小时前”的时间戳(毫秒级)
--
Gitblit v1.9.3