瀏覽代碼

优化返回数据

JiahengHe 7 月之前
父節點
當前提交
1f8f2c916a

+ 2 - 3
price_collection/src/main/java/com/liangjiang/price_collection/controller/ApiController.java

@@ -1,6 +1,5 @@
 package com.liangjiang.price_collection.controller;
 
-import com.liangjiang.price_collection.dto.ExchangeCoinDto;
 import com.liangjiang.price_collection.dto.PriceInfoDto;
 import com.liangjiang.price_collection.dto.PriceListDto;
 import com.liangjiang.price_collection.res.R;
@@ -48,8 +47,8 @@ public class ApiController {
     }
 
     @GetMapping("/getPrices")
-    public R getPriceRest(@RequestParam("exchange") String exchange, @RequestParam("coin")String coin){
-        Integer[] times = QueryService.getDaysAgoTime(System.currentTimeMillis(), 3L);
+    public R getPriceRest(@RequestParam("exchange") String exchange, @RequestParam("coin")String coin, @RequestParam("dayNum") long hoursNum){
+        Integer[] times = QueryService.getDaysAgoTime(System.currentTimeMillis(), hoursNum);
 //        Integer start = 1705916692;
 //        Integer end = 1705916714;
         try {

+ 32 - 7
price_collection/src/main/java/com/liangjiang/price_collection/service/impl/QueryService.java

@@ -45,14 +45,23 @@ public class QueryService {
                 results.add(dto);
             }
         }
+        // 时间切片
+        Set<Integer> times = generateArithmeticSequence(startTime, endTime);
         if(CollectionUtil.isEmpty(results)){
             log.warn("没有查询到数据");
-            return PriceListDto.builder().time(new HashSet<>()).values(new LinkedList<>()).build();
+            if (!isAdd) {
+                return PriceListDto.builder().time(new HashSet<>()).values(new LinkedList<>()).build();
+            }
+            PriceListDto empty_result = PriceListDto.builder().time(times).values(new LinkedList<>()).build();
+            List<String> emptyArr = new LinkedList<>();
+            for (String exchange : exchanges) {
+                empty_result.getValues().add(ExchangeCoinPriceArrDto.builder().exchange(exchange).symbol(coin).ask(emptyArr).bid(emptyArr).build());
+            }
+            return empty_result;
         }
         List<Future<ExchangeCoinPriceArrDto>> resultFutures = new LinkedList<>();
         List<ExchangeCoinPriceArrDto> data = new LinkedList<>();
-        //
-        Set<Integer> times = generateArithmeticSequence(startTime, endTime);
+
 
         // 获取已有时间的合集
 //        Set<Integer> times = new TreeSet<>();
@@ -71,7 +80,23 @@ public class QueryService {
             data.add(dto);
         }
         priceListDto.setTime(times);
-        priceListDto.setValues(data.stream().sorted(Comparator.comparing(ExchangeCoinPriceArrDto::getExchange)).collect(Collectors.toList()));
+        // 最终结果排序处理
+        priceListDto.setValues(data.stream()
+                .sorted(Comparator.comparing(ExchangeCoinPriceArrDto::getExchange))
+                .collect(Collectors.toList()));
+        // 交易所数量对不上则补
+        if (priceListDto.getValues().size() != exchanges.length) {
+            Set<String> processedExchanges = priceListDto.getValues().stream()
+                    .map(ExchangeCoinPriceArrDto::getExchange)
+                    .collect(Collectors.toSet());
+            List<String> emptyArr = new LinkedList<>();
+            for (String exchange : exchanges) {
+                if (!processedExchanges.contains(exchange)) {
+                    priceListDto.getValues().add(ExchangeCoinPriceArrDto.builder().exchange(exchange).ask(emptyArr).bid(emptyArr).symbol(coin).build());
+                }
+            }
+        }
+        
         return priceListDto;
     }
 
@@ -80,10 +105,10 @@ public class QueryService {
         executorService.shutdown();
     }
 
-    public static Integer[] getDaysAgoTime(long startTime, long dayNum){
+    public static Integer[] getDaysAgoTime(long startTime, long hoursNum){
         int startTimeInt = Math.toIntExact(startTime / 1000);
-        // 计算3天的毫秒数 (3天 * 24小时 * 60分钟 * 60秒 * 1000毫秒)
-        long threeDaysInMillis = dayNum * 24 * 60 * 60 * 1000;
+        // 计算hoursNum小时的毫秒数 (hoursNum * 60分钟 * 60秒 * 1000毫秒)
+        long threeDaysInMillis = hoursNum * 60 * 60 * 1000;
         // 计算3天前的毫秒级时间戳
         long threeDaysAgoMillis = startTime - threeDaysInMillis;
         // 将毫秒级时间戳转换为秒级时间戳

+ 2 - 2
price_collection/src/main/resources/application.yml

@@ -43,9 +43,9 @@ spring:
     # Redis服务器连接端口
     port: 6379
     # Redis服务器连接密码(默认为空)
-#    password: 43626546
+    password: 43626546
     # 连接超时时间
-    timeout: 1000ms
+    timeout: 3000ms
     lettuce:
       pool:
         # 连接池最大连接数