60 lines
1.4 KiB
PHP
Executable File
60 lines
1.4 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\User;
|
|
|
|
final class CountUser
|
|
{
|
|
|
|
public function RegByDay($iDay)
|
|
{
|
|
if (!$iDay) {
|
|
$iDay = 0;
|
|
}
|
|
|
|
if ($iDay == 0) {
|
|
$sDateEnd = false;
|
|
} else {
|
|
$sDateEnd = date("Y-m-d");
|
|
}
|
|
|
|
$sDateStart = date("Y-m-d", strtotime("-$iDay day"));
|
|
|
|
return $this->RegGroupFrom($sDateStart, $sDateEnd);
|
|
}
|
|
|
|
public function RegByDate($sDateStart, $sDateEnd = false)
|
|
{
|
|
if (!$sDateEnd) {
|
|
$sDateEnd = false;
|
|
}
|
|
|
|
return $this->RegGroupFrom($sDateStart, $sDateEnd);
|
|
}
|
|
|
|
public function RegGroupFrom($sDateStart, $sDateEnd = false)
|
|
{
|
|
$query = User::selectRaw('COALESCE(`from`, "无") as `from`, COUNT(*) as count');
|
|
|
|
if ($sDateEnd) {
|
|
$query->whereDate('reg_date', '>=', $sDateStart)
|
|
->whereDate('reg_date', '<=', $sDateEnd);
|
|
} else {
|
|
$query->whereDate('reg_date', $sDateStart);
|
|
}
|
|
|
|
// 添加排序,按 user_count 降序排列
|
|
$aCountUser = $query->groupBy('from')
|
|
->orderBy('count', 'desc') // 根据用户数量降序排序
|
|
->get()
|
|
->toArray();
|
|
|
|
return $aCountUser;
|
|
|
|
}
|
|
|
|
}
|