|
@@ -4,9 +4,12 @@
|
|
|
|
|
|
|
|
use App\Services\BaseService;
|
|
use App\Services\BaseService;
|
|
|
use App\Models\Timezone;
|
|
use App\Models\Timezone;
|
|
|
|
|
+use App\Models\Country;
|
|
|
|
|
|
|
|
class SyncFootballDataService extends BaseService
|
|
class SyncFootballDataService extends BaseService
|
|
|
{
|
|
{
|
|
|
|
|
+
|
|
|
|
|
+ // 同步时区数据
|
|
|
public static function syncTimezone()
|
|
public static function syncTimezone()
|
|
|
{
|
|
{
|
|
|
$result = app('api-football')->timezone();
|
|
$result = app('api-football')->timezone();
|
|
@@ -37,4 +40,42 @@ public static function syncTimezone()
|
|
|
|
|
|
|
|
return $result;
|
|
return $result;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 同步国家/地区数据
|
|
|
|
|
+ public static function syncCountry()
|
|
|
|
|
+ {
|
|
|
|
|
+ $result = app('api-football')->countries();
|
|
|
|
|
+ $response = $result['response'] ?? [];
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($response)) {
|
|
|
|
|
+ return $result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 先将所有国家/地区设为无效
|
|
|
|
|
+ Country::query()->update(['status' => Country::STATUS_NO]);
|
|
|
|
|
+
|
|
|
|
|
+ foreach ($response as $item) {
|
|
|
|
|
+ // 使用 firstOrCreate 简化代码
|
|
|
|
|
+ $country = Country::firstOrCreate(
|
|
|
|
|
+ ['code' => $item['code']],
|
|
|
|
|
+ [
|
|
|
|
|
+ 'name' => $item['name'],
|
|
|
|
|
+ 'code' => $item['code'],
|
|
|
|
|
+ 'flag' => $item['flag'],
|
|
|
|
|
+ 'status' => Country::STATUS_YES
|
|
|
|
|
+ ]
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // 如果已存在,更新状态和其他字段
|
|
|
|
|
+ if (!$country->wasRecentlyCreated) {
|
|
|
|
|
+ $country->update([
|
|
|
|
|
+ 'name' => $item['name'],
|
|
|
|
|
+ 'flag' => $item['flag'],
|
|
|
|
|
+ 'status' => Country::STATUS_YES
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return $result;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|