feature: main page - always show current user at the bottom of ranking

This commit is contained in:
antv
2026-07-06 16:54:41 +07:00
parent e163d2f349
commit 1e050233b9
3 changed files with 92 additions and 52 deletions
+12 -10
View File
@@ -156,14 +156,15 @@ class UserService implements UserServiceInterface
$column = $type === 'sent' ? 'sent' : 'received';
$users = User::where('status', User::STATUS_ACTIVE)
$allActiveUsers = User::where('status', User::STATUS_ACTIVE)
->addSelect([
'score' => Administration::selectRaw('COALESCE(SUM(' . $column . '), 0)')
->whereColumn('msnv', 'user.msnv')
->whereBetween('date', [$startOfMonth, $endOfMonth])
])
->get()
->filter(fn($u) => $u->score > 0)
->get();
$rankedUsers = $allActiveUsers->filter(fn($u) => $u->score > 0)
->sortBy([
['score', 'desc'],
['msnv', 'asc']
@@ -177,7 +178,7 @@ class UserService implements UserServiceInterface
$rankers = [];
$currentUserRankItem = null;
foreach ($users as $index => $userItem) {
foreach ($rankedUsers as $index => $userItem) {
$score = (int) $userItem->score;
if ($score !== $prevScore) {
$denseRank = $rank;
@@ -197,17 +198,18 @@ class UserService implements UserServiceInterface
$rank++;
}
$showCurrentUserAtBottom = false;
if ($currentUserRankItem) {
$currentUserIndex = $users->search(fn($u) => $u->msnv == $currentUser->msnv);
if ($currentUserIndex !== false && $currentUserIndex >= 4) {
$showCurrentUserAtBottom = true;
if (!$currentUserRankItem) {
$currentUserModel = $allActiveUsers->first(fn($u) => $u->msnv == $currentUser->msnv);
if ($currentUserModel) {
$currentUserRankItem = clone $currentUserModel;
$currentUserRankItem->score = 0;
$currentUserRankItem->rank = 0;
}
}
return [
'rankers' => $rankers,
'currentUserRankItem' => $showCurrentUserAtBottom ? $currentUserRankItem : null
'currentUserRankItem' => $currentUserRankItem
];
}
}