From a78add24db92e8d276f9473e077a136c44dc5625 Mon Sep 17 00:00:00 2001
From: sfd <sun.sunshine@163.com>
Date: 星期三, 21 五月 2025 11:42:02 +0800
Subject: [PATCH] 修改导入数据重复问题

---
 aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipelinePredictionServiceImpl.java |   30 +++++++++++++++---------------
 1 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipelinePredictionServiceImpl.java b/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipelinePredictionServiceImpl.java
index f6e3a61..3012ee2 100644
--- a/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipelinePredictionServiceImpl.java
+++ b/aps-modules/aps-core/src/main/java/com/aps/core/service/impl/ApsGasPipelinePredictionServiceImpl.java
@@ -22,6 +22,7 @@
 import java.sql.Timestamp;
 import java.text.SimpleDateFormat;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.stream.Collectors;
 
 /**
@@ -112,9 +113,9 @@
         int rows = sheet.getLastRowNum();
         if (rows > 0) {
             List<ApsGasPipelinePrediction> list = new ArrayList<>();
-
+            Set<String> keys = new HashSet<>();
             /*鏁版嵁鍒椾粠1寮�濮�*/
-            for (int i = 1; i <= rows; i++) {
+            for (int i = rows; i > 0; --i) {
                 Row row = sheet.getRow(i);
                 if (row.getCell(0) == null) {
                     continue;
@@ -137,32 +138,31 @@
                     item.setCreateTime(DateUtils.getNowDate());
                     item.setPredictQuantity(new BigDecimal(quantity));
                     item.setPredictDate(new Timestamp(date.getTime()));
-                    list.add(item);
+                    if (!keys.contains(item.getKey())) {
+                        keys.add(item.getKey());
+                        list.add(item);
+                    }
                 }
             }
             if (!list.isEmpty()) {
-
-                List<Map<String, Object>> facCodeKey = apsGasPipelinePredictionMapper.selectByFacOrMaterial(
-                        list.stream().map(ApsGasPipelinePrediction::getFactory).collect(Collectors.toSet()),
-                        list.stream().map(ApsGasPipelinePrediction::getMaterialCode).collect(Collectors.toSet())
-                );
+                List<Map<String, Object>> facCodeKey = apsGasPipelinePredictionMapper.selectByFacOrMaterial(keys);
                 if (!facCodeKey.isEmpty()) {
-                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                     Iterator<ApsGasPipelinePrediction> it = list.iterator();
                     while (it.hasNext()) {
                         ApsGasPipelinePrediction item = it.next();
-                        String key = item.getFactory() + "##" + item.getMaterialCode() + "##" + sdf.format(item.getPredictDate());
-                        facCodeKey.stream()
-                                .filter(map -> map.get("key").equals(key))
-                                .forEach(row -> {
+                        long count = facCodeKey.stream()
+                                .filter(map -> map.get("key").equals(item.getKey()))
+                                .peek(row -> {
                                     item.setId((Long) row.get("id"));
                                     item.setCreateBy(null);
                                     item.setCreateTime(null);
                                     item.setUpdateBy(SecurityUtils.getUsername());
                                     item.setUpdateTime(new Timestamp(System.currentTimeMillis()));
                                     apsGasPipelinePredictionMapper.updateById(item);
-                                    it.remove();
-                                });
+                                }).count();
+                        if (count > 0){
+                            it.remove();
+                        }
                     }
                 }
                 if (!list.isEmpty()) {

--
Gitblit v1.9.3