Files
sbnews/server/app/adminapi/lists/match/MatchOddsLists.php
T
2026-07-11 11:19:43 +08:00

220 lines
7.0 KiB
PHP

<?php
namespace app\adminapi\lists\match;
use app\adminapi\lists\BaseAdminDataLists;
use app\common\lists\ListsExtendInterface;
use app\common\model\match\MatchOdds;
class MatchOddsLists extends BaseAdminDataLists implements ListsExtendInterface
{
public function lists(): array
{
$lists = $this->buildQuery()
->field('id,source_site,source_url,source_match_id,source_parent_id,source_league_id,source_event_id,sport_type,show_type,odds_type,league_name,match_time_text,home_team,away_team,strong_side,more_count,is_running,handicap_line,handicap_home_odds,handicap_away_odds,total_line,total_over_odds,total_under_odds,home_win_odds,draw_odds,away_win_odds,half_handicap_line,half_home_win_odds,half_draw_odds,half_away_win_odds,create_time,update_time')
->limit($this->limitOffset, $this->limitLength)
->order('update_time', 'desc')
->order('id', 'desc')
->select()
->toArray();
foreach ($lists as &$item) {
$item['source_site_text'] = self::formatSourceSite((string) ($item['source_site'] ?? ''));
$item['show_type_text'] = self::formatShowType((string) ($item['show_type'] ?? ''));
$item['sport_type_text'] = self::formatSportType((string) ($item['sport_type'] ?? ''));
$item['create_time_text'] = self::formatTime((int) ($item['create_time'] ?? 0));
$item['update_time_text'] = self::formatTime((int) ($item['update_time'] ?? 0));
$item['has_odds'] = self::hasOdds($item) ? 1 : 0;
$item['market_count'] = self::marketCount($item);
}
unset($item);
return $lists;
}
public function count(): int
{
return $this->buildQuery()->count();
}
public function extend(): array
{
$sourceSites = MatchOdds::where('source_site', '<>', '')
->distinct(true)
->column('source_site');
$sourceOptions = [];
foreach ($sourceSites as $sourceSite) {
$sourceSite = (string) $sourceSite;
$sourceOptions[] = [
'label' => self::formatSourceSite($sourceSite),
'value' => $sourceSite,
];
}
return [
'source_options' => $sourceOptions,
];
}
protected function buildQuery()
{
$query = MatchOdds::query();
$sourceSite = $this->normalizeScalarParam('source_site');
if ($sourceSite !== '') {
$query->where('source_site', $sourceSite);
}
$showType = $this->normalizeScalarParam('show_type');
if ($showType !== '') {
$query->where('show_type', $showType);
}
$sportType = $this->normalizeScalarParam('sport_type');
if ($sportType !== '') {
$query->where('sport_type', $sportType);
}
$leagueName = $this->normalizeScalarParam('league_name');
if ($leagueName !== '') {
$query->where('league_name', 'like', '%' . addcslashes($leagueName, '\\%_') . '%');
}
$keyword = $this->normalizeScalarParam('keyword');
if ($keyword !== '') {
$likeKeyword = '%' . addcslashes($keyword, '\\%_') . '%';
$query->where(function ($query) use ($likeKeyword) {
$query->where('league_name', 'like', $likeKeyword)
->whereOr('home_team', 'like', $likeKeyword)
->whereOr('away_team', 'like', $likeKeyword)
->whereOr('source_match_id', 'like', $likeKeyword)
->whereOr('match_time_text', 'like', $likeKeyword);
});
}
$hasOdds = $this->params['has_odds'] ?? '1';
if ((string) $hasOdds === '1') {
$this->applyHasOddsWhere($query);
}
return $query;
}
protected function normalizeScalarParam(string $name): string
{
$value = $this->params[$name] ?? '';
if (!is_scalar($value)) {
return '';
}
return trim((string) $value);
}
protected function applyHasOddsWhere($query): void
{
$query->where(function ($query) {
foreach (self::oddsFields() as $index => $field) {
if ($index === 0) {
$query->where($field, '<>', '');
continue;
}
$query->whereOr($field, '<>', '');
}
});
}
protected static function hasOdds(array $item): bool
{
foreach (self::oddsFields() as $field) {
if (trim((string) ($item[$field] ?? '')) !== '') {
return true;
}
}
return false;
}
protected static function marketCount(array $item): int
{
$count = 0;
if (
trim((string) ($item['home_win_odds'] ?? '')) !== ''
|| trim((string) ($item['draw_odds'] ?? '')) !== ''
|| trim((string) ($item['away_win_odds'] ?? '')) !== ''
) {
$count++;
}
if (
trim((string) ($item['handicap_home_odds'] ?? '')) !== ''
|| trim((string) ($item['handicap_away_odds'] ?? '')) !== ''
) {
$count++;
}
if (
trim((string) ($item['total_over_odds'] ?? '')) !== ''
|| trim((string) ($item['total_under_odds'] ?? '')) !== ''
) {
$count++;
}
if (
trim((string) ($item['half_home_win_odds'] ?? '')) !== ''
|| trim((string) ($item['half_draw_odds'] ?? '')) !== ''
|| trim((string) ($item['half_away_win_odds'] ?? '')) !== ''
) {
$count++;
}
return $count;
}
protected static function oddsFields(): array
{
return [
'home_win_odds',
'draw_odds',
'away_win_odds',
'handicap_home_odds',
'handicap_away_odds',
'total_over_odds',
'total_under_odds',
'half_home_win_odds',
'half_draw_odds',
'half_away_win_odds',
];
}
public static function formatSourceSite(string $sourceSite): string
{
$map = [
'hg' => '滚球',
'titan007' => '球探',
];
return $map[$sourceSite] ?? ($sourceSite !== '' ? strtoupper($sourceSite) : '未知平台');
}
public static function formatShowType(string $showType): string
{
$map = [
'live' => '滚球盘',
'today' => '今日盘',
'early' => '早盘',
];
return $map[$showType] ?? ($showType !== '' ? $showType : '未知盘口');
}
public static function formatSportType(string $sportType): string
{
$map = [
'FT' => '足球',
'BK' => '篮球',
];
return $map[$sportType] ?? ($sportType !== '' ? $sportType : '未知');
}
protected static function formatTime(int $timestamp): string
{
if ($timestamp <= 0) {
return '';
}
return date('Y-m-d H:i:s', $timestamp);
}
}