链街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\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 $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. $subsidyTotal = 0;
  26. foreach($data as $key => $value){
  27. if($key < $this->showNumber){
  28. $this->data[] = $value['subsidy_total'];
  29. $this->categories[] = $value['dtime'];
  30. }
  31. $subsidyTotal += $value['subsidy_total'];
  32. }
  33. $this->total['subsidy_total'] = $subsidyTotal;
  34. }
  35. $data = $this->data;
  36. $categories = $this->categories;
  37. $chartData = [
  38. [
  39. 'name' => '金额',
  40. 'data' => $data,
  41. ]
  42. ];
  43. $this->withContent($this->total);
  44. $this->withChart($chartData);
  45. $this->withCategories($categories);
  46. }
  47. protected function init()
  48. {
  49. parent::init();
  50. $color = Admin::color();
  51. // 设置标题
  52. $this->title('总补贴金额(元)');
  53. $this->subTitle('其中'.$this->showNumber.'天每天总补贴金额');
  54. $this->chartHeight(140);
  55. // 设置图表颜色
  56. $this->chartColors([$color->primary()]);
  57. $this->chartOption(
  58. 'tooltip.x',
  59. ['show' => true]
  60. );
  61. $this->chartOption(
  62. 'tooltip.y.formatter',
  63. JavaScript::make("function (params,index) {
  64. return params ;
  65. }")
  66. );
  67. $this->chartOption(
  68. 'xaxis.type',
  69. 'category'
  70. );
  71. $this->chartOption(
  72. 'xaxis.categories',
  73. $this->categories
  74. );
  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. $subsidyTotal = 0;
  94. foreach($data as $key => $value){
  95. if($key < $this->showNumber){
  96. $this->data[] = $value['subsidy_total'];
  97. $this->categories[] = $value['dtime'];
  98. }
  99. $subsidyTotal += $value['subsidy_total'];
  100. }
  101. $this->total['subsidy_total'] = $subsidyTotal;
  102. }
  103. }
  104. $data = $this->data;
  105. $categories = $this->categories;
  106. $chartData = [
  107. [
  108. 'name' => '金额',
  109. 'data' => $data,
  110. ]
  111. ];
  112. $this->withContent($this->total);
  113. $this->withChart($chartData);
  114. $this->withCategories($categories);
  115. }
  116. /**
  117. * 这里返回需要异步传递到 handler 方法的参数
  118. *
  119. * @return array
  120. */
  121. public function parameters(): array
  122. {
  123. $this->marketId = Auth::getMarket();
  124. return [
  125. 'page' => request()->input('page', 1),
  126. 'name' => request()->input('name', ''),
  127. 'market_id' => $this->marketId ? $this->marketId : request()->input('market_id',0),
  128. 'store_id' => request()->input('store_id',0),
  129. 'start_time' => request()->input('start_time',''),
  130. 'end_time' => request()->input('end_time',''),
  131. ];
  132. }
  133. /**
  134. * 设置图表数据.
  135. *
  136. * @param array $data
  137. *
  138. * @return $this
  139. */
  140. public function withChart(array $data)
  141. {
  142. return $this->chart([
  143. 'series' => $data,
  144. ]);
  145. }
  146. /**
  147. * 渲染卡片内容.
  148. *
  149. * @return string
  150. */
  151. public function withContent($total = [])
  152. {
  153. $content = $total['subsidy_total'] ??0;
  154. return $this->content(
  155. <<<HTML
  156. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  157. <h2 class="ml-1 font-lg-1">{$content}</h2>
  158. <!-- <span class="mb-0 mr-1 text-80">{$this->title}</span> -->
  159. </div>
  160. HTML
  161. );
  162. }
  163. }