| | |
| | | |
| | | import java.lang.reflect.Type; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import javax.annotation.Resource; |
| | |
| | | |
| | | // 判断当前时间是否在交易时间内 |
| | | if (isTimeInRange(currentTime, startTime, endTime)) { |
| | | PremarketStockVO vo = new PremarketStockVO(); |
| | | vo.setCode(setting.getStockCode()); |
| | | |
| | | // 计算价格 |
| | | BigDecimal price; |
| | | if ("0".equals(setting.getType())) { |
| | | // type=0: 直接指定价格 |
| | | price = new BigDecimal(setting.getPrice()); |
| | | } else { |
| | | // type=1: 百分比调价格,需要获取当前价格 |
| | | Stock stock = stockMapper.selectOne(new QueryWrapper<Stock>().eq("stock_code", setting.getStockCode())); |
| | | if (stock != null) { |
| | | String s = RedisKeyUtil.doPost(stock.getStockCode(), stock.getStockType()); |
| | | if (s != null) { |
| | | Map<String, Object> stringObjectMap = RedisKeyUtil.jsonToMap(s); |
| | | BigDecimal currentPrice = new BigDecimal(stringObjectMap.get("Last").toString()); |
| | | price = currentPrice.multiply(new BigDecimal(setting.getPrice())); |
| | | } else { |
| | | continue; // 无法获取当前价格,跳过 |
| | | } |
| | | } else { |
| | | if (stock == null) { |
| | | continue; // 股票不存在,跳过 |
| | | } |
| | | |
| | | // 先获取真实价格(从Redis获取,不经过StockSetting计算) |
| | | String s = RedisKeyUtil.doPost(stock.getStockCode(), stock.getStockType()); |
| | | if (s == null) { |
| | | continue; // 无法获取真实价格,跳过 |
| | | } |
| | | vo.setPrice(price); |
| | | |
| | | Map<String, Object> stringObjectMap = RedisKeyUtil.jsonToMap(s); |
| | | BigDecimal realPrice = new BigDecimal(stringObjectMap.get("Last").toString()); |
| | | |
| | | // 计算盘前价格 |
| | | BigDecimal premarketPrice; |
| | | if ("0".equals(setting.getType())) { |
| | | // type=0: 直接指定价格 |
| | | premarketPrice = new BigDecimal(setting.getPrice()); |
| | | } else { |
| | | // type=1: 百分比调价格,基于真实价格计算 |
| | | premarketPrice = realPrice.multiply(new BigDecimal(setting.getPrice())); |
| | | } |
| | | |
| | | // 计算涨跌价格 = 盘前价格 - 真实价格 |
| | | BigDecimal chg = premarketPrice.subtract(realPrice); |
| | | |
| | | // 计算涨跌幅 = (盘前价格 - 真实价格) / 真实价格 * 100 |
| | | BigDecimal chgPct = BigDecimal.ZERO; |
| | | if (realPrice.compareTo(BigDecimal.ZERO) > 0) { |
| | | chgPct = chg.divide(realPrice, 4, RoundingMode.HALF_UP) |
| | | .multiply(new BigDecimal("100")); |
| | | } |
| | | |
| | | PremarketStockVO vo = new PremarketStockVO(); |
| | | vo.setCode(setting.getStockCode()); |
| | | vo.setPrice(premarketPrice); |
| | | vo.setChg(chg); |
| | | vo.setChgPct(chgPct); |
| | | resultList.add(vo); |
| | | } |
| | | } |