diff --git a/src/main/java/cn/stock/market/web/MoneyApiController.java b/src/main/java/cn/stock/market/web/MoneyApiController.java index 777f17f..e7ed26c 100644 --- a/src/main/java/cn/stock/market/web/MoneyApiController.java +++ b/src/main/java/cn/stock/market/web/MoneyApiController.java @@ -60,11 +60,11 @@ public class MoneyApiController { private static final String EXTERNAL_API_URL = "https://priceapi.moneycontrol.com/techCharts/indianMarket/stock/history"; - @ApiOperation(value = "股票详情信息",httpMethod = "GET") + @ApiOperation(value = "股票详情信息", httpMethod = "GET") @ApiImplicitParams({ - @ApiImplicitParam(name="stockType",value = "BSE或者NSE"), - @ApiImplicitParam(name="symbol",value = "scId值"), - @ApiImplicitParam(name="id",value = "id值"), + @ApiImplicitParam(name = "stockType", value = "BSE或者NSE"), + @ApiImplicitParam(name = "symbol", value = "scId值"), + @ApiImplicitParam(name = "id", value = "id值"), }) @ApiResponses(value = { @ApiResponse(code = 200, message = "" + @@ -163,66 +163,67 @@ public class MoneyApiController { "priceprevclose: 前一交易日的收盘价\n" + "30DayAvg: 过去30天的平均", response = JSONObject.class), }) - @GetMapping({"/market/api/market/money/getStockDetail","/api/market/money/getStockDetail"}) + @GetMapping({"/market/api/market/money/getStockDetail", "/api/market/money/getStockDetail"}) @ResponseBody @EncryptFilter(decryptRequest = false) - public ServerResponse getStockDetail(@RequestParam String stockType, @RequestParam String symbol ) { + public ServerResponse getStockDetail(@RequestParam String stockType, @RequestParam String symbol) { MoneyStock moneyStock = moneyStockRepository.findOne(QMoneyStockPO.moneyStockPO.stockType.eq(stockType) - .and(QMoneyStockPO.moneyStockPO.moneyScId.eq(symbol)) - .and(QMoneyStockPO.moneyStockPO.isLock.eq(0)) - .and(QMoneyStockPO.moneyStockPO.isShow.eq(0))) + .and(QMoneyStockPO.moneyStockPO.moneyScId.eq(symbol)) + .and(QMoneyStockPO.moneyStockPO.isLock.eq(0)) + .and(QMoneyStockPO.moneyStockPO.isShow.eq(0))) .orElse(null); /* if(moneyStock==null){ return ServerResponse.createByErrorMsg("没有找到该股票"); }*/ // 设置重试次数 - if("ANI".equals(symbol)){ + if ("ANI".equals(symbol)) { JSONObject json1 = new JSONObject(); - json1.put("company","Archit Nuwood Industries Ltd"); - json1.put("pricepercentchange","Archit Nuwood Industries Ltd"); - json1.put("stockType",stockType); - json1.put("pricecurrent","386"); - json1.put("dataSourceType","3"); - json1.put("symbol","ANI"); - json1.put("BSEID","ANI"); - json1.put("NSEID","ANI"); + json1.put("company", "Archit Nuwood Industries Ltd"); + json1.put("pricepercentchange", "Archit Nuwood Industries Ltd"); + json1.put("stockType", stockType); + json1.put("pricecurrent", "386"); + json1.put("dataSourceType", "3"); + json1.put("symbol", "ANI"); + json1.put("BSEID", "ANI"); + json1.put("NSEID", "ANI"); return ServerResponse.createBySuccess(json1); } - String url = String.format("https://priceapi.moneycontrol.com/pricefeed/%s/equitycash/%s",stockType,symbol); + String url = String.format("https://priceapi.moneycontrol.com/pricefeed/%s/equitycash/%s", stockType, symbol); int maxRetries = 3; for (int retry = 1; retry <= maxRetries; retry++) { try { ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, String.class); JSONObject json1 = new JSONObject(); - if (responseEntity.getStatusCode().value() == 200 && responseEntity.getBody() != null ) { + if (responseEntity.getStatusCode().value() == 200 && responseEntity.getBody() != null) { JSONObject data = JSONObject.parseObject(responseEntity.getBody()).getJSONObject("data"); - if(data!=null){ - json1.put("company",data.getString("SC_FULLNM")); - json1.put("pricepercentchange",data.getString("pricepercentchange")); - json1.put("stockType",stockType); - json1.put("pricechange",data.getString("pricechange")); - json1.put("pricecurrent",data.getString("pricecurrent")); - json1.put("priceprevclose",data.getString("priceprevclose")); - json1.put("PREVDATE",data.getString("PREVDATE")); - json1.put("VOL",data.getString("VOL")); - json1.put("dataSourceType","3"); - json1.put("symbol",data.getString("symbol")); - json1.put("BSEID",data.getString("BSEID")); - json1.put("NSEID",data.getString("NSEID")); - json1.put("LTH",data.getString("HP")); - json1.put("LTL",data.getString("LP")); - json1.put("OPN",data.getString("OPN")); - if(null!=moneyStock){ - json1.put("id",moneyStock.getId()); + if (data != null) { + json1.put("company", data.getString("SC_FULLNM")); + json1.put("pricepercentchange", data.getString("pricepercentchange")); + json1.put("stockType", stockType); + json1.put("pricechange", data.getString("pricechange")); + json1.put("pricecurrent", data.getString("pricecurrent")); + json1.put("priceprevclose", data.getString("priceprevclose")); + json1.put("PREVDATE", data.getString("PREVDATE")); + json1.put("VOL", data.getString("VOL")); + json1.put("dataSourceType", "3"); + json1.put("symbol", data.getString("symbol")); + json1.put("BSEID", data.getString("BSEID")); + json1.put("NSEID", data.getString("NSEID")); + json1.put("LTH", data.getString("HP")); + json1.put("LTL", data.getString("LP")); + json1.put("OPN", data.getString("OPN")); + if (null != moneyStock) { + json1.put("id", moneyStock.getId()); } - if(StringUtils.equals(data.getString("pricecurrent"),"0.00") - && (!StringUtils.equals(data.getString("priceprevclose"),"0.00"))){ - json1.put("pricecurrent",data.getString("priceprevclose")); + if (StringUtils.equals(data.getString("pricecurrent"), "0.00") + && (!StringUtils.equals(data.getString("priceprevclose"), "0.00"))) { + json1.put("pricecurrent", data.getString("priceprevclose")); } } - return ServerResponse.createBySuccess(json1); + if (json1.size() > 0) + return ServerResponse.createBySuccess(json1); } } catch (Exception e) { } @@ -240,7 +241,7 @@ public class MoneyApiController { try { // Get data from nseindia JSONObject json = NseIndiaRequest.stockByJYSFromHttp(stockType, symbol, moneyStock.getNseIndiaId()); - json.put("id",moneyStock.getId()); + json.put("id", moneyStock.getId()); return ServerResponse.createBySuccess(json); @@ -252,16 +253,15 @@ public class MoneyApiController { } - - private static List nseActives() { + private static List nseActives() { List list = new ArrayList<>(); String url = "https://www.moneycontrol.com/stocks/marketstats/nse-mostactive-stocks/nifty-50-9/"; try { Document doc = Jsoup.connect(url).get(); int size = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr").size(); - for (int i =1;i<=size;i++){ + for (int i = 1; i <= size; i++) { MoneyStockSuggestDTO dto = new MoneyStockSuggestDTO(); - Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child("+i+") > td.PR > span > a").first(); + Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td.PR > span > a").first(); if (company_a != null) { String stockUrl = company_a.attr("href"); String stockName = company_a.text(); @@ -269,7 +269,7 @@ public class MoneyApiController { dto.setStockUrl(stockUrl); } - String highPrice = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child("+i+") > td:nth-child(2)").first()); + String highPrice = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td:nth-child(2)").first()); dto.setHighPrice(highPrice); String lowPrice = getTextOrEmpty(doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td:nth-child(3)").first()); @@ -294,7 +294,7 @@ public class MoneyApiController { return list; } - private static List bseActives() { + private static List bseActives() { List list = new ArrayList<>(); String url = "https://www.moneycontrol.com/stocks/marketstats/bsemact1/index.php"; @@ -302,9 +302,9 @@ public class MoneyApiController { Document doc = Jsoup.connect(url).get(); int size = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr").size(); System.err.println(size); - for (int i =1;i<=size;i++){ + for (int i = 1; i <= size; i++) { MoneyStockSuggestDTO dto = new MoneyStockSuggestDTO(); - Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child("+i+") > td.PR > span > a").first(); + Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td.PR > span > a").first(); if (company_a != null) { String stockUrl = company_a.attr("href"); String stockName = company_a.text(); @@ -333,19 +333,19 @@ public class MoneyApiController { } } catch (IOException e) { - log.error("occur Exception",e); + log.error("occur Exception", e); } return list; } - private static List bseGainer() { + private static List bseGainer() { String url = "https://www.moneycontrol.com/stocks/marketstats/bse-gainer/sensex_4/"; List list = Lists.newArrayList(); try { Document doc = Jsoup.connect(url).get(); int size = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr").size(); - for (int i =1;i<=size;i++){ - Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child("+i+") > td.PR > span > a").first(); + for (int i = 1; i <= size; i++) { + Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td.PR > span > a").first(); MoneyStockSuggestDTO dto = new MoneyStockSuggestDTO(); if (company_a != null) { String stockUrl = company_a.attr("href"); @@ -389,9 +389,9 @@ public class MoneyApiController { try { Document doc = Jsoup.connect(url).get(); int size = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr").size(); - for (int i =1;i<=size;i++){ + for (int i = 1; i <= size; i++) { MoneyStockSuggestDTO dto = new MoneyStockSuggestDTO(); - Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child("+i+") > td.PR > span > h3 > a").first(); + Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td.PR > span > h3 > a").first(); if (company_a != null) { String stockUrl = company_a.attr("href"); String stockName = company_a.text(); @@ -426,20 +426,20 @@ public class MoneyApiController { return list; } - private static List nseTopLoser() { + private static List nseTopLoser() { String url = "https://www.moneycontrol.com/stocks/marketstats/nseloser/index.php"; List list = Lists.newArrayList(); try { Document doc = Jsoup.connect(url).get(); int size = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr").size(); - for (int i =1;i<=size;i++){ + for (int i = 1; i <= size; i++) { MoneyStockSuggestDTO dto = new MoneyStockSuggestDTO(); - Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child("+i+") > td.PR > span > h3 > a").first(); + Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td.PR > span > h3 > a").first(); if (company_a != null) { String stockUrl = company_a.attr("href"); String stockName = company_a.text(); - dto.setStockUrl(stockUrl); + dto.setStockUrl(stockUrl); dto.setStockName(stockName); } @@ -466,7 +466,7 @@ public class MoneyApiController { } catch (IOException e) { e.printStackTrace(); } - return list; + return list; } private static List bseTopLoser() { @@ -476,7 +476,7 @@ public class MoneyApiController { try { Document doc = Jsoup.connect(url).get(); int size = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr").size(); - for (int i =1;i<=size;i++){ + for (int i = 1; i <= size; i++) { Element company_a = doc.select("#mc_content > section > section > div.clearfix.stat_container > div.columnst.FR.wbg.brdwht > div > div > div.bsr_table.hist_tbl_hm > table > tbody > tr:nth-child(" + i + ") > td.PR > span > a").first(); MoneyStockSuggestDTO dto = new MoneyStockSuggestDTO(); if (company_a != null) { @@ -521,17 +521,15 @@ public class MoneyApiController { } - - - @ApiOperation(value = "股票推荐TopGainer",httpMethod = "GET") + @ApiOperation(value = "股票推荐TopGainer", httpMethod = "GET") @ApiImplicitParams({ - @ApiImplicitParam(name="stockType",value = "BSE或者NSE"), + @ApiImplicitParam(name = "stockType", value = "BSE或者NSE"), }) @ApiResponses(value = { @ApiResponse(code = 200, message = "" + "股票推荐相关: top gainer", response = JSONObject.class), }) - @GetMapping({"/market/api/market/money/getTopGainer","/api/market/money/getTopGainer"}) + @GetMapping({"/market/api/market/money/getTopGainer", "/api/market/money/getTopGainer"}) @ResponseBody @EncryptFilter(decryptRequest = false) @@ -549,15 +547,15 @@ public class MoneyApiController { } Map map = new HashMap<>(); moneyStockSuggestDTOS = moneyStockSuggestDTOS.stream() - .filter(f->StringUtils.isNotBlank(f.getStockName())) + .filter(f -> StringUtils.isNotBlank(f.getStockName())) .filter(i -> map.putIfAbsent(i.getStockName(), Boolean.TRUE) == null).collect(Collectors.toList()); - if(CollectionUtils.isNotEmpty(moneyStockSuggestDTOS)){ + if (CollectionUtils.isNotEmpty(moneyStockSuggestDTOS)) { List selfUlrList = moneyStockSuggestDTOS.stream().map(MoneyStockSuggestDTO::getStockName).collect(Collectors.toList()); - if(CollectionUtils.isNotEmpty(selfUlrList)){ + if (CollectionUtils.isNotEmpty(selfUlrList)) { List all = moneyStockRepository.findAll(QMoneyStockPO.moneyStockPO.stockName.in(selfUlrList)); - if(CollectionUtils.isNotEmpty(all)){ - moneyStockSuggestDTOS.stream().filter(f->all.stream().anyMatch(s->s.getStockName().equals(f.getStockName()))) - .forEach(f->f.setScId(all.stream().filter(s->s.getStockName().equals(f.getStockName())).findFirst().orElse(null).getMoneyScId())); + if (CollectionUtils.isNotEmpty(all)) { + moneyStockSuggestDTOS.stream().filter(f -> all.stream().anyMatch(s -> s.getStockName().equals(f.getStockName()))) + .forEach(f -> f.setScId(all.stream().filter(s -> s.getStockName().equals(f.getStockName())).findFirst().orElse(null).getMoneyScId())); } } gainerStockSuggestCache.put(stockType, moneyStockSuggestDTOS); @@ -568,47 +566,47 @@ public class MoneyApiController { } - @ApiOperation(value = "股票推荐TopLoser",httpMethod = "GET") + @ApiOperation(value = "股票推荐TopLoser", httpMethod = "GET") @ApiImplicitParams({ - @ApiImplicitParam(name="stockType",value = "BSE或者NSE"), + @ApiImplicitParam(name = "stockType", value = "BSE或者NSE"), }) @ApiResponses(value = { @ApiResponse(code = 200, message = "" + "股票推荐相关: TopLoser", response = JSONObject.class), }) - @GetMapping({"/market/api/market/money/getTopLoser","/api/market/money/getTopLoser"}) + @GetMapping({"/market/api/market/money/getTopLoser", "/api/market/money/getTopLoser"}) @ResponseBody @EncryptFilter(decryptRequest = false) public List getTopLoser(@RequestParam String stockType) { List moneyStockSuggestDTOS = null; moneyStockSuggestDTOS = loserStockSuggestCache.getIfPresent(stockType); - if(null==moneyStockSuggestDTOS){ - if(StringUtils.equals(stockType,"nse")){ + if (null == moneyStockSuggestDTOS) { + if (StringUtils.equals(stockType, "nse")) { moneyStockSuggestDTOS = nseTopLoser(); - }else if(StringUtils.equals(stockType,"bse")){ + } else if (StringUtils.equals(stockType, "bse")) { moneyStockSuggestDTOS = bseTopLoser(); } Map map = new HashMap<>(); moneyStockSuggestDTOS = moneyStockSuggestDTOS.stream() - .filter(f->StringUtils.isNotBlank(f.getStockName())) + .filter(f -> StringUtils.isNotBlank(f.getStockName())) .filter(i -> map.putIfAbsent(i.getStockName(), Boolean.TRUE) == null).collect(Collectors.toList()); - if(CollectionUtils.isNotEmpty(moneyStockSuggestDTOS)){ - moneyStockSuggestDTOS.stream().forEach(f->f.setDispId(extractLastSegment(f.getStockUrl()))); + if (CollectionUtils.isNotEmpty(moneyStockSuggestDTOS)) { + moneyStockSuggestDTOS.stream().forEach(f -> f.setDispId(extractLastSegment(f.getStockUrl()))); List selfUlrList = moneyStockSuggestDTOS.stream().map(MoneyStockSuggestDTO::getStockName).collect(Collectors.toList()); - if(CollectionUtils.isNotEmpty(selfUlrList)){ + if (CollectionUtils.isNotEmpty(selfUlrList)) { List all = moneyStockRepository.findAll(QMoneyStockPO.moneyStockPO.stockName.in(selfUlrList)); - if(CollectionUtils.isNotEmpty(all)){ - moneyStockSuggestDTOS.stream().filter(f->all.stream().anyMatch(s->s.getStockName().equals(f.getStockName()))) - .forEach(f->f.setScId(all.stream().filter(s->s.getStockName().equals(f.getStockName())).findFirst().orElse(null).getMoneyScId())); + if (CollectionUtils.isNotEmpty(all)) { + moneyStockSuggestDTOS.stream().filter(f -> all.stream().anyMatch(s -> s.getStockName().equals(f.getStockName()))) + .forEach(f -> f.setScId(all.stream().filter(s -> s.getStockName().equals(f.getStockName())).findFirst().orElse(null).getMoneyScId())); } - List noScIdList = moneyStockSuggestDTOS.stream().filter(f->StringUtils.isBlank(f.getScId())).collect(Collectors.toList()); - if(CollectionUtils.isNotEmpty(noScIdList)){ + List noScIdList = moneyStockSuggestDTOS.stream().filter(f -> StringUtils.isBlank(f.getScId())).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(noScIdList)) { List dispIdList = noScIdList.stream().map(MoneyStockSuggestDTO::getDispId).collect(Collectors.toList()); List all1 = moneyStockRepository.findAll(QMoneyStockPO.moneyStockPO.selfDispId.in(dispIdList)); - if(CollectionUtils.isNotEmpty(all1)){ - moneyStockSuggestDTOS.stream().filter(f->all1.stream().anyMatch(s->s.getSelfDispId().equals(f.getDispId()))) - .forEach(f->f.setScId(all.stream().filter(s->s.getSelfDispId().equals(f.getDispId())).findFirst().orElse(null).getMoneyScId())); + if (CollectionUtils.isNotEmpty(all1)) { + moneyStockSuggestDTOS.stream().filter(f -> all1.stream().anyMatch(s -> s.getSelfDispId().equals(f.getDispId()))) + .forEach(f -> f.setScId(all.stream().filter(s -> s.getSelfDispId().equals(f.getDispId())).findFirst().orElse(null).getMoneyScId())); } } } @@ -619,49 +617,47 @@ public class MoneyApiController { } - - - @ApiOperation(value = "股票推荐TopActive",httpMethod = "GET") + @ApiOperation(value = "股票推荐TopActive", httpMethod = "GET") @ApiImplicitParams({ - @ApiImplicitParam(name="stockType",value = "BSE或者NSE"), + @ApiImplicitParam(name = "stockType", value = "BSE或者NSE"), }) @ApiResponses(value = { @ApiResponse(code = 200, message = "" + "股票推荐相关: top active", response = JSONObject.class), }) - @GetMapping({"/market/api/market/money/getTopActives","/api/market/money/getTopActives"}) + @GetMapping({"/market/api/market/money/getTopActives", "/api/market/money/getTopActives"}) @ResponseBody @EncryptFilter(decryptRequest = false) public List getTopActive(@RequestParam String stockType) { List moneyStockSuggestDTOS = null; moneyStockSuggestDTOS = activesStockSuggestCache.getIfPresent(stockType); - if(moneyStockSuggestDTOS ==null){ - if(StringUtils.equals(stockType,"nse")){ + if (moneyStockSuggestDTOS == null) { + if (StringUtils.equals(stockType, "nse")) { moneyStockSuggestDTOS = nseActives(); - }else if(StringUtils.equals(stockType,"bse")){ + } else if (StringUtils.equals(stockType, "bse")) { moneyStockSuggestDTOS = bseActives(); } Map map = new HashMap<>(); moneyStockSuggestDTOS = moneyStockSuggestDTOS.stream() - .filter(f->StringUtils.isNotBlank(f.getStockName())) + .filter(f -> StringUtils.isNotBlank(f.getStockName())) .filter(i -> map.putIfAbsent(i.getStockName(), Boolean.TRUE) == null).collect(Collectors.toList()); - if(CollectionUtils.isNotEmpty(moneyStockSuggestDTOS)){ - moneyStockSuggestDTOS.stream().forEach(f->f.setDispId(extractLastSegment(f.getStockUrl()))); + if (CollectionUtils.isNotEmpty(moneyStockSuggestDTOS)) { + moneyStockSuggestDTOS.stream().forEach(f -> f.setDispId(extractLastSegment(f.getStockUrl()))); List selfUlrList = moneyStockSuggestDTOS.stream().map(MoneyStockSuggestDTO::getStockName).collect(Collectors.toList()); - if(CollectionUtils.isNotEmpty(selfUlrList)){ + if (CollectionUtils.isNotEmpty(selfUlrList)) { List all = moneyStockRepository.findAll(QMoneyStockPO.moneyStockPO.stockName.in(selfUlrList)); - if(CollectionUtils.isNotEmpty(all)){ - moneyStockSuggestDTOS.stream().filter(f->all.stream().anyMatch(s->s.getStockName().equals(f.getStockName()))) - .forEach(f->f.setScId(all.stream().filter(s->s.getStockName().equals(f.getStockName())).findFirst().orElse(null).getMoneyScId())); + if (CollectionUtils.isNotEmpty(all)) { + moneyStockSuggestDTOS.stream().filter(f -> all.stream().anyMatch(s -> s.getStockName().equals(f.getStockName()))) + .forEach(f -> f.setScId(all.stream().filter(s -> s.getStockName().equals(f.getStockName())).findFirst().orElse(null).getMoneyScId())); } - List noScIdList = moneyStockSuggestDTOS.stream().filter(f->StringUtils.isBlank(f.getScId())).collect(Collectors.toList()); - if(CollectionUtils.isNotEmpty(noScIdList)){ + List noScIdList = moneyStockSuggestDTOS.stream().filter(f -> StringUtils.isBlank(f.getScId())).collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(noScIdList)) { List dispIdList = noScIdList.stream().map(MoneyStockSuggestDTO::getDispId).collect(Collectors.toList()); List all1 = moneyStockRepository.findAll(QMoneyStockPO.moneyStockPO.selfDispId.in(dispIdList)); - if(CollectionUtils.isNotEmpty(all1)){ - moneyStockSuggestDTOS.stream().filter(f->all1.stream().anyMatch(s->s.getSelfDispId().equals(f.getDispId()))) - .forEach(f->f.setScId(all.stream().filter(s->s.getSelfDispId().equals(f.getDispId())).findFirst().orElse(null).getMoneyScId())); + if (CollectionUtils.isNotEmpty(all1)) { + moneyStockSuggestDTOS.stream().filter(f -> all1.stream().anyMatch(s -> s.getSelfDispId().equals(f.getDispId()))) + .forEach(f -> f.setScId(all.stream().filter(s -> s.getSelfDispId().equals(f.getDispId())).findFirst().orElse(null).getMoneyScId())); } } } @@ -673,9 +669,8 @@ public class MoneyApiController { } - - @GetMapping({"/market/api/market/money/history/kLine","/api/market/money/history/kLine"}) - @ApiOperation(value = "获取kline的money数据源", notes = "获取kline的money数据源",response = StockHistoryResponse.class) + @GetMapping({"/market/api/market/money/history/kLine", "/api/market/money/history/kLine"}) + @ApiOperation(value = "获取kline的money数据源", notes = "获取kline的money数据源", response = StockHistoryResponse.class) @ApiImplicitParams({ @ApiImplicitParam(name = "symbol", value = "Stock symbol 对应的是NSEID 或者是BSEID", required = true, dataType = "String", paramType = "query"), @ApiImplicitParam(name = "resolution", value = "单位:60 1D 1W 1D 对应H,D,W,Y", required = true, dataType = "String", paramType = "query"), @@ -685,34 +680,34 @@ public class MoneyApiController { @ApiImplicitParam(name = "currencyCode", value = "INR 不变", required = true, dataType = "String", paramType = "query") }) @ResponseBody - @EncryptFilter(decryptRequest = false) - public ResponseEntity getStockHistory( @RequestParam String symbol, - @RequestParam String resolution - ) { + @EncryptFilter(decryptRequest = false) + public ResponseEntity getStockHistory(@RequestParam String symbol, + @RequestParam String resolution + ) { // 向外部API发起请求,并获取响应 StockHistoryRequest request = new StockHistoryRequest(); request.setSymbol(symbol); Long to = null; Long from = null; int countback = 5; - if(StringUtils.equals("H",resolution)){ - to = (long) (System.currentTimeMillis() / 1000); - from = to - ( 60 * 60 ); + if (StringUtils.equals("H", resolution)) { + to = (long) (System.currentTimeMillis() / 1000); + from = to - (60 * 60); countback = 60; request.setResolution("1"); - }else if(StringUtils.equals("D",resolution)){ - to = (long) (System.currentTimeMillis() / 1000); - from = to - (24 * 60 * 60 ); + } else if (StringUtils.equals("D", resolution)) { + to = (long) (System.currentTimeMillis() / 1000); + from = to - (24 * 60 * 60); countback = 390; request.setResolution("1"); - }else if(StringUtils.equals("W",resolution)){ - to = (long) (System.currentTimeMillis() / 1000); - from = to - (7 * 24 * 60 * 60 ); + } else if (StringUtils.equals("W", resolution)) { + to = (long) (System.currentTimeMillis() / 1000); + from = to - (7 * 24 * 60 * 60); countback = 471; request.setResolution("5"); - }else if(StringUtils.equals("M",resolution)){ - to = (long) (System.currentTimeMillis() / 1000); - from = to - (35 * 24 * 60 * 60 ); + } else if (StringUtils.equals("M", resolution)) { + to = (long) (System.currentTimeMillis() / 1000); + from = to - (35 * 24 * 60 * 60); countback = 328; request.setResolution("30"); } @@ -722,7 +717,7 @@ public class MoneyApiController { request.setCountback(countback); request.setCurrencyCode("INR"); String apiUrl = buildApiUrl(request); - log.info("request url:"+apiUrl); + log.info("request url:" + apiUrl); StockHistoryResponse response = null; int maxRetries = 3; int retryCount = 0; @@ -746,7 +741,7 @@ public class MoneyApiController { } if (response != null) { - setResponse(response,resolution); + setResponse(response, resolution); // API request successful, return the response return ResponseEntity.ok(response); } else { @@ -757,14 +752,14 @@ public class MoneyApiController { // 返回响应 } - private void setResponse(StockHistoryResponse response,String resolution){ - if(!"ok".equals(response.getS())){ + private void setResponse(StockHistoryResponse response, String resolution) { + if (!"ok".equals(response.getS())) { return; } - if(Objects.isNull(response.getT())){ + if (Objects.isNull(response.getT())) { return; } - if(StringUtils.equals("H",resolution) || StringUtils.equals("D",resolution)) { + if (StringUtils.equals("H", resolution) || StringUtils.equals("D", resolution)) { List t = new ArrayList<>(); List o = new ArrayList<>(); List h = new ArrayList<>(); @@ -776,7 +771,7 @@ public class MoneyApiController { Date currentTime = new Date(); //判断最后一条是不是当天的数据,如果不是,把最后一条的时间作为当天的时间 long getTime = response.getT().get(response.getT().size() - 1) * 1000L; - if(!DateUtil.isSameDay(currentTime, new Date(getTime))){ + if (!DateUtil.isSameDay(currentTime, new Date(getTime))) { currentTime = new Date(getTime); } int i = 0; @@ -802,14 +797,14 @@ public class MoneyApiController { } - - 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) { @@ -834,7 +829,7 @@ public class MoneyApiController { //// bseTopLoser(); System.out.println(new Date()); System.out.println(new Date(1713949200000L)); - System.out.println(DateUtil.isSameDay(new Date(),new Date(1713949200000L))); + System.out.println(DateUtil.isSameDay(new Date(), new Date(1713949200000L))); } private Cache> gainerStockSuggestCache = CacheBuilder.newBuilder()