From 16f4bb3c28fc85cffcc511718c903ada9fdab134 Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期五, 26 十二月 2025 16:59:45 +0800
Subject: [PATCH] 调用mes导入工程参数修改,Excel表数据流程卡号一致;增加读取层号/工程号方法; 工程号一致覆盖更新

---
 mes-processes/mes-plcSend/src/main/java/com/mes/service/PlcTestWriteService.java |  155 +++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 119 insertions(+), 36 deletions(-)

diff --git a/mes-processes/mes-plcSend/src/main/java/com/mes/service/PlcTestWriteService.java b/mes-processes/mes-plcSend/src/main/java/com/mes/service/PlcTestWriteService.java
index 82582c4..51d053e 100644
--- a/mes-processes/mes-plcSend/src/main/java/com/mes/service/PlcTestWriteService.java
+++ b/mes-processes/mes-plcSend/src/main/java/com/mes/service/PlcTestWriteService.java
@@ -70,11 +70,16 @@
                 // 浣跨敤鏂扮殑PLC瀹㈡埛绔鍙栨暟鎹�
                 Map<String, Object> currentData = plcClient.readAllData();
                 if (currentData != null && !currentData.isEmpty()) {
-                    // 妫�鏌ヨ仈鏈虹姸鎬�
-                    Integer onlineState = parseInteger(currentData.get("onlineState"));
-                    if (onlineState != null && onlineState == OFF) {
-                        log.info("褰撳墠PLC鑱旀満妯″紡涓�0锛屽仠姝㈣仈鏈�: deviceId={}", deviceId);
-                        return false;
+                    // 妫�鏌ヨ仈鏈虹姸鎬侊紙浠呭綋閰嶇疆涓瓨鍦ㄨ瀛楁鏃讹級
+                    if (hasFieldInConfig(device, "onlineState")) {
+                        Object onlineStateObj = currentData.get("onlineState");
+                        if (onlineStateObj != null) {
+                            Integer onlineState = parseInteger(onlineStateObj);
+                            if (onlineState == OFF) {
+                                log.info("褰撳墠PLC鑱旀満妯″紡涓�0锛屽仠姝㈣仈鏈�: deviceId={}", deviceId);
+                                return false;
+                            }
+                        }
                     }
 
                     // 妫�鏌ユ眹鎶ュ瓧锛屽鏋滀负1鍒欓噸缃负0
@@ -126,27 +131,29 @@
                 return false;
             }
 
-            // 妫�鏌ヨ仈鏈虹姸鎬�
-            Object onlineStateObj = currentData.get("onlineState");
-            Integer onlineState = null;
-            if (onlineStateObj != null) {
-                if (onlineStateObj instanceof Number) {
-                    onlineState = ((Number) onlineStateObj).intValue();
-                } else {
-                    try {
-                        String strValue = String.valueOf(onlineStateObj);
-                        if (!strValue.isEmpty() && !"null".equalsIgnoreCase(strValue)) {
-                            onlineState = Integer.parseInt(strValue);
+            // 妫�鏌ヨ仈鏈虹姸鎬侊紙浠呭綋閰嶇疆涓瓨鍦ㄨ瀛楁鏃讹級
+            if (hasFieldInConfig(device, "onlineState")) {
+                Object onlineStateObj = currentData.get("onlineState");
+                Integer onlineState = null;
+                if (onlineStateObj != null) {
+                    if (onlineStateObj instanceof Number) {
+                        onlineState = ((Number) onlineStateObj).intValue();
+                    } else {
+                        try {
+                            String strValue = String.valueOf(onlineStateObj);
+                            if (!strValue.isEmpty() && !"null".equalsIgnoreCase(strValue)) {
+                                onlineState = Integer.parseInt(strValue);
+                            }
+                        } catch (NumberFormatException e) {
+                            log.warn("瑙f瀽onlineState澶辫触: deviceId={}, value={}", device.getId(), onlineStateObj, e);
                         }
-                    } catch (NumberFormatException e) {
-                        log.warn("瑙f瀽onlineState澶辫触: deviceId={}, value={}", device.getId(), onlineStateObj, e);
                     }
                 }
-            }
 
-            if (onlineState != null && onlineState == OFF) {
-                log.info("褰撳墠PLC鑱旀満妯″紡涓�0锛屽仠姝㈣仈鏈�: deviceId={}", device.getId());
-                return false;
+                if (onlineState != null && onlineState == OFF) {
+                    log.info("褰撳墠PLC鑱旀満妯″紡涓�0锛屽仠姝㈣仈鏈�: deviceId={}", device.getId());
+                    return false;
+                }
             }
 
             // 妫�鏌ユ眹鎶ュ瓧锛屽鏋滀负1鍒欓噸缃负0
@@ -281,14 +288,32 @@
             // 灏濊瘯浣跨敤鏂扮殑PLC瀹㈡埛绔伐鍘�
             PlcClient plcClient = plcClientFactory.getClient(device);
             if (plcClient != null) {
-                // 鏋勫缓閲嶇疆鏁版嵁
+                // 鏋勫缓閲嶇疆鏁版嵁锛堝彧娣诲姞閰嶇疆涓瓨鍦ㄧ殑瀛楁锛�
                 Map<String, Object> resetData = new HashMap<>();
-                resetData.put("plcRequest", OFF);
-                resetData.put("plcReport", OFF);
-                resetData.put("mesSend", OFF);
-                resetData.put("mesConfirm", OFF);
-                resetData.put("onlineState", ON);
-                resetData.put("alarmInfo", OFF);
+                if (hasFieldInConfig(device, "plcRequest")) {
+                    resetData.put("plcRequest", OFF);
+                }
+                if (hasFieldInConfig(device, "plcReport")) {
+                    resetData.put("plcReport", OFF);
+                }
+                if (hasFieldInConfig(device, "mesSend")) {
+                    resetData.put("mesSend", OFF);
+                }
+                if (hasFieldInConfig(device, "mesConfirm")) {
+                    resetData.put("mesConfirm", OFF);
+                }
+                if (hasFieldInConfig(device, "onlineState")) {
+                    resetData.put("onlineState", ON);
+                }
+                if (hasFieldInConfig(device, "alarmInfo")) {
+                    resetData.put("alarmInfo", OFF);
+                }
+
+                // 妫�鏌ユ槸鍚︽湁瀛楁闇�瑕侀噸缃�
+                if (resetData.isEmpty()) {
+                    log.warn("璁惧閰嶇疆涓湭鎵惧埌浠讳綍鍙噸缃殑瀛楁: deviceId={}", deviceId);
+                    return false;
+                }
 
                 // 浣跨敤鏂扮殑PLC瀹㈡埛绔啓鍏ユ暟鎹�
                 boolean success = plcClient.writeData(resetData);
@@ -321,14 +346,32 @@
                 return false;
             }
 
-            // 鏋勫缓閲嶇疆鏁版嵁
+            // 鏋勫缓閲嶇疆鏁版嵁锛堝彧娣诲姞閰嶇疆涓瓨鍦ㄧ殑瀛楁锛�
             Map<String, Object> resetData = new HashMap<>();
-            resetData.put("plcRequest", OFF);
-            resetData.put("plcReport", OFF);
-            resetData.put("mesSend", OFF);
-            resetData.put("mesConfirm", OFF);
-            resetData.put("onlineState", ON);
-            resetData.put("alarmInfo", OFF);
+            if (hasFieldInConfig(device, "plcRequest")) {
+                resetData.put("plcRequest", OFF);
+            }
+            if (hasFieldInConfig(device, "plcReport")) {
+                resetData.put("plcReport", OFF);
+            }
+            if (hasFieldInConfig(device, "mesSend")) {
+                resetData.put("mesSend", OFF);
+            }
+            if (hasFieldInConfig(device, "mesConfirm")) {
+                resetData.put("mesConfirm", OFF);
+            }
+            if (hasFieldInConfig(device, "onlineState")) {
+                resetData.put("onlineState", ON);
+            }
+            if (hasFieldInConfig(device, "alarmInfo")) {
+                resetData.put("alarmInfo", OFF);
+            }
+
+            // 妫�鏌ユ槸鍚︽湁瀛楁闇�瑕侀噸缃�
+            if (resetData.isEmpty()) {
+                log.warn("璁惧閰嶇疆涓湭鎵惧埌浠讳綍鍙噸缃殑瀛楁: deviceId={}", device.getId());
+                return false;
+            }
 
             // 浣跨敤PlcDynamicDataService鍐欏叆鏁版嵁
             plcDynamicDataService.writePlcData(device, resetData, s7Serializer);
@@ -572,4 +615,44 @@
         
         throw new IllegalStateException("鏃犳硶瑙f瀽璁惧鐨凱LC椤圭洰鏍囪瘑, deviceId=" + device.getId());
     }
+    
+    /**
+     * 妫�鏌ヨ澶囬厤缃腑鏄惁瀛樺湪鎸囧畾瀛楁
+     * 
+     * @param device 璁惧閰嶇疆
+     * @param fieldName 瀛楁鍚�
+     * @return 鏄惁瀛樺湪
+     */
+    private boolean hasFieldInConfig(DeviceConfig device, String fieldName) {
+        if (device == null || fieldName == null || fieldName.isEmpty()) {
+            return false;
+        }
+        
+        try {
+            // 浠� configJson 涓鏌ワ紙鏂扮粨鏋勶級
+            Map<String, Object> configParams = ConfigJsonHelper.parseToMap(device.getConfigJson(), objectMapper);
+            if (configParams.containsKey(fieldName)) {
+                return true;
+            }
+            
+            // 浠� extraParams.addressMapping 涓鏌ワ紙鍏煎鏃х粨鏋勶級
+            Map<String, Object> extraParams = parseExtraParams(device);
+            Object addressMapping = extraParams.get("addressMapping");
+            if (addressMapping != null) {
+                Map<String, Object> addressMappingMap;
+                if (addressMapping instanceof Map) {
+                    addressMappingMap = (Map<String, Object>) addressMapping;
+                } else if (addressMapping instanceof String) {
+                    addressMappingMap = objectMapper.readValue((String) addressMapping, MAP_TYPE);
+                } else {
+                    return false;
+                }
+                return addressMappingMap.containsKey(fieldName);
+            }
+        } catch (Exception e) {
+            log.warn("妫�鏌ュ瓧娈垫槸鍚﹀瓨鍦ㄦ椂鍑洪敊: deviceId={}, fieldName={}", device.getId(), fieldName, e);
+        }
+        
+        return false;
+    }
 }

--
Gitblit v1.8.0