From 22e17b6db03ca58bc477a35ca067e55a09cffce7 Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期三, 10 十二月 2025 17:04:08 +0800
Subject: [PATCH] 添加工程表及相关逻辑,防止重复导入Excel表序号重复生成

---
 mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/GlassInfoServiceImpl.java |  325 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 319 insertions(+), 6 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 2e81b91..6c26414 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
@@ -1,19 +1,23 @@
 package com.mes.device.service.impl;
 
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 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.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 import java.util.stream.Collectors;
+
+import static java.util.stream.IntStream.range;
 
 /**
  * 鐜荤拑淇℃伅鏈嶅姟瀹炵幇绫�
@@ -22,8 +26,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) {
@@ -100,8 +108,35 @@
             GlassInfo existing = baseMapper.selectByGlassId(glassInfo.getGlassId());
             if (existing != null) {
                 glassInfo.setId(existing.getId());
+                // 淇濈暀鍘熷鍒涘缓淇℃伅
+                if (glassInfo.getCreatedTime() == null) {
+                    glassInfo.setCreatedTime(existing.getCreatedTime());
+                }
+                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);
             } else {
+                Date now = new Date();
+                if (glassInfo.getCreatedTime() == null) {
+                    glassInfo.setCreatedTime(now);
+                }
+                if (glassInfo.getUpdatedTime() == null) {
+                    glassInfo.setUpdatedTime(now);
+                }
+                if (glassInfo.getCreatedBy() == null) {
+                    glassInfo.setCreatedBy("system");
+                }
+                if (glassInfo.getUpdatedBy() == null) {
+                    glassInfo.setUpdatedBy("system");
+                }
                 return save(glassInfo);
             }
         } catch (Exception e) {
@@ -136,7 +171,7 @@
             Date timeThreshold = new Date(System.currentTimeMillis() - minutes * 60 * 1000L);
             
             LambdaQueryWrapper<GlassInfo> wrapper = new LambdaQueryWrapper<>();
-            wrapper.eq(GlassInfo::getStatus, GlassInfo.Status.ACTIVE)
+            wrapper.eq(GlassInfo::getStatus, GlassInfo.Status.PENDING)
                    .ge(GlassInfo::getCreatedTime, timeThreshold)
                    .orderByDesc(GlassInfo::getCreatedTime)
                    .last("LIMIT " + limit);
@@ -160,5 +195,283 @@
             return Collections.emptyList();
         }
     }
+
+    @Override
+    public boolean updateGlassStatus(List<String> glassIds, String status) {
+        if (CollectionUtils.isEmpty(glassIds) || status == null) {
+            return true;
+        }
+        try {
+            LambdaUpdateWrapper<GlassInfo> wrapper = new LambdaUpdateWrapper<>();
+            wrapper.in(GlassInfo::getGlassId, glassIds);
+            GlassInfo update = new GlassInfo();
+            update.setStatus(status);
+            update.setUpdatedTime(new Date());
+            update.setUpdatedBy("system");
+            return this.update(update, wrapper);
+        } catch (Exception e) {
+            log.error("鎵归噺鏇存柊鐜荤拑鐘舵�佸け璐�, glassIds={}, status={}", glassIds, status, e);
+            return false;
+        }
+    }
+
+    @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<>();
+        if (excelRows == null || excelRows.isEmpty()) {
+            return result;
+        }
+
+        // 宸ョ▼鍙风敓鎴愶細浣跨敤鏁版嵁搴撹嚜澧炲簭鍙凤紝閬垮厤閲嶅
+        final String engineerId = engineeringSequenceService.generateAndSaveEngineeringId(new Date());
+        final String filmsIdDefault = firstValue(excelRows, "filmsId", "鐧界幓");
+        final double thicknessDefault = parseDouble(firstValue(excelRows, "thickness"), 0d);
+
+        // glassInfolList
+        final String engineerIdFinal = engineerId;
+        final String filmsIdDefaultFinal = filmsIdDefault;
+        final double thicknessDefaultFinal = thicknessDefault;
+
+        List<Map<String, Object>> glassInfolList = excelRows.stream()
+                .flatMap(row -> {
+                    int qty = (int) parseDouble(row.getOrDefault("quantity", 1), 1);
+                    if (qty <= 0) qty = 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);
+
+                    int finalQty = qty;
+                    return range(0, qty).mapToObj(idx -> {
+                        String finalGlassId = finalQty > 1 ? glassId + "_" + (idx + 1) : glassId;
+                        String finalFlowCardId = flowCardId.isEmpty() ? finalGlassId : flowCardId;
+                        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", flowCardId + "/" + (idx + 1));
+                        m.put("process", "");
+                        m.put("rawAngle", 0);
+                        m.put("graphNo", 0);
+                        m.put("processParam", "");
+                        return m;
+                    });
+                })
+                .collect(Collectors.toList());
+
+        // 鍘熺墖淇℃伅鍘婚噸
+        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 thickness = parseDouble(row.get("thickness"), thicknessDefaultFinal);
+            String filmsId = strOrDefault(row.get("filmsId"), filmsIdDefaultFinal);
+            String key = width + "_" + height + "_" + thickness + "_" + filmsId;
+            if (!rawGlassMap.containsKey(key)) {
+                Map<String, Object> m = new HashMap<>();
+                m.put("engineeringId", engineerIdFinal);
+                m.put("filmsId", filmsId);
+                m.put("rawGlassWidth", width);
+                m.put("rawGlassHeight", height);
+                m.put("rawGlassThickness", thickness);
+                m.put("rawSequence", rawGlassMap.size() + 1);
+                m.put("usageRate", "0.95");
+                rawGlassMap.put(key, m);
+            }
+        }
+
+        List<Map<String, Object>> engineeringRawQueueList = rawGlassMap.values().stream().collect(Collectors.toList());
+
+        // 娴佺▼鍗′俊鎭�
+        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;
+            }
+            double width = parseDouble(row.get("width"), 0d);
+            double height = parseDouble(row.get("height"), 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"));
+
+            Map<String, Object> exist = flowCardMap.get(flowCardId);
+            if (exist == null) {
+                Map<String, Object> m = new HashMap<>();
+                m.put("flowCardId", flowCardId);
+                m.put("width", width);
+                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("productName", productName);
+                m.put("customerName", customerName);
+                flowCardMap.put(flowCardId, m);
+            } else {
+                int count = (int) exist.getOrDefault("glassTotal", 1);
+                exist.put("glassTotal", count + 1);
+            }
+        }
+        List<Map<String, Object>> flowCardInfoList = flowCardMap.values().stream().collect(Collectors.toList());
+
+        // 姹囨��
+        int glassTotal = glassInfolList.size();
+        double glassTotalArea = glassInfolList.stream()
+                .mapToDouble(m -> parseDouble(m.get("width"), 0d) * parseDouble(m.get("height"), 0d) / 1_000_000d)
+                .sum();
+        double patternArea = engineeringRawQueueList.stream()
+                .mapToDouble(m -> parseDouble(m.get("rawGlassWidth"), 0d) * parseDouble(m.get("rawGlassHeight"), 0d) / 1_000_000d)
+                .sum();
+
+        result.put("engineerId", engineerIdFinal);
+        result.put("engineerName", "宸ョ▼_" + engineerIdFinal);
+        result.put("avgAvailability", "90");
+        result.put("validAvailability", "90");
+        result.put("lastAvailability", "90");
+        result.put("glassTotal", glassTotal);
+        result.put("glassTotalArea", round2(glassTotalArea));
+        result.put("planPatternTotal", engineeringRawQueueList.size());
+        result.put("planPatternTotalArea", round2(patternArea));
+        result.put("realityPatternTotal", engineeringRawQueueList.size());
+        result.put("realityPatternTotalArea", round2(patternArea));
+        result.put("filmsId", filmsIdDefaultFinal);
+        result.put("thickness", thicknessDefaultFinal);
+        result.put("engineeringRawQueueList", engineeringRawQueueList);
+        result.put("glassInfolList", glassInfolList);
+        result.put("flowCardInfoList", flowCardInfoList);
+        result.put("hollowFormulaDetailsList", null);
+        result.put("temperingParameterList", null);
+
+        return result;
+    }
+
+
+    /**
+     * 鎻愬彇List涓涓�涓狹ap鐨勬寚瀹歬ey鍊硷紙榛樿绌哄瓧绗︿覆锛�
+     *
+     * @param rows 鏁版嵁琛屽垪琛紙鍙负null/绌猴級
+     * @param key  瑕佹彁鍙栫殑閿�
+     * @return 绗竴涓狹ap鐨刱ey瀵瑰簲鍊硷紙绌哄垯杩斿洖""锛�
+     */
+    private String firstValue(List<Map<String, Object>> rows, String key) {
+        return firstValue(rows, key, "");
+    }
+
+    /**
+     * 鎻愬彇List涓涓�涓狹ap鐨勬寚瀹歬ey鍊硷紙鑷畾涔夐粯璁ゅ�硷級
+     *
+     * @param rows       鏁版嵁琛屽垪琛紙鍙负null/绌猴級
+     * @param key        瑕佹彁鍙栫殑閿�
+     * @param defaultVal 绌哄�兼椂鐨勯粯璁よ繑鍥炲��
+     * @return 绗竴涓狹ap鐨刱ey瀵瑰簲鍊硷紙绌哄垯杩斿洖defaultVal锛�
+     */
+    private String firstValue(List<Map<String, Object>> rows, String key, String defaultVal) {
+        if (rows == null || rows.isEmpty() || key == null) {
+            return defaultVal;
+        }
+
+        Map<String, Object> firstRow = rows.get(0);
+        Object value = firstRow.get(key);
+        return value == null ? defaultVal : value.toString();
+    }
+
+    /**
+     * 瀵硅薄杞瓧绗︿覆锛坣ull杞┖涓诧紝鑷姩鍘婚櫎棣栧熬绌烘牸锛�
+     *
+     * @param v 寰呰浆鎹㈠璞�
+     * @return 澶勭悊鍚庣殑瀛楃涓�
+     */
+    private String str(Object v) {
+        return v == null ? "" : v.toString().trim();
+    }
+
+    /**
+     * 瀵硅薄杞瓧绗︿覆锛堢┖涓叉椂杩斿洖榛樿鍊硷紝鑷姩鍘婚櫎棣栧熬绌烘牸锛�
+     *
+     * @param v   寰呰浆鎹㈠璞�
+     * @param def 绌哄�奸粯璁ゅ��
+     * @return 澶勭悊鍚庣殑瀛楃涓�
+     */
+    private String strOrDefault(Object v, String def) {
+        String result = str(v);
+        return result.isEmpty() ? def : result;
+    }
+
+    /**
+     * 瑙f瀽瀵硅薄涓篸ouble锛堝け璐�/绌哄�艰繑鍥為粯璁ゅ�硷級
+     *
+     * @param v   寰呰В鏋愬璞★紙鏀寔鏁板瓧/瀛楃涓茬被鍨嬶級
+     * @param def 瑙f瀽澶辫触鏃剁殑榛樿鍊�
+     * @return 瑙f瀽鍚庣殑double鍊�
+     */
+    private double parseDouble(Object v, double def) {
+        if (v == null) {
+            return def;
+        }
+
+        try {
+            if (v instanceof Number) {
+                return ((Number) v).doubleValue();
+            }
+            return Double.parseDouble(v.toString().trim());
+        } catch (NumberFormatException e) {
+            // 浠呮崟鑾锋暟瀛楁牸寮忓寲寮傚父锛岄伩鍏嶅悶鎺夊叾浠栧紓甯�
+            return def;
+        }
+    }
+
+    /**
+     * 淇濈暀涓や綅灏忔暟锛堝洓鑸嶄簲鍏ワ級
+     *
+     * @param v 鍘熷鏁板��
+     * @return 淇濈暀涓や綅灏忔暟鍚庣殑鏁板��
+     */
+    private double round2(double v) {
+        return Math.round(v * 100.0) / 100.0;
+    }
+
 }
 

--
Gitblit v1.8.0