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

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