| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace App\Http\Controllers\Admin;
- use App\Http\Controllers\Controller;
- use App\Models\GameEnterDailyStat;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\DB;
- class GameEnterLogController extends Controller
- {
- /**
- * Daily per-game enter duration statistics (from aggregate table).
- */
- public function statistics(Request $request)
- {
- $today = date('Y-m-d');
- $startDate = $request->input('start_date', $today);
- $endDate = $request->input('end_date', $today);
- $rows = DB::connection('read')->table(GameEnterDailyStat::TABLE)
- ->whereBetween('log_date', [$startDate, $endDate])
- ->select('log_date', 'game_id', 'enter_count', 'total_duration_ms')
- ->orderByDesc('log_date')
- ->orderByDesc('enter_count')
- ->get();
- $gameIds = $rows->pluck('game_id')->filter()->unique()->values()->toArray();
- $games = [];
- if (!empty($gameIds)) {
- $gamesData = DB::connection('mysql')
- ->table('webgame.games')
- ->whereIn('id', $gameIds)
- ->select('id', 'brand', 'title')
- ->get();
- foreach ($gamesData as $game) {
- $games[$game->id] = $game;
- }
- }
- $statistics = [];
- foreach ($rows as $row) {
- $gameId = (int) $row->game_id;
- $enterCount = (int) $row->enter_count;
- $totalMs = (int) $row->total_duration_ms;
- $avgMs = $enterCount > 0 ? $totalMs / $enterCount : 0;
- $gameName = __('auto.δ֪ÓÎÏ·');
- if (isset($games[$gameId])) {
- $game = $games[$gameId];
- $gameName = $game->brand . ' - ' . $game->title;
- }
- $statistics[] = [
- 'log_date' => $row->log_date,
- 'game_id' => $gameId,
- 'game_name' => $gameName,
- 'enter_count' => $enterCount,
- 'avg_duration_ms' => round($avgMs, 2),
- 'avg_duration_sec' => round($avgMs / 1000, 3),
- ];
- }
- return view('admin.game_enter_log.statistics', [
- 'statistics' => $statistics,
- 'start_date' => $startDate,
- 'end_date' => $endDate,
- ]);
- }
- }
|