diff --git a/dongjian-dashboard-back-service/src/main/java/com/dongjian/dashboard/back/service/impl/DeviceGroupServiceImpl.java b/dongjian-dashboard-back-service/src/main/java/com/dongjian/dashboard/back/service/impl/DeviceGroupServiceImpl.java index c4c053b..234c100 100644 --- a/dongjian-dashboard-back-service/src/main/java/com/dongjian/dashboard/back/service/impl/DeviceGroupServiceImpl.java +++ b/dongjian-dashboard-back-service/src/main/java/com/dongjian/dashboard/back/service/impl/DeviceGroupServiceImpl.java @@ -357,29 +357,28 @@ public class DeviceGroupServiceImpl implements DeviceGroupService { } List deviceInfoIdList = CommonUtil.commaStr2LongList(bindDeviceForGroupParams.getDeviceInfoIds()); - if (CollectionUtils.isEmpty(deviceInfoIdList)) { return SimpleDataResponse.success(); } Long groupId = bindDeviceForGroupParams.getDeviceGroupId(); - List existedIds = deviceGroupRelationMapperExt.selectDeviceInfoIdsByGroupId(groupId); - Set existedSet = new HashSet<>(existedIds); - - // 过滤掉已绑定的 - List filteredList = deviceInfoIdList.stream() - .filter(id -> !existedSet.contains(id.intValue())) + // 转换为 Integer 列表用于删除 + List deviceInfoIds = deviceInfoIdList.stream() + .map(Long::intValue) .collect(Collectors.toList()); - // 插入新绑定关系 - if (CollectionUtils.isNotEmpty(filteredList)) { - for (Long deviceInfoId : filteredList){ - DeviceGroupRelation deviceGroupRelation = new DeviceGroupRelation(); - deviceGroupRelation.setDeviceGroupId(bindDeviceForGroupParams.getDeviceGroupId()); - deviceGroupRelation.setDeviceInfoId(deviceInfoId.intValue()); - deviceGroupRelationMapperExt.insertSelective(deviceGroupRelation); - } + // 2. 使用 Example 删除指定设备ID的所有绑定记录 + DeviceGroupRelationExample example = new DeviceGroupRelationExample(); + example.createCriteria().andDeviceInfoIdIn(deviceInfoIds); + deviceGroupRelationMapperExt.deleteByExample(example); + + // 3. 插入新绑定关系 + for (Long deviceInfoId : deviceInfoIdList) { + DeviceGroupRelation deviceGroupRelation = new DeviceGroupRelation(); + deviceGroupRelation.setDeviceGroupId(groupId); + deviceGroupRelation.setDeviceInfoId(deviceInfoId.intValue()); + deviceGroupRelationMapperExt.insertSelective(deviceGroupRelation); } return SimpleDataResponse.success();