using Newtonsoft.Json; namespace CoreAgent.Domain.Models.Protocol; /// /// 用户网络身份信息 /// 用于存储移动网络用户的基本身份和位置信息 /// 遵循DDD(领域驱动设计)原则,作为领域模型的一部分 /// public class UserNetworkIdentity { /// /// 公共陆地移动网络标识 /// [JsonProperty("plmn")] public string? Plmn { get; set; } /// /// 旧的临时移动用户标识 /// [JsonProperty("oldTmsi")] public string? OldTmsi { get; set; } /// /// 临时移动用户标识 /// [JsonProperty("tmsi")] public string? Tmsi { get; set; } /// /// 国际移动用户标识 /// [JsonProperty("imsi")] public string? Imsi { get; set; } /// /// 小区ID /// [JsonProperty("cellId")] public int? CellId { get; set; } /// /// 用户设备ID /// [JsonProperty("ueId")] public int? UeId { get; set; } /// /// 初始化用户网络身份信息的新实例 /// public UserNetworkIdentity() { } /// /// 初始化用户网络身份信息的新实例 /// /// 公共陆地移动网络标识 /// 旧的临时移动用户标识 /// 临时移动用户标识 /// 国际移动用户标识 /// 小区ID /// 用户设备ID public UserNetworkIdentity( string? plmn = null, string? oldTmsi = null, string? tmsi = null, string? imsi = null, int? cellId = null, int? ueId = null) { Plmn = plmn; OldTmsi = oldTmsi; Tmsi = tmsi; Imsi = imsi; CellId = cellId; UeId = ueId; } /// /// 检查是否包含有效的用户身份信息 /// /// 是否包含有效信息 public bool HasValidIdentity() { return !string.IsNullOrWhiteSpace(Imsi) || !string.IsNullOrWhiteSpace(Tmsi) || UeId.HasValue; } /// /// 检查是否包含位置信息 /// /// 是否包含位置信息 public bool HasLocationInfo() { return !string.IsNullOrWhiteSpace(Plmn) || CellId.HasValue; } /// /// 获取用户身份摘要信息 /// /// 摘要信息 public string GetIdentitySummary() { var parts = new List(); if (!string.IsNullOrWhiteSpace(Imsi)) parts.Add($"IMSI:{Imsi}"); if (!string.IsNullOrWhiteSpace(Tmsi)) parts.Add($"TMSI:{Tmsi}"); if (UeId.HasValue) parts.Add($"UE:{UeId}"); if (!string.IsNullOrWhiteSpace(Plmn)) parts.Add($"PLMN:{Plmn}"); if (CellId.HasValue) parts.Add($"Cell:{CellId}"); return parts.Count > 0 ? string.Join(" | ", parts) : "No Identity Info"; } /// /// 检查TMSI是否发生变化 /// /// TMSI是否发生变化 public bool HasTmsiChanged() { return !string.IsNullOrWhiteSpace(OldTmsi) && !string.IsNullOrWhiteSpace(Tmsi) && !OldTmsi.Equals(Tmsi, StringComparison.OrdinalIgnoreCase); } /// /// 获取TMSI变化信息 /// /// TMSI变化信息 public string GetTmsiChangeInfo() { if (HasTmsiChanged()) { return $"TMSI Changed: {OldTmsi} -> {Tmsi}"; } return "TMSI Unchanged"; } /// /// 创建用户网络身份信息的副本 /// /// 新的用户网络身份信息 public UserNetworkIdentity Copy() { return new UserNetworkIdentity { Plmn = Plmn, OldTmsi = OldTmsi, Tmsi = Tmsi, Imsi = Imsi, CellId = CellId, UeId = UeId }; } /// /// 更新TMSI信息 /// /// 新的TMSI public void UpdateTmsi(string? newTmsi) { if (!string.IsNullOrWhiteSpace(Tmsi)) { OldTmsi = Tmsi; } Tmsi = newTmsi; } /// /// 转换为JSON字符串 /// /// JSON字符串 public string ToJson() { return JsonConvert.SerializeObject(this, Formatting.Indented); } /// /// 从JSON字符串创建用户网络身份信息 /// /// JSON字符串 /// 用户网络身份信息 public static UserNetworkIdentity FromJson(string json) { return JsonConvert.DeserializeObject(json) ?? new UserNetworkIdentity(); } /// /// 合并另一个用户网络身份信息 /// /// 另一个用户网络身份信息 /// 合并后的用户网络身份信息 public UserNetworkIdentity Merge(UserNetworkIdentity other) { if (other == null) return this; return new UserNetworkIdentity { Plmn = Plmn ?? other.Plmn, OldTmsi = OldTmsi ?? other.OldTmsi, Tmsi = Tmsi ?? other.Tmsi, Imsi = Imsi ?? other.Imsi, CellId = CellId ?? other.CellId, UeId = UeId ?? other.UeId }; } }