193 lines
7.5 KiB
PHP
193 lines
7.5 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Widgets\Data\LoadEvents;
|
|
|
|
use App\Models\StatistikoEvent;
|
|
use App\Models\StatistikoPlayer;
|
|
|
|
class LoadEventsQuery
|
|
{
|
|
public static function load(array $filter, array $handicap_range, array $total_range, object $match)
|
|
{
|
|
$league_id = (int) $filter['league_id'] ?? 0;
|
|
$team_id = (int) $filter['team_id'] ?? 0;
|
|
$season = $filter['season'] ?? 0;
|
|
$start = $filter['start_date'] ?? 0;
|
|
$end = $filter['end_date'] ?? 0;
|
|
$ss_player_id = (int) $filter['ss_player_id'] ?? 0;
|
|
$player_on_field = $filter['player_on_field'] ?? 'all';
|
|
$similar_past_matches = $filter['similar_past_matches'];
|
|
$statistic_type = $filter['statistic_type'] ?? 'gls';
|
|
$team_position = $filter['team_position'];
|
|
$season_or_date = $filter['season_or_date'] ?? 'season';
|
|
$home_id = $match->home_id;
|
|
$away_id = $match->away_id;
|
|
|
|
if ($statistic_type == 'none') {
|
|
$statistic_type = 'gls';
|
|
}
|
|
|
|
switch ($season_or_date) {
|
|
case 'season':
|
|
$start = 0;
|
|
$end = 0;
|
|
break;
|
|
default:
|
|
$season = 0;
|
|
break;
|
|
}
|
|
|
|
/**
|
|
* IF WITH PLAYER
|
|
*/
|
|
$matches_played = [];
|
|
|
|
if ($ss_player_id) {
|
|
dd('ss_player_id', $ss_player_id);
|
|
$matches_season = StatistikoEvent::where(function ($query) use ($team_id, $team_position) {
|
|
switch ($team_position) {
|
|
case 'home':
|
|
return $query->where('home_id', '=', $team_id);
|
|
case 'away':
|
|
return $query->where('away_id', '=', $team_id);
|
|
default:
|
|
return $query->where('home_id', '=', $team_id)
|
|
->orWhere('away_id', '=', $team_id);
|
|
}
|
|
})->pluck('match_id')->toArray();
|
|
|
|
$matches_played = StatistikoPlayer::query()
|
|
->whereIn('match_id', $matches_season)
|
|
->where('ss_player_id', $ss_player_id)
|
|
->when($player_on_field, function ($query) use ($player_on_field) {
|
|
// 'all' => 'All',
|
|
// 'lineup' => 'Lineup',
|
|
// 'substituted' => 'Substituted',
|
|
// 'notplayed' => 'Not Played',
|
|
switch ($player_on_field) {
|
|
case 'all':
|
|
return $query->where('minutes_played', '>', 0);
|
|
case 'lineup':
|
|
return $query->where('lineup', 'T')->where('minutes_played', '>', 0);
|
|
case 'substituted':
|
|
return $query->where('lineup', 'P')->where('minutes_played', '>', 0);
|
|
default:
|
|
return $query->where('minutes_played', '>', 0);
|
|
}
|
|
})
|
|
->pluck('match_id')->toArray();
|
|
|
|
if (! $matches_played) {
|
|
$matches_played[] = -1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* STATTISTIC DATA
|
|
*/
|
|
|
|
// dd($league_id, $home_id, $away_id);
|
|
$res = StatistikoEvent::with('home')->with('away')
|
|
->where(function ($query) use ($league_id, $similar_past_matches) {
|
|
switch ($similar_past_matches) {
|
|
case 'none':
|
|
case 'league':
|
|
return $query->where('league_id', $league_id);
|
|
}
|
|
})
|
|
->where(function ($query) use ($team_id, $home_id, $away_id, $similar_past_matches) {
|
|
if ($similar_past_matches == 'none') {
|
|
if ($team_id) {
|
|
return $query->where('home_id', '=', $team_id)
|
|
->orWhere('away_id', '=', $team_id);
|
|
} else {
|
|
return $query->where('home_id', '=', $home_id)
|
|
->orWhere('away_id', '=', $home_id)
|
|
->orWhere('home_id', '=', $away_id)
|
|
->orWhere('away_id', '=', $away_id);
|
|
}
|
|
}
|
|
})
|
|
->when($matches_played, function ($query) use ($matches_played, $player_on_field) {
|
|
if ($player_on_field == 'notplayed') {
|
|
return $query->whereNotIn('match_id', $matches_played);
|
|
} else {
|
|
return $query->whereIn('match_id', $matches_played);
|
|
}
|
|
})
|
|
->when($handicap_range, function ($query) use ($handicap_range) {
|
|
return $query->whereBetween('exp_handicap', $handicap_range);
|
|
})->when($total_range, function ($query) use ($total_range) {
|
|
return $query->whereBetween('exp_total', $total_range);
|
|
})->when($start, function ($query) use ($start) {
|
|
return $query->whereDate('starttime', '>=', $start);
|
|
})->when($end, function ($query) use ($end) {
|
|
return $query->whereDate('starttime', '<=', $end);
|
|
})->when($season, function ($query) use ($season) {
|
|
return $query->whereIn('season', $season);
|
|
})
|
|
->orderBy('starttime', 'ASC')
|
|
->get();
|
|
|
|
// dd($res);
|
|
|
|
$arr = [];
|
|
foreach ($res as $value) {
|
|
$tmp = [];
|
|
$tmp['match_id'] = $value->match_id;
|
|
$tmp['sport_id'] = $value->sport_id;
|
|
$tmp['league_id'] = $value->league_id;
|
|
$tmp['home_id'] = $value->home_id;
|
|
$tmp['away_id'] = $value->away_id;
|
|
$tmp['starttime'] = $value->starttime;
|
|
$tmp['season'] = $value->season;
|
|
$tmp['exp_home'] = $value->exp_home;
|
|
$tmp['exp_away'] = $value->exp_away;
|
|
$tmp['exp_handicap'] = $value->exp_handicap;
|
|
$tmp['exp_total'] = $value->exp_total;
|
|
$tmp['result_code'] = $value->result_code;
|
|
$tmp['home'] = $value->home->ss_team_name;
|
|
$tmp['away'] = $value->away->ss_team_name;
|
|
|
|
switch ($statistic_type) {
|
|
case 'gls':
|
|
$tmp['val_home'] = $value->gls_h;
|
|
$tmp['val_away'] = $value->gls_a;
|
|
break;
|
|
case 'crn':
|
|
$tmp['val_home'] = $value->crn_h;
|
|
$tmp['val_away'] = $value->crn_a;
|
|
break;
|
|
case 'fls':
|
|
$tmp['val_home'] = $value->fls_h;
|
|
$tmp['val_away'] = $value->fls_a;
|
|
break;
|
|
case 'ofs':
|
|
$tmp['val_home'] = $value->ofs_h;
|
|
$tmp['val_away'] = $value->ofs_a;
|
|
break;
|
|
case 'crd':
|
|
$tmp['val_home'] = $value->crd_h;
|
|
$tmp['val_away'] = $value->crd_a;
|
|
break;
|
|
case 'snt':
|
|
$tmp['val_home'] = $value->snt_h;
|
|
$tmp['val_away'] = $value->snt_a;
|
|
break;
|
|
case 'tsh':
|
|
$tmp['val_home'] = $value->tsh_h;
|
|
$tmp['val_away'] = $value->tsh_a;
|
|
break;
|
|
}
|
|
|
|
$arr['all'][] = $tmp;
|
|
if ($value->home_id == $team_id)
|
|
$arr['home'][] = $tmp;
|
|
if ($value->away_id == $team_id)
|
|
$arr['away'][] = $tmp;
|
|
}
|
|
|
|
return $arr;
|
|
}
|
|
}
|