package com.nokia.tsl_data.config;

import com.nokia.tsl_data.properties.TslDataProperties;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

@Configuration
@EnableScheduling
@EnableConfigurationProperties(TslDataProperties.class)
public class TslDataConfig {

    private final int poolSize = 3;

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 注入工作流查询专用的workFlowJdbcTemplate
     * 连接到工作流数据库的jdbcTemplate
     */
    @Bean("workFlowJdbcTemplate")
    public JdbcTemplate workFlowJdbcTemplate() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName("org.postgresql.Driver");
        config.setJdbcUrl("jdbc:postgresql://192.168.70.136:9999/flow_hb?characterEncoding=utf8");
        config.setUsername("flow_web");
        config.setPassword("Richr00t");
        return new JdbcTemplate(new HikariDataSource(config));
    }

    /**
     * 调度器，使用默认的ThreadPoolTaskScheduler
     */
    @Bean
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(poolSize);
        taskScheduler.setThreadNamePrefix("taskSchedulerThreadPool-");
        taskScheduler.initialize();
        return taskScheduler;
    }
}
