Sport.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Sport as SportModel;
  5. use App\Services\SportClientService;
  6. use Carbon\Carbon;
  7. class Sport extends Command
  8. {
  9. /**
  10. * 命令名称和签名
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'sport';
  15. /**
  16. * 命令描述
  17. *
  18. * @var string
  19. */
  20. protected $description = '当天会去更新明天的赛事(23:59:00执行一次)';
  21. /**
  22. * 执行命令
  23. *
  24. * @return int
  25. */
  26. public function handle()
  27. {
  28. $this->info('开始执行统计比赛数据任务...');
  29. $this->fixtures();
  30. $this->info('结束执行统计比赛数据任务');
  31. }
  32. public function sportData()
  33. {
  34. try {
  35. $today = date('Y-m-d');
  36. $host = env('SPORT_HOST');
  37. $url = "{$host}/api/sport";
  38. $url .= "?date={$today}";
  39. $result = file_get_contents($url);
  40. $result = $result ? json_decode($result, true) : [];
  41. foreach ($result as $item) {
  42. $info = SportModel::where('data_id',$item['data_id'])->first();
  43. $data = [
  44. 'home_team_id' => $item['home_team_id'] ?? '',
  45. 'home_team_en' => $item['home_team_en'] ?? '',
  46. 'home_team' => $item['home_team'] ?? '',
  47. 'home_team_logo' => $item['home_team_logo'] ?? '',
  48. 'guest_team_id' => $item['guest_team_id'] ?? '',
  49. 'guest_team_en' => $item['guest_team_en'] ?? '',
  50. 'guest_team' => $item['guest_team'] ?? '',
  51. 'guest_team_logo' => $item['guest_team_logo'] ?? '',
  52. 'half_score' => $item['half_score'] ?? '',
  53. 'rbt' => $item['rbt'] ?? '',
  54. 'is_roll' => $item['is_roll'] ?? 0,
  55. 'score' => $item['score'] ?? '',
  56. 'league_en' => $item['league_en'] ?? '',
  57. 'league' => $item['league'] ?? '',
  58. 'odds' => $item['odds'] ?? '',
  59. 'state' => $item['state'] ?? 0,
  60. 'game_time' => $item['game_time'] ?? 0,
  61. 'status' => $item['status'] ?? 0,
  62. 'handicap_limit' => $item['handicap_limit'] ?? '',
  63. 'over_under_limit' => $item['over_under_limit'] ?? '',
  64. 'duying_limit' => $item['duying_limit'] ?? '',
  65. 'correct_core_limit' => $item['correct_core_limit'] ?? '',
  66. 'odd_even_limit' => $item['odd_even_limit'] ?? '',
  67. 'total_goal_limit' => $item['total_goal_limit'] ?? '',
  68. 'is_handicap' => $item['is_handicap'] ?? 0,
  69. 'is_over_under' => $item['is_over_under'] ?? 0,
  70. 'is_duying' => $item['is_duying'] ?? 0,
  71. 'is_correct_core' => $item['is_correct_core'] ?? 0,
  72. 'is_odd_even' => $item['is_odd_even'] ?? 0,
  73. 'is_total_goal' => $item['is_total_goal'] ?? 0,
  74. 'is_locked' => $item['is_locked'] ?? 0,
  75. ];
  76. if ($info) {
  77. //更新数据
  78. SportModel::where('data_id',$item['data_id'])->update($data);
  79. } else {
  80. $data['data_id'] = $item['data_id'];
  81. SportModel::create($data);
  82. }
  83. }
  84. } catch (\Exception $e) {
  85. $this->error($e->getMessage());
  86. }
  87. return true;
  88. }
  89. /**
  90. * 获取指定日期的所有赛事
  91. *
  92. * @return array
  93. */
  94. public function fixtures()
  95. {
  96. $date = Carbon::tomorrow()->toDateString();
  97. $data = SportClientService::fixtures(['date' => $date]);
  98. print_r($data);die;
  99. $data = $data['response'];
  100. $tableData = [];
  101. $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,];
  102. foreach ($data as $item) {
  103. if (!Sport::where('data_id', $item['fixture']['id'])->exists()) {
  104. $tableData[] = [
  105. 'data_id' => $item['fixture']['id'],
  106. 'home_team_id' => $item['teams']['home']['id'],
  107. 'home_team_en' => $item['teams']['home']['name'],
  108. 'home_team' => lang($item['teams']['home']['name']),
  109. 'home_team_logo' => $item['teams']['home']['logo'],
  110. 'guest_team_id' => $item['teams']['away']['id'],
  111. 'guest_team_en' => $item['teams']['away']['name'],
  112. 'guest_team' => lang($item['teams']['away']['name']),
  113. 'guest_team_logo' => $item['teams']['away']['logo'],
  114. 'half_score' => "{$item['score']['halftime']['home']}-{$item['score']['halftime']['away']}",
  115. 'rbt' => $item['fixture']['timestamp'],
  116. 'score' => "{$item['goals']['home']}-{$item['goals']['away']}",
  117. 'league' => lang($item['league']['name']),
  118. 'league_en' => $item['league']['name'],
  119. 'state' => $status[$item['fixture']['status']['short']],//比赛状态:0未开始1进行中2已完场3延期4取消
  120. 'game_time' => $item['fixture']['timestamp'],
  121. 'created_at' => now(),
  122. 'updated_at' => now(),
  123. ];
  124. }
  125. }
  126. Sport::insert($tableData);
  127. return $tableData;
  128. }
  129. }