GameEnterLogController.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\GameEnterDailyStat;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Support\Facades\DB;
  7. class GameEnterLogController extends Controller
  8. {
  9. /**
  10. * Daily per-game enter duration statistics (from aggregate table).
  11. */
  12. public function statistics(Request $request)
  13. {
  14. $today = date('Y-m-d');
  15. $startDate = $request->input('start_date', $today);
  16. $endDate = $request->input('end_date', $today);
  17. $rows = DB::connection('read')->table(GameEnterDailyStat::TABLE)
  18. ->whereBetween('log_date', [$startDate, $endDate])
  19. ->select('log_date', 'game_id', 'enter_count', 'total_duration_ms')
  20. ->orderByDesc('log_date')
  21. ->orderByDesc('enter_count')
  22. ->get();
  23. $gameIds = $rows->pluck('game_id')->filter()->unique()->values()->toArray();
  24. $games = [];
  25. if (!empty($gameIds)) {
  26. $gamesData = DB::connection('mysql')
  27. ->table('webgame.games')
  28. ->whereIn('id', $gameIds)
  29. ->select('id', 'brand', 'title')
  30. ->get();
  31. foreach ($gamesData as $game) {
  32. $games[$game->id] = $game;
  33. }
  34. }
  35. $statistics = [];
  36. foreach ($rows as $row) {
  37. $gameId = (int) $row->game_id;
  38. $enterCount = (int) $row->enter_count;
  39. $totalMs = (int) $row->total_duration_ms;
  40. $avgMs = $enterCount > 0 ? $totalMs / $enterCount : 0;
  41. $gameName = __('auto.未知游戏');
  42. if (isset($games[$gameId])) {
  43. $game = $games[$gameId];
  44. $gameName = $game->brand . ' - ' . $game->title;
  45. }
  46. $statistics[] = [
  47. 'log_date' => $row->log_date,
  48. 'game_id' => $gameId,
  49. 'game_name' => $gameName,
  50. 'enter_count' => $enterCount,
  51. 'avg_duration_ms' => round($avgMs, 2),
  52. 'avg_duration_sec' => round($avgMs / 1000, 3),
  53. ];
  54. }
  55. return view('admin.game_enter_log.statistics', [
  56. 'statistics' => $statistics,
  57. 'start_date' => $startDate,
  58. 'end_date' => $endDate,
  59. ]);
  60. }
  61. }