SportOdds.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 Illuminate\Support\Facades\Log;
  7. class SportOdds extends Command
  8. {
  9. /**
  10. * 命令名称和签名
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'sport:odds {is_live=0}';
  15. protected $is_live = 0;
  16. protected $long_status = [
  17. 'Time To Be Defined' => 0,
  18. 'Not Started' => 0,
  19. 'First Half' => 1,
  20. 'First Half, Kick Off' => 1,
  21. 'Halftime' => 1,
  22. 'Second Half' => 1,
  23. 'Second Half, 2nd Half Started' => 1,
  24. 'Extra Time' => 1,
  25. 'Break Time' => 1,
  26. 'Penalty In Progress' => 1,
  27. 'Match Suspended' => 1,
  28. 'Match Interrupted' => 1,
  29. 'Match Finished' => 2,
  30. 'Match Finished' => 2,
  31. 'Match Finished' => 2,
  32. 'Match Postponed' => 3,
  33. 'Match Cancelled' => 4,
  34. 'Match Abandoned' => 4,
  35. 'Technical Loss' => 4,
  36. 'WalkOver' => 4,
  37. 'In Progress' => 1,
  38. ];
  39. protected $fixture_status = [
  40. 'First Half' => 1,
  41. 'First Half, Kick Off' => 1,
  42. 'Halftime' => 1,
  43. 'Second Half' => 1,
  44. 'Second Half, 2nd Half Started' => 1,
  45. 'Extra Time' => 1,
  46. 'Break Time' => 1,
  47. 'Penalty In Progress' => 1,
  48. ];
  49. /**
  50. * 命令描述
  51. *
  52. * @var string
  53. */
  54. protected $description = '赔率(直播赔率5秒更新一次,塞前赔率3小时更新一次)';
  55. /**
  56. * 执行命令
  57. *
  58. * @return int
  59. */
  60. public function handle()
  61. {
  62. $this->is_live = $this->argument('is_live');
  63. if ($this->is_live == 1) {
  64. $this->info('直播赔率开始执行任务...');
  65. $this->sportOddsData($this->is_live);
  66. } else {
  67. $this->info('开赛前赔率开始执行任务...');
  68. $this->sportOddsData($this->is_live);
  69. }
  70. $this->info('结束执行赔率任务');
  71. }
  72. public function sportOddsData($is_live)
  73. {
  74. try {
  75. $limit = 100;
  76. //直播赔率(获取正在直播的所有赛事的赔率)
  77. if ($is_live == 1) {
  78. $data = SportClientService::oddsLive([]);
  79. // $data = SportClientService::oddsLive(['fixture' => '1537245']);
  80. // $data = SportClientService::odds(['fixture' => '1537245']);
  81. // die;
  82. file_put_contents("oddsLive.json",json_encode($data));
  83. $this->updateOddsLive($data);
  84. } else {
  85. //按照日期获取赔率
  86. // $data = SportClientService::odds([
  87. // 'date' => date('Y-m-d'),
  88. // ]);
  89. // die;
  90. //普通球赛是开赛前购买,更新数据
  91. $where['state'] = 0; //比赛状态:0未开始1进行中2已完场3延期4取消
  92. $list = SportModel::where($where)->where('odds',null)->limit($limit)->get()->toArray();
  93. // $list = SportModel::whereIn('data_id', [1512757, 1525674])->get()->toArray();
  94. foreach($list as $item) {
  95. $data = SportClientService::odds([
  96. 'fixture' => $item['data_id'],
  97. ]);
  98. // file_put_contents("oddsToday.json",json_encode($data));
  99. $this->updateOdds($item['data_id'],$data);
  100. }
  101. }
  102. } catch (\Exception $e) {
  103. Log::error($e->getMessage());
  104. }
  105. return true;
  106. }
  107. public function updateOddsLive($data)
  108. {
  109. if(!empty($data['response'])) {
  110. $data = $data['response'];
  111. foreach($data as $item) {
  112. $data_id = $item['fixture']['id'];
  113. $odds = json_encode($item['odds']);
  114. $update_data = [
  115. 'odds' => $odds,
  116. 'is_send' => 0,
  117. ];
  118. $fixture_status = null;
  119. if (isset($item['fixture']['status']['long']) && isset($this->$fixture_status[$item['fixture']['status']['long']])) {
  120. $fixture_status = json_encode($item['fixture']['status']);
  121. $update_data['fixture_status'] = $fixture_status;
  122. if (isset($this->long_status[$item['fixture']['status']['long']])) {
  123. $update_data['state'] = $this->long_status[$item['fixture']['status']['long']];
  124. }
  125. }
  126. if (isset($item['teams']['home']['goals'])) {
  127. $update_data['score'] = $item['teams']['home']['goals'] ."-". $item['teams']['away']['goals'];
  128. }
  129. //锁盘
  130. if (isset($item['fixture']['status']['blocked']) && $item['fixture']['status']['blocked']) {
  131. $update_data['is_locked'] = 1;
  132. }
  133. //已结束
  134. if (isset($item['fixture']['status']['finished']) && $item['fixture']['status']['finished']) {
  135. $update_data['state'] = 2;
  136. }
  137. SportModel::where('data_id',$data_id)->update($update_data);
  138. echo $data_id.": 更新成功\r\n";
  139. }
  140. }
  141. }
  142. public function updateOdds($data_id,$data)
  143. {
  144. echo $data_id;
  145. if (!empty($data['response'][0]['bookmakers'][0]['bets'])) {
  146. $odds = $data['response'][0]['bookmakers'][0]['bets'];
  147. echo "更新成功\r\n";
  148. SportModel::where('data_id',$data_id)->update(['odds' => json_encode($odds), 'is_send' => 0]);
  149. } else {
  150. echo "更新失败\r\n";
  151. file_put_contents('odds-error.log', json_encode($data)."\r\n\r\n", FILE_APPEND);
  152. }
  153. }
  154. }