webFlux 递归用例

329 阅读1分钟
@Transactional(rollbackFor = Exception.class)
@CacheEvict(value = "gatewayGroup", key = "#groupId")
public Mono<G> deleteGatewayGroup(UserVO userVO, Integer groupId) {
    return r2dbcEntityTemplate
        .select(query(where("group_id").is(groupId).and("tenant_id").is(userVO.getTenantId())), GatewayGroup.class)
        .flatMap(gatewayGroup-> deleteChildGroup(gatewayGroup, userVO))
        .then(Mono.just(G.success()));
}

private Mono<G> deleteChildGroup(GatewayGroup gatewayGroup, UserVO userVO){
   return r2dbcEntityTemplate
       // 存在 删除群组
       .delete(query(where("group_id").is(gatewayGroup.getGroupId()).and("tenant_id").is(userVO.getTenantId())), GatewayGroup.class)
       .flatMap(m->{
           // 删除子组
           return r2dbcEntityTemplate.select(query(where("parent_id").is(gatewayGroup.getGroupId()).and("tenant_id").is(userVO.getTenantId())), GatewayGroup.class)
               .flatMap(c -> Mono.just(Optional.of(c))).defaultIfEmpty(Optional.empty())
               .flatMap(option->{
                   if(option.isPresent()){
                       GatewayGroup child = option.get();
                       return deleteChildGroup(child, userVO);
                   }
                   return Mono.empty();
               })
           .then(Mono.just(G.success()));
       });

}