链街Dcat后台
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.

152 lines
4.1 KiB

  1. <?php
  2. namespace App\Admin\Widgets\Charts;
  3. use App\Admin\Common\Auth;
  4. use App\Admin\Repositories\v3\GoodsActivityReport;
  5. use Dcat\Admin\Admin;
  6. use Dcat\Admin\Support\JavaScript;
  7. use Dcat\Admin\Widgets\Metrics\Bar;
  8. use Illuminate\Http\Request;
  9. class OrderGoodsActivityTotalChart extends Bar
  10. {
  11. /**
  12. * 活动商品总数
  13. */
  14. protected $categories = [];
  15. protected $data = [];
  16. protected $total = [];
  17. protected $valueData = [];
  18. protected $GoodsActivityReport = null;
  19. protected $showNumber = 7;
  20. public function __construct($data = [])
  21. {
  22. $this->GoodsActivityReport = new GoodsActivityReport($data);
  23. parent::__construct();
  24. }
  25. protected function init()
  26. {
  27. parent::init();
  28. $color = Admin::color();
  29. // 设置标题
  30. $this->title('销售总数量(单)');
  31. $this->subTitle('其中'.$this->showNumber.'天每天的销量');
  32. // 设置图表颜色
  33. $this->chartColors([$color->primary()]);
  34. $this->chartOption(
  35. 'tooltip.x',
  36. ['show' => true]
  37. );
  38. $this->chartOption(
  39. 'tooltip.y.formatter',
  40. JavaScript::make("function (params,index) {
  41. return params ;
  42. }")
  43. );
  44. $this->chartOption(
  45. 'xaxis.type',
  46. 'category'
  47. );
  48. $this->chartOption(
  49. 'xaxis.categories',
  50. $this->categories
  51. );
  52. }
  53. /**
  54. * 处理请求
  55. * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求
  56. *
  57. * @param Request $request
  58. * @return mixed|void
  59. */
  60. public function handle(Request $request)
  61. {
  62. // 数据查询逻辑
  63. // 分页的时候不重复查询数据
  64. $currentPage = request()->input('page', 1);
  65. if($currentPage == 1){
  66. $params = $this->parameters();
  67. $data = $this->GoodsActivityReport->getCountData($params);
  68. if(!empty($data) && is_array($data)){
  69. $total = 0;
  70. foreach($data as $key => $value){
  71. if($key < $this->showNumber){
  72. $this->data[] = $value['total'];
  73. $this->categories[] = $value['dtime'];
  74. }
  75. $total += $value['total'];
  76. }
  77. $this->total['number_total'] = $total;
  78. }
  79. }
  80. $categories = $this->categories;
  81. $chartData = [
  82. [
  83. 'name' => '销量',
  84. 'data' => $this->data,
  85. ]
  86. ];
  87. $this->withContent($this->total);
  88. $this->withChart($chartData);
  89. $this->withCategories($categories);
  90. }
  91. /**
  92. * 这里返回需要异步传递到 handler 方法的参数
  93. *
  94. * @return array
  95. */
  96. public function parameters(): array
  97. {
  98. $this->marketId = Auth::getMarket();
  99. return [
  100. 'page' => request()->input('page', 1),
  101. 'name' => request()->input('name', ''),
  102. 'market_id' => $this->marketId ? $this->marketId : request()->input('market_id',0),
  103. 'store_id' => request()->input('store_id',0),
  104. 'start_time' => request()->input('start_time',''),
  105. 'end_time' => request()->input('end_time',''),
  106. ];
  107. }
  108. /**
  109. * 设置图表数据.
  110. *
  111. * @param array $data
  112. *
  113. * @return $this
  114. */
  115. public function withChart(array $data)
  116. {
  117. return $this->chart([
  118. 'series' => $data,
  119. ]);
  120. }
  121. /**
  122. * 渲染卡片内容.
  123. *
  124. * @return string
  125. */
  126. public function withContent($total = [])
  127. {
  128. $value = $total['number_total'] ?? 0;
  129. $minHeight = '113px';
  130. return $this->content(
  131. <<<HTML
  132. <div class="d-flex p-1 flex-column justify-content-between" style="padding-top: 0;width: 100%;height: 100%;min-height: {$minHeight}">
  133. <div class="text-left">
  134. <h2 class="ml-1 font-lg-1">{$value}</h2>
  135. </div>
  136. </div>
  137. HTML
  138. );
  139. }
  140. }