package org.enfi.common.util;

import org.enfi.common.system.util.JwtUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadUtil {

    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static void setApplicationContext(ApplicationContext applicationContext) {
        ThreadUtil.applicationContext = applicationContext;
    }

    public static ThreadPoolExecutor getTheadPool() {
        return ThreadUtil.getTheadPool(5);
    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request;
    }

    public static HttpServletResponse getResponse() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return response;
    }

    public static ThreadPoolExecutor getTheadPool(Integer corePoolSize) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, //核心线程数
                20, //最大线程数
                120, TimeUnit.SECONDS, //每个线程最大运行时间 ,和时间单位
                new LinkedBlockingQueue<Runnable>(20000));//线程存放队列 ，有界队列容量2万
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        RequestContextHolder.setRequestAttributes(servletRequestAttributes, true);//设置子线程共享
        return threadPoolExecutor;
    }


    public static Boolean waitThreadPoolCompleted(ThreadPoolExecutor threadPoolExecutor) {
        return ThreadUtil.waitThreadPoolCompleted(threadPoolExecutor, "");
    }

    public static Boolean waitThreadPoolCompleted(ThreadPoolExecutor threadPoolExecutor, String message) {
        return ThreadUtil.waitThreadPoolCompleted(threadPoolExecutor, message, () -> {
        });
    }

    public static Boolean waitThreadPoolCompleted(ThreadPoolExecutor threadPoolExecutor, String message, ExecuteOperation operation) {
        threadPoolExecutor.shutdown();
        try {
            while (true) {
                TimeUnit.MILLISECONDS.sleep(1000); //线程睡眠一秒
                if (threadPoolExecutor.isTerminated()) {
                    System.out.println("===========================================================================");
                    System.out.println(message + " 全部线程执行完毕 ");
                    System.out.println("===========================================================================");
                    operation.execute();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static String getUserName() {
        return JwtUtil.getUserNameByToken(getRequest());
    }

    public interface ExecuteOperation {
        void execute();
    }
}
