FruitDailyOutputChart.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers\Metrics;
  3. use UCore\DcatAdmin\Metrics\Examples\NewUsers;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Module\GameItems\Models\ItemTransactionLog;
  7. /**
  8. * 果实每日产出折线图
  9. * 显示各种果实的每日产出数量趋势
  10. */
  11. class FruitDailyOutputChart extends NewUsers
  12. {
  13. /**
  14. * 果实物品ID映射
  15. */
  16. protected $fruitItems = [
  17. 2 => '萝卜',
  18. 3 => '辣椒',
  19. 4 => '苹果',
  20. 5 => '西瓜',
  21. 6 => '草莓',
  22. 7 => '南瓜',
  23. 8 => '核桃',
  24. 9 => '可可',
  25. 10 => '人参',
  26. 11 => '玫瑰',
  27. ];
  28. /**
  29. * 初始化卡片内容
  30. */
  31. protected function init()
  32. {
  33. parent::init();
  34. $this->title('果实每日产出趋势');
  35. $this->dropdown([
  36. '7' => '最近 7 天',
  37. '14' => '最近 14 天',
  38. '30' => '最近 30 天',
  39. '90' => '最近 90 天',
  40. ]);
  41. }
  42. /**
  43. * 处理请求
  44. *
  45. * @param Request $request
  46. * @return mixed|void
  47. */
  48. public function handle(Request $request)
  49. {
  50. $days = (int)$request->get('option', 7);
  51. $chartData = $this->getFruitOutputData($days);
  52. // 计算总产出数量作为卡片内容
  53. $totalOutput = array_sum(array_map('array_sum', $chartData['data']));
  54. // 卡片内容
  55. $this->withContent(number_format($totalOutput));
  56. // 图表数据
  57. $this->withChart($chartData['series'], $chartData['categories']);
  58. }
  59. /**
  60. * 获取果实产出数据
  61. *
  62. * @param int $days
  63. * @return array
  64. */
  65. protected function getFruitOutputData($days)
  66. {
  67. // 生成日期范围
  68. $dates = [];
  69. for ($i = $days - 1; $i >= 0; $i--) {
  70. $dates[] = now()->subDays($i)->format('Y-m-d');
  71. }
  72. // 查询每日产出数据
  73. $outputData = ItemTransactionLog::select([
  74. 'item_id',
  75. DB::raw('DATE(created_at) as date'),
  76. DB::raw('SUM(quantity) as daily_output')
  77. ])
  78. ->whereIn('item_id', array_keys($this->fruitItems))
  79. ->where('transaction_type', 1) // 获取类型
  80. ->where('created_at', '>=', now()->subDays($days))
  81. ->groupBy('item_id', DB::raw('DATE(created_at)'))
  82. ->get()
  83. ->groupBy('item_id');
  84. // 构建图表数据
  85. $series = [];
  86. $data = [];
  87. foreach ($this->fruitItems as $itemId => $itemName) {
  88. $itemData = [];
  89. $itemOutputs = $outputData->get($itemId, collect());
  90. foreach ($dates as $date) {
  91. $dayOutput = $itemOutputs->where('date', $date)->first();
  92. $quantity = $dayOutput ? (int)$dayOutput->daily_output : 0;
  93. $itemData[] = $quantity;
  94. }
  95. $series[] = [
  96. 'name' => $itemName,
  97. 'data' => $itemData,
  98. ];
  99. $data[$itemId] = $itemData;
  100. }
  101. return [
  102. 'series' => $series,
  103. 'categories' => $dates,
  104. 'data' => $data,
  105. ];
  106. }
  107. /**
  108. * 设置图表数据
  109. *
  110. * @param array $series
  111. * @param array $categories
  112. * @return $this
  113. */
  114. public function withChart(array $series, array $categories = [])
  115. {
  116. return $this->chart([
  117. 'series' => $series,
  118. 'xaxis' => [
  119. 'categories' => $categories,
  120. ],
  121. ]);
  122. }
  123. /**
  124. * 设置卡片内容
  125. *
  126. * @param string $content
  127. * @return $this
  128. */
  129. public function withContent($content)
  130. {
  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">总产出数量</span>
  136. </div>
  137. HTML
  138. );
  139. }
  140. }