Преглед изворни кода

feat(shareProsperity): 新增共富区部门功能

- 添加共富区路由配置和部门接口
- 实现团队用户数据访问层方法
- 添加递归获取下级用户ID和PV统计功能
- 创建团队管理控制器和业务逻辑处理
shichen пре 5 месеци
родитељ
комит
66d9948cc7

+ 182 - 0
app/common/dao/shared/SharedProsperityUserDao.php

@@ -38,4 +38,186 @@ class SharedProsperityUserDao extends BaseDao
38 38
         }
39 39
         return $entityList;
40 40
     }
41
+
42
+    /**
43
+     * 获取所有直推用户(不分页)
44
+     * @param int $userId
45
+     * @return array
46
+     * @author 史晨
47
+     * @date 2026/2/9 09:35
48
+     */
49
+    public function getDirectByUserId(int $userId): array
50
+    {
51
+        $list = [];
52
+        try {
53
+            /** @var SharedProsperityUser $model */
54
+            $model = $this->getModel();
55
+            $list = $model::getDB()
56
+                ->where('parent_id', $userId)
57
+                ->select()
58
+                ->toArray();
59
+        } catch (\Exception $e) {
60
+
61
+        }
62
+        return $list;
63
+    }
64
+
65
+    /**
66
+     * 递归获取团队所有下级用户ID(可选择是否包含自己)
67
+     * @param int $userId
68
+     * @param bool $includeSelf 是否包含自己
69
+     * @return int[]
70
+     */
71
+    public function getAllSubordinateUserIds(int $userId, bool $includeSelf = false): array
72
+    {
73
+        $allIds = [];
74
+        try {
75
+            /** @var SharedProsperityUser $model */
76
+            $model = $this->getModel();
77
+            $getRecursiveChildren = function ($parentId) use (&$getRecursiveChildren, $model) {
78
+                $children = $model::getDB()
79
+                    ->where('parent_id', $parentId)
80
+                    ->column('user_id');
81
+                $allChildren = [];
82
+                foreach ($children as $childId) {
83
+                    $allChildren[] = $childId;
84
+                    $allChildren = array_merge($allChildren, $getRecursiveChildren($childId));
85
+                }
86
+                return $allChildren;
87
+            };
88
+            $allIds = $getRecursiveChildren($userId);
89
+            if ($includeSelf) {
90
+                array_unshift($allIds, $userId);
91
+            }
92
+        } catch (\Exception $e) {
93
+            // 记录日志或忽略
94
+        }
95
+        return $allIds;
96
+    }
97
+
98
+    /**
99
+     * 获取团队所有下级用户PV总和(不包括自己)
100
+     * @param int $userId
101
+     * @return float
102
+     */
103
+    public function getTeamPvSum(int $userId): float
104
+    {
105
+        $userIds = $this->getAllSubordinateUserIds($userId, false);
106
+        if (empty($userIds)) {
107
+            return 0.0;
108
+        }
109
+        try {
110
+            /** @var SharedProsperityUser $model */
111
+            $model = $this->getModel();
112
+            $sum = $model::getDB()
113
+                ->whereIn('user_id', $userIds)
114
+                ->sum('pv');
115
+            return (float)$sum;
116
+        } catch (\Exception $e) {
117
+            return 0.0;
118
+        }
119
+    }
120
+
121
+    /**
122
+     * 获取团队所有下级用户PV总和(包括自己)
123
+     * @param int $userId
124
+     * @return float
125
+     */
126
+    public function getTeamPvSumWithSelf(int $userId): float
127
+    {
128
+        $userIds = $this->getAllSubordinateUserIds($userId, true);
129
+        if (empty($userIds)) {
130
+            return 0.0;
131
+        }
132
+        try {
133
+            /** @var SharedProsperityUser $model */
134
+            $model = $this->getModel();
135
+            $sum = $model::getDB()
136
+                ->whereIn('user_id', $userIds)
137
+                ->sum('pv');
138
+            return (float)$sum;
139
+        } catch (\Exception $e) {
140
+            return 0.0;
141
+        }
142
+    }
143
+
144
+    /**
145
+     * 获取团队所有下级用户实体列表(不包括自己)
146
+     * @param int $userId
147
+     * @return SharedProsperityUserEntity[]
148
+     */
149
+    public function getTeamUserEntities(int $userId): array
150
+    {
151
+        $userIds = $this->getAllSubordinateUserIds($userId, false);
152
+        if (empty($userIds)) {
153
+            return [];
154
+        }
155
+        return $this->getSharedProsperityUserEntityListByUserIdList($userIds);
156
+    }
157
+
158
+    /**
159
+     * 获取团队所有下级用户实体列表(包括自己)
160
+     * @param int $userId
161
+     * @return SharedProsperityUserEntity[]
162
+     */
163
+    public function getTeamUserEntitiesWithSelf(int $userId): array
164
+    {
165
+        $userIds = $this->getAllSubordinateUserIds($userId, true);
166
+        if (empty($userIds)) {
167
+            return [];
168
+        }
169
+        return $this->getSharedProsperityUserEntityListByUserIdList($userIds);
170
+    }
171
+
172
+    /**
173
+     * 获取团队所有下级用户(包括分页,不包括自己)
174
+     * @param int $userId
175
+     * @param int $page
176
+     * @param int $pageSize
177
+     * @return array
178
+     */
179
+    public function getTeamUsers(int $userId, int $page = 1, int $pageSize = 10): array
180
+    {
181
+        $userIds = $this->getAllSubordinateUserIds($userId, false);
182
+        if (empty($userIds)) {
183
+            return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
184
+        }
185
+        try {
186
+            /** @var SharedProsperityUser $model */
187
+            $model = $this->getModel();
188
+            $list = $model::getDB()
189
+                ->whereIn('user_id', $userIds)
190
+                ->paginate(['page' => $page, 'list_rows' => $pageSize])
191
+                ->toArray();
192
+            return $list;
193
+        } catch (\Exception $e) {
194
+            return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
195
+        }
196
+    }
197
+
198
+    /**
199
+     * 获取团队所有下级用户(包括分页,包括自己)
200
+     * @param int $userId
201
+     * @param int $page
202
+     * @param int $pageSize
203
+     * @return array
204
+     */
205
+    public function getTeamUsersWithSelf(int $userId, int $page = 1, int $pageSize = 10): array
206
+    {
207
+        $userIds = $this->getAllSubordinateUserIds($userId, true);
208
+        if (empty($userIds)) {
209
+            return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
210
+        }
211
+        try {
212
+            /** @var SharedProsperityUser $model */
213
+            $model = $this->getModel();
214
+            $list = $model::getDB()
215
+                ->whereIn('user_id', $userIds)
216
+                ->paginate(['page' => $page, 'list_rows' => $pageSize])
217
+                ->toArray();
218
+            return $list;
219
+        } catch (\Exception $e) {
220
+            return ['data' => [], 'total' => 0, 'per_page' => $pageSize, 'current_page' => $page, 'last_page' => 1];
221
+        }
222
+    }
41 223
 }

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

