經銷商注冊。
我想用以下有關經銷商的資訊填寫串列
Id
ParentId
Name
對于每個分銷商,必須確定分銷商在系統中注冊的推薦人。這可以是已經在系統中注冊的任何人——在這種情況下,他們必須從已經注冊的分銷商串列中選擇,或者推薦人資訊可以為空,這意味著分銷商在沒有推薦人的情況下在系統中注冊。每個經銷商根據其推薦最多可帶三個人,系統應確保在同一經銷商“下”注冊的人數不超過三個。另外,提到的分銷商的層級深度最多應該是5級——也就是說,分銷商可以帶一個人推薦,他也會帶一個人推薦,等等。最多5個級別。因此,在這樣的一組中,總共可以有 1 3 9 27 81 = 121 人。系統必須提供對給定級別的控制。
uj5u.com熱心網友回復:
您可以使用遞回來查找串列中任何元素的深度,并使用簡單的舊計數來查找參考者的數量。
以下代碼是該想法的實作。
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://www.uj5u.com/net/392397.html
