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

181 lines
3.6 KiB

4 years ago
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 Dcat\Admin\Admin;
  7. use Dcat\Admin\Widgets\Metrics\Bar;
  8. use Illuminate\Support\Facades\DB;
  9. class SalesExamples extends Bar
  10. {
  11. protected $labels = [];
  12. protected $colors;
  13. /**
  14. * 初始化卡片内容
  15. */
  16. protected function init()
  17. {
  18. parent::init();
  19. $color = Admin::color();
  20. $this->colors = [$color->green(), $color->red(), $color->yellow(), $color->orange1(), $color->dark90(), $color->dark70(), $color->custom()];
  21. $this->contentWidth(3, 9);
  22. $this->title('销售排行');
  23. //$this->chartPullRight = false;
  24. //$this->subTitle('Last 30 days');
  25. $this->height = 280;
  26. $this->chartHeight = 250;
  27. // 设置图表颜色
  28. $this->chartColors($this->colors);
  29. }
  30. /**
  31. * 渲染模板
  32. *
  33. * @return string
  34. */
  35. public function render()
  36. {
  37. $this->fill();
  38. return parent::render();
  39. }
  40. /**
  41. * 写入数据.
  42. *
  43. * @return void
  44. */
  45. public function fill()
  46. {
  47. $warehouse = OrderProductItem::query()
  48. ->with(['product', 'order:id,title'])
  49. ->select('*')
  50. ->addSelect(DB::raw('count(id) as count_id'))
  51. ->whereHas('order',function ($query) {
  52. $query->where('status',OrderStatus::SUCCESS);
  53. })
  54. ->where('supplier_id',Admin::user()->id)
  55. ->orderByDesc('count_id')
  56. ->groupBy('product_id')
  57. ->limit(5)
  58. ->get();
  59. //$warehouse = [];
  60. $data = $categories = [];
  61. foreach ($warehouse as $v) {
  62. $title = $v->product->title ?? $v->title;
  63. array_push($this->labels, $title);
  64. array_push($data, $v->count_id);
  65. $categories[] = [$title];
  66. }
  67. $this->withChart($data, $categories);
  68. //$this->chartLabels($this->labels);
  69. $count = OrderProductItem::query()
  70. ->where('supplier_id',Admin::user()->id)
  71. ->whereHas('order',function ($query) {
  72. $query->where('status',OrderStatus::SUCCESS);
  73. })
  74. ->count();
  75. //$count = 0;
  76. if ($count > 0) {
  77. array_unshift($data, $count);
  78. array_unshift($this->labels, '总销售量');
  79. $color = Admin::color();
  80. array_unshift($this->colors, $color->primary());
  81. }
  82. $this->withContent($data);
  83. }
  84. /**
  85. * 设置图表数据.
  86. *
  87. * @param array $data
  88. *
  89. * @return $this
  90. */
  91. public function withChart($data, $categories)
  92. {
  93. return $this->chart([
  94. 'series' => [[
  95. 'name' => '销量',
  96. 'data' => $data
  97. ]],
  98. 'chart' => [
  99. //'width' => '180%',
  100. 'type' => 'bar',
  101. 'events' => [
  102. ],
  103. 'toolbar' => ['show' => false],
  104. ],
  105. 'colors' => $this->colors,
  106. 'plotOptions' => [
  107. 'bar' => [
  108. //'columnWidth' => '45%',
  109. 'distributed' => true,
  110. ]
  111. ],
  112. 'dataLabels' => [
  113. 'enabled' => false
  114. ],
  115. 'legend' => [
  116. 'show' => false
  117. ],
  118. 'xaxis' => [
  119. 'categories' =>
  120. $categories
  121. ,
  122. 'labels' => [
  123. 'show' => false,
  124. 'style' => [
  125. 'colors' => $this->colors,
  126. 'fontSize' => '12px'
  127. ]
  128. ],
  129. ],
  130. 'yaxis' => [
  131. 'show' => false
  132. ],
  133. 'tooltip' => [
  134. 'x' => ['show' => true],
  135. ],
  136. ]);
  137. }
  138. /**
  139. * 设置卡片头部内容.
  140. *
  141. * @param mixed $desktop
  142. * @param mixed $mobile
  143. *
  144. * @return $this
  145. */
  146. protected function withContent($data)
  147. {
  148. $content = '';
  149. foreach ($data as $k => $v) {
  150. $content .= '
  151. <div class="d-flex pl-1 mt-2">
  152. <div style="width:100%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
  153. <i class="fa fa-circle" style="color:' . $this->colors[$k] . '"></i> ' . $this->labels[$k] . '
  154. <div style="display:inline-block">' . $v . '</div>
  155. </div>
  156. </div>
  157. ';
  158. }
  159. return $this->content(
  160. <<<HTML
  161. $content
  162. HTML
  163. );
  164. }
  165. }