using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace IStation.Untity { /// /// 树节点标识辅助类 /// public class TreeParentIdsHelper { /// /// 分隔符 /// public const string Separator = ","; /// /// To 列表 /// public static List ToList(string parentIds) { if (string.IsNullOrEmpty(parentIds)) return new List(); var list = parentIds.Split(new string[] { Separator }, StringSplitOptions.RemoveEmptyEntries); return list.Select(x => long.Parse(x)).ToList(); } /// /// To 字符串 /// public static string ToString(List parentIds) { if (parentIds == null || parentIds.Count < 1) return string.Empty; var sb = new StringBuilder(); sb.Append(Separator); foreach (var item in parentIds) { sb.Append(item.ToString()); sb.Append(Separator); } return sb.ToString(); } /// /// 获取子节点的ParentIds /// public static string GetChildParentIds(long CurrentID, string CurrentParentIds) { if (string.IsNullOrEmpty(CurrentParentIds)) CurrentParentIds = Separator; return string.Format("{0}{1}{2}", CurrentParentIds, CurrentID, Separator); } /// /// 获取父节点ID /// public static long GetLastParentID(string ParentIds) { if (string.IsNullOrEmpty(ParentIds)) return 0; var ids = ParentIds.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); return Convert.ToInt64(ids.Last()); } /// /// 获取子节点的ParentIds /// public static List GetChildParentIds(long CurrentID, List CurrentParentIds) { var parentIds = CurrentParentIds?.ToList(); if (parentIds == null) parentIds = new List(); parentIds.Add(CurrentID); return parentIds; } /// /// 获取父节点ID /// public static long GetLastParentID(List ParentIds) { if (ParentIds == null || ParentIds.Count < 1) return 0; return ParentIds.Last(); } } }