@@ -1,57 +1,137 @@
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.ApiImplicitParam ;
import io.swagger.annotations.ApiImplicitParams ;
import io.swagger.annotations.ApiOperation ;
import io.swagger.annotations.ApiResponse ;
import io.swagger.annotations.ApiResponses ;
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.IO Exception ;
import java.util.* ;
import java.util.concurrent.ConcurrentHashMap ;
import java.text.Parse Exception ;
import java.text.SimpleDateFormat ;
import java.util.ArrayList ;
import java.util.Date ;
import java.util.List ;
import java.util.concurrent.TimeUnit ;
import java.util.function.Function ;
import java.util.function.Predicate ;
import java.util.stream.Collectors ;
import org.springframework.web.bind.annotation.GetMapping ;
import org.springframework.web.bind.annotation.RequestMapping ;
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数据源接口
*
*/
@RestController
@Api ( value = " /TwelveApiController " , tags = " twelvedata股票行情 " )
@Slf4j
@RequestMapping ( { " /api/market/twelvedata " , " /api/hq/twelvedata " } )
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 ;
}
}