海南旅游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.

160 lines
3.3 KiB

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