package cn.com.capcloud.core.quartz.config;

import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@Configuration
public class ScheduledTaskConfig {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ScheduledTaskConfig.class);
    @Bean
    public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
        //重写sout输出 todo 这块代码后续独立出来，暂时先放TaskConfig里
        PrintStream customOut = new PrintStream(System.out) {
            @Override
            public void print(boolean s) {
                String outMessage=getSystemOutMessage();
                super.print(outMessage+s);
            }

            @Override
            public void print(int s) {
                String outMessage=getSystemOutMessage();
                super.print(outMessage+s);
            }

            @Override
            public void print(long s) {
                String outMessage=getSystemOutMessage();
                super.print(outMessage+s);
            }

            @Override
            public void print(float s) {
                String outMessage=getSystemOutMessage();
                super.print(outMessage+s);
            }

            @Override
            public void print(double s) {
                String outMessage=getSystemOutMessage();
                super.print(outMessage+s);
            }

            @Override
            public void print(String s) {
                String outMessage=getSystemOutMessage();
                super.print(outMessage+s);
            }

            @Override
            public void print(Object s) {
                String outMessage=getSystemOutMessage();
                super.print(outMessage+s);
            }

            @Override
            public void print(char s) {
                String outMessage=getSystemOutMessage();
                super.print(outMessage+s);
            }

        };
        System.setOut(customOut);
        //end

        logger.info("创建定时任务调度线程池 start");
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(50);
        threadPoolTaskScheduler.setThreadNamePrefix("taskExecutor-");
        threadPoolTaskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        threadPoolTaskScheduler.setAwaitTerminationSeconds(60);
        logger.info("创建定时任务调度线程池 end");
        return threadPoolTaskScheduler;
    }

    public String getSystemOutMessage(){
        // 获取调用栈信息
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        StackTraceElement caller = stackTrace[4];
        String className = caller.getClassName();
        int lineNumber = caller.getLineNumber();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
        Date now = new Date();// 获取当前日期和时间
        String formattedDate = sdf.format(now);// 格式化当前日期和时间
        String message = formattedDate+" (" + className+"."+caller.getMethodName() + ":" + lineNumber+"): ";
        return message;
    }
}
