MatchListWeb.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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)){
  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. //如果有传玩法代码,则获取查询字段
  125. if($p_code){
  126. //时间条件
  127. if(!empty($time)){
  128. if($time == 'other'){
  129. $timeWhere[] = ['match_date','>',date("Y-m-d",strtotime("+1weeks",strtotime(date('Y-m-d',time()))))];
  130. }else{
  131. $timeWhere[] = ['match_date',$time];
  132. }
  133. }
  134. if($p_code == 'kemp'){
  135. $select = [$models['model_league'].'.lg_id',$models['model_league'].'.name_chinese as leagueName','last_time','country_id','area_id'];
  136. }else{
  137. $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'];
  138. }
  139. }
  140. //如果是滚球
  141. if($type == 'StRollBall'){
  142. $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'];
  143. }
  144. //赛事数据
  145. $matchNum = lm($models['model_league'],"Sports")
  146. ->join($models['model_match'],$models['model_match'].'.lg_id',$models['model_league'].'.lg_id')
  147. ->select($select)
  148. ->where($models['model_match'].'.source',$this->source['source'])
  149. ->where($where)
  150. ->where($timeWhere)
  151. ->get()->toArray();
  152. //赔率数据
  153. $oddsCodeNum = $this->getOddsData($game_code,$models['model_odds'],$matchNum,$p_code,$type);
  154. if($p_code){
  155. $match_odds = [];
  156. if($p_code == 'kemp'){
  157. $matchNum = $this->uniquArr($matchNum,'lg_id');
  158. foreach ($oddsCodeNum as $k=>$v){
  159. foreach ($matchNum as $kk => $vv){
  160. if($v['lg_id'] == $vv['lg_id']){
  161. $match_odds[$kk]['area_id'] = $vv['area_id'];
  162. $match_odds[$kk]['country_id'] = $vv['country_id'];
  163. $match_odds[$kk]['lg_id'] = $vv['lg_id'];
  164. $match_odds[$kk]['leagueName'] = $vv['leagueName'];
  165. $match_odds[$kk]['last_time'] = $vv['last_time'];
  166. $match_odds[$kk]['oddsData'][] = $v;
  167. }
  168. }
  169. }
  170. sort($match_odds);
  171. $gjData = [];
  172. foreach ($match_odds as $k=>$v){
  173. //按p_code分组
  174. $p_code=array();
  175. foreach($v['oddsData'] as $kk=>$vv){
  176. $p_code[$vv['p_code']][]=$vv;
  177. }
  178. $sd = [];
  179. foreach ($p_code as $k1=>$v1){
  180. foreach ($v1 as $k2=>$v2){
  181. $sd[$v2['p_code']]['last_time'] =$v['last_time'];
  182. $sd[$v2['p_code']][$v2['odds_code']][] = $v2;
  183. }
  184. }
  185. $gjData[] = $sd;
  186. }
  187. //国家数据
  188. $country = countryModel::getName();
  189. //获取国家联赛列表
  190. $countryList = [];
  191. foreach ($country as $k=>$v){
  192. foreach ($match_odds as $kk=>$vv){
  193. if($v['country_id'] == $vv['country_id']){
  194. unset($vv['last_time']);
  195. //联赛下父级玩法 盘口数量
  196. $oddsCode = array_column($vv['oddsData'], 'odds_code');
  197. //获取冠军盘口下 父级玩法 数量
  198. $oddsCodeN = array_count_values($oddsCode);
  199. $vv['matchNum'] = count($oddsCodeN);
  200. unset($vv['oddsData']);
  201. $v['lg_list'][] = $vv;
  202. $countryList[$k] = $v;
  203. }
  204. }
  205. }
  206. sort($countryList);
  207. //地区数据
  208. $area = areaModel::getName();
  209. //获取地区联赛列表
  210. $areaList = [];
  211. foreach ($area as $k=>$v){
  212. foreach ($match_odds as $kk=>$vv){
  213. if($v['id'] == $vv['area_id']){
  214. unset($vv['last_time']);
  215. //联赛下父级玩法 盘口数量
  216. $oddsCode = array_column($vv['oddsData'], 'odds_code');
  217. //获取冠军盘口下 父级玩法 数量
  218. $oddsCodeN = array_count_values($oddsCode);
  219. $vv['matchNum'] = count($oddsCodeN);
  220. unset($vv['oddsData']);
  221. $v['lg_list'][] = $vv;
  222. $areaList[$k] = $v;
  223. }
  224. }
  225. }
  226. sort($areaList);
  227. //国家/地区 联赛列表
  228. $leagueList = array_merge($countryList,$areaList);
  229. $data = [$gjData,$leagueList];
  230. return $data;
  231. }
  232. foreach ($oddsCodeNum as $k=>$v){
  233. foreach ($matchNum as $kk=>$vv){
  234. if($v['match_id'] == $vv['match_id']){
  235. $match_odds[$kk]['lg_id'] = $vv['lg_id'];
  236. $match_odds[$kk]['leagueName'] = $vv['leagueName'];
  237. $match_odds[$kk]['match_id'] = $vv['match_id'];
  238. $match_odds[$kk]['home_team'] = $vv['home_team'];
  239. $match_odds[$kk]['guest_team'] = $vv['guest_team'];
  240. $match_odds[$kk]['match_date'] = $vv['match_date'];
  241. $match_odds[$kk]['match_time'] = $vv['match_time'];
  242. $match_odds[$kk]['tag'] = $vv['tag'];
  243. $match_odds[$kk]['oddsData'][] = $v;
  244. }
  245. }
  246. }
  247. //排序
  248. sort($match_odds);
  249. $data = [];
  250. foreach ($matchNum as $k=>$v){
  251. foreach ($match_odds as $kk=>$vv){
  252. if($v['lg_id'] == $vv['lg_id']){
  253. $data[$k]['area_id'] = $v['area_id'];
  254. $data[$k]['country_id'] = $v['country_id'];
  255. $data[$k]['lg_id'] = $v['lg_id'];
  256. $data[$k]['leagueName'] = $v['leagueName'];
  257. $data[$k]['matchData'][] = $vv;
  258. }
  259. }
  260. }
  261. //赛事详细数据 去重
  262. $matchData = $this->uniquArr($data,'lg_id');
  263. //国家数据
  264. $country = countryModel::getName();
  265. //获取国家联赛列表
  266. $countryList = [];
  267. foreach ($country as $k=>$v){
  268. foreach ($matchData as $kk=>$vv){
  269. if($v['country_id'] == $vv['country_id']){
  270. //联赛下赛事数量
  271. $vv['matchNum'] = count($vv['matchData']);
  272. unset($vv['matchData']);
  273. $v['lg_list'][] = $vv;
  274. $countryList[$k] = $v;
  275. }
  276. }
  277. }
  278. sort($countryList);
  279. //地区数据
  280. $area = areaModel::getName();
  281. //获取地区联赛列表
  282. $areaList = [];
  283. foreach ($area as $k=>$v){
  284. foreach ($matchData as $kk=>$vv){
  285. if($v['id'] == $vv['area_id']){
  286. //联赛下赛事数量
  287. $vv['matchNum'] = count($vv['matchData']);
  288. unset($vv['matchData']);
  289. $v['lg_list'][] = $vv;
  290. $areaList[$k] = $v;
  291. }
  292. }
  293. }
  294. sort($areaList);
  295. //国家/地区 联赛列表
  296. $leagueList = array_merge($countryList,$areaList);
  297. $data = [$matchData,$leagueList];
  298. return $data;
  299. }
  300. return $oddsCodeNum;
  301. }
  302. //根据match_id获取赔率数据
  303. public function getOddsData($game_code,$model_odds,$match_ids,$p_code='',$type=''){
  304. $match_id = [];
  305. $lg_ids = [];
  306. foreach ($match_ids as $k => $v){
  307. $match_id[] = $v['match_id'];
  308. $lg_ids[] = $v['lg_id'];
  309. }
  310. $select = ['match_id','p_code'];
  311. $whereOr = [];
  312. if($p_code){
  313. $select = ['lg_id','match_id','id','p_code','odds_code','condition','odds','odds_only','sort','status','team'];
  314. //获取冠军盘口
  315. if($p_code == 'kemp'){
  316. $whereOr = [['sort','=',0],['type','=',1]];
  317. $oddsData = lm($model_odds,"Sports")
  318. ->select($select)
  319. ->whereIn('lg_id', $lg_ids)
  320. ->where('source',$this->source['source'])
  321. ->where($whereOr)
  322. ->get()
  323. ->toArray();
  324. return $oddsData;
  325. }
  326. if($p_code == 'concede' and $game_code == 'zq'){
  327. $whereOr = [['sort','=',0]];
  328. $oddsData = lm($model_odds,"Sports")
  329. ->select($select)
  330. ->whereIn('match_id', $match_id)
  331. ->where('source',$this->source['source'])
  332. ->where($whereOr)
  333. ->whereIn('p_code',['concede','goal_size'])
  334. ->get()
  335. ->toArray();
  336. return $oddsData;
  337. }
  338. if($p_code == 'concede' and $game_code == 'lq'){
  339. $whereOr = [['sort','=',0]];
  340. $oddsData = lm($model_odds,"Sports")
  341. ->select($select)
  342. ->whereIn('match_id', $match_id)
  343. ->where('source',$this->source['source'])
  344. ->where($whereOr)
  345. ->whereIn('p_code',['concede','total_size','team_score'])
  346. ->get()
  347. ->toArray();
  348. return $oddsData;
  349. }
  350. if($p_code == 'concede' and $game_code == 'wq'){
  351. $whereOr = [['sort','=',0]];
  352. $oddsData = lm($model_odds,"Sports")
  353. ->select($select)
  354. ->whereIn('match_id', $match_id)
  355. ->where('source',$this->source['source'])
  356. ->where($whereOr)
  357. ->whereIn('p_code',['dishes','bureau','total_number'])
  358. ->get()
  359. ->toArray();
  360. return $oddsData;
  361. }
  362. if($p_code == 'concede' and $game_code == 'bq'){
  363. $whereOr = [['sort','=',0]];
  364. $oddsData = lm($model_odds,"Sports")
  365. ->select($select)
  366. ->whereIn('match_id', $match_id)
  367. ->where('source',$this->source['source'])
  368. ->where($whereOr)
  369. ->whereIn('p_code',['concede','total_size','two_sides'])
  370. ->get()
  371. ->toArray();
  372. return $oddsData;
  373. }
  374. else{
  375. $whereOr = [['p_code','=',$p_code],['sort','=',0]];
  376. $oddsData = lm($model_odds,"Sports")
  377. ->select($select)
  378. ->whereIn('match_id', $match_id)
  379. ->where('source',$this->source['source'])
  380. ->where($whereOr)
  381. ->get()
  382. ->toArray();
  383. return $oddsData;
  384. }
  385. }
  386. $oddsData = lm($model_odds,"Sports")
  387. ->select($select)
  388. ->whereIn('match_id', $match_id)
  389. ->where('source',$this->source['source'])
  390. ->where($whereOr)
  391. ->get()
  392. ->toArray();
  393. //按父级玩法分组
  394. $oddsCode = [];
  395. foreach ($oddsData as $kk=>$vv){
  396. $oddsCode[$vv['p_code']][]= $vv;
  397. }
  398. // 去重 获取每个玩法下的赛事数量
  399. $oddsCodeNum = [];
  400. $game_num = count($match_ids);
  401. foreach ($oddsCode as $kkk=>$vvv){
  402. $oddsCodeNum[$kkk]['game_code'] = $game_code;
  403. $oddsCodeNum[$kkk]['game_num'] = $game_num;
  404. $oddsCodeNum[$kkk]['p_code'] = $kkk;
  405. $oddsCodeNum[$kkk]['code_num'] = count($this->uniquArr($vvv,'match_id'));
  406. }
  407. sort($oddsCodeNum);
  408. return $oddsCodeNum;
  409. }
  410. /**
  411. * 二维数组根据某个字段去重
  412. * @param array $array 二维数组
  413. * @para array 去重后的数组
  414. */
  415. function uniquArr($array,$key){
  416. $result = array();
  417. foreach($array as $k=>$val){
  418. $code = false;
  419. foreach($result as $_val){
  420. if($_val[$key] == $val[$key]){
  421. $code = true;
  422. break;
  423. }
  424. }
  425. if(!$code){
  426. $result[]=$val;
  427. }
  428. }
  429. return $result;
  430. }
  431. //获取直播数据
  432. public function getBroadcast(){
  433. $data = lm('st_broadcast',"Sports")
  434. ->select('id','showid','shower','league_name','game_type','host_team','guest_team','start_time','doing')
  435. ->get()
  436. ->toArray();
  437. Render($data, '1', lang('Tips','Sports')->get('success'));
  438. }
  439. }