package com.qqchen.deploy.backend.schedule.entity;

import com.qqchen.deploy.backend.framework.annotation.LogicDelete;
import com.qqchen.deploy.backend.framework.domain.Entity;
import com.qqchen.deploy.backend.schedule.enums.ScheduleJobStatusEnum;
import jakarta.persistence.Column;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;

/**
 * 定时任务
 * 
 * @author qichen
 */
@Data
@EqualsAndHashCode(callSuper = true)
@jakarta.persistence.Entity
@Table(name = "schedule_job")
@LogicDelete
public class ScheduleJob extends Entity<Long> {

    /**
     * 任务名称
     */
    @Column(name = "job_name", nullable = false, length = 100)
    private String jobName;

    /**
     * 任务描述
     */
    @Column(name = "job_description", length = 500)
    private String jobDescription;

    /**
     * 任务分类ID
     */
    @Column(name = "category_id", nullable = false)
    private Long categoryId;

    // ==================== 执行配置 ====================

    /**
     * Spring Bean名称
     */
    @Column(name = "bean_name", nullable = false, length = 100)
    private String beanName;

    /**
     * 方法名称
     */
    @Column(name = "method_name", nullable = false, length = 100)
    private String methodName;

    /**
     * 参数表单ID
     */
    @Column(name = "form_definition_id")
    private Long formDefinitionId;

    /**
     * 方法参数JSON
     */
    @Column(name = "method_params", columnDefinition = "TEXT")
    private String methodParams;

    // ==================== 调度配置 ====================

    /**
     * Cron表达式
     */
    @Column(name = "cron_expression", nullable = false, length = 100)
    private String cronExpression;

    /**
     * 任务状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    private ScheduleJobStatusEnum status;

    /**
     * 是否允许并发执行
     */
    @Column(name = "concurrent", nullable = false)
    private Boolean concurrent = false;

    // ==================== 统计信息 ====================

    /**
     * 上次执行时间
     */
    @Column(name = "last_execute_time")
    private LocalDateTime lastExecuteTime;

    /**
     * 下次执行时间
     */
    @Column(name = "next_execute_time")
    private LocalDateTime nextExecuteTime;

    /**
     * 执行次数
     */
    @Column(name = "execute_count", nullable = false)
    private Integer executeCount = 0;

    /**
     * 成功次数
     */
    @Column(name = "success_count", nullable = false)
    private Integer successCount = 0;

    /**
     * 失败次数
     */
    @Column(name = "fail_count", nullable = false)
    private Integer failCount = 0;

    // ==================== 高级配置 ====================

    /**
     * 超时时间（秒）
     */
    @Column(name = "timeout_seconds")
    private Integer timeoutSeconds;

    /**
     * 失败重试次数
     */
    @Column(name = "retry_count", nullable = false)
    private Integer retryCount = 0;

    /**
     * 告警邮箱（多个用逗号分隔）
     */
    @Column(name = "alert_email", length = 500)
    private String alertEmail;
}

