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

180 lines
3.6 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\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')
  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. array_push($this->labels, $v->product->title);
  63. array_push($data, $v->count_id);
  64. $categories[] = [$v->product->title];
  65. }
  66. $this->withChart($data, $categories);
  67. //$this->chartLabels($this->labels);
  68. $count = OrderProductItem::query()
  69. ->where('supplier_id',Admin::user()->id)
  70. ->whereHas('order',function ($query) {
  71. $query->where('status',OrderStatus::SUCCESS);
  72. })
  73. ->count();
  74. //$count = 0;
  75. if ($count > 0) {
  76. array_unshift($data, $count);
  77. array_unshift($this->labels, '总销售量');
  78. $color = Admin::color();
  79. array_unshift($this->colors, $color->primary());
  80. }
  81. $this->withContent($data);
  82. }
  83. /**
  84. * 设置图表数据.
  85. *
  86. * @param array $data
  87. *
  88. * @return $this
  89. */
  90. public function withChart($data, $categories)
  91. {
  92. return $this->chart([
  93. 'series' => [[
  94. 'name' => '销量',
  95. 'data' => $data
  96. ]],
  97. 'chart' => [
  98. //'width' => '180%',
  99. 'type' => 'bar',
  100. 'events' => [
  101. ],
  102. 'toolbar' => ['show' => false],
  103. ],
  104. 'colors' => $this->colors,
  105. 'plotOptions' => [
  106. 'bar' => [
  107. //'columnWidth' => '45%',
  108. 'distributed' => true,
  109. ]
  110. ],
  111. 'dataLabels' => [
  112. 'enabled' => false
  113. ],
  114. 'legend' => [
  115. 'show' => false
  116. ],
  117. 'xaxis' => [
  118. 'categories' =>
  119. $categories
  120. ,
  121. 'labels' => [
  122. 'show' => false,
  123. 'style' => [
  124. 'colors' => $this->colors,
  125. 'fontSize' => '12px'
  126. ]
  127. ],
  128. ],
  129. 'yaxis' => [
  130. 'show' => false
  131. ],
  132. 'tooltip' => [
  133. 'x' => ['show' => true],
  134. ],
  135. ]);
  136. }
  137. /**
  138. * 设置卡片头部内容.
  139. *
  140. * @param mixed $desktop
  141. * @param mixed $mobile
  142. *
  143. * @return $this
  144. */
  145. protected function withContent($data)
  146. {
  147. $content = '';
  148. foreach ($data as $k => $v) {
  149. $content .= '
  150. <div class="d-flex pl-1 mt-2">
  151. <div style="width:100%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
  152. <i class="fa fa-circle" style="color:' . $this->colors[$k] . '"></i> ' . $this->labels[$k] . '
  153. <div style="display:inline-block">' . $v . '</div>
  154. </div>
  155. </div>
  156. ';
  157. }
  158. return $this->content(
  159. <<<HTML
  160. $content
  161. HTML
  162. );
  163. }
  164. }