package com.erp.task.entity;

import com.erp.task.enums.TaskStatus;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 定时任务实体
 */
@Entity
@Table(name = "scheduled_task", indexes = {
    @Index(name = "idx_task_name", columnList = "taskName"),
    @Index(name = "idx_task_group", columnList = "taskGroup"),
    @Index(name = "idx_task_status", columnList = "status"),
    @Index(name = "idx_cron", columnList = "cronExpression")
})
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = {"id"})
public class ScheduledTask {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 任务名称
     */
    @Column(nullable = false, length = 100)
    private String taskName;

    /**
     * 任务描述
     */
    @Column(length = 500)
    private String description;

    /**
     * 任务分组
     */
    @Column(nullable = false, length = 50)
    private String taskGroup;

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

    /**
     * 任务类名（Spring Bean名称或完全限定类名）
     */
    @Column(nullable = false, length = 500)
    private String taskClass;

    /**
     * 任务方法名
     */
    @Column(length = 100)
    private String methodName;

    /**
     * 任务参数（JSON格式）
     */
    @Column(columnDefinition = "TEXT")
    private String taskParams;

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

    /**
     * 是否并行执行
     */
    @Column(nullable = false)
    private Boolean concurrent = true;

    /**
     * 是否同步执行
     */
    @Column(nullable = false)
    private Boolean sync = false;

    /**
     * 任务类型：BEAN、CLASS、XXL_JOB
     */
    @Column(nullable = false, length = 20)
    private String taskType;

    /**
     * XXL-Job任务ID（兼容模式）
     */
    private Long xxlJobId;

    /**
     * 最大重试次数
     */
    @Column(nullable = false)
    private Integer maxRetries = 3;

    /**
     * 重试间隔（秒）
     */
    @Column(nullable = false)
    private Integer retryInterval = 60;

    /**
     * 超时时间（秒）
     */
    private Integer timeout;

    /**
     * 告警邮箱（多个用逗号分隔）
     */
    @Column(length = 500)
    private String alertEmails;

    /**
     * 告警手机号（多个用逗号分隔）
     */
    @Column(length = 200)
    private String alertPhones;

    /**
     * 是否启用告警
     */
    @Column(nullable = false)
    private Boolean alertEnabled = false;

    /**
     * Misfire策略：DO_NOTHING、FIRE_NOW、FIRE_ONCE
     */
    @Column(length = 20)
    private String misfirePolicy = "DO_NOTHING";

    /**
     * 下次执行时间
     */
    private LocalDateTime nextFireTime;

    /**
     * 上次执行时间
     */
    private LocalDateTime prevFireTime;

    /**
     * 执行次数
     */
    @Column(nullable = false)
    private Long executeCount = 0L;

    /**
     * 负责人
     */
    @Column(length = 50)
    private String owner;

    /**
     * 备注
     */
    @Column(length = 500)
    private String remark;

    /**
     * 租户ID
     */
    @Column(length = 50)
    private String tenantId;

    @CreatedDate
    private LocalDateTime createdAt;

    @CreatedBy
    @Column(length = 50)
    private String createdBy;

    @LastModifiedDate
    private LocalDateTime updatedAt;

    @LastModifiedBy
    @Column(length = 50)
    private String updatedBy;
}
