package com.njzscloud.common.job;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({XxlJobProperties.class})
@ConditionalOnProperty(value = "xxl-job.enable", havingValue = "true")
public class XxlJobAutoConfiguration {

    @Bean
    public XxlJobSpringExecutor xxlJobSpringExecutor(XxlJobProperties xxlJobProperties) {
        XxlAdminProperties xxlAdminProperties = xxlJobProperties.getAdmin();
        XxlExecutorProperties xxlExecutorProperties = xxlJobProperties.getExecutor();
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(xxlAdminProperties.getAddresses());
        xxlJobSpringExecutor.setAppname(xxlExecutorProperties.getAppName());
        xxlJobSpringExecutor.setIp(xxlExecutorProperties.getIp());
        xxlJobSpringExecutor.setPort(xxlExecutorProperties.getPort());
        xxlJobSpringExecutor.setAccessToken(xxlAdminProperties.getAccessToken());
        xxlJobSpringExecutor.setLogPath(xxlExecutorProperties.getLogPath());
        xxlJobSpringExecutor.setLogRetentionDays(xxlExecutorProperties.getLogRetentionDays());
        return xxlJobSpringExecutor;
    }
}
