海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
3.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminAgent\Metrics\Examples;
  3. use App\Common\OrderStatus;
  4. use App\Models\Order;
  5. use Carbon\Carbon;
  6. use Dcat\Admin\Admin;
  7. use Dcat\Admin\Widgets\Metrics\RadialBar;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Arr;
  10. use Illuminate\Support\Facades\DB;
  11. class OrderExamples extends RadialBar
  12. {
  13. /**
  14. * 初始化卡片内容
  15. */
  16. protected function init()
  17. {
  18. parent::init();
  19. $this->title('订单统计');
  20. $this->height(400);
  21. $this->chartHeight(300);
  22. }
  23. /**
  24. * 处理请求
  25. *
  26. * @param Request $request
  27. *
  28. * @return mixed|void
  29. */
  30. public function handle(Request $request)
  31. {
  32. //总订单数
  33. $count = Order::query()->where('agent_id',Admin::user()->id)->count();
  34. // 卡片内容
  35. $this->withContent($count);
  36. // 卡片底部
  37. //今日订单
  38. $countToday = Order::query()->where('agent_id',Admin::user()->id)->whereDate('created_at',Carbon::today())->count();
  39. $countYesterday = Order::query()->where('agent_id',Admin::user()->id)->whereDate('created_at',Carbon::yesterday())->count();
  40. $price = Order::query()->where('agent_id',Admin::user()->id)->where('status',OrderStatus::SUCCESS)->whereDate('created_at',Carbon::today())->sum('price');
  41. $this->withFooter($countToday, $countYesterday, $price);
  42. $order = Order::query()
  43. ->select('*')
  44. ->addSelect(DB::raw("COUNT(id) as count_id,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"))
  45. ->where('agent_id',Admin::user()->id)
  46. ->limit(30)
  47. ->orderBy('created_at', 'asc')
  48. ->groupBy('statistics_time')
  49. ->get()
  50. ->toArray();
  51. $categories = Arr::pluck($order,'statistics_time');
  52. // 图表数据
  53. $data = Arr::pluck($order,'count_id');
  54. $this->withChart($data,$categories);
  55. }
  56. /**
  57. * 设置图表数据.
  58. *
  59. * @param int $data
  60. *
  61. * @return $this
  62. */
  63. public function withChart(array $data,array $categories)
  64. {
  65. return $this->chart([
  66. 'series' => [
  67. [
  68. 'name' => '订单量',
  69. 'data' => $data
  70. ]
  71. ],
  72. 'chart' => [
  73. 'type' => 'line',
  74. 'zoom' => [
  75. 'enabled' => false
  76. ],
  77. 'toolbar' => [
  78. 'show' => false
  79. ],
  80. ],
  81. 'colors' => [
  82. Admin::color()->green(),
  83. ],
  84. 'dataLabels' => [
  85. 'enabled' => false
  86. ],
  87. 'stroke' => [
  88. 'curve' => 'smooth'
  89. ],
  90. 'legend' => [
  91. 'position' =>'top',
  92. //'horizontalAlign' => 'right'
  93. ],
  94. 'fill' => [
  95. 'opacity' => 1,
  96. 'type' => 'solid',
  97. ],
  98. 'xaxis' => [
  99. 'categories' => $categories,
  100. ]
  101. ]);
  102. }
  103. /**
  104. * 卡片内容
  105. *
  106. * @param string $content
  107. *
  108. * @return $this
  109. */
  110. public function withContent($content)
  111. {
  112. return $this->content(
  113. <<<HTML
  114. <div class="d-flex flex-column flex-wrap text-left p-1">
  115. <h1 class="font-lg-2 mt-2 mb-0">{$content}</h1>
  116. <small>总订单数</small>
  117. </div>
  118. HTML
  119. );
  120. }
  121. /**
  122. * 卡片底部内容.
  123. *
  124. * @param string $new
  125. * @param string $open
  126. * @param string $response
  127. *
  128. * @return $this
  129. */
  130. public function withFooter($new, $open, $response)
  131. {
  132. return $this->footer(
  133. <<<HTML
  134. <div class="d-flex justify-content-between p-1" style="padding-top: 0!important;">
  135. <div class="text-center">
  136. <p>今日订单</p>
  137. <span class="font-lg-1">{$new}</span>
  138. </div>
  139. <div class="text-center">
  140. <p>昨日订单</p>
  141. <span class="font-lg-1">{$open}</span>
  142. </div>
  143. <div class="text-center">
  144. <p>今日成交()</p>
  145. <span class="font-lg-1">{$response}</span>
  146. </div>
  147. </div>
  148. HTML
  149. );
  150. }
  151. }