SportOdds.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. // file_put_contents("oddsLive.json",json_encode($data));
  81. $this->updateOddsLive($data);
  82. } else {
  83. //普通球赛是开赛前购买,更新数据
  84. $where['state'] = 0; //比赛状态:0未开始1进行中2已完场3延期4取消
  85. $list = SportModel::where($where)->where('odds',null)->limit($limit)->get()->toArray();
  86. foreach($list as $item) {
  87. $data = SportClientService::odds([
  88. 'fixture' => $item['data_id'],
  89. ]);
  90. $this->updateOdds($item['data_id'],$data);
  91. }
  92. }
  93. } catch (\Exception $e) {
  94. Log::error($e->getMessage());
  95. }
  96. return true;
  97. }
  98. public function updateOddsLive($data)
  99. {
  100. //体育赛事结束前几(分钟)锁盘,90分钟结束
  101. $sport_locked = Config::where('field', 'sport_locked')->first()->val ?? 1;
  102. if(!empty($data['response'])) {
  103. $data = $data['response'];
  104. foreach($data as $item) {
  105. $data_id = $item['fixture']['id'];
  106. $odds = json_encode($item['odds']);
  107. $update_data = [
  108. 'is_send' => 0,
  109. 'is_roll' => 1,
  110. 'is_locked' => 0,
  111. ];
  112. $fixture_status = null;
  113. if (isset($item['fixture']['status']['long']) ) {
  114. $long = $item['fixture']['status']['long'];
  115. if (isset($this->fixture_status[$long])) {
  116. $fixture_status = json_encode($item['fixture']['status']);
  117. $update_data['fixture_status'] = $fixture_status;
  118. }
  119. if (isset($this->long_status[$long])) {
  120. $update_data['state'] = $this->long_status[$long];
  121. }
  122. }
  123. if (isset($item['teams']['home']['goals'])) {
  124. $update_data['score'] = $item['teams']['home']['goals'] ."-". $item['teams']['away']['goals'];
  125. }
  126. //锁盘
  127. if (isset($item['fixture']['status']['blocked']) && $item['fixture']['status']['blocked']) {
  128. $update_data['is_locked'] = 1;
  129. }
  130. //提前锁盘(比赛进行时长,分钟)
  131. if (isset($item['fixture']['status']['elapsed'])) {
  132. $elapsed = $item['fixture']['status']['elapsed'];
  133. if ((int)$elapsed >= 90 - $sport_locked ) {
  134. $update_data['is_locked'] = 1;
  135. }
  136. }
  137. //已结束
  138. if (isset($item['fixture']['status']['finished']) && $item['fixture']['status']['finished']) {
  139. $update_data['state'] = 2;
  140. }
  141. //如果赛事取消、延期等,标记需要退款
  142. if (isset($update_data['state']) && $update_data['state'] > 2) {
  143. $update_data['refund_status'] = 1;
  144. }
  145. $update_data['odds'] = $odds;
  146. SportModel::where('data_id',$data_id)->update($update_data);
  147. // echo $data_id.": 更新成功\r\n";
  148. }
  149. }
  150. }
  151. public function updateOdds($data_id,$data)
  152. {
  153. // echo $data_id;
  154. if (!empty($data['response'][0]['bookmakers'][0]['bets'])) {
  155. $odds = $data['response'][0]['bookmakers'][0]['bets'];
  156. // echo "更新成功\r\n";
  157. SportModel::where('data_id',$data_id)->update(['odds' => json_encode($odds), 'is_send' => 0]);
  158. } else {
  159. // echo "更新失败\r\n";
  160. }
  161. }
  162. }