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

167 lines
3.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\Admin\Metrics\Examples;
  3. use App\Common\DataTime;
  4. use App\Common\OrderStatus;
  5. use App\Models\Order;
  6. use Carbon\Carbon;
  7. use Dcat\Admin\Admin;
  8. use Dcat\Admin\Widgets\Metrics\Round;
  9. use Illuminate\Http\Request;
  10. class ProductOrders extends Round
  11. {
  12. /**
  13. * 初始化卡片内容
  14. */
  15. protected function init()
  16. {
  17. parent::init();
  18. $this->title('订单状态统计');
  19. $this->chartLabels(['Finished', 'Pending', 'Rejected']);
  20. $this->dropdown([
  21. //'day' => '本日',
  22. 'week' => '本周',
  23. 'month' => '本月',
  24. 'year' => '今年',
  25. ]);
  26. }
  27. /**
  28. * 处理请求
  29. *
  30. * @param Request $request
  31. *
  32. * @return mixed|void
  33. */
  34. public function handle(Request $request)
  35. {
  36. $orders = Order::query()
  37. ->whereIn('status',[
  38. OrderStatus::PAY_EARNEST,
  39. OrderStatus::PAID,
  40. OrderStatus::PAID_RETAINAGE,
  41. OrderStatus::OFFLINE_PAID,
  42. OrderStatus::SUCCESS
  43. ]);
  44. switch ($request->get('option')) {
  45. case 'day':
  46. $orders->whereDate('created_at', Carbon::today());
  47. break;
  48. case 'week':
  49. $time = DataTime::beginAndEnd('week');
  50. $orders->whereBetween('created_at', $time);
  51. break;
  52. case 'month':
  53. $orders->whereMonth('created_at', Carbon::now()->month);
  54. break;
  55. case 'year':
  56. $orders->whereYear('created_at', Carbon::now()->year);
  57. break;
  58. default:
  59. $time = DataTime::beginAndEnd('week');
  60. $orders->whereBetween('created_at', $time);
  61. }
  62. $orders = $orders->pluck('status')->toArray();
  63. $payEarnest = $pay = $success = 0;
  64. $arr = [
  65. OrderStatus::PAID,
  66. OrderStatus::PAID_RETAINAGE,
  67. OrderStatus::OFFLINE_PAID,
  68. ];
  69. $count = count($orders);
  70. if ($orders > 0) {
  71. foreach ($orders as $status) {
  72. if ($status == OrderStatus::PAY_EARNEST) {
  73. $payEarnest++;
  74. } elseif (in_array($status,$arr)) {
  75. $pay++;
  76. } elseif ($status == OrderStatus::SUCCESS) {
  77. $success++;
  78. }
  79. }
  80. }
  81. // 卡片内容
  82. $this->withContent($payEarnest, $pay, $success);
  83. $payEarnestPer = $count > 0 ? bcdiv($payEarnest,$count,2) * 100 : 0;
  84. $payPer = $count > 0 ? bcdiv($pay,$count,2) * 100 : 0;
  85. $successPer = $count > 0 ? bcdiv($success,$count,2) * 100 : 0;
  86. // 图表数据
  87. $this->withChart([$payEarnestPer, $payPer, $successPer]);
  88. // 总数
  89. $this->chartTotal('总计', $count);
  90. }
  91. /**
  92. * 设置图表数据.
  93. *
  94. * @param array $data
  95. *
  96. * @return \App\Admin\Metrics\Examples\ProductOrders
  97. */
  98. public function withChart(array $data)
  99. {
  100. return $this->chart([
  101. 'series' => $data,
  102. ]);
  103. }
  104. /**
  105. * 卡片内容.
  106. *
  107. * @param int $finished
  108. * @param int $pending
  109. * @param int $rejected
  110. *
  111. * @return $this
  112. */
  113. public function withContent($finished, $pending, $rejected)
  114. {
  115. return $this->content(
  116. <<<HTML
  117. <div class="col-12 d-flex flex-column flex-wrap text-center" style="max-width: 220px">
  118. <div class="chart-info d-flex justify-content-between mb-1 mt-2" >
  119. <div class="series-info d-flex align-items-center">
  120. <i class="fa fa-circle-o text-bold-700 text-primary"></i>
  121. <span class="text-bold-600 ml-50">已付定金</span>
  122. </div>
  123. <div class="product-result">
  124. <span>{$finished}</span>
  125. </div>
  126. </div>
  127. <div class="chart-info d-flex justify-content-between mb-1">
  128. <div class="series-info d-flex align-items-center">
  129. <i class="fa fa-circle-o text-bold-700 text-warning"></i>
  130. <span class="text-bold-600 ml-50">已付款</span>
  131. </div>
  132. <div class="product-result">
  133. <span>{$pending}</span>
  134. </div>
  135. </div>
  136. <div class="chart-info d-flex justify-content-between mb-1">
  137. <div class="series-info d-flex align-items-center">
  138. <i class="fa fa-circle-o text-bold-700 text-danger"></i>
  139. <span class="text-bold-600 ml-50">已完成</span>
  140. </div>
  141. <div class="product-result">
  142. <span>{$rejected}</span>
  143. </div>
  144. </div>
  145. </div>
  146. HTML
  147. );
  148. }
  149. }