webflux 链表逻辑用例

78 阅读1分钟
public Mono<G<IPage<GatewayRefPrivilegeGroupVO>>> selectGatewayRefPrivilegeGroupsPages(Integer groupId, String keyword, Integer type, Pageable pageable) {
    UserVO userVO = ContextUtils.getCurrentUser();
    Criteria criteria = null;
    Criteria gatewayCriteria = null;
    if(StringUtils.hasText(keyword)){
        gatewayCriteria = where("tenant_id").is(userVO.getTenantId());
        gatewayCriteria = type==1?
                gatewayCriteria.and("gateway_name").like("%"+keyword+"%")
                :gatewayCriteria.and("serial_number").like("%"+keyword+"%");
    } else {
        gatewayCriteria = where("tenant_id").is(userVO.getTenantId());
    }

    Criteria finalGatewayCriteria = gatewayCriteria;
    return r2dbcEntityTemplate
        .select(query(gatewayCriteria).sort(Sort.by(Sort.Order.desc("create_time"))), Gateway.class)
        //  
        .flatMap(tt->{
            Criteria finalCriteria = where("tenant_id").is(userVO.getTenantId())
                    .and("gateway_id").is(tt.getGatewayId());
                    //  
                    if(groupId != -1){
                        finalCriteria = finalCriteria.and("privilege_group_id").is(groupId);
                    }
            return r2dbcEntityTemplate.selectOne(query(finalCriteria), GatewayRefPrivilegeGroup.class)
                .flatMap(c -> Mono.just(Optional.of(c))).defaultIfEmpty(Optional.empty())
                .flatMap(option -> {
                    if(option.isPresent() && groupId != -1){
                        GatewayRefPrivilegeGroup gateway = option.get();
                        GatewayRefPrivilegeGroupVO refPrivilegeGroupVO = new GatewayRefPrivilegeGroupVO();
                        refPrivilegeGroupVO.setSerialNumber(tt.getSerialNumber());
                        refPrivilegeGroupVO.setGatewayName(tt.getGatewayName());
                        refPrivilegeGroupVO.setGatewayId(gateway.getGatewayId());
                        return Mono.just(refPrivilegeGroupVO);
                    } else if(!option.isPresent() && groupId == -1){
                        GatewayRefPrivilegeGroupVO refPrivilegeGroupVO = new GatewayRefPrivilegeGroupVO();
                        refPrivilegeGroupVO.setSerialNumber(tt.getSerialNumber());
                        refPrivilegeGroupVO.setGatewayName(tt.getGatewayName());
                        refPrivilegeGroupVO.setGatewayId(tt.getGatewayId());
                        return Mono.just(refPrivilegeGroupVO);
                    }
                return Mono.empty();
            });
        }).collectList().flatMap(list->{
            Integer pageNum = pageable.getPageNumber();
            Integer pageSize = pageable.getPageSize();
            AtomicLong count = new AtomicLong();
            IPage<GatewayRefPrivilegeGroupVO> page = new Page<GatewayRefPrivilegeGroupVO>(pageNum, pageSize);
            Integer fromIdx = pageNum * pageSize>list.size()?0:pageNum * pageSize;
            Integer toIndx = (pageNum + 1) * pageSize>list.size()?list.size():(pageNum + 1) * pageSize;
            List<GatewayRefPrivilegeGroupVO> records = list.subList(fromIdx, toIndx);
            page.setRecords(records);
            // 获取总数
            return r2dbcEntityTemplate.select(query(finalGatewayCriteria), Gateway.class).flatMap(tt->{
                    Criteria finalCriteria = where("tenant_id").is(userVO.getTenantId())
                        .and("privilege_group_id").is(groupId)
                        .and("gateway_id").is(tt.getGatewayId());
                    return r2dbcEntityTemplate.count(query(finalCriteria), GatewayRefPrivilegeGroup.class);
                }
            ).doOnNext(num->{
                count.addAndGet(num);
                page.setTotal(count.get());
            }).then(Mono.just(G.success(page)));
        });
}