From 8ec0064cd95292f14027006a8be47c1a71f69af9 Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期三, 31 十二月 2025 17:02:57 +0800
Subject: [PATCH] 添加查询所有玻璃数据

---
 mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/GlassInfoServiceImpl.java |  202 +++++++++++++++++++++++++++++++++++--------------
 1 files changed, 143 insertions(+), 59 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 f07e714..0de3ef9 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
@@ -107,9 +107,10 @@
             return false;
         }
         try {
-            // 妫�鏌ユ槸鍚﹀凡瀛樺湪
-            GlassInfo existing = baseMapper.selectByGlassId(glassInfo.getGlassId());
+            // 鏌ヨ鍖呮嫭閫昏緫鍒犻櫎鐨勮褰�
+            GlassInfo existing = baseMapper.selectByGlassIdIncludingDeleted(glassInfo.getGlassId());
             if (existing != null) {
+                // 瀛樺湪鍒欐洿鏂�
                 glassInfo.setId(existing.getId());
                 // 淇濈暀鍘熷鍒涘缓淇℃伅
                 if (glassInfo.getCreatedTime() == null) {
@@ -118,15 +119,42 @@
                 if (glassInfo.getCreatedBy() == null) {
                     glassInfo.setCreatedBy(existing.getCreatedBy());
                 }
-                // 鏇存柊涓哄綋鍓嶆椂闂�
+                // 璁剧疆鏇存柊鏃堕棿
                 if (glassInfo.getUpdatedTime() == null) {
                     glassInfo.setUpdatedTime(new Date());
                 }
                 if (glassInfo.getUpdatedBy() == null) {
                     glassInfo.setUpdatedBy("system");
                 }
-                return updateById(glassInfo);
+
+                if (existing.getIsDeleted() != null && existing.getIsDeleted() != 0) {
+                    log.info("鎭㈠閫昏緫鍒犻櫎鐨勭幓鐠冧俊鎭�: glassId={}, id={}", glassInfo.getGlassId(), existing.getId());
+                    int updatedRows = baseMapper.restoreAndUpdateById(
+                            existing.getId(),
+                            glassInfo.getGlassId(),
+                            glassInfo.getGlassLength(),
+                            glassInfo.getGlassWidth(),
+                            glassInfo.getGlassThickness(),
+                            glassInfo.getStatus(),
+                            glassInfo.getState(),
+                            glassInfo.getEngineeringId(),
+                            glassInfo.getUpdatedTime() != null ? glassInfo.getUpdatedTime() : new Date(),
+                            glassInfo.getUpdatedBy() != null ? glassInfo.getUpdatedBy() : "system"
+                    );
+                    boolean updated = updatedRows > 0;
+                    log.info("鎭㈠閫昏緫鍒犻櫎璁板綍缁撴灉: glassId={}, updatedRows={}, updated={}", 
+                            glassInfo.getGlassId(), updatedRows, updated);
+                    if (!updated) {
+                        log.error("鎭㈠閫昏緫鍒犻櫎璁板綍澶辫触锛屽彲鑳藉師鍥狅細鏇存柊鏉′欢涓嶅尮閰嶆垨鏁版嵁寮傚父, glassId={}, id={}", 
+                                glassInfo.getGlassId(), existing.getId());
+                    }
+                    return updated;
+                } else {
+                    // 姝e父鏇存柊
+                    return updateById(glassInfo);
+                }
             } else {
+                // 涓嶅瓨鍦ㄥ垯鏂板
                 Date now = new Date();
                 if (glassInfo.getCreatedTime() == null) {
                     glassInfo.setCreatedTime(now);
@@ -139,6 +167,9 @@
                 }
                 if (glassInfo.getUpdatedBy() == null) {
                     glassInfo.setUpdatedBy("system");
+                }
+                if (glassInfo.getIsDeleted() == null) {
+                    glassInfo.setIsDeleted(0);
                 }
                 return save(glassInfo);
             }
@@ -154,10 +185,19 @@
             return true;
         }
         try {
+            int successCount = 0;
+            int failCount = 0;
             for (GlassInfo glassInfo : glassInfos) {
-                saveOrUpdateGlassInfo(glassInfo);
+                boolean result = saveOrUpdateGlassInfo(glassInfo);
+                if (result) {
+                    successCount++;
+                } else {
+                    failCount++;
+                    log.warn("淇濆瓨鎴栨洿鏂扮幓鐠冧俊鎭け璐�: glassId={}", glassInfo != null ? glassInfo.getGlassId() : "null");
+                }
             }
-            return true;
+            log.info("鎵归噺淇濆瓨鎴栨洿鏂扮幓鐠冧俊鎭畬鎴�: 鎬绘暟={}, 鎴愬姛={}, 澶辫触={}", glassInfos.size(), successCount, failCount);
+            return failCount == 0;
         } catch (Exception e) {
             log.error("鎵归噺淇濆瓨鎴栨洿鏂扮幓鐠冧俊鎭け璐�", e);
             return false;
@@ -233,8 +273,18 @@
             return result;
         }
 
-        // 宸ョ▼鍙风敓鎴愶細姣忔瀵煎叆閮界敓鎴愭柊鐨勫伐绋嬪彿锛堝厛鍙敓鎴愶紝涓嶄繚瀛樺埌鏁版嵁搴擄紝绛夊埌MES璋冪敤鎴愬姛鍚庡啀淇濆瓨锛�
-        final String engineerId = engineeringSequenceService.generateEngineeringId(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);
 
@@ -246,6 +296,15 @@
         // 鐢熸垚鏃ユ湡瀛楃涓诧紙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<>();
@@ -276,32 +335,25 @@
 
             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;
+            // 娴佺▼鍗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");
@@ -351,7 +403,7 @@
                 m.put("height", height);
                 m.put("thickness", thickness);
                 m.put("filmsId", filmsId);
-                m.put("layer", 1);
+                m.put("layer", layer);
                 m.put("totalLayer", 1);
                 m.put("edgWidth", width);
                 m.put("edgHeight", height);
@@ -403,26 +455,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()) {
-                // 浣跨敤宸茬敓鎴愮殑娴佺▼鍗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);
-                }
+            // 娴佺▼鍗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+$", "");
@@ -592,6 +633,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;
+    }
 
     /**
      * 淇濈暀涓や綅灏忔暟锛堝洓鑸嶄簲鍏ワ級
@@ -622,6 +697,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();
 
@@ -641,7 +723,7 @@
             // 涓庡鍏ヨ鍒欎繚鎸佷竴鑷达細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);
@@ -661,8 +743,12 @@
         }
 
         if (!glassInfos.isEmpty()) {
-            batchSaveOrUpdateGlassInfo(glassInfos);
-            log.info("宸蹭繚瀛� {} 鏉$幓鐠冧俊鎭埌鏈湴鏁版嵁搴擄紝宸ョ▼鍙�: {}", glassInfos.size(), engineeringId);
+            boolean success = batchSaveOrUpdateGlassInfo(glassInfos);
+            if (success) {
+                log.info("宸蹭繚瀛� {} 鏉$幓鐠冧俊鎭埌鏈湴鏁版嵁搴擄紝宸ョ▼鍙�: {}", glassInfos.size(), engineeringId);
+            } else {
+                log.error("淇濆瓨鐜荤拑淇℃伅鍒版湰鍦版暟鎹簱澶辫触锛屽伐绋嬪彿: {}, 鎬绘暟: {}", engineeringId, glassInfos.size());
+            }
         }
     }
 
@@ -719,14 +805,12 @@
         try {
             // 鍏堟煡璇㈣鍒犻櫎鐨勬暟閲忥紙鍒犻櫎鍓嶏級
             LambdaQueryWrapper<GlassInfo> countWrapper = new LambdaQueryWrapper<>();
-            countWrapper.eq(GlassInfo::getEngineeringId, engineeringId.trim())
-                       .eq(GlassInfo::getIsDeleted, 0); // 鏌ヨ鏈垹闄ょ殑璁板綍
+            countWrapper.eq(GlassInfo::getEngineeringId, engineeringId.trim());
             long count = this.count(countWrapper);
             
             // 浣跨敤MyBatis-Plus鐨剅emove鏂规硶锛屼細鏍规嵁@TableLogic鑷姩杩涜閫昏緫鍒犻櫎
             LambdaQueryWrapper<GlassInfo> removeWrapper = new LambdaQueryWrapper<>();
-            removeWrapper.eq(GlassInfo::getEngineeringId, engineeringId.trim())
-                        .eq(GlassInfo::getIsDeleted, 0); // 鍙垹闄ゆ湭鍒犻櫎鐨勮褰�
+            removeWrapper.eq(GlassInfo::getEngineeringId, engineeringId.trim());
             
             boolean result = this.remove(removeWrapper);
             if (result) {

--
Gitblit v1.8.0