huang
2025-12-02 628aa6a42e587e9f337e213f87f922fc2ab2af02
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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);
        }
    }
}