Explorar el Código

用户团队贡献值统计

xialei hace 1 año
padre
commit
bca61d8cd2
Se han modificado 1 ficheros con 101 adiciones y 13 borrados
  1. 101 13
      app/controller/api/user/User.php

+ 101 - 13
app/controller/api/user/User.php

@@ -231,6 +231,75 @@ class User extends BaseController
231
         log::info("=======date==={$data}");
231
         log::info("=======date==={$data}");
232
         return app("json")->success($data);
232
         return app("json")->success($data);
233
     }
233
     }
234
+
235
+
236
+    public function buildTreeContribute($users, $parentId = 0) {
237
+        $branch = array();
238
+        foreach ($users as $user) {
239
+            if ($user['spread_uid'] == $parentId) {
240
+                $children = $this->buildTreeContribute($users, $user['uid']); // 递归查找子节点
241
+                if ($children) {
242
+                    $user['children'] = $children; // 如果有子节点,添加到当前节点
243
+                }
244
+                $user['total_contribute'] = $user['contribute_sum']; // 初始化当前节点的总余额为自身余额
245
+                if ($children) { // 如果存在子节点,计算总余额
246
+                    foreach ($children as $child) {
247
+                        $user['total_contribute'] += $child['total_contribute']; // 累加子节点的总余额
248
+                    }
249
+                }
250
+                $branch[] = $user; // 将当前节点添加到树中
251
+            }
252
+        }
253
+        return $branch; // 返回当前分支的所有节点
254
+    }
255
+
256
+    // 根据父级uid,递归获取所有子级UID数组 默认深度10级
257
+    public function getRecursiveChildrenChengji($parentUid, $number = 10) {
258
+        $user = Db::name('user')->where('uid', $parentUid)->field('uid,spread_uid,contribute,per_contribute')->find();
259
+        $user['contribute_sum'] = bcadd($user['contribute'], $user['per_contribute'], 2);
260
+        $uids = [];// 用于存储符合条件的子级 uid
261
+        $result = [$user]; // 用于存储符合条件的子级数据
262
+
263
+        // 定义递归函数
264
+        $num = 0;//当前层级
265
+        $recursion = function ($parentUid, $number, $num) use (&$uids, &$result, &$recursion) {
266
+            if($num >= $number){ 
267
+                return;
268
+            }
269
+
270
+            // 查询当前父级的所有直接子级
271
+            $children = Db::name('user')->field('uid,spread_uid,contribute,per_contribute')->where("spread_uid", $parentUid)->select()->toArray();
272
+            // 如果没有子级,直接返回
273
+            if (!count($children) && count($children) < 1) {
274
+                return;
275
+            }else{
276
+                $num = $num + 1;
277
+            }
278
+
279
+            // 遍历每个子级
280
+            foreach ($children as $childUid) {
281
+                //如果有重复的就跳过 防止死循环
282
+                if(in_array($childUid['uid'], $uids)){
283
+                    continue;
284
+                }
285
+                // 将符合条件的子级 uid 添加到结果数组
286
+                $uids[] = $childUid['uid'];
287
+
288
+                // 将符合条件的子级 uid 添加到结果数组
289
+                $childUid['contribute_sum'] = bcadd($childUid['contribute'], $childUid['per_contribute'], 2);
290
+                $result[] = $childUid;
291
+
292
+                // 递归查询子级的子级
293
+                $recursion($childUid['uid'], $number, $num);
294
+            }
295
+        };
296
+
297
+        // 从初始父级开始递归查询
298
+        $recursion($parentUid, $number, $num);
299
+
300
+        return $result; // 返回结果数组,并去重
301
+    }
302
+
234
     //贡献值
303
     //贡献值
235
 
304
 
236
     public function  gongxian_info(){
305
     public function  gongxian_info(){
@@ -245,33 +314,52 @@ class User extends BaseController
245
             //     $totalcon += $v['number'];
314
             //     $totalcon += $v['number'];
246
             // }
315
             // }
247
             //已经入账贡献值
316
             //已经入账贡献值
248
-            if( $con['user_group'] <= 2 ){
249
-                $data['total_source'] =  $con['contribute'] + $con['per_contribute'];
250
-            }else{
251
-                $data['total_source'] =  $con['contribute'];
252
-            }
317
+            // if( $con['user_group'] <= 2 ){
318
+            //     $data['total_source'] =  $con['contribute'] + $con['per_contribute'];
319
+            // }else{
320
+            $data['total_source'] =  $con['contribute'];
321
+            // }
253
            
322
            
254
             
323
             
255
            // $getteamscon = $this->getPerformance($user->uid);
324
            // $getteamscon = $this->getPerformance($user->uid);
256
 
325
 
257
             //$getcon = $this->getPerformance_getcon( $user->uid);
326
             //$getcon = $this->getPerformance_getcon( $user->uid);
258
 
327
 
259
-            $team_userinfo_old_jbp22 = $this->getPerformance22($user->uid);
328
+            // $team_userinfo_old_jbp22 = $this->getPerformance22($user->uid);
260
 
329
 
261
-            //我的团队贡献值
262
-            if( $con['user_group'] <= 2 ){
263
-                $data['team_gongxian_num'] =  $team_userinfo_old_jbp22['person_nums_pv'] - $con['per_contribute'];
330
+            $user_id = $con['uid'];
331
+            $user_group = $con['user_group'];
332
+            $number = 10;
333
+            if($user_group <= 1){
334
+                $number = 1;
335
+            }
336
+            //如果下级团队只有一个人 那就不用再查了 为0
337
+            //如果用户等级为用户身份 只查一级直推
338
+            //去除掉贡献值最大的下级
339
+            $child_nums = $this->getRecursiveChildrenChengji($user_id, $number);
340
+            $child_nums_Tree = $this->buildTreeContribute($child_nums);
341
+            if (empty($child_nums_Tree)){
342
+                $child_nums_Tree = $child_nums;
343
+                $total_contribute = $child_nums_Tree[0]['per_contribute'];
264
             }else{
344
             }else{
265
-                $data['team_gongxian_num'] = $team_userinfo_old_jbp22['person_nums_pv'];
345
+                // 找到贡献值最大的用户的索引
346
+                $maxBalanceIndex = array_search(max(array_column($child_nums_Tree[0]['children'], 'total_contribute')), array_column($child_nums_Tree, 'total_contribute'));
347
+                // 减去该用户的贡献值
348
+                $total_contribute = bcsub($child_nums_Tree[0]['total_contribute'], $child_nums_Tree[0]['children'][$maxBalanceIndex]['total_contribute'], 2);
266
             }
349
             }
350
+            $data['team_gongxian_num'] = $total_contribute;
267
 
351
 
268
-           
352
+            //我的团队贡献值
353
+            // if( $con['user_group'] <= 2 ){
354
+            //     $data['team_gongxian_num'] = $team_userinfo_old_jbp22['person_nums_pv'] - $con['per_contribute'];
355
+            // }else{
356
+            //     $data['team_gongxian_num'] = $team_userinfo_old_jbp22['person_nums_pv'];
357
+            // }
269
 
358
 
270
-            //为生贡献值
359
+            //未生效贡献值
271
             $data['no_total_source']=Db::name('user_sign_gongxian')->where('status', -1)->where('uid', $user->uid)->sum('number');
360
             $data['no_total_source']=Db::name('user_sign_gongxian')->where('status', -1)->where('uid', $user->uid)->sum('number');
272
             log::info("====spread_info_yhc=={$data['total_source']}======={$data['no_total_source']}");
361
             log::info("====spread_info_yhc=={$data['total_source']}======={$data['no_total_source']}");
273
             return app('json')->success($data);
362
             return app('json')->success($data);
274
-
275
         }
363
         }
276
 
364
 
277
     }
365
     }