package com.njzscloud.common.sichen.support;

import cn.hutool.extra.spring.SpringUtil;
import com.njzscloud.common.core.tuple.Tuple2;
import com.njzscloud.common.sichen.dispatcher.SichenScheduler;
import com.njzscloud.common.sichen.executor.SichenExecutor;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Method;
import java.util.List;

@Slf4j
public class Cable {
    private final static SichenScheduler SICHEN_SCHEDULER = SpringUtil.getBean(SichenScheduler.class);
    private final static SichenExecutor SICHEN_EXECUTOR = SpringUtil.getBean(SichenExecutor.class);
    private final static TaskStore taskStore = SpringUtil.getBean(TaskStore.class);

    public static Tuple2<Object, Method> getFn(String fn) {
        return taskStore.getFn(fn);
    }

    public static void execute(TaskHandle taskHandle) {
        try {
            SICHEN_EXECUTOR.execute(taskHandle);
        } catch (Exception e) {
            log.error("任务添加失败", e);
        }
    }

    public static void execute(List<TaskHandle> tasks) {
        if (tasks == null || tasks.isEmpty()) {
            return;
        }
        for (TaskHandle taskHandle : tasks) {
            try {
                SICHEN_EXECUTOR.execute(taskHandle);
            } catch (Exception e) {
                log.error("任务添加失败", e);
            }
        }
    }

    public static void report(TaskHandle taskHandle) {
    }

}
