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

168 lines
3.6 KiB

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