MatchListWeb.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Jun.peng
  5. * Date: 2019/5/14
  6. * Time: 16:25
  7. */
  8. namespace App\Sports\Controller;
  9. use BaseController\Controller;
  10. use Biz\Match\GetmatchData;
  11. use Biz\Match\GetOddsData;
  12. /**
  13. * Class MatchListWeb
  14. * @package App\Sports\Controller
  15. * pc端 接口
  16. */
  17. class MatchListWeb extends Controller{
  18. private $getTypeData;
  19. public function init() {
  20. $this->getTypeData = new GetmatchData();
  21. $this->getOddsData = new GetOddsData();
  22. $this->commonFunction = C()->get('commonFunction');
  23. }
  24. //各球类玩法列表及数量
  25. public function gameList(){
  26. $type = $_REQUEST['type'];//状态代码
  27. $game_code = $_REQUEST['game_code'];//球类代码
  28. $p_code = $_REQUEST['p_code'];//父级玩法代码
  29. $time = $_REQUEST['match_date'];//时间条件
  30. try {
  31. if(empty($type) ){
  32. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  33. }
  34. switch ($type){
  35. case 'StRollBall'://滚球
  36. $this->getRollBall($type,$game_code,$p_code);
  37. break;
  38. case 'StSoon'://即将
  39. $this->Other($type,$game_code,$p_code,$time);
  40. break;
  41. case 'StToday'://今日
  42. $this->Other($type,$game_code,$p_code,$time);
  43. break;
  44. case 'StMorningPlate'://早盘
  45. $this->Other($type,$game_code,$p_code,$time);
  46. break;
  47. case 'StStringScene'://串场
  48. $this->Other($type,$game_code,$p_code,$time);
  49. break;
  50. default:
  51. throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
  52. }
  53. Render([], '1', lang('Tips','Sports')->get('success'));
  54. } catch (\Exception $e) {
  55. echo $e->getMessage();
  56. }
  57. }
  58. //获取滚球
  59. public function getRollBall($type,$game_code,$p_code){
  60. //根据球类代码及玩法代码 获取相关赛事赔率数据
  61. if(!empty($game_code) and !empty($p_code)){
  62. $matchData = $this->getMatchData($type,$game_code,$p_code);
  63. Render($matchData, '1', lang('Tips','Sports')->get('success'));
  64. }
  65. $game = lm('GameType', 'Sports')->select('game_code')->where('status',1)->get()->toArray();
  66. $gameData = [];
  67. foreach ($game as $k=>$v){
  68. if($v['game_code'] !='gj'){
  69. $models = $this->commonFunction->getModels($v['game_code'],1);
  70. $where = $this->commonFunction->getState($type,$models['model_match']);
  71. $matchNum = lm($models['model_league'],"Sports")
  72. ->join($models['model_match'],$models['model_match'].'.lg_id',$models['model_league'].'.lg_id')
  73. ->where($models['model_match'].'.source',$this->source['source'])
  74. ->where($where)
  75. ->count("*");
  76. //获取满足状态的球类
  77. if($matchNum != 0){
  78. $gameData[$k]['game_code'] = $v['game_code'];
  79. $gameData[$k]['game_num'] = $matchNum;
  80. }
  81. }
  82. }
  83. sort($gameData);
  84. $num = [];
  85. foreach ($gameData as $kk=>$vv){
  86. $num[] = $vv['game_num'];
  87. }
  88. $gameData[]['all_num'] = array_sum($num);
  89. Render($gameData, '1', lang('Tips','Sports')->get('success'));
  90. }
  91. //获取非滚球数据
  92. public function Other($type,$game_code,$p_code,$time=''){
  93. //根据球类代码及玩法代码 获取相关赛事赔率数据
  94. if(!empty($game_code) and !empty($p_code)){
  95. $matchData = $this->getMatchData($type,$game_code,$p_code,$time);
  96. Render($matchData, '1', lang('Tips','Sports')->get('success'));
  97. }
  98. //根据状态获取所有球类/玩法下赛事数量
  99. $game = lm('GameType', 'Sports')->select('game_code')->where('status',1)->get()->toArray();
  100. $oddsCodeNum = [];
  101. foreach ($game as $k=>$v) {
  102. if ($v['game_code'] != 'gj') {
  103. //获取各球类下父级玩法赛事数量
  104. $oddsCodeNum[$k] = $this->getMatchData($type,$v['game_code'],'');
  105. }
  106. }
  107. $matchDataNum = [];
  108. foreach ($oddsCodeNum as $k => $v){
  109. foreach ($v as $kk=>$vv){
  110. $matchDataNum[] = $vv;
  111. }
  112. }
  113. Render($matchDataNum, '1', lang('Tips','Sports')->get('success'));
  114. }
  115. //获取赛事数据
  116. public function getMatchData($type,$game_code,$p_code='',$time=''){
  117. //$p_code 获取具体玩法下的赛事数据
  118. $models = $this->commonFunction->getModels($game_code,1);
  119. $where = $this->commonFunction->getState($type,$models['model_match']);
  120. $select = [$models['model_match'].'.match_id'];
  121. $timeWhere = [];
  122. if($p_code){
  123. //时间条件
  124. if(!empty($time)){
  125. if($time == 'other'){
  126. $timeWhere[] = ['match_date','>',date("Y-m-d",strtotime("+1weeks",strtotime(date('Y-m-d',time()))))];
  127. }else{
  128. $timeWhere[] = ['match_date',$time];
  129. }
  130. }
  131. if($p_code == 'kemp'){
  132. $select = [$models['model_league'].'.lg_id',$models['model_league'].'.name_chinese as leagueName','last_time','country_id','area_id'];
  133. }else{
  134. $select = [$models['model_league'].'.lg_id',$models['model_league'].'.name_chinese as leagueName',$models['model_match'].'.match_id','home_team','guest_team','match_date','match_time','tag','country_id','area_id'];
  135. }
  136. }
  137. //国家数据
  138. $country = lm('st_country',"Sports")
  139. ->select('country_id','name_chinese')
  140. ->get()->toArray();
  141. //赛事数据
  142. $matchNum = lm($models['model_league'],"Sports")
  143. ->join($models['model_match'],$models['model_match'].'.lg_id',$models['model_league'].'.lg_id')
  144. ->select($select)
  145. ->where($models['model_match'].'.source',$this->source['source'])
  146. ->where($where)
  147. ->where($timeWhere)
  148. ->get()->toArray();
  149. //赔率数据
  150. $oddsCodeNum = $this->getOddsData($game_code,$models['model_odds'],$matchNum,$p_code);
  151. if($p_code){
  152. $match_odds = [];
  153. if($p_code == 'kemp'){
  154. foreach ($oddsCodeNum as $k=>$v){
  155. foreach ($matchNum as $kk => $vv){
  156. if($v['lg_id'] == $vv['lg_id']){
  157. $match_odds[$kk]['lg_id'] = $vv['lg_id'];
  158. $match_odds[$kk]['leagueName'] = $vv['leagueName'];
  159. $match_odds[$kk]['last_time'] = $vv['last_time'];
  160. $match_odds[$kk]['oddsData'][] = $v;
  161. }
  162. }
  163. }
  164. $gjData = [];
  165. foreach ($match_odds as $k=>$v){
  166. //按p_code分组
  167. $p_code=array();
  168. foreach($v['oddsData'] as $kk=>$vv){
  169. $p_code[$vv['p_code']][]=$vv;
  170. }
  171. $sd = [];
  172. foreach ($p_code as $k1=>$v1){
  173. foreach ($v1 as $k2=>$v2){
  174. $sd[$v2['p_code']]['last_time'] =$v['last_time'];
  175. $sd[$v2['p_code']][$v2['odds_code']][] = $v2;
  176. }
  177. }
  178. $gjData[] = $sd;
  179. }
  180. return $gjData;
  181. }
  182. foreach ($oddsCodeNum as $k=>$v){
  183. foreach ($matchNum as $kk=>$vv){
  184. if($v['match_id'] == $vv['match_id']){
  185. $match_odds[$kk]['lg_id'] = $vv['lg_id'];
  186. $match_odds[$kk]['leagueName'] = $vv['leagueName'];
  187. $match_odds[$kk]['match_id'] = $vv['match_id'];
  188. $match_odds[$kk]['home_team'] = $vv['home_team'];
  189. $match_odds[$kk]['guest_team'] = $vv['guest_team'];
  190. $match_odds[$kk]['match_date'] = $vv['match_date'];
  191. $match_odds[$kk]['match_time'] = $vv['match_time'];
  192. $match_odds[$kk]['tag'] = $vv['tag'];
  193. $match_odds[$kk]['oddsData'][] = $v;
  194. }
  195. }
  196. }
  197. //排序
  198. sort($match_odds);
  199. $data = [];
  200. foreach ($matchNum as $k=>$v){
  201. foreach ($match_odds as $kk=>$vv){
  202. if($v['lg_id'] == $vv['lg_id']){
  203. $data[$k]['area_id'] = $v['area_id'];
  204. $data[$k]['country_id'] = $v['country_id'];
  205. $data[$k]['lg_id'] = $v['lg_id'];
  206. $data[$k]['leagueName'] = $v['leagueName'];
  207. $data[$k]['matchData'][] = $vv;
  208. }
  209. }
  210. }
  211. //赛事详细数据 去重
  212. $matchData = $this->uniquArr($data,'lg_id');
  213. //获取国家联赛列表
  214. $leagueList = [];
  215. foreach ($country as $k=>$v){
  216. foreach ($matchData as $kk=>$vv){
  217. if($v['country_id'] == $vv['country_id']){
  218. //联赛下赛事数量
  219. $vv['matchNum'] = count($vv['matchData']);
  220. unset($vv['matchData']);
  221. $v['lg_list'][] = $vv;
  222. $leagueList[$k] = $v;
  223. }
  224. }
  225. }
  226. sort($leagueList);
  227. //国家-联赛列表
  228. $data = [$matchData,$leagueList];
  229. return $data;
  230. }
  231. return $oddsCodeNum;
  232. }
  233. //根据match_id获取赔率数据
  234. public function getOddsData($game_code,$model_odds,$match_ids,$p_code=''){
  235. $match_id = [];
  236. $lg_ids = [];
  237. foreach ($match_ids as $k => $v){
  238. $match_id[] = $v['match_id'];
  239. $lg_ids[] = $v['lg_id'];
  240. }
  241. $select = ['match_id','p_code'];
  242. $whereOr = [];
  243. if($p_code){
  244. $select = ['lg_id','match_id','id','p_code','odds_code','condition','odds','odds_only','sort','status','team'];
  245. //获取冠军盘口
  246. if($p_code == 'kemp'){
  247. $whereOr = [['sort','=',0],['type','=',1]];
  248. $oddsData = lm($model_odds,"Sports")
  249. ->select($select)
  250. ->whereIn('lg_id', $lg_ids)
  251. ->where('source',$this->source['source'])
  252. ->where($whereOr)
  253. ->get()
  254. ->toArray();
  255. return $oddsData;
  256. }
  257. if($p_code == 'concede' and $game_code == 'zq'){
  258. $whereOr = [['sort','=',0]];
  259. $oddsData = lm($model_odds,"Sports")
  260. ->select($select)
  261. ->whereIn('match_id', $match_id)
  262. ->where('source',$this->source['source'])
  263. ->where($whereOr)
  264. ->whereIn('p_code',['concede','goal_size'])
  265. ->get()
  266. ->toArray();
  267. return $oddsData;
  268. }
  269. if($p_code == 'concede' and $game_code == 'lq'){
  270. $whereOr = [['sort','=',0]];
  271. $oddsData = lm($model_odds,"Sports")
  272. ->select($select)
  273. ->whereIn('match_id', $match_id)
  274. ->where('source',$this->source['source'])
  275. ->where($whereOr)
  276. ->whereIn('p_code',['concede','total_size','team_score'])
  277. ->get()
  278. ->toArray();
  279. return $oddsData;
  280. }
  281. if($p_code == 'concede' and $game_code == 'wq'){
  282. $whereOr = [['sort','=',0]];
  283. $oddsData = lm($model_odds,"Sports")
  284. ->select($select)
  285. ->whereIn('match_id', $match_id)
  286. ->where('source',$this->source['source'])
  287. ->where($whereOr)
  288. ->whereIn('p_code',['dishes','bureau','total_number'])
  289. ->get()
  290. ->toArray();
  291. return $oddsData;
  292. }
  293. if($p_code == 'concede' and $game_code == 'bq'){
  294. $whereOr = [['sort','=',0]];
  295. $oddsData = lm($model_odds,"Sports")
  296. ->select($select)
  297. ->whereIn('match_id', $match_id)
  298. ->where('source',$this->source['source'])
  299. ->where($whereOr)
  300. ->whereIn('p_code',['concede','total_size','two_sides'])
  301. ->get()
  302. ->toArray();
  303. return $oddsData;
  304. }
  305. else{
  306. $whereOr = [['p_code','=',$p_code],['sort','=',0]];
  307. $oddsData = lm($model_odds,"Sports")
  308. ->select($select)
  309. ->whereIn('match_id', $match_id)
  310. ->where('source',$this->source['source'])
  311. ->where($whereOr)
  312. ->get()
  313. ->toArray();
  314. return $oddsData;
  315. }
  316. }
  317. $oddsData = lm($model_odds,"Sports")
  318. ->select($select)
  319. ->whereIn('match_id', $match_id)
  320. ->where('source',$this->source['source'])
  321. ->where($whereOr)
  322. ->get()
  323. ->toArray();
  324. //按父级玩法分组
  325. $oddsCode = [];
  326. foreach ($oddsData as $kk=>$vv){
  327. $oddsCode[$vv['p_code']][]= $vv;
  328. }
  329. // 去重 获取每个玩法下的赛事数量
  330. $oddsCodeNum = [];
  331. $game_num = count($match_ids);
  332. foreach ($oddsCode as $kkk=>$vvv){
  333. $oddsCodeNum[$kkk]['game_code'] = $game_code;
  334. $oddsCodeNum[$kkk]['game_num'] = $game_num;
  335. $oddsCodeNum[$kkk]['p_code'] = $kkk;
  336. $oddsCodeNum[$kkk]['code_num'] = count($this->uniquArr($vvv,'match_id'));
  337. }
  338. sort($oddsCodeNum);
  339. return $oddsCodeNum;
  340. }
  341. /**
  342. * 二维数组根据某个字段去重
  343. * @param array $array 二维数组
  344. * @para array 去重后的数组
  345. */
  346. function uniquArr($array,$key){
  347. $result = array();
  348. foreach($array as $k=>$val){
  349. $code = false;
  350. foreach($result as $_val){
  351. if($_val[$key] == $val[$key]){
  352. $code = true;
  353. break;
  354. }
  355. }
  356. if(!$code){
  357. $result[]=$val;
  358. }
  359. }
  360. return $result;
  361. }
  362. //获取直播数据
  363. public function getBroadcast(){
  364. $data = lm('st_broadcast',"Sports")
  365. ->select('id','showid','shower','league_name','game_type','host_team','guest_team','start_time','doing')
  366. ->get()
  367. ->toArray();
  368. Render($data, '1', lang('Tips','Sports')->get('success'));
  369. }
  370. }