|
|
@@ -5,6 +5,7 @@ namespace app\common\repositories\shared;
|
|
5
|
5
|
use app\common\dao\shared\SharedProsperityUserDao;
|
|
6
|
6
|
use app\common\dao\user\UserDao;
|
|
7
|
7
|
use app\common\enum\CommonEnum;
|
|
|
8
|
+use app\common\enum\shared\SharedProsperityUserEnum;
|
|
8
|
9
|
use app\common\repositories\BaseRepository;
|
|
9
|
10
|
use app\entity\data\shared\SharedProsperityRecommendConfigEntity;
|
|
10
|
11
|
use app\entity\data\shared\SharedProsperityUserEntity;
|
|
|
@@ -75,6 +76,16 @@ class SharedProsperityUserRepository extends BaseRepository
|
|
75
|
76
|
}
|
|
76
|
77
|
|
|
77
|
78
|
/**
|
|
|
79
|
+ * 获取直接下级用户ID列表
|
|
|
80
|
+ * @param int $parentId 用户ID
|
|
|
81
|
+ * @return SharedProsperityUserEntity[] 直接下级用户ID列表
|
|
|
82
|
+ */
|
|
|
83
|
+ public function getSharedProsperityUserEntityListByParentId(int $parentId): array
|
|
|
84
|
+ {
|
|
|
85
|
+ return $this->dao->getSharedProsperityUserEntityListByParentId($parentId);
|
|
|
86
|
+ }
|
|
|
87
|
+
|
|
|
88
|
+ /**
|
|
78
|
89
|
* @param array $userIdList
|
|
79
|
90
|
* @return SharedProsperityUserEntity[]
|
|
80
|
91
|
*/
|
|
|
@@ -117,7 +128,7 @@ class SharedProsperityUserRepository extends BaseRepository
|
|
117
|
128
|
}
|
|
118
|
129
|
}
|
|
119
|
130
|
|
|
120
|
|
- public function updateByUserId($userId,$data)
|
|
|
131
|
+ public function updateByUserId($userId, $data): int
|
|
121
|
132
|
{
|
|
122
|
133
|
try {
|
|
123
|
134
|
return $this->dao->updateByUserId($userId, $data);
|
|
|
@@ -289,7 +300,9 @@ class SharedProsperityUserRepository extends BaseRepository
|
|
289
|
300
|
*/
|
|
290
|
301
|
public function identityUpgrade(array $userIdList)
|
|
291
|
302
|
{
|
|
292
|
|
- $sharedProsperityUserEntityList = $this->getSharedProsperityUserEntityListByUserIdList($userIdList);
|
|
|
303
|
+ if (empty($userIdList)) {
|
|
|
304
|
+ return;
|
|
|
305
|
+ }
|
|
293
|
306
|
|
|
294
|
307
|
/** @var SharedProsperityRecommendConfigRepository $sharedProsperityRecommendConfigRepository */
|
|
295
|
308
|
$sharedProsperityRecommendConfigRepository = app()->make(SharedProsperityRecommendConfigRepository::class);
|
|
|
@@ -297,23 +310,214 @@ class SharedProsperityUserRepository extends BaseRepository
|
|
297
|
310
|
usort($sharedProsperityRecommendConfigEntityList, function (SharedProsperityRecommendConfigEntity $a, SharedProsperityRecommendConfigEntity $b) {
|
|
298
|
311
|
return $a->getPv() <=> $b->getPv(); // 从小到大排序
|
|
299
|
312
|
});
|
|
|
313
|
+
|
|
|
314
|
+ $sharedProsperityUserEntityList = $this->getSharedProsperityUserEntityListByUserIdList($userIdList);
|
|
|
315
|
+
|
|
|
316
|
+ // 批量更新用户等级
|
|
|
317
|
+ $this->batchUpgradeUsers($sharedProsperityUserEntityList, $sharedProsperityRecommendConfigEntityList);
|
|
|
318
|
+
|
|
|
319
|
+ // foreach ($sharedProsperityUserEntityList as $sharedProsperityUserEntity) {
|
|
|
320
|
+ // $partnerLevel = 0;
|
|
|
321
|
+ // foreach ($sharedProsperityRecommendConfigEntityList as $sharedProsperityRecommendConfigEntity) {
|
|
|
322
|
+ // if ($sharedProsperityUserEntity->getPv() >= $sharedProsperityRecommendConfigEntity->getPv()) {
|
|
|
323
|
+ // $partnerLevel = $sharedProsperityRecommendConfigEntity->getLevel();
|
|
|
324
|
+ // } else {
|
|
|
325
|
+ // // 如果本次验证的等级结果大于 之前的等级,说明用户升级了 需要更新等级
|
|
|
326
|
+ // if ($partnerLevel > $sharedProsperityUserEntity->getPartnerLevel()) {
|
|
|
327
|
+ // $this->updateByEntity(
|
|
|
328
|
+ // SharedProsperityUserEntity::newInstance()
|
|
|
329
|
+ // ->setId($sharedProsperityUserEntity->getId())
|
|
|
330
|
+ // ->setPartnerLevel($partnerLevel)
|
|
|
331
|
+ // );
|
|
|
332
|
+ // }
|
|
|
333
|
+ // break;
|
|
|
334
|
+ // }
|
|
|
335
|
+ // }
|
|
|
336
|
+ // }
|
|
|
337
|
+ }
|
|
|
338
|
+
|
|
|
339
|
+ /**
|
|
|
340
|
+ * 批量更新用户等级
|
|
|
341
|
+ * @param SharedProsperityUserEntity[] $sharedProsperityUserEntityList 用户实体列表
|
|
|
342
|
+ * @param SharedProsperityRecommendConfigEntity[] $sharedProsperityRecommendConfigEntityList 等级配置列表
|
|
|
343
|
+ * @return void
|
|
|
344
|
+ */
|
|
|
345
|
+ private function batchUpgradeUsers(array $sharedProsperityUserEntityList, array $sharedProsperityRecommendConfigEntityList)
|
|
|
346
|
+ {
|
|
300
|
347
|
foreach ($sharedProsperityUserEntityList as $sharedProsperityUserEntity) {
|
|
301
|
|
- $partnerLevel = 0;
|
|
302
|
|
- foreach ($sharedProsperityRecommendConfigEntityList as $sharedProsperityRecommendConfigEntity) {
|
|
303
|
|
- if ($sharedProsperityUserEntity->getPv() >= $sharedProsperityRecommendConfigEntity->getPv()) {
|
|
304
|
|
- $partnerLevel = $sharedProsperityRecommendConfigEntity->getLevel();
|
|
305
|
|
- } else {
|
|
306
|
|
- // 如果本次验证的等级结果大于 之前的等级,说明用户升级了 需要更新等级
|
|
307
|
|
- if ($partnerLevel > $sharedProsperityUserEntity->getPartnerLevel()) {
|
|
308
|
|
- $this->updateByEntity(
|
|
309
|
|
- SharedProsperityUserEntity::newInstance()
|
|
310
|
|
- ->setId($sharedProsperityUserEntity->getId())
|
|
311
|
|
- ->setPartnerLevel($partnerLevel)
|
|
312
|
|
- );
|
|
313
|
|
- }
|
|
314
|
|
- break;
|
|
|
348
|
+
|
|
|
349
|
+ // // 计算用户的团队业绩(向下5级)
|
|
|
350
|
+ // $teamPv = $this->calculateTeamPv($sharedProsperityUserEntity->getId());
|
|
|
351
|
+
|
|
|
352
|
+ // 根据团队业绩确定等级
|
|
|
353
|
+ // $newLevel = $this->calculateLevelByTeamPv($teamPv, $sharedProsperityRecommendConfigEntityList);
|
|
|
354
|
+ //
|
|
|
355
|
+ // // 如果新等级大于当前等级,则更新
|
|
|
356
|
+ // if ($newLevel > $sharedProsperityUserEntity->getPartnerLevel()) {
|
|
|
357
|
+ // $this->updateByEntity(
|
|
|
358
|
+ // SharedProsperityUserEntity::newInstance()
|
|
|
359
|
+ // ->setId($sharedProsperityUserEntity->getId())
|
|
|
360
|
+ // ->setPartnerLevel($newLevel)
|
|
|
361
|
+ // );
|
|
|
362
|
+ // }
|
|
|
363
|
+
|
|
|
364
|
+ $this->upgradeUpperLevels($sharedProsperityUserEntity->getUserId(), $sharedProsperityRecommendConfigEntityList);
|
|
|
365
|
+ }
|
|
|
366
|
+ }
|
|
|
367
|
+
|
|
|
368
|
+ /**
|
|
|
369
|
+ * 向上追溯更新上级身份等级(最多向上5层)
|
|
|
370
|
+ * @param int $userId 当前用户ID
|
|
|
371
|
+ * @param SharedProsperityRecommendConfigEntity[] $sharedProsperityRecommendConfigEntityList 等级配置列表
|
|
|
372
|
+ * @return void
|
|
|
373
|
+ */
|
|
|
374
|
+ private function upgradeUpperLevels(int $userId, array $sharedProsperityRecommendConfigEntityList)
|
|
|
375
|
+ {
|
|
|
376
|
+ $maxLevels = SharedProsperityUserEnum::TEAM_DEPTH;
|
|
|
377
|
+ $currentUserId = $userId;
|
|
|
378
|
+
|
|
|
379
|
+ for ($i = 0; $i < $maxLevels; $i++) {
|
|
|
380
|
+ // 获取上级用户ID
|
|
|
381
|
+ $sharedProsperityUserEntity = $this->getSharedProsperityUserEntityByUserId($currentUserId);
|
|
|
382
|
+ $parentId = $sharedProsperityUserEntity->getParentId();
|
|
|
383
|
+
|
|
|
384
|
+ if (empty($parentId)) {
|
|
|
385
|
+ break; // 没有上级,停止追溯
|
|
|
386
|
+ }
|
|
|
387
|
+
|
|
|
388
|
+ // 重新计算上级的团队业绩和等级
|
|
|
389
|
+ $this->upgradeSingleUser($parentId, $sharedProsperityRecommendConfigEntityList);
|
|
|
390
|
+
|
|
|
391
|
+ // 继续向上追溯
|
|
|
392
|
+ $currentUserId = $parentId;
|
|
|
393
|
+ }
|
|
|
394
|
+ }
|
|
|
395
|
+
|
|
|
396
|
+ /**
|
|
|
397
|
+ * 升级单个用户身份
|
|
|
398
|
+ * @param int $userId 用户ID
|
|
|
399
|
+ * @param SharedProsperityRecommendConfigEntity[] $sharedProsperityRecommendConfigEntityList 等级配置列表
|
|
|
400
|
+ * @return void
|
|
|
401
|
+ */
|
|
|
402
|
+ private function upgradeSingleUser(int $userId, array $sharedProsperityRecommendConfigEntityList)
|
|
|
403
|
+ {
|
|
|
404
|
+ // 获取用户实体
|
|
|
405
|
+ $sharedProsperityUserEntity = $this->getSharedProsperityUserEntityByUserId($userId);
|
|
|
406
|
+
|
|
|
407
|
+ if (empty($sharedProsperityUserEntity->getId())) {
|
|
|
408
|
+ return;
|
|
|
409
|
+ }
|
|
|
410
|
+
|
|
|
411
|
+ // 计算团队业绩
|
|
|
412
|
+ $teamPvMap = $this->calculateTeamPv($userId);
|
|
|
413
|
+
|
|
|
414
|
+ // 当前只计算团队业绩的 小团队业绩
|
|
|
415
|
+ $maxKey = array_keys($teamPvMap, max($teamPvMap))[0];
|
|
|
416
|
+ unset($teamPvMap[$maxKey]);
|
|
|
417
|
+ $teamPvBySmallSum = array_sum($teamPvMap);
|
|
|
418
|
+
|
|
|
419
|
+ // 计算新等级
|
|
|
420
|
+ $newLevel = $this->calculateLevelByTeamPv($teamPvBySmallSum, $sharedProsperityRecommendConfigEntityList);
|
|
|
421
|
+
|
|
|
422
|
+ // 如果新等级大于当前等级,则更新
|
|
|
423
|
+ if ($newLevel > $sharedProsperityUserEntity->getPartnerLevel()) {
|
|
|
424
|
+ $this->updateByEntity(
|
|
|
425
|
+ SharedProsperityUserEntity::newInstance()
|
|
|
426
|
+ ->setId($sharedProsperityUserEntity->getId())
|
|
|
427
|
+ ->setPartnerLevel($newLevel)
|
|
|
428
|
+ );
|
|
|
429
|
+ }
|
|
|
430
|
+ }
|
|
|
431
|
+
|
|
|
432
|
+ /**
|
|
|
433
|
+ * 计算用户的团队业绩(向下5级)
|
|
|
434
|
+ * @param int $userId 用户ID
|
|
|
435
|
+ * @return array 团队业绩
|
|
|
436
|
+ */
|
|
|
437
|
+ private function calculateTeamPv(int $userId): array
|
|
|
438
|
+ {
|
|
|
439
|
+ $teamPvMap = [];
|
|
|
440
|
+
|
|
|
441
|
+ // 获取所有下级用户ID(向下5级)
|
|
|
442
|
+ // 第一层级在这里查询 因为要统计大小区
|
|
|
443
|
+ $sharedProsperityUserEntityListByParentId = $this->getSharedProsperityUserEntityListByParentId($userId);
|
|
|
444
|
+
|
|
|
445
|
+ // 循环 直属下级
|
|
|
446
|
+ foreach ($sharedProsperityUserEntityListByParentId as $sharedProsperityUserEntityByUnderling) {
|
|
|
447
|
+ $departmentTeamUserIdList = $this->getUserIdListByParentIdForDepth($sharedProsperityUserEntityByUnderling->getUserId(), SharedProsperityUserEnum::TEAM_DEPTH - 1);
|
|
|
448
|
+ $departmentTeamUserIdList[] = $sharedProsperityUserEntityByUnderling->getUserId();
|
|
|
449
|
+
|
|
|
450
|
+ // 直属团队的 业绩总和
|
|
|
451
|
+ $teamPv = '0.00';
|
|
|
452
|
+ if (!empty($departmentTeamUserIdList)) {
|
|
|
453
|
+ // 批量获取下级用户的个人业绩并求和
|
|
|
454
|
+ // 假设有方法批量获取用户的PV
|
|
|
455
|
+ $sharedProsperityUserEntityListByTeam = $this->getSharedProsperityUserEntityListByUserIdList($departmentTeamUserIdList);
|
|
|
456
|
+ foreach ($sharedProsperityUserEntityListByTeam as $sharedProsperityUserEntityByTeam) {
|
|
|
457
|
+ $teamPv = bcadd($teamPv, (string)$sharedProsperityUserEntityByTeam->getPv(), 2);
|
|
315
|
458
|
}
|
|
316
|
459
|
}
|
|
|
460
|
+ $teamPvMap[$sharedProsperityUserEntityByUnderling->getUserId()] = $teamPv;
|
|
317
|
461
|
}
|
|
|
462
|
+
|
|
|
463
|
+ return $teamPvMap;
|
|
|
464
|
+ }
|
|
|
465
|
+
|
|
|
466
|
+
|
|
|
467
|
+ /**
|
|
|
468
|
+ * 根据团队业绩计算等级
|
|
|
469
|
+ * @param float $teamPv 团队业绩
|
|
|
470
|
+ * @param array $configEntityList 等级配置列表
|
|
|
471
|
+ * @return int 等级
|
|
|
472
|
+ */
|
|
|
473
|
+ private function calculateLevelByTeamPv(float $teamPv, array $configEntityList): int
|
|
|
474
|
+ {
|
|
|
475
|
+ $level = 0;
|
|
|
476
|
+
|
|
|
477
|
+ foreach ($configEntityList as $configEntity) {
|
|
|
478
|
+ if ($teamPv >= $configEntity->getPv()) {
|
|
|
479
|
+ $level = $configEntity->getLevel();
|
|
|
480
|
+ } else {
|
|
|
481
|
+ break;
|
|
|
482
|
+ }
|
|
|
483
|
+ }
|
|
|
484
|
+
|
|
|
485
|
+ return $level;
|
|
|
486
|
+ }
|
|
|
487
|
+
|
|
|
488
|
+ /**
|
|
|
489
|
+ * 获取下级用户ID列表(向下指定层级)
|
|
|
490
|
+ * @param int $parentId 用户ID
|
|
|
491
|
+ * @param int $maxLevel 最大层级数
|
|
|
492
|
+ * @param int $currentLevel 当前层级(内部递归使用)
|
|
|
493
|
+ * @return array 下级用户ID列表
|
|
|
494
|
+ */
|
|
|
495
|
+ private function getUserIdListByParentIdForDepth(int $parentId, int $maxLevel, int $currentLevel = 1): array
|
|
|
496
|
+ {
|
|
|
497
|
+ if ($currentLevel > $maxLevel) {
|
|
|
498
|
+ return [];
|
|
|
499
|
+ }
|
|
|
500
|
+
|
|
|
501
|
+ $subordinateIds = [];
|
|
|
502
|
+
|
|
|
503
|
+ // 获取直接下级用户ID列表(假设有方法获取直接下级)
|
|
|
504
|
+ $sharedProsperityUserEntityListByParentId = $this->getSharedProsperityUserEntityListByParentId($parentId);
|
|
|
505
|
+
|
|
|
506
|
+ if (empty($sharedProsperityUserEntityListByParentId)) {
|
|
|
507
|
+ return [];
|
|
|
508
|
+ }
|
|
|
509
|
+
|
|
|
510
|
+ // 添加直接下级
|
|
|
511
|
+ $subordinateIds = array_merge($subordinateIds, array_column($sharedProsperityUserEntityListByParentId, 'userId'));
|
|
|
512
|
+
|
|
|
513
|
+ // 递归获取下级的下级
|
|
|
514
|
+ foreach ($sharedProsperityUserEntityListByParentId as $sharedProsperityUserEntity) {
|
|
|
515
|
+ $nestedSubordinateIds = $this->getUserIdListByParentIdForDepth($sharedProsperityUserEntity->getUserId(), $maxLevel, $currentLevel + 1);
|
|
|
516
|
+ if (!empty($nestedSubordinateIds)) {
|
|
|
517
|
+ $subordinateIds = array_merge($subordinateIds, $nestedSubordinateIds);
|
|
|
518
|
+ }
|
|
|
519
|
+ }
|
|
|
520
|
+
|
|
|
521
|
+ return array_unique($subordinateIds);
|
|
318
|
522
|
}
|
|
319
|
523
|
}
|