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

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