From 83999e53b1adafdde5a3ccd7bd734ea84a71e82b Mon Sep 17 00:00:00 2001 From: xiaoliuhu Date: Tue, 19 Dec 2023 09:48:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E6=94=B9=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/basic/service/StockService.java | 67 +++++++++++++++++++ .../api/investing/InvestingInvokerApis.java | 2 +- .../infrastructure/job/InvestingTask.java | 4 +- .../stock/market/web/StockApiController.java | 12 +++- 4 files changed, 80 insertions(+), 5 deletions(-) diff --git a/src/main/java/cn/stock/market/domain/basic/service/StockService.java b/src/main/java/cn/stock/market/domain/basic/service/StockService.java index 4059817..d462e13 100644 --- a/src/main/java/cn/stock/market/domain/basic/service/StockService.java +++ b/src/main/java/cn/stock/market/domain/basic/service/StockService.java @@ -13,8 +13,10 @@ import javax.servlet.http.HttpServletRequest; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.text.StrFormatter; import cn.stock.market.dto.model.*; +import cn.stock.market.infrastructure.api.investing.InvestingInvokerApis; import cn.stock.market.infrastructure.api.sina.vo.HotSearchVO; import cn.stock.market.utils.*; +import com.ag.utils.CollectionUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; @@ -741,4 +743,69 @@ public class StockService { builder.header(key, value); } } + public void syncIndiaData(){ + log.info("同步股票数据开始。。。。"); + Map stockMap = StockRepository.of().cacheCodeMap(); + Stopwatch stopwatch = Stopwatch.createStarted(); + List list = Lists.newArrayList(); + int currentTotal = 0; + int pageNum =1; + int pageSize = 300; + try { + for(int i = 0;i < pageNum;i ++) { + JSONObject jsonObject = InvestingInvokerApis.of().__page(pageNum, pageSize); + int total = 0; + if(jsonObject.containsKey("total")){ + total = Integer.parseInt(jsonObject.get("total").toString()); + } + JSONArray dataObjArray = new JSONArray(); + if(jsonObject.containsKey("data")){ + dataObjArray = JSON.parseArray(jsonObject.get("data").toString()); + } + for (Object obj : dataObjArray) { + JSONObject jsonObject2 = JSON.parseObject(obj.toString()); + String code = jsonObject2.get("Id").toString(); + String name = jsonObject2.get("Name").toString(); + String stockSymbol = jsonObject2.get("Symbol").toString(); + String exchangeId = jsonObject2.get("ExchangeId").toString(); + if (stockMap.containsKey(code)) { + log.info("已经存在 {} 信息, 跳过", code); + continue; + } + + if(! Utils.isShOrSzOrBJ(code)) { + log.info("{} 非 sh 或者 sz 或者 bj , 跳过", code); + continue; + } + Stock stock = new Stock(); + stock.setStockSymbol(stockSymbol); + stock.setStockExchangeId(exchangeId); + stock.setStockName(name); + stock.setStockCode(code); + stock.setIsLock(0); + stock.setIsShow(0); + stock.setAddTime(new Date()); + stock.setStockState(0); + list.add(stock); + } + currentTotal += pageSize; + if((total - currentTotal) < pageSize ){ + pageSize = total - currentTotal; + } + if(total == currentTotal){ + break; + } + pageNum ++; + } + if(CollectionUtils.isNotEmpty(list)) { + StockRepository.of().saveAll(list); + } + int count = list.size(); + log.info("syncAFutureStockList执行, 受影响数{}, 耗时:{}毫秒", count, stopwatch.elapsed(TimeUnit.MILLISECONDS)); + log.info("同步股票数据结束。。。。"); + } catch (Exception e) { + log.info("同步股票数据异常,异常信息{}。。。。",e.getMessage()); + } + + } } \ No newline at end of file diff --git a/src/main/java/cn/stock/market/infrastructure/api/investing/InvestingInvokerApis.java b/src/main/java/cn/stock/market/infrastructure/api/investing/InvestingInvokerApis.java index bb6457a..52cda30 100644 --- a/src/main/java/cn/stock/market/infrastructure/api/investing/InvestingInvokerApis.java +++ b/src/main/java/cn/stock/market/infrastructure/api/investing/InvestingInvokerApis.java @@ -203,7 +203,7 @@ public class InvestingInvokerApis { } public static void main(String[] args) throws IOException { - JSONObject __market = of().__market(StockCode.of("17988")); + JSONObject __market = of().__page(1,100); System.out.println(__market); JSONObject __page = of().__page(1, 10); diff --git a/src/main/java/cn/stock/market/infrastructure/job/InvestingTask.java b/src/main/java/cn/stock/market/infrastructure/job/InvestingTask.java index cb512db..008ea14 100644 --- a/src/main/java/cn/stock/market/infrastructure/job/InvestingTask.java +++ b/src/main/java/cn/stock/market/infrastructure/job/InvestingTask.java @@ -24,7 +24,7 @@ import java.util.concurrent.TimeUnit; @Component public class InvestingTask { - @Scheduled(cron = "0 0 5 * * ?") + @Scheduled(cron = "0 0 6 * * ?") public void syncIndiaData(){ log.info("同步股票数据开始。。。。"); Map stockMap = StockRepository.of().cacheCodeMap(); @@ -74,7 +74,7 @@ public class InvestingTask { if((total - currentTotal) < pageSize ){ pageSize = total - currentTotal; } - if(total == currentTotal){ + if(total <= currentTotal){ break; } pageNum ++; diff --git a/src/main/java/cn/stock/market/web/StockApiController.java b/src/main/java/cn/stock/market/web/StockApiController.java index a0462d7..f37b03c 100644 --- a/src/main/java/cn/stock/market/web/StockApiController.java +++ b/src/main/java/cn/stock/market/web/StockApiController.java @@ -642,9 +642,17 @@ public class StockApiController { @ApiOperation(value = "新股待上市接口",httpMethod = "GET", response = ServerResponse.class) @RequestMapping({"getNewStockList.do"}) @ResponseBody - public - ServerResponse getNewStockList() { + public ServerResponse getNewStockList() { return this.stockService.getNewStockList(); } + + @ApiOperation(value = "印度股票入库",httpMethod = "GET", response = ServerResponse.class) + @RequestMapping({"syncIndiaData.do"}) + @ResponseBody + public ServerResponse syncIndiaData() { + + stockService.syncIndiaData(); + return ServerResponse.createBySuccess(); + } } \ No newline at end of file