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

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\AdminAgent\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. ->where('agent_id', Admin::user()->id)
  38. ->whereIn('status',[
  39. OrderStatus::PAY_EARNEST,
  40. OrderStatus::PAID,
  41. OrderStatus::PAID_RETAINAGE,
  42. OrderStatus::OFFLINE_PAID,
  43. OrderStatus::SUCCESS
  44. ]);
  45. switch ($request->get('option')) {
  46. case 'day':
  47. $orders->whereDate('created_at', Carbon::today());
  48. break;
  49. case 'week':
  50. $time = DataTime::beginAndEnd('week');
  51. $orders->whereBetween('created_at', $time);
  52. break;
  53. case 'month':
  54. $orders->whereMonth('created_at', Carbon::now()->month);
  55. break;
  56. case 'year':
  57. $orders->whereYear('created_at', Carbon::now()->year);
  58. break;
  59. default:
  60. $time = DataTime::beginAndEnd('week');
  61. $orders->whereBetween('created_at', $time);
  62. }
  63. $orders = $orders->pluck('status')->toArray();
  64. $payEarnest = $pay = $success = 0;
  65. $arr = [
  66. OrderStatus::PAID,
  67. OrderStatus::PAID_RETAINAGE,
  68. OrderStatus::OFFLINE_PAID,
  69. ];
  70. $count = count($orders);
  71. if ($orders > 0) {
  72. foreach ($orders as $status) {
  73. if ($status == OrderStatus::PAY_EARNEST) {
  74. $payEarnest++;
  75. } elseif (in_array($status,$arr)) {
  76. $pay++;
  77. } elseif ($status == OrderStatus::SUCCESS) {
  78. $success++;
  79. }
  80. }
  81. }
  82. // 卡片内容
  83. $this->withContent($payEarnest, $pay, $success);
  84. $payEarnestPer = $count > 0 ? bcdiv($payEarnest,$count,2) * 100 : 0;
  85. $payPer = $count > 0 ? bcdiv($pay,$count,2) * 100 : 0;
  86. $successPer = $count > 0 ? bcdiv($success,$count,2) * 100 : 0;
  87. // 图表数据
  88. $this->withChart([$payEarnestPer, $payPer, $successPer]);
  89. // 总数
  90. $this->chartTotal('总计', $count);
  91. }
  92. /**
  93. * 设置图表数据.
  94. *
  95. * @param array $data
  96. *
  97. * @return $this
  98. */
  99. public function withChart(array $data)
  100. {
  101. return $this->chart([
  102. 'series' => $data,
  103. ]);
  104. }
  105. /**
  106. * 卡片内容.
  107. *
  108. * @param int $finished
  109. * @param int $pending
  110. * @param int $rejected
  111. *
  112. * @return $this
  113. */
  114. public function withContent($finished, $pending, $rejected)
  115. {
  116. return $this->content(
  117. <<<HTML
  118. <div class="col-12 d-flex flex-column flex-wrap text-center" style="max-width: 220px">
  119. <div class="chart-info d-flex justify-content-between mb-1 mt-2" >
  120. <div class="series-info d-flex align-items-center">
  121. <i class="fa fa-circle-o text-bold-700 text-primary"></i>
  122. <span class="text-bold-600 ml-50">已付定金</span>
  123. </div>
  124. <div class="product-result">
  125. <span>{$finished}</span>
  126. </div>
  127. </div>
  128. <div class="chart-info d-flex justify-content-between mb-1">
  129. <div class="series-info d-flex align-items-center">
  130. <i class="fa fa-circle-o text-bold-700 text-warning"></i>
  131. <span class="text-bold-600 ml-50">已付款</span>
  132. </div>
  133. <div class="product-result">
  134. <span>{$pending}</span>
  135. </div>
  136. </div>
  137. <div class="chart-info d-flex justify-content-between mb-1">
  138. <div class="series-info d-flex align-items-center">
  139. <i class="fa fa-circle-o text-bold-700 text-danger"></i>
  140. <span class="text-bold-600 ml-50">已完成</span>
  141. </div>
  142. <div class="product-result">
  143. <span>{$rejected}</span>
  144. </div>
  145. </div>
  146. </div>
  147. HTML
  148. );
  149. }
  150. }