|
|
<?php
namespace App\AdminAgent\Metrics\Examples;
use App\Common\DataTime;use App\Models\User;use Carbon\Carbon;use Dcat\Admin\Admin;use Dcat\Admin\Widgets\Metrics\Line;use Illuminate\Http\Request;use Illuminate\Support\Arr;use Illuminate\Support\Facades\DB;
class NewUsers extends Line{ /** * 初始化卡片内容 * * @return void */ protected function init() { parent::init();
$this->title('新增用户'); $this->dropdown([ //'day' => '本日',
'week' => '本周', 'month' => '本月', //'year' => '今年',
]); }
/** * 处理请求 * * @param Request $request * * @return mixed|void */ public function handle(Request $request) { $count = User::query()->where('agent_id', Admin::user()->id); $user = User::query() ->select('*') ->addSelect(DB::raw("COUNT(id) as count_id,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time")); switch ($request->get('option')) { case 'day': $count->whereDate('created_at', Carbon::today()); $user->whereDate('created_at', Carbon::today()); break; case 'week': $time = DataTime::beginAndEnd('week'); $count->whereBetween('created_at', $time); $user->whereBetween('created_at', $time); break; case 'month': $count->whereMonth('created_at', Carbon::now()->month); $user->whereMonth('created_at', Carbon::now()->month); break; case 'year': $count->whereYear('created_at', Carbon::now()->year); $user->whereYear('created_at', Carbon::now()->year); break; default: $time = DataTime::beginAndEnd('week'); $count->whereBetween('created_at', $time); $user->whereBetween('created_at', $time); } $user = $user->where('agent_id', Admin::user()->id) ->limit(30) ->groupBy('statistics_time') ->get() ->toArray(); $count = $count->count(); $this->withContent($count); // 图表数据
$this->chartLabels(Arr::pluck($user, 'statistics_time')); $this->withChart(Arr::pluck($user, 'count_id')); }
/** * 设置图表数据. * * @param array $data * * @return $this */ public function withChart(array $data) { return $this->chart([ 'series' => [ [ 'name' => $this->title, 'data' => $data, ], ], ]); }
/** * 设置卡片内容. * * @param string $content * * @return $this */ public function withContent($content) { return $this->content( <<<HTML<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px"> <h2 class="ml-1 font-lg-1">{$content}</h2> <span class="mb-0 mr-1 text-80">{$this->title}</span></div>HTML ); }}
|