详情查询、详情列表查询

This commit is contained in:
zhanghuilong
2024-05-31 18:11:23 +08:00
parent 67a3d6fd74
commit f565132607
5 changed files with 126 additions and 40 deletions

View File

@@ -18,10 +18,12 @@ import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Stopwatch; import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import cn.qutaojing.common.utils.SpringUtils;
import cn.stock.market.domain.basic.entity.TwelveStock; import cn.stock.market.domain.basic.entity.TwelveStock;
import cn.stock.market.domain.basic.service.TwelveStockService; import cn.stock.market.domain.basic.service.TwelveStockService;
import cn.stock.market.dto.TwelveStockData; import cn.stock.market.dto.TwelveStockData;
import cn.stock.market.utils.HttpRequest; import cn.stock.market.utils.HttpRequest;
import cn.stock.market.web.config.Config;
@Slf4j @Slf4j
@Component @Component
@@ -78,7 +80,8 @@ public class TwelvedataTask {
String country = "India"; String country = "India";
List<TwelveStockData> list = Lists.newArrayList(); List<TwelveStockData> list = Lists.newArrayList();
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
String url = String.format("https://api.twelvedata.com/stocks?source=%s&country=%s&apikey=%s",source,country,"4098a6fc2c3245a88c7da804f10223c2"); Config config = SpringUtils.getBean(Config.class);
String url = String.format("https://api.twelvedata.com/stocks?source=%s&country=%s&apikey=%s",source,country,config.getTwelveApikey());
String result = HttpRequest.doTwelveGet(url); String result = HttpRequest.doTwelveGet(url);
log.info("getStockList获取 {} {} 的股票全量列表结束,耗时: {} ms ",source,country,(System.currentTimeMillis()-start)); log.info("getStockList获取 {} {} 的股票全量列表结束,耗时: {} ms ",source,country,(System.currentTimeMillis()-start));
JSONObject jsonObject = JSONObject.parseObject(result); JSONObject jsonObject = JSONObject.parseObject(result);

View File

@@ -1,57 +1,137 @@
package cn.stock.market.web; package cn.stock.market.web;
import cn.hutool.core.date.DateUtil;
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 cn.stock.market.utils.RequestCacheUtils;
import cn.stock.market.utils.ServerResponse;
import cn.stock.market.web.annotations.EncryptFilter;
import com.alibaba.fastjson.JSONObject;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.io.IOException; import java.text.ParseException;
import java.util.*; import java.text.SimpleDateFormat;
import java.util.concurrent.ConcurrentHashMap; import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.function.Function; import org.springframework.web.bind.annotation.GetMapping;
import java.util.function.Predicate; import org.springframework.web.bind.annotation.RequestMapping;
import java.util.stream.Collectors; import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.StringUtil;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import cn.qutaojing.common.utils.SpringUtils;
import cn.stock.market.dto.StockHistoryResponse;
import cn.stock.market.dto.TwelveStockInfo;
import cn.stock.market.utils.HttpRequest;
import cn.stock.market.utils.ServerResponse;
import cn.stock.market.web.annotations.EncryptFilter;
import cn.stock.market.web.config.Config;
/** /**
* twelvedata数据源接口 * twelvedata数据源接口
*
*/ */
@RestController @RestController
@Api(value = "/TwelveApiController", tags = "twelvedata股票行情") @Api(value = "/TwelveApiController", tags = "twelvedata股票行情")
@Slf4j @Slf4j
@RequestMapping({"/api/market/twelvedata", "/api/hq/twelvedata"})
public class TwelveApiController { public class TwelveApiController {
@ApiOperation(value = "股票详情信息", httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(name = "stockCode",value = "股票对应代码",dataType ="String",required = true, paramType = "query"),
@ApiImplicitParam(name = "stockType",value = "股票对应代码:BSE/NSE",dataType ="String",required = true, paramType = "query")
})
@GetMapping("/getStockDetail")
@EncryptFilter(decryptRequest = false)
public ServerResponse<?> getStockDetail(String stockCode,String stockType) {
try {
long start = System.currentTimeMillis();
Config config = SpringUtils.getBean(Config.class);
String url = String.format("https://api.twelvedata.com/quote?symbol=%s&exchange=%s&apikey=%s",stockCode,stockType,config.getTwelveApikey());
String result = HttpRequest.doTwelveGet(url);
if(StringUtil.isNotEmpty(result)) {
JSONObject data = JSONObject.parseObject(result);
TwelveStockInfo info = mapJsonToTwelveStock(data);
log.info("getStockDetail获取 {}, {} 的股票详情结束,耗时: {} ms ",stockCode,stockType,(System.currentTimeMillis()-start));
return ServerResponse.createBySuccess("操作成功", info);
}
} catch (Exception e) {
log.error("获取股票详情参数:{},{}. Exception:{}" , stockCode,stockType, e);
}
return ServerResponse.createBySuccess("操作成功");
}
private TwelveStockInfo mapJsonToTwelveStock(JSONObject jsonObject) {
TwelveStockInfo info = new TwelveStockInfo();
if (jsonObject != null && !jsonObject.isEmpty()) {
info.setStockCode(jsonObject.getString("symbol"));
info.setSymbol(jsonObject.getString("symbol"));
info.setStockName(jsonObject.getString("name"));
info.setStockType(jsonObject.getString("exchange"));
info.setLastPrice(jsonObject.getString("close"));
info.setPerchg(jsonObject.getString("percent_change"));
info.setChange(jsonObject.getString("change"));
info.setOpenPrice(jsonObject.getString("open"));
info.setPreviousPrice(jsonObject.getString("previous_close"));
info.setVolume(jsonObject.getString("volume"));
info.setHighPrice(jsonObject.getString("high"));
info.setLowPrice(jsonObject.getString("low"));
JSONObject json = jsonObject.getJSONObject("fifty_two_week");
if(json!=null && !json.isEmpty()) {
info.setWeek52HighPrice(json.getString("high"));
info.setWeek52LowPrice(json.getString("low"));
}
}
return info;
}
@ApiOperation(value = "股票详情列表", httpMethod = "GET")
@ApiImplicitParams({
@ApiImplicitParam(name = "stockCodes",value = "股票对应代码, 多个时以英文逗号分隔",dataType ="String",required = true, paramType = "query"),
@ApiImplicitParam(name = "stockType",value = "股票对应代码:BSE/NSE",dataType ="String",required = true, paramType = "query")
})
@GetMapping("/getStockDetailList")
@EncryptFilter(decryptRequest = false)
public ServerResponse<?> getStockDetailList(String stockCodes,String stockType) {
try {
long start = System.currentTimeMillis();
Config config = SpringUtils.getBean(Config.class);
String url = String.format("https://api.twelvedata.com/quote?symbol=%s&exchange=%s&apikey=%s",stockCodes,stockType,config.getTwelveApikey());
String result = HttpRequest.doTwelveGet(url);
if(StringUtil.isNotEmpty(result)) {
JSONObject data = JSONObject.parseObject(result);
List<TwelveStockInfo> list = mapJsonToTwelveList(stockCodes,data);
log.info("getStockDetailList获取 {}, {} 的股票列表详情结束,耗时: {} ms ",stockCodes,stockType,(System.currentTimeMillis()-start));
return ServerResponse.createBySuccess("操作成功", list);
}
} catch (Exception e) {
log.error("获取股票详情参数:{},{}. Exception:{}" , stockCodes,stockType, e);
}
return ServerResponse.createBySuccess("操作成功");
}
private List<TwelveStockInfo> mapJsonToTwelveList(String stockCodes,JSONObject jsonObject) {
if (jsonObject == null || jsonObject.isEmpty()) {
return new ArrayList<>();
}
List<TwelveStockInfo> list = new ArrayList<>();
String[] items = stockCodes.split(",");
if(items!=null && items.length>0) {
if(items.length == 1) {
TwelveStockInfo info = mapJsonToTwelveStock(jsonObject);
list.add(info);
return list;
}
for (String str : items) {
JSONObject json = jsonObject.getJSONObject(str);
TwelveStockInfo vo = mapJsonToTwelveStock(json);
list.add(vo);
}
}
return list;
}
} }

