SportOdds.php 6.2 KB

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