package cn.cslg.pas.common.core;

import cn.cslg.pas.domain.Task;
import cn.cslg.pas.service.TaskService;
import cn.cslg.pas.service.upLoadPatent.PantentQueueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目启动时初始化任务列表队列
 *
 * @Author chenyu
 * @Date 2023/6/2
 */
@Component
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class CreateTaskThread implements InitializingBean {

    private final TaskService taskService;
    private final PantentQueueService pantentQueueService;

    @Override
    public void afterPropertiesSet() throws Exception {
        //初始化任务队列
        //查找任务状态为0（队列中）和1（进行中）的任务
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>().in(Task::getStatus, new ArrayList<>(Arrays.asList(0, 1))));
        if (tasks.size() > 0) {
            //过滤出任务ids
            List<Integer> taskIds = tasks.stream().map(Task::getId).collect(Collectors.toList());
            //过滤出进行中的任务的id
            List<Task> doingTasks = tasks.stream().filter(item -> item.getStatus() == 1).collect(Collectors.toList());
            List<Integer> doingTaskIds = doingTasks.stream().map(Task::getId).collect(Collectors.toList());
            if (doingTaskIds.size() > 0) {
                //先将进行中的任务（项目重启后进行中的任务仅会有1个）的id先存入任务队列，再将其余存入
                pantentQueueService.taskQueueListAddTask(doingTaskIds);
                taskIds.removeAll(doingTaskIds);
            }
            pantentQueueService.taskQueueListAddTask(taskIds);
        }

        //生产者线程
        Thread threadProducer = new Thread(pantentQueueService::addPatnetToQueue);
        threadProducer.start();

        //装载专利著录线程
        Thread threadSetPatentZhuLu = new Thread(pantentQueueService::setPatentZhuLu);
        threadSetPatentZhuLu.start();
        //装载权要线程
        Thread threadSetPatentClaim = new Thread(pantentQueueService::setPatentRight);
        threadSetPatentClaim.start();
        //装载说明书文本线程
        Thread threadSetPatentInstructionText = new Thread(pantentQueueService::setPatentInstructionText);
        threadSetPatentInstructionText.start();
        //装载说明书pdf线程
        Thread threadSetPatentInstructionPDF = new Thread(pantentQueueService::setPatentInstructionPDF);
        threadSetPatentInstructionPDF.start();
        //装载摘要附图线程
        Thread threadSetPatentPicture = new Thread(pantentQueueService::setPatentImage);
        threadSetPatentPicture.start();

        //消费者1线程（著录项目）
        Thread threadConsumer1 = new Thread(pantentQueueService::pushPatentZhuLuToDB);
        threadConsumer1.start();
        //消费者2线程（权利要求）
        Thread threadConsumer2 = new Thread(pantentQueueService::pushPatentRightToDB);
        threadConsumer2.start();
        //消费者3线程（说明书文本）
        Thread threadConsumer3 = new Thread(pantentQueueService::pushPatentInstructionTextToDB);
        threadConsumer3.start();
        //消费者4线程（说明书pdf）
        Thread threadConsumer4 = new Thread(pantentQueueService::pushPatentInstructionPDFToDB);
        threadConsumer4.start();
        //消费者5线程（摘要附图）
        Thread threadConsumer5 = new Thread(pantentQueueService::pushPatentImageToDB);
        threadConsumer5.start();

    }

}
