Cascade.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Models;
  3. use DB;
  4. /**
  5. * 获取级联关系 Model
  6. */
  7. class Cascade extends BaseModel
  8. {
  9. /**
  10. * 获取洲
  11. *
  12. * @access public
  13. * @param mixed $select 查询参数
  14. * @param mixed $orderBy 排序字段
  15. * @return array JsonString
  16. */
  17. public function getArea($select, $orderBy = 'id') {
  18. $result = [];
  19. $result['area'] = DB :: table('st_area')
  20. -> select($select)
  21. -> orderBy($orderBy)
  22. -> get();
  23. return $result;
  24. }
  25. /**
  26. * 循环获取国家级联关系
  27. *
  28. * @access public
  29. * @param mixed $select 查询参数
  30. * @param mixed $where 查询条件
  31. * @param mixed $gameType 球类型
  32. * @param mixed $orderBy 排序字段
  33. * @return array JsonString
  34. */
  35. public function getCountry($select, $where, $gameType, $gameTypeId, $orderBy = 'country_order') {
  36. // 获取国家的数据
  37. $result['country'] = DB :: table('st_country')
  38. -> select($select)
  39. -> where($where)
  40. -> orderBy($orderBy)
  41. -> get();
  42. // 从洲直接获取联赛
  43. $getLeagueSelect = ['name_chinese', 'name_english', 'kind', 'league_pic', 'country_id', 'area_id', 'id', 'lg_id'];
  44. $leagueByAreaWhere['area_id'] = $where['country_area'];
  45. $leagueByArea = $this -> getLeague($getLeagueSelect, $leagueByAreaWhere, $gameType);
  46. $getCompetitionSelect = ['id', 'home_team', 'guest_team', 'lg_id'];
  47. // 循环从国家取联赛、球队
  48. $leagueByCountry = [];
  49. $competitionByCountry = [];
  50. foreach ($result['country'] as $key => $value) {
  51. // 获取联赛
  52. $leagueByCountryWhere['country_id'] = $value -> country_id;
  53. $getLeagueByCountry = $this->getLeague($getLeagueSelect, $leagueByCountryWhere, $gameType);
  54. $leagueByCountry = array_merge($leagueByCountry, $getLeagueByCountry);
  55. // 获取球队
  56. /*$leagueByCountryWhere['game_type_id'] = $gameTypeId;
  57. $getCompetitionByCountry = $this->getCompetitionByCountry($getLeagueSelect, $leagueByCountryWhere, $gameType);
  58. $competitionByCountry = array_merge_recursive($competitionByCountry, $getCompetitionByCountry);*/
  59. }
  60. // 不同获取路径合并 并去重
  61. $league = array_merge($leagueByCountry, $leagueByArea);
  62. $result['league'] = array_unique($league, SORT_REGULAR)['league'];
  63. // 从联赛获取球队
  64. $competitionByLeague = [];
  65. foreach ($result['league'] as $key => $value) {
  66. $competitionByLeagueWhere['lg_id'] = $value -> lg_id;
  67. $getCompetitionByLeague = $this -> getCompetition($getCompetitionSelect, $competitionByLeagueWhere, $gameType);
  68. $competitionByLeague = array_merge_recursive($competitionByLeague, $getCompetitionByLeague);
  69. }
  70. foreach ($competitionByLeague['competition'] as $key => $value) {
  71. $result['competition'] = array_unique($value, SORT_REGULAR);
  72. }
  73. return $result;
  74. }
  75. /**
  76. * 循环获取联赛级联关系
  77. *
  78. * @access public
  79. * @param mixed $select 查询参数
  80. * @param mixed $where 查询条件
  81. * @param mixed $gameType 球类型
  82. * @return array JsonString
  83. */
  84. public function league($select, $where, $gameType) {
  85. // 获取联赛
  86. $getLeagueByCountry = $this->getLeague($select, $where, $gameType);
  87. $result['league'] = $getLeagueByCountry['league'];
  88. // 从联赛获取球队
  89. $getCompetitionSelect = ['id', 'home_team', 'guest_team', 'lg_id'];
  90. $competitionByLeague = [];
  91. foreach ($result['league'] as $key => $value) {
  92. $competitionByLeagueWhere['lg_id'] = $value -> lg_id;
  93. $getCompetitionByLeague = $this -> getCompetition($getCompetitionSelect, $competitionByLeagueWhere, $gameType);
  94. $competitionByLeague = array_merge_recursive($competitionByLeague, $getCompetitionByLeague);
  95. }
  96. foreach ($competitionByLeague['competition'] as $key => $value) {
  97. $result['competition'] = array_unique($value, SORT_REGULAR);
  98. }
  99. return $result;
  100. }
  101. /**
  102. * 获取联赛
  103. *
  104. * @access public
  105. * @param mixed $select 查询参数
  106. * @param mixed $where 查询条件
  107. * @param mixed $gameType 球类型
  108. * @param mixed $orderBy 排序字段
  109. * @return array JsonString
  110. */
  111. public function getLeague($select, $where, $gameType, $orderBy = 'league_list') {
  112. $leagueTable = 'st_' . $gameType . '_league';
  113. $result['league'] = DB :: table($leagueTable)
  114. -> select($select)
  115. -> where($where)
  116. -> orderBy($orderBy)
  117. -> get();
  118. return $result;
  119. }
  120. /**
  121. * 获取球队
  122. *
  123. * @access public
  124. * @param mixed $select 查询参数
  125. * @param mixed $where 查询条件
  126. * @param mixed $gameType 球类型
  127. * @param mixed $orderBy 排序字段
  128. * @return array JsonString
  129. */
  130. public function getCompetition($select, $where, $gameType, $orderBy = 'id') {
  131. $competitionTable = 'st_' . $gameType . '_competition';
  132. $result['competition'] = DB :: table($competitionTable)
  133. -> select($select)
  134. -> where($where)
  135. -> orderBy($orderBy)
  136. -> get();
  137. return $result;
  138. }
  139. /**
  140. * 获取球队
  141. *
  142. * @access public
  143. * @param mixed $select 查询参数
  144. * @param mixed $where 查询条件
  145. * @param mixed $gameType 球类型
  146. * @param mixed $orderBy 排序字段
  147. * @return array JsonString
  148. */
  149. public function getCompetitionByCountry($select, $where, $gameType, $orderBy = 'id') {
  150. $competitionTable = 'st_' . $gameType . '_competition';
  151. $result['competition'] = DB :: table('st_team')
  152. -> join($competitionTable, $competitionTable.'.team_id','=','st_team.id')
  153. -> select($select)
  154. -> where($where)
  155. -> orderBy($orderBy)
  156. -> get();
  157. return $result;
  158. }
  159. }