From ab389a5a6b329b15a655340ba7b87bce7fd7871d Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期三, 24 十二月 2025 17:16:19 +0800
Subject: [PATCH] 添加新增设备自动生成编码

---
 mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/GlassInfoServiceImpl.java |  276 +++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 199 insertions(+), 77 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 36e73b8..d46acfb 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,10 +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;
 
 /**
  * 鐜荤拑淇℃伅鏈嶅姟瀹炵幇绫�
@@ -231,8 +233,8 @@
             return result;
         }
 
-        // 宸ョ▼鍙风敓鎴愶細姣忔瀵煎叆閮界敓鎴愭柊鐨勫伐绋嬪彿锛堜娇鐢ㄦ暟鎹簱鑷搴忓彿锛岄伩鍏嶉噸澶嶏級
-        final String engineerId = engineeringSequenceService.generateAndSaveEngineeringId(new Date());
+        // 宸ョ▼鍙风敓鎴愶細姣忔瀵煎叆閮界敓鎴愭柊鐨勫伐绋嬪彿锛堝厛鍙敓鎴愶紝涓嶄繚瀛樺埌鏁版嵁搴擄紝绛夊埌MES璋冪敤鎴愬姛鍚庡啀淇濆瓨锛�
+        final String engineerId = engineeringSequenceService.generateEngineeringId(new Date());
         final String filmsIdDefault = firstValue(excelRows, "filmsId", "鐧界幓");
         final double thicknessDefault = parseDouble(firstValue(excelRows, "thickness"), 0d);
 
@@ -241,75 +243,144 @@
         final String filmsIdDefaultFinal = filmsIdDefault;
         final double thicknessDefaultFinal = thicknessDefault;
 
-        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;
+        // 鐢熸垚鏃ユ湡瀛楃涓诧紙yyMMdd鏍煎紡锛夛紝鐢ㄤ簬娴佺▼鍗D鐢熸垚
+        LocalDate localDate = new Date().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
+        String dateStr = localDate.format(DateTimeFormatter.ofPattern("yyMMdd"));
+        
+        // 鐢ㄤ簬瀛樺偍姣忎釜鐜荤拑ID瀵瑰簲鐨勬祦绋嬪崱ID锛堝悓涓�鐜荤拑ID鐨勫涓幓鐠冨叡浜悓涓�涓祦绋嬪崱ID锛�
+        Map<String, String> glassIdFlowCardIdMap = new HashMap<>();
+        
+        // 鐢ㄤ簬鎸� flowCardId 璁℃暟 temperingFeedSequence
+        Map<String, Integer> temperingFeedSequenceCounter = new HashMap<>();
+        // 鐢ㄤ簬鎸� flowCardId 鍒嗛厤 temperingLayoutId
+        Map<String, Integer> temperingLayoutIdMap = new HashMap<>();
+        AtomicInteger nextTemperingLayoutId = new AtomicInteger(1);
+        
+        // 鍏堝缓绔嬪師鐗囨槧灏勶紝鐢ㄤ簬鍚庣画鍖归厤 rawSequence
+        Map<String, Integer> rawSequenceMap = new HashMap<>();
+        for (Map<String, Object> row : excelRows) {
+            double width = parseDouble(row.get("width"), 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;
+            if (!rawSequenceMap.containsKey(key)) {
+                rawSequenceMap.put(key, rawSequenceMap.size() + 1);
+            }
+        }
 
-                    String glassId = str(row.get("glassId"));
-                    String filmsId = strOrDefault(row.get("filmsId"), filmsIdDefaultFinal);
-                    String flowCardId = str(row.get("flowCardId"));
-                    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);
+        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;
 
-                    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;
+            String glassId = str(row.get("glassId"));
+            String filmsId = strOrDefault(row.get("filmsId"), filmsIdDefaultFinal);
+            String flowCardId = str(row.get("flowCardId"));
+            
+            // 濡傛灉娴佺▼鍗D涓虹┖锛屾寜鏂拌鍒欑敓鎴愶細NG + yyMMdd + 搴忓彿锛堜袱浣嶏紝浣跨敤鐜荤拑ID锛� + A001
+            if (flowCardId.isEmpty()) {
+                // 妫�鏌ユ槸鍚﹀凡涓鸿鐜荤拑ID鐢熸垚杩囨祦绋嬪崱ID锛堝悓涓�鐜荤拑ID鐨勫涓幓鐠冨叡浜悓涓�涓祦绋嬪崱ID锛�
+                String generatedFlowCardId = glassIdFlowCardIdMap.get(glassId);
+                if (generatedFlowCardId == null) {
+                    // 浣跨敤鐜荤拑ID浣滀负搴忓彿锛堣В鏋愪负鏁存暟锛屽鏋滆В鏋愬け璐ュ垯浣跨敤1锛�
+                    int sequence;
+                    try {
+                        sequence = Integer.parseInt(glassId.trim());
+                        if (sequence <= 0) {
+                            sequence = 1;
+                        }
+                    } catch (NumberFormatException e) {
+                        log.warn("鐜荤拑ID鏃犳硶瑙f瀽涓烘暣鏁帮紝浣跨敤榛樿鍊�1: glassId={}", glassId);
+                        sequence = 1;
+                    }
+                    generatedFlowCardId = "NG" + dateStr + String.format("%02d", sequence) + "A001";
+                    glassIdFlowCardIdMap.put(glassId, generatedFlowCardId);
+                    log.info("涓虹幓鐠僆D {} 鐢熸垚娴佺▼鍗D: flowCardId={}", glassId, generatedFlowCardId);
+                }
+                flowCardId = generatedFlowCardId;
+            }
+            // 鍘绘帀灏鹃儴 "/鏁板瓧"锛堝鏋滄湁锛�
+            String baseFlowCardId = flowCardId.replaceFirst("/\\d+$", "");
+            
+            // 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);
 
-                        String baseFlowCardId = flowCardId.isEmpty() ? baseGlassId : flowCardId;
-                        String finalFlowCardSequence = baseFlowCardId + "/" + (idx + 1);
-                        String finalFlowCardId = baseFlowCardId;
+            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);
 
-                        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", finalFlowCardId);
-                        m.put("productSortNumber", idx + 1);
-                        m.put("hollowCombineDirection", "0");
-                        m.put("width", width);
-                        m.put("height", height);
-                        m.put("thickness", thickness);
-                        m.put("filmsId", filmsId);
-                        m.put("layer", 0);
-                        m.put("totalLayer", 0);
-                        m.put("edgWidth", width);
-                        m.put("edgHeight", height);
-                        m.put("isMultiple", 0);
-                        m.put("maxWidth", width);
-                        m.put("maxHeight", height);
-                        m.put("isHorizontal", 0);
-                        m.put("rawSequence", 0);
-                        m.put("temperingLayoutId", 0);
-                        m.put("temperingFeedSequence", 0);
-                        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", 1);
+                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;
@@ -328,19 +399,44 @@
 
         List<Map<String, Object>> engineeringRawQueueList = rawGlassMap.values().stream().collect(Collectors.toList());
 
-        // 娴佺▼鍗′俊鎭�
+        // 娴佺▼鍗′俊鎭紙闇�瑕佺粺璁℃瘡涓� flowCardId 鐨勫疄闄呯幓鐠冩暟閲忥紝鑰冭檻 quantity锛�
         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 = glassId;
+                // 浣跨敤宸茬敓鎴愮殑娴佺▼鍗D锛堜笌glassInfolList涓殑閫昏緫淇濇寔涓�鑷达級
+                flowCardId = glassIdFlowCardIdMap.get(glassId);
+                if (flowCardId == null) {
+                    // 濡傛灉鏈敓鎴愶紝鍒欐寜瑙勫垯鐢熸垚锛堢悊璁轰笂涓嶅簲璇ヨ蛋鍒拌繖閲岋紝鍥犱负glassInfolList宸茬粡鐢熸垚杩囷級
+                    int sequence;
+                    try {
+                        sequence = Integer.parseInt(glassId.trim());
+                        if (sequence <= 0) {
+                            sequence = 1;
+                        }
+                    } catch (NumberFormatException e) {
+                        log.warn("鐜荤拑ID鏃犳硶瑙f瀽涓烘暣鏁帮紝浣跨敤榛樿鍊�1: glassId={}", glassId);
+                        sequence = 1;
+                    }
+                    flowCardId = "NG" + dateStr + String.format("%02d", sequence) + "A001";
+                    glassIdFlowCardIdMap.put(glassId, flowCardId);
+                    log.warn("娴佺▼鍗D鏈湪glassInfolList涓敓鎴愶紝姝ゅ琛ュ厖鐢熸垚: flowCardId={}, glassId={}", flowCardId, glassId);
+                }
             }
+            // 鍘绘帀灏鹃儴 "/鏁板瓧"锛堝鏋滄湁锛�
+            flowCardId = flowCardId.replaceFirst("/\\d+$", "");
+            // orderNumber 鏄暣鍨嬶紙鐜荤拑绫诲瀷锛夛紝浠� Excel 璇诲彇鎴栦娇鐢ㄩ粯璁ゅ�� 1
+            Object orderNumberObj = row.get("orderNumber");
+            Integer orderNumber = orderNumberObj != null 
+                    ? (int) parseDouble(orderNumberObj, 1) 
+                    : 1;
+            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"));
             String customerName = str(row.get("customerName"));
 
@@ -352,15 +448,16 @@
                 m.put("height", height);
                 m.put("thickness", thickness);
                 m.put("filmsId", filmsId);
-                m.put("totalLayer", 0);
-                m.put("layer", 0);
-                m.put("glassTotal", 1);
+                m.put("totalLayer", 1);
+                m.put("layer", 1);
+                m.put("glassTotal", qty); // 浣跨敤瀹為檯鏁伴噺
+                m.put("orderNumber", orderNumber);
                 m.put("productName", productName);
                 m.put("customerName", customerName);
                 flowCardMap.put(flowCardId, m);
             } else {
-                int count = (int) exist.getOrDefault("glassTotal", 1);
-                exist.put("glassTotal", count + 1);
+                int count = (int) exist.getOrDefault("glassTotal", 0);
+                exist.put("glassTotal", count + qty); // 绱姞鏁伴噺
             }
         }
         List<Map<String, Object>> flowCardInfoList = flowCardMap.values().stream().collect(Collectors.toList());
@@ -374,8 +471,33 @@
                 .mapToDouble(m -> parseDouble(m.get("rawGlassWidth"), 0d) * parseDouble(m.get("rawGlassHeight"), 0d) / 1_000_000d)
                 .sum();
 
+        // 璁$畻鏈�澶ц礋杞藉昂瀵革紙鐢ㄤ簬閽㈠寲鍙傛暟锛�
+        double maxLoadWidth = glassInfolList.stream()
+                .mapToDouble(m -> parseDouble(m.get("width"), 0d))
+                .max().orElse(2000);
+        double maxLoadLength = glassInfolList.stream()
+                .mapToDouble(m -> parseDouble(m.get("height"), 0d))
+                .max().orElse(3000);
+        
+        // 鐢熸垚閽㈠寲鍙傛暟
+        List<Map<String, Object>> temperingParameterList = new ArrayList<>();
+        Map<String, Object> temperingParam = new HashMap<>();
+        temperingParam.put("engineerId", engineerIdFinal);
+        temperingParam.put("heatMode", 1);
+        temperingParam.put("chaosPct", 50);
+        temperingParam.put("maxLoadPct", 100);
+        temperingParam.put("loadWidth", (int) Math.round(maxLoadWidth));
+        temperingParam.put("loadLength", (int) Math.round(maxLoadLength));
+        temperingParam.put("xSpace", 50);
+        temperingParam.put("ySpace", 50);
+        temperingParam.put("rotateMode", 0);
+        temperingParam.put("allowRotate", 0);
+        temperingParam.put("tempering", 1);
+        temperingParam.put("curtainWall", 0);
+        temperingParameterList.add(temperingParam);
+        
         result.put("engineerId", engineerIdFinal);
-        result.put("engineerName", "宸ョ▼_" + engineerIdFinal);
+        result.put("engineerName", thicknessDefaultFinal + "mm" + filmsIdDefaultFinal);
         result.put("avgAvailability", "90");
         result.put("validAvailability", "90");
         result.put("lastAvailability", "90");
@@ -513,18 +635,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