whereNull('delete_time') ->order('create_time', 'asc') ->order('id', 'asc') ->find(); MatchEvent::where('id', $matchId)->update([ 'live_url' => $row ? (string) $row->source_url : '', 'update_time' => time(), ]); } public static function getLiveListForApi(int $matchId): array { $liveMap = self::getLiveMapForApi([$matchId]); return $liveMap[$matchId] ?? []; } public static function getLiveMapForApi(array $matchIds): array { $matchIds = array_values(array_unique(array_filter(array_map('intval', $matchIds)))); if (empty($matchIds)) { return []; } $rows = MatchLive::whereIn('match_id', $matchIds) ->whereNull('delete_time') ->order('create_time', 'asc') ->order('id', 'asc') ->select() ->toArray(); $result = []; foreach ($rows as &$row) { $row['stream_options'] = self::buildStreamOptions($row); $row['default_play_url'] = self::resolveDefaultPlayUrl($row); $matchId = (int) ($row['match_id'] ?? 0); if ($matchId <= 0) { continue; } $result[$matchId][] = $row; } unset($row); return $result; } public static function buildStreamOptions(array $row): array { $definitions = [ ['label' => 'M3U8', 'field' => 'play_url_m3u8'], ['label' => 'HD M3U8', 'field' => 'play_url_hd_m3u8'], ['label' => 'FLV', 'field' => 'play_url_flv'], ['label' => 'HD FLV', 'field' => 'play_url_hd_flv'], ]; $options = []; foreach ($definitions as $definition) { $url = trim((string) ($row[$definition['field']] ?? '')); if ($url === '') { continue; } $options[] = [ 'label' => $definition['label'], 'url' => $url, ]; } return $options; } protected static function resolveDefaultPlayUrl(array $row): string { if (!empty($row['stream_options'][0]['url'])) { return trim((string) $row['stream_options'][0]['url']); } foreach (['play_url_m3u8', 'play_url_hd_m3u8', 'play_url_flv', 'play_url_hd_flv'] as $field) { $url = trim((string) ($row[$field] ?? '')); if ($url !== '') { return $url; } } return ''; } public static function getLiveCards( int $sportType = 0, string $leagueName = '', bool $worldCupOnly = false ): array { $query = MatchLive::alias('live') ->join('match m', 'm.id = live.match_id') ->where(['m.is_show' => 1]) ->where([['m.status', '<>', 2]]); if ($sportType > 0) { $query->where('m.sport_type', $sportType); } if ($leagueName !== '') { $query->where('m.league_name', $leagueName); } if ($worldCupOnly) { $query ->where(function ($query) { $query->where('m.league_name', '世界杯') ->whereOr('m.competition_id', 61); }); } $rows = $query ->field([ 'live.id as live_id', 'live.match_id', 'live.title', 'live.source_url', 'live.anchor_name', 'live.anchor_avatar', 'live.room_view_count', 'live.room_focus_count', 'live.room_notice', 'live.play_url_m3u8', 'live.play_url_hd_m3u8', 'live.play_url_flv', 'live.play_url_hd_flv', 'live.fetch_status', 'live.last_fetch_at', 'live.update_time as live_update_time', 'm.id', 'm.match_id as third_match_id', 'm.competition_id', 'm.league_name', 'm.round_name', 'm.stage', 'm.home_team', 'm.home_icon', 'm.home_score', 'm.away_team', 'm.away_icon', 'm.away_score', 'm.status', 'm.match_time', 'm.current_minute', 'm.half_score', ]) ->order('m.match_time', 'asc') ->order('live.create_time', 'asc') ->order('live.id', 'asc') ->select() ->toArray(); $cards = []; foreach ($rows as $row) { $row['stream_options'] = self::buildStreamOptions($row); $row['default_play_url'] = self::resolveDefaultPlayUrl($row); if ($row['default_play_url'] === '') { continue; } $cards[] = [ 'live_id' => (int) $row['live_id'], 'match_id' => (int) $row['match_id'], 'title' => (string) ($row['title'] ?? ''), 'source_url' => (string) ($row['source_url'] ?? ''), 'anchor_name' => (string) ($row['anchor_name'] ?? ''), 'anchor_avatar' => (string) ($row['anchor_avatar'] ?? ''), 'room_view_count' => (int) ($row['room_view_count'] ?? 0), 'room_focus_count' => (int) ($row['room_focus_count'] ?? 0), 'room_notice' => (string) ($row['room_notice'] ?? ''), 'play_url' => (string) $row['default_play_url'], 'default_play_url' => (string) $row['default_play_url'], 'stream_options' => $row['stream_options'], 'fetch_status' => $row['fetch_status'] ?? '', 'last_fetch_at' => $row['last_fetch_at'] ?? '', 'update_time' => $row['live_update_time'] ?? '', 'id' => (int) $row['id'], 'competition_id' => (int) ($row['competition_id'] ?? 0), 'league_name' => (string) ($row['league_name'] ?? ''), 'round_name' => (string) ($row['round_name'] ?? ''), 'stage' => (string) ($row['stage'] ?? ''), 'home_team' => (string) ($row['home_team'] ?? ''), 'home_icon' => (string) ($row['home_icon'] ?? ''), 'home_score' => (int) ($row['home_score'] ?? 0), 'away_team' => (string) ($row['away_team'] ?? ''), 'away_icon' => (string) ($row['away_icon'] ?? ''), 'away_score' => (int) ($row['away_score'] ?? 0), 'status' => (int) ($row['status'] ?? 0), 'match_time' => (int) ($row['match_time'] ?? 0), 'current_minute' => $row['current_minute'] ?? '', 'half_score' => (string) ($row['half_score'] ?? ''), ]; } return $cards; } public static function getWorldCupLiveCards(): array { return self::getLiveCards(0, '', true); } }