123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace App\Admin\Metrics\Statistics;
- use App\Models\Device;
- use App\Models\Order;
- use App\Models\Shop;
- use App\Repositories\Enums\PayStatusEnum;
- use Carbon\Carbon;
- use Dcat\Admin\Widgets\Metrics\Card;
- use Illuminate\Contracts\Support\Renderable;
- use Illuminate\Http\Request;
- class TotalDevices extends Card
- {
- /**
- * 卡片底部内容.
- *
- * @var string|Renderable|\Closure
- */
- protected $footer;
- /**
- * 处理请求.
- *
- * @param Request $request
- *
- * @return void
- */
- public function handle(Request $request)
- {
- $total = 0;
- $model = Device::query();
- switch ($request->get('option')) {
- case '365':
- $total = $model->whereDate('created_at', '>=', Carbon::now()->addYears(-1)->toDateString())->count();
- break;
- case '30':
- $total = $model->whereDate('created_at', '>=', Carbon::now()->addDays(-30)->toDateString())->count();
- break;
- case '14':
- $total = $model->whereDate('created_at', '>=', Carbon::now()->addDays(-14)->toDateString())->count();
- break;
- case '7':
- $total = $model->whereDate('created_at', '>=', Carbon::now()->addDays(-7)->toDateString())->count();
- break;
- case '3':
- default:
- $total = $model->whereDate('created_at', '>=', Carbon::now()->addDays(-1)->toDateString())->count();
- break;
- }
- $this->content($total);
- $this->total(Device::count());
- }
- /**
- * @param int $percent
- *
- * @return $this
- */
- public function total($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 <<<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>
- </div>
- <div class="ml-1 mt-1 font-weight-bold text-80">
- {$this->renderFooter()}
- </div>
- HTML;
- }
- /**
- * 渲染卡片底部内容.
- *
- * @return string
- */
- public function renderFooter()
- {
- return $this->toString($this->footer);
- }
- /**
- * 初始化卡片.
- */
- protected function init()
- {
- parent::init();
- $this->title('设备数');
- $this->dropdown([
- '3' => '近三天',
- '7' => '近一周',
- '14' => '近两周',
- '30' => '近一月',
- '365' => '近一年',
- ]);
- }
- }
|