Просмотр исходного кода

优化计算团队贡献值逻辑

sunxiaobiao 1 год назад
Родитель
Сommit
6a75fbb3f6
1 измененных файлов с 261 добавлено и 160 удалено
  1. 261 160
      app/common/repositories/user/UserRepository.php

+ 261 - 160
app/common/repositories/user/UserRepository.php

@@ -3284,59 +3284,9 @@ class UserRepository extends BaseRepository
3284 3284
         // 3、获取个人贡献值
3285 3285
         $userData = $this->dao->get($uid)->toArray();
3286 3286
         // 这一步骤是为了 确定用户升级时间,因为后边的团队贡献值,需要查询 用户 升级为业务员后 所产生的团队贡献值
3287
-        if (empty($userData['identity_up_time'])) {
3288
-            // 老用户的身份 升级时间已无法确定, 则采用用户的创建时间
3289
-            if ($userData['is_old'] == 1 && $userData['user_group'] > 1) {
3290
-                if (empty($userData['create_time'])) {
3291
-                    return $userData['user_group'];
3292
-                }
3293
-                $userData['identity_up_time'] = $userData['create_time'];
3294
-                $this->dao->update($uid, ['identity_up_time' => $userData['identity_up_time']]);
3295
-            }
3296
-            // 如果是新用户 则 根绝他的贡献值记录表 确认成为业务员的时间
3297
-            if ($userData['is_old'] == 0) {
3298
-                $upArriveNumOwn = 0;
3299
-                $yeWuYuanId = 0;
3300
-                foreach ($identityList as $value) {
3301
-                    if ($value['user_group_name'] == '业务员') {
3302
-                        $upArriveNumOwn = $value['arrive_num_own'];
3303
-                        $yeWuYuanId = $value['user_group_id'];
3304
-                        break;
3305
-                    }
3306
-                }
3307
-                if (!empty($upArriveNumOwn)) {
3308
-                    $userSignGongxianList = Db::name('user_sign_gongxian')
3309
-                        ->where('uid', $uid)
3310
-                        ->where('status', 1) // 贡献值状态1-有效 0-失效 -1待确认
3311
-                        ->whereNotNull('settlement_time') // `settlement_time` datetime DEFAULT NULL COMMENT '结算时间',
3312
-                        ->order('id asc')
3313
-                        ->field('uid, number, type, settlement_time') // 只需要下级的uid和贡献值
3314
-                        ->select()
3315
-                        ->toArray();
3316
-                    $myArriveNumOwn = 0;
3317
-                    $myIdentityUpTime = '';
3318
-                    foreach ($userSignGongxianList as $value) {
3319
-                        if ($value['type'] == 1) {
3320
-                            $myArriveNumOwn += $value['number'];
3321
-                        } else {
3322
-                            $myArriveNumOwn -= $value['number'];
3323
-                        }
3324
-                        if ($myArriveNumOwn >= $upArriveNumOwn) {
3325
-                            $myIdentityUpTime = $value['settlement_time'];
3326
-                            break;
3327
-                        }
3328
-                    }
3329
-                    if (!empty($myIdentityUpTime) && !empty($yeWuYuanId)) {
3330
-                        $userData['identity_up_time'] = $myIdentityUpTime;
3331
-                        $userData['user_group'] = $yeWuYuanId;
3332
-                        $this->dao->update($uid, [
3333
-                            'identity_up_time' => $myIdentityUpTime,
3334
-                            'user_group' => $yeWuYuanId
3335
-                        ]);
3336
-                    }
3337
-                }
3338
-            }
3339
-        }
3287
+        $determineResult = $this->determineUserUpgradeTime($uid);
3288
+        $userData['user_group'] = $determineResult['user_group'];
3289
+        $userData['identity_up_time'] = $determineResult['identity_up_time'];
3340 3290
         if ($userData['user_group'] == 1) {
3341 3291
             $maxLvRestrict = 2;
3342 3292
         }
@@ -3414,6 +3364,11 @@ class UserRepository extends BaseRepository
3414 3364
 
3415 3365
     /**
3416 3366
      * 获取该会员的所有下级会员
3367
+     * @param array $uidList
3368
+     * @return array
3369
+     * @throws DataNotFoundException
3370
+     * @throws DbException
3371
+     * @throws ModelNotFoundException
3417 3372
      */
