using Hotline.Share.Enums.BatchTask;
using SqlSugar;
using XF.Domain.Repository;

namespace Hotline.BatchTask;

[SugarIndex("index_apptaskitem_creationtime", nameof(ApptaskItem.CreationTime), OrderByType.Asc)]
public class ApptaskItem : CreationEntity
{
    /// <summary>
    /// 任务id
    /// </summary>
    public string ApptaskId { get; set; }

    /// <summary>
    /// 业务id（冗余）
    /// </summary>
    public string BusinessId { get; set; }

    /// <summary>
    /// 任务类型(冗余)
    /// </summary>
    public ETaskType TaskType { get; set; }

    /// <summary>
    /// 任务状态
    /// </summary>
    public ETaskStatus TaskStatus { get; set; }

    /// <summary>
    /// 优先级
    /// 由高到低（0-9）
    /// </summary>
    public int Priority { get; set; } = 9;

    /// <summary>
    /// 任务执行时间
    /// </summary>
    public DateTime? TaskStartTime { get; set; }
    public DateTime? TaskEndTime { get; set; }

    /// <summary>
    /// 参数
    /// </summary>
    [SugarColumn(ColumnDataType = "varchar(8000)")]
    public string? TaskParams { get; set; }

    /// <summary>
    /// 执行次数
    /// </summary>
    public int Tries { get; set; }

    /// <summary>
    /// 执行次数上限
    /// </summary>
    public int TryLimit { get; set; } = 1;

    public string? Message { get; set; }

    [SugarColumn(IsEnableUpdateVersionValidation = true)]
    public string Ver { get; set; } = Guid.NewGuid().ToString();

    [Navigate(NavigateType.OneToOne, nameof(ApptaskId))]
    public Apptask Apptask { get; set; }

    public void SetProcessing()
    {
        TaskStatus = ETaskStatus.Processing;
        TaskStartTime = DateTime.Now;
    }
}