| | |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.yami.trading.admin.util.us.HttpClientRequest; |
| | | import com.yami.trading.admin.util.us.ReponseBase; |
| | | import com.yami.trading.bean.data.domain.Realtime; |
| | |
| | | import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import java.lang.reflect.Type; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.concurrent.CompletableFuture; |
| | |
| | | /** |
| | | * 同步系统所需要的股票 |
| | | */ |
| | | @Scheduled(cron = "0 0/5 * * * ?") |
| | | @Scheduled(cron = "0 0/30 * * * ?") |
| | | public void syncINStockData() { |
| | | |
| | | if (syncINStockData.get()) { // 判断任务是否在处理中 |
| | |
| | | if (item != null) { //已有不添加 |
| | | continue; |
| | | } |
| | | |
| | | item = new Item(); |
| | | String name = StringUtils.trim(o.getName()); |
| | | item.setEnName(name); |
| | |
| | | item.setStockCode(o.getId()); |
| | | |
| | | updateStockList.add(item); |
| | | |
| | | |
| | | Realtime realtime = new Realtime(); |
| | | realtime.setUuid(o.getId()); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 初始化所有股票数据 |
| | | * 初始化更新价格 |
| | | */ |
| | | public void initAllStock(EStockType eStockType) { |
| | | log.info("init US股票 数据 {}", eStockType.getCode()); |
| | | List<StockRealTimeBean> list = new ArrayList<>(); |
| | | int totleStock = 1; |
| | | int page = 0; |
| | | try { |
| | | while (totleStock > list.size()) { |
| | | try { |
| | | String result = HttpClientRequest.doGet(eStockType.stockUrl + "list?country_id=" + eStockType.getContryId() + "&size=100000&page=" + page + "&key=" + eStockType.stockKey); |
| | | ReponseBase reponseBase = new Gson().fromJson(result, ReponseBase.class); |
| | | list.addAll(reponseBase.getData()); |
| | | page++; |
| | | totleStock = reponseBase.getTotal(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | break; |
| | | } |
| | | } |
| | | if (list.isEmpty()) { |
| | | return; |
| | | } |
| | | /*list.forEach( x-> { |
| | | if (x.getSymbol().equalsIgnoreCase("IONM")) { |
| | | System.out.println(x); |
| | | System.out.println(x); |
| | | } |
| | | });*/ |
| | | Map<String, Object> paramMap = new HashMap<>(); |
| | | List<Item> stockList = itemService.list(new QueryWrapper<Item>() |
| | | .eq("type", Item.US_STOCKS)); |
| | | int batchSize = 5000; // 每批5000条 |
| | | int totalSize = stockList.size(); // 总条数 |
| | | |
| | | List<Item> indicesList = itemService.list(new QueryWrapper<Item>() |
| | | .eq("type", Item.indices)); |
| | | for (StockRealTimeBean o : list) { |
| | | Item indices = indicesList.stream() |
| | | .filter(x -> x.getSymbol().equals(o.getSymbol())) |
| | | .findFirst() |
| | | .orElse(null); |
| | | if (indices != null) { //指数不添加 |
| | | continue; |
| | | } |
| | | List<String> pids = new ArrayList<>(); |
| | | // 循环分批处理 |
| | | for (int i = 0; i < totalSize; i += batchSize) { |
| | | // 计算当前批次的结束索引(避免最后一批不足5000条时越界) |
| | | int endIndex = Math.min(i + batchSize, totalSize); |
| | | |
| | | Realtime realtime = new Realtime(); |
| | | realtime.setUuid(o.getId()); |
| | | realtime.setSymbol(o.getSymbol()); |
| | | realtime.setName(o.getName()); |
| | | realtime.setClose(new BigDecimal(o.getLast().trim()).doubleValue()); |
| | | realtime.setLow(new BigDecimal(o.getLow().trim()).doubleValue()); |
| | | realtime.setHigh(new BigDecimal(o.getHigh().trim()).doubleValue()); |
| | | realtime.setOpen(new BigDecimal(o.getOpen().trim()).doubleValue()); |
| | | realtime.setPrevClose(new BigDecimal(o.getPrevClose().trim()).doubleValue()); |
| | | realtime.setTs(Long.valueOf(o.getTime() + "000")); |
| | | realtime.setVolume(new BigDecimal(o.getVolume().trim()).doubleValue()); |
| | | realtime.setNetChange(new BigDecimal(o.getChg().trim()).doubleValue()); |
| | | realtime.setChangeRatio(new BigDecimal(o.getChgPct()).doubleValue()); |
| | | realtime.setType(o.getType()); |
| | | realtime.setBid(new BigDecimal(o.getBid()).doubleValue()); |
| | | realtime.setAsk(new BigDecimal(o.getAsk()).doubleValue()); |
| | | // 截取当前批次的子列表([i, endIndex),左闭右开) |
| | | List<Item> batchList = stockList.subList(i, endIndex); |
| | | |
| | | DataCache.putRealtime(realtime.getSymbol(), realtime); |
| | | // 拼接当前批次的pid(逗号分隔的stockCode) |
| | | String batchPid = batchList.stream() |
| | | .map(item -> String.valueOf(item.getStockCode())) // 提取stockCode并转字符串 |
| | | .collect(Collectors.joining(",")); // 拼接 |
| | | |
| | | pids.add(batchPid); |
| | | } |
| | | pids.forEach(pid -> { |
| | | paramMap.put("pid", pid); |
| | | String result = HttpClientRequest.doPost(eStockType.stockUrl + "stock?" + "key=" + eStockType.stockKey, paramMap); |
| | | // 定义List<StockRealTimeBean>的类型(通过TypeToken保留泛型信息) |
| | | Type type = new TypeToken<List<StockRealTimeBean>>() { |
| | | }.getType(); |
| | | // 解析为List<StockRealTimeBean> |
| | | List<StockRealTimeBean> stockRealTimeBeanList = new Gson().fromJson(result, type); |
| | | list.addAll(stockRealTimeBeanList); |
| | | }); |
| | | |
| | | stockList.forEach(item -> { |
| | | StockRealTimeBean o = list.stream().filter(x -> x.getId() != null && |
| | | x.getId().equals(item.getStockCode())).findFirst().orElse(null); |
| | | if (o != null) { |
| | | Realtime realtime = new Realtime(); |
| | | realtime.setUuid(item.getStockCode()); |
| | | realtime.setSymbol(item.getSymbol()); |
| | | realtime.setName(item.getName()); |
| | | realtime.setClose(new BigDecimal(o.getLast().trim()).doubleValue()); |
| | | realtime.setLow(new BigDecimal(o.getLow().trim()).doubleValue()); |
| | | realtime.setHigh(new BigDecimal(o.getHigh().trim()).doubleValue()); |
| | | realtime.setOpen(new BigDecimal(o.getOpen().trim()).doubleValue()); |
| | | realtime.setPrevClose(new BigDecimal(o.getPrevClose().trim()).doubleValue()); |
| | | realtime.setTs(Long.valueOf(o.getTime() + "000")); |
| | | realtime.setVolume(new BigDecimal(o.getVolume().trim()).doubleValue()); |
| | | realtime.setNetChange(new BigDecimal(o.getChg().trim()).doubleValue()); |
| | | realtime.setChangeRatio(new BigDecimal(o.getChgPct()).doubleValue()); |
| | | realtime.setType(o.getType()); |
| | | realtime.setBid(new BigDecimal(o.getBid()).doubleValue()); |
| | | realtime.setAsk(new BigDecimal(o.getAsk()).doubleValue()); |
| | | DataCache.putRealtime(realtime.getSymbol(), realtime); |
| | | } |
| | | }); |
| | | |
| | | log.info("init US股票 数据 成功"); |
| | | } catch ( |
| | | Exception e) { |
| | | log.error("同步出错", e); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage(), e); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | public static void main(String[] args) { |
| | | StockTask task = new StockTask(); |