2

这个问题与层次数据有些相关,但我的数据库中已经有一个存储过程,它返回下面提到的数据。我不是试图找出如何构建层次结构(完成),我试图找出使用 PHP(也许是递归数组?)基于该层次结构计算数字的最佳方法。

使用自定义存储过程,我可以提取我们公司的销售代表列表、他们销售的产品以及新行中每个产品的“收入”百分比及其后续销售代表(如果在树中) .

返回的数据示例如下:

Repnum | Name | productID | sales | earn | depth | parentRep
------------------------------------------------------------
1      |   A  |   1       | 5000  | 0.50 |  1    | 0
1      |   A  |   2       | 10000 | 0.35 |  1    | 0  
2      |   B  |   1       | 400   | 0.40 |  2    | 1
2      |   B  |   2       | 1000  | 0.30 |  2    | 1
7      |   E  |   1       | 30000 | 0.35 |  3    | 2
3      |   C  |   1       | 5000  | 0.33 |  2    | 1

可以安全地假设返回的数据的顺序已经根据此处未提供的深度和信息进行了适当的格式化和排序。在树视图中,上面的数据如下所示:

1-A-1-5000-0.50-1-0
1-A-2-10000-0.35-1-0
   2-B-1-400-0.40-2-1
   2-B-2-1000-0.30-2-1
      7-E-1-30000-0.35-3-2
   3-C-1-5000-0.33-2-1

在我的 while(results) 循环中,我试图为返回的每个 repnum 计算以下内容:

  • 该代表人数的总销售额乘以每种产品的收入百分比(他们的总销售额和收入金额)
  • 每个下线树的总销售额乘以当前代表与其下级之间的差值

为了展示这将如何工作的数学:

代表 1 收益:

Rep 1 total sales = 5000 + 10000 = 15,000
Rep 1 earn on self = (5000 * 0.50) + (10000 * 0.35) = 6,000 hello!

