| | |
| | | |
| | | |
| | | 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; |
| | |
| | | import java.time.*; |
| | | import java.time.temporal.ChronoUnit; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | |
| | | return realtime; |
| | | } |
| | | |
| | | public BeforeClose getBeforeClose(String symbol, String line, Long ts) { |
| | | BeforeClose beforeClose = (BeforeClose) redisTemplate.opsForValue().get(RedisKeys.REAL_TIME_BEFORE_CLOSE + symbol + line); |
| | | if (beforeClose == null) { |
| | | // 直接获取当前时间的毫秒级时间戳(系统默认时区,但值是全球统一的) |
| | | long currentTimeStamp = System.currentTimeMillis(); |
| | | |
| | | // 如果需要严格基于东京时区的当前时间戳(结果和上面一致,因为时间戳是UTC绝对时间) |
| | | //long currentTokyoTimeStamp = Instant.now().atZone(ZoneId.of("Asia/Tokyo")).toInstant().toEpochMilli(); |
| | | 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"); |
| | | // 4. 执行聚合查询,用selectMap接收结果(键值对:maxClose/minClose -> 对应值) |
| | | Map<String, Object> resultMap = realtimeService.getMap(queryWrapper); |
| | | RequestDataHelper.clear(); |
| | | beforeClose = new BeforeClose(); |
| | | if (resultMap == null || resultMap.isEmpty()) { |
| | | return beforeClose; |
| | | } |
| | | beforeClose.setMaxClose(convertToBigDecimal(resultMap.get("maxClose"))); |
| | | beforeClose.setMinClose(convertToBigDecimal(resultMap.get("minClose"))); |
| | | redisTemplate.opsForValue().set(RedisKeys.REAL_TIME_BEFORE_CLOSE + symbol + line, beforeClose , 5 , TimeUnit.MINUTES); |
| | | } |
| | | return beforeClose; |
| | | } |
| | | |
| | | // 辅助方法:统一转换为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小时前”的时间戳(毫秒级) |