|
|
@@ -520,4 +520,115 @@ class SharedProsperityUserRepository extends BaseRepository
|
|
520
|
520
|
|
|
521
|
521
|
return array_unique($subordinateIds);
|
|
522
|
522
|
}
|
|
|
523
|
+
|
|
|
524
|
+ /**
|
|
|
525
|
+ * 获取用户树
|
|
|
526
|
+ * @param int $userId 用户ID
|
|
|
527
|
+ * @return array 用户树,格式:[{name: '张三', parent: null, children: [...]}]
|
|
|
528
|
+ */
|
|
|
529
|
+ public function getUserTree($userId)
|
|
|
530
|
+ {
|
|
|
531
|
+ /** @var SharedProsperityUserDao $userDao */
|
|
|
532
|
+ $userDao = app()->make(SharedProsperityUserDao::class);
|
|
|
533
|
+
|
|
|
534
|
+ // 获取所有下级用户ID(包括自己)
|
|
|
535
|
+ $subordinateIds = $userDao->getAllSubordinateUserIds($userId, true);
|
|
|
536
|
+ if (empty($subordinateIds)) {
|
|
|
537
|
+ return [];
|
|
|
538
|
+ }
|
|
|
539
|
+
|
|
|
540
|
+ // 获取这些共富用户的基本信息(包含parent_id)
|
|
|
541
|
+ $sharedUsers = $userDao->getSharedProsperityUserEntityListByUserIdList($subordinateIds);
|
|
|
542
|
+ if (empty($sharedUsers)) {
|
|
|
543
|
+ return [];
|
|
|
544
|
+ }
|
|
|
545
|
+
|
|
|
546
|
+ // 获取用户昵称映射
|
|
|
547
|
+ $userIds = array_map(function ($entity) {
|
|
|
548
|
+ return $entity->getUserId();
|
|
|
549
|
+ }, $sharedUsers);
|
|
|
550
|
+ /** @var UserDao $userDao */
|
|
|
551
|
+ $userDao = app()->make(UserDao::class);
|
|
|
552
|
+ $userInfos = $userDao->getUserEntityListByUidList($userIds);
|
|
|
553
|
+ $idToName = [];
|
|
|
554
|
+ foreach ($userInfos as $userInfo) {
|
|
|
555
|
+ $idToName[$userInfo->getUid()] = $userInfo->getNickname() . '(' . $userInfo->account .')' ?? ('用户' . $userInfo->getUid());
|
|
|
556
|
+ }
|
|
|
557
|
+
|
|
|
558
|
+ // 构建节点数组
|
|
|
559
|
+ $nodes = [];
|
|
|
560
|
+ foreach ($sharedUsers as $sharedUser) {
|
|
|
561
|
+ $nodes[] = [
|
|
|
562
|
+ 'user_id' => $sharedUser->getUserId(),
|
|
|
563
|
+ 'parent_id' => $sharedUser->getParentId(),
|
|
|
564
|
+ 'name' => $idToName[$sharedUser->getUserId()] ?? ('用户' . $sharedUser->getUserId()),
|
|
|
565
|
+ ];
|
|
|
566
|
+ }
|
|
|
567
|
+
|
|
|
568
|
+ // 构建树,以传入的用户为根节点
|
|
|
569
|
+ $rootNode = $this->findNode($nodes, $userId);
|
|
|
570
|
+ if (!$rootNode) {
|
|
|
571
|
+ return [];
|
|
|
572
|
+ }
|
|
|
573
|
+ $tree = [
|
|
|
574
|
+ 'name' => $rootNode['name'],
|
|
|
575
|
+ 'parent' => null,
|
|
|
576
|
+ 'children' => $this->buildSharedTree($nodes, $userId)
|
|
|
577
|
+ ];
|
|
|
578
|
+ return [$tree];
|
|
|
579
|
+ }
|
|
|
580
|
+
|
|
|
581
|
+ /**
|
|
|
582
|
+ * 递归构建共富用户树
|
|
|
583
|
+ * @param array $nodes 节点数组,每个节点包含 user_id, parent_id, name
|
|
|
584
|
+ * @param int $parentId 父ID
|
|
|
585
|
+ * @return array
|
|
|
586
|
+ */
|
|
|
587
|
+ private function buildSharedTree($nodes, $parentId)
|
|
|
588
|
+ {
|
|
|
589
|
+ $branch = [];
|
|
|
590
|
+ foreach ($nodes as $node) {
|
|
|
591
|
+ if ($node['parent_id'] == $parentId) {
|
|
|
592
|
+ $children = $this->buildSharedTree($nodes, $node['user_id']);
|
|
|
593
|
+ $branch[] = [
|
|
|
594
|
+ 'name' => $node['name'],
|
|
|
595
|
+ 'parent' => $this->findNodeName($nodes, $parentId) ?? null,
|
|
|
596
|
+ 'children' => $children
|
|
|
597
|
+ ];
|
|
|
598
|
+ }
|
|
|
599
|
+ }
|
|
|
600
|
+ return $branch;
|
|
|
601
|
+ }
|
|
|
602
|
+
|
|
|
603
|
+ /**
|
|
|
604
|
+ * 根据user_id查找节点名称
|
|
|
605
|
+ * @param array $nodes
|
|
|
606
|
+ * @param int $userId
|
|
|
607
|
+ * @return string|null
|
|
|
608
|
+ */
|
|
|
609
|
+ private function findNodeName($nodes, $userId)
|
|
|
610
|
+ {
|
|
|
611
|
+ foreach ($nodes as $node) {
|
|
|
612
|
+ if ($node['user_id'] == $userId) {
|
|
|
613
|
+ return $node['name'];
|
|
|
614
|
+ }
|
|
|
615
|
+ }
|
|
|
616
|
+ return null;
|
|
|
617
|
+ }
|
|
|
618
|
+
|
|
|
619
|
+ /**
|
|
|
620
|
+ * 根据user_id查找节点
|
|
|
621
|
+ * @param array $nodes
|
|
|
622
|
+ * @param int $userId
|
|
|
623
|
+ * @return array|null
|
|
|
624
|
+ */
|
|
|
625
|
+ private function findNode($nodes, $userId)
|
|
|
626
|
+ {
|
|
|
627
|
+ foreach ($nodes as $node) {
|
|
|
628
|
+ if ($node['user_id'] == $userId) {
|
|
|
629
|
+ return $node;
|
|
|
630
|
+ }
|
|
|
631
|
+ }
|
|
|
632
|
+ return null;
|
|
|
633
|
+ }
|
|
523
|
634
|
}
|