删除设备后置处理
This commit is contained in:
parent
16cf97055c
commit
00502e621c
|
|
@ -10,6 +10,7 @@ import org.hswebframework.ezorm.rdb.mapping.ReactiveRepository;
|
|||
import org.hswebframework.ezorm.rdb.mapping.ReactiveUpdate;
|
||||
import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
|
||||
import org.hswebframework.ezorm.rdb.operator.dml.Terms;
|
||||
import org.hswebframework.web.crud.events.EntityDeletedEvent;
|
||||
import org.hswebframework.web.crud.service.GenericReactiveCrudService;
|
||||
import org.hswebframework.web.exception.BusinessException;
|
||||
import org.hswebframework.web.id.IDGenerator;
|
||||
|
|
@ -35,6 +36,7 @@ import org.jetlinks.core.metadata.PropertyMetadata;
|
|||
import org.jetlinks.core.metadata.types.StringType;
|
||||
import org.jetlinks.core.utils.CyclicDependencyChecker;
|
||||
import org.reactivestreams.Publisher;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
|
@ -512,39 +514,37 @@ public class LocalDeviceInstanceService extends GenericReactiveCrudService<Devic
|
|||
}
|
||||
|
||||
/**
|
||||
* 批量删除设备,同时在注册中心取消激活已激活设备,并解绑子设备和网关.
|
||||
*
|
||||
* @param id 设备ID
|
||||
* @return 删除结果数
|
||||
* 删除设备后置处理,解绑子设备和网关,并在注册中心取消激活已激活设备.
|
||||
*/
|
||||
public Mono<Integer> deleteDevice(Publisher<String> id) {
|
||||
Map<String, List<String>> childrenDevices = Maps.newHashMap();
|
||||
return this.findById(Flux.from(id))
|
||||
.flatMap(device -> {
|
||||
Mono<Void> todoMono = Mono.empty();
|
||||
if (device.getState() != DeviceState.notActive) {
|
||||
todoMono = registry.unregisterDevice(device.getId());
|
||||
}
|
||||
if (!StringUtils.isEmpty(device.getParentId())) {
|
||||
childrenDevices.getOrDefault(device.getParentId(), Lists.newArrayList()).add(device.getId());
|
||||
}
|
||||
return todoMono.thenReturn(device.getId());
|
||||
})
|
||||
.as(this::deleteById)
|
||||
.zipWith(Mono.from(Flux.defer(() ->
|
||||
Flux.fromIterable(childrenDevices.entrySet()).flatMap(entry -> {
|
||||
private Flux<Void> deletedHandle(Flux<DeviceInstanceEntity> devices) {
|
||||
return devices.filter(device -> !StringUtils.isEmpty(device.getParentId()))
|
||||
.collectMultimap(DeviceInstanceEntity::getParentId, DeviceInstanceEntity::getId)
|
||||
.flatMapMany(map ->
|
||||
Flux.fromIterable(map.entrySet()).flatMap(entry -> {
|
||||
String parentId = entry.getKey();
|
||||
List<String> children = entry.getValue();
|
||||
Collection<String> children = entry.getValue();
|
||||
// 解绑子设备和网关
|
||||
return Flux.fromIterable(children).flatMap(childrenId -> registry.getDevice(childrenId)
|
||||
.flatMap(device -> device.removeConfig(DeviceConfigKey.parentGatewayId.getKey()).thenReturn(device))
|
||||
)
|
||||
.as(childrenDeviceOp -> registry.getDevice(parentId)
|
||||
.flatMap(gwOperator -> gwOperator.getProtocol()
|
||||
.flatMap(protocolSupport -> protocolSupport.onChildUnbind(gwOperator, childrenDeviceOp))
|
||||
));
|
||||
.flatMap(gwOperator -> gwOperator.getProtocol()
|
||||
.flatMap(protocolSupport -> protocolSupport.onChildUnbind(gwOperator, childrenDeviceOp))
|
||||
)
|
||||
);
|
||||
})
|
||||
)))
|
||||
.map(Tuple2::getT1);
|
||||
)
|
||||
// 取消激活
|
||||
.thenMany(
|
||||
devices.filter(device -> device.getState() != DeviceState.notActive)
|
||||
.flatMap(device -> registry.unregisterDevice(device.getId()))
|
||||
);
|
||||
}
|
||||
|
||||
@EventListener
|
||||
public void deletedHandle(EntityDeletedEvent<DeviceInstanceEntity> event) {
|
||||
event.async(
|
||||
this.deletedHandle(Flux.fromIterable(event.getEntity()))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -345,7 +345,7 @@ public class DeviceInstanceController implements
|
|||
@Operation(summary = "批量删除设备")
|
||||
public Mono<Integer> deleteBatch(@RequestBody Mono<List<String>> idList) {
|
||||
return idList.flatMapMany(Flux::fromIterable)
|
||||
.as(service::deleteDevice);
|
||||
.as(service::deleteById);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue