package com.cyksj.xxljob;


import cn.hutool.core.net.URLEncoder;
import com.cyksj.common.exception.BusinessRuntimeException;
import lombok.Data;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.Charset;

/**
 * @ClassName XxlJobInfo
 * @Description TODO
 * @Author Huang yuanzhi
 * @Date 2023/6/12 16:11
 * @Version 1.0.0
 **/
@Data
public class XxlJobInfo {

	public XxlJobInfo() {
	}

	public XxlJobInfo(int id, String jobDesc, String jobCron, int jobGroup, String author, String alarmEmail, String executorHandler, String executorParam) {
		this.id = id;
		this.jobGroup = jobGroup;
		this.jobDesc = jobDesc;
		this.jobCron = jobCron;
		this.author = author;
		this.alarmEmail = alarmEmail;
		this.executorHandler = executorHandler;
		this.executorParam = executorParam;
		this.triggerStatus = 1;
	}

	public XxlJobInfo(int id, String executorParam) {
		this.id = id;
		this.executorParam = executorParam;
	}

	private int id;
	/**
	 * 执行器主键ID
	 */
	private int jobGroup = 5;
	/**
	 * 备注
	 */
	private String jobDesc;
	/**
	 * 表达式
	 */
	private String jobCron;

	private String glueType = "BEAN";
	/**
	 * 负责人
	 */
	private String author;
	/**
	 * 报警邮件
	 */
	private String alarmEmail;

	/**
	 * 执行器路由策略
	 */
	private String executorRouteStrategy = "FIRST";
	/**
	 * 执行器，任务Handler名称
	 */
	private String executorHandler;
	/**
	 * 执行器，任务参数
	 */
	private String executorParam;
	/**
	 * 阻塞处理策略
	 */
	private String executorBlockStrategy = "SERIAL_EXECUTION";
	/**
	 * 任务执行超时时间，单位秒
	 */
	private int executorTimeout;

	private String glueRemark = "GLUE代码初始化";
	/**
	 * 失败重试次数
	 */
	private int executorFailRetryCount;

	/**
	 * 子任务ID，多个逗号分隔
	 */
	private String childJobId;
	/**
	 * 调度状态：0-停止，1-运行
	 */
	private int triggerStatus;
	/**
	 * 上次调度时间
	 */
	private long triggerLastTime;
	/**
	 * 下次调度时间
	 */
	private long triggerNextTime;

	//银河执行器 id
	private static int jobGroups = 10;


	/**
	 * 创建执行器
	 *
	 * @param author
	 * @param jobDesc         任务秒速
	 * @param corn            表达式
	 * @param executorHandler executorHandler
	 * @param executorParam   参数
	 * @return
	 */
	public static XxlJobInfo getInstance(AUTHOR author, String jobDesc, String corn, int jopGroup, String executorHandler, String executorParam) {
		return new XxlJobInfo(0, jobDesc, URLEncoder.createQuery().encode(corn, Charset.defaultCharset()), jopGroup, author.getName(), author.email, executorHandler, executorParam);
	}

	public static XxlJobInfo getUpdateInstance(int id, AUTHOR author, String jobDesc, String corn, int jopGroup, String executorHandler, String executorParam) {
		return new XxlJobInfo(id, jobDesc, URLEncoder.createQuery().encode(corn, Charset.defaultCharset()), jopGroup, author.getName(), author.email, executorHandler, executorParam);
	}

	public static XxlJobInfo getTaskInstance(int id, String corn, String executorParam) {
		AUTHOR author = AUTHOR.Zou;
		if (StringUtils.isBlank(corn)) {
			throw BusinessRuntimeException.getInstance("任务运行周期不能为空");
		}
		return new XxlJobInfo(id, "周期群发任务", URLEncoder.createQuery().encode(corn, Charset.defaultCharset()), jobGroups, author.getName(), author.email, "yh_corp_msg_bulk_task_cycle_job", executorParam);
	}

	public static XxlJobInfo getExecuteTaskInstance(int id, String executorParam) {
		return new XxlJobInfo(id, executorParam);
	}

	@Getter
	public enum AUTHOR {

		Zou("zou", "1091496505@qq.com");

		AUTHOR(String name, String email) {
			this.name = name;
			this.email = email;
		}

		private String name;

		private String email;
	}

	@Override
	public String toString() {
		return
				"?id=" + id +
						"&jobDesc=" + jobDesc +
						"&jobCron=" + jobCron +
						"&jobGroup=" + jobGroup +
						"&glueType=" + glueType +
						"&author=" + author +
						"&alarmEmail=" + alarmEmail +
						"&executorRouteStrategy=" + executorRouteStrategy +
						"&executorHandler=" + executorHandler +
						"&executorParam=" + executorParam +
						"&executorBlockStrategy=" + executorBlockStrategy +
						"&executorTimeout=" + executorTimeout +
						"&glueRemark=" + glueRemark +
						"&executorFailRetryCount=" + executorFailRetryCount +
						"&triggerStatus=" + triggerStatus +
						"&triggerLastTime=" + triggerLastTime +
						"&triggerNextTime=" + triggerNextTime;
	}
}