| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\Models\Sport as SportModel;
- use App\Services\SportClientService;
- use Illuminate\Support\Facades\Log;
- use App\Models\Config;
- class SportOdds extends Command
- {
- /**
- * 命令名称和签名
- *
- * @var string
- */
- protected $signature = 'sport:odds {is_live=0}';
- protected $is_live = 0;
-
- protected $long_status = [
- 'Time To Be Defined' => 0,
- 'Not Started' => 0,
- 'First Half' => 1,
- 'First Half, Kick Off' => 1,
- 'Halftime' => 1,
- 'Second Half' => 1,
- 'Second Half, 2nd Half Started' => 1,
- 'Extra Time' => 1,
- 'Break Time' => 1,
- 'Penalty In Progress' => 1,
- 'Match Suspended' => 1,
- 'Match Interrupted' => 1,
- 'Match Finished' => 2,
- 'Match Finished' => 2,
- 'Match Finished' => 2,
- 'Match Postponed' => 3,
- 'Match Cancelled' => 4,
- 'Match Abandoned' => 4,
- 'Technical Loss' => 4,
- 'WalkOver' => 4,
- 'In Progress' => 1,
- ];
-
- protected $fixture_status = [
- 'First Half' => 1,
- 'First Half, Kick Off' => 1,
- 'Halftime' => 1,
- 'Second Half' => 1,
- 'Second Half, 2nd Half Started' => 1,
- 'Extra Time' => 1,
- 'Break Time' => 1,
- 'Penalty In Progress' => 1,
- ];
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '赔率(直播赔率5秒更新一次,塞前赔率3小时更新一次)';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $this->is_live = $this->argument('is_live');
- if ($this->is_live == 1) {
- // $this->info('直播赔率开始执行任务...');
- $this->sportOddsData($this->is_live);
- } else {
- // $this->info('开赛前赔率开始执行任务...');
- $this->sportOddsData($this->is_live);
- }
-
- // $this->info('结束执行赔率任务');
- }
- public function sportOddsData($is_live)
- {
- try {
- $limit = 3000;
- //直播赔率(获取正在直播的所有赛事的赔率)
- if ($is_live == 1) {
- $data = SportClientService::oddsLive([]);
- // file_put_contents("oddsLive.json",json_encode($data));
- $this->updateOddsLive($data);
- } else {
- //普通球赛是开赛前购买,更新数据
- $where['state'] = 0; //比赛状态:0未开始1进行中2已完场3延期4取消
- $list = SportModel::where($where)->where('odds',null)->limit($limit)->get()->toArray();
- foreach($list as $item) {
- $data = SportClientService::odds([
- 'fixture' => $item['data_id'],
- ]);
- $this->updateOdds($item['data_id'],$data);
- }
- }
- } catch (\Exception $e) {
- Log::error($e->getMessage());
- }
- return true;
- }
- public function updateOddsLive($data)
- {
- //体育赛事结束前几(分钟)锁盘,90分钟结束
- $sport_locked = Config::where('field', 'sport_locked')->first()->val ?? 1;
- if(!empty($data['response'])) {
- $data = $data['response'];
- foreach($data as $item) {
- $data_id = $item['fixture']['id'];
- $odds = json_encode($item['odds']);
-
- $update_data = [
- 'is_send' => 0,
- 'is_roll' => 1,
- 'is_locked' => 0,
- ];
- $fixture_status = null;
- if (isset($item['fixture']['status']['long']) ) {
- $long = $item['fixture']['status']['long'];
- if (isset($this->fixture_status[$long])) {
- $fixture_status = json_encode($item['fixture']['status']);
- $update_data['fixture_status'] = $fixture_status;
- }
-
- if (isset($this->long_status[$long])) {
- $update_data['state'] = $this->long_status[$long];
- }
- }
- if (isset($item['teams']['home']['goals'])) {
- $update_data['score'] = $item['teams']['home']['goals'] ."-". $item['teams']['away']['goals'];
- }
-
- //锁盘
- if (isset($item['fixture']['status']['blocked']) && $item['fixture']['status']['blocked']) {
- $update_data['is_locked'] = 1;
- }
- //提前锁盘(比赛进行时长,分钟)
- if (isset($item['fixture']['status']['elapsed'])) {
- $elapsed = $item['fixture']['status']['elapsed'];
- if ((int)$elapsed >= 90 - $sport_locked ) {
- $update_data['is_locked'] = 1;
- }
- }
- //已结束
- if (isset($item['fixture']['status']['finished']) && $item['fixture']['status']['finished']) {
- $update_data['state'] = 2;
- }
- //如果赛事取消、延期等,标记需要退款
- if (isset($update_data['state']) && $update_data['state'] > 2) {
- $update_data['refund_status'] = 1;
- }
- $update_data['odds'] = $odds;
- SportModel::where('data_id',$data_id)->update($update_data);
- // echo $data_id.": 更新成功\r\n";
- }
- }
- }
- public function updateOdds($data_id,$data)
- {
- // echo $data_id;
- if (!empty($data['response'][0]['bookmakers'][0]['bets'])) {
- $odds = $data['response'][0]['bookmakers'][0]['bets'];
- // echo "更新成功\r\n";
- SportModel::where('data_id',$data_id)->update(['odds' => json_encode($odds), 'is_send' => 0]);
- } else {
- // echo "更新失败\r\n";
- }
- }
- }
|