From 9571229a2013472dc701ecf5767f2873b36d8f90 Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期四, 11 十二月 2025 17:07:19 +0800
Subject: [PATCH] 修复导入Excel功能工程号自增; 添加选择工程号自动填写玻璃id列表

---
 mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/GlassInfoServiceImpl.java |  227 ++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 169 insertions(+), 58 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 5d8b503..36e73b8 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
@@ -5,17 +5,17 @@
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.mes.device.entity.GlassInfo;
 import com.mes.device.mapper.DeviceGlassInfoMapper;
+import com.mes.device.service.EngineeringSequenceService;
 import com.mes.device.service.GlassInfoService;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.cloud.context.config.annotation.RefreshScope;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 
-import java.time.LocalDate;
-import java.time.format.DateTimeFormatter;
+import java.math.BigDecimal;
 import java.util.*;
-import java.util.concurrent.atomic.AtomicInteger;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 import static java.util.stream.IntStream.range;
@@ -27,8 +27,12 @@
  * @since 2024-11-20
  */
 @Slf4j
+@RefreshScope
 @Service("deviceGlassInfoService")
 public class GlassInfoServiceImpl extends ServiceImpl<DeviceGlassInfoMapper, GlassInfo> implements GlassInfoService {
+
+    @Autowired
+    private EngineeringSequenceService engineeringSequenceService;
 
     @Override
     public GlassInfo getGlassInfo(String glassId) {
@@ -212,6 +216,14 @@
         }
     }
 
+    @Value("${mes.engineering.import-url}")
+    private String mesEngineeringImportUrl;
+
+    @Override
+    public String getMesEngineeringImportUrl() {
+        return mesEngineeringImportUrl;
+    }
+
     @Override
     public Map<String, Object> buildEngineerImportPayload(List<Map<String, Object>> excelRows) {
         Map<String, Object> result = new HashMap<>();
@@ -219,9 +231,8 @@
             return result;
         }
 
-        // 宸ョ▼鍙风敓鎴愶細P + yyMMdd + 搴忓彿(2浣�)
-        AtomicInteger seq = new AtomicInteger(1);
-        final String engineerId = generateEngineerId(firstValue(excelRows, "glassId"), seq.getAndIncrement());
+        // 宸ョ▼鍙风敓鎴愶細姣忔瀵煎叆閮界敓鎴愭柊鐨勫伐绋嬪彿锛堜娇鐢ㄦ暟鎹簱鑷搴忓彿锛岄伩鍏嶉噸澶嶏級
+        final String engineerId = engineeringSequenceService.generateAndSaveEngineeringId(new Date());
         final String filmsIdDefault = firstValue(excelRows, "filmsId", "鐧界幓");
         final double thicknessDefault = parseDouble(firstValue(excelRows, "thickness"), 0d);
 
