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 | 261 ++++++++++++++++++++++++++++++++++++++++-----------
1 files changed, 203 insertions(+), 58 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 92b2db7..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,97 +1,223 @@
package com.nq.utils.task.stock;
-import cn.hutool.json.JSONUtil;
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.nq.dao.*;
+import com.google.gson.reflect.TypeToken;
+import com.nq.Repository.StockRepository;
+import com.nq.dao.StockMapper;
+import com.nq.dao.UserPositionMapper;
import com.nq.enums.EStockType;
import com.nq.pojo.*;
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 com.nq.utils.redis.RedisShardedPoolUtils;
-import com.nq.utils.stock.BuyAndSellUtils;
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.math.BigDecimal;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
+import java.lang.reflect.Type;
+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;
+import java.util.stream.Collectors;
@Component
public class StockTask {
@Autowired
- IStockService stockService;
- @Autowired
StockMapper stockMapper;
+ @Autowired
+ IStockService istockService;
@Autowired
- IUserPositionService userPositionService;
-
- @Autowired
- UserPositionMapper userPositionMapper;
+ 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 0/2 * * ?")
+ */
+ @Scheduled(cron = "0 0/1 * * * ?")
public void syncINStockData() {
- loadAllStock(EStockType.IN);
-// loadAllStock(EStockType.HK);
+
+ if (syncINStockData.get()) { // 判断任务是否在处理中
+ return;
+ }
+ if (syncINStockDataLock.tryLock()) {
+ try {
+ syncINStockData.set(true);
+
+ // 使用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);
+
+ // 等待所有任务完成
+ CompletableFuture.allOf(future1, future2, future3, future4).join();
+ } catch (Exception e) {
+ log.error("同步股票数据出错", e);
+ } finally {
+ syncINStockDataLock.unlock();
+ syncINStockData.set(false);
+ }
+ }
}
+ /**
+ * 加载所有指数数据
+ */
+ private void syncIndices(EStockType eStockType) {
+ List<DataStockBean> list = new ArrayList<>();
+ int totleStock = 1;
+ try {
+ while (totleStock > list.size()) {
+ try {
+ String result = HttpRequest.doGrabGet(eStockType.stockUrl + "indices?key=" + eStockType.getStockKey() + "&country_id=" + eStockType.getContryId());
+ // 把JSON数据解析为List<DataStockBean>
+ Type listType = new TypeToken<List<DataStockBean>>(){}.getType();
+ list = new Gson().fromJson(result, listType);
+ totleStock = list.size();
+ } catch (Exception e) {
+ e.printStackTrace();
+ break;
+ }
+ }
+ if (list.isEmpty()) {
+ return;
+ }
+ List<String> stockCodeList = list.stream().map(DataStockBean::getId).collect(Collectors.toList());
+ List<Stock> stockList = stockMapper.selectList(new QueryWrapper<Stock>().in("stock_code", stockCodeList));
+ List<Stock> updateStockList = new ArrayList<>();
+ for (DataStockBean o : list) {
+ Stock stock = stockList.stream()
+ .filter(x -> x.getStockCode().equals(o.getId()))
+ .findFirst()
+ .orElse(null);
+ if (stock == null) {
+ stock = new Stock();
+ }
+ stock.setStockCode(o.getId());
+ stock.setStockName(o.getName());
+ stock.setStockType(eStockType.getCode());
+ if (o.getType() == null) {
+ stock.setStockGid(eStockType.getCode());
+ } else {
+ stock.setStockGid(o.getType());
+ }
+ stock.setStockSpell(o.getSymbol());
+ stock.setIsLock(0);
+ stock.setIsShow(0);
+ stock.setDataBase(0);
+ stock.setAddTime(new Date());
+ updateStockList.add(stock);
+ /*if (stock.getId() == null) {
+ stockMapper.insert1(stock);
+ } else {
+ 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());
+ }
/**
* 同步美国股票
- * */
+ */
// @Scheduled(cron = "0 0/30 * * * ?")
- public void loadStockCompanies(){
+ public void loadStockCompanies() {
loadAllCompanies();
}
-
-
/**
* 加载公司信息
- * */
- public void loadAllCompanies(){
- List<Stock> list = stockMapper.findStockList();
- for (int i = 0; i <list.size() ; i++) {
+ */
+ public void loadAllCompanies() {
+ List<Stock> list = stockMapper.findStockList();
+ for (int i = 0; i < list.size(); i++) {
Stock stock = list.get(i);
EStockType eStockType = EStockType.getEStockTypeByCode(stock.getStockType());
- String result = HttpClientRequest.doGet(eStockType.stockUrl+"companies?pid=+"+stock.getStockCode()+"+country_id="+eStockType.getContryId()+"&size=1&page=1&key="+eStockType.stockKey);
+ String result = HttpClientRequest.doGet(eStockType.stockUrl + "companies?pid=+" + stock.getStockCode() + "+country_id=" + eStockType.getContryId() + "&size=1&page=1&key=" + eStockType.stockKey);
try {
JSONObject jsonObject = JSONObject.parseObject(result);
JSONObject companiesInfo = jsonObject.getJSONArray("data").getJSONObject(0);
- RedisKeyUtil.setCacheCompanies(stock,new Gson().toJson(companiesInfo));
- }catch (Exception e){
+ RedisKeyUtil.setCacheCompanies(stock, new Gson().toJson(companiesInfo));
+ } catch (Exception e) {
log.info("");
}
@@ -101,35 +227,45 @@
/**
* 加载所有股票数据
- * */
- public void loadAllStock(EStockType eStockType){
- log.info("同步股票 数据 {}",eStockType.getCode());
+ */
+ public void loadAllStock(EStockType eStockType) {
+ log.info("同步股票 数据 {}", eStockType.getCode());
List<DataStockBean> list = new ArrayList<>();
int totleStock = 1;
- int page =0;
+ int page = 0;
try {
- while (totleStock>list.size()){
- try{
- String result = HttpClientRequest.doGet(eStockType.stockUrl+"list?country_id="+eStockType.getContryId()+"&size=1000&page="+page+"&key="+eStockType.stockKey);
- ReponseBase reponseBase = new Gson().fromJson(result,ReponseBase.class);
+ 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){
+ } catch (Exception e) {
e.printStackTrace();
break;
}
}
+ if (list.isEmpty()) {
+ return;
+ }
+ List<String> stockCodeList = list.stream().map(DataStockBean::getId).collect(Collectors.toList());
+ List<Stock> stockList = stockMapper.selectList(new QueryWrapper<Stock>().in("stock_code", stockCodeList));
+ List<Stock> updateStockList = new ArrayList<>();
for (DataStockBean o : list) {
- Stock stock = stockMapper.findStockByCode(o.getId());
+ //Stock stock = stockMapper.findStockByCode(o.getId());
+ Stock stock = stockList.stream()
+ .filter(x -> x.getStockCode().equals(o.getId()))
+ .findFirst()
+ .orElse(null);
if (stock == null) {
stock = new Stock();
stock.setStockCode(o.getId());
stock.setStockName(o.getName());
stock.setStockType(eStockType.getCode());
- if(o.getType() == null){
+ if (o.getType() == null) {
stock.setStockGid(eStockType.getCode());
- }else{
+ } else {
stock.setStockGid(o.getType());
}
stock.setStockSpell(o.getSymbol());
@@ -137,14 +273,14 @@
stock.setIsShow(0);
stock.setDataBase(0);
stock.setAddTime(new Date());
- stockMapper.insert1(stock);
- }else{
+ //stockMapper.insert1(stock);
+ } else {
stock.setStockCode(o.getId());
stock.setStockName(o.getName());
stock.setStockType(eStockType.getCode());
- if(o.getType() == null){
+ if (o.getType() == null) {
stock.setStockGid(eStockType.getCode());
- }else{
+ } else {
stock.setStockGid(o.getType());
}
stock.setStockSpell(o.getSymbol());
@@ -152,26 +288,34 @@
stock.setIsShow(0);
stock.setDataBase(0);
stock.setAddTime(new Date());
- stockMapper.updateById(stock);
+ //stockMapper.updateById(stock);
}
- RedisKeyUtil.setCaCheKeyBaseStock(eStockType,o);
+ updateStockList.add(stock);
+ RedisKeyUtil.setCaCheKeyBaseStock(eStockType, o);
}
- log.info("同步股票 数据 成功 {} 总共同步数据 {}",eStockType.getCode(),list.size());
+ stockRepository.saveAll(updateStockList);
+ log.info("同步股票 数据 成功 {} 总共同步数据 {}", eStockType.getCode(), list.size());
} catch (
Exception e) {
log.error("同步出错", e);
}
}
+ private final AtomicBoolean stockConstraint = new AtomicBoolean(false);
+
/**
* 强制平仓
*/
- @Scheduled(cron = "0/1 * * * * ?")
- public void stockConstraint(){
+// @Scheduled(cron = "0/1 * * * * ?")
+ /*public void stockConstraint() {
+ if (stockConstraint.get()) { // 判断任务是否在处理中
+ return;
+ }
if (stockConstraintLock.tryLock()) {
try {
+ stockConstraint.set(true); // 设置处理中标识为true
List<UserPosition> userPositions = userPositionMapper.selectList(new LambdaQueryWrapper<UserPosition>().isNull(UserPosition::getSellOrderId));
- if(CollectionUtils.isNotEmpty(userPositions)){
+ if (CollectionUtils.isNotEmpty(userPositions)) {
userPositionService.stockConstraint(userPositions);
}
} catch (Exception e) {
@@ -179,9 +323,10 @@
log.error("强制平仓任务错误:" + e.getMessage());
} finally {
stockConstraintLock.unlock();
+ stockConstraint.set(false); // 设置处理中标识为false
}
} else {
log.info("强制平仓任务--------->上次任务还未执行完成,本次任务忽略");
}
- }
+ }*/
}
--
Gitblit v1.9.3