Forráskód Böngészése

feat:【获取表卡生命周期接口】仅最新的表数据入库

zhuangxingjv 1 hónapja
szülő
commit
07a7a13ae7

+ 33 - 8
ruoyi-api/src/main/java/com/ruoyi/api/service/impl/WwkjMeterCardHistoryServiceImpl.java

@@ -17,10 +17,10 @@ import org.springframework.stereotype.Service;
 import java.math.BigDecimal;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
+import java.util.stream.Collectors;
 
 @Service
 public class WwkjMeterCardHistoryServiceImpl implements IWwkjMeterCardHistoryService {
@@ -67,16 +67,41 @@ public class WwkjMeterCardHistoryServiceImpl implements IWwkjMeterCardHistorySer
                 return null;
             }
 
-            // 3. 转换数据并保存
+            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+            // 3. 转换数据并仅保留最新一条数据
             List<WwkjMeterCardHistory> historyList = new ArrayList<>();
-            for (ExternalMeterCardHistoryResponse.ExternalMeterCardHistoryData externalData : externalResponse.getData()) {
-                WwkjMeterCardHistory history = convertToMeterCardHistory(externalData);
+            // 3.1 根据 表卡编号 分组,并获取最新的一条数据
+            Map<String, ExternalMeterCardHistoryResponse.ExternalMeterCardHistoryData> collect = externalResponse.getData()
+                    .stream()
+                    .collect(Collectors.groupingBy(
+                            ExternalMeterCardHistoryResponse.ExternalMeterCardHistoryData::getCardCode,
+                            Collectors.collectingAndThen(
+                                    Collectors.maxBy(Comparator.comparing(
+                                            e -> LocalDateTime.parse(e.getOperateDate(), formatter)
+                                    )),
+                                    o -> o.orElse(null)
+                            )
+                    ));
+            // 3.2 数据入库
+            collect.values().forEach(e -> {
+                WwkjMeterCardHistory history = convertToMeterCardHistory(e);
                 historyList.add(history);
 
                 // 4. 保存到数据库(新增操作,生命周期记录通常只新增不更新)
                 history.setDelFlag("0"); // 设置删除标志为正常
                 wwkjMeterCardHistoryMapper.insertWwkjMeterCardHistory(history);
-            }
+            });
+
+//            // 3. 转换数据并保存
+//            List<WwkjMeterCardHistory> historyList = new ArrayList<>();
+//            for (ExternalMeterCardHistoryResponse.ExternalMeterCardHistoryData externalData : externalResponse.getData()) {
+//                WwkjMeterCardHistory history = convertToMeterCardHistory(externalData);
+//                historyList.add(history);
+//
+//                // 4. 保存到数据库(新增操作,生命周期记录通常只新增不更新)
+//                history.setDelFlag("0"); // 设置删除标志为正常
+//                wwkjMeterCardHistoryMapper.insertWwkjMeterCardHistory(history);
+//            }
 
             return historyList;
         } catch (Exception e) {