using Hotline.FlowEngine.Definitions;
using Hotline.Orders;
using Hotline.Share.Dtos.Order.Detail;
using Hotline.Share.Enums.FlowEngine;
using SqlSugar;

namespace Hotline.FlowEngine.Workflows;

/// <summary>
/// 流转记录
/// </summary>
[SugarIndex("index_trace_workflowId", nameof(WorkflowTrace.WorkflowId), OrderByType.Desc)]
[SugarIndex("index_trace_externalId", nameof(WorkflowTrace.ExternalId), OrderByType.Desc)]
public class WorkflowTrace : StepBasicEntity
{
    /// <summary>
    /// 节点Id
    /// </summary>
    public string? StepId { get; set; }

    /// <summary>
    /// 会签从属关系
    /// </summary>
    public string? ParentId { get; set; }

    /// <summary>
    /// 流转记录状态
    /// </summary>
    public EWorkflowTraceType? TraceType { get; set; }

    /// <summary>
    /// 派单组办理次数
    /// </summary>
    [SugarColumn(DefaultValue = "0")]
    public int SendHandleTimes { get; set; }

    /// <summary>
    /// 快照对应节点状态
    /// </summary>
    [SugarColumn(DefaultValue = "0")]
    public EWorkflowTraceState TraceState { get; set; }

    /// <summary>
    /// 快照类型
    /// </summary>
    [SugarColumn(DefaultValue = "0")]
    public ETraceStyle TraceStyle { get; set; } = ETraceStyle.Flow;

    public string? OrderPublishId { get; set; }
    
    [Navigate(NavigateType.OneToOne, nameof(OrderPublishId))]
    public OrderPublish OrderPublish { get; set; }

    public string? OrderVisitId { get; set; }

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

    /// <summary>
    /// 会签流转记录
    /// </summary>
    [SugarColumn(IsIgnore = true)]
    public List<WorkflowTrace> Traces { get; set; }



}

/// <summary>
/// 快照对应节点状态
/// </summary>
public enum EWorkflowTraceState
{
    /// <summary>
    /// 正常办理
    /// </summary>
    Normal = 0,

    /// <summary>
    /// 对应节点因退回被删除
    /// </summary>
    StepRemoveByPrevious = 10,

    /// <summary>
    /// 对应节点因特提被删除(工单未归档)
    /// </summary>
    StepRemoveByRecall = 20,

    /// <summary>
    /// 对应节点因特提被删除(工单已归档)
    /// </summary>
    StepRemoveByRecallWhenFiled = 21,
}