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/DeviceConfigServiceImpl.java | 163 +++++++++++++++++++++++++++++++++++++++--------------
1 files changed, 119 insertions(+), 44 deletions(-)
diff --git a/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DeviceConfigServiceImpl.java b/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DeviceConfigServiceImpl.java
index 55a9693..c48c702 100644
--- a/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DeviceConfigServiceImpl.java
+++ b/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DeviceConfigServiceImpl.java
@@ -11,6 +11,7 @@
import com.mes.device.vo.DeviceConfigVO;
import com.mes.device.vo.StatisticsVO;
import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.io.IOException;
@@ -27,14 +28,32 @@
public class DeviceConfigServiceImpl extends ServiceImpl<DeviceConfigMapper, DeviceConfig> implements DeviceConfigService {
private final ObjectMapper objectMapper = new ObjectMapper();
+ private static final TypeReference<Map<String, Object>> MAP_TYPE = new TypeReference<Map<String, Object>>() {};
@Override
public boolean createDevice(DeviceConfig deviceConfig) {
try {
+ // 鑻ユ湭浼犲垯鐢熸垚璁惧缂栫爜
+ String code = StringUtils.trimToEmpty(deviceConfig.getDeviceCode());
+ if (StringUtils.isBlank(code)) {
+ code = generateDeviceCode();
+ deviceConfig.setDeviceCode(code);
+ } else {
+ deviceConfig.setDeviceCode(code);
+ }
+
// 妫�鏌ヨ澶囩紪鍙锋槸鍚﹀凡瀛樺湪
- if (isDeviceCodeExists(deviceConfig.getDeviceCode(), null)) {
- log.warn("璁惧缂栧彿宸插瓨鍦�: {}", deviceConfig.getDeviceCode());
- return false;
+ if (isDeviceCodeExists(code, null)) {
+ log.warn("璁惧缂栧彿宸插瓨鍦�: {}", code);
+ throw new IllegalArgumentException("璁惧缂栫爜宸插瓨鍦�");
+ }
+
+ // 鍏煎鏃у瓧娈碉細缁熶竴灏� device_id 濉负 deviceCode锛岄伩鍏嶉潪绌�/鍞竴绾︽潫闂
+ deviceConfig.setDeviceId(code);
+
+ // 椤圭洰ID鏈紶鍒欎娇鐢ㄩ粯璁ら」鐩紙鍗曢」鐩満鏅彲鐢級锛岄伩鍏嶉潪绌虹害鏉�
+ if (deviceConfig.getProjectId() == null) {
+ deviceConfig.setProjectId(1L);
}
// 鍒濆鍖栬澶囩姸鎬佷负绂荤嚎
@@ -49,7 +68,7 @@
return result;
} catch (Exception e) {
log.error("鍒涘缓璁惧閰嶇疆澶辫触", e);
- return false;
+ throw e;
}
}
@@ -60,6 +79,16 @@
if (isDeviceCodeExists(deviceConfig.getDeviceCode(), deviceConfig.getId())) {
log.warn("璁惧缂栧彿宸插瓨鍦�: {}", deviceConfig.getDeviceCode());
return false;
+ }
+
+ // 鍚屾 device_id 涓� deviceCode锛屼繚鎸佷竴鑷�
+ if (StringUtils.isNotBlank(deviceConfig.getDeviceCode())) {
+ deviceConfig.setDeviceId(deviceConfig.getDeviceCode().trim());
+ }
+
+ // 鑻ラ」鐩甀D缂哄け锛屼娇鐢ㄩ粯璁ら」鐩�
+ if (deviceConfig.getProjectId() == null) {
+ deviceConfig.setProjectId(1L);
}
boolean result = updateById(deviceConfig);
@@ -134,10 +163,7 @@
// 璁惧绫诲瀷杩囨护
if (deviceType != null && !deviceType.trim().isEmpty()) {
- String convertedDeviceType = convertDeviceTypeFromString(deviceType);
- if (convertedDeviceType != null) {
- wrapper.eq(DeviceConfig::getDeviceType, convertedDeviceType);
- }
+ wrapper.eq(DeviceConfig::getDeviceType, deviceType.trim());
}
// 璁惧鐘舵�佽繃婊�
@@ -214,7 +240,7 @@
vo.setStatus(getStatusName(device.getStatus()));
vo.setDeviceStatus(convertStatusToCode(device.getStatus()));
vo.setDescription(device.getDescription());
- vo.setLocation("榛樿浣嶇疆"); // TODO: 浠庢墿灞曞弬鏁版垨鍏宠仈琛ㄤ腑鑾峰彇
+ vo.setLocation(extractLocationFromDevice(device));
vo.setCreatedTime(device.getCreatedTime());
vo.setUpdatedTime(device.getUpdatedTime());
vo.setProjectId(device.getProjectId());
@@ -311,29 +337,6 @@
}
}
- /**
- * 瀛楃涓茶浆鎹负璁惧绫诲瀷
- */
- private String convertDeviceTypeFromString(String deviceType) {
- if (deviceType == null) return null;
-
- switch (deviceType.trim().toLowerCase()) {
- case "load_vehicle":
- case "涓婂ぇ杞�":
- case "1":
- return DeviceConfig.DeviceType.LOAD_VEHICLE;
- case "large_glass":
- case "澶х悊鐗�":
- case "2":
- return DeviceConfig.DeviceType.LARGE_GLASS;
- case "glass_storage":
- case "鐜荤拑瀛樺偍":
- case "3":
- return DeviceConfig.DeviceType.GLASS_STORAGE;
- default:
- return null;
- }
- }
/**
* 瀛楃涓茶浆鎹负鐘舵��
@@ -366,8 +369,13 @@
@Override
public boolean isDeviceCodeExists(String deviceCode, Long excludeId) {
+ if (StringUtils.isBlank(deviceCode)) {
+ return false;
+ }
+ String trimmed = deviceCode.trim();
+
LambdaQueryWrapper<DeviceConfig> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(DeviceConfig::getDeviceCode, deviceCode);
+ wrapper.eq(DeviceConfig::getDeviceCode, trimmed);
wrapper.eq(DeviceConfig::getIsDeleted, 0);
if (excludeId != null) {
@@ -375,6 +383,16 @@
}
return count(wrapper) > 0;
+ }
+
+ /**
+ * 绠�鍗曠殑璁惧缂栫爜鐢熸垚鍣細DEV_鍓嶇紑
+ */
+ private String generateDeviceCode() {
+ Long maxNo = getBaseMapper().selectMaxDeviceCodeNumber();
+ long next = (maxNo == null ? 0 : maxNo) + 1;
+ // 宸︿晶琛ラ浂鍒�6浣嶏紝渚嬪 DEV_000123
+ return String.format("DEV_%06d", next);
}
@Override
@@ -623,12 +641,9 @@
}
// 璁惧绫诲瀷杩囨护
- if (deviceType != null && !deviceType.trim().isEmpty()) {
- String convertedDeviceType = convertDeviceTypeFromString(deviceType);
- if (convertedDeviceType != null) {
- wrapper.eq(DeviceConfig::getDeviceType, convertedDeviceType);
- }
- }
+ if (deviceType != null && !deviceType.trim().isEmpty()) {
+ wrapper.eq(DeviceConfig::getDeviceType, deviceType.trim());
+ }
// 璁惧鐘舵�佽繃婊�
if (deviceStatus != null && !deviceStatus.trim().isEmpty()) {
@@ -656,12 +671,42 @@
@Override
public List<String> getAllDeviceTypes() {
- List<String> deviceTypes = new ArrayList<>();
- deviceTypes.add("PLC璁惧");
- deviceTypes.add("浼犳劅鍣ㄨ澶�");
- deviceTypes.add("鎵ц鍣ㄨ澶�");
- deviceTypes.add("浜烘満鐣岄潰璁惧");
+ try {
+ // 浠庢暟鎹簱涓煡璇㈡墍鏈夊凡瀛樺湪鐨勮澶囩被鍨嬶紙鍘婚噸锛�
+ LambdaQueryWrapper<DeviceConfig> wrapper = new LambdaQueryWrapper<>();
+ wrapper.select(DeviceConfig::getDeviceType);
+ wrapper.eq(DeviceConfig::getIsDeleted, 0);
+ wrapper.isNotNull(DeviceConfig::getDeviceType);
+ wrapper.ne(DeviceConfig::getDeviceType, "");
+
+ List<DeviceConfig> devices = list(wrapper);
+ List<String> deviceTypes = devices.stream()
+ .map(DeviceConfig::getDeviceType)
+ .filter(Objects::nonNull)
+ .filter(type -> !type.trim().isEmpty())
+ .distinct()
+ .sorted()
+ .collect(Collectors.toList());
+
+ // 濡傛灉鏁版嵁搴撲腑娌℃湁璁惧绫诲瀷锛岃繑鍥為粯璁ょ被鍨�
+ if (deviceTypes.isEmpty()) {
+ deviceTypes.add(DeviceConfig.DeviceType.LOAD_VEHICLE);
+ deviceTypes.add(DeviceConfig.DeviceType.LARGE_GLASS);
+ deviceTypes.add(DeviceConfig.DeviceType.WORKSTATION_SCANNER);
+ deviceTypes.add(DeviceConfig.DeviceType.WORKSTATION_TRANSFER);
+ }
+
return deviceTypes;
+ } catch (Exception e) {
+ log.error("鑾峰彇璁惧绫诲瀷鍒楄〃澶辫触", e);
+ // 寮傚父鏃惰繑鍥為粯璁ょ被鍨�
+ List<String> defaultTypes = new ArrayList<>();
+ defaultTypes.add(DeviceConfig.DeviceType.LOAD_VEHICLE);
+ defaultTypes.add(DeviceConfig.DeviceType.LARGE_GLASS);
+ defaultTypes.add(DeviceConfig.DeviceType.WORKSTATION_SCANNER);
+ defaultTypes.add(DeviceConfig.DeviceType.WORKSTATION_TRANSFER);
+ return defaultTypes;
+ }
}
@Override
@@ -771,4 +816,34 @@
return new ArrayList<>();
}
}
+
+ /**
+ * 浠庤澶囨墿灞曞弬鏁颁腑鎻愬彇浣嶇疆淇℃伅
+ */
+ private String extractLocationFromDevice(DeviceConfig device) {
+ if (device == null) {
+ return "榛樿浣嶇疆";
+ }
+ try {
+ // 浼樺厛浠巈xtraParams涓幏鍙�
+ if (device.getExtraParams() != null && !device.getExtraParams().trim().isEmpty()) {
+ Map<String, Object> extraParams = objectMapper.readValue(device.getExtraParams(), MAP_TYPE);
+ Object location = extraParams.get("location");
+ if (location != null) {
+ return String.valueOf(location);
+ }
+ }
+ // 浠巆onfigJson涓幏鍙�
+ if (device.getConfigJson() != null && !device.getConfigJson().trim().isEmpty()) {
+ Map<String, Object> configJson = objectMapper.readValue(device.getConfigJson(), MAP_TYPE);
+ Object location = configJson.get("location");
+ if (location != null) {
+ return String.valueOf(location);
+ }
+ }
+ } catch (Exception e) {
+ log.warn("瑙f瀽璁惧浣嶇疆淇℃伅澶辫触, deviceId={}", device.getId(), e);
+ }
+ return "榛樿浣嶇疆";
+ }
}
\ No newline at end of file
--
Gitblit v1.8.0