MatchListWeb.php 18 KB

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