diff --git a/labs/NestedSetModelTest/.gitignore b/labs/NestedSetModelTest/.gitignore new file mode 100644 index 00000000..988994ff --- /dev/null +++ b/labs/NestedSetModelTest/.gitignore @@ -0,0 +1,16 @@ +*.bak +*.suo +*.db +*.user +.vs +obj +Obj +bin +Bin +debug +Debug +release +Release +Logs +logs +node_modules \ No newline at end of file diff --git a/labs/NestedSetModelTest/BaseTreeEntity.cs b/labs/NestedSetModelTest/BaseTreeEntity.cs new file mode 100644 index 00000000..07f64a7f --- /dev/null +++ b/labs/NestedSetModelTest/BaseTreeEntity.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; + +namespace NestedSetModelTest +{ + public class EntityBase + { + public TKey Id { get; set; } + public string IsDeleted { get; set; } + + public override string ToString() + { + return $"{ GetType().FullName}[{Id}]"; + } + } + + public abstract class BaseEntity : EntityBase + { + public BaseEntity() + { + this.Id = Guid.NewGuid(); + } + } + + public abstract class BaseTreeEntity : BaseEntity where T : BaseTreeEntity + { + public int DisplayOrder { get; set; } + public int Left { get; set; } + public int Right { get; set; } + public Guid? ParentId { get; set; } + public T Parent { get; set; } +#pragma warning disable CA2227 // 集合属性应为只读 + public List Children { get; set; } = new List(); +#pragma warning restore CA2227 // 集合属性应为只读 + + public List GetPath() + { + var list = new List(); + var item = this as T; + while (item != null) + { + list.Add(item); + if (item.Parent == null || item.Parent.Id == this.Id) + { + break; + } + item = item.Parent; + } + list.Reverse(); + return list; + } + } + + public class Node : BaseTreeEntity + { + public string Name { get; set; } + public string Number { get; set; } + } +} \ No newline at end of file diff --git a/labs/NestedSetModelTest/NestedSetModelTest.csproj b/labs/NestedSetModelTest/NestedSetModelTest.csproj new file mode 100644 index 00000000..c73e0d16 --- /dev/null +++ b/labs/NestedSetModelTest/NestedSetModelTest.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp3.1 + + + diff --git a/labs/NestedSetModelTest/NestedSetModelTest.sln b/labs/NestedSetModelTest/NestedSetModelTest.sln new file mode 100644 index 00000000..9c55ae65 --- /dev/null +++ b/labs/NestedSetModelTest/NestedSetModelTest.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30717.126 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NestedSetModelTest", "NestedSetModelTest.csproj", "{37D9722F-9B16-481C-AEC0-35553CD13575}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {37D9722F-9B16-481C-AEC0-35553CD13575}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {37D9722F-9B16-481C-AEC0-35553CD13575}.Debug|Any CPU.Build.0 = Debug|Any CPU + {37D9722F-9B16-481C-AEC0-35553CD13575}.Release|Any CPU.ActiveCfg = Release|Any CPU + {37D9722F-9B16-481C-AEC0-35553CD13575}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9C3B8285-8771-4F47-89D3-A4B9BD41B469} + EndGlobalSection +EndGlobal diff --git a/labs/NestedSetModelTest/Program.cs b/labs/NestedSetModelTest/Program.cs new file mode 100644 index 00000000..18be6dc7 --- /dev/null +++ b/labs/NestedSetModelTest/Program.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace NestedSetModelTest +{ + internal class Program + { + private static List list = new List(); + + private static void Main(string[] args) + { + var root = CreateTree(); + Add(root); + var i = 1; + foreach (var item in list) + { + item.Left = i++; + } + list.Reverse(); + foreach (var item in list) + { + item.Right = i++; + } + list.Reverse(); + foreach (var item in list) + { + Console.WriteLine($"{item.Name}:{item.Number},{item.Left}-{item.Right}"); + } + } + + private static void Add(Node node) + { + list.Add(node); + if (node.Children.Any()) + { + foreach (var item in node.Children.OrderBy(o => o.DisplayOrder)) + { + Add(item); + } + } + } + + private static Node CreateTree() + { + var root = new Node + { + Name = "root", + Number = "1-18", + Children = new List { + new Node{ + Name="Fruit", + Number="2-11", + Children=new List{ + new Node{ + Name="Red", + Number="3-6", + Children=new List + { + new Node{ + Name="Cherry", + Number="4-5" + } + } + }, + new Node{ + Name="Yellow", + Number="7-10", + DisplayOrder=1, + Children=new List + { + new Node{ + Name="Banana", + Number="8-9" + } + } + } + } + }, + new Node{ + Name="Meat", + Number="12-17", + DisplayOrder=1, + Children=new List{ + new Node{ + Name="Beef", + Number="13-14" + }, + new Node{ + Name="Port", + Number="15-16", + DisplayOrder=1, + } + } + } + } + }; + return root; + } + } +} \ No newline at end of file