feature: main page - code be
This commit is contained in:
@@ -21,8 +21,20 @@ class UserController extends Controller
|
||||
$user = Auth::user();
|
||||
|
||||
$administrations = $this->userService->getDashboard($user, $selectedMonth);
|
||||
$stats = $this->userService->getPersonalStats($user, $selectedMonth);
|
||||
|
||||
return view('user.dashboard', compact('administrations', 'selectedMonth', 'user'));
|
||||
$receivedRanking = $this->userService->getRankingList('received', $selectedMonth, $user);
|
||||
$sentRanking = $this->userService->getRankingList('sent', $selectedMonth, $user);
|
||||
|
||||
return view('user.dashboard', array_merge([
|
||||
'administrations' => $administrations,
|
||||
'selectedMonth' => $selectedMonth,
|
||||
'user' => $user,
|
||||
'receivedRankers' => $receivedRanking['rankers'],
|
||||
'receivedCurrentUserRankItem' => $receivedRanking['currentUserRankItem'],
|
||||
'sentRankers' => $sentRanking['rankers'],
|
||||
'sentCurrentUserRankItem' => $sentRanking['currentUserRankItem']
|
||||
], $stats));
|
||||
}
|
||||
|
||||
public function sendThankcards()
|
||||
|
||||
@@ -23,4 +23,14 @@ class Administration extends Model
|
||||
'receiver',
|
||||
'date',
|
||||
];
|
||||
|
||||
public function senderUser()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'sender', 'msnv');
|
||||
}
|
||||
|
||||
public function receiverUser()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'receiver', 'msnv');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,4 +69,14 @@ class User extends Authenticatable
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
public function transactions()
|
||||
{
|
||||
return $this->hasMany(Administration::class, 'msnv', 'msnv');
|
||||
}
|
||||
|
||||
public function cardPurchases()
|
||||
{
|
||||
return $this->hasMany(AddCard::class, 'buyer', 'msnv');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,4 +14,8 @@ interface UserServiceInterface
|
||||
public function sendThankcards(User $sender, string $receiverMsnv, int $amount): void;
|
||||
|
||||
public function updatePassword(User $user, string $newPassword): void;
|
||||
|
||||
public function getPersonalStats(User $user, string $selectedMonth): array;
|
||||
|
||||
public function getRankingList(string $type, string $selectedMonth, User $currentUser): array;
|
||||
}
|
||||
@@ -94,4 +94,102 @@ class UserService implements UserServiceInterface
|
||||
$user->first_login = User::FIRST_LOGIN_FALSE;
|
||||
$user->save();
|
||||
}
|
||||
|
||||
public function getPersonalStats(User $user, string $selectedMonth): array
|
||||
{
|
||||
$startOfMonth = Carbon::parse($selectedMonth)->startOfMonth();
|
||||
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth();
|
||||
|
||||
$receivedCount = (int) Administration::where('msnv', $user->msnv)
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->sum('received');
|
||||
|
||||
$sentCount = (int) Administration::where('msnv', $user->msnv)
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
->sum('sent');
|
||||
|
||||
// Calculate current rank (based on received)
|
||||
$rankings = User::where('status', User::STATUS_ACTIVE)
|
||||
->addSelect([
|
||||
'score' => Administration::selectRaw('COALESCE(SUM(received), 0)')
|
||||
->whereColumn('msnv', 'user.msnv')
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
])
|
||||
->orderByDesc('score')
|
||||
->get();
|
||||
|
||||
$currentRank = 0;
|
||||
$prevScore = null;
|
||||
$rank = 1;
|
||||
$denseRank = 0;
|
||||
|
||||
foreach ($rankings as $item) {
|
||||
$score = (int) $item->score;
|
||||
if ($score !== $prevScore) {
|
||||
$denseRank = $rank;
|
||||
$prevScore = $score;
|
||||
}
|
||||
|
||||
if ($item->msnv == $user->msnv) {
|
||||
$currentRank = $denseRank;
|
||||
break;
|
||||
}
|
||||
$rank++;
|
||||
}
|
||||
|
||||
return [
|
||||
'receivedCount' => $receivedCount,
|
||||
'sentCount' => $sentCount,
|
||||
'currentRank' => $currentRank
|
||||
];
|
||||
}
|
||||
|
||||
public function getRankingList(string $type, string $selectedMonth, User $currentUser): array
|
||||
{
|
||||
$startOfMonth = Carbon::parse($selectedMonth)->startOfMonth();
|
||||
$endOfMonth = Carbon::parse($selectedMonth)->endOfMonth();
|
||||
|
||||
$column = $type === 'sent' ? 'sent' : 'received';
|
||||
|
||||
$users = User::where('status', User::STATUS_ACTIVE)
|
||||
->addSelect([
|
||||
'score' => Administration::selectRaw('COALESCE(SUM(' . $column . '), 0)')
|
||||
->whereColumn('msnv', 'user.msnv')
|
||||
->whereBetween('date', [$startOfMonth, $endOfMonth])
|
||||
])
|
||||
->orderByDesc('score')
|
||||
->get();
|
||||
|
||||
$prevScore = null;
|
||||
$rank = 1;
|
||||
$denseRank = 0;
|
||||
|
||||
$rankers = [];
|
||||
$currentUserRankItem = null;
|
||||
|
||||
foreach ($users as $index => $userItem) {
|
||||
$score = (int) $userItem->score;
|
||||
if ($score !== $prevScore) {
|
||||
$denseRank = $rank;
|
||||
$prevScore = $score;
|
||||
}
|
||||
|
||||
$userItem->rank = $denseRank;
|
||||
$userItem->score = $score;
|
||||
|
||||
if ($userItem->msnv == $currentUser->msnv) {
|
||||
$currentUserRankItem = $userItem;
|
||||
}
|
||||
|
||||
if ($index < 5) {
|
||||
$rankers[] = $userItem;
|
||||
}
|
||||
$rank++;
|
||||
}
|
||||
|
||||
return [
|
||||
'rankers' => $rankers,
|
||||
'currentUserRankItem' => $currentUserRankItem
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user