我正在开发一个C#WPF的小软件,用于从事连锁系统业务的咨询公司。我有编码树结构来显示谁转介谁。现在它有佣金取决于水平。如果1指2& 3然后1将获得1级佣金。如果2指的是4,5和& 3提到6,7然后1将获得2级佣金。这个链将继续到某个总数。我的问题是如何实现这个逻辑;我能够计算出谁通过UDF引用了多少成员来将TreeViewItem添加到TreeView。或者告诉我如何在一定程度上计算树视图中的项目? 节点添加UDF:
我正在使用MySQL,它的表成员包括columns-id,fullname,refercode。
public void AddNodes(int uid, TreeViewItem tSubNode)
{
string query = "select fullname, id from members where refCode=" + uid + ";";
MySqlCommand cmd = new MySqlCommand(query, db.conn);
MySqlDataAdapter _DA = new MySqlDataAdapter(cmd);
DataTable _DT = new DataTable();
tSubNode.IsExpanded = true;
_DA.Fill(_DT);
foreach (DataRow _dr in _DT.Rows)
{
TreeViewItem tNode = new TreeViewItem();
tNode.Header = _dr["fullname"].ToString()+" ("+_dr["id"].ToString()+")";
tSubNode.Items.Add(tNode);
if (db.HasMembers(Convert.ToInt32(_dr["id"].ToString())))
{
AddNodes(Convert.ToInt32(_dr["id"]), tNode);
}
}
//This line tracks who has referred how many members
Console.WriteLine("Tree node Count : "+tSubNode.Items.Count.ToString()+", UID "+uid);
}
请帮帮我!!!!
答案 0 :(得分:1)
通过计算树视图中的节点来计算这类事情非常愚蠢,您将在将其绑定到UI元素后进行计算。
在这种特殊情况下,我也不喜欢使用LINQ,而LINQ会做得很好,它有以下缺点:
您尚未指定使用的数据库,我将假设SQL。 如果您拥有人员的姓名以及在数据库的自引用表中引用它们的密钥,您应该能够轻松地计算出来。
你的表格如下:
int RecordID
varchar(100) Name
int ReferrerID
然后对于数据,它将如下所示(使用上面的示例):
_______________________________________________
| RecordID | Name | ReferrerID |
_______________________________________________
| 1 | Person 1 | null |
| 2 | Person 2 | 1 |
| 3 | Person 3 | 1 |
| 4 | Person 4 | 2 |
| 5 | Person 5 | 2 |
| 6 | Person 6 | 3 |
| 7 | Person 7 | 3 |
_______________________________________________
我还将使用一个名为Levels
的表,其中包含每个级别的佣金金额。在此表中,我为每个1级推荐分配0.50,为每个2级推荐分配0.25:
int LevelID
money LevelAmount
现在使用您的自引用表,您可以通过将自身连接两次来计算1级和2级所需的内容。这个sql会给你初始表:
SELECT p.RecordID
,p.PersonName
,Level1Referral.PersonName
,Level1Commission.LevelAmount
,Level2Referral.PersonName
,Level2Commission.LevelAmount
FROM People p
LEFT JOIN People Level1Referral
ON Level1Referral.ReferrerID = p.RecordID
LEFT JOIN Levels Level1Commission
ON Level1Referral.ReferrerID IS NOT NULL
AND Level1Commission.LevelID = 1
LEFT JOIN People Level2Referral
ON Level2Referral.ReferrerID = Level1Referral.RecordID
LEFT JOIN Levels Level2Commission
ON Level2Referral.ReferrerID IS NOT NULL
AND Level2Commission.LevelID = 2
我所做的就是为每个可能的佣金级别加入表格,如果您要引入第三级推荐佣金,那么您只需添加另一个联接。
如果您运行此语句,您会注意到Level1Referral.PersonName
如何为每个2级引荐提供重复条目,这不是最佳选择,并且不容易分组。然而,我们可以做的是有一个周围的SELECT语句并对其应用一些分组,并在此过程中计算每个级别的佣金金额。以下是执行所需操作的最终SQL语句:
SELECT CommissionEarnerID
,CommissionEarnerName
,COUNT(DISTINCT L1Referral) AS [Number Of L1 Referrals]
,COUNT(DISTINCT L1Referral)
* (SELECT LevelAmount
FROM Levels
WHERE LevelID = 1) AS [Level 1 Commission]
,COUNT(DISTINCT L2Referral) AS [Number Of L2 Referrals]
,COUNT(DISTINCT L2Referral)
* (SELECT LevelAmount
FROM Levels
WHERE LevelID = 2) AS [Level 2 Commission]
FROM (
SELECT p.RecordID AS CommissionEarnerID
,p.PersonName AS CommissionEarnerName
,Level1Referral.RecordID AS L1Referral
,Level1Referral.PersonName AS L1ReferralName
,Level2Referral.RecordID AS L2Referral
,Level2Referral.PersonName AS L2ReferralName
FROM People p
LEFT JOIN People Level1Referral
ON Level1Referral.ReferrerID = p.RecordID
LEFT JOIN People Level2Referral
ON Level2Referral.ReferrerID = Level1Referral.RecordID
) x
GROUP BY CommissionEarnerID
,CommissionEarnerName
我稍微整理了一下这个说法,并加了几个列名。这会生成如下所示的结果集:
_____________________________________________________________________________
| Commission | Commission | Number | Level 1 | Number | Level 2 |
| Earner ID | Earner | of L1 | Commission | of L2 | Commission |
| | Name | Referrals | | Referrals | |
_____________________________________________________________________________
| 1 | Person 1 | 2 | 1.00 | 4 | 1.00 |
| 2 | Person 2 | 2 | 1.00 | 0 | 0.00 |
| 3 | Person 3 | 2 | 1.00 | 0 | 0.00 |
| 4 | Person 4 | 0 | 0.00 | 0 | 0.00 |
| 5 | Person 5 | 0 | 0.00 | 0 | 0.00 |
| 6 | Person 6 | 0 | 0.00 | 0 | 0.00 |
| 7 | Person 7 | 0 | 0.00 | 0 | 0.00 |
_____________________________________________________________________________