package com.inspect.job.domain;

import com.inspect.base.core.annotation.Excel;
import com.inspect.base.core.annotation.Excel.ColumnType;
import com.inspect.base.core.utils.StringUtils;
import com.inspect.base.core.web.domain.BaseEntity;
import com.inspect.job.util.CronUtils;
import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

import lombok.Setter;

@Setter
public class SysJob extends BaseEntity {
    private static final long serialVersionUID = 1L;
    @Excel(
            name = "任务序号",
            cellType = ColumnType.NUMERIC
    )
    private Long jobId;
    @Excel(
            name = "任务名称"
    )
    private String jobName;
    @Excel(
            name = "任务组名"
    )
    private String jobGroup;
    @Excel(
            name = "调用目标字符串"
    )
    private String invokeTarget;
    @Excel(
            name = "执行表达式 "
    )
    private String cronExpression;
    @Excel(
            name = "计划策略 ",
            readConverterExp = "0=默认,1=立即触发执行,2=触发一次执行,3=不触发立即执行"
    )
    private String misfirePolicy = "0";
    @Excel(
            name = "并发执行",
            readConverterExp = "0=允许,1=禁止"
    )
    private String concurrent;
    @Excel(
            name = "任务状态",
            readConverterExp = "0=正常,1=暂停"
    )
    private String status;

    public Long getJobId() {
        return this.jobId;
    }

    @NotBlank(
            message = "任务名称不能为空"
    )
    @Size(
            min = 0,
            max = 64,
            message = "任务名称不能超过64个字符"
    )
    public String getJobName() {
        return this.jobName;
    }

    public String getJobGroup() {
        return this.jobGroup;
    }

    @NotBlank(
            message = "调用目标字符串不能为空"
    )
    @Size(
            min = 0,
            max = 500,
            message = "调用目标字符串长度不能超过500个字符"
    )
    public String getInvokeTarget() {
        return this.invokeTarget;
    }

    @NotBlank(
            message = "Cron执行表达式不能为空"
    )
    @Size(
            min = 0,
            max = 255,
            message = "Cron执行表达式不能超过255个字符"
    )
    public String getCronExpression() {
        return this.cronExpression;
    }

    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss"
    )
    public Date getNextValidTime() {
        return StringUtils.isNotEmpty(this.cronExpression) ? CronUtils.getNextExecution(this.cronExpression) : null;
    }

    public String getMisfirePolicy() {
        return this.misfirePolicy;
    }

    public String getConcurrent() {
        return this.concurrent;
    }

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

    @Override
    public String toString() {
        return "SysJob{" +
                "jobId=" + jobId +
                ", jobName='" + jobName + '\'' +
                ", jobGroup='" + jobGroup + '\'' +
                ", invokeTarget='" + invokeTarget + '\'' +
                ", cronExpression='" + cronExpression + '\'' +
                ", misfirePolicy='" + misfirePolicy + '\'' +
                ", concurrent='" + concurrent + '\'' +
                ", status='" + status + '\'' +
                '}';
    }
}
