package com.ruoyi.credit.config;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * 验证任务配置类
 * 配置异步任务执行器和调度器
 * 
 * @author ruoyi
 * @date 2025-12-31
 */
@Configuration
@EnableAsync
@EnableScheduling
public class ValidationTaskConfig {

    /** 验证任务线程池核心线程数 */
    @Value("${loan.validation.task.corePoolSize:5}")
    private int corePoolSize;

    /** 验证任务线程池最大线程数 */
    @Value("${loan.validation.task.maxPoolSize:20}")
    private int maxPoolSize;

    /** 验证任务线程池队列容量 */
    @Value("${loan.validation.task.queueCapacity:200}")
    private int queueCapacity;

    /** 验证任务线程池线程名前缀 */
    @Value("${loan.validation.task.threadNamePrefix:ValidationTask-}")
    private String threadNamePrefix;

    /** 验证任务线程池空闲线程存活时间（秒） */
    @Value("${loan.validation.task.keepAliveSeconds:60}")
    private int keepAliveSeconds;

    /**
     * 验证任务异步执行器
     * 用于处理验证相关的异步任务
     * 
     * @return 线程池任务执行器
     */
    @Bean("validationTaskExecutor")
    public Executor validationTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        
        // 设置核心线程数
        executor.setCorePoolSize(corePoolSize);
        
        // 设置最大线程数
        executor.setMaxPoolSize(maxPoolSize);
        
        // 设置队列容量
        executor.setQueueCapacity(queueCapacity);
        
        // 设置线程名前缀
        executor.setThreadNamePrefix(threadNamePrefix);
        
        // 设置空闲线程存活时间
        executor.setKeepAliveSeconds(keepAliveSeconds);
        
        // 设置拒绝策略：由调用线程处理该任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        // 设置线程池关闭时等待所有任务完成
        executor.setWaitForTasksToCompleteOnShutdown(true);
        
        // 设置线程池关闭时的等待时间
        executor.setAwaitTerminationSeconds(30);
        
        // 初始化线程池
        executor.initialize();
        
        return executor;
    }

    /**
     * 通知任务异步执行器
     * 用于处理通知相关的异步任务
     * 
     * @return 线程池任务执行器
     */
    @Bean("notificationTaskExecutor")
    public Executor notificationTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        
        // 通知任务使用较小的线程池
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("NotificationTask-");
        executor.setKeepAliveSeconds(60);
        
        // 设置拒绝策略：丢弃最旧的任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardOldestPolicy());
        
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(15);
        
        executor.initialize();
        
        return executor;
    }

    /**
     * 批量处理任务异步执行器
     * 用于处理批量验证等大批量任务
     * 
     * @return 线程池任务执行器
     */
    @Bean("batchProcessTaskExecutor")
    public Executor batchProcessTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        
        // 批量处理使用更大的线程池
        executor.setCorePoolSize(3);
        executor.setMaxPoolSize(15);
        executor.setQueueCapacity(50);
        executor.setThreadNamePrefix("BatchProcessTask-");
        executor.setKeepAliveSeconds(120);
        
        // 设置拒绝策略：由调用线程处理该任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        
        executor.initialize();
        
        return executor;
    }
}