Rep 2 total sales = 400 + 1000 = 1400
Rep 1 earn on rep 2 sales = (400 * (0.50-0.40) + (1000 * (.35-0.30)) = 90

Rep 7 total sales = 30000
Rep 1 earn on rep 7 sales = (30000 * (0.50-0.40)) = 3000*
(* the reason the earn is calculated at rep 1 earn - rep 2 earn is because in any tree, the "parent" can only ever earn the difference of his/her earn and the direct depth below him/her)

Rep 3 total sales = 5000
Rep 1 earn on rep 3 sales = (5000 * (0.50-0.33)) = 850

**Rep 1 TOTAL earn = 6,000 + 90 + 3000 + 850 = 9,940**

代表 2 收益:

Rep 2 total sales = 400 + 1000 = 1400
Rep 2 total earn on self = (400 * 0.40) + (1000 * 0.30) = 460

Rep 7 total sales = 30000
Rep 2 total earn on rep 7 sales = (30000 * (0.40-0.35)) = 1500*
(* here, rep 2 earns the difference between him/her and rep 7 because he is in the depth right above it / his/her parent)

**Rep 2 TOTAL earn = 460 + 1500 = 1,960**

... 等等


我本可以构建脚本以使用 HEAVY mysql 递归并简单地为每个深度执行大量的 while() 循环,但发现它是不必要的,并且考虑到我用来继续并预先计算层次结构和深度并对数据进行适当的排序。计算顶级代表很简单,但是然后从列表上的第二个人(等等)返回并重新开始是我正在努力的地方。

我希望能够根据上面的示例数据返回类似于以下内容的输出:

num | Name | earnAmount
------------------------
1   |  A   | 9940
2   |  B   | 1960
7   |  E   | 10500
3   |  C   | 1650

提前感谢您的任何帮助!

对已提出问题的说明:

问:如何计算收入百分比?

答:它们不是计算出来的,它们是由代表所销售的特定产品 ID 确定的。


问:您如何确定“级别”或父/子关系?

A:我不在这个例子中。这部分等式是通过 MySQL 存储过程处理的,并且在很多方面不相关(我也可以显示每个 rep 的父 repnum,但由于数组是自上而下构建的,它可能不太有用)。第一个表中示例数据的深度和排序顺序已经被格式化和布局,使得每棵树都可以通过 PHP 中的简单 print(results_array) 语句打印出来。


Q:第一个表中productID的相关性是什么?

答:每个代表都可以销售我们系统中可用的任何产品(数百个),按 productID 分类。每个销售代表还从该特定产品的每次销售中获得一定百分比。收入百分比与特定产品类型(尽管每种产品都有最大和最小收入)或特定深度完全无关(尽管深度较高的代表对于特定产品 ID 的收入百分比永远不会低于他的下线树) .


问:您的数据在第一个表中是如何排序的?

A:有点无关紧要(相信我),但在幕后我创建了一个面包屑列,它采用当前的 repnum,然后根据它的组合和树的深度添加子节点并在该点进行排序。给出的示例:

0 (invisible parent which selects ALL sales reps)
  0-1
    0-1-2
      0-1-2-7
    0-1-3
...

4

1 回答 1

4

这是我的方法。
我假设数组可以是一维的。depth足以让我们确定Rep是否有“孩子”。所以数组看起来像这样:

$reps = array(
    array("rep" => "1", "name" => "A", "productId" => "1", "sales" => 5000, "earn" => 0.50, "depth" => "1"),
    array("rep" => "1", "name" => "A", "productId" => "2", "sales" => 10000, "earn" => 0.35, "depth" => "1"),
    array("rep" => "2", "name" => "B", "productId" => "1", "sales" => 400, "earn" => 0.40, "depth" => "2"),
    array("rep" => "2", "name" => "B", "productId" => "2", "sales" => 1000, "earn" => 0.30, "depth" => "2"),
    array("rep" => "7", "name" => "E", "productId" => "1", "sales" => 30000, "earn" => 0.35, "depth" => "3"),
    array("rep" => "3", "name" => "C", "productId" => "1", "sales" => 5000, "earn" => 0.33, "depth" => "2")
);

我决定采用递归方法。我们在earn()函数中循环遍历数组,在每次迭代后推进数组指针。在函数内部,我们再次for循环迭代,从current + 1数组元素开始到末尾找到Rep的“孩子”。代码如下所示:

function earn() {
    global $reps, $earns;
    $rep = current($reps);
    $key = key($reps);
    $immediateChildEarn = null;

    //basic Rep's earnings
    $earn = $rep['sales'] * $rep['earn'];

    //loop to find children with the same productId
    for ($i = $key + 1; $i < count($reps); $i++) {
        $child = $reps[$i];

        //we're only interested in Reps with the same product and deeper position
        if ($rep['productId'] !== $child['productId'] || $rep['depth'] >= $child['depth']) {
            continue;
        }

        //collect the earn of the immediate child
        if (null === $immediateChildEarn) {
            $immediateChildEarn = $child['earn'];
        }

        //the earn difference can't be greater than the difference between Rep and its first immediate child Rep
        if ($immediateChildEarn > $child['earn'] && $rep['depth'] + 1 < $child['depth']) {
            $child['earn'] = $immediateChildEarn;
        }

        //calculate the earnings gained from this child
        $earn += $child['sales'] * ($rep['earn'] - $child['earn']);
    }

    //just a quick fix to prevent throwing Notices - not significant for the algorithm itself
    if (!isset($earns[$rep['rep']])) {
        $earns[$rep['rep']] = 0;
    }

    $earns[$rep['rep']] += $earn;

    $finish = next($reps);
    if (false !== $finish) {
        earn();
    }
}

$earns = array();
reset($reps);
earn();

结果var_dump($earns)将是:

Array
(
    [1] => 9940
    [2] => 1960
    [7] => 10500
    [3] => 1650
)

请随时评论我的答案。我将尝试修复任何错误并尽我所能改进代码。

复杂性
我不擅长计算算法的复杂性,但在我的解决方案中,我认为复杂性是:

  1. 时间复杂度
    • 最坏情况O(n logn)
    • 最佳情况O(2n)
  2. 内存复杂度(不包括输入数组)
    • 最坏情况O(n)
    • 最佳情况O(1)

如果我错了,请随时纠正我。

于 2013-10-17T21:21:23.437 回答