Cascade.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Models;
  3. use DB;
  4. use PHPUnit\Runner\Filter\NameFilterIterator;
  5. /**
  6. * 获取级联关系 Model
  7. */
  8. class Cascade extends BaseModel
  9. {
  10. /**
  11. * 获取洲
  12. *
  13. * @access public
  14. * @param mixed $select 查询参数
  15. * @param mixed $orderBy 排序字段
  16. * @return array JsonString
  17. */
  18. public function allArea($select, $orderBy = 'id') {
  19. $result = 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 $orderBy 排序字段
  32. * @return array JsonString
  33. */
  34. public function getCountry($select, $where, $orderBy = 'country_order') {
  35. $result = [];
  36. if (empty($where)) {
  37. $result = DB :: table('st_country')
  38. -> select($select)
  39. -> orderBy($orderBy)
  40. -> get();
  41. } else {
  42. $result = DB :: table('st_country')
  43. -> select($select)
  44. -> where($where)
  45. -> orderBy($orderBy)
  46. -> get();
  47. }
  48. return $result;
  49. }
  50. /**
  51. * 获取联赛
  52. *
  53. * @access public
  54. * @param mixed $select 查询参数
  55. * @param mixed $where 查询条件
  56. * @param mixed $gameType 球类型
  57. * @param mixed $orderBy 排序字段
  58. * @return array JsonString
  59. */
  60. public function getLeague($select, $where, $gameType, $orderBy = 'league_list') {
  61. $leagueTable = 'st_' . $gameType . '_league';
  62. $result = [];
  63. if (empty($where)) {
  64. $result = DB :: table($leagueTable)
  65. -> select($select)
  66. -> orderBy($orderBy)
  67. -> get();
  68. } else {
  69. $result = DB :: table($leagueTable)
  70. -> select($select)
  71. -> where($where)
  72. -> orderBy($orderBy)
  73. -> get();
  74. }
  75. return $result;
  76. }
  77. /**
  78. * 获取联赛球队
  79. *
  80. * @access public
  81. * @param mixed $select 查询参数
  82. * @param mixed $where 查询条件
  83. * @param mixed $gameType 球类型
  84. * @param mixed $orderBy 排序字段
  85. * @return array JsonString
  86. */
  87. public function getCompetition($select, $where, $gameType, $orderBy = 'id') {
  88. $competitionTable = 'st_' . $gameType . '_competition';
  89. $result = [];
  90. if (empty($where)) {
  91. $result = DB :: table($competitionTable)
  92. -> select($select)
  93. -> orderBy($orderBy)
  94. -> get();
  95. } else {
  96. $result = DB :: table($competitionTable)
  97. -> select($select)
  98. -> where($where)
  99. -> orderBy($orderBy)
  100. -> get();
  101. }
  102. return $result;
  103. }
  104. /**
  105. * 获取国家球队
  106. *
  107. * @access public
  108. * @param mixed $select 查询参数
  109. * @param mixed $where 查询条件
  110. * @param mixed $orderBy 排序字段
  111. * @return array JsonString
  112. */
  113. public function getCountryCompetition($select, $where, $orderBy = 'id') {
  114. $result = [];
  115. if (empty($where)) {
  116. $result = DB :: table('st_team')
  117. -> select($select)
  118. -> orderBy($orderBy)
  119. -> get();
  120. } else {
  121. $result = DB :: table('st_team')
  122. -> select($select)
  123. -> where($where)
  124. -> orderBy($orderBy)
  125. -> get();
  126. }
  127. return $result;
  128. }
  129. /**
  130. * 获取国家级联关系
  131. *
  132. * @access public
  133. * @param mixed $select 查询参数
  134. * @param mixed $where 查询条件
  135. * @param mixed $gameType 球类型
  136. * @param mixed $gameTypeId 运动类型Id
  137. * @return array JsonString
  138. */
  139. public function getCountryByArea($select, $where, $gameType, $gameTypeId) {
  140. // 获取国家的数据
  141. $result['country'] = $this -> getCountry($select, $where);
  142. // 循环获取所有国家ID集
  143. $countryId = [];
  144. foreach ($result['country'] as $key => $value) {
  145. $countryId[] = $value->country_id;
  146. }
  147. // 获取所有联赛数据
  148. $getLeagueSelect = ['name_chinese', 'name_english', 'kind', 'league_pic', 'country_id', 'area_id', 'id', 'lg_id'];
  149. $getAllLeague = $this -> getLeague($getLeagueSelect, '', $gameType);
  150. // 获取所有联赛球队
  151. $getCompetitionSelect = ['id', 'home_team', 'guest_team', 'lg_id'];
  152. $getAllCompetition = $this -> getCompetition($getCompetitionSelect, '', $gameType);
  153. // 获取所有国家球队
  154. $getCtyCtnSelect = ['id', 'team_id', 'country_id', 'game_type_id', 'team_name_cn', 'team_name_en'];
  155. $getCtyCtnWhere['game_type_id'] = $gameTypeId;
  156. $getCtyCtn = $this -> getCountryCompetition($getCtyCtnSelect, $getCtyCtnWhere);
  157. // 循环获取联赛
  158. $getLeague = [];
  159. foreach ($getAllLeague as $key => $value) {
  160. // 从洲直接获取联赛
  161. if ($value->area_id == $where['country_area']) {
  162. $getLeague[] = $value;
  163. // 从国家获取联赛
  164. } elseif (in_array($value->country_id, $countryId)) {
  165. $getLeague[] = $value;
  166. }
  167. }
  168. // 数组去重
  169. $result['league'] = array_unique($getLeague, SORT_REGULAR);
  170. // 循环获取所有联赛ID集
  171. $leagueId = [];
  172. foreach ($result['league'] as $key => $value) {
  173. $leagueId[] = $value->lg_id;
  174. }
  175. // 循环获取联赛球队
  176. $getCompetition = [];
  177. foreach ($getAllCompetition as $key => $value) {
  178. if (in_array($value->lg_id, $leagueId)) {
  179. $getCompetition[] = $value;
  180. }
  181. }
  182. // 循环获取国家球队
  183. foreach ($getCtyCtn as $key => $value) {
  184. if (in_array($value->country_id, $countryId)) {
  185. $getCompetition[] = $value;
  186. }
  187. }
  188. // 数组去重
  189. $result['competition'] = array_unique($getCompetition, SORT_REGULAR);
  190. return $result;
  191. }
  192. /**
  193. * 获取联赛级联关系
  194. *
  195. * @access public
  196. * @param mixed $select 查询参数
  197. * @param mixed $where 查询条件
  198. * @param mixed $gameType 球类型
  199. * @return array JsonString
  200. */
  201. public function league($select, $where, $gameType) {
  202. // 获取联赛数据
  203. $result['league'] = $this -> getLeague($select, $where, $gameType);
  204. // 循环获取所有联赛ID集
  205. $leagueId = [];
  206. foreach ($result['league'] as $key => $value) {
  207. $leagueId[] = $value->lg_id;
  208. }
  209. // 获取所有联赛球队
  210. $getCompetitionSelect = ['id', 'home_team', 'guest_team', 'lg_id'];
  211. $getAllCompetition = $this -> getCompetition($getCompetitionSelect, '', $gameType);
  212. // 循环获取联赛球队
  213. $getCompetition = [];
  214. foreach ($getAllCompetition as $key => $value) {
  215. if (in_array($value->lg_id, $leagueId)) {
  216. $getCompetition[] = $value;
  217. }
  218. }
  219. // 数组去重
  220. $result['competition'] = array_unique($getCompetition, SORT_REGULAR);
  221. return $result;
  222. }
  223. }