TotalDevices.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Admin\Metrics\Statistics;
  3. use App\Models\Device;
  4. use App\Models\Order;
  5. use App\Models\Shop;
  6. use App\Repositories\Enums\PayStatusEnum;
  7. use Carbon\Carbon;
  8. use Dcat\Admin\Widgets\Metrics\Card;
  9. use Illuminate\Contracts\Support\Renderable;
  10. use Illuminate\Http\Request;
  11. class TotalDevices extends Card
  12. {
  13. /**
  14. * 卡片底部内容.
  15. *
  16. * @var string|Renderable|\Closure
  17. */
  18. protected $footer;
  19. /**
  20. * 处理请求.
  21. *
  22. * @param Request $request
  23. *
  24. * @return void
  25. */
  26. public function handle(Request $request)
  27. {
  28. $total = 0;
  29. $model = Device::query();
  30. switch ($request->get('option')) {
  31. case '365':
  32. $total = $model->whereDate('created_at', '>=', Carbon::now()->addYears(-1)->toDateString())->count();
  33. break;
  34. case '30':
  35. $total = $model->whereDate('created_at', '>=', Carbon::now()->addDays(-30)->toDateString())->count();
  36. break;
  37. case '14':
  38. $total = $model->whereDate('created_at', '>=', Carbon::now()->addDays(-14)->toDateString())->count();
  39. break;
  40. case '7':
  41. $total = $model->whereDate('created_at', '>=', Carbon::now()->addDays(-7)->toDateString())->count();
  42. break;
  43. case '3':
  44. default:
  45. $total = $model->whereDate('created_at', '>=', Carbon::now()->addDays(-1)->toDateString())->count();
  46. break;
  47. }
  48. $this->content($total);
  49. $this->total(Device::count());
  50. }
  51. /**
  52. * @param int $percent
  53. *
  54. * @return $this
  55. */
  56. public function total($percent)
  57. {
  58. return $this->footer(
  59. "共 {$percent} 台设备"
  60. );
  61. }
  62. /**
  63. * 设置卡片底部内容.
  64. *
  65. * @param string|Renderable|\Closure $footer
  66. *
  67. * @return $this
  68. */
  69. public function footer($footer)
  70. {
  71. $this->footer = $footer;
  72. return $this;
  73. }
  74. /**
  75. * 渲染卡片内容.
  76. *
  77. * @return string
  78. */
  79. public function renderContent()
  80. {
  81. $content = parent::renderContent();
  82. return <<<HTML
  83. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  84. <h2 class="ml-1 font-lg-1">{$content}</h2>
  85. </div>
  86. <div class="ml-1 mt-1 font-weight-bold text-80">
  87. {$this->renderFooter()}
  88. </div>
  89. HTML;
  90. }
  91. /**
  92. * 渲染卡片底部内容.
  93. *
  94. * @return string
  95. */
  96. public function renderFooter()
  97. {
  98. return $this->toString($this->footer);
  99. }
  100. /**
  101. * 初始化卡片.
  102. */
  103. protected function init()
  104. {
  105. parent::init();
  106. $this->title('设备数');
  107. $this->dropdown([
  108. '3' => '近三天',
  109. '7' => '近一周',
  110. '14' => '近两周',
  111. '30' => '近一月',
  112. '365' => '近一年',
  113. ]);
  114. }
  115. }