| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\Models\Sport as SportModel;
- use App\Services\SportClientService;
- use Illuminate\Support\Facades\Log;
- 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,
- ];
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '赔率(直播赔率5秒更新一次,塞前赔率3小时更新一次)';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $data = SportClientService::odds([
- 'fixture' => '1526463',
- ]);
- file_put_contents('odds-1526463.json', json_encode($data, JSON_UNESCAPED_UNICODE));
- die;
- $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 = 100;
- //直播赔率(获取正在直播的所有赛事的赔率)
- if ($is_live == 1) {
- $data = SportClientService::oddsLive([]);
- $this->updateOddsLive($data);
- } else {
- //按照日期获取赔率
- // $data = SportClientService::odds([
- // 'date' => date('Y-m-d'),
- // ]);
- //普通球赛是开赛前购买,更新数据
- $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)
- {
- if(!empty($data['response'])) {
- $data = $data['response'];
- foreach($data as $item) {
- $data_id = $item['fixture']['id'];
- $long_status = $item['fixture']['status']['long'];
- $state = isset($this->long_status[$long_status]) ? $this->long_status[$long_status] : null;
- $odds = $item['odds'];
- $update_data = [
- 'odds' => $odds,
- ];
- if ($state) {
- $update_data = [
- 'state' => $state,
- '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)]);
- } else {
- echo "更新失败\r\n";
- file_put_contents('odds-error.log', json_encode($data)."\r\n\r\n", FILE_APPEND);
- }
- }
- }
|