package com.tofly.auth.balance;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.Server;
import com.tofly.common.core.constant.SecurityConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author tuonbed
 * @date 2020/7/21  16:29
 */
@Slf4j
public class ToflyChooseBalancerRule extends AbstractLoadBalancerRule {

//    @Autowired
//    private RedisTemplate redisTemplate;

    @Autowired
    private ValueOperations<String,String> valueOperations;

    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {

    }

    /**
     * 自定义的选择方法
     * @param key
     * @return
     */
    @Override
    public Server choose(Object key) {
        List<Server> servers = this.getLoadBalancer().getReachableServers();
        if (servers.isEmpty()) {
            return null;
        }
        if (servers.size() == 1) {
            return servers.get(0);
        }
//        if (key == null) {
//            return randomChoose(servers);
//        }
        return myChoose(servers, key);
    }

    /**
     * 返回随机实例
     * @param servers
     * @return
     */
    private Server randomChoose(List<Server> servers) {
        int randomIndex = RandomUtils.nextInt(servers.size());
        return servers.get(randomIndex);
    }

    /**
     * 自定义的根据规则返回实例
     * @param servers
     * @param key
     * @return
     */
    private Server myChoose(List<Server> servers, Object key) {

//        String o = (String)redisTemplate.opsForValue().get(SecurityConstants.REDIS_LOGIN_LOCK);
        String o = valueOperations.get(SecurityConstants.REDIS_LOGIN_LOCK);
        log.info("从redis中获取到project:{}",o);
        if(o!=null){
            for(Server server:servers){
                if(server.getPort()==Integer.valueOf(o)){
                    return server;
                }
            }
        }
        return randomChoose(servers);

    }
}
