From bd46f203b0d30a3d33b7c51ae9a528e4807477e1 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期四, 17 九月 2026 17:17:16 +0800
Subject: [PATCH] feat: 1.检验单详情温湿度 2.报告编制样品外观.平均值优化

---
 inspect-server/src/main/java/com/ruoyi/inspect/service/impl/InsOrderServiceImpl.java |   62 ++++++++++++++++++++++++-------
 1 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/inspect-server/src/main/java/com/ruoyi/inspect/service/impl/InsOrderServiceImpl.java b/inspect-server/src/main/java/com/ruoyi/inspect/service/impl/InsOrderServiceImpl.java
index c62c2ce..be9fcb1 100644
--- a/inspect-server/src/main/java/com/ruoyi/inspect/service/impl/InsOrderServiceImpl.java
+++ b/inspect-server/src/main/java/com/ruoyi/inspect/service/impl/InsOrderServiceImpl.java
@@ -84,6 +84,8 @@
 public class InsOrderServiceImpl extends ServiceImpl<InsOrderMapper, InsOrder>
         implements InsOrderService {
 
+    private static final String DIRECT_TEMPLATE_LABORATORY = "鏂拌仛";
+
     private InsOrderMapper insOrderMapper;
     private InsSampleService insSampleService;
     private InsSampleMapper insSampleMapper;
@@ -137,36 +139,52 @@
         insOrderMapper.updateById(insOrder);
         List<InsSample> insSamples = insSampleMapper.selectList(Wrappers.<InsSample>lambdaQuery().eq(InsSample::getInsOrderId, orderId));
         List<Integer> ids = insSamples.stream().map(InsSample::getId).collect(Collectors.toList());
-        List<InsProduct> insProducts = insProductMapper.selectList(Wrappers.<InsProduct>lambdaQuery()
-                .in(InsProduct::getInsSampleId, ids)
-                .eq(InsProduct::getState, 1)
-                .select(InsProduct::getSonLaboratory).groupBy(InsProduct::getSonLaboratory));
+        List<InsProduct> insProducts = ids.isEmpty()
+                ? Collections.emptyList()
+                : insProductMapper.selectList(Wrappers.<InsProduct>lambdaQuery()
+                        .in(InsProduct::getInsSampleId, ids)
+                        .eq(InsProduct::getState, 1)
+                        .select(InsProduct::getSonLaboratory).groupBy(InsProduct::getSonLaboratory));
+
+        InsOrder order = insOrderMapper.selectById(orderId);
+        boolean directTemplateOrder = isDirectTemplateOrder(order, insSamples);
+        Set<String> taskLaboratories = directTemplateOrder
+                ? Collections.singleton(DIRECT_TEMPLATE_LABORATORY)
+                : insProducts.stream()
+                        .map(InsProduct::getSonLaboratory)
+                        .filter(StringUtils::isNotBlank)
+                        .collect(Collectors.toCollection(LinkedHashSet::new));
+        if (!directTemplateOrder && taskLaboratories.size() != insProducts.size()) {
+            throw new ErrorException("璇ユ楠屽崟鏈夋湭缁存姢瀹為獙瀹ょ殑妫�楠岄」鐩�");
+        }
 
         // 鎵归噺娣诲姞妫�楠屼换鍔$姸鎬佽〃
-        List<InsOrderState> insOrderStateList = insProducts.stream().map(insProduct -> {
+        List<InsOrderState> insOrderStateList = taskLaboratories.stream()
+                .filter(laboratory -> insOrderStateService.count(Wrappers.<InsOrderState>lambdaQuery()
+                        .eq(InsOrderState::getInsOrderId, orderId)
+                        .eq(InsOrderState::getLaboratory, laboratory)) == 0)
+                .map(laboratory -> {
             InsOrderState insOrderState = new InsOrderState();
             insOrderState.setInsOrderId(orderId);
-            try {
-                insOrderState.setLaboratory(insProduct.getSonLaboratory());
-            } catch (NullPointerException e) {
-                throw new ErrorException("璇ユ楠屽崟鏈夋湭缁存姢瀹為獙瀹ょ殑妫�楠岄」鐩�");
-            }
+            insOrderState.setLaboratory(laboratory);
             insOrderState.setInsState(0);
             return insOrderState;
         }).collect(Collectors.toList());
-        insOrderStateService.saveBatch(insOrderStateList);
+        if (!insOrderStateList.isEmpty()) {
+            insOrderStateService.saveBatch(insOrderStateList);
+        }
 
         if (userId != null) {
             InsSampleUser insSampleUser = new InsSampleUser();
             insSampleUser.setState(0);
             insSampleUser.setUserId(userId);
             insSampleUser.setInsSampleId(orderId);
-            insSampleUser.setSonLaboratory(sonLaboratory);
+            insSampleUser.setSonLaboratory(directTemplateOrder
+                    ? DIRECT_TEMPLATE_LABORATORY : sonLaboratory);
             insSampleUserMapper.insert(insSampleUser);
         }
 
         // 鍒ゆ柇璁㈠崟鏈夋病鏈夌粦瀹氭娊鏍疯鍒�
-        InsOrder order = insOrderMapper.selectById(orderId);
         if (order.getQuarterItemId() != null) {
            // 闇�瑕佹坊鍔犱笅鍙戞椂闂村埌鎶芥牱鏃堕棿, 鍙栨牱浜哄憳灏辨槸妫�娴嬩汉
             SpotCheckQuarterItem spotCheckQuarterItem = spotCheckQuarterItemMapper.selectById(order.getQuarterItemId());
@@ -177,6 +195,22 @@
         }
 
         return 1;
+    }
+
+    private boolean isDirectTemplateOrder(InsOrder order, List<InsSample> samples) {
+        if (order == null || CollectionUtils.isEmpty(samples)) {
+            return false;
+        }
+        Long snapshotCount = insOrderStandardTemplateMapper.selectCount(
+                Wrappers.<InsOrderStandardTemplate>lambdaQuery()
+                        .eq(InsOrderStandardTemplate::getInsOrderId, order.getId()));
+        return snapshotCount > 0 && samples.stream().anyMatch(sample ->
+                sample.getStandardMethodListId() == null
+                        && StringUtils.isBlank(sample.getFactory())
+                        && StringUtils.isBlank(sample.getSampleType())
+                        && StringUtils.isBlank(sample.getModel())
+                        && StringUtils.isBlank(order.getFactory())
+                        && StringUtils.isBlank(order.getSampleType()));
     }
 
 
@@ -530,7 +564,7 @@
             product.setModel(null);
             product.setTemplateId(item.getTemplateId());
             product.setTemplateRowIndex(item.getTemplateRowIndex());
-            product.setSonLaboratory("鏂拌仛");
+            product.setSonLaboratory(DIRECT_TEMPLATE_LABORATORY);
             product.setState(1);
             product.setSort(productSort++);
             insProductMapper.insert(product);

--
Gitblit v1.9.3