package com.ruoyi.ems.core;

import cn.hutool.core.map.MapUtil;
import com.ruoyi.quartz.domain.SysJob;
import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

public abstract class BaseTask {
    public static final Logger log = LoggerFactory.getLogger(BaseTask.class);

    @Autowired
    private Scheduler scheduler;

    public abstract void run(SysJob taskProperties);

    public void execute() {
        try {
            Map<String, Object> wrappedMap = scheduler.getCurrentlyExecutingJobs().get(0).getJobDetail().getJobDataMap()
                .getWrappedMap();
            SysJob taskProperties = MapUtil.get(wrappedMap, "TASK_PROPERTIES", SysJob.class);
            log.info("开始执行任务，任务名：{}", taskProperties);
            run(taskProperties);
        }
        catch (Exception e) {
            log.error("执行任务失败:{}",e.getMessage(), e);
        }
    }
}
