<?php
namespace App\Models;

use DB;
use PHPUnit\Runner\Filter\NameFilterIterator;

/**
 * 获取级联关系 Model
 */
class Cascade extends BaseModel
{
    /**
     * 获取洲
     *
     * @access public
     * @param mixed $select 查询参数
     * @param mixed $orderBy 排序字段
     * @return array JsonString
     */
    public function allArea($select, $orderBy = 'id') {
        $result = DB :: table('st_area')
                -> select($select)
                -> orderBy($orderBy)
                -> get();

        return $result;
    }

    /**
     * 获取国家
     *
     * @access public
     * @param mixed $select 查询参数
     * @param mixed $where 查询条件
     * @param mixed $orderBy 排序字段
     * @return array JsonString
     */
    public function getCountry($select, $where, $orderBy = 'country_order') {
        $result = [];
        if (empty($where)) {
            $result = DB :: table('st_country')
                -> select($select)
                -> orderBy($orderBy)
                -> get();
        } else {
            $result = DB :: table('st_country')
                -> select($select)
                -> where($where)
                -> orderBy($orderBy)
                -> get();
        }

        return $result;
    }

    /**
     * 获取联赛
     *
     * @access public
     * @param mixed $select 查询参数
     * @param mixed $where 查询条件
     * @param mixed $gameType 球类型
     * @param mixed $orderBy 排序字段
     * @return array JsonString
     */
    public function getLeague($select, $where, $gameType, $orderBy = 'league_list') {
        $leagueTable = 'st_' . $gameType . '_league';
        $result = [];
        if (empty($where)) {
            $result = DB :: table($leagueTable)
                -> select($select)
                -> orderBy($orderBy)
                -> get();
        } else {
            $result = DB :: table($leagueTable)
                -> select($select)
                -> where($where)
                -> orderBy($orderBy)
                -> get();
        }

        return $result;
    }

    /**
     * 获取联赛球队
     *
     * @access public
     * @param mixed $select 查询参数
     * @param mixed $where 查询条件
     * @param mixed $gameType 球类型
     * @param mixed $orderBy 排序字段
     * @return array JsonString
     */
    public function getCompetition($select, $where, $gameType, $orderBy = 'id') {
        $competitionTable = 'st_' . $gameType . '_competition';
        $result = [];
        if (empty($where)) {
            $result = DB :: table($competitionTable)
                -> select($select)
                -> orderBy($orderBy)
                -> get();
        } else {
            $result = DB :: table($competitionTable)
                -> select($select)
                -> where($where)
                -> orderBy($orderBy)
                -> get();
        }

        return $result;
    }

    /**
     * 获取国家球队
     *
     * @access public
     * @param mixed $select 查询参数
     * @param mixed $where 查询条件
     * @param mixed $orderBy 排序字段
     * @return array JsonString
     */
    public function getCountryCompetition($select, $where, $orderBy = 'id') {
        $result = [];
        if (empty($where)) {
            $result = DB :: table('st_team')
                -> select($select)
                -> orderBy($orderBy)
                -> get();
        } else {
            $result = DB :: table('st_team')
                -> select($select)
                -> where($where)
                -> orderBy($orderBy)
                -> get();
        }

        return $result;
    }

    /**
     * 获取国家级联关系
     *
     * @access public
     * @param mixed $select 查询参数
     * @param mixed $where 查询条件
     * @param mixed $gameType 球类型
     * @param mixed $gameTypeId 运动类型Id
     * @return array JsonString
     */
    public function getCountryByArea($select, $where, $gameType, $gameTypeId) {
        // 获取国家的数据
        $result['country'] = $this -> getCountry($select, $where);
        // 循环获取所有国家ID集
        $countryId = [];
        foreach ($result['country'] as $key => $value) {
            $countryId[] = $value->country_id;
        }
        // 获取所有联赛数据
        $getLeagueSelect = ['name_chinese', 'name_english', 'kind', 'league_pic', 'country_id', 'area_id', 'id', 'lg_id'];
        $getAllLeague = $this -> getLeague($getLeagueSelect, '', $gameType);
        // 获取所有联赛球队
        $getCompetitionSelect = ['id', 'home_team', 'guest_team', 'lg_id'];
        $getAllCompetition = $this -> getCompetition($getCompetitionSelect, '', $gameType);
        // 获取所有国家球队
        $getCtyCtnSelect = ['id', 'team_id', 'country_id', 'game_type_id', 'team_name_cn', 'team_name_en'];
        $getCtyCtnWhere['game_type_id'] = $gameTypeId;
        $getCtyCtn = $this -> getCountryCompetition($getCtyCtnSelect, $getCtyCtnWhere);

        // 循环获取联赛
        $getLeague = [];
        foreach ($getAllLeague as $key => $value) {
            // 从洲直接获取联赛
            if ($value->area_id == $where['country_area']) {
                $getLeague[] = $value;


            // 从国家获取联赛
            } elseif (in_array($value->country_id, $countryId)) {
                $getLeague[] = $value;
            }
        }
        // 数组去重
        $result['league'] = array_unique($getLeague, SORT_REGULAR);
        // 循环获取所有联赛ID集
        $leagueId = [];
        foreach ($result['league'] as $key => $value) {
            $leagueId[] = $value->lg_id;
        }

        // 循环获取联赛球队
        $getCompetition = [];
        foreach ($getAllCompetition as $key => $value) {
            if (in_array($value->lg_id, $leagueId)) {
                $getCompetition[] = $value;
            }
        }
        // 循环获取国家球队
        foreach ($getCtyCtn as $key => $value) {
            if (in_array($value->country_id, $countryId)) {
                $getCompetition[] = $value;
            }
        }
        // 数组去重
        $result['competition'] = array_unique($getCompetition, SORT_REGULAR);

        return $result;
    }

    /**
     * 获取联赛级联关系
     *
     * @access public
     * @param mixed $select 查询参数
     * @param mixed $where 查询条件
     * @param mixed $gameType 球类型
     * @return array JsonString
     */
    public function league($select, $where, $gameType) {
        // 获取联赛数据
        $result['league'] = $this -> getLeague($select, $where, $gameType);
        // 循环获取所有联赛ID集
        $leagueId = [];
        foreach ($result['league'] as $key => $value) {
            $leagueId[] = $value->lg_id;
        }
        // 获取所有联赛球队
        $getCompetitionSelect = ['id', 'home_team', 'guest_team', 'lg_id'];
        $getAllCompetition = $this -> getCompetition($getCompetitionSelect, '', $gameType);

        // 循环获取联赛球队
        $getCompetition = [];
        foreach ($getAllCompetition as $key => $value) {
            if (in_array($value->lg_id, $leagueId)) {
                $getCompetition[] = $value;
            }
        }
        // 数组去重
        $result['competition'] = array_unique($getCompetition, SORT_REGULAR);

        return $result;
    }
}
