From d5a3fddbd46c6656a4b1e2004b323ff4770c6ce9 Mon Sep 17 00:00:00 2001 From: Achilles Date: Sat, 6 Jan 2024 18:31:20 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=A7=E7=BB=AD=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stock/market/dto/StockHistoryRequest.java | 14 ++++++ .../market/dto/StockHistoryResponse.java | 17 +++++++ .../stock/market/web/MoneyApiController.java | 49 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/main/java/cn/stock/market/dto/StockHistoryRequest.java create mode 100644 src/main/java/cn/stock/market/dto/StockHistoryResponse.java diff --git a/src/main/java/cn/stock/market/dto/StockHistoryRequest.java b/src/main/java/cn/stock/market/dto/StockHistoryRequest.java new file mode 100644 index 0000000..ef4c781 --- /dev/null +++ b/src/main/java/cn/stock/market/dto/StockHistoryRequest.java @@ -0,0 +1,14 @@ +package cn.stock.market.dto; + +import lombok.Data; + +@Data +public class StockHistoryRequest { + private String symbol; + private String resolution; + private long from; + private long to; + private int countback; + private String currencyCode; + +} \ No newline at end of file diff --git a/src/main/java/cn/stock/market/dto/StockHistoryResponse.java b/src/main/java/cn/stock/market/dto/StockHistoryResponse.java new file mode 100644 index 0000000..66d27aa --- /dev/null +++ b/src/main/java/cn/stock/market/dto/StockHistoryResponse.java @@ -0,0 +1,17 @@ +package cn.stock.market.dto; + +import lombok.Data; + +import java.util.List; + +@Data +public class StockHistoryResponse { + private String s; + private List t; + private List o; + private List h; + private List l; + private List c; + private List v; + +} \ No newline at end of file diff --git a/src/main/java/cn/stock/market/web/MoneyApiController.java b/src/main/java/cn/stock/market/web/MoneyApiController.java index 1c3558c..046f7a5 100644 --- a/src/main/java/cn/stock/market/web/MoneyApiController.java +++ b/src/main/java/cn/stock/market/web/MoneyApiController.java @@ -3,6 +3,8 @@ package cn.stock.market.web; import cn.stock.market.MoneyStockSuggestDTO; import cn.stock.market.domain.basic.entity.MoneyStock; import cn.stock.market.domain.basic.repository.MoneyStockRepository; +import cn.stock.market.dto.StockHistoryRequest; +import cn.stock.market.dto.StockHistoryResponse; import cn.stock.market.infrastructure.db.po.QMoneyStockPO; import com.alibaba.fastjson.JSONObject; import com.google.common.collect.Lists; @@ -49,6 +51,9 @@ public class MoneyApiController { @Autowired private MoneyStockRepository moneyStockRepository; + private static final String EXTERNAL_API_URL = "https://priceapi.moneycontrol.com/techCharts/indianMarket/stock/history"; + + @ApiOperation(value = "股票详情信息",httpMethod = "GET") @ApiImplicitParams({ @@ -576,6 +581,50 @@ public class MoneyApiController { } + + @GetMapping("/api/market/money/history/kLine") + @ApiOperation(value = "获取kline的money数据源", notes = "获取kline的money数据源",response = StockHistoryResponse.class) + @ApiImplicitParams({ + @ApiImplicitParam(name = "symbol", value = "Stock symbol", required = true, dataType = "String", paramType = "query"), + @ApiImplicitParam(name = "resolution", value = "Resolution", required = true, dataType = "String", paramType = "query"), + @ApiImplicitParam(name = "from", value = "Start timestamp", required = true, dataType = "long", paramType = "query"), + @ApiImplicitParam(name = "to", value = "End timestamp", required = true, dataType = "long", paramType = "query"), + @ApiImplicitParam(name = "countback", value = "Countback", required = true, dataType = "int", paramType = "query"), + @ApiImplicitParam(name = "currencyCode", value = "Currency code", required = true, dataType = "String", paramType = "query") + }) + @ResponseBody + public ResponseEntity getStockHistory( @RequestParam String symbol, + @RequestParam String resolution, + @RequestParam long from, + @RequestParam long to, + @RequestParam int countback, + @RequestParam String currencyCode) { + // 创建一个RestTemplate实例 + RestTemplate restTemplate = new RestTemplate(); + // 向外部API发起请求,并获取响应 + StockHistoryRequest request = new StockHistoryRequest(); + request.setSymbol(symbol); + request.setResolution(resolution); + request.setFrom(from); + request.setTo(to); + request.setCountback(countback); + request.setCurrencyCode(currencyCode); + String apiUrl = buildApiUrl(request); + StockHistoryResponse response = restTemplate.getForObject(apiUrl, StockHistoryResponse.class); + + // 返回响应 + return ResponseEntity.ok(response); + } + + + private String buildApiUrl(StockHistoryRequest request) { + // 构建外部API的URL + return String.format("%s?symbol=%s&resolution=%s&from=%d&to=%d&countback=%d¤cyCode=%s", + EXTERNAL_API_URL, request.getSymbol(), request.getResolution(), request.getFrom(), + request.getTo(), request.getCountback(), request.getCurrencyCode()); + }; + + private static String extractLastSegment(String url) { if (url == null) { return null;