VdaSegmentedPathCache.cs 4.58 KB
using Rcs.Domain.Enums;

namespace Rcs.Application.Services.PathFind.Models;

/// <summary>
/// VDA5050分段路径缓存
/// @author zzy
/// 2026-02-05 更新:支持两层切割结构
/// 2026-02-06 更新:支持网络动作执行状态跟踪
/// </summary>
public class VdaSegmentedPathCache
{
    public Guid TaskId { get; set; }
    public string TaskCode { get; set; } = string.Empty;
    public Guid RobotId { get; set; }
    public Guid MapId { get; set; }
    public string MapCode { get; set; } = string.Empty;
    public DateTime CreatedAt { get; set; }
    /// <summary>
    /// 当前路口段索引(第一层)
    /// </summary>
    public int CurrentJunctionIndex { get; set; }
    /// <summary>
    /// 当前资源段索引(第二层)
    /// </summary>
    public int CurrentResourceIndex { get; set; }
    /// <summary>
    /// 路口切割后的段落列表
    /// </summary>
    public List<VdaJunctionSegmentCache> JunctionSegments { get; set; } = new();

    /// <summary>
    /// 当前活跃的网络动作上下文ID(用于跟踪正在执行的网络动作)
    /// @author zzy
    /// </summary>
    public string? ActiveNetActionContextId { get; set; }

    /// <summary>
    /// 网络动作执行状态
    /// @author zzy
    /// </summary>
    public NetActionExecutionStatus? NetActionStatus { get; set; }

    /// <summary>
    /// 是否正在等待网络动作完成
    /// @author zzy
    /// </summary>
    public bool IsWaitingForNetAction =>
        NetActionStatus == NetActionExecutionStatus.Executing ||
        NetActionStatus == NetActionExecutionStatus.WaitingAsyncResponse;
}

/// <summary>
/// 路口切割段缓存(第一层)
/// @author zzy
/// </summary>
public class VdaJunctionSegmentCache
{
    /// <summary>
    /// 资源/模板切割后的子段列表
    /// </summary>
    public List<VdaSegmentCacheItem> ResourceSegments { get; set; } = new();
}

/// <summary>
/// VDA5050分段缓存项(第二层)
/// @author zzy
/// 2026-02-06 更新:添加网络动作相关字段
/// </summary>
public class VdaSegmentCacheItem
{
    public bool IsSent { get; set; } = false;
    public List<PathSegmentWithCode> Segments { get; set; } = new();

    /// <summary>
    /// 终点网络动作上下文ID列表(用于关联网络动作执行状态)
    /// @author zzy
    /// </summary>
    public List<string> EndNodeNetActionContextIds { get; set; } = new();

    /// <summary>
    /// 起点网络动作上下文ID列表(用于关联网络动作执行状态)
    /// @author zzy
    /// </summary>
    public List<string> StartNodeNetActionContextIds { get; set; } = new();

    /// <summary>
    /// 是否已执行终点网络动作
    /// @author zzy
    /// </summary>
    public bool IsEndNodeNetActionExecuted { get; set; } = false;

    /// <summary>
    /// 是否已执行起点网络动作
    /// @author zzy
    /// </summary>
    public bool IsStartNodeNetActionExecuted { get; set; } = false;

    /// <summary>
    /// 获取该段的终点网络动作ID列表(仅返回动作申请类型的网络动作)
    /// @author zzy
    /// 2026-02-06 更新:从字典Keys获取,过滤只返回ActionType为Apply的网络动作
    /// </summary>
    public List<Guid> GetEndNodeNetActionIds()
    {
        if (Segments.Count == 0)
        {
            return new List<Guid>();
        }

        var lastSegment = Segments.Last();
        if (lastSegment.EndNodeNetActionTypes == null || lastSegment.EndNodeNetActionTypes.Count == 0)
        {
            return new List<Guid>();
        }

        // 从字典Keys中获取ActionType为Apply的网络动作ID
        return lastSegment.EndNodeNetActionTypes
            .Where(kv => kv.Value == ActionType.Apply)
            .Select(kv => kv.Key)
            .ToList();
    }

    /// <summary>
    /// 获取该段的起点网络动作ID列表(仅返回动作申请类型的网络动作)
    /// @author zzy
    /// 2026-02-06 更新:从字典Keys获取,过滤只返回ActionType为Apply的网络动作
    /// </summary>
    public List<Guid> GetStartNodeNetActionIds()
    {
        if (Segments.Count == 0)
        {
            return new List<Guid>();
        }

        var firstSegment = Segments.First();
        if (firstSegment.StartNodeNetActionTypes == null || firstSegment.StartNodeNetActionTypes.Count == 0)
        {
            return new List<Guid>();
        }

        // 从字典Keys中获取ActionType为Apply的网络动作ID
        return firstSegment.StartNodeNetActionTypes
            .Where(kv => kv.Value == ActionType.Apply)
            .Select(kv => kv.Key)
            .ToList();
    }
}