using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using CoreAgent.ProtocolClient.Models; using Microsoft.Extensions.Logging; namespace CoreAgent.ProtocolClient.Context.UeStateManager { /// /// UeIdentifierManager UE信息管理部分 /// public partial class UeIdentifierManager { #region UE信息管理方法 /// /// 合并两个UE的信息 /// 性能优化:减少不必要的对象创建 /// /// 第一个UE ID /// 第二个UE ID public void MergeUeInfo(int ueId1, int ueId2) { if (ueId1 <= 0 || ueId2 <= 0) { throw new UeInfoException(Math.Min(ueId1, ueId2), "UE ID必须大于0"); } try { lock (_lockObject) { UeList.TryGetValue(ueId1, out var ue1); UeList.TryGetValue(ueId2, out var ue2); if (ReferenceEquals(ue1, ue2)) { if (ue2 == null) { ue2 = CreateUeInfo(ueId2); UeList[ueId1] = ue2; } } else if (ue1 == null) { UeList[ueId1] = ue2; } else if (ue2 == null) { UeList[ueId2] = ue1; SyncPlmnFromExistingUe(ueId1, ueId2); } else { UeList[ueId2] = ue1; SyncPlmnFromExistingUe(ueId1, ueId2); } _logger?.LogDebug("合并UE信息: UE {UeId1} 和 UE {UeId2}", ueId1, ueId2); } } catch (Exception ex) { _logger?.LogError(ex, "合并UE信息失败: UE {UeId1} 和 UE {UeId2}", ueId1, ueId2); throw new UeInfoException(Math.Min(ueId1, ueId2), $"合并UE信息失败: {ex.Message}"); } } /// /// 创建新的UE信息 /// /// UE ID /// 创建的UE信息 private UEInfo CreateUeInfo(int ueId) { var ueInfo = new UEInfo { UeId = ueId, Caps = new ProtocolCaps { UeId = ueId } }; UeList[ueId] = ueInfo; _logger?.LogDebug("创建新的UE信息: UE {UeId}", ueId); return ueInfo; } /// /// 获取UE信息 /// /// UE ID /// UE信息,如果不存在则返回null public UEInfo? GetUeInfo(int ueId) { if (ueId <= 0) { _logger?.LogWarning("尝试获取无效UE ID的信息: {UeId}", ueId); return null; } lock (_lockObject) { return UeList.TryGetValue(ueId, out var ueInfo) ? ueInfo : null; } } /// /// 获取UE能力信息,如果不存在则创建 /// 同时根据ImsiToUeId映射表更新IMSI信息 /// /// UE ID /// UE能力信息 public ProtocolCaps GetUeCaps(int ueId) { if (ueId <= 0) { throw new UeInfoException(ueId, "UE ID必须大于0"); } lock (_lockObject) { if (!UeList.TryGetValue(ueId, out var ueInfo) || ueInfo == null) { ueInfo = CreateUeInfo(ueId); } if (ueInfo.Caps == null) { ueInfo.Caps = new ProtocolCaps { UeId = ueId }; } // 根据ImsiToUeId映射表更新IMSI信息 UpdateImsiFromMapping(ueId, ueInfo); return ueInfo.Caps; } } /// /// 根据ImsiToUeId映射表更新UE的IMSI信息 /// 性能优化:使用LINQ提高查找效率 /// /// UE ID /// UE信息对象 private void UpdateImsiFromMapping(int ueId, UEInfo ueInfo) { var imsiMapping = ImsiToUeId.FirstOrDefault(kvp => kvp.Value.Contains(ueId)); if (!string.IsNullOrEmpty(imsiMapping.Key)) { ueInfo.Imsi = imsiMapping.Key; _logger?.LogDebug("更新UE {UeId} 的IMSI信息: {Imsi}", ueId, imsiMapping.Key); } } /// /// 从现有UE ID同步IMSI信息到新的UE ID /// /// 现有UE ID /// 新的UE ID private void SyncImsiFromExistingUe(int existingUeId, int newUeId) { var imsiMapping = ImsiToUeId.FirstOrDefault(kvp => kvp.Value.Contains(existingUeId)); if (!string.IsNullOrEmpty(imsiMapping.Key)) { SetImsi(newUeId, imsiMapping.Key); _logger?.LogDebug("同步IMSI信息: 从UE {ExistingUeId} 到 UE {NewUeId}: {Imsi}", existingUeId, newUeId, imsiMapping.Key); } } /// /// 从现有UE ID同步PLMN信息到新的UE ID /// /// 现有UE ID /// 新的UE ID private void SyncPlmnFromExistingUe(int existingUeId, int newUeId) { var plmnMapping = PlmnToUeId.FirstOrDefault(kvp => kvp.Value.Contains(existingUeId)); if (!string.IsNullOrEmpty(plmnMapping.Key)) { SetPlmn(newUeId, plmnMapping.Key); _logger?.LogDebug("同步PLMN信息: 从UE {ExistingUeId} 到 UE {NewUeId}: {Plmn}", existingUeId, newUeId, plmnMapping.Key); } } /// /// 获取UeList中UEInfo.UeId与Caps.UeId组合的字典 /// /// 包含UE ID和Caps UeId组合的字典,键为UEInfo.UeId,值为Caps.UeId public Dictionary GetUeIdToCapsUeIdMapping() { lock (_lockObject) { var mapping = new Dictionary(); foreach (var kvp in UeList) { var ueId = kvp.Key; var ueInfo = kvp.Value; if (ueInfo?.Caps != null) { mapping[ueId] = ueInfo.Caps.UeId; } } return mapping; } } /// /// 批量获取UE信息 /// 性能优化:批量处理减少锁开销 /// /// UE ID列表 /// UE信息字典 public Dictionary GetUeInfoBatch(IEnumerable ueIds) { if (ueIds == null) { throw new ArgumentNullException(nameof(ueIds)); } var result = new Dictionary(); lock (_lockObject) { foreach (var ueId in ueIds) { if (UeList.TryGetValue(ueId, out var ueInfo)) { result[ueId] = ueInfo; } } } _logger?.LogDebug("批量获取UE信息,共获取 {Count} 个UE的信息", result.Count); return result; } #endregion } }