View File

@@ -39,4 +39,5 @@ public class Config {
String aliyunAccessKeyId; String aliyunAccessKeyId;
String aliyunAccessKeySecret; String aliyunAccessKeySecret;
String aliyunAppCode; String aliyunAppCode;
String twelveApikey;
} }

View File

@@ -42,6 +42,7 @@ gugudataAppKey: K5LZKV8KAYM4
aliyunAccessKeyId: LTAI5tJi2z8cegG8fTW7BSQu aliyunAccessKeyId: LTAI5tJi2z8cegG8fTW7BSQu
aliyunAccessKeySecret: fnCI9LUcqLuH7D6nkhvSHQMob1JSm8 aliyunAccessKeySecret: fnCI9LUcqLuH7D6nkhvSHQMob1JSm8
aliyunAppCode: 75ef2615da614eaaa71e2e2058fc53b0 aliyunAppCode: 75ef2615da614eaaa71e2e2058fc53b0
twelveApikey: 4098a6fc2c3245a88c7da804f10223c2
# 具体看类:CloudStorageConfig # 具体看类:CloudStorageConfig
oss: oss:
type: 2 # 类型 1七牛 2阿里云 3腾讯云 type: 2 # 类型 1七牛 2阿里云 3腾讯云

View File

@@ -46,6 +46,7 @@ gugudataAppKey: K5LZKV8KAYM4
aliyunAccessKeyId: LTAI5tCQRp7t158TVyoF2Yhe aliyunAccessKeyId: LTAI5tCQRp7t158TVyoF2Yhe
aliyunAccessKeySecret: glyzOno773Cv7HeWDliveu2H5gX486 aliyunAccessKeySecret: glyzOno773Cv7HeWDliveu2H5gX486
aliyunAppCode: 75ef2615da614eaaa71e2e2058fc53b0 aliyunAppCode: 75ef2615da614eaaa71e2e2058fc53b0
twelveApikey: 4098a6fc2c3245a88c7da804f10223c2
# 具体看类:CloudStorageConfig # 具体看类:CloudStorageConfig
oss: oss:
type: 2 # 类型 1七牛 2阿里云 3腾讯云 type: 2 # 类型 1七牛 2阿里云 3腾讯云