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 implements DeviceGroupRelationService { @Autowired private DeviceGroupRelationMapper deviceGroupRelationMapper; @Override public void addDeviceToGroup(Long groupId, Long deviceId, String deviceRole) { try { // 检查设备是否已在设备组中 LambdaQueryWrapper 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 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 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 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 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 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); } } }