package com.sw.mes.gateway.routes;//package js.hz.sw.mes.routes;
//
//import com.alibaba.fastjson.JSON;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.route.RouteDefinition;
//import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
//import org.springframework.data.redis.core.BoundHashOperations;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Component;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
//@Component
//public class RedisRouteDefinitionRepository implements RouteDefinitionRepository {
//
//    public static final String GATEWAY_ROUTES_PREFIX = "GATEWAY_ROUTES";
//
//    @Autowired
//    private RedisTemplate redisTemplate;
//
//    private Set<RouteDefinition> routeDefinitions = new HashSet<>();
//
//    @Override
//    public Flux<RouteDefinition> getRouteDefinitions() {
//
//        routeDefinitions.clear();
//
//        BoundHashOperations<String, String, String> boundHashOperations = redisTemplate.boundHashOps(GATEWAY_ROUTES_PREFIX);
//
//        Map<String ,String> map =  boundHashOperations.entries() ;
//
//        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
//        while (it.hasNext()) {
//            Map.Entry<String, String> entry = it.next();
//            routeDefinitions.add(JSON.parseObject(entry.getValue(), RouteDefinition.class));
//        }
//
//        return Flux.fromIterable(routeDefinitions);
//    }
//
//    @Override
//    public Mono<Void> save(Mono<RouteDefinition> route) {
//        return route.flatMap(routeDefinition -> {
//            routeDefinitions.add(routeDefinition);
//            return Mono.empty();
//        });
//    }
//
//    @Override
//    public Mono<Void> delete(Mono<String> routeId) {
//        return routeId.flatMap(id -> {
//            List<RouteDefinition> collect = routeDefinitions.stream().filter(
//                    routeDefinition -> StringUtils.equals(routeDefinition.getId(), id)
//            ).collect(Collectors.toList());
//            routeDefinitions.removeAll(collect);
//            return Mono.empty();
//        });
//    }
//}
