链街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.

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