package com.elab.core.async.pruducer;

import com.elab.core.async.RealTaskExecutor;
import com.elab.core.async.SchedulingTaskExecutor;
import com.elab.core.async.TaskStoreProcess;
import com.elab.core.async.model.TaskStoreData;
import com.elab.core.async.store.TaskExecutorDecoration;
import com.elab.core.async.store.TaskExecutorQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 实时生产者
 *
 * @author ： liukx
 * @time ： 2020/8/6 - 11:17
 */
public class AbstractTaskProducer implements ITaskProducer {

    private Logger logger = LoggerFactory.getLogger(AbstractTaskProducer.class);

    private TaskExecutorQueue taskExecutorQueue;

    private TaskStoreProcess handler;

    public AbstractTaskProducer(TaskExecutorQueue taskExecutorQueue) {
        this.taskExecutorQueue = taskExecutorQueue;
    }

    public void setHandler(TaskStoreProcess handler) {
        this.handler = handler;
    }

    @Override
    public void sendRealTimeQueue(RealTaskExecutor taskExecutor) {
        sendRealTimeQueue(taskExecutor, null);
    }

    @Override
    public void sendRealTimeQueue(RealTaskExecutor taskExecutor, TaskStoreData taskStoreData) {
        if (taskStoreData != null && taskStoreData.isSave()) {
            this.handler.add(taskStoreData);
        }
        TaskExecutorDecoration taskExecutorDecoration = new TaskExecutorDecoration(taskStoreData, taskExecutor);
        boolean isConsumer = this.taskExecutorQueue.getRealTimeQueue().add(taskExecutorDecoration);

        if (isConsumer) {
            logger.debug("加入异步消费队列");
        } else {

        }
    }

    @Override
    public void sendSchedulingList(SchedulingTaskExecutor schedulingTaskExecutor) {
        this.taskExecutorQueue.getSchedulingList().add(schedulingTaskExecutor);
    }
}
