diff --git a/src/main/java/cn/stock/market/web/MoneyApiController.java b/src/main/java/cn/stock/market/web/MoneyApiController.java index 3e94a29..8b028f6 100644 --- a/src/main/java/cn/stock/market/web/MoneyApiController.java +++ b/src/main/java/cn/stock/market/web/MoneyApiController.java @@ -823,6 +823,18 @@ public class MoneyApiController { } } + private Long convertToTimestamp(String dateStr) { + try { + SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a", Locale.US); + Date date = sdf.parse(dateStr); + return date.getTime(); + } catch (ParseException e) { + e.printStackTrace(); + return 0L; + } + } + + @GetMapping({"/market/api/market/money/history/kLine", "/api/market/money/history/kLine"}) @ApiOperation(value = "获取kline的money数据源", notes = "获取kline的money数据源", response = StockHistoryResponse.class) @@ -872,6 +884,8 @@ public class MoneyApiController { List volumeList = new ArrayList<>(); List dateList = new ArrayList<>(); + Map latestDataMap = new LinkedHashMap<>(); + if (dataInputValues.size() > 0) { JsonObject dataObject = dataInputValues.get(0).getAsJsonObject(); @@ -882,12 +896,80 @@ public class MoneyApiController { JsonArray volumeArray = dataObject.getAsJsonArray("VolumeData"); JsonArray dateArray = dataObject.getAsJsonArray("DateData"); - openArray.forEach(o -> addToListDouble(openList, o.getAsJsonObject().get("Open").getAsString())); - highArray.forEach(h -> addToListDouble(highList, h.getAsJsonObject().get("High").getAsString())); - lowArray.forEach(l -> addToListDouble(lowList, l.getAsJsonObject().get("Low").getAsString())); - closeArray.forEach(c -> addToListDouble(closeList, c.getAsJsonObject().get("Close").getAsString())); - volumeArray.forEach(v -> addToList(volumeList, v.getAsJsonObject().get("Volume").getAsString())); - dateArray.forEach(d -> addDatesToList(dateList, d.getAsJsonObject().get("Date").getAsString())); + if (resolution.equals("H")) { + openArray.forEach(o -> addToListDouble(openList, o.getAsJsonObject().get("Open").getAsString())); + highArray.forEach(h -> addToListDouble(highList, h.getAsJsonObject().get("High").getAsString())); + lowArray.forEach(l -> addToListDouble(lowList, l.getAsJsonObject().get("Low").getAsString())); + closeArray.forEach(c -> addToListDouble(closeList, c.getAsJsonObject().get("Close").getAsString())); + volumeArray.forEach(v -> addToList(volumeList, v.getAsJsonObject().get("Volume").getAsString())); + dateArray.forEach(d -> addDatesToList(dateList, d.getAsJsonObject().get("Date").getAsString())); + } else { + for (int i = 0; i < openArray.size(); i++) { + String open = openArray.get(i).getAsJsonObject().get("Open").getAsString(); + String high = highArray.get(i).getAsJsonObject().get("High").getAsString(); + String low = lowArray.get(i).getAsJsonObject().get("Low").getAsString(); + String close = closeArray.get(i).getAsJsonObject().get("Close").getAsString(); + String volume = volumeArray.get(i).getAsJsonObject().get("Volume").getAsString(); + String dateStr = dateArray.get(i).getAsJsonObject().get("Date").getAsString(); + + Long timestamp = convertToTimestamp(dateStr); + + String dateTimeKey = dateStr; + + JsonObject dataJson = new JsonObject(); + dataJson.addProperty("Open", open); + dataJson.addProperty("High", high); + dataJson.addProperty("Low", low); + dataJson.addProperty("Close", close); + dataJson.addProperty("Volume", volume); + dataJson.addProperty("Date", dateStr); + + latestDataMap.put(dateTimeKey, dataJson); + } + + latestDataMap.forEach((key, value) -> { + // Lấy giá trị các trường Open, High, Low, Close, Volume, Date từ dữ liệu + String openData = value.get("Open").getAsString(); + String highData = value.get("High").getAsString(); + String lowData = value.get("Low").getAsString(); + String closeData = value.get("Close").getAsString(); + String volumeData = value.get("Volume").getAsString(); + String dateData = value.get("Date").getAsString(); + + // Tách các giá trị trong chuỗi bằng dấu phẩy + String[] openValues = openData.split(","); + String[] highValues = highData.split(","); + String[] lowValues = lowData.split(","); + String[] closeValues = closeData.split(","); + String[] volumeValues = volumeData.split(","); + String[] dateValues = dateData.split(","); + + for (int i = 0; i < openValues.length; i++) { + double open = Double.parseDouble(openValues[i]); + double high = Double.parseDouble(highValues[i]); + double low = Double.parseDouble(lowValues[i]); + double close = Double.parseDouble(closeValues[i]); + long volume = Math.round(Double.parseDouble(volumeValues[i])); + long timestamp = convertToTimestamp(dateValues[i]); + + int index = dateList.indexOf(timestamp); + if (index == -1) { + dateList.add(timestamp); + openList.add(open); + highList.add(high); + lowList.add(low); + closeList.add(close); + volumeList.add(volume); + } else { + openList.set(index, open); + highList.set(index, high); + lowList.set(index, low); + closeList.set(index, close); + volumeList.set(index, volume); + } + } + }); + } StockHistoryResponse response = new StockHistoryResponse(); response.setS("ok");