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/DeviceGroupRelationServiceImpl.java |  156 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 156 insertions(+), 0 deletions(-)

diff --git a/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DeviceGroupRelationServiceImpl.java b/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DeviceGroupRelationServiceImpl.java
new file mode 100644
index 0000000..a25665c
--- /dev/null
+++ b/mes-processes/mes-plcSend/src/main/java/com/mes/device/service/impl/DeviceGroupRelationServiceImpl.java
@@ -0,0 +1,156 @@
+package com.mes.device.service.impl;
+
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.mes.device.entity.DeviceGroupRelation;
+import com.mes.device.mapper.DeviceGroupRelationMapper;
+import com.mes.device.service.DeviceGroupRelationService;
+import com.mes.device.vo.DeviceGroupVO;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.List;
+
+/**
+ * 璁惧缁勫叧绯绘湇鍔″疄鐜扮被
+ * 
+ * @author mes
+ * @since 2024-10-30
+ */
+@Slf4j
+@Service
+@Transactional
+public class DeviceGroupRelationServiceImpl extends ServiceImpl<DeviceGroupRelationMapper, DeviceGroupRelation> implements DeviceGroupRelationService {
+
+    @Autowired
+    private DeviceGroupRelationMapper deviceGroupRelationMapper;
+
+    @Override
+    public void addDeviceToGroup(Long groupId, Long deviceId, String deviceRole) {
+        try {
+            // 妫�鏌ヨ澶囨槸鍚﹀凡鍦ㄨ澶囩粍涓�
+            LambdaQueryWrapper<DeviceGroupRelation> wrapper = new LambdaQueryWrapper<>();
+            wrapper.eq(DeviceGroupRelation::getGroupId, groupId)
+                   .eq(DeviceGroupRelation::getDeviceId, deviceId)
+                   .eq(DeviceGroupRelation::getIsDeleted, 0);
+            
+            long count = count(wrapper);
+            if (count > 0) {
+                throw new RuntimeException("璁惧宸插湪璁惧缁勪腑");
+            }
+
+            // 鍒涘缓璁惧缁勫叧鑱旇褰�
+            DeviceGroupRelation relation = new DeviceGroupRelation();
+            relation.setGroupId(groupId);
+            relation.setDeviceId(deviceId);
+            
+            // 璁剧疆瑙掕壊
+            Integer roleValue;
+            switch (deviceRole.toUpperCase()) {
+                case "CONTROLLER":
+                    roleValue = DeviceGroupRelation.Role.CONTROLLER;
+                    break;
+                case "COLLABORATOR":
+                    roleValue = DeviceGroupRelation.Role.COLLABORATOR;
+                    break;
+                case "MONITOR":
+                    roleValue = DeviceGroupRelation.Role.MONITOR;
+                    break;
+                default:
+                    roleValue = DeviceGroupRelation.Role.COLLABORATOR;
+            }
+            relation.setRole(roleValue);
+            relation.setStatus(DeviceGroupRelation.Status.NORMAL);
+            relation.setPriority(5);
+            
+            save(relation);
+            log.info("璁惧 {} 宸叉垚鍔熸坊鍔犲埌璁惧缁� {}", deviceId, groupId);
+        } catch (Exception e) {
+            log.error("娣诲姞璁惧鍒拌澶囩粍澶辫触: groupId={}, deviceId={}, deviceRole={}", groupId, deviceId, deviceRole, e);
+            throw new RuntimeException("娣诲姞璁惧鍒拌澶囩粍澶辫触: " + e.getMessage(), e);
+        }
+    }
+
+    @Override
+    public void removeDeviceFromGroup(Long groupId, Long deviceId) {
+        try {
+            LambdaQueryWrapper<DeviceGroupRelation> wrapper = new LambdaQueryWrapper<>();
+            wrapper.eq(DeviceGroupRelation::getGroupId, groupId)
+                   .eq(DeviceGroupRelation::getDeviceId, deviceId)
+                   .eq(DeviceGroupRelation::getIsDeleted, 0);
+            
+            boolean removed = remove(wrapper);
+            if (!removed) {
+                throw new RuntimeException("璁惧鏈湪鎸囧畾璁惧缁勪腑");
+            }
+            
+            log.info("璁惧 {} 宸蹭粠璁惧缁� {} 绉婚櫎", deviceId, groupId);
+        } catch (Exception e) {
+            log.error("浠庤澶囩粍绉婚櫎璁惧澶辫触: groupId={}, deviceId={}", groupId, deviceId, e);
+            throw new RuntimeException("浠庤澶囩粍绉婚櫎璁惧澶辫触: " + e.getMessage(), e);
+        }
+    }
+
+    @Override
+    public void updateDeviceRole(Long groupId, Long deviceId, String deviceRole) {
+        try {
+            deviceGroupRelationMapper.updateDeviceRole(groupId, deviceId, deviceRole);
+            log.info("璁惧 {} 鍦ㄨ澶囩粍 {} 涓殑瑙掕壊宸叉洿鏂颁负 {}", deviceId, groupId, deviceRole);
+        } catch (Exception e) {
+            log.error("鏇存柊璁惧瑙掕壊澶辫触: groupId={}, deviceId={}, deviceRole={}", groupId, deviceId, deviceRole, e);
+            throw new RuntimeException("鏇存柊璁惧瑙掕壊澶辫触: " + e.getMessage(), e);
+        }
+    }
+
+    @Override
+    public List<DeviceGroupVO.DeviceInfo> getGroupDevices(Long groupId) {
+        try {
+            return deviceGroupRelationMapper.getGroupDevices(groupId);
+        } catch (Exception e) {
+            log.error("鑾峰彇璁惧缁勮澶囧垪琛ㄥけ璐�: groupId={}", groupId, e);
+            throw new RuntimeException("鑾峰彇璁惧缁勮澶囧垪琛ㄥけ璐�: " + e.getMessage(), e);
+        }
+    }
+
+    @Override
+    public List<DeviceGroupVO.GroupInfo> getDeviceGroups(Long deviceId) {
+        try {
+            return deviceGroupRelationMapper.getDeviceGroups(deviceId);
+        } catch (Exception e) {
+            log.error("鑾峰彇璁惧璁惧缁勫垪琛ㄥけ璐�: deviceId={}", deviceId, e);
+            throw new RuntimeException("鑾峰彇璁惧璁惧缁勫垪琛ㄥけ璐�: " + e.getMessage(), e);
+        }
+    }
+
+    @Override
+    public void batchAddDevicesToGroup(Long groupId, List<Long> deviceIds) {
+        try {
+            if (deviceIds == null || deviceIds.isEmpty()) {
+                throw new IllegalArgumentException("璁惧ID鍒楄〃涓嶈兘涓虹┖");
+            }
+
+            deviceGroupRelationMapper.batchAddDevicesToGroup(groupId, deviceIds, "COLLABORATOR");
+            log.info("鎵归噺娣诲姞 {} 涓澶囧埌璁惧缁� {}", deviceIds.size(), groupId);
+        } catch (Exception e) {
+            log.error("鎵归噺娣诲姞璁惧鍒拌澶囩粍澶辫触: groupId={}, deviceIds={}", groupId, deviceIds, e);
+            throw new RuntimeException("鎵归噺娣诲姞璁惧鍒拌澶囩粍澶辫触: " + e.getMessage(), e);
+        }
+    }
+
+    @Override
+    public void batchRemoveDevicesFromGroup(Long groupId, List<Long> deviceIds) {
+        try {
+            if (deviceIds == null || deviceIds.isEmpty()) {
+                throw new IllegalArgumentException("璁惧ID鍒楄〃涓嶈兘涓虹┖");
+            }
+
+            deviceGroupRelationMapper.batchRemoveDevicesFromGroup(groupId, deviceIds);
+            log.info("鎵归噺浠庤澶囩粍 {} 绉婚櫎 {} 涓澶�", groupId, deviceIds.size());
+        } catch (Exception e) {
+            log.error("鎵归噺浠庤澶囩粍绉婚櫎璁惧澶辫触: groupId={}, deviceIds={}", groupId, deviceIds, e);
+            throw new RuntimeException("鎵归噺浠庤澶囩粍绉婚櫎璁惧澶辫触: " + e.getMessage(), e);
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.8.0