subMinutes(15); // 批量关闭符合条件的开放日志,保留现有 duration_seconds $affected = PositionDutyLog::query() ->whereNull('logout_at') ->where('updated_at', '<=', $threshold) ->update([ 'logout_at' => DB::raw('NOW()'), // 补算最终在线时长,避免日榜 SUM 使用过时的旧值 'duration_seconds' => DB::raw('GREATEST(0, TIMESTAMPDIFF(SECOND, login_at, NOW()))'), ]); if ($affected > 0) { $this->info("共关闭 {$affected} 条掉线职务日志。"); } else { $this->info('无需处理,无掉线日志。'); } return Command::SUCCESS; } }