From 8ef7b46038e5a99c6c6d99ed30e9d2834a61ccec Mon Sep 17 00:00:00 2001 From: VoGiaHuy2058 <39868777+vogiahuy2058@users.noreply.github.com> Date: Wed, 18 Jun 2025 22:41:58 +0700 Subject: [PATCH] done home page --- .../domain/basic/service/StockService.java | 77 +++++-------------- .../stock/market/dto/model/ChartCandle.java | 11 +++ .../infrastructure/api/HomeApiIndex.java | 66 +++++++++++++++- .../api/investing/IndiaIndexNewVo.java | 18 +++++ 4 files changed, 112 insertions(+), 60 deletions(-) create mode 100644 src/main/java/cn/stock/market/dto/model/ChartCandle.java create mode 100644 src/main/java/cn/stock/market/infrastructure/api/investing/IndiaIndexNewVo.java diff --git a/src/main/java/cn/stock/market/domain/basic/service/StockService.java b/src/main/java/cn/stock/market/domain/basic/service/StockService.java index c6536d8..84ab368 100644 --- a/src/main/java/cn/stock/market/domain/basic/service/StockService.java +++ b/src/main/java/cn/stock/market/domain/basic/service/StockService.java @@ -15,10 +15,7 @@ import cn.stock.market.infrastructure.api.EttechchartsApis; import cn.stock.market.infrastructure.api.GrowwInApis; import cn.stock.market.infrastructure.api.HomeApiIndex; import cn.stock.market.infrastructure.api.TodayApis; -import cn.stock.market.infrastructure.api.investing.IndiaIndexVo; -import cn.stock.market.infrastructure.api.investing.IndiaStockVO; -import cn.stock.market.infrastructure.api.investing.InvestingApis; -import cn.stock.market.infrastructure.api.investing.InvestingInvokerApis; +import cn.stock.market.infrastructure.api.investing.*; import cn.stock.market.infrastructure.api.sina.vo.HotSearchVO; import cn.stock.market.utils.*; import com.ag.utils.CollectionUtils; @@ -1051,69 +1048,31 @@ public class StockService { public ServerResponse getIndexByBtoday() throws Exception { List list = HomeApiIndex.fetchStockIndices(); - List indexVoList = new ArrayList<>(); - try { - String exchange = "bse"; - IndiaIndexVo vo1 = new IndiaIndexVo(); - String coCode = "20558"; - JSONObject object = TodayApis.getStockDetail("in%3BSEN", coCode); - IndiaStockVO market = objToVo(object); - market.setName("BSESENSEX指数"); + + List indexVoList = new ArrayList<>(); + + for (StockIndex stockIndex : list) { + IndiaIndexNewVo vo1 = new IndiaIndexNewVo(); + IndiaStockVO market = new IndiaStockVO(); + market.setClose(String.valueOf(stockIndex.getClose())); + market.setHigh(String.valueOf(stockIndex.getHigh())); + market.setLow(String.valueOf(stockIndex.getLow())); + market.setName(stockIndex.getName()); + market.setNowPrice(String.valueOf(stockIndex.getClose())); + market.setOpen(String.valueOf(stockIndex.getOpen())); + market.setRate(String.valueOf(stockIndex.getPercentChange())); vo1.setIndexVo(market); - //获取k线图 1D 当天的数据 - String format = "S"; - String durationType = "D"; - String duration = "1"; - List kine = TodayApis.getStockKline(exchange,coCode,format,durationType,duration); - vo1.setKLine(kine); + List kLines = HomeApiIndex.fetchChartData(stockIndex.getId(), 419); +// List kline = HomeApiIndex.convertToJsonList(kLines); + vo1.setKLine(kLines); indexVoList.add(vo1); - }catch (Exception e){ - log.error("BToday获取BSESENSEX指数数据异常,异常信息。。。。", e); - try { - GrowwInApis.requestSenSexData(indexVoList); - } catch (Exception e1) { - log.error("GrowwIn获取BSESENSEX指数数据异常,异常信息。。。。", e1); - try{ - EttechchartsApis.requestSensexData(indexVoList); - } catch (Exception e2) { - log.error("Ettechcharts获取BSESENSEX指数数据异常,异常信息。。。。", e2); - } - } } - try { - String exchange = "nse"; - IndiaIndexVo vo1 = new IndiaIndexVo(); - String coCode = "20559"; - JSONObject object = TodayApis.getStockDetail("in%3BNSX", coCode); - IndiaStockVO market = objToVo(object); - market.setName("NIFTY50指数"); - vo1.setIndexVo(market); - - //获取k线图 1D 当天的数据 - String format = "S"; - String durationType = "D"; - String duration = "1"; - List kine = TodayApis.getStockKline(exchange,coCode,format,durationType,duration); - vo1.setKLine(kine); - indexVoList.add(vo1); - }catch (Exception e){ - log.error("BToday获取NIFTY50指数数据异常,异常信息。。。。", e); - try { - GrowwInApis.requestNifty50Data(indexVoList); - } catch (Exception e1) { - log.error("GrowwIn获取NIFTY50指数数据异常,异常信息。。。。", e1); - try{ - EttechchartsApis.requestNifty50Data(indexVoList); - } catch (Exception e2) { - log.error("Ettechcharts获取NIFTY50指数数据异常,异常信息。。。。", e2); - } - } - } return ServerResponse.createBySuccess(indexVoList); } + private IndiaStockVO objToVo(JSONObject object){ IndiaStockVO market = new IndiaStockVO(); if(object.containsKey("priceprevclose")){ diff --git a/src/main/java/cn/stock/market/dto/model/ChartCandle.java b/src/main/java/cn/stock/market/dto/model/ChartCandle.java new file mode 100644 index 0000000..ef77fb8 --- /dev/null +++ b/src/main/java/cn/stock/market/dto/model/ChartCandle.java @@ -0,0 +1,11 @@ +package cn.stock.market.dto.model; + +import lombok.Data; + +@Data +public class ChartCandle { + private String upd_date; + private Double price; + + // Getters & Setters (hoặc @Data nếu dùng Lombok) +} diff --git a/src/main/java/cn/stock/market/infrastructure/api/HomeApiIndex.java b/src/main/java/cn/stock/market/infrastructure/api/HomeApiIndex.java index 0165415..2eaa3c9 100644 --- a/src/main/java/cn/stock/market/infrastructure/api/HomeApiIndex.java +++ b/src/main/java/cn/stock/market/infrastructure/api/HomeApiIndex.java @@ -1,4 +1,5 @@ package cn.stock.market.infrastructure.api; +import cn.stock.market.dto.model.ChartCandle; import cn.stock.market.dto.model.StockIndex; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -6,13 +7,17 @@ import okhttp3.Response; import org.json.JSONArray; import org.json.JSONObject; +import java.time.Instant; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; public class HomeApiIndex { private static final OkHttpClient client = new OkHttpClient(); private static final String API_URL = "https://apinode-dgdev.moneytj.com/api/ger-market/stocks/query-list?symbols=XETR:DAX,XETR:MDAX,XETR:SDXP,XETR:HDAX"; - + private static final String BASE_URL = "https://apinode-dgdev.moneytj.com/api/ger-market/chart"; public static List fetchStockIndices() throws Exception { List result = new ArrayList<>(); @@ -64,6 +69,65 @@ public class HomeApiIndex { return result; } + public static List fetchChartData(String symbol, int amount) throws Exception { + List result = new ArrayList<>(); + + String url = BASE_URL + "?symbol=" + symbol + "&interval=D&amount=" + amount; + + Request request = new Request.Builder() + .url(url) + .addHeader("User-Agent", "Mozilla/5.0") + .addHeader("Accept", "application/json") + .build(); + + Response response = client.newCall(request).execute(); + + if (!response.isSuccessful()) { + throw new RuntimeException("HTTP error code: " + response.code()); + } + + String body = response.body().string(); + JSONObject json = new JSONObject(body); + JSONArray data = json.getJSONArray("data"); + + for (int i = 0; i < data.length(); i++) { + JSONObject obj = data.getJSONObject(i); + ChartCandle candle = new ChartCandle(); + + long ts = obj.optLong("time"); + String formattedTime = convertToGermanTime(ts); + + candle.setUpd_date(formattedTime); + candle.setPrice(getDoubleOrNull(obj, "close")); + + result.add(candle); + } + + return result; + } + +// public static List convertToJsonList(List candles) { +// List result = new ArrayList<>(); +// +// for (ChartCandle c : candles) { +// JSONObject obj = new JSONObject(); +// String formattedTime = convertToGermanTime(c.getTime()); +// obj.put("upd_date", formattedTime); +// obj.put("price", c.getClose()); +// result.add(obj); +// } +// +// return result; +// } + + public static String convertToGermanTime(long epochSeconds) { + ZoneId germanyZone = ZoneId.of("Europe/Berlin"); + Instant instant = Instant.ofEpochSecond(epochSeconds); + ZonedDateTime zonedDateTime = instant.atZone(germanyZone); + DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + return formatter.format(zonedDateTime); + } + // Helper to safely parse nullable numbers private static Double getDoubleOrNull(JSONObject obj, String key) { return obj.isNull(key) ? null : obj.optDouble(key); diff --git a/src/main/java/cn/stock/market/infrastructure/api/investing/IndiaIndexNewVo.java b/src/main/java/cn/stock/market/infrastructure/api/investing/IndiaIndexNewVo.java new file mode 100644 index 0000000..2eefa48 --- /dev/null +++ b/src/main/java/cn/stock/market/infrastructure/api/investing/IndiaIndexNewVo.java @@ -0,0 +1,18 @@ +package cn.stock.market.infrastructure.api.investing; + +import cn.stock.market.dto.model.ChartCandle; +import com.alibaba.fastjson.JSONObject; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.util.List; + +@Data +@ApiModel(value = "指数信息") +public class IndiaIndexNewVo { + @ApiModelProperty(value = "指数详情") + private IndiaStockVO indexVo; + @ApiModelProperty(value = "指数k线") + private List kLine; +}