package com.persagy.dmp.job.config;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * xxl-job config
 * @author : lijie
 * Update By 2022/1/24 20:16
 */
@Configuration
@Slf4j
public class XxlJobConfig {

    @Value("${xxl.job.admin.addresses:http://job-admin:8080/job-admin}")
    private String adminAddresses;

    @Value("${xxl.job.accessToken:}")
    private String accessToken;

    @Value("${xxl.job.executor.appname:${spring.application.name}-job-executor}")
    private String appname;

    @Value("${xxl.job.executor.address:}")
    private String address;

    @Value("${xxl.job.executor.ip:}")
    private String ip;

    @Value("${xxl.job.executor.port:}")
    private Integer port;

    @Value("${xxl.job.executor.logpath:./logs/job-executor/application/jobHandler}")
    private String logPath;

    @Value("${xxl.job.executor.logretentiondays:7}")
    private int logRetentionDays;

    @Autowired
    private Environment environment;

    /**应用名称key*/
    private final static String APPLICATION_NAME="spring.application.name";
    /**http协议头*/
    private final static String HTTP_HEAD="http://";
    /**应用端口*/
    private final static String SERVER_PORT="server.port";


    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        if (StrUtil.isNotBlank(environment.getProperty(APPLICATION_NAME))
                && StrUtil.isNotBlank(environment.getProperty(SERVER_PORT))
                && NumberUtil.isNumber(environment.getProperty(SERVER_PORT))){
            if (StrUtil.isBlank(address)){
                address=HTTP_HEAD+environment.getProperty(APPLICATION_NAME)
                        +StrUtil.COLON+(NumberUtil.parseInt(environment.getProperty(SERVER_PORT))+1);
            }
            if (StrUtil.isBlank(ip)){
                ip=environment.getProperty(APPLICATION_NAME);
            }
            if (ObjectUtil.isNull(port)){
                port=NumberUtil.parseInt(environment.getProperty(SERVER_PORT))+1;
            }
        }
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppname(appname);
        xxlJobSpringExecutor.setAddress(address);
        xxlJobSpringExecutor.setIp(ip);
        xxlJobSpringExecutor.setPort(port);
        xxlJobSpringExecutor.setAccessToken(accessToken);
        xxlJobSpringExecutor.setLogPath(logPath);
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        return xxlJobSpringExecutor;
    }

    /**
     * 针对多网卡、容器内部署等情况，可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP；
     *
     *      1、引入依赖：
     *          <dependency>
     *             <groupId>org.springframework.cloud</groupId>
     *             <artifactId>spring-cloud-commons</artifactId>
     *             <version>${version}</version>
     *         </dependency>
     *
     *      2、配置文件，或者容器启动变量
     *          spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.'
     *
     *      3、获取IP
     *          String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
     */


}