package com.nokia.alarm.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration

public class BeanConfig {

    @Value("${scheduling.scheduler.pool.size:5}")
    private int poolSize;

    @Bean
    public ThreadPoolTaskScheduler taskScheduler() {
        // 使用最常用的ThreadPoolTaskScheduler
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        // 线程数
        taskScheduler.setPoolSize(poolSize);
        taskScheduler.setRemoveOnCancelPolicy(true);
        taskScheduler.setThreadNamePrefix("taskSchedulerThreadPool-");
        log.info("已完成 ThreadPoolTaskScheduler 配置, poolSize = {}", poolSize);
        return taskScheduler;
    }

    // 使用gpload速度较快
    @Bean("pgJdbcTemplate")
    public JdbcTemplate pgJdbcTemplate() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName("org.postgresql.Driver");
        config.setJdbcUrl("jdbc:postgresql://192.168.70.172:5432/sqmmt");
        config.setUsername("sqmdb");
        config.setPassword("sqmdb_1QAZ");
        config.setMaximumPoolSize(1);
        config.setMinimumIdle(1);
        return new JdbcTemplate(new HikariDataSource(config));
    }

    // @Bean("gpJdbcTemplate")
    public JdbcTemplate gpJdbcTemplate() {
        HikariConfig config = new HikariConfig();
        config.setDriverClassName("org.postgresql.Driver");
        config.setJdbcUrl("jdbc:postgresql://192.168.70.109:5432/sqmmt");
        config.setUsername("sqmdb");
        config.setPassword("sqmdb_1QAZ");
        config.setMaximumPoolSize(1);
        config.setMinimumIdle(1);
        return new JdbcTemplate(new HikariDataSource(config));
    }

}
