From 16f4bb3c28fc85cffcc511718c903ada9fdab134 Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期五, 26 十二月 2025 16:59:45 +0800
Subject: [PATCH] 调用mes导入工程参数修改,Excel表数据流程卡号一致;增加读取层号/工程号方法; 工程号一致覆盖更新

---
 mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/GlassInfoServiceImpl.java |  279 ++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 183 insertions(+), 96 deletions(-)

diff --git a/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/GlassInfoServiceImpl.java b/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/GlassInfoServiceImpl.java
index e0c1ea0..3e04c93 100644
--- a/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/GlassInfoServiceImpl.java
+++ b/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/GlassInfoServiceImpl.java
@@ -15,11 +15,12 @@
 import org.springframework.util.CollectionUtils;
 
 import java.math.BigDecimal;
+import java.time.LocalDate;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
 import java.util.*;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
-
-import static java.util.stream.IntStream.range;
 
 /**
  * 鐜荤拑淇℃伅鏈嶅姟瀹炵幇绫�
@@ -232,8 +233,18 @@
             return result;
         }
 
-        // 宸ョ▼鍙风敓鎴愶細姣忔瀵煎叆閮界敓鎴愭柊鐨勫伐绋嬪彿锛堜娇鐢ㄦ暟鎹簱鑷搴忓彿锛岄伩鍏嶉噸澶嶏級
-        final String engineerId = engineeringSequenceService.generateAndSaveEngineeringId(new Date());
+        // 宸ョ▼鍙凤細浠h〃鏁翠釜Excel琛紝浼樺厛浣跨敤Excel涓殑宸ョ▼鍙凤紙浠庣涓�琛屾垨浠绘剰涓�琛岃幏鍙栵級锛屽鏋滄墍鏈夎閮芥病鏈夊垯鑷姩鐢熸垚
+        String engineerIdFromExcel = null;
+        for (Map<String, Object> row : excelRows) {
+            String engineeringId = str(row.get("engineeringId"));
+            if (engineeringId != null && !engineeringId.trim().isEmpty()) {
+                engineerIdFromExcel = engineeringId.trim();
+                break; // 鎵惧埌绗竴涓潪绌虹殑宸ョ▼鍙峰氨浣跨敤
+            }
+        }
+        final String engineerId = engineerIdFromExcel != null 
+                ? engineerIdFromExcel 
+                : engineeringSequenceService.generateEngineeringId(new Date());
         final String filmsIdDefault = firstValue(excelRows, "filmsId", "鐧界幓");
         final double thicknessDefault = parseDouble(firstValue(excelRows, "thickness"), 0d);
 
@@ -242,8 +253,22 @@
         final String filmsIdDefaultFinal = filmsIdDefault;
         final double thicknessDefaultFinal = thicknessDefault;
 
-        // 鐢ㄤ簬鎸� flowCardId 鍏ㄥ眬璁℃暟搴忓彿
-        Map<String, Integer> flowCardSequenceCounter = new HashMap<>();
+        // 鐢熸垚鏃ユ湡瀛楃涓诧紙yyMMdd鏍煎紡锛夛紝鐢ㄤ簬娴佺▼鍗D鐢熸垚
+        LocalDate localDate = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
+        String dateStr = localDate.format(DateTimeFormatter.ofPattern("yyMMdd"));
+        
+        // 妫�鏌ユ槸鍚︽湁娴佺▼鍗D锛氬鏋滄墍鏈夎鐨勬祦绋嬪崱ID閮戒负绌猴紝鍒欐墍鏈夎褰曞叡浜悓涓�涓祦绋嬪崱ID
+        boolean allFlowCardIdEmpty = excelRows.stream()
+                .allMatch(row -> {
+                    String flowCardId = str(row.get("flowCardId"));
+                    return flowCardId == null || flowCardId.trim().isEmpty();
+                });
+        // 濡傛灉鎵�鏈夋祦绋嬪崱ID閮戒负绌猴紝鐢熸垚涓�涓叡浜殑娴佺▼鍗D
+        String sharedFlowCardId = allFlowCardIdEmpty ? "NG" + dateStr + "01A001" : null;
+        
+        // 鐢ㄤ簬瀛樺偍姣忎釜鐜荤拑ID瀵瑰簲鐨勬祦绋嬪崱ID锛堝悓涓�鐜荤拑ID鐨勫涓幓鐠冨叡浜悓涓�涓祦绋嬪崱ID锛�
+        Map<String, String> glassIdFlowCardIdMap = new HashMap<>();
+        
         // 鐢ㄤ簬鎸� flowCardId 璁℃暟 temperingFeedSequence
         Map<String, Integer> temperingFeedSequenceCounter = new HashMap<>();
         // 鐢ㄤ簬鎸� flowCardId 鍒嗛厤 temperingLayoutId
@@ -254,7 +279,7 @@
         Map<String, Integer> rawSequenceMap = new HashMap<>();
         for (Map<String, Object> row : excelRows) {
             double width = parseDouble(row.get("width"), 0d);
-            double height = parseDouble(row.get("height"), 0d);
+            double height = parseDouble(row.get("length"), 0d);
             double thickness = parseDouble(row.get("thickness"), thicknessDefaultFinal);
             String filmsId = strOrDefault(row.get("filmsId"), filmsIdDefaultFinal);
             String key = width + "_" + height + "_" + thickness + "_" + filmsId;
@@ -263,96 +288,111 @@
             }
         }
 
-        List<Map<String, Object>> glassInfolList = excelRows.stream()
-                .flatMap(row -> {
-                    Object qtyObj = row.getOrDefault("quantity", 1);
-                    int qty = parseDouble(qtyObj, 1) > 0 ? (int) parseDouble(qtyObj, 1) : 1;
+        List<Map<String, Object>> glassInfolList = new ArrayList<>();
+        for (Map<String, Object> row : excelRows) {
+            Object qtyObj = row.getOrDefault("quantity", 1);
+            int qty = parseDouble(qtyObj, 1) > 0 ? (int) parseDouble(qtyObj, 1) : 1;
 
-                    String glassId = str(row.get("glassId"));
-                    String filmsId = strOrDefault(row.get("filmsId"), filmsIdDefaultFinal);
-                    String flowCardId = str(row.get("flowCardId"));
-                    // orderNumber 鏄暣鍨嬶紙鐜荤拑绫诲瀷锛夛紝浠� Excel 璇诲彇鎴栦娇鐢ㄩ粯璁ゅ�� 1
-                    Object orderNumberObj = row.get("orderNumber");
-                    final Integer finalOrderNumber = orderNumberObj != null 
-                            ? (int) parseDouble(orderNumberObj, 1) 
-                            : 1;
-                    String productName = str(row.get("productName"));
-                    String customerName = str(row.get("customerName"));
-                    double width = parseDouble(row.get("width"), 0d);
-                    double height = parseDouble(row.get("height"), 0d);
-                    double thickness = parseDouble(row.get("thickness"), thicknessDefaultFinal);
-                    
-                    // 璁$畻 rawSequence
-                    String rawKey = width + "_" + height + "_" + thickness + "_" + filmsId;
-                    Integer rawSequence = rawSequenceMap.get(rawKey);
+            String glassId = str(row.get("glassId"));
+            String filmsId = strOrDefault(row.get("filmsId"), filmsIdDefaultFinal);
+            // 娴佺▼鍗D锛氬鏋淓xcel涓湁锛屼娇鐢‥xcel鐨勶紱濡傛灉涓虹┖锛屼娇鐢ㄥ叡浜殑娴佺▼鍗D
+            String flowCardIdFromExcel = str(row.get("flowCardId"));
+            String flowCardId;
+            if (flowCardIdFromExcel != null && !flowCardIdFromExcel.trim().isEmpty()) {
+                // Excel涓湁娴佺▼鍗D锛屼娇鐢‥xcel鐨�
+                flowCardId = flowCardIdFromExcel.trim();
+            } else {
+                // Excel涓祦绋嬪崱ID涓虹┖锛屼娇鐢ㄥ叡浜殑娴佺▼鍗D
+                flowCardId = sharedFlowCardId != null ? sharedFlowCardId : getOrGenerateFlowCardId(glassId, dateStr, glassIdFlowCardIdMap);
+            }
+            // 鍘绘帀灏鹃儴 "/鏁板瓧"锛堝鏋滄湁锛�
+            String baseFlowCardId = flowCardId.replaceFirst("/\\d+$", "");
+            
+            // 灞傚彿锛氬鏋淓xcel涓湁锛屼娇鐢‥xcel鐨勶紱濡傛灉娌℃湁锛岄粯璁�1
+            Object layerObj = row.get("layer");
+            int layer = layerObj != null ? (int) parseDouble(layerObj, 1) : 1;
+            if (layer <= 0) {
+                layer = 1;
+            }
+            
+            // orderNumber 鏄暣鍨嬶紙鐜荤拑绫诲瀷锛夛紝浠� Excel 璇诲彇鎴栦娇鐢ㄩ粯璁ゅ�� 1
+            Object orderNumberObj = row.get("orderNumber");
+            final Integer finalOrderNumber = orderNumberObj != null 
+                    ? (int) parseDouble(orderNumberObj, 1) 
+                    : 1;
+            String productName = str(row.get("productName"));
+            String customerName = str(row.get("customerName"));
+            double width = parseDouble(row.get("width"), 0d);
+            double height = parseDouble(row.get("length"), 0d);
+            double thickness = parseDouble(row.get("thickness"), thicknessDefaultFinal);
+            
+            // 璁$畻 rawSequence
+            String rawKey = width + "_" + height + "_" + thickness + "_" + filmsId;
+            Integer rawSequence = rawSequenceMap.get(rawKey);
 
-                    int finalQty = qty;
-                    log.info("瑙f瀽鍒版暟閲忥細row={}, quantity={}, 鏈�缁坬ty={}", row, qtyObj, finalQty);
-                    return range(0, qty).mapToObj(idx -> {
-                        String baseGlassId = engineerIdFinal + glassId;
-                        String finalGlassId = finalQty > 1 ? baseGlassId + (idx + 1) : baseGlassId;
+            int finalQty = qty;
+            log.info("瑙f瀽鍒版暟閲忥細row={}, quantity={}, 鏈�缁坬ty={}", row, qtyObj, finalQty);
+            
+            // 鎸� flowCardId 鍒嗛厤 temperingLayoutId
+            Integer temperingLayoutId = temperingLayoutIdMap.computeIfAbsent(baseFlowCardId, k -> nextTemperingLayoutId.getAndIncrement());
+            
+            // 涓哄悓涓�琛岀殑澶氫釜鐜荤拑鐢熸垚鏁版嵁
+            for (int idx = 0; idx < qty; idx++) {
+                String baseGlassId = engineerIdFinal + glassId;
+                String finalGlassId = finalQty > 1 ? baseGlassId + (idx + 1) : baseGlassId;
+                
+                // 鎸� flowCardId 閫掑 temperingFeedSequence
+                int temperingFeedSequence = temperingFeedSequenceCounter.compute(baseFlowCardId, (k, v) -> (v == null ? 0 : v) + 1);
+                String finalFlowCardSequence = baseFlowCardId + "/" + 1;
+                
+                log.debug("鐢熸垚鐜荤拑淇℃伅: glassId={}, idx={}, baseFlowCardId={}, finalFlowCardSequence={}, temperingLayoutId={}, temperingFeedSequence={}", 
+                        glassId, idx, baseFlowCardId, finalFlowCardSequence, temperingLayoutId, temperingFeedSequence);
 
-                        String baseFlowCardId = flowCardId.isEmpty() ? baseGlassId : flowCardId;
-                        // 濡傛灉 baseFlowCardId 宸茬粡鍖呭惈灏鹃儴 "/鏁板瓧"锛屽厛鍘绘帀锛屽啀鐢卞悗绔粺涓�杩藉姞搴忓彿
-                        baseFlowCardId = baseFlowCardId.replaceFirst("/\\d+$", "");
-                        // 鎸� flowCardId 鍏ㄥ眬閫掑搴忓彿
-                        int sequenceNum = flowCardSequenceCounter.compute(baseFlowCardId, (k, v) -> (v == null ? 0 : v) + 1);
-                        String finalFlowCardSequence = baseFlowCardId + "/" + 1;
-                        
-                        // 鎸� flowCardId 鍒嗛厤 temperingLayoutId
-                        Integer temperingLayoutId = temperingLayoutIdMap.computeIfAbsent(baseFlowCardId, k -> nextTemperingLayoutId.getAndIncrement());
-                        // 鎸� flowCardId 閫掑 temperingFeedSequence
-                        int temperingFeedSequence = temperingFeedSequenceCounter.compute(baseFlowCardId, (k, v) -> (v == null ? 0 : v) + 1);
-                        
-                        log.debug("鐢熸垚flowCardSequence: idx={}, baseFlowCardId={}, sequenceNum={}, finalFlowCardSequence={}, temperingLayoutId={}, temperingFeedSequence={}", 
-                                idx, baseFlowCardId, sequenceNum, finalFlowCardSequence, temperingLayoutId, temperingFeedSequence);
-
-                        Map<String, Object> m = new HashMap<>();
-                        m.put("xAxis", 0);
-                        m.put("xCoordinate", 0);
-                        m.put("yAxis", 0);
-                        m.put("yCoordinate", 0);
-                        m.put("glassId", finalGlassId);
-                        m.put("engineerId", engineerIdFinal);
-                        m.put("flowCardId", baseFlowCardId);
-                        m.put("orderNumber", finalOrderNumber);
-                        m.put("productSortNumber", 1); // 缁熶竴涓�1
-                        m.put("hollowCombineDirection", "");
-                        m.put("width", width);
-                        m.put("height", height);
-                        m.put("thickness", thickness);
-                        m.put("filmsId", filmsId);
-                        m.put("layer", 1);
-                        m.put("totalLayer", 1);
-                        m.put("edgWidth", width);
-                        m.put("edgHeight", height);
-                        m.put("isMultiple", finalQty > 1 ? 1 : 0); // 鏁伴噺>1鏃朵负1
-                        m.put("maxWidth", width);
-                        m.put("maxHeight", height);
-                        m.put("isHorizontal", 0);
-                        m.put("rawSequence", rawSequence != null ? rawSequence : 0);
-                        m.put("temperingLayoutId", temperingLayoutId);
-                        m.put("temperingFeedSequence", temperingFeedSequence);
-                        m.put("angle", 0);
-                        m.put("ruleId", 0);
-                        m.put("combine", 0);
-                        m.put("markIcon", "");
-                        m.put("filmRemove", 0);
-                        m.put("flowCardSequence", finalFlowCardSequence);
-                        m.put("process", "");
-                        m.put("rawAngle", 0);
-                        m.put("graphNo", 0);
-                        m.put("processParam", "");
-                        return m;
-                    });
-                })
-                .collect(Collectors.toList());
+                Map<String, Object> m = new HashMap<>();
+                m.put("xAxis", 0);
+                m.put("xCoordinate", 0);
+                m.put("yAxis", 0);
+                m.put("yCoordinate", 0);
+                m.put("glassId", finalGlassId);
+                m.put("engineerId", engineerIdFinal);
+                m.put("flowCardId", baseFlowCardId);
+                m.put("orderNumber", finalOrderNumber);
+                m.put("productSortNumber", 1);
+                m.put("hollowCombineDirection", "");
+                m.put("width", width);
+                m.put("height", height);
+                m.put("thickness", thickness);
+                m.put("filmsId", filmsId);
+                m.put("layer", layer);
+                m.put("totalLayer", 1);
+                m.put("edgWidth", width);
+                m.put("edgHeight", height);
+                m.put("isMultiple", finalQty > 1 ? 1 : 0);
+                m.put("maxWidth", width);
+                m.put("maxHeight", height);
+                m.put("isHorizontal", 0);
+                m.put("rawSequence", rawSequence != null ? rawSequence : 0);
+                m.put("temperingLayoutId", temperingLayoutId);
+                m.put("temperingFeedSequence", temperingFeedSequence);
+                m.put("angle", 0);
+                m.put("ruleId", 0);
+                m.put("combine", 0);
+                m.put("markIcon", "");
+                m.put("filmRemove", 0);
+                m.put("flowCardSequence", finalFlowCardSequence);
+                m.put("process", "");
+                m.put("rawAngle", 0);
+                m.put("graphNo", 0);
+                m.put("processParam", "");
+                glassInfolList.add(m);
+            }
+        }
 
         // 鍘熺墖淇℃伅鍘婚噸
         Map<String, Map<String, Object>> rawGlassMap = new HashMap<>();
         for (Map<String, Object> row : excelRows) {
             double width = parseDouble(row.get("width"), 0d);
-            double height = parseDouble(row.get("height"), 0d);
+            double height = parseDouble(row.get("length"), 0d);
             double thickness = parseDouble(row.get("thickness"), thicknessDefaultFinal);
             String filmsId = strOrDefault(row.get("filmsId"), filmsIdDefaultFinal);
             String key = width + "_" + height + "_" + thickness + "_" + filmsId;
@@ -375,9 +415,15 @@
         Map<String, Map<String, Object>> flowCardMap = new HashMap<>();
         for (Map<String, Object> row : excelRows) {
             String glassId = str(row.get("glassId"));
-            String flowCardId = str(row.get("flowCardId"));
-            if (flowCardId.isEmpty()) {
-                flowCardId = engineerIdFinal + glassId;
+            // 娴佺▼鍗D锛氬鏋淓xcel涓湁锛屼娇鐢‥xcel鐨勶紱濡傛灉涓虹┖锛屼娇鐢ㄥ叡浜殑娴佺▼鍗D锛堜笌glassInfolList閫昏緫涓�鑷达級
+            String flowCardIdFromExcel = str(row.get("flowCardId"));
+            String flowCardId;
+            if (flowCardIdFromExcel != null && !flowCardIdFromExcel.trim().isEmpty()) {
+                // Excel涓湁娴佺▼鍗D锛屼娇鐢‥xcel鐨�
+                flowCardId = flowCardIdFromExcel.trim();
+            } else {
+                // Excel涓祦绋嬪崱ID涓虹┖锛屼娇鐢ㄥ叡浜殑娴佺▼鍗D
+                flowCardId = sharedFlowCardId != null ? sharedFlowCardId : getOrGenerateFlowCardId(glassId, dateStr, glassIdFlowCardIdMap);
             }
             // 鍘绘帀灏鹃儴 "/鏁板瓧"锛堝鏋滄湁锛�
             flowCardId = flowCardId.replaceFirst("/\\d+$", "");
@@ -389,7 +435,7 @@
             Object qtyObj = row.getOrDefault("quantity", 1);
             int qty = parseDouble(qtyObj, 1) > 0 ? (int) parseDouble(qtyObj, 1) : 1;
             double width = parseDouble(row.get("width"), 0d);
-            double height = parseDouble(row.get("height"), 0d);
+            double height = parseDouble(row.get("length"), 0d);
             double thickness = parseDouble(row.get("thickness"), thicknessDefaultFinal);
             String filmsId = strOrDefault(row.get("filmsId"), filmsIdDefaultFinal);
             String productName = str(row.get("productName"));
@@ -547,6 +593,40 @@
             return def;
         }
     }
+    
+    /**
+     * 鑾峰彇鎴栫敓鎴愭祦绋嬪崱ID
+     * 濡傛灉宸插瓨鍦ㄥ垯杩斿洖锛屽惁鍒欑敓鎴愭柊鐨勬祦绋嬪崱ID骞剁紦瀛�
+     * 鍓嶇鏍煎紡锛氬師濮媑lassId + 涓や綅搴忓彿锛堝"101"銆�"102"銆�"201"锛夛紝閫氳繃闄や互100鍘绘帀鏈�鍚庝袱浣嶆彁鍙栧師濮媑lassId
+     * 
+     * @param glassId 鐜荤拑ID锛堝"101"銆�"102"銆�"201"锛�
+     * @param dateStr 鏃ユ湡瀛楃涓诧紙yyMMdd鏍煎紡锛�
+     * @param glassIdFlowCardIdMap 鐜荤拑ID鍒版祦绋嬪崱ID鐨勬槧灏勭紦瀛�
+     * @return 娴佺▼鍗D锛堟牸寮忥細NG + yyMMdd + 搴忓彿锛堜袱浣嶏級 + A001锛�
+     */
+    private String getOrGenerateFlowCardId(String glassId, String dateStr, Map<String, String> glassIdFlowCardIdMap) {
+        String flowCardId = glassIdFlowCardIdMap.get(glassId);
+        if (flowCardId == null) {
+            // 浠巊lassId涓彁鍙栧師濮嬫暟瀛楋細101 -> 1, 102 -> 1, 201 -> 2
+            int sequence = 1;
+            if (glassId != null && !glassId.trim().isEmpty()) {
+                try {
+                    String cleaned = glassId.trim().split("[\\r\\n\\t\\s]+")[0];
+                    if (cleaned.matches("\\d+")) {
+                        int num = Integer.parseInt(cleaned);
+                        // 濡傛灉闀垮害>=3锛岄櫎浠�100鍘绘帀鏈�鍚庝袱浣嶏紙鍓嶇杩藉姞鐨勫簭鍙凤級
+                        sequence = (cleaned.length() >= 3 && num >= 100) ? num / 100 : (num > 0 ? num : 1);
+                    }
+                } catch (Exception e) {
+                    log.error("浠巊lassId涓彁鍙栧師濮嬫暟瀛楀け璐�: glassId={}", glassId, e);
+                }
+            }
+            flowCardId = "NG" + dateStr + String.format("%02d", sequence) + "A001";
+            glassIdFlowCardIdMap.put(glassId, flowCardId);
+            log.info("涓虹幓鐠僆D {} 鐢熸垚娴佺▼鍗D: flowCardId={}", glassId, flowCardId);
+        }
+        return flowCardId;
+    }
 
     /**
      * 淇濈暀涓や綅灏忔暟锛堝洓鑸嶄簲鍏ワ級
@@ -577,6 +657,13 @@
             return;
         }
 
+        // 濡傛灉宸ョ▼鍙峰凡瀛樺湪锛屽厛鍒犻櫎璇ュ伐绋嬪彿涓嬬殑鏃ф暟鎹紝瀹炵幇瑕嗙洊鏇存柊
+        List<GlassInfo> existingGlassInfos = getGlassInfosByEngineeringId(engineeringId.trim());
+        if (!existingGlassInfos.isEmpty()) {
+            log.info("妫�娴嬪埌宸ョ▼鍙� {} 宸插瓨鍦� {} 鏉¤褰曪紝灏嗗垹闄ゆ棫鏁版嵁骞舵洿鏂�", engineeringId, existingGlassInfos.size());
+            deleteGlassInfosByEngineeringId(engineeringId.trim());
+        }
+
         List<GlassInfo> glassInfos = new ArrayList<>();
         Date now = new Date();
 
@@ -590,18 +677,18 @@
             if (qty <= 0) qty = 1;
 
             double width = parseDouble(row.get("width"), 0d);
-            double height = parseDouble(row.get("height"), 0d);
+            double length = parseDouble(row.get("length"), 0d);
             double thickness = parseDouble(row.get("thickness"), 0d);
 
             // 涓庡鍏ヨ鍒欎繚鎸佷竴鑷达細glassId 鍓嶅姞宸ョ▼鍙峰墠缂�锛屾暟閲�>1鏃惰拷鍔犲簭鍙�
             String baseGlassId = engineeringId.trim() + glassId;
             for (int idx = 0; idx < qty; idx++) {
-                String finalGlassId = qty > 1 ? baseGlassId + "_" + (idx + 1) : baseGlassId;
+                String finalGlassId = qty > 1 ? baseGlassId + (idx + 1) : baseGlassId;
 
                 GlassInfo glassInfo = new GlassInfo();
                 glassInfo.setGlassId(finalGlassId);
                 glassInfo.setEngineeringId(engineeringId.trim());
-                glassInfo.setGlassLength((int) Math.round(height));
+                glassInfo.setGlassLength((int) Math.round(length));
                 glassInfo.setGlassWidth((int) Math.round(width));
                 glassInfo.setGlassThickness(BigDecimal.valueOf(thickness));
                 glassInfo.setStatus(GlassInfo.Status.ACTIVE);

--
Gitblit v1.8.0