1
0
Fork 0
party-rank-server/app/Admin/Services/CateRankService.php

55 lines
1.4 KiB
PHP

<?php
namespace App\Admin\Services;
use App\ModelFilters\CateRankFilter;
use App\Models\{CateRank, PartyCate};
class CateRankService extends BaseService
{
protected array $withRelationships = ['cate'];
protected string $modelName = CateRank::class;
protected string $modelFilterName = CateRankFilter::class;
public function listQuery()
{
$filter = $this->getModelFilter();
$query = $this->query();
if ($this->withRelationships) {
$query->with($this->withRelationships);
}
if ($filter) {
$query->filter(request()->input(), $filter);
}
return $query->sort();
}
public function list()
{
$sn = request('sn', '当前');
$items = [];
if ($sn == '当前') {
$items = PartyCate::withCount(['users'])->get()->map(fn($item) => [
'id' => $item->id,
'name' => $item->name,
'cate' => ['name' => $item->name],
'current_score' => $item->current_score,
'count' => $item->users_count,
'avg_score' => $item->users_count ? floor($item->current_score / $item->users_count) : 0,
])->sortByDesc('avg_score');
} else {
$query = $this->listQuery();
$items = (clone $query)->get();
}
return compact('items');
}
}