seven 1 месяц назад
Родитель
Сommit
ecf41eb94a

+ 32 - 0
app/Http/Controllers/Admin/TestController.php

@@ -0,0 +1,32 @@
+<?php
+
+
+namespace App\Http\Controllers\Admin;
+
+use App\Constants\HttpStatus;
+use App\Http\Controllers\Controller;
+use App\Models\Config;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Validation\ValidationException;
+use Exception;
+
+use App\Services\SyncFootballDataService;
+
+class TestController extends Controller
+{
+    public function index()
+    {
+        $params = [
+            'type' => 'league',
+        ];
+        // $result = app('api-football')->leagues($params);
+        // file_put_contents(storage_path('logs/leagues.log'),json_encode($result) . PHP_EOL, FILE_APPEND);
+
+        // $result = json_decode(file_get_contents(storage_path('logs/leagues.log')), true);
+        // echo '<pre>';
+        // var_dump($result);
+        // return $this->success($result);
+
+        SyncFootballDataService::syncLeagues($params);
+    }
+}

+ 19 - 0
app/Models/League.php

@@ -0,0 +1,19 @@
+<?php
+
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Model;
+
+class League extends BaseModel
+{
+    protected $table = 'leagues';
+    protected $fillable = ['league_id', 'name', 'type', 'logo', 'country_name', 'country_code', 'country_flag', 'seasons', 'is_active', 'last_synced_at'];
+
+    protected $casts = [
+        'seasons' => 'array',
+    ];
+
+    const IS_ACTIVE_YES = 1;
+    const IS_ACTIVE_NOT = 0;
+}

+ 6 - 0
app/Services/ApiFootball/Client.php

@@ -57,6 +57,12 @@ public static function leagues($params = [])
         return static::get('leagues', $params);
     }
 
+    // 联赛赛季  获取特定联赛的赛季列表。
+    public static function leaguesSeasons($params = [])
+    {
+        return static::get('leagues/seasons', $params);
+    }
+
     public static function fixturesRounds($params = [])
     {
         return static::get('fixtures/rounds', $params);

+ 18 - 3
app/Services/ApiFootball/Sdk.php

@@ -17,9 +17,9 @@ public function timezone()
 
     /**
      * @description: 获取国家/地区数据
-     * @param {array} $params.name 国家/地区名称(可选)
-      * @param {array} $params.code 国家/地区代码(可选) 
-      * @param {array} $params.search 国家/地区名称(可选) 
+     * @param {string} $params.name 国家/地区名称(可选)
+     * @param {string} $params.code 国家/地区代码(可选)
+     * @param {string} $params.search 国家/地区名称(可选)
      * @return {array}
      */    
     public function countries($params = [])
@@ -28,6 +28,21 @@ public function countries($params = [])
         return $result;
     }
 
+    /**
+     * @description: 获取联赛数据
+     * @param {*} $params
+     * @return {array}
+     */    
+    public function leagues($params = [])
+    {
+        return Client::leagues($params);
+    }
+
+    public function leaguesSeasons($params = [])
+    {
+        return Client::leaguesSeasons($params);
+    }
+
     public function fixtures($params = [])
     {
         return Client::fixtures($params);

+ 57 - 0
app/Services/SyncFootballDataService.php

@@ -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;
+    }
 }

+ 47 - 0
database/migrations/2026_03_04_172400_create_leagues_table.php

@@ -0,0 +1,47 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::create('leagues', function (Blueprint $table) {
+            $table->id();
+            // 来自 API 的联赛唯一标识
+            $table->unsignedBigInteger('league_id')->unique()->comment('API-Sports 联赛唯一ID');
+            
+            // 联赛基本信息
+            $table->string('name', 100)->comment('联赛名称 e.g. "Ligue 2", "Premier League"');
+            $table->string('type', 50)->nullable()->comment('League / Cup 等');
+            $table->string('logo', 255)->nullable()->comment('联赛logo URL');
+
+            // 国家信息(扁平化,方便按国家筛选)
+            $table->string('country_name', 100)->comment('国家名称');
+            $table->string('country_code', 25)->nullable()->comment('国家代码');
+            $table->string('country_flag', 255)->nullable()->comment('国旗 SVG/PNG URL');
+
+            // 赛季信息 - 直接存 JSON 数组
+            // 示例结构:[{"year":2010,"start":"2010-08-06","end":"2011-05-20","current":false,"coverage":{...}}, ...]
+            $table->json('seasons')->nullable()->comment('赛季列表 JSON');
+
+            // 辅助字段(根据实际业务需要)
+            $table->boolean('is_active')->default(true)->comment('是否当前活跃联赛');
+            $table->timestamp('last_synced_at')->nullable()->comment('最后从API同步时间');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::dropIfExists('leagues');
+    }
+};

+ 6 - 4
routes/admin.php

@@ -5,8 +5,10 @@
 
 use Illuminate\Support\Facades\Route;
 
-use App\Services\SyncFootballDataService;
+use App\Http\Controllers\Admin\TestController;
 
-Route::get('/test', function () {
-    return SyncFootballDataService::syncCountry();
-});
+// Route::get('/test', function () {
+//     return SyncFootballDataService::syncCountry();
+// });
+
+Route::get('/test', [TestController::class, 'index']);