Sfoglia il codice sorgente

我的股票功能更新

Zhangbw 3 mesi fa
parent
commit
5eda3d97b6

+ 17 - 1
src/main/java/com/yingpai/gupiao/controller/StockDataController.java

@@ -222,6 +222,7 @@ public class StockDataController {
                     JsonNode trends = data.get("trends");
                     
                     if (trends != null && trends.isArray()) {
+                        List<BigDecimal> allPrices = new ArrayList<>();
                         // 提取价格数据(trends数组中每个元素格式:时间,价格,均价,成交量...)
                         for (JsonNode trend : trends) {
                             String trendStr = trend.asText();
@@ -229,12 +230,27 @@ public class StockDataController {
                             if (parts.length >= 2) {
                                 try {
                                     BigDecimal price = new BigDecimal(parts[1]);  // 第二个字段是价格
-                                    trendData.add(price);
+                                    allPrices.add(price);
                                 } catch (NumberFormatException e) {
                                     // 忽略解析失败的数据点
                                 }
                             }
                         }
+                        
+                        // 采样:将数据点减少到约30个,让图表更直观
+                        int targetPoints = 30;
+                        if (allPrices.size() <= targetPoints) {
+                            trendData = allPrices;
+                        } else {
+                            // 等间隔采样
+                            double step = (double) (allPrices.size() - 1) / (targetPoints - 1);
+                            for (int i = 0; i < targetPoints; i++) {
+                                int index = (int) Math.round(i * step);
+                                if (index < allPrices.size()) {
+                                    trendData.add(allPrices.get(index));
+                                }
+                            }
+                        }
                     }
                 }
             }

+ 12 - 2
src/main/java/com/yingpai/gupiao/service/impl/UserStockServiceImpl.java

@@ -28,14 +28,19 @@ public class UserStockServiceImpl implements UserStockService {
     
     @Override
     public List<UserStockVO> getUserStocks(Long userId) {
+        System.out.println("[查询股票] userId=" + userId);
+        
         LambdaQueryWrapper<UserStock> wrapper = new LambdaQueryWrapper<>();
         wrapper.eq(UserStock::getUserId, userId)
                .orderByDesc(UserStock::getAddDate);
         
         List<UserStock> stocks = userStockMapper.selectList(wrapper);
+        System.out.println("[查询股票] 查询到 " + stocks.size() + " 条记录");
+        
         List<UserStockVO> result = new ArrayList<>();
         
         for (UserStock stock : stocks) {
+            System.out.println("[查询股票] 股票: " + stock.getStockCode() + " - " + stock.getStockName());
             UserStockVO vo = UserStockVO.builder()
                     .stockCode(stock.getStockCode())
                     .stockName(stock.getStockName())
@@ -50,8 +55,11 @@ public class UserStockServiceImpl implements UserStockService {
     
     @Override
     public boolean addStock(Long userId, AddUserStockDTO dto) {
+        System.out.println("[添加股票] userId=" + userId + ", stockCode=" + dto.getStockCode() + ", stockName=" + dto.getStockName());
+        
         // 检查是否已存在
         if (isStockAdded(userId, dto.getStockCode())) {
+            System.out.println("[添加股票] 股票已存在,跳过添加");
             return false;
         }
         
@@ -61,7 +69,7 @@ public class UserStockServiceImpl implements UserStockService {
                 price = new BigDecimal(dto.getCurrentPrice());
             }
         } catch (NumberFormatException e) {
-            // 忽略解析错误
+            System.err.println("[添加股票] 价格解析失败: " + dto.getCurrentPrice());
         }
         
         UserStock userStock = UserStock.builder()
@@ -72,7 +80,9 @@ public class UserStockServiceImpl implements UserStockService {
                 .addDate(LocalDate.now())
                 .build();
         
-        return userStockMapper.insert(userStock) > 0;
+        int result = userStockMapper.insert(userStock);
+        System.out.println("[添加股票] 插入结果: " + result);
+        return result > 0;
     }
     
     @Override