package com.jiayue.ipfcst;

import com.jiayue.ipfcst.console.service.QuartzJobFactory;
import io.netty.util.ResourceLeakDetector;
import lombok.extern.slf4j.Slf4j;
import net.sf.ehcache.CacheManager;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * 启动类
 *
 * @author zzy
 * @version 1.0
 * @since 2019/7/22 9:05
 */
@Configuration
@SpringBootApplication()
@Slf4j
@EnableCaching
@EnableScheduling
public class ConsoleApplication {

    private final Environment environment;
    @Qualifier("dataSource")
    @Autowired
    private DataSource dataSource;

    @Autowired
    public ConsoleApplication(Environment environment) {
        this.environment = environment;
    }

    public static void main(String[] args) {
      ResourceLeakDetector.setLevel(ResourceLeakDetector.Level.ADVANCED);
        //变更配置文件读取位置启动
        new SpringApplicationBuilder(ConsoleApplication.class).run(args);
    }

    /**
     * 调度器Bean
     *
     * @param transactionManager 事务管理
     * @param applicationContext 上下文
     * @param quartzJobFactory   任务工厂
     * @return 调度器Bean
     * @throws Exception 调度器启动异常
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(
            PlatformTransactionManager transactionManager, ApplicationContext applicationContext,
            QuartzJobFactory quartzJobFactory) throws Exception {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setDataSource(dataSource);
        schedulerFactoryBean.setTransactionManager(transactionManager);
        schedulerFactoryBean.setApplicationContext(applicationContext);
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        schedulerFactoryBean.setConfigLocation(new ClassPathResource("quartz.properties"));
        schedulerFactoryBean.setAutoStartup(true);
        schedulerFactoryBean.setSchedulerName(this.environment.getProperty("org.quartz.scheduler.instanceName"));
        log.info("定时器启动... ...");
        schedulerFactoryBean.setJobFactory(quartzJobFactory);
        schedulerFactoryBean.start();
        log.info("定时器启动完成");
        return schedulerFactoryBean;
    }

    /**
     * ecache缓存对象获取
     */
    @Bean
    public EhCacheCacheManager cacheManager(CacheManager cacheManagerFactory) {
        EhCacheCacheManager ehCacheCacheManager = new EhCacheCacheManager();
        ehCacheCacheManager.setCacheManager(cacheManagerFactory);
        return ehCacheCacheManager;
    }

    /**
     * ecache缓存配置加载
     */
    @Bean
    public EhCacheManagerFactoryBean cacheManagerFactory() {
        EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache-setting.xml"));
        return ehCacheManagerFactoryBean;
    }

    /**
     * 上报文件模板引擎
     *
     * @return 返回模板引擎
     */
    @Bean
    public VelocityEngine velocityEngine() {
//    VelocityEngine ve = new VelocityEngine();
//    ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
//    ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
//    ve.setProperty("input.encoding", "UTF-8");
//    ve.setProperty("output.encoding", "UTF-8");
//    ve.init();


        VelocityEngine ve = new VelocityEngine();
        Properties properties = new Properties();
        //    String vmsPath = BaseService.class.getClassLoader().getResource("vms").getPath();
        //设置velocity资源加载方式为file
        properties.setProperty("resource.loader", "file");
        //设置velocity资源加载方式为file时的处理类
        properties
                .setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
        properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "");
        properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
        properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
        ve.init(properties);
        return ve;
    }
}
