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

145 lines
2.8 KiB

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 Dcat\Admin\Admin;
  7. use Dcat\Admin\Widgets\ApexCharts\Chart;
  8. use Dcat\Admin\Widgets\Metrics\Bar;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Arr;
  11. use Illuminate\Support\Facades\DB;
  12. class FinanceStatistics extends Chart
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. $this->setUpOptions();
  18. }
  19. /**
  20. * 初始化图表配置
  21. */
  22. protected function setUpOptions()
  23. {
  24. $this->options([
  25. 'chart' => [
  26. //'width' => '180%',
  27. 'type' => 'bar',
  28. 'events' => [
  29. ],
  30. 'toolbar' => ['show' => false],
  31. ],
  32. 'plotOptions' => [
  33. 'bar' => [
  34. //'columnWidth' => '45%',
  35. 'distributed' => true,
  36. ]
  37. ],
  38. 'dataLabels' => [
  39. 'enabled' => false
  40. ],
  41. 'legend' => [
  42. 'show' => false
  43. ],
  44. 'xaxis' => [
  45. //'categories' =>
  46. // [75, 125, 225, 175, 125, 75, 25]
  47. //,
  48. 'labels' => [
  49. 'show' => true,
  50. 'style' => [
  51. 'fontSize' => '12px'
  52. ]
  53. ],
  54. ],
  55. 'yaxis' => [
  56. 'show' => true
  57. ],
  58. 'tooltip' => [
  59. 'x' => ['show' => true],
  60. ],
  61. ]);
  62. }
  63. /**
  64. * 处理图表数据
  65. */
  66. protected function buildData()
  67. {
  68. $query = OrderProductItem::query()
  69. ->where('supplier_id',Admin::user()->id)
  70. ->whereHas('order',function ($query) {
  71. $query->complete();
  72. })
  73. ->select('*');
  74. $dateTime = request('created_at', 0);
  75. if ($dateTime) {
  76. $query->whereBetween('created_at',$dateTime);
  77. }
  78. switch (request('time_key', 0)) {
  79. case '1':
  80. $query->addSelect(DB::raw("sum(price) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  81. break;
  82. case '30':
  83. $query->addSelect(DB::raw("sum(price) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at)) AS statistics_time"));
  84. break;
  85. case '365':
  86. $query->addSelect(DB::raw("sum(price) as sum_price,CONCAT(YEAR(created_at)) AS statistics_time"));
  87. break;
  88. default:
  89. $query->addSelect(DB::raw("sum(price) as sum_price,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  90. }
  91. $order = $query->groupBy('statistics_time')
  92. ->orderBy('created_at')
  93. ->get()
  94. ->toArray();
  95. $this->withData([
  96. [
  97. 'name' => '金额',
  98. 'data' => Arr::pluck($order,'sum_price')
  99. ],
  100. ]
  101. );
  102. $this->withCategories(
  103. Arr::pluck($order,'statistics_time')
  104. );
  105. }
  106. public function withData(array $data)
  107. {
  108. return $this->option('series', $data);
  109. }
  110. /**
  111. * 设置图表类别.
  112. *
  113. * @param array $data
  114. *
  115. * @return $this
  116. */
  117. public function withCategories(array $data)
  118. {
  119. return $this->option('xaxis.categories', $data);
  120. }
  121. /**
  122. * 渲染图表
  123. *
  124. * @return string
  125. */
  126. public function render()
  127. {
  128. $this->buildData();
  129. return parent::render();
  130. }
  131. }