Files
sbnews/server/app/adminapi/logic/match/MatchLiveLogic.php
T

98 lines
2.9 KiB
PHP

<?php
namespace app\adminapi\logic\match;
use app\common\logic\BaseLogic;
use app\common\model\match\MatchLive;
use app\common\service\match\MatchLiveService;
class MatchLiveLogic extends BaseLogic
{
public static function detail(array $params): array
{
return MatchLive::findOrEmpty($params['id'])->toArray();
}
public static function add(array $params): bool
{
try {
$now = time();
MatchLive::create([
'match_id' => (int) $params['match_id'],
'title' => trim((string) $params['title']),
'source_url' => trim((string) $params['source_url']),
'fetch_status' => 0,
'fetch_error' => '',
'last_fetch_at' => 0,
'next_fetch_at' => $now,
'create_time' => $now,
'update_time' => $now,
]);
MatchLiveService::syncLegacyLiveUrl((int) $params['match_id']);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function edit(array $params): bool
{
try {
$live = MatchLive::findOrEmpty($params['id']);
if ($live->isEmpty()) {
self::setError('直播线路不存在');
return false;
}
$oldMatchId = (int) $live->match_id;
$newMatchId = (int) $params['match_id'];
$now = time();
$live->save([
'match_id' => $newMatchId,
'title' => trim((string) $params['title']),
'source_url' => trim((string) $params['source_url']),
'play_url_m3u8' => '',
'play_url_hd_m3u8' => '',
'play_url_flv' => '',
'play_url_hd_flv' => '',
'fetch_status' => 0,
'fetch_error' => '',
'last_fetch_at' => 0,
'next_fetch_at' => $now,
'update_time' => $now,
]);
if ($oldMatchId > 0 && $oldMatchId !== $newMatchId) {
MatchLiveService::syncLegacyLiveUrl($oldMatchId);
}
MatchLiveService::syncLegacyLiveUrl($newMatchId);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
public static function delete(array $params): bool
{
try {
$live = MatchLive::findOrEmpty($params['id']);
if ($live->isEmpty()) {
self::setError('直播线路不存在');
return false;
}
$matchId = (int) $live->match_id;
$live->delete();
MatchLiveService::syncLegacyLiveUrl($matchId);
return true;
} catch (\Exception $e) {
self::setError($e->getMessage());
return false;
}
}
}