@@ -3,6 +3,7 @@
3 3
 namespace app\common\repositories\shared;
4 4
 
5 5
 use app\common\dao\shared\SharedProsperityUserDao;
6
+use app\common\dao\user\UserDao;
6 7
 use app\common\repositories\BaseRepository;
7 8
 use app\entity\data\shared\SharedProsperityUserEntity;
8 9
 use think\db\exception\DbException;
@@ -115,4 +116,111 @@ class SharedProsperityUserRepository extends BaseRepository
115 116
         }
116 117
         return true;
117 118
     }
119
+
120
+    /**
121
+     * 获取直推用户列表,并计算每个直推用户的团队总人数和团队总业绩(包含自己)
122
+     * 返回三个列表:全部直推、大部门(团队业绩最大的直推用户)、小部门(其余直推用户)
123
+     * 同时返回统计数据:我的部门总人数、总业绩、小部门业绩总和、大部门业绩总和
124
+     * @param int $userId
125
+     * @return array
126
+     */
127
+    public function getTeam(int $userId): array
128
+    {
129
+        // 获取直推用户
130
+        $list = $this->dao->getDirectByUserId($userId);
131
+        if (empty($list)) {
132
+            // 没有直推用户,仍然返回统计数据(我的部门总人数和总业绩)
133
+            $myTeamUserIds = $this->dao->getAllSubordinateUserIds($userId, true);
134
+            $myTeamTotalCount = count($myTeamUserIds);
135
+            $myTeamPvSum = $this->dao->getTeamPvSumWithSelf($userId);
136
+            return [
137
+                'all_direct' => [],
138
+                'big_department' => [],
139
+                'small_department' => [],
140
+                'stats' => [
141
+                    'my_team_total_count' => $myTeamTotalCount,
142
+                    'my_team_pv_sum' => $myTeamPvSum,
143
+                    'small_department_pv_sum' => 0,
144
+                    'big_department_pv_sum' => 0,
145
+                ]
146
+            ];
147
+        }
148
+        // 收集所有直推用户ID
149
+        $userIds = array_column($list, 'user_id');
150
+        // 获取用户详细信息(昵称、头像、注册时间)
151
+        /** @var UserDao $userDao */
152
+        $userDao = app()->make(UserDao::class);
153
+        $userEntities = $userDao->getUserEntityListByUidList($userIds);
154
+        $userMap = [];
155
+        foreach ($userEntities as $userEntity) {
156
+            $userMap[$userEntity->getUid()] = [
157
+                'nickname' => $userEntity->getNickname(),
158
+                'avatar' => $userEntity->getAvatar(),
159
+                'create_time' => $userEntity->getCreateTime(),
160
+            ];
161
+        }
162
+        // 计算每个直推用户的团队总人数和团队总业绩
163
+        $maxPv = 0;
164
+        $maxPvIndex = -1;
165
+        foreach ($list as $index => &$item) {
166
+            $teamUserIds = $this->dao->getAllSubordinateUserIds($item['user_id'], true);
167
+            $teamTotalCount = count($teamUserIds);
168
+            $teamPvSum = $this->dao->getTeamPvSumWithSelf($item['user_id']);
169
+            $item['team_total_count'] = $teamTotalCount;
170
+            $item['team_pv_sum'] = $teamPvSum;
171
+            // 补充用户信息
172
+            if (isset($userMap[$item['user_id']])) {
173
+                $item['nickname'] = $userMap[$item['user_id']]['nickname'];
174
+                $item['avatar'] = $userMap[$item['user_id']]['avatar'];
175
+                $item['create_time'] = $userMap[$item['user_id']]['create_time'];
176
+            } else {
177
+                $item['nickname'] = '';
178
+                $item['avatar'] = '';
179
+                $item['create_time'] = '';
180
+            }
181
+            // 找出团队业绩最大的直推用户
182
+            if ($teamPvSum > $maxPv) {
183
+                $maxPv = $teamPvSum;
184
+                $maxPvIndex = $index;
185
+            }
186
+        }
187
+        unset($item);
188
+        // 分割大部门和小部门
189
+        $bigDepartment = [];
190
+        $smallDepartment = [];
191
+        if ($maxPvIndex >= 0) {
192
+            $bigDepartment[] = $list[$maxPvIndex];
193
+            foreach ($list as $index => $item) {
194
+                if ($index !== $maxPvIndex) {
195
+                    $smallDepartment[] = $item;
196
+                }
197
+            }
198
+        } else {
199
+            // 没有直推用户(理论上不会发生)
200
+            $smallDepartment = $list;
201
+        }
202
+        // 计算统计数据
203
+        $myTeamUserIds = $this->dao->getAllSubordinateUserIds($userId, true);
204
+        $myTeamTotalCount = count($myTeamUserIds);
205
+        $myTeamPvSum = $this->dao->getTeamPvSumWithSelf($userId);
206
+        $smallDepartmentPvSum = 0;
207
+        foreach ($smallDepartment as $item) {
208
+            $smallDepartmentPvSum += $item['team_pv_sum'];
209
+        }
210
+        $bigDepartmentPvSum = 0;
211
+        foreach ($bigDepartment as $item) {
212
+            $bigDepartmentPvSum += $item['team_pv_sum'];
213
+        }
214
+        return [
215
+            'all_direct' => $list,
216
+            'big_department' => $bigDepartment,
217
+            'small_department' => $smallDepartment,
218
+            'stats' => [
219
+                'my_team_total_count' => $myTeamTotalCount,
220
+                'my_team_pv_sum' => $myTeamPvSum,
221
+                'small_department_pv_sum' => $smallDepartmentPvSum,
222
+                'big_department_pv_sum' => $bigDepartmentPvSum,
223
+            ]
224
+        ];
225
+    }
118 226
 }

+ 18 - 0
app/controller/api/shareProsperity/Team.php

@@ -0,0 +1,18 @@
1
+<?php
2
+
3
+namespace app\controller\api\shareProsperity;
4
+
5
+use app\common\model\shared\SharedProsperityUser;
6
+use app\common\repositories\shared\SharedProsperityUserRepository;
7
+
8
+class Team extends BaseController
9
+{
10
+    public function list()
11
+    {
12
+        $userId = $this->request->uid();
13
+        /** @var SharedProsperityUserRepository $repository */
14
+        $repository = app()->make(SharedProsperityUserRepository::class);
15
+        $list = $repository->getTeam($userId);
16
+        return app('json')->success($list);
17
+    }
18
+}

+ 6 - 0
route/api.php

@@ -350,6 +350,12 @@ Route::group('api/', function () {
350 350
 
351 351
         })->prefix('api.user.');
352 352
 
353
+        // 共富区
354
+        Route::group('shareProsperity', function () {
355
+            // 部门
356
+            Route::get('get_share_prosperity_team_list', 'Team/list');
357
+        })->prefix('api.shareProsperity.');
358
+
353 359
         //购物车
354 360
         Route::group('user/cart', function () {
355 361
             Route::get('/lst', 'StoreCart/lst');