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

183 lines
5.1 KiB

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