From eabb757720375b74900027e23e50303b7e02ca36 Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期二, 09 十二月 2025 17:04:23 +0800
Subject: [PATCH] 添加nacos配置中心,可动态更新mes导入工程接口;修改Excel表数据转json格式
---
mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/GlassInfoServiceImpl.java | 399 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 395 insertions(+), 4 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 94ed660..9b1dd51 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,18 +1,26 @@
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.GlassInfoService;
import lombok.extern.slf4j.Slf4j;
+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.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
+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;
/**
* 鐜荤拑淇℃伅鏈嶅姟瀹炵幇绫�
@@ -21,6 +29,7 @@
* @since 2024-11-20
*/
@Slf4j
+@RefreshScope
@Service("deviceGlassInfoService")
public class GlassInfoServiceImpl extends ServiceImpl<DeviceGlassInfoMapper, GlassInfo> implements GlassInfoService {
@@ -99,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) {
@@ -124,5 +160,360 @@
return false;
}
}
+
+ @Override
+ public List<String> getRecentScannedGlassIds(Integer minutesAgo, Integer maxCount, String workLine) {
+ try {
+ // 榛樿鏌ヨ鏈�杩�2鍒嗛挓鍐呯殑璁板綍锛屾渶澶氳繑鍥�20鏉�
+ int minutes = minutesAgo != null && minutesAgo > 0 ? minutesAgo : 2;
+ int limit = maxCount != null && maxCount > 0 ? maxCount : 20;
+
+ Date timeThreshold = new Date(System.currentTimeMillis() - minutes * 60 * 1000L);
+
+ LambdaQueryWrapper<GlassInfo> wrapper = new LambdaQueryWrapper<>();
+ wrapper.eq(GlassInfo::getStatus, GlassInfo.Status.PENDING)
+ .ge(GlassInfo::getCreatedTime, timeThreshold)
+ .orderByDesc(GlassInfo::getCreatedTime)
+ .last("LIMIT " + limit);
+
+ // 濡傛灉鎸囧畾浜唚orkLine锛屽垯杩囨护description
+ if (workLine != null && !workLine.trim().isEmpty()) {
+ wrapper.like(GlassInfo::getDescription, "workLine=" + workLine);
+ }
+
+ List<GlassInfo> recentGlasses = baseMapper.selectList(wrapper);
+
+ // 鎻愬彇鐜荤拑ID鍒楄〃
+ return recentGlasses.stream()
+ .map(GlassInfo::getGlassId)
+ .filter(id -> id != null && !id.trim().isEmpty())
+ .collect(Collectors.toList());
+
+ } catch (Exception e) {
+ log.error("鏌ヨ鏈�杩戞壂鐮佺殑鐜荤拑ID澶辫触, minutesAgo={}, maxCount={}, workLine={}",
+ minutesAgo, maxCount, workLine, e);
+ 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;
+ }
+
+ // 宸ョ▼鍙风敓鎴愶細P + yyMMdd + 搴忓彿(2浣�)
+ AtomicInteger seq = new AtomicInteger(1);
+ final String engineerId = generateEngineerId(firstValue(excelRows, "glassId"), seq.getAndIncrement());
+ 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;
+ }
+
+ // 鏃ユ湡鏍煎紡鍖栧櫒锛堢嚎绋嬩笉瀹夊叏锛屼娇鐢═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鍊硷紙榛樿绌哄瓧绗︿覆锛�
+ *
+ * @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