diff --git a/pom.xml b/pom.xml index 2dfa1d0..9ef511d 100644 --- a/pom.xml +++ b/pom.xml @@ -38,18 +38,6 @@ - org.apache.maven.plugins - maven-compiler-plugin - - - - org.projectlombok - lombok - - - - - org.springframework.boot spring-boot-maven-plugin diff --git a/src/main/java/com/deepchart/controller/IndexController.java b/src/main/java/com/deepchart/controller/IndexController.java index 2ea40d9..167d969 100644 --- a/src/main/java/com/deepchart/controller/IndexController.java +++ b/src/main/java/com/deepchart/controller/IndexController.java @@ -1,5 +1,10 @@ package com.deepchart.controller; +import com.deepchart.entity.StockInfo; +import com.deepchart.service.IndexService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -7,4 +12,11 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping(value = "/api/index", produces = "application/json") public class IndexController { + @Autowired + private IndexService indexService; + + @PostMapping("/macd") + public String macd(@RequestBody StockInfo stock) { + return ""; + } } diff --git a/src/main/java/com/deepchart/entity/StockDailyData.java b/src/main/java/com/deepchart/entity/StockDailyData.java new file mode 100644 index 0000000..a555aaa --- /dev/null +++ b/src/main/java/com/deepchart/entity/StockDailyData.java @@ -0,0 +1,17 @@ +package com.deepchart.entity; + +import lombok.Data; + +import java.time.LocalDate; + +/** + * 股票单日行情基本数据实体类 + */ +@Data +public class StockDailyData { + private LocalDate date; // 交易日日期 + private double openPrice; // 开盘价 + private double closePrice; // 收盘价 + private double lowPrice; // 最低价 + private double highPrice; // 最高价 +} \ No newline at end of file diff --git a/src/main/java/com/deepchart/entity/StockInfo.java b/src/main/java/com/deepchart/entity/StockInfo.java new file mode 100644 index 0000000..d4e68b4 --- /dev/null +++ b/src/main/java/com/deepchart/entity/StockInfo.java @@ -0,0 +1,9 @@ +package com.deepchart.entity; + +import lombok.Data; + +@Data +public class StockInfo { + private String market; + private String code; +} diff --git a/src/main/java/com/deepchart/service/impl/IndexServiceImpl.java b/src/main/java/com/deepchart/service/impl/IndexServiceImpl.java index 4863cf7..be4f654 100644 --- a/src/main/java/com/deepchart/service/impl/IndexServiceImpl.java +++ b/src/main/java/com/deepchart/service/impl/IndexServiceImpl.java @@ -1,8 +1,14 @@ package com.deepchart.service.impl; import com.deepchart.service.IndexService; +import com.deepchart.utils.StockDataUtil; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class IndexServiceImpl implements IndexService { + + @Autowired + private StockDataUtil stockDataUtil; + } diff --git a/src/main/java/com/deepchart/utils/StockDataUtil.java b/src/main/java/com/deepchart/utils/StockDataUtil.java new file mode 100644 index 0000000..8b4ba0e --- /dev/null +++ b/src/main/java/com/deepchart/utils/StockDataUtil.java @@ -0,0 +1,100 @@ +package com.deepchart.utils; + +import com.deepchart.entity.StockDailyData; +import com.deepchart.entity.StockInfo; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.stereotype.Component; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * 股票行情基本数据工具类 + */ +@Component +public class StockDataUtil { + private static final String STOCK_DATA_PREFIX = "https://api.homilychart.com/link"; + private static final String TOKEN = "8nkj4QBV1RPIb4CzoRTnbZi0+fEeMx8pywnIlrmTxdwROKkuwWqAWu9orpkpeXVqL98DPfeonNYpHv+mucA"; + private static final ObjectMapper objectMapper = new ObjectMapper(); + + public List getStockData(StockInfo stock) { + + List> kLine20 = List.of(); + + try { + // 1. 构建请求URL + String url = STOCK_DATA_PREFIX + "/api/superBrainData"; + + // 2. 使用Map构造请求体参数 + Map requestBody = new HashMap<>(); + requestBody.put("brainPrivilegeState", 1); + requestBody.put("marketList", "can,usa,hk,vi,sg,th,in,cn,gb,my"); + requestBody.put("market", stock.getMarket()); + requestBody.put("code", stock.getCode()); + + // 3. 将Map转换为JSON字符串 + String jsonInputString = objectMapper.writeValueAsString(requestBody); + + // 4. 创建HTTP连接 + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("token", TOKEN); + connection.setDoOutput(true); + + // 5. 发送请求数据 + try (OutputStream os = connection.getOutputStream()) { + byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8); + os.write(input, 0, input.length); + } + + // 6. 解析响应 + if (connection.getResponseCode() == 200) { + try (BufferedReader br = new BufferedReader( + new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { + StringBuilder response = new StringBuilder(); + String responseLine; + while ((responseLine = br.readLine()) != null) { + response.append(responseLine.trim()); + } + + // 解析JSON响应 + Map jsonResponse = objectMapper.readValue(response.toString(), Map.class); + Map data = (Map) jsonResponse.get("data"); + Map brain = (Map) data.get("Brain"); + kLine20 = (List>) brain.get("KLine20"); + } + } + + // 7. 关闭连接 + connection.disconnect(); + + } catch (Exception e) { + e.printStackTrace(); + } + + List list = new ArrayList<>(); + + for (List v : kLine20) { + StockDailyData s = new StockDailyData(); + s.setDate(LocalDate.parse(v.get(0), DateTimeFormatter.ofPattern("yyyy/MM/dd"))); + s.setOpenPrice(Double.parseDouble(v.get(1))); + s.setClosePrice(Double.parseDouble(v.get(2))); + s.setLowPrice(Double.parseDouble(v.get(3))); + s.setHighPrice(Double.parseDouble(v.get(4))); + list.add(s); + } + + return list; + } +}