title('消费金币'); $this->height(300); $this->dropdown([ '1' => '当日新增消费金币', '7' => '7天新增消费金币', '30' => '当月新增消费金币', '-1' => '总消费金币', ]); } /** * 处理请求. * * @return void */ public function handle(Request $request) { switch ($request->get('option')) { case '-1': $this->content($this->getConsume()); break; case '7': $startAt = Carbon::now()->subDays(7)->toDateString() . ' 23:59:59'; $endAt = Carbon::now()->toDateString() . ' 23:59:59'; $today = $this->getConsume($startAt, $endAt); $this->content($today); break; case '30': $startAt = Carbon::now()->subDays(30)->toDateString() . ' 23:59:59'; $endAt = Carbon::now()->toDateString() . ' 23:59:59'; $today = $this->getConsume($startAt, $endAt); $this->content($today); break; case '1': default: $startAt = Carbon::now()->subDay()->toDateString() . ' 23:59:59'; $endAt = Carbon::now()->toDateString() . ' 23:59:59'; $today = $this->getConsume($startAt, $endAt); $this->content($today); break; } } private function getConsume($startAt = '', $endAt = ''): int { $res = UserConsumeRecord::when($startAt, function ($query) use ($startAt) { $query->where('created_at', '>', $startAt); })->when($endAt, function ($query) use ($endAt) { $query->where('created_at', '<=', $endAt); })->where('type', 2) ->sum('change'); return abs($res); } /** * @param int $percent * * @return $this */ public function up($percent) { return $this->footer( " {$percent}% 增加" ); } /** * @param int $percent * * @return $this */ public function down($percent) { return $this->footer( " {$percent}% 减少" ); } /** * 设置卡片底部内容. * * @param string|Renderable|\Closure $footer * * @return $this */ public function footer($footer) { $this->footer = $footer; return $this; } /** * 渲染卡片内容. * * @return string */ public function renderContent() { $content = parent::renderContent(); return <<