Browse Source

修改:接收到大量单据时分段处理

GDW 1 week ago
parent
commit
dff06bb32b

+ 35 - 1
src/main/java/cn/lttc/modules/lttcsamplescan/service/impl/SampleScanServiceImpl.java

@@ -32,6 +32,8 @@ import java.util.*;
 @Service
 @Service
 public class SampleScanServiceImpl implements ISampleScanService {
 public class SampleScanServiceImpl implements ISampleScanService {
 
 
+    final int BATCH_INSERT_LIMITS = 100;
+
     @Value("${file.upload-path}")
     @Value("${file.upload-path}")
     private String uploadDir;
     private String uploadDir;
     @Resource
     @Resource
@@ -243,7 +245,14 @@ public class SampleScanServiceImpl implements ISampleScanService {
         sampleScanMapper.addMainData(sampleScan);
         sampleScanMapper.addMainData(sampleScan);
 
 
         if (sampleScanDetails !=null && sampleScanDetails.size() > 0) {
         if (sampleScanDetails !=null && sampleScanDetails.size() > 0) {
-            sampleScanMapper.addDetailData(sampleScanDetails);
+            if (sampleScanDetails.size() >= BATCH_INSERT_LIMITS) {
+                List<List<SampleScanDetail>> lists = splitList(sampleScanDetails, BATCH_INSERT_LIMITS);
+                for (int i = 0; i < lists.size(); i++) {
+                    sampleScanMapper.addDetailData(lists.get(i));
+                }
+            } else {
+                sampleScanMapper.addDetailData(sampleScanDetails);
+            }
         }
         }
     }
     }
 
 
@@ -282,4 +291,29 @@ public class SampleScanServiceImpl implements ISampleScanService {
         }
         }
         return null;
         return null;
     }
     }
+
+    /**
+     * 将列表按指定大小分割
+     * @param list 原始列表
+     * @param size 每个子列表的大小
+     * @return 分割后的子列表集合
+     */
+    public static <T> List<List<T>> splitList(List<T> list, int size) {
+        List<List<T>> result = new ArrayList<>();
+        if (list == null || list.size() == 0 || size <= 0) {
+            return result;
+        }
+
+        int fullChunks = list.size() / size;
+        for (int i = 0; i < fullChunks; i++) {
+            result.add(new ArrayList<>(list.subList(i * size, (i + 1) * size)));
+        }
+
+        // 处理剩余的元素
+        if (fullChunks * size < list.size()) {
+            result.add(new ArrayList<>(list.subList(fullChunks * size, list.size())));
+        }
+
+        return result;
+    }
 }
 }

+ 3 - 0
src/main/resources/application.yml

@@ -26,6 +26,9 @@ spring:
 mybatis-plus:
 mybatis-plus:
   mapper-locations: classpath*:cn/lttc/modules/**/mapper/**/*.xml
   mapper-locations: classpath*:cn/lttc/modules/**/mapper/**/*.xml
   type-aliases-package: cn.lttc.modules
   type-aliases-package: cn.lttc.modules
+#  configuration:
+#    # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
+#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
 
 
 # sa-token 配置
 # sa-token 配置
 sa-token:
 sa-token: