| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace App\Services;
- use App\Models\Sport;
- use App\Services\ApiFootball\Client;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Cache;
- class FixtureService extends BaseService
- {
- /**
- * 赛前赔率
- * 3小时更新一次
- *
- * @param $id integer|string 赛事ID
- */
- static function odds(int|string $id)
- {
- $key = "odds_{$id}";
- $odds = Cache::get($key);
- if ($odds) return json_decode($odds, true);
- $res = Client::odds(['fixture' => $id]);
- if (count($res['response']) <= 0) return [];
- $res = $res['response'][0];
- Cache::put($key, json_encode($res), now()->addHours(3));
- return $res;
- }
- /**
- * 比赛中的赔率
- * 5秒更新一次
- *
- * @param $id integer|string 赛事ID
- */
- static function oddsLive($id)
- {
- $key = "odds_live_{$id}";
- $odds = Cache::get($key);
- if ($odds) return json_decode($odds, true);
- $res = Client::oddsLive(['fixture' => $id]);
- if (count($res['response']) <= 0) return [];
- $res = $res['response'][0];
- Cache::put($key, json_encode($res), 5);
- return $res;
- }
- /**
- * 将第二天的赛事插入到数据库
- *
- * @return array
- */
- static function index()
- {
- $date = Carbon::tomorrow()->toDateString();
- $data = Client::fixtures(['date' => $date]);
- $data = $data['response'];
- $tableData = [];
- $status = ['NS' => 0, '1H' => 1, 'HT' => 1, '2H' => 1, 'ET' => 1, 'BT' => 1, 'P' => 1, 'SUSP' => 1, 'INT' => 1, 'LIVE' => 1, 'FT' => 2, 'AET' => 2, 'PEN' => 2, 'PST' => 3, 'CANC' => 4, 'ABD' => 4,];
- foreach ($data as $item) {
- if (!Sport::where('data_id', $item['fixture']['id'])->exists()) {
- $tableData[] = [
- 'data_id' => $item['fixture']['id'],
- 'home_team_id' => $item['teams']['home']['id'],
- 'home_team_en' => $item['teams']['home']['name'],
- 'home_team' => lang($item['teams']['home']['name']),
- 'home_team_logo' => $item['teams']['home']['logo'],
- 'guest_team_id' => $item['teams']['away']['id'],
- 'guest_team_en' => $item['teams']['away']['name'],
- 'guest_team' => lang($item['teams']['away']['name']),
- 'guest_team_logo' => $item['teams']['away']['logo'],
- 'half_score' => "{$item['score']['halftime']['home']}-{$item['score']['halftime']['away']}",
- 'rbt' => $item['fixture']['timestamp'],
- 'score' => "{$item['goals']['home']}-{$item['goals']['away']}",
- 'league' => lang($item['league']['name']),
- 'league_en' => $item['league']['name'],
- 'state' => $status[$item['fixture']['status']['short']],//比赛状态:0未开始1进行中2已完场3延期4取消
- 'game_time' => $item['fixture']['timestamp'],
- 'created_at' => now(),
- 'updated_at' => now(),
- ];
- }
- }
- Sport::insert($tableData);
- return $tableData;
- }
- }
|