package com.base.task.core;

import com.base.task.model.Enums.TaskTypeEnum;
import com.base.task.model.Task;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.Iterator;
import java.util.List;

@Slf4j
public class TaskThread implements Runnable {


    private TaskTypeEnum taskType;

    private Execute execute;


    public TaskThread(TaskTypeEnum taskType, Execute execute) {

        this.taskType = taskType;
        this.execute = execute;

    }

    @Override
    public void run() {
        try {
            while (true) {
                Thread.sleep(50);
                TaskCache taskCache = TaskCache.getInstance();
                List<Task> list = taskCache.getCurrentTaskMap().get(taskType);
                if (CollectionUtils.isEmpty(list)) {
                    Thread.sleep(50);
                    continue;
                }
                for (Task task:list){
                    if (task.getState()==0){
                        execute.execute(task);
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
    }
}
