| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\Models\Sport as SportModel;
- use App\Services\SportClientService;
- use Carbon\Carbon;
- class Sport extends Command
- {
- /**
- * 命令名称和签名
- *
- * @var string
- */
- protected $signature = 'sport';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '当天会去更新明天的赛事(23:59:00执行一次)';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle()
- {
- $this->info('开始执行统计比赛数据任务...');
- $this->fixtures();
-
- $this->info('结束执行统计比赛数据任务');
- }
- public function sportData()
- {
- try {
- $today = date('Y-m-d');
- $host = env('SPORT_HOST');
- $url = "{$host}/api/sport";
- $url .= "?date={$today}";
- $result = file_get_contents($url);
- $result = $result ? json_decode($result, true) : [];
- foreach ($result as $item) {
- $info = SportModel::where('data_id',$item['data_id'])->first();
- $data = [
- 'home_team_id' => $item['home_team_id'] ?? '',
- 'home_team_en' => $item['home_team_en'] ?? '',
- 'home_team' => $item['home_team'] ?? '',
- 'home_team_logo' => $item['home_team_logo'] ?? '',
- 'guest_team_id' => $item['guest_team_id'] ?? '',
- 'guest_team_en' => $item['guest_team_en'] ?? '',
- 'guest_team' => $item['guest_team'] ?? '',
- 'guest_team_logo' => $item['guest_team_logo'] ?? '',
- 'half_score' => $item['half_score'] ?? '',
- 'rbt' => $item['rbt'] ?? '',
- 'is_roll' => $item['is_roll'] ?? 0,
- 'score' => $item['score'] ?? '',
- 'league_en' => $item['league_en'] ?? '',
- 'league' => $item['league'] ?? '',
- 'odds' => $item['odds'] ?? '',
- 'state' => $item['state'] ?? 0,
- 'game_time' => $item['game_time'] ?? 0,
- 'status' => $item['status'] ?? 0,
- 'handicap_limit' => $item['handicap_limit'] ?? '',
- 'over_under_limit' => $item['over_under_limit'] ?? '',
- 'duying_limit' => $item['duying_limit'] ?? '',
- 'correct_core_limit' => $item['correct_core_limit'] ?? '',
- 'odd_even_limit' => $item['odd_even_limit'] ?? '',
- 'total_goal_limit' => $item['total_goal_limit'] ?? '',
- 'is_handicap' => $item['is_handicap'] ?? 0,
- 'is_over_under' => $item['is_over_under'] ?? 0,
- 'is_duying' => $item['is_duying'] ?? 0,
- 'is_correct_core' => $item['is_correct_core'] ?? 0,
- 'is_odd_even' => $item['is_odd_even'] ?? 0,
- 'is_total_goal' => $item['is_total_goal'] ?? 0,
- 'is_locked' => $item['is_locked'] ?? 0,
- ];
- if ($info) {
- //更新数据
- SportModel::where('data_id',$item['data_id'])->update($data);
- } else {
- $data['data_id'] = $item['data_id'];
- SportModel::create($data);
- }
- }
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
-
-
-
- return true;
- }
- /**
- * 获取指定日期的所有赛事
- *
- * @return array
- */
- public function fixtures()
- {
- $date = Carbon::tomorrow()->toDateString();
- $data = SportClientService::fixtures(['date' => $date]);
- print_r($data);die;
- $data = $data['response'];
- $tableData = [];
- $status = ['NS' => 0, '1H' => 1, 'HT' => 1, '2H' => 1, 'ET' => 1, 'BT' => 1, 'P' => 1, 'SUSP' => 1, 'INT' => 1, 'LIVE' => 1, 'FT' => 2, 'AET' => 2, 'PEN' => 2, 'PST' => 3, 'CANC' => 4, 'ABD' => 4,];
- foreach ($data as $item) {
- if (!Sport::where('data_id', $item['fixture']['id'])->exists()) {
- $tableData[] = [
- 'data_id' => $item['fixture']['id'],
- 'home_team_id' => $item['teams']['home']['id'],
- 'home_team_en' => $item['teams']['home']['name'],
- 'home_team' => lang($item['teams']['home']['name']),
- 'home_team_logo' => $item['teams']['home']['logo'],
- 'guest_team_id' => $item['teams']['away']['id'],
- 'guest_team_en' => $item['teams']['away']['name'],
- 'guest_team' => lang($item['teams']['away']['name']),
- 'guest_team_logo' => $item['teams']['away']['logo'],
- 'half_score' => "{$item['score']['halftime']['home']}-{$item['score']['halftime']['away']}",
- 'rbt' => $item['fixture']['timestamp'],
- 'score' => "{$item['goals']['home']}-{$item['goals']['away']}",
- 'league' => lang($item['league']['name']),
- 'league_en' => $item['league']['name'],
- 'state' => $status[$item['fixture']['status']['short']],//比赛状态:0未开始1进行中2已完场3延期4取消
- 'game_time' => $item['fixture']['timestamp'],
- 'created_at' => now(),
- 'updated_at' => now(),
- ];
- }
- }
- Sport::insert($tableData);
- return $tableData;
- }
- }
|