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

181 lines
5.0 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->total['subsidy_total'] = $subsidyTotal;
  35. }
  36. $this->chartData = [
  37. [
  38. 'name' => '金额',
  39. 'data' => $this->data,
  40. ]
  41. ];
  42. $color = Admin::color();
  43. // 设置标题
  44. $this->title('总补贴金额(元)');
  45. $this->subTitle('其中'.$this->showNumber.'天每天总补贴金额');
  46. $this->chartHeight(140);
  47. // 设置图表颜色
  48. $this->chartColors([$color->primary()]);
  49. $this->chartOption(
  50. 'tooltip.x',
  51. ['show' => true]
  52. );
  53. $this->chartOption(
  54. 'tooltip.y.formatter',
  55. JavaScript::make("function (params,index) {
  56. return params ;
  57. }")
  58. );
  59. $this->chartOption(
  60. 'xaxis.type',
  61. 'category'
  62. );
  63. $this->chartOption(
  64. 'xaxis.categories',
  65. $this->categories
  66. );
  67. $this->withContent($this->total);
  68. $this->withChart($this->chartData);
  69. $this->withCategories($this->categories);
  70. }
  71. protected function init()
  72. {
  73. parent::init();
  74. }
  75. /**
  76. * 处理请求
  77. * 如果你的图表类中包含此方法,则可以通过此方法处理前端通过ajax提交的获取图表数据的请求
  78. *
  79. * @param Request $request
  80. * @return mixed|void
  81. */
  82. public function handleBar(Request $request)
  83. {
  84. // $this->GoodsActivityReport = new GoodsActivityReport($data);
  85. // 数据查询逻辑
  86. // 分页的时候不重复查询数据
  87. $currentPage = request()->input('page', 1);
  88. if($currentPage == 1){
  89. $params = $this->parameters();
  90. $data = $this->GoodsActivityReport->getCountData($params);
  91. if(!empty($data) && is_array($data)){
  92. $subsidyTotal = 0;
  93. foreach($data as $key => $value){
  94. if($key < $this->showNumber){
  95. $this->data[] = $value['subsidy_total'];
  96. $this->categories[] = $value['dtime'];
  97. }
  98. $subsidyTotal += $value['subsidy_total'];
  99. }
  100. $this->total['subsidy_total'] = $subsidyTotal;
  101. }
  102. }
  103. $data = $this->data;
  104. $categories = $this->categories;
  105. $chartData = [
  106. [
  107. 'name' => '金额',
  108. 'data' => $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. $content = $total['subsidy_total'] ??0;
  153. return $this->content(
  154. <<<HTML
  155. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  156. <h2 class="ml-1 font-lg-1">{$content}</h2>
  157. <!-- <span class="mb-0 mr-1 text-80">{$this->title}</span> -->
  158. </div>
  159. HTML
  160. );
  161. }
  162. }