经销商注册。
我想填写以下有关发行商的信息
Id
ParentId
Name
对于每个发行商,必须根据谁的推荐在系统中注册分销商。这可以是已经在系统中注册的任何人--在这种情况下,必须从已经注册的分销商列表中选择他们,或者推荐者的信息可以是空白的,这意味着分销商在系统中注册时没有推荐。每一分销商可根据其推荐最多带三人,系统应确保不超过三人在同一分销商的“下”注册。此外,上述分销商等级的深度应最大为5,也就是说,分销商可以带一名具有推荐意见的人,该人也会带着他的推荐等最多5层。因此,在这样一个群体中,总共可以有1+3+9+ 27 + 81 = 121人。系统必须提供对给定级别的控制。
发布于 2021-12-23 17:41:50
您可以使用递归查找列表中任何元素的深度,并使用普通的老计数来查找引用的数量。
下面的代码是该想法的实现。
void Main()
{
var distributors = new List<Distributor> {
new Distributor { Id = 1, ParentId = 0, Name = "A" },
new Distributor { Id = 7, ParentId = 1, Name = "B" },
new Distributor { Id = 9, ParentId = 7, Name = "C" },
new Distributor { Id = 13, ParentId = 9, Name = "D" },
new Distributor { Id = 28, ParentId = 13, Name = "E" },
};
var valid = IsValidToAdd(distributors, 9);
Console.WriteLine(valid);
}
public bool IsValidToAdd(List<Distributor> distributors, int parentId)
{
var referCount = distributors.Count(d => d.ParentId == parentId);
Console.WriteLine($"refer:{referCount}");
if (referCount == 3)
{
Console.WriteLine("There are already 3 referals for this parent");
return false;
}
var level = GetLevel(distributors, parentId);
Console.WriteLine($"level: {level}");
if (level > 5)
{
Console.WriteLine("There are already 5 levels of referals");
return false;
}
return true;
}
public int GetLevel(List<Distributor> distributors, int parentId)
{
var parent = distributors.FirstOrDefault(d => d.Id == parentId);
if (parent == null)
{
return 1;
}
return 1 + GetLevel(distributors, parent.ParentId);
}
public class Distributor
{
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
}
https://stackoverflow.com/questions/70464981
复制相似问题