TotalShops.php 2.8 KB

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