递归查询团队总人数

main
ztt 8 months ago
parent 38137ef641
commit 2abf686316
  1. 97
      addons/shopro/controller/commission/Agent.php
  2. 9
      addons/shopro/controller/user/WalletLog.php
  3. 6
      application/common.php

@ -161,26 +161,16 @@ class Agent extends Commission
//好友总数 //好友总数
$son_total = 0; $son_total = 0;
$user_id = $user->id; $user_id = $user->id;
$son_total = UserModel::where('parent_user_id', $user_id)->count('id'); $son_total = UserModel::where('parent_user_id', $user_id)->count('id');
$list = UserModel::with('agent.levelInfo')->where('parent_user_id', $user_id)->select();
$team_total = UserModel::with(['parentUser' => function($query) use ($user_id) {
// $query->where('');
}])->where('parent_user_id', $user_id)->count('id');
$list = UserModel::with('agent.levelInfo')->where('id', $user_id)->select();
$result = [ $result = [
'team_total' => $team_total, 'team_total' => $this->runNextUserCount($this->getParent($user_id)),
'son_total' => $son_total ?? 0, 'son_total' => $son_total,
'list' => $list 'list' => $list
]; ];
foreach ($list as &$item) { foreach ($list as &$item) {
$item['nextUserCount'] = 0; $item['nextUserCount'] = $this->runNextUserCount($this->getParent($item['id']));
} }
$this->success('success', $result); $this->success('success', $result);
@ -188,7 +178,8 @@ class Agent extends Commission
} }
public function test() { public function test() {
$result = $this->runNextUser( $this->getTestData(1)); $son = $this->getTestData(1);
$result = $this->runNextUser($son);
$this->success('success', $result); $this->success('success', $result);
} }
@ -203,10 +194,10 @@ class Agent extends Commission
['id' => 2, 'pid' => 1], ['id' => 2, 'pid' => 1],
['id' => 3, 'pid' => 1], ['id' => 3, 'pid' => 1],
['id' => 4, 'pid' => 2], ['id' => 4, 'pid' => 2],
['id' => 5, 'pid' => 4], ['id' => 5, 'pid' => 3],
['id' => 6, 'pid' => 5], ['id' => 6, 'pid' => 1],
['id' => 7, 'pid' => 6], ['id' => 7, 'pid' => 6],
['id' => 8, 'pid' => 2], ['id' => 8, 'pid' => 1],
['id' => 9, 'pid' => 2], ['id' => 9, 'pid' => 2],
['id' => 10, 'pid' => 7], ['id' => 10, 'pid' => 7],
['id' => 11, 'pid' => 3], ['id' => 11, 'pid' => 3],
@ -224,25 +215,67 @@ class Agent extends Commission
/** /**
* 递归处理用户的下级人数 * 递归处理用户的下级人数
* @param $list * @param $list
* @param $pid * @param int $pid
* @return mixed * @return mixed
*/ */
public function runNextUser($user) { public function runNextUserCount($user) {
static $data = []; $count = 0;
$nextUser = $user; if (count($user) > 0) {
// return $nextUser; $count += count($user);
// $nextUserCount = count($this->getTestData($user_id)); }
foreach ($user as $item) {
$nexUserData = $this->getParent($item['id']);
if (is_array($nexUserData)) {
$this->runNextUser($nexUserData);
}
}
return $count;
}
foreach ($nextUser as $k => $v) { /**
// if($v['pid'] == $user_id) { * @param $user
// $data[] = $v; * @return array
$v['son'] = $this->runNextUser($v['id']); */
!empty($son)&& $v['son'] = $this->runNextUser($v['id']); public function runNextUser($user) {
$data[] = $v; $count = 0;
unset($nextUser[$k]); $data = [];
// } if (count($user) > 0) {
$count += count($user);
}
foreach ($user as $item) {
$nexUserData = $this->getParent($item['id']);
if (is_array($nexUserData)) {
$item['son'] = $this->runNextUser($nexUserData);
}
$data[] = $item;
}
return ['list'=> $data, 'count'=> $count];
}
function getTree($data, $pId)
{
$tree = [];
foreach($data as $v)
{
if(strcmp($v['condition'], $pId) == 0)
{
$v['children'] = $this->getTree($data, $v['task_id']);
$tree[] = $v;
}
}
return $tree;
}
function format($tree,$parent = [],$data = [])
{
foreach ($tree as $v) {
$tmp = $parent;
$tmp[] = $v['task_id'];
if (empty($v['children'])) {
$data[] = $tmp;
} else {
$data = $this->format($v['children'],$tmp,$data);
}
} }
return $data; return $data;
} }

@ -37,7 +37,12 @@ class WalletLog extends Common
$income = UserWalletLogModel::where('user_id', $user->id)->{$type}()->where('amount', '>', 0)->whereTime('createtime', 'between', $date)->sum('amount'); $income = UserWalletLogModel::where('user_id', $user->id)->{$type}()->where('amount', '>', 0)->whereTime('createtime', 'between', $date)->sum('amount');
$expense = UserWalletLogModel::where('user_id', $user->id)->{$type}()->where('amount', '<', 0)->whereTime('createtime', 'between', $date)->sum('amount'); $expense = UserWalletLogModel::where('user_id', $user->id)->{$type}()->where('amount', '<', 0)->whereTime('createtime', 'between', $date)->sum('amount');
$logs = UserWalletLogModel::where($where)->{$type}()->whereTime('createtime', 'between', $date)->order('createtime', 'desc')->paginate($list_rows); $logs = UserWalletLogModel::where($where)->{$type}()->whereTime('createtime', 'between', $date)->order('createtime', 'desc')->paginate($list_rows);
$this->success('获取成功', ['list' => $logs, 'income' => $income, 'expense' => $expense]); // dd($logs->toArray());
$data = $logs->toArray();
foreach ($data['data'] as &$row) {
$row['oper_type'] = UserModel::get($row['oper_id'])['nickname'] ?? '';
}
$this->success('获取成功', ['list' => $data, 'income' => $income, 'expense' => $expense]);
} }
/** /**
@ -85,7 +90,7 @@ class WalletLog extends Common
$this->error('未查到转让用户'); $this->error('未查到转让用户');
} }
if (!Sms::check($params['exchange_phone'], $params['code'], 'score')) { if (!Sms::check($user->mobile, $params['code'], 'score')) {
$this->error('验证码不正确'); $this->error('验证码不正确');
} }

@ -564,3 +564,9 @@ EOT;
function formatImage($url) { function formatImage($url) {
return request()->domain().$url; return request()->domain().$url;
} }
function dd($data) {
echo '<pre>';
print_r($data);
exit();
}

Loading…
Cancel
Save