You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/Infrastructure/Domain/BaseTreeEntity.cs

79 lines
2.3 KiB

using System;
using System.Collections.Generic;
using System.Linq;
namespace Infrastructure.Domain
{
public abstract class BaseTreeEntity<T> : BaseEntity where T : BaseTreeEntity<T>
{
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<T> Children { get; set; } = new List<T>();
#pragma warning restore CA2227 // 集合属性应为只读
//public List<T> GetPath()
//{
// var list = new List<T>();
// 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;
//}
private void UpdateLeftRight()
{
}
public void Update()
{
Left(this as T);
void Left(T node, int prev = 0)
{
if (node.Parent == null)
{
node.Left = 1;
}
else
{
node.Left = prev + 1;
}
if (node.Children.Any())
{
Left(node.Children.OrderBy(o => o.DisplayOrder).First(), node.Left);
}
else
{
Right(node, node.Left);
}
}
void Right(T node, int prev)
{
node.Right = prev + 1;
if (node.Parent != null)
{
var list = node.Parent.Children.OrderBy(o => o.DisplayOrder).ToList();
var index = list.IndexOf(node);
if (list.Count > index + 1)
{
Left(list[index + 1], node.Right);
}
else
{
Right(node.Parent, node.Right);
}
}
}
}
}
}