package com.liangjian.dataplatform.member.entity.po.primary;

import com.liangjian.dataplatform.member.entity.po.BasicEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.persistence.Column;
import javax.persistence.Entity;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: 黄子奇
 * @Date: 2019/9/9 11:21
 *
 * 用于存储描述作业的信息
 */
@Entity(name = "job_info")
@ApiModel("任务描述")
public class JobInfo extends BasicEntity implements Serializable {
    private static final long serialVersionUID = -287697731819023150L;

    /** 执行一次 */
    public static final int ONCE = 0;
    /** 计划调度，每x秒 */
    public static final int SCHEDULE_SEC = 1;
    /** 计划调度，每x分 */
    public static final int SCHEDULE_MIL = 2;
    /** 计划调度，每x时 */
    public static final int SCHEDULE_HOU = 3;
    /** 计划调度，每x天 */
    public static final int SCHEDULE_DAY = 4;
    /** 计划调度，每x周 */
    public static final int SCHEDULE_WEK = 5;
    /** 计划调度，每x月 */
    public static final int SCHEDULE_MON = 6;
    /** 重复执行x次 */
    public static final int REPEAT = 7;

    @ApiModelProperty("名称")
    private String name;

    @Column(name = "description")
    @ApiModelProperty("描述/用途")
    private String desc;

    @Column(name = "start_time")
    @ApiModelProperty("执行时间/开始时间")
    private Date startTime;

    @ApiModelProperty(value = "是否重复执行", notes = "选择了重复执行的，按照调度规则、重复次数执行")
    private boolean repeats;

    @Column(name = "repeat_type")
    @ApiModelProperty(value = "重复/调度类型，按照：每隔{单位}{单位描述}执行1次, 如：每隔1天执行1次，每隔5分钟执行1次")
    private Integer repeatType; //重复（调度）执行的类型

    @Column(name = "repeat_rule")
    @ApiModelProperty(value = "调度执行的单位")
    private Integer repeatRule; //重复执行的规则，详细在静态变量中有描述

    @Column(name = "active")
    @ApiModelProperty("是否已启用")
    private boolean active;

    public String getInfoStr() {
        String active = this.getActive() ? "已开启: " : "未开启: ";
        Date startDate = this.getStartTime();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        String startDateStr = sf.format(startDate);

        String suffix = "";
        String suffixBone = "每%s%s1次";
        Integer type = this.getRepeatType();
        String x = this.getRepeatRule() == 1 ? "": this.getRepeatRule()+"";
        if(!this.isRepeats()) {
            suffix = "仅一次";
        } else {
            switch (type) {
                case JobInfo.SCHEDULE_SEC: suffix = String.format(suffixBone,x,"秒");break;
                case JobInfo.SCHEDULE_MIL: suffix = String.format(suffixBone,x,"分钟");break;
                case JobInfo.SCHEDULE_HOU: suffix = String.format(suffixBone,x,"小时");break;
                case JobInfo.SCHEDULE_DAY: suffix = String.format(suffixBone,x,"天");break;
                case JobInfo.SCHEDULE_WEK: suffix = String.format(suffixBone,x,"周");break;
                case JobInfo.SCHEDULE_MON: suffix = String.format(suffixBone,x,"月");break;
                case JobInfo.REPEAT: suffix = "仅重复"+x+"次";break;
                default: suffix = "未知重复类型";
            }
        }
        return active + startDateStr + suffix;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Date getStartTime() {
        return startTime;
    }

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

    public boolean isRepeats() {
        return repeats;
    }

    public void setRepeats(boolean repeat) {
        this.repeats = repeat;
    }

    public Integer getRepeatType() {
        return repeatType;
    }

    public void setRepeatType(Integer repeatType) {
        this.repeatType = repeatType;
    }

    public Integer getRepeatRule() {
        return repeatRule;
    }

    public void setRepeatRule(Integer repeatRule) {
        this.repeatRule = repeatRule;
    }

    public boolean getActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }
}
