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