@@ -232,10 +243,10 @@
 
         List<Map<String, Object>> glassInfolList = excelRows.stream()
                 .flatMap(row -> {
-                    int qty = (int) parseDouble(row.getOrDefault("quantity", 1), 1);
-                    if (qty <= 0) qty = 1;
+                    Object qtyObj = row.getOrDefault("quantity", 1);
+                    int qty = parseDouble(qtyObj, 1) > 0 ? (int) parseDouble(qtyObj, 1) : 1;
+
                     String glassId = str(row.get("glassId"));
-                    Integer orderNumber = Integer.parseInt(str(row.get("orderNumber")));
                     String filmsId = strOrDefault(row.get("filmsId"), filmsIdDefaultFinal);
                     String flowCardId = str(row.get("flowCardId"));
                     String productName = str(row.get("productName"));
@@ -245,9 +256,15 @@
                     double thickness = parseDouble(row.get("thickness"), thicknessDefaultFinal);
 
                     int finalQty = qty;
+                    log.info("瑙f瀽鍒版暟閲忥細row={}, quantity={}, 鏈�缁坬ty={}", row, qtyObj, finalQty);
                     return range(0, qty).mapToObj(idx -> {
-                        String finalGlassId = finalQty > 1 ? glassId + "_" + (idx + 1) : glassId;
-                        String finalFlowCardId = flowCardId.isEmpty() ? finalGlassId : flowCardId;
+                        String baseGlassId = engineerIdFinal + glassId;
+                        String finalGlassId = finalQty > 1 ? baseGlassId + "_" + (idx + 1) : baseGlassId;
+
+                        String baseFlowCardId = flowCardId.isEmpty() ? baseGlassId : flowCardId;
+                        String finalFlowCardSequence = baseFlowCardId + "/" + (idx + 1);
+                        String finalFlowCardId = baseFlowCardId;
+
                         Map<String, Object> m = new HashMap<>();
                         m.put("xAxis", 0);
                         m.put("xCoordinate", 0);
@@ -256,7 +273,6 @@
                         m.put("glassId", finalGlassId);
                         m.put("engineerId", engineerIdFinal);
                         m.put("flowCardId", finalFlowCardId);
-                        m.put("orderNumber", orderNumber);
                         m.put("productSortNumber", idx + 1);
                         m.put("hollowCombineDirection", "0");
                         m.put("width", width);
@@ -279,7 +295,7 @@
                         m.put("combine", 0);
                         m.put("markIcon", "");
                         m.put("filmRemove", 0);
-                        m.put("flowCardSequence", String.valueOf(idx + 1));
+                        m.put("flowCardSequence", finalFlowCardSequence);
                         m.put("process", "");
                         m.put("rawAngle", 0);
                         m.put("graphNo", 0);
@@ -324,7 +340,7 @@
             double height = parseDouble(row.get("height"), 0d);
             double thickness = parseDouble(row.get("thickness"), thicknessDefaultFinal);
             String filmsId = strOrDefault(row.get("filmsId"), filmsIdDefaultFinal);
-            Integer orderNumber = Integer.parseInt(str(row.get("orderNumber")));
+
             String productName = str(row.get("productName"));
             String customerName = str(row.get("customerName"));
 
@@ -339,7 +355,6 @@
                 m.put("totalLayer", 0);
                 m.put("layer", 0);
                 m.put("glassTotal", 1);
-                m.put("orderNumber", orderNumber);
                 m.put("productName", productName);
                 m.put("customerName", customerName);
                 flowCardMap.put(flowCardId, m);
@@ -381,47 +396,6 @@
         return result;
     }
 
-    // 鏃ユ湡鏍煎紡鍖栧櫒锛堢嚎绋嬩笉瀹夊叏锛屼娇鐢═hreadLocal淇濊瘉绾跨▼瀹夊叏锛�
-    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyMMdd");
-
-    // 鏁板瓧鍖归厤姝e垯锛堥缂栬瘧鎻愬崌鎬ц兘锛�
-    private static final Pattern DIGIT_PATTERN = Pattern.compile("\\d+");
-
-    /**
-     * 鐢熸垚宸ョ▼甯圛D
-     * 鏍煎紡瑙勫垯锛歅 + 骞存湀鏃�(yyMMdd) + 涓や綅搴忓彿
-     * 搴忓彿浼樺厛浠巊lassId涓彁鍙栨湯灏句袱浣嶆暟瀛楋紝鍚﹀垯浣跨敤浼犲叆鐨刬ndex琛ラ浂
-     *
-     * @param glassId 鐜荤拑ID锛堝彲涓簄ull锛岀敤浜庢彁鍙栨暟瀛楀簭鍙凤級
-     * @param index   澶囩敤搴忓彿锛堝綋glassId鏃犳湁鏁堟暟瀛楁椂浣跨敤锛�
-     * @return 鏍煎紡鍖栫殑宸ョ▼甯圛D锛堝锛歅25010801锛�
-     */
-    private String generateEngineerId(Object glassId, int index) {
-        // 1. 鐢熸垚鏃ユ湡鍓嶇紑锛坹yMMdd锛�
-        String base = LocalDate.now().format(DATE_FORMATTER);
-
-        // 2. 鍒濆鍖栧簭鍙凤紙涓や綅琛ラ浂锛�
-        String seq = String.format("%02d", index);
-
-        // 3. 浠巊lassId涓彁鍙栨湯灏句袱浣嶆暟瀛楋紙瑕嗙洊榛樿搴忓彿锛�
-        if (glassId != null) {
-            String glassIdStr = glassId.toString();
-            Matcher matcher = DIGIT_PATTERN.matcher(glassIdStr);
-            String lastDigitStr = null;
-
-            // 閬嶅巻鍖归厤鎵�鏈夋暟瀛楁锛屽彇鏈�鍚庝竴涓�
-            while (matcher.find()) {
-                lastDigitStr = matcher.group();
-            }
-
-            // 鑻ユ暟瀛楁闀垮害鈮�2锛屽彇鏈�鍚庝袱浣嶏紱鍚﹀垯淇濈暀鍘熷簭鍙�
-            if (lastDigitStr != null && lastDigitStr.length() >= 2) {
-                seq = lastDigitStr.substring(lastDigitStr.length() - 2);
-            }
-        }
-
-        return "P" + base + seq;
-    }
 
     /**
      * 鎻愬彇List涓涓�涓狹ap鐨勬寚瀹歬ey鍊硷紙榛樿绌哄瓧绗︿覆锛�
@@ -507,5 +481,142 @@
         return Math.round(v * 100.0) / 100.0;
     }
 
+    @Override
+    public List<GlassInfo> getGlassInfosByEngineeringId(String engineeringId) {
+        if (engineeringId == null || engineeringId.trim().isEmpty()) {
+            return Collections.emptyList();
+        }
+        try {
+            return baseMapper.selectByEngineeringId(engineeringId.trim());
+        } catch (Exception e) {
+            log.error("鏍规嵁宸ョ▼鍙锋煡璇㈢幓鐠冧俊鎭け璐�, engineeringId={}", engineeringId, e);
+            return Collections.emptyList();
+        }
+    }
+
+    @Override
+    public void saveGlassInfosFromExcel(List<Map<String, Object>> excelRows, String engineeringId) {
+        if (excelRows == null || excelRows.isEmpty() || engineeringId == null || engineeringId.trim().isEmpty()) {
+            return;
+        }
+
+        List<GlassInfo> glassInfos = new ArrayList<>();
+        Date now = new Date();
+
+        for (Map<String, Object> row : excelRows) {
+            String glassId = str(row.get("glassId"));
+            if (glassId == null || glassId.trim().isEmpty()) {
+                continue;
+            }
+
+            int qty = (int) parseDouble(row.getOrDefault("quantity", 1), 1);
+            if (qty <= 0) qty = 1;
+
+            double width = parseDouble(row.get("width"), 0d);
+            double height = parseDouble(row.get("height"), 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;
+
+                GlassInfo glassInfo = new GlassInfo();
+                glassInfo.setGlassId(finalGlassId);
+                glassInfo.setEngineeringId(engineeringId.trim());
+                glassInfo.setGlassLength((int) Math.round(height));
+                glassInfo.setGlassWidth((int) Math.round(width));
+                glassInfo.setGlassThickness(BigDecimal.valueOf(thickness));
+                glassInfo.setStatus(GlassInfo.Status.ACTIVE);
+                glassInfo.setState(0);
+                glassInfo.setCreatedTime(now);
+                glassInfo.setUpdatedTime(now);
+                glassInfo.setCreatedBy("system");
+                glassInfo.setUpdatedBy("system");
+
+                glassInfos.add(glassInfo);
+            }
+        }
+
+        if (!glassInfos.isEmpty()) {
+            batchSaveOrUpdateGlassInfo(glassInfos);
+            log.info("宸蹭繚瀛� {} 鏉$幓鐠冧俊鎭埌鏈湴鏁版嵁搴擄紝宸ョ▼鍙�: {}", glassInfos.size(), engineeringId);
+        }
+    }
+
+    @Override
+    public boolean updateGlassStateAfterScan(String glassId, Integer width, Integer height, Integer workLine) {
+        if (glassId == null || glassId.trim().isEmpty()) {
+            return false;
+        }
+
+        try {
+            // 鏌ヨ宸插瓨鍦ㄧ殑鐜荤拑淇℃伅
+            GlassInfo existing = baseMapper.selectByGlassId(glassId.trim());
+            if (existing == null) {
+                log.debug("鐜荤拑淇℃伅涓嶅瓨鍦紝鏃犳硶鏇存柊鐘舵��: glassId={}", glassId);
+                return false;
+            }
+
+            // 鏇存柊鐘舵�佷负1锛堝凡鎵爜浜や簰锛�
+            LambdaUpdateWrapper<GlassInfo> wrapper = new LambdaUpdateWrapper<>();
+            wrapper.eq(GlassInfo::getGlassId, glassId.trim())
+                   .eq(GlassInfo::getIsDeleted, 0)
+                   .set(GlassInfo::getState, 1)
+                   .set(GlassInfo::getUpdatedTime, new Date())
+                   .set(GlassInfo::getUpdatedBy, "system");
+
+            // 濡傛灉鎻愪緵浜嗗昂瀵镐俊鎭紝涔熸洿鏂板昂瀵�
+            if (width != null) {
+                wrapper.set(GlassInfo::getGlassWidth, width);
+            }
+            if (height != null) {
+                wrapper.set(GlassInfo::getGlassLength, height);
+            }
+            if (workLine != null) {
+                wrapper.set(GlassInfo::getWorkLine, workLine);
+            }
+
+            boolean updated = this.update(wrapper);
+            if (updated) {
+                log.info("宸叉洿鏂扮幓鐠冧俊鎭姸鎬佷负宸叉壂鐮佷氦浜�: glassId={}, state=1", glassId);
+            }
+            return updated;
+        } catch (Exception e) {
+            log.error("鏇存柊鐜荤拑淇℃伅鐘舵�佸け璐�: glassId={}", glassId, e);
+            return false;
+        }
+    }
+
+    @Override
+    public int deleteGlassInfosByEngineeringId(String engineeringId) {
+        if (engineeringId == null || engineeringId.trim().isEmpty()) {
+            return 0;
+        }
+
+        try {
+            // 鍏堟煡璇㈣鍒犻櫎鐨勬暟閲忥紙鍒犻櫎鍓嶏級
+            LambdaQueryWrapper<GlassInfo> countWrapper = new LambdaQueryWrapper<>();
+            countWrapper.eq(GlassInfo::getEngineeringId, engineeringId.trim())
+                       .eq(GlassInfo::getIsDeleted, 0); // 鏌ヨ鏈垹闄ょ殑璁板綍
+            long count = this.count(countWrapper);
+            
+            // 浣跨敤MyBatis-Plus鐨剅emove鏂规硶锛屼細鏍规嵁@TableLogic鑷姩杩涜閫昏緫鍒犻櫎
+            LambdaQueryWrapper<GlassInfo> removeWrapper = new LambdaQueryWrapper<>();
+            removeWrapper.eq(GlassInfo::getEngineeringId, engineeringId.trim())
+                        .eq(GlassInfo::getIsDeleted, 0); // 鍙垹闄ゆ湭鍒犻櫎鐨勮褰�
+            
+            boolean result = this.remove(removeWrapper);
+            if (result) {
+                log.info("宸插垹闄ゅ伐绋嬪彿涓嬬殑鐜荤拑淇℃伅: engineeringId={}, count={}", engineeringId, count);
+                return (int) count;
+            }
+            return 0;
+        } catch (Exception e) {
+            log.error("鍒犻櫎宸ョ▼鍙蜂笅鐨勭幓鐠冧俊鎭け璐�: engineeringId={}", engineeringId, e);
+            return 0;
+        }
+    }
+
 }
 

--
Gitblit v1.8.0