3418 3373
     public function getUidContributeListBySpreadUidList(array $uidList)
3419 3374
     {
@@ -3433,14 +3388,15 @@ class UserRepository extends BaseRepository
3433 3388
      *                      1 表示只计算第1层(直接下级)的总贡献。
3434 3389
      *                      N 表示计算到第 N 层(包含第N层)的总贡献。
3435 3390
      * @param int $is_old
3436
-     * @param null $identity_up_time
3391
+     * @param null $startTime
3392
+     * @param null $endTime
3437 3393
      * @return array 返回一个数组,键是层级号(从1开始),值是该层级所有用户的贡献值总和。
3438 3394
      *               例如:[ 1 => 600, 2 => 1300 ]
3439 3395
      * @throws DataNotFoundException
3440 3396
      * @throws DbException
3441 3397
      * @throws ModelNotFoundException
3442 3398
      */
3443
-    public function getContributionSumByLevel(array $initialUserIds, int $maxDepth = 0, $is_old = 0, $identity_up_time = null): array
3399
+    public function getContributionSumByLevel(array $initialUserIds, int $maxDepth = 0, $is_old = 0, $startTime = null, $endTime = null): array
3444 3400
     {
3445 3401
         $levelContributions = []; // 结果数组 [level => total_contribution]
3446 3402
         if (empty($initialUserIds)) {
@@ -3481,57 +3437,52 @@ class UserRepository extends BaseRepository
3481 3437
                 break;
3482 3438
             }
3483 3439
 
3484
-            $subordinatesOfCurrentLevelUidList =[];
3485
-            foreach ($subordinatesOfCurrentLevel as &$value){
3486
-                $subordinatesOfCurrentLevelUidList[$value['uid']] = 0;
3487
-                $value['contribute'] = 0;
3488
-            }
3489
-
3490
-            // 如果 传入 $identity_up_time 不为空 证明当前在 确定身份的 用户已经是 业务员了
3491
-            if (!empty($identity_up_time)) {
3492
-                // 用户有贡献值记录,我们要根据 用户的身份 从普通消费者 升级 成业务员 的时间来 查询时间范围内产生的贡献值
3493
-                $userSignGongxianList = Db::name('user_sign_gongxian')
3494
-                    ->whereIn('uid', array_keys($subordinatesOfCurrentLevelUidList))
3495
-                    ->where('addtime', '>=', strtotime($identity_up_time)) //   `addtime` int(10) NOT NULL COMMENT '添加时间'
3496
-                    ->where('status', 1) // 贡献值状态1-有效 0-失效 -1待确认
3497
-                    ->whereNotNull('settlement_time') // `settlement_time` datetime DEFAULT NULL COMMENT '结算时间',
3498
-                    ->field('uid, number, type') // 只需要下级的uid和贡献值
3499
-                    ->select()
3500
-                    ->toArray();
3501
-                foreach ($userSignGongxianList as $value) {
3502
-                    if($value['type'] == 1) {
3503
-                        $subordinatesOfCurrentLevelUidList[$value['uid']] += $value['number'];
3504
-                    } else {
3505
-                        $subordinatesOfCurrentLevelUidList[$value['uid']] -= $value['number'];
3506
-                    }
3507
-                }
3508
-
3509
-                if ($is_old == 1) {
3510
-                    // 如果是老用户,除了通过贡献值记录表  还要计算 老订单 产生的贡献值
3511
-                    $oldUserOfGoodsList = Db::name('old_user_of_goods')
3512
-                        ->whereIn('user_id', array_keys($subordinatesOfCurrentLevelUidList))
3513
-                        ->where('create_time', '>=', strtotime($identity_up_time)) //   `addtime` int(10) NOT NULL COMMENT '添加时间'
3514
-                        ->where('status', 1) // '是否完成 0, 未完成 1 已完成',
3515
-                        ->field('user_id, money, status, create_time') // 只需要下级的uid和贡献值
3516
-                        ->select()
3517
-                        ->toArray();
3518
-
3519
-                    foreach ($oldUserOfGoodsList as $storeProductData) {
3520
-                        if ($storeProductData['money'] == 1180) {
3521
-                            $subordinatesOfCurrentLevelUidList[$storeProductData['user_id']] += 700;
3522
-                        } else if ($storeProductData['money'] == 10620) {
3523
-                            $subordinatesOfCurrentLevelUidList[$storeProductData['user_id']] += 6300;
3524
-                        } else if ($storeProductData['money'] == 11800) {
3525
-                            $subordinatesOfCurrentLevelUidList[$storeProductData['user_id']] += 7000;
3526
-                        } else if ($storeProductData['money'] == 9440) {
3527
-                            $subordinatesOfCurrentLevelUidList[$storeProductData['user_id']] += 5600;
3528
-                        } else if ($storeProductData['money'] == 2360) {
3529
-                            $subordinatesOfCurrentLevelUidList[$storeProductData['user_id']] += 1400;
3530
-                        } else {
3531
-                            $subordinatesOfCurrentLevelUidList[$storeProductData['user_id']] += bcmul($storeProductData['money'], 0.7, 2);
3532
-                        }
3533
-                    }
3534
-                }
3440
+            // 如果 传入 $startTime 不为空 证明当前在 确定身份的 用户已经是 业务员了
3441
+            if (!empty($startTime)) {
3442
+                $subordinatesOfCurrentLevelUidList = $this->getUserContributeByUidList(array_column($subordinatesOfCurrentLevel, 'uid'), $startTime, $endTime);
3443
+                // // 用户有贡献值记录,我们要根据 用户的身份 从普通消费者 升级 成业务员 的时间来 查询时间范围内产生的贡献值
3444
+                // $userSignGongxianList = Db::name('user_sign_gongxian')
3445
+                //     ->whereIn('uid', array_keys($subordinatesOfCurrentLevelUidList))
3446
+                //     ->where('addtime', '>=', strtotime($startTime)) //   `addtime` int(10) NOT NULL COMMENT '添加时间'
3447
+                //     ->where('status', 1) // 贡献值状态1-有效 0-失效 -1待确认
3448
+                //     ->whereNotNull('settlement_time') // `settlement_time` datetime DEFAULT NULL COMMENT '结算时间',
3449
+                //     ->field('uid, number, type') // 只需要下级的uid和贡献值
3450
+                //     ->select()
3451
+                //     ->toArray();
3452
+                // foreach ($userSignGongxianList as $value) {
3453
+                //     if($value['type'] == 1) {
3454
+                //         $subordinatesOfCurrentLevelUidList[$value['uid']] += $value['number'];
3455
+                //     } else {
3456
+                //         $subordinatesOfCurrentLevelUidList[$value['uid']] -= $value['number'];
3457
+                //     }
3458
+                // }
3459
+                //
3460
+                // if ($is_old == 1) {
3461
+                //     // 如果是老用户,除了通过贡献值记录表  还要计算 老订单 产生的贡献值
3462
+                //     $oldUserOfGoodsList = Db::name('old_user_of_goods')
3463
+                //         ->whereIn('user_id', array_keys($subordinatesOfCurrentLevelUidList))
3464
+                //         ->where('create_time', '>=', strtotime($startTime)) //   `addtime` int(10) NOT NULL COMMENT '添加时间'
3465
+                //         ->where('status', 1) // '是否完成 0, 未完成 1 已完成',
3466
+                //         ->field('user_id, money, status, create_time') // 只需要下级的uid和贡献值
3467
+                //         ->select()
3468
+                //         ->toArray();
3469
+                //
3470
+                //     foreach ($oldUserOfGoodsList as $storeProductData) {
3471
+                //         if ($storeProductData['money'] == 1180) {
3472
+                //             $subordinatesOfCurrentLevelUidList[$storeProductData['user_id']] += 700;
3473
+                //         } else if ($storeProductData['money'] == 10620) {
3474
+                //             $subordinatesOfCurrentLevelUidList[$storeProductData['user_id']] += 6300;
3475
+                //         } else if ($storeProductData['money'] == 11800) {
3476
+                //             $subordinatesOfCurrentLevelUidList[$storeProductData['user_id']] += 7000;
3477
+                //         } else if ($storeProductData['money'] == 9440) {
3478
+                //             $subordinatesOfCurrentLevelUidList[$storeProductData['user_id']] += 5600;
3479
+                //         } else if ($storeProductData['money'] == 2360) {
3480
+                //             $subordinatesOfCurrentLevelUidList[$storeProductData['user_id']] += 1400;
3481
+                //         } else {
3482
+                //             $subordinatesOfCurrentLevelUidList[$storeProductData['user_id']] += bcmul($storeProductData['money'], 0.7, 2);
3483
+                //         }
3484
+                //     }
3485
+                // }
3535 3486
             } else {
3536 3487
                 // 如果传入的 $identity_up_time 为空,说明该用户只是一个普通用户(消费者),没有团队业绩
3537 3488
                 break;
@@ -3582,66 +3533,204 @@ class UserRepository extends BaseRepository
3582 3533
         return $levelContributions;
3583 3534
     }
3584 3535
 
3585
-    public function getUserContributeInfoByDepth($uid, $depth = 10)
3586
-    {
3587
-        // 3、获取个人贡献值
3536
+
3537
+    /**
3538
+     * 获取用户 历史的 个人贡献值记录
3539
+     * @param $uidList
3540
+     * @return array
3541
+     * @throws DataNotFoundException
3542
+     * @throws DbException
3543
+     * @throws ModelNotFoundException
3544
+     */
3545
+    public function getUserContributeTimeMapByUidList($uidList) {
3546
+        $userContributeTimeMap = [];
3547
+        $userSignGongxianList = Db::name('user_sign_gongxian')
3548
+            ->whereIn('uid', $uidList)
3549
+            ->where('status', 1) // 贡献值状态1-有效 0-失效 -1待确认
3550
+            ->whereNotNull('settlement_time') // `settlement_time` datetime DEFAULT NULL COMMENT '结算时间',
3551
+            ->field('uid, number, type, mark, settlement_time') // 只需要下级的uid和贡献值
3552
+            ->select()
3553
+            ->toArray();
3554
+        foreach ($userSignGongxianList as $value) {
3555
+            // 贡献表中 包含多种类型的 贡献值记录,比如 用户消费商家获得赠送贡献值 , 推广获得的贡献值(直推),这些都不能算是 用户的 直接贡献值
3556
+            // 只有 用户取消费 比如:购买 ** 商品、扫码消费获得赠送贡献值 等 才算做个人贡献值
3557
+            if (strpos($value['mark'], "购买") !== false || strpos($value['mark'], "扫码") !== false) {
3558
+                if($value['type'] == 1) {
3559
+                    $type = 1;
3560
+                } else {
3561
+                    $type = 2;
3562
+                }
3563
+                $userContributeTimeMap[$value['uid']][] = [
3564
+                    'type' => $type,
3565
+                    'contribute' => $value['number'],
3566
+                    'settlementTime' => $value['settlement_time']
3567
+                ];
3568
+            }
3569
+        }
3570
+        // 老订单 产生的贡献值 并未在 贡献记录表中
3571
+        $oldUserOfGoodsList = Db::name('old_user_of_goods')
3572
+            ->where('status', 1) // '是否完成 0, 未完成 1 已完成',
3573
+            ->whereIn('user_id', $uidList)
3574
+            ->field('user_id, money, status, create_time') // 只需要下级的uid和贡献值
3575
+            ->select()
3576
+            ->toArray();
3577
+        // 定义一个映射数组,用于存储不同 money 对应的 contribute 值
3578
+        $contributeMapping = [
3579
+            1180 => 700,
3580
+            10620 => 6300,
3581
+            11800 => 7000,
3582
+            9440 => 5600,
3583
+            2360 => 1400,
3584
+        ];
3585
+        // 遍历每个 $storeProductData
3586
+        foreach ($oldUserOfGoodsList as $storeProductData) {
3587
+            // 获取 money 的值
3588
+            $money = $storeProductData['money'];
3589
+            // 检查 money 是否在映射中
3590
+            if (isset($contributeMapping[$money])) {
3591
+                // 如果存在,则使用映射的值
3592
+                $contribute = $contributeMapping[$money];
3593
+            } else {
3594
+                // 否则按默认规则计算
3595
+                $contribute = bcmul($money, 0.7, 2);
3596
+            }
3597
+            // 将数据添加到 $userContributeTimeList
3598
+            $userContributeTimeMap[$storeProductData['user_id']][] = [
3599
+                'type' => 1,
3600
+                'contribute' => $contribute,
3601
+                'settlementTime' => $storeProductData['create_time']
3602
+            ];
3603
+        }
3604
+        foreach ($userContributeTimeMap as $key => &$subArray) {
3605
+            usort($subArray, function ($a, $b) {
3606
+                return strtotime($a['settlementTime']) - strtotime($b['settlementTime']);
3607
+            });
3608
+        }
3609
+        return $userContributeTimeMap;
3610
+    }
3611
+
3612
+    /**
3613
+     * 获取用户升级成业务员的时间
3614
+     * @param $uid
3615
+     * @return mixed|string
3616
+     * @throws DataNotFoundException
3617
+     * @throws DbException
3618
+     * @throws ModelNotFoundException
3619
+     */
3620
+    public function determineUserUpgradeTime($uid) {
3588 3621
         $userData = $this->dao->get($uid)->toArray();
3589
-        // 这一步骤是为了 确定用户升级时间,因为后边的团队贡献值,需要查询 用户 升级为业务员后 所产生的团队贡献值
3590 3622
         if (empty($userData['identity_up_time'])) {
3591
-            // 老用户的身份 升级时间已无法确定, 则采用用户的创建时间
3592
-            if ($userData['is_old'] == 1 && $userData['user_group'] > 1) {
3593
-                if (empty($userData['create_time'])) {
3594
-                    return $userData['user_group'];
3623
+            $upArriveNumOwn = 0;
3624
+            $yeWuYuanId = 0;
3625
+            // 1、获取所有等级信息
3626
+            /** @var UserIdentityRepository $identityRepository */
3627
+            $identityRepository = app()->make(UserIdentityRepository::class);
3628
+            $identityList = $identityRepository->getIdentityList();
3629
+            foreach ($identityList as $value) {
3630
+                if ($value['user_group_name'] == '业务员') {
3631
+                    $upArriveNumOwn = $value['arrive_num_own'];
3632
+                    $yeWuYuanId = $value['user_group_id'];
3633
+                    break;
3595 3634
                 }
3596
-                $userData['identity_up_time'] = $userData['create_time'];
3597
-                $this->dao->update($uid, ['identity_up_time' => $userData['identity_up_time']]);
3598
-            }
3599
-            // 如果是新用户 则 根绝他的贡献值记录表 确认成为业务员的时间
3600
-            if ($userData['is_old'] == 0) {
3601
-                $upArriveNumOwn = 0;
3602
-                $yeWuYuanId = 0;
3603
-                // 1、获取所有等级信息
3604
-                /** @var UserIdentityRepository $identityRepository */
3605
-                $identityRepository = app()->make(UserIdentityRepository::class);
3606
-                $identityList = $identityRepository->getIdentityList();
3607
-                foreach ($identityList as $value) {
3608
-                    if ($value['user_group_name'] == '业务员') {
3609
-                        $upArriveNumOwn = $value['arrive_num_own'];
3610
-                        $yeWuYuanId = $value['user_group_id'];
3635
+            }
3636
+            // 2、获取用户贡献值 日志记录 对应时间
3637
+            $userContributeTimeMap = $this->getUserContributeTimeMapByUidList([$uid]);
3638
+            if (!empty($userContributeTimeMap[$uid])) {
3639
+                $myArriveNumOwn = 0;
3640
+                $myIdentityUpTime = '';
3641
+                foreach ($userContributeTimeMap[$uid] as $value) {
3642
+                    if ($value['type'] == 1) {
3643
+                        $myArriveNumOwn += $value['contribute'];
3644
+                    } else {
3645
+                        $myArriveNumOwn -= $value['contribute'];
3646
+                    }
3647
+                    if ($myArriveNumOwn >= $upArriveNumOwn) {
3648
+                        $myIdentityUpTime = $value['settlementTime'];
3611 3649
                         break;
3612 3650
                     }
3613 3651
                 }
3614
-                if (!empty($upArriveNumOwn)) {
3615
-                    $userSignGongxianList = Db::name('user_sign_gongxian')
3616
-                        ->where('uid', $uid)
3617
-                        ->where('status', 1) // 贡献值状态1-有效 0-失效 -1待确认
3618
-                        ->whereNotNull('settlement_time') // `settlement_time` datetime DEFAULT NULL COMMENT '结算时间',
3619
-                        ->order('id asc')
3620
-                        ->field('uid, number, type, settlement_time') // 只需要下级的uid和贡献值
3621
-                        ->select()
3622
-                        ->toArray();
3623
-                    $myArriveNumOwn = 0;
3624
-                    $myIdentityUpTime = '';
3625
-                    foreach ($userSignGongxianList as $value) {
3626
-                        if ($value['type'] == 1) {
3627
-                            $myArriveNumOwn += $value['number'];
3628
-                        } else {
3629
-                            $myArriveNumOwn -= $value['number'];
3630
-                        }
3631
-                        if ($myArriveNumOwn >= $upArriveNumOwn) {
3632
-                            $myIdentityUpTime = $value['settlement_time'];
3633
-                            break;
3634
-                        }
3652
+                if (!empty($myIdentityUpTime) && !empty($yeWuYuanId)) {
3653
+                    $userData['identity_up_time'] = $myIdentityUpTime;
3654
+                    $userData['user_group'] = $yeWuYuanId;
3655
+                    $this->dao->update($uid, [
3656
+                        'identity_up_time' => $userData['identity_up_time'],
3657
+                        'user_group' => $yeWuYuanId
3658
+                    ]);
3659
+                }
3660
+            }
3661
+        }
3662
+        return [
3663
+            'user_group' => $userData['user_group'],
3664
+            'identity_up_time' => $userData['identity_up_time']
3665
+        ];
3666
+    }
3667
+
3668
+    /**
3669
+     * 获取用户列表中 每个人 的 个人贡献值
3670
+     * @param array $uidList
3671
+     * @param null $startTime
3672
+     * @param null $endTime
3673
+     * @return array
3674
+     * @throws DataNotFoundException
3675
+     * @throws DbException
3676
+     * @throws ModelNotFoundException
3677
+     */
3678
+    public function getUserContributeByUidList($uidList = [], $startTime = null, $endTime = null)
3679
+    {
3680
+        $subordinatesOfCurrentLevelUidList = [];
3681
+        foreach ($uidList as $uid) {
3682
+            $subordinatesOfCurrentLevelUidList[$uid] = 0;
3683
+        }
3684
+        $userContributeTimeMap = $this->getUserContributeTimeMapByUidList($uidList);
3685
+        if (!empty($userContributeTimeMap)) {
3686
+            foreach ($userContributeTimeMap as $uid => $contributeInfoList) {
3687
+                foreach ($contributeInfoList as $value) {
3688
+                    // 跳过时间范围外的 贡献值
3689
+                    if (!empty($startTime) && strtotime($value['settlementTime']) < strtotime($startTime)) {
3690
+                        break;
3635 3691
                     }
3636
-                    if (!empty($myIdentityUpTime) && !empty($yeWuYuanId)) {
3637
-                        $userData['identity_up_time'] = $myIdentityUpTime;
3638
-                        $userData['user_group'] = $yeWuYuanId;
3639
-                        $this->dao->update($uid, [
3640
-                            'identity_up_time' => $userData['identity_up_time'],
3641
-                            'user_group' => $yeWuYuanId
3642
-                        ]);
3692
+                    if (!empty($endTime) && strtotime($value['settlementTime']) > strtotime($endTime)) {
3693
+                        break;
3694
+                    }
3695
+                    if ($value['type'] == 1) {
3696
+                        $subordinatesOfCurrentLevelUidList[$uid] += $value['contribute'];
3697
+                    } else {
3698
+                        $subordinatesOfCurrentLevelUidList[$uid] -= $value['contribute'];
3643 3699
                     }
3644 3700
                 }
3701
+
3702
+            }
3703
+        }
3704
+        return $subordinatesOfCurrentLevelUidList;
3705
+    }
3706
+
3707
+    /**
3708
+     * 获取用户 团队贡献值信息
3709
+     * @param $uid
3710
+     * @param int $depth // 最深递归层级
3711
+     * @param array $betweenTime // 贡献值获取范围
3712
+     * @return array
3713
+     * @throws DataNotFoundException
3714
+     * @throws DbException
3715
+     * @throws ModelNotFoundException
3716
+     */
3717
+    public function getUserContributeInfoByDepth($uid, $depth = 10, $betweenTime = [])
3718
+    {
3719
+        // 3、获取个人贡献值
3720
+        $userData = $this->dao->get($uid)->toArray();
3721
+        // 这一步骤是为了 确定用户升级时间,因为后边的团队贡献值,需要查询 用户 升级为业务员后 所产生的团队贡献值
3722
+        $determineResult = $this->determineUserUpgradeTime($uid);
3723
+        $userData['user_group'] = $determineResult['user_group'];
3724
+        $userData['identity_up_time'] = $determineResult['identity_up_time']; // 获得的值 有可能是时间 有可能是 null
3725
+
3726
+        $startTime = $userData['identity_up_time'];
3727
+        $endTime = date('Y-m-d H:i:s');
3728
+        if (!empty($betweenTime) && !empty($betweenTime['startTime']) && !empty($betweenTime['endTime'])) {
3729
+            if(empty($startTime) || strtotime($startTime) < strtotime($betweenTime['startTime'])) {
3730
+                $startTime = $betweenTime['startTime'];
3731
+            }
3732
+            if(strtotime($betweenTime['endTime']) < strtotime($endTime)) {
3733
+                $endTime = $betweenTime['endTime'];
3645 3734
             }
3646 3735
         }
3647 3736
 
@@ -3658,15 +3747,20 @@ class UserRepository extends BaseRepository
3658 3747
         $userContributionSumMap = [];
3659 3748
         // 获取用户的直属下级
3660 3749
         $uidContributeList = $this->getUidContributeListBySpreadUidList([$uid]);
3750
+        $uidContributeUidList = array_column($uidContributeList, 'uid');
3751
+        // TODO 身份是用户的 $startTime = null
3752
+        $subordinatesOfCurrentLevelUidList = $this->getUserContributeByUidList($uidContributeUidList, $startTime, $endTime);
3753
+
3661 3754
         foreach ($uidContributeList as $uidContribute) {
3662 3755
             // 获取每个下级(部门)的个人贡献值
3663 3756
             $userContributionSumMap[$uidContribute['uid']] = $this->getContributionSumByLevel(
3664 3757
                 [$uidContribute['uid']],
3665 3758
                 $depth,
3666 3759
                 $userData['is_old'],
3667
-                $userData['identity_up_time'] ?? null
3760
+                $startTime,
3761
+                $endTime
3668 3762
             );
3669
-            $userContributionSumMap[$uidContribute['uid']][0] = $uidContribute['contribute'];
3763
+            $userContributionSumMap[$uidContribute['uid']][0] = $subordinatesOfCurrentLevelUidList[$uidContribute['uid']];
3670 3764
             ksort($userContributionSumMap[$uidContribute['uid']]);
3671 3765
             // 假设 老大的 下级是 张三和李四, 董事(最高级身份)需要循环最深的等级是 3 级    得出来的结果↓  这样是为了计算大小部门
3672 3766
             // $userContributionSumMap[直属下级(部门)张三] =>[0 => 张三个人贡献值, 1 => 张三所有下级用户的个人贡献值, 2 => 张三所有下级用户的下级用户贡献值]
@@ -3677,6 +3771,7 @@ class UserRepository extends BaseRepository
3677 3771
         foreach ($userContributionSumMap as $key => $value) {
3678 3772
             $subordinateContribution[$key] = array_sum($value);
3679 3773
         }
3774
+        $dqYj = 0;
3680 3775
         $arriveNumTeam = 0;
3681 3776
         if (!empty($subordinateContribution)) {
3682 3777
             // 大区业绩
@@ -3687,7 +3782,13 @@ class UserRepository extends BaseRepository
3687 3782
             $arriveNumTeam = bcsub($zYj, $dqYj, 2);
3688 3783
         }
3689 3784
 
3690
-        return ['arriveNumTeam' => $arriveNumTeam, 'contribute' => $userData['contribute'], 'per_contribute' => $userData['per_contribute']];
3785
+        return [
3786
+            'arriveNumTeam' => $arriveNumTeam, // 小部门贡献值和
3787
+            'contribute' => $userData['contribute'], // 个人贡献值
3788
+            'perContribute' => $userData['per_contribute'], // 直推贡献值
3789
+            'largeContribute' => $dqYj, // 大部门贡献值
3790
+            'subordinate' => $subordinateContribution // 各部门贡献值和
3791
+        ];
3691 3792
 
3692 3793
     }
3693 3794
 }