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

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