FixtureService.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Sport;
  4. use App\Services\ApiFootball\Client;
  5. use Carbon\Carbon;
  6. use Illuminate\Support\Facades\Cache;
  7. class FixtureService extends BaseService
  8. {
  9. /**
  10. * 赛前赔率
  11. * 3小时更新一次
  12. *
  13. * @param $id integer|string 赛事ID
  14. */
  15. static function odds(int|string $id)
  16. {
  17. $key = "odds_{$id}";
  18. $odds = Cache::get($key);
  19. // if ($odds) return json_decode($odds, true);
  20. $res = Client::odds(['fixture' => $id]);
  21. if (count($res['response']) <= 0) return [];
  22. $res = $res['response'][0];
  23. Cache::put($key, json_encode($res), now()->addHours(3));
  24. return $res;
  25. }
  26. /**
  27. * 比赛中的赔率
  28. * 5秒更新一次
  29. *
  30. * @param $id integer|string 赛事ID
  31. */
  32. static function oddsLive($id)
  33. {
  34. $key = "odds_live_{$id}";
  35. $odds = Cache::get($key);
  36. // if ($odds) return json_decode($odds, true);
  37. $res = Client::oddsLive(['fixture' => $id]);
  38. if (count($res['response']) <= 0) return [];
  39. $res = $res['response'][0];
  40. Cache::put($key, json_encode($res), 5);
  41. return $res;
  42. }
  43. /**
  44. * 将第二天的赛事插入到数据库
  45. *
  46. * @return array
  47. */
  48. static function index()
  49. {
  50. $date = Carbon::tomorrow()->toDateString();
  51. $data = Client::fixtures(['date' => $date]);
  52. $data = $data['response'];
  53. $tableData = [];
  54. $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,];
  55. foreach ($data as $item) {
  56. if (!Sport::where('data_id', $item['fixture']['id'])->exists()) {
  57. $tableData[] = [
  58. 'data_id' => $item['fixture']['id'],
  59. 'home_team_id' => $item['teams']['home']['id'],
  60. 'home_team_en' => $item['teams']['home']['name'],
  61. 'home_team' => lang($item['teams']['home']['name']),
  62. 'home_team_logo' => $item['teams']['home']['logo'],
  63. 'guest_team_id' => $item['teams']['away']['id'],
  64. 'guest_team_en' => $item['teams']['away']['name'],
  65. 'guest_team' => lang($item['teams']['away']['name']),
  66. 'guest_team_logo' => $item['teams']['away']['logo'],
  67. 'half_score' => "{$item['score']['halftime']['home']}-{$item['score']['halftime']['away']}",
  68. 'rbt' => $item['fixture']['timestamp'],
  69. 'score' => "{$item['goals']['home']}-{$item['goals']['away']}",
  70. 'league' => lang($item['league']['name']),
  71. 'league_en' => $item['league']['name'],
  72. 'state' => $status[$item['fixture']['status']['short']],//比赛状态:0未开始1进行中2已完场3延期4取消
  73. 'game_time' => $item['fixture']['timestamp'],
  74. 'created_at' => now(),
  75. 'updated_at' => now(),
  76. ];
  77. }
  78. }
  79. Sport::insert($tableData);
  80. return $tableData;
  81. }
  82. }