package com.td.boss.game.comsnbapply.scheduled;

import com.td.boss.util.IpUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.net.InetAddress;

/**
 * 判断是否为本机ip
 *
 * @author:slambb
 * @date:2022/3/15
 */
@Slf4j
public class IPCondition implements Condition {

    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        String runTaskIP = conditionContext.getEnvironment().getProperty("apply.ip");
        String localIP = IpUtil.getIpVo(null).getIp(); //this.getLocalIP();

        log.info("local ip: {} , run Task IP: {}" ,localIP , runTaskIP);

        return runTaskIP.equals(localIP);
    }

    @SneakyThrows
    public String getLocalIP(){
        InetAddress address = InetAddress.getLocalHost();
        return address.getHostAddress();
    }
}