input('tab', 'roster'); // ── 任职列表:按部门→职务展示全部(含空缺) ──────────────────── $currentStaff = null; if ($tab === 'roster') { $currentStaff = Department::query() ->with([ 'positions' => fn ($q) => $q->orderByDesc('rank'), 'positions.activeUserPositions.user', ]) ->orderByDesc('rank') ->get(); } // ── 日/周/月/总榜:勤务时长排行 ────────────────────────────────── $leaderboard = null; if (in_array($tab, ['day', 'week', 'month', 'all'])) { $query = PositionDutyLog::query() ->selectRaw('user_id, SUM(duration_seconds) as total_seconds, COUNT(*) as checkin_count'); // 按时间段过滤 match ($tab) { 'day' => $query->whereDate('login_at', today()), 'week' => $query->whereBetween('login_at', [now()->startOfWeek(), now()->endOfWeek()]), 'month' => $query->whereYear('login_at', now()->year)->whereMonth('login_at', now()->month), 'all' => null, // 不加时间限制 }; $leaderboard = $query ->groupBy('user_id') ->orderByDesc('total_seconds') ->limit(20) ->with('user') ->get(); } // 各榜标签配置 $tabs = [ 'roster' => ['label' => '任职列表', 'icon' => '🏛️'], 'day' => ['label' => '日榜', 'icon' => '☀️'], 'week' => ['label' => '周榜', 'icon' => '📆'], 'month' => ['label' => '月榜', 'icon' => '🗓️'], 'all' => ['label' => '总榜', 'icon' => '🏆'], ]; return view('duty-hall.index', compact( 'tab', 'tabs', 'currentStaff', 'leaderboard', )); } }