package com.huashe.park.common.job.schedule;

import org.slf4j.Logger;
import org.springframework.scheduling.support.CronTrigger;

public abstract class TriggerTaskSupport implements ITriggerTask {
    private static final Logger log = org.slf4j.LoggerFactory.getLogger(TriggerTaskSupport.class);

    public CronTrigger trigger;

    /**
     * 获取触发器，一般是 CronTrigger
     *
     * @return
     */
    @Override
    public CronTrigger getTrigger() {
        return trigger;
    }

    /**
     * 接口 动态修改 定时任务的表达式
     *
     * @param cron
     */
    @Override
    public CronTrigger setTrigger(String cron) {
        this.trigger = new CronTrigger("0 0/10 * * * ?");
        return this.trigger;
    }

    @Override
    public String type() {
        return this.getClass().getSimpleName().toLowerCase();
    }

    public abstract String jobName();

    /**
     * 获取 run 方法
     *
     * @return
     */
    @Override
    public Runnable getTask() {
        return () -> {
            try {
                log.info(">>>>>>>>>>>>>{}启动<<<<<<<<<<<<<<<<< ", jobName());
                scheduledJob();
                log.info(">>>>>>>>>>>>>{}结束<<<<<<<<<<<<<<<<< ", jobName());
            }
            catch (Exception e) {
                log.info("定时任务{}执行异常", jobName(), e);
            }
        };
    }

    @Override
    public String toString() {
        return "TriggerTask{" + "type=" + type() + ", task=" + getTask() + "cronTrigger=" + getTrigger().getExpression()
            + '}';
    }

}
