package com.erp.task.entity;

import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 任务执行记录实体
 */
@Entity
@Table(name = "task_execution_log", indexes = {
    @Index(name = "idx_log_task_id", columnList = "taskId"),
    @Index(name = "idx_log_status", columnList = "status"),
    @Index(name = "idx_log_start_time", columnList = "startTime"),
    @Index(name = "idx_log_trigger_time", columnList = "triggerTime")
})
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(of = {"id"})
public class TaskExecutionLog {

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

    /**
     * 任务ID
     */
    @Column(nullable = false)
    private Long taskId;

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

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

    /**
     * 执行批次号
     */
    @Column(nullable = false, length = 64)
    private String batchNo;

    /**
     * 触发时间
     */
    @Column(nullable = false)
    private LocalDateTime triggerTime;

    /**
     * 开始时间
     */
    private LocalDateTime startTime;

    /**
     * 结束时间
     */
    private LocalDateTime endTime;

    /**
     * 执行状态：RUNNING、SUCCESS、FAILED、RETRYING、TIMEOUT、CANCELLED
     */
    @Column(nullable = false, length = 20)
    private String status;

    /**
     * 执行耗时（毫秒）
     */
    private Long duration;

    /**
     * 执行节点（IP）
     */
    @Column(length = 50)
    private String executorNode;

    /**
     * 执行结果/错误信息
     */
    @Column(columnDefinition = "TEXT")
    private String result;

    /**
     * 错误堆栈
     */
    @Column(columnDefinition = "TEXT")
    private String errorTrace;

    /**
     * 重试次数
     */
    @Column(nullable = false)
    private Integer retryCount = 0;

    /**
     * 是否是重试执行
     */
    private Boolean isRetry = false;

    /**
     * 任务参数
     */
    @Column(columnDefinition = "TEXT")
    private String taskParams;

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

    @CreatedDate
    private LocalDateTime createdAt;
}
