package org.jeecg.module.custom.ocr.api.thread;

import lombok.SneakyThrows;
import org.jeecg.module.custom.ocr.api.entity.FormRecord;
import org.jeecg.module.custom.ocr.api.thread.tasks.PictureDisposeTask;
import org.jeecg.module.custom.ocr.api.thread.tasks.PictureImgToLocalTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

/**
 * 任务线程池
 *
 * @author hugh(shuli.yao) 1397940314@qq.com
 * @version 1.0
 * @date 2024/03/14 3:08
 */
public class TaskThreadPool {

    private static Logger logger = LoggerFactory.getLogger(TaskQueue.class);
    ExecutorService threadPool = null;
    ExecutorService threadPoolImg = null;

    public TaskThreadPool() {
        if (threadPool == null) {
            threadPool = new ThreadPoolExecutor(
                    10,
                    40,
                    3,
                    TimeUnit.SECONDS,
                    new LinkedBlockingDeque<>(),
                    Executors.defaultThreadFactory(),
                    new ThreadPoolExecutor.DiscardOldestPolicy());

            threadPoolImg = new ThreadPoolExecutor(
                    10,
                    40,
                    3,
                    TimeUnit.SECONDS,
                    new LinkedBlockingDeque<>(),
                    Executors.defaultThreadFactory(),
                    new ThreadPoolExecutor.DiscardOldestPolicy());
        }
    }

    /**
     * 启动任务 入库消费者
     */
    public void startPictureDisposePull() {
        Runnable runnable = new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                while (true) {
                    logger.debug("无量云接口数据处理消费线程检测中...,队列数量:{}",TaskQueue.pictureDisposeQueue.size());
                    FormRecord pictureSourceResult = TaskQueue.pictureDisposePullData();
                    if (pictureSourceResult != null) {
                        threadPool.execute(new PictureDisposeTask(pictureSourceResult));
                    } else {
                        Thread.sleep(5000);
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

    /**
     * 启动任务 入库消费者
     */
    public void startPictureImgToLocalPull() {
        Runnable runnable = new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                while (true) {
                    logger.debug("图片下载消费线程检测中...,队列数量:{}",TaskQueue.pictureImgToLocalQueue.size());
                    try {
                        FormRecord pictureImgToLocalEntity = TaskQueue.pictureImgToLocalPullData();
                        if (pictureImgToLocalEntity != null) {
                            threadPoolImg.execute(new PictureImgToLocalTask(pictureImgToLocalEntity));
                        } else {
                            Thread.sleep(5000);
                        }
                    } catch (Exception e) {
                        logger.error("图片下载消费线程报错{}", e.getMessage());
                        Thread.sleep(5000);
                    }

                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

}
