|
|
@@ -5,6 +5,7 @@
|
|
|
use App\Services\BaseService;
|
|
|
use App\Models\Timezone;
|
|
|
use App\Models\Country;
|
|
|
+use App\Models\League;
|
|
|
|
|
|
class SyncFootballDataService extends BaseService
|
|
|
{
|
|
|
@@ -77,4 +78,60 @@ public static function syncCountry()
|
|
|
|
|
|
return $result;
|
|
|
}
|
|
|
+
|
|
|
+ public static function syncLeagues($params = [])
|
|
|
+ {
|
|
|
+ $result = app('api-football')->leagues($params);
|
|
|
+
|
|
|
+ $result = json_decode(file_get_contents(storage_path('logs/leagues.log')), true);
|
|
|
+ $response = $result['response'] ?? [];
|
|
|
+
|
|
|
+ if (empty($response)) {
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ // // 先将所有联赛设为无效
|
|
|
+ // League::query()->update(['status' => League::STATUS_NO]);
|
|
|
+ foreach ($response as $item) {
|
|
|
+ $is_active = League::IS_ACTIVE_NOT;
|
|
|
+ foreach ($item['seasons'] as $season) {
|
|
|
+ // 处理赛季数据
|
|
|
+ if ($season['current']) {
|
|
|
+ $is_active = League::IS_ACTIVE_YES;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用 updateOrCreate 简化代码
|
|
|
+ $league = League::updateOrCreate(
|
|
|
+ ['league_id' => $item['league']['id']],
|
|
|
+ [
|
|
|
+ 'name' => $item['league']['name'],
|
|
|
+ 'type' => $item['league']['type'],
|
|
|
+ 'logo' => $item['league']['logo'],
|
|
|
+ 'country_name' => $item['country']['name'],
|
|
|
+ 'country_code' => $item['country']['code'],
|
|
|
+ 'country_flag' => $item['country']['flag'],
|
|
|
+ 'seasons' => json_encode($item['seasons'] ?? []),
|
|
|
+ 'is_active' => $is_active,
|
|
|
+ 'last_synced_at' => now(),
|
|
|
+ ]
|
|
|
+ );
|
|
|
+
|
|
|
+ // // 如果已存在,更新状态和其他字段
|
|
|
+ // if (!$league->wasRecentlyCreated) {
|
|
|
+ // $league->update([
|
|
|
+ // 'name' => $item['league']['name'],
|
|
|
+ // 'type' => $item['league']['type'],
|
|
|
+ // 'logo' => $item['league']['logo'],
|
|
|
+ // 'country_name' => $item['country']['name'],
|
|
|
+ // 'country_code' => $item['country']['code'],
|
|
|
+ // 'country_flag' => $item['country']['flag'],
|
|
|
+ // 'seasons' => json_encode($item['seasons'] ?? []),
|
|
|
+ // 'is_active' => $is_active,
|
|
|
+ // ]);
|
|
|
+ // }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
}
|