继续优化代码

This commit is contained in:
Achilles
2024-01-06 18:31:20 +08:00
parent 7b0517cc33
commit d5a3fddbd4
3 changed files with 80 additions and 0 deletions

View File

@@ -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;
}

View File

@@ -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<Long> t;
private List<Double> o;
private List<Double> h;
private List<Double> l;
private List<Double> c;
private List<Long> v;
}

View File

@@ -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<StockHistoryResponse> 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&currencyCode=%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;