/**
 * @ProjectName: 智慧评标-主观分助手
 * @Package: com.zhpb.gdyd_zhpb_zgf.config
 * @ClassName: AsyncConfig
 * @Description: 异步任务线程池配置
 * @Author: 张志锋
 * @Date: 2025-12-11
 * @Version: 1.0
 * @Copyright: © 2025 智慧评标-主观分助手
 */
package com.zhpb.gdyd_zhpb_zgf.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

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

/**
 * 异步任务线程池配置
 * 为AI模型调用和异步处理任务提供专用的线程池
 */
@Configuration
@EnableAsync
public class AsyncConfig {

    private static final Logger logger = LoggerFactory.getLogger(AsyncConfig.class);

    /**
     * AI任务专用线程池
     * 用于章节总结、横向对比等AI相关的异步任务
     */
    @Bean(name = "aiTaskExecutor")
    public Executor aiTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(3);      // 核心线程数
        executor.setMaxPoolSize(6);       // 最大线程数
        executor.setQueueCapacity(50);    // 队列容量
        executor.setThreadNamePrefix("ai-task-");
        executor.setKeepAliveSeconds(60);
        
        // 设置拒绝策略：由调用者线程执行（确保任务不会丢失）
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        executor.initialize();
        logger.info("AI任务线程池初始化完成: coreSize={}, maxSize={}, queueCapacity={}", 
            executor.getCorePoolSize(), executor.getMaxPoolSize(), 50);
        return executor;
    }

    /**
     * 目录抽取专用线程池
     * 用于文件目录抽取等IO密集型任务
     */
    @Bean(name = "directoryTaskExecutor")
    public Executor directoryTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);      // 核心线程数
        executor.setMaxPoolSize(4);       // 最大线程数
        executor.setQueueCapacity(100);   // 队列容量
        executor.setThreadNamePrefix("dir-task-");
        executor.setKeepAliveSeconds(60);
        
        // 设置拒绝策略：由调用者线程执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        executor.initialize();
        logger.info("目录抽取线程池初始化完成: coreSize={}, maxSize={}, queueCapacity={}", 
            executor.getCorePoolSize(), executor.getMaxPoolSize(), 100);
        return executor;
    }

    /**
     * 对比分析专用线程池
     * 用于供应商横向对比等分析任务
     */
    @Bean(name = "comparisonTaskExecutor")
    public Executor comparisonTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);      // 核心线程数
        executor.setMaxPoolSize(4);       // 最大线程数
        executor.setQueueCapacity(30);    // 队列容量
        executor.setThreadNamePrefix("comp-task-");
        executor.setKeepAliveSeconds(60);
        
        // 设置拒绝策略：由调用者线程执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        
        executor.initialize();
        logger.info("对比分析线程池初始化完成: coreSize={}, maxSize={}, queueCapacity={}", 
            executor.getCorePoolSize(), executor.getMaxPoolSize(), 30);
        return executor;
    }
}
