From e76f0739e647fe8a7e0e2618914e2faff554b1b7 Mon Sep 17 00:00:00 2001
From: huang <1532065656@qq.com>
Date: 星期一, 17 十一月 2025 17:33:23 +0800
Subject: [PATCH] 解决冲突

---
 mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DeviceControlProfileServiceImpl.java |   76 ++++++++++++++++++++++++++++++++++++++
 1 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DeviceControlProfileServiceImpl.java b/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DeviceControlProfileServiceImpl.java
new file mode 100644
index 0000000..3762a95
--- /dev/null
+++ b/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DeviceControlProfileServiceImpl.java
@@ -0,0 +1,76 @@
+package com.mes.device.service.impl;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.mes.device.entity.DeviceConfig;
+import com.mes.device.service.DeviceConfigService;
+import com.mes.device.service.DeviceControlProfileService;
+import com.mes.device.vo.DeviceControlProfile;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 璁惧鎺у埗鍙傛暟鏈嶅姟瀹炵幇
+ */
+@Slf4j
+@Service
+@RequiredArgsConstructor
+public class DeviceControlProfileServiceImpl implements DeviceControlProfileService {
+
+    private static final String CONTROL_PROFILE_KEY = "controlProfile";
+
+    private final DeviceConfigService deviceConfigService;
+    private final ObjectMapper objectMapper;
+
+    @Override
+    public DeviceControlProfile getProfile(Long deviceId) {
+        DeviceConfig device = deviceConfigService.getDeviceById(deviceId);
+        if (device == null) {
+            throw new IllegalArgumentException("璁惧涓嶅瓨鍦�: " + deviceId);
+        }
+        Map<String, Object> extraMap = readExtraMap(device);
+        Object profileNode = extraMap.get(CONTROL_PROFILE_KEY);
+        if (profileNode == null) {
+            return DeviceControlProfile.builder().autoRequest(true).build();
+        }
+        return objectMapper.convertValue(profileNode, DeviceControlProfile.class);
+    }
+
+    @Override
+    public void updateProfile(Long deviceId, DeviceControlProfile profile) {
+        DeviceConfig device = deviceConfigService.getDeviceById(deviceId);
+        if (device == null) {
+            throw new IllegalArgumentException("璁惧涓嶅瓨鍦�: " + deviceId);
+        }
+        Map<String, Object> extraMap = readExtraMap(device);
+        extraMap.put(CONTROL_PROFILE_KEY, profile);
+        try {
+            String json = objectMapper.writeValueAsString(extraMap);
+            device.setExtraParams(json);
+            deviceConfigService.updateById(device);
+        } catch (Exception e) {
+            log.error("淇濆瓨鎺у埗鍙傛暟澶辫触 deviceId={}", deviceId, e);
+            throw new RuntimeException("淇濆瓨鎺у埗鍙傛暟澶辫触: " + e.getMessage(), e);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    private Map<String, Object> readExtraMap(DeviceConfig device) {
+        try {
+            if (device.getExtraParams() == null || device.getExtraParams().trim().isEmpty()) {
+                return new HashMap<>();
+            }
+            return objectMapper.readValue(device.getExtraParams(),
+                    new TypeReference<Map<String, Object>>() {});
+        } catch (Exception e) {
+            log.warn("瑙f瀽璁惧鎵╁睍鍙傛暟澶辫触 deviceId={}", device.getId(), e);
+            return new HashMap<>();
+        }
+    }
+}
+

--
Gitblit v1.8.0