Selaa lähdekoodia

Merge branch 'dev-sc-260204'

shichen 5 kuukautta sitten
vanhempi
commit
c24e547f67

+ 111 - 0
app/common/repositories/shared/SharedProsperityUserRepository.php

@@ -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
 }

+ 9 - 4
app/controller/api/Test.php

@@ -1,6 +1,7 @@
1 1
 <?php
2 2
 
3 3
 namespace app\controller\api;
4
+use app\common\repositories\shared\SharedProsperityUserRepository;
4 5
 use app\common\repositories\user\UserRepository;
5 6
 use app\traits\GzcApiRequest;
6 7
 
@@ -8,11 +9,15 @@ class Test
8 9
 {
9 10
     public function test()
10 11
     {
11
-        $field = request()->param('field');
12
-        /** @var UserRepository $userRepository */
13
-        $userRepository = app()->make(UserRepository::class);
14
-        $list = $userRepository->getUserForTree($field);
12
+        /** @var SharedProsperityUserRepository $userRepository */
13
+        $userRepository = app()->make(SharedProsperityUserRepository::class);
14
+        $list = $userRepository->getUserTree(43);
15 15
         var_dump(json_encode($list));
16
+        // $field = request()->param('field');
17
+        // /** @var UserRepository $userRepository */
18
+        // $userRepository = app()->make(UserRepository::class);
19
+        // $list = $userRepository->getUserForTree($field);
20
+        // var_dump(json_encode($list));
16 21
         // return app('json')->success($list);
17 22
     }
18 23
 }