getCount($where, $userMap); $list = $userDao->getPageList($page, $limit, $where, $sort, $userMap); foreach ($list as &$item) { //团队会员(包括1、2级和自身)的积分账户合计、余额账户合计 // 计算团队成员(包括1、2级和自身)ID $teamIds = $this->getTeamMoneyAndScore($item['id']); // 积分账户合计 $item['score_total'] = $teamIds['score']; // 余额账户合计 $item['money_total'] = $teamIds['money']; } $data = [ 'code' => 0, 'msg' => Db::getLastSql(), 'count' => $count, 'data' => $list, ]; return json($data); } /** * 获取团队 * @param $uid */ private function getTeamMoneyAndScore($uid) { $cacheKey = 'userLogicTeamIds_' . $uid; if (!Cache::has($cacheKey)) { // 自身 $user = Db::table(User::$table) ->where('id', $uid) ->field(['id', 'score', 'money']) ->find(); $totalScore = $user['score']; $totalMoney = $user['money']; // 一级子用户 Db::table(User::$table) ->where('pid', $uid) ->field(['id', 'score', 'money']) ->chunk(100, function ($users) use (&$totalScore, &$totalMoney) { foreach ($users as $user) { $uids[] = $user['id']; $totalScore += $user['score']; $totalMoney += $user['money']; } // 二级子用户 Db::table(User::$table) ->whereIn('pid', $uids) ->field(['id', 'score', 'money']) ->chunk(100, function ($users) use (&$totalScore, &$totalMoney) { foreach ($users as $user) { $totalScore += $user['score']; $totalMoney += $user['money']; } }); }, ['score', 'money']); Cache::set($cacheKey, ['score' => $totalScore, 'money' => sprintf("%.2f", $totalMoney)], 5 * 60); } return Cache::get($cacheKey); } /** * 修改用户手机号码 * @param mixed $uid * @param mixed $phone */ public function modifyPhone($uid, $phone) { // 判断手机号码是否使用过 $user = User::getUserById($uid); if ($user['mobile'] == $phone) { return "请输入新的手机号码"; } $user = User::getUserByMobile($phone); if ($user) { return "该手机号码已注册"; } // 修改用户信息 try { $result = User::ModifyMobile($uid, $phone); if (!$result) { return "更新手机号码失败,请稍后重试"; } } catch (\Exception $e) { return '失败' . $e->getMessage(); } return true; } /** * 修改用户所属上级 * @param mixed $uid * @param mixed $pid */ public function modifypid($uid, $pid) { // 查询pid是否存在用户path中 $user = User::getUserById($uid); if ($uid == $pid) { return "不能修改自己为上级"; } if ($user['pid'] == $pid) { return "该用户已所属待变更的上级,不能修改"; } // 当前用户关系层级链路 $newUserPath = ""; if ($pid == 0) { $newPathPrefix = $user['id']; } else { $pUser = User::getUserById($pid); if (empty($pUser)) { return "待变更的上级用户不存在"; } $userPathArr = explode(',', $user['path']); if (in_array($pid, $userPathArr)) { $pidIndex = array_search($pid, $userPathArr); $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex + 1); } else { $pidIndex = array_search($user['pid'], $userPathArr); $userPathArrPrefix = array_slice($userPathArr, 0, $pidIndex); $userPathArrPrefix[] = $pid; } $newUserPath = implode(',', $userPathArrPrefix); // 子级用户Path待替换关系层级前部分链表 $userPathArrPrefix[] = $user['id']; $newPathPrefix = implode(',', $userPathArrPrefix); } // 子级用户Path被替换关系层级前部分链表 $oldPathPrefix = $user['path'] == 0 ? $uid : $user['path'] . ',' . $uid; Db::startTrans(); try { // 子级path变更 $result = Db::table(User::$table) ->where([ ['path', 'like', $oldPathPrefix . '%'], ['id', '<>', $uid] ]) ->field(['id', 'path']) ->chunk(100, function ($users) use ($pid, $oldPathPrefix, $newPathPrefix, $uid) { foreach ($users as $user) { $newPath = str_replace($oldPathPrefix, $newPathPrefix, $user['path']); User::modifyUserPath($user['id'], $newPath); } }); if (!$result) { Db::rollback(); return "修改用户所属上级失败,请确认用户的层级关系"; } // 更新用户Pid,Path User::modifyUserPidAndPath($uid, $pid, $newUserPath); Db::commit(); } catch (\Exception $exception) { Db::rollback(); return "失败:" . $exception->getMessage(); } return true; } /** * 修改余额 * @param mixed $post */ public function ModifyMoney($post) { $user = User::getUserById($post['uid']); if (empty($user)) { return "用户不存在"; } Db::startTrans(); try { if ($post['state'] == 1) { $afterMoney = $user['money'] + $post['money']; } else { $afterMoney = $user['money'] - $post['money']; if ($afterMoney < 0) { return "账号余额不足"; } } $afterMoney = round($afterMoney, 2); $moneyLog = [ 'uid' => $post['uid'], 'type' => $post['type'] ?? 0, 'money' => $post['money'], 'create_at' => date('Y-m-d H:i:s'), 'state' => $post['state'] ?? '0', 'from_id' => $post['from_id'] ?? '0', 'before_money' => $user['money'], 'after_money' => $afterMoney, 'uid2' => $post['uid2'] ?? 0, 'free_type' => $post['free_type'] ?? '', 'remark' => $post['remark'], ]; // 子级path变更 MoneyLog::AddMoneyLog($moneyLog); User::UpdateUserMoney($user['id'], $afterMoney); Db::commit(); } catch (\Exception $exception) { Db::rollback(); return "失败:" . $exception->getMessage(); } return true; } /** * 修改积分 * @param mixed $post */ public function ModifyScore($post) { $user = User::getUserById($post['uid']); if (empty($user)) { return "用户不存在"; } Db::startTrans(); try { if ($post['state'] == 1) { $afterScore = $user['score'] + $post['score']; } else { $afterScore = $user['score'] - $post['score']; if ($afterScore < 0) { return "账号积分不足"; } } $moneyLog = [ 'uid' => $post['uid'], 'type' => $post['type'] ?? 0, 'score' => $post['score'], 'create_at' => date('Y-m-d H:i:s'), 'scene' => $post['scene'] ?? 0, 'from_id' => $post['from_id'] ?? '0', 'state' => $post['state'] ?? '0', 'before_score' => $user['score'], 'after_score' => $afterScore, 'uid2' => $post['uid2'] ?? 0, ]; // 子级path变更 ScoreLog::AddScoreLog($moneyLog); User::UpdateUserScore($user['id'], $afterScore); Db::commit(); } catch (\Exception $exception) { Db::rollback(); return "失败:" . $exception->getMessage(); } return true; } }