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