TotalOrders.php 3.0 KB

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