package cn.cslg.pas.service.upLoadPatent;

import cn.cslg.pas.domain.Task;
import cn.cslg.pas.domain.asso.TaskCondition;
import cn.cslg.pas.service.TaskService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Arrays;
import java.util.List;

/**
 * 定义任务
 */
@RequiredArgsConstructor
@DisallowConcurrentExecution
public class TaskAddJob extends QuartzJobBean {

    private static final Log logger = LogFactory.getLog(TaskAddJob.class);
    private boolean flag = true;
    private final PantentQueueService pantentQueueService;
    private final TaskService taskService;

    @Override
    public void executeInternal(JobExecutionContext context) throws JobExecutionException {
        JobDataMap jobDataMap = context.getTrigger().getJobDataMap();
        TaskCondition taskCondition = (TaskCondition) jobDataMap.get("taskCondition");
        Task task = new Task();
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskConditionId, taskCondition.getId()));
        if (tasks != null && tasks.size() > 0) {
            task.setProjectId(tasks.get(0).getProjectId());
            task.setCreateBy(tasks.get(0).getCreateBy());
            task.setPramJson(tasks.get(0).getPramJson());
        }
        task.setType(taskCondition.getTaskType2());
        task.setTaskType(taskCondition.getTaskType());
        task.setTaskConditionId(taskCondition.getId());
        task.setTotal(taskCondition.getTotal());
        task.setStatus(0);
        task.setSuccessNum(0);
        task.setTrueSuccessNum(0);
        task.setDefaultNum(0);
        taskService.save(task);
        //任务存入生产者任务队列并唤醒生产者线程
        pantentQueueService.taskQueueListAddTask(Arrays.asList(task.getId()));
        pantentQueueService.awakeTasktch();

        logger.info("添加任务" + "信息是" + task);
    }

}