From b28a97e1bf66e3279e78f31ce58122427787ceec Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Thu, 11 Jun 2026 09:44:20 +0800
Subject: [PATCH] 1
---
trading-order-huobi/src/main/java/com.yami.trading.huobi/data/internal/DataDBServiceImpl.java | 88 ++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 85 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 38f9362..eb73aa7 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/Tokyo)
- LocalDate tsDate = instant.atZone(ZoneId.of("Asia/Tokyo")).toLocalDate();
+ // 2. 将时间戳转换为当地时区的日期(指定时区更准确,如America/New_York)
+ LocalDate tsDate = instant.atZone(ZoneId.of("America/New_York")).toLocalDate();
// 3. 获取“昨天的日期”(当前日期减1天)
- LocalDate yesterday = LocalDate.now(ZoneId.of("Asia/Tokyo")).minusDays(1);
+ LocalDate yesterday = LocalDate.now(ZoneId.of("America/New_York")).minusDays(1);
// 4. 判断是否为昨天
boolean isYesterday = tsDate.equals(yesterday);
@@ -139,6 +142,85 @@
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);
+ long queryTs = normalizeToMillis(ts);
+ if (beforeClose == null || queryTs > beforeClose.getTs()) {
+ long currentTimeStamp = System.currentTimeMillis();
+ RequestDataHelper.set("symbol", symbol);
+ QueryWrapper<Realtime> queryWrapper = new QueryWrapper<Realtime>()
+ .eq("symbol", symbol)
+ .ge("ts", queryTs)
+ .le("ts", currentTimeStamp)
+ .select("MAX(CAST(close AS DECIMAL(20,8))) as maxClose",
+ "MIN(CAST(close AS DECIMAL(20,8))) as minClose");
+ Map<String, Object> resultMap = realtimeService.getMap(queryWrapper);
+ RequestDataHelper.clear();
+ beforeClose = new BeforeClose();
+ beforeClose.setTs(queryTs);
+ if (resultMap != null && !resultMap.isEmpty()) {
+ beforeClose.setMaxClose(convertToBigDecimal(resultMap.get("maxClose")));
+ beforeClose.setMinClose(convertToBigDecimal(resultMap.get("minClose")));
+ }
+ redisTemplate.opsForValue().set(RedisKeys.REAL_TIME_BEFORE_CLOSE + symbol + line, beforeClose);
+ }
+
+ mergeRealtimeIntoBeforeClose(beforeClose, realtime, symbol, line);
+ if (!isValidBeforeClose(beforeClose)) {
+ return null;
+ }
+ return beforeClose;
+ }
+
+ private void mergeRealtimeIntoBeforeClose(BeforeClose beforeClose, Realtime realtime, String symbol, String line) {
+ if (realtime == null || realtime.getClose() == null || realtime.getClose().compareTo(BigDecimal.ZERO) <= 0) {
+ return;
+ }
+ BigDecimal close = realtime.getClose();
+ if (!isValidBeforeClose(beforeClose)) {
+ beforeClose.setMaxClose(close);
+ beforeClose.setMinClose(close);
+ } else {
+ if (close.compareTo(beforeClose.getMaxClose()) > 0) {
+ beforeClose.setMaxClose(close);
+ }
+ if (beforeClose.getMinClose().compareTo(BigDecimal.ZERO) <= 0
+ || close.compareTo(beforeClose.getMinClose()) < 0) {
+ beforeClose.setMinClose(close);
+ }
+ }
+ redisTemplate.opsForValue().set(RedisKeys.REAL_TIME_BEFORE_CLOSE + symbol + line, beforeClose);
+ }
+
+ private boolean isValidBeforeClose(BeforeClose beforeClose) {
+ return beforeClose != null
+ && beforeClose.getMaxClose() != null && beforeClose.getMaxClose().compareTo(BigDecimal.ZERO) > 0
+ && beforeClose.getMinClose() != null && beforeClose.getMinClose().compareTo(BigDecimal.ZERO) > 0;
+ }
+
+ private long normalizeToMillis(Long ts) {
+ if (ts == null) {
+ return System.currentTimeMillis();
+ }
+ return String.valueOf(ts).length() <= 10 ? ts * 1000 : ts;
+ }
+
+ // 辅助方法:统一转换为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