6
0
Fork 0

调整新会员统计

release
vine_liutk 2021-12-31 15:48:48 +08:00
parent ae3daf122f
commit 5fffcf0fc3
1 changed files with 38 additions and 13 deletions

View File

@ -2,6 +2,8 @@
namespace App\Admin\Metrics;
use App\Models\User;
use Carbon\Carbon;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
@ -40,30 +42,30 @@ class NewUsers extends Line
};
switch ($request->get('option')) {
case '365':
case '90':
// 卡片内容
$this->withContent(mt_rand(1000, 5000).'k');
$start_time = Carbon::now()->subDays(89)->startOfDay();
$end_time = Carbon::now()->endOfDay();
$this->withContent($this->getUserCount($start_time, $end_time).'人');
// 图表数据
$this->withChart(collect($generator(30))->toArray());
$this->withChart($this->getUserGenerator($start_time, $end_time, 90));
break;
case '30':
// 卡片内容
$this->withContent(mt_rand(400, 1000).'k');
$start_time = Carbon::now()->subDays(29)->startOfDay();
$end_time = Carbon::now()->endOfDay();
$this->withContent($this->getUserCount($start_time, $end_time).'人');
// 图表数据
$this->withChart(collect($generator(30))->toArray());
break;
case '28':
// 卡片内容
$this->withContent(mt_rand(400, 1000).'k');
// 图表数据
$this->withChart(collect($generator(28))->toArray());
$this->withChart($this->getUserGenerator($start_time, $end_time, 30));
break;
case '7':
default:
// 卡片内容
$this->withContent('89.2k');
$start_time = Carbon::now()->subDays(7)->startOfDay();
$end_time = Carbon::now()->endOfDay();
$this->withContent($this->getUserCount($start_time, $end_time).'人');
// 图表数据
$this->withChart([28, 40, 36, 52, 38, 60, 55]);
$this->withChart($this->getUserGenerator($start_time, $end_time, 7));
}
}
@ -86,6 +88,29 @@ class NewUsers extends Line
]);
}
/**
* 获取指定时间段用户数量
*/
public function getUserCount($start_time, $end_time)
{
return User::whereBetween('created_at', [$start_time, $end_time])->count();
}
public function getUserGenerator($start_time, $end_time, $days, $groupBy='d')
{
$list = User::selectRaw("COUNT(DISTINCT id) AS userNum, DATE_FORMAT(created_at,'%Y-%m-%d') dDay")
->whereBetween('created_at', [$start_time, $end_time])
->groupBy('dDay')->pluck('userNum', 'dDay')->toArray();
for ($i = 0; $i<=$days; $i++) {
$time = Carbon::now()->subDay($i)->toDateString();
if (!isset($list[Carbon::now()->subDay($i)->toDateString()])) {
$list[$time] = 0;
}
}
ksort($list);
return array_values($list);
}
/**
* 设置卡片内容.
*