package org.jeecg.modules.deliver.cmos.job.service.impl;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.common.util.ThreadExecutorsUtil;
import org.jeecg.modules.deliver.cmos.job.CmosTask;
import org.jeecg.modules.deliver.cmos.job.service.CmosTaskService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;

@Slf4j
@RequiredArgsConstructor
@Service
public class CmosTaskServiceImpl implements CmosTaskService {

    final List<CmosTask> taskList;

    @Override
    public void execute(final String type) {
        log.info("任务调度：{}", type);
        Optional<CmosTask> optional = this.taskList.stream()
                .filter(it -> StrUtil.equals(it.getType(), type))
                .findFirst();

        if (optional.isPresent()) {
            final CmosTask task = optional.get();

            CompletableFuture.runAsync(() -> {
                try {
                    task.execute();
                }catch (Exception e) {
                    log.error(e.getMessage());
                }
            }, ThreadExecutorsUtil.executor);
        } else{
            log.warn("未实现的调度任务：{}", type);
        }
    }
}
