From 5c5d5112e7b999381f8b20c97e53314e6cedada2 Mon Sep 17 00:00:00 2001
From: zyy <zyy@email.com>
Date: Wed, 09 Jul 2025 18:34:52 +0800
Subject: [PATCH] 股票主页、市场页面接口
---
src/main/java/com/nq/utils/task/stock/StockTask.java | 121 ++++++++++++++++++++++------------------
1 files changed, 67 insertions(+), 54 deletions(-)
diff --git a/src/main/java/com/nq/utils/task/stock/StockTask.java b/src/main/java/com/nq/utils/task/stock/StockTask.java
index f7520a6..ec13638 100644
--- a/src/main/java/com/nq/utils/task/stock/StockTask.java
+++ b/src/main/java/com/nq/utils/task/stock/StockTask.java
@@ -1,13 +1,8 @@
package com.nq.utils.task.stock;
import com.alibaba.fastjson2.JSONObject;
-import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
-import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.gson.Gson;
-import com.google.gson.JsonArray;
-import com.google.gson.JsonElement;
-import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.nq.Repository.StockRepository;
import com.nq.dao.StockMapper;
@@ -17,24 +12,21 @@
import com.nq.service.IMandatoryLiquidationService;
import com.nq.service.IStockService;
import com.nq.service.IUserPositionService;
+import com.nq.service.impl.StockServiceImpl;
import com.nq.utils.PropertiesUtil;
import com.nq.utils.http.HttpClientRequest;
import com.nq.utils.http.HttpRequest;
import com.nq.utils.redis.RedisKeyUtil;
-import net.sf.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import java.lang.reflect.Type;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
+import java.util.*;
+import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
@@ -44,85 +36,79 @@
@Component
public class StockTask {
@Autowired
- IStockService stockService;
- @Autowired
StockMapper stockMapper;
-
@Autowired
- IUserPositionService userPositionService;
-
- @Autowired
- UserPositionMapper userPositionMapper;
+ IStockService istockService;
@Autowired
StockRepository stockRepository;
private final Lock stockConstraintLock = new ReentrantLock();
-
@Autowired
IMandatoryLiquidationService mandatoryLiquidationService;
-
private static final Logger log = LoggerFactory.getLogger(StockTask.class);
-
-
private final AtomicBoolean syncINStockData = new AtomicBoolean(false);
private final Lock syncINStockDataLock = new ReentrantLock();
+
+ @Autowired
+ private ThreadPoolTaskExecutor taskExecutor;
+ @Autowired
+ private StockServiceImpl iStockService;
+
+ /**
+ * test
+ */
+ //@Scheduled(cron = "0 0/1 * * * ?")
+ @Scheduled(cron = "*/5 * * * * *")
+ public void test() {
+ //iStockService.getStockByType(1, 20, "desc","st" , "US", null);
+ //istockService.getIndicesList("US");
+ //istockService.getIndicesAndKData("15882", "US");
+ }
/**
* 同步系统所需要的股票
*/
@Scheduled(cron = "0 0/1 * * * ?")
public void syncINStockData() {
+
if (syncINStockData.get()) { // 判断任务是否在处理中
return;
}
if (syncINStockDataLock.tryLock()) {
- ExecutorService executor = Executors.newFixedThreadPool(4);
- Future<?> future1 = null;
- Future<?> future2 = null;
- Future<?> future3 = null;
- Future<?> future4 = null;
try {
- syncINStockData.set(true); // 设置处理中标识为true
+ syncINStockData.set(true);
- // 同步股票数据
- future1 = executor.submit(() -> loadAllStock(EStockType.US));
- future2 = executor.submit(() -> loadAllStock(EStockType.MX));
- // 同步指数数据
- future3 = executor.submit(() -> syncIndices(EStockType.US));
- future4 = executor.submit(() -> syncIndices(EStockType.MX));
+ // 使用CompletableFuture并行执行任务
+ CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> loadAllStock(EStockType.US), taskExecutor);
+ CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> loadAllStock(EStockType.MX), taskExecutor);
+ CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> syncIndices(EStockType.US), taskExecutor);
+ CompletableFuture<Void> future4 = CompletableFuture.runAsync(() -> syncIndices(EStockType.MX), taskExecutor);
- // 等待任务都完成
- future1.get();
- future2.get();
- future3.get();
- future4.get();
+ // 等待所有任务完成
+ CompletableFuture.allOf(future1, future2, future3, future4).join();
} catch (Exception e) {
- Thread.currentThread().interrupt();
log.error("同步股票数据出错", e);
} finally {
- // 关闭线程池
- if (executor != null) {
- executor.shutdown();
- }
syncINStockDataLock.unlock();
- syncINStockData.set(false); // 设置处理中标识为false
+ syncINStockData.set(false);
}
}
}
+ /**
+ * 加载所有指数数据
+ */
private void syncIndices(EStockType eStockType) {
List<DataStockBean> list = new ArrayList<>();
int totleStock = 1;
try {
while (totleStock > list.size()) {
try {
- //String result = HttpClientRequest.doGet(eStockType.stockUrl + "indices?key=" + eStockType.getStockKey() + "&country_id=" + eStockType.getContryId());
- //ReponseBase reponseBase = new Gson().fromJson(result, ReponseBase.class);
String result = HttpRequest.doGrabGet(eStockType.stockUrl + "indices?key=" + eStockType.getStockKey() + "&country_id=" + eStockType.getContryId());
// 把JSON数据解析为List<DataStockBean>
Type listType = new TypeToken<List<DataStockBean>>(){}.getType();
@@ -169,12 +155,44 @@
stockMapper.updateById(stock);
}*/
RedisKeyUtil.setCaCheKeyBaseStock(eStockType, o);
+
+ /*StockRealTimeBean stockRealTimeBean = new StockRealTimeBean();
+ stockRealTimeBean.setPid(o.getId());
+ stockRealTimeBean.setLast(o.getLast());
+ stockRealTimeBean.setHigh(o.getHigh());
+ stockRealTimeBean.setLow(o.getLow());
+ stockRealTimeBean.setPc(o.getChg());
+ stockRealTimeBean.setPcp(o.getChgPct()+ "%");
+ stockRealTimeBean.setTime(o.getTime());
+ RedisKeyUtil.setCacheRealTimeStock(eStockType, stockRealTimeBean);*/
}
stockRepository.saveAll(updateStockList);
+ cacheKData(eStockType.getCode(), list);
log.info("同步指数 数据 成功 {} 总共同步数据 {}", eStockType.getCode(), list.size());
} catch (Exception e) {
+ e.printStackTrace();
log.error("同步指数列表出现异常: {}", e.getMessage());
}
+ }
+
+ /**
+ * 同步指数股票后缓存k线图
+ */
+ public void cacheKData(String stockType, List<DataStockBean> list) {
+ String usCodeList = PropertiesUtil.getProperty("us_home_indices_code");
+ String mxCodeList = PropertiesUtil.getProperty("mx_home_indices_code");
+ for (DataStockBean dataStockBean : list) {
+ //缓存首页指数k线图
+ if (usCodeList.contains(dataStockBean.getId()) || mxCodeList.contains(dataStockBean.getId())) {
+ // 获取K线数据
+ Object kData = istockService.getKData(dataStockBean.getId(), "D", stockType);
+ if (kData != null) {
+ //缓存redis
+ RedisKeyUtil.setCaCheStockKData(stockType, dataStockBean.getId(), kData);
+ }
+ }
+ }
+ log.info("同步指数k线图 数据 成功 {} 总共同步数据 {}", stockType, list.size());
}
/**
@@ -218,7 +236,7 @@
try {
while (totleStock > list.size()) {
try {
- String result = HttpClientRequest.doGet(eStockType.stockUrl + "list?country_id=" + eStockType.getContryId() + "&size=1000&page=" + page + "&key=" + eStockType.stockKey);
+ 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++;
@@ -233,9 +251,7 @@
}
List<String> stockCodeList = list.stream().map(DataStockBean::getId).collect(Collectors.toList());
List<Stock> stockList = stockMapper.selectList(new QueryWrapper<Stock>().in("stock_code", stockCodeList));
- int i = 1;
List<Stock> updateStockList = new ArrayList<>();
- System.out.println(new Date());
for (DataStockBean o : list) {
//Stock stock = stockMapper.findStockByCode(o.getId());
Stock stock = stockList.stream()
@@ -276,11 +292,8 @@
}
updateStockList.add(stock);
RedisKeyUtil.setCaCheKeyBaseStock(eStockType, o);
- System.out.println(i);
- i++;
}
stockRepository.saveAll(updateStockList);
- System.out.println(new Date());
log.info("同步股票 数据 成功 {} 总共同步数据 {}", eStockType.getCode(), list.size());
} catch (
Exception e) {
--
Gitblit v1.9.3