package org.nl.acs.auto.run;

import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public abstract class AbstractAutoRunnable implements Runnable {
    private ThreadStatusEnum status;
    private Date startTime;
    private Date stopTime;
    private String stopMessage;
    private ThreadUsedStatusEnum usedStatus;
    @Override
    public void run() {
        this.setStatus(ThreadStatusEnum.run);
        this.setStartTime(new Date());
        this.setStopMessage("");
        String true_clear = "执行完毕";

        try {
            this.before();
            //子类该方法是个死循环
            this.autoRun();
            this.setStopMessage(true_clear);
        } catch (Throwable arg5) {
            log.warn("", arg5);
            System.out.println(arg5.getMessage());
            // this.setStopMessage(ExceptionUtlEx.getSimpleTrace(arg5));
        } finally {
            this.setStopTime(new Date());
            this.setStatus(ThreadStatusEnum.stop);
            this.after();
        }

    }

    public void before() throws Exception {
    }

    public void after() {
    }

    public void stop() {
        this.after();
    }

    public Boolean getForbidStop() {
        return Boolean.valueOf(false);
    }

    public Boolean getForbidDisable() {
        return Boolean.valueOf(false);
    }

    public abstract String getCode();

    public abstract String getName();

    public String getDescription() {
        return this.getName();
    }

    public abstract void autoRun() throws Exception;

    public ThreadStatusEnum getStatus() {
        return this.status;
    }

    public void setStatus(ThreadStatusEnum status) {
        this.status = status;
    }

    public Date getStartTime() {
        return this.startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getStopTime() {
        return this.stopTime;
    }

    public void setStopTime(Date stopTime) {
        this.stopTime = stopTime;
    }

    public String getStopMessage() {
        return this.stopMessage;
    }

    public void setStopMessage(String stopMessage) {
        this.stopMessage = stopMessage;
    }

    public ThreadUsedStatusEnum getUsedStatus() {
        return this.usedStatus;
    }

    public void setUsedStatus(ThreadUsedStatusEnum usedStatus) {
        this.usedStatus = usedStatus;
    }
}
