MatchListWeb.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. try {
  30. if(empty($type) ){
  31. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  32. }
  33. switch ($type){
  34. case 'StRollBall'://滚球
  35. $this->getRollBall($type,$game_code,$p_code);
  36. break;
  37. case 'StSoon'://即将
  38. $this->Other($type,$game_code,$p_code);
  39. break;
  40. case 'StToday'://今日
  41. $this->Other($type,$game_code,$p_code);
  42. break;
  43. case 'StMorningPlate'://早盘
  44. $this->Other($type,$game_code,$p_code);
  45. break;
  46. case 'StStringScene'://串场
  47. $this->Other($type,$game_code,$p_code);
  48. break;
  49. default:
  50. throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
  51. }
  52. Render([], '1', lang('Tips','Sports')->get('success'));
  53. } catch (\Exception $e) {
  54. echo $e->getMessage();
  55. }
  56. }
  57. //获取滚球
  58. public function getRollBall($type,$game_code,$p_code){
  59. //根据球类代码及玩法代码 获取相关赛事赔率数据
  60. if(!empty($game_code) and !empty($p_code)){
  61. $matchData = $this->getMatchData($type,$game_code,$p_code);
  62. Render($matchData, '1', lang('Tips','Sports')->get('success'));
  63. }
  64. $game = lm('GameType', 'Sports')->select('game_code')->where('status',1)->get()->toArray();
  65. // $gameData = [];
  66. // foreach ($game as $k=>$v){
  67. // if($v['game_code'] !='gj'){
  68. // $where = $this->commonFunction->getState($type);
  69. // $models = $this->commonFunction->getModels($v['game_code'],1);
  70. // $matchNum = lm($models['model_league'],"Sports")
  71. // ->join($models['model_match'],$models['model_match'].'.lg_id',$models['model_league'].'.lg_id')
  72. // ->where($models['model_match'].'.source',$this->source['source'])
  73. // ->where($where)
  74. // ->count("*");
  75. // //获取满足状态的球类
  76. // if($matchNum != 0){
  77. // $gameData[$v['game_code']] = $matchNum;
  78. // }
  79. // }
  80. // }
  81. // $gameData['all_num'] = array_sum($gameData);
  82. // $data[] = $gameData;
  83. // Render($data, '1', lang('Tips','Sports')->get('success'));
  84. $gameData = [];
  85. foreach ($game as $k=>$v){
  86. if($v['game_code'] !='gj'){
  87. $where = $this->commonFunction->getState($type);
  88. $models = $this->commonFunction->getModels($v['game_code'],1);
  89. $matchNum = lm($models['model_league'],"Sports")
  90. ->join($models['model_match'],$models['model_match'].'.lg_id',$models['model_league'].'.lg_id')
  91. ->where($models['model_match'].'.source',$this->source['source'])
  92. ->where($where)
  93. ->count("*");
  94. //获取满足状态的球类
  95. if($matchNum != 0){
  96. $gameData[$k]['game_code'] = $v['game_code'];
  97. $gameData[$k]['game_num'] = $matchNum;
  98. }
  99. }
  100. }
  101. sort($gameData);
  102. $num = [];
  103. foreach ($gameData as $kk=>$vv){
  104. $num[] = $vv['game_num'];
  105. }
  106. $gameData[]['all_num'] = array_sum($num);
  107. Render($gameData, '1', lang('Tips','Sports')->get('success'));
  108. }
  109. //获取非滚球数据
  110. public function Other($type,$game_code,$p_code){
  111. //根据球类代码及玩法代码 获取相关赛事赔率数据
  112. if(!empty($game_code) and !empty($p_code)){
  113. $matchData = $this->getMatchData($type,$game_code,$p_code);
  114. Render($matchData, '1', lang('Tips','Sports')->get('success'));
  115. }
  116. //根据状态获取所有球类/玩法下赛事数量
  117. $game = lm('GameType', 'Sports')->select('game_code')->where('status',1)->get()->toArray();
  118. $oddsCodeNum = [];
  119. foreach ($game as $k=>$v) {
  120. if ($v['game_code'] != 'gj') {
  121. //获取各球类下父级玩法赛事数量
  122. $oddsCodeNum[$k] = $this->getMatchData($type,$v['game_code'],'');
  123. }
  124. }
  125. $matchDataNum = [];
  126. foreach ($oddsCodeNum as $k => $v){
  127. foreach ($v as $kk=>$vv){
  128. $matchDataNum[] = $vv;
  129. }
  130. }
  131. Render($matchDataNum, '1', lang('Tips','Sports')->get('success'));
  132. }
  133. //获取赛事数据
  134. public function getMatchData($type,$game_code,$p_code=''){
  135. //$p_code 获取具体玩法下的赛事数据
  136. $where = $this->commonFunction->getState($type);
  137. $models = $this->commonFunction->getModels($game_code,1);
  138. $select = [$models['model_match'].'.match_id'];
  139. if($p_code){
  140. $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'];
  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($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')
  145. ->select($select)
  146. ->where($models['model_match'].'.source',$this->source['source'])
  147. ->where($where)
  148. ->get()->toArray();
  149. $oddsCodeNum = $this->getOddsData($game_code,$models['model_odds'],$matchNum,$p_code);
  150. if($p_code){
  151. $match_odds = [];
  152. foreach ($oddsCodeNum as $k=>$v){
  153. foreach ($matchNum as $kk=>$vv){
  154. if($v['match_id'] == $vv['match_id']){
  155. $match_odds[$kk]['lg_id'] = $vv['lg_id'];
  156. $match_odds[$kk]['leagueName'] = $vv['leagueName'];
  157. $match_odds[$kk]['match_id'] = $vv['match_id'];
  158. $match_odds[$kk]['home_team'] = $vv['home_team'];
  159. $match_odds[$kk]['guest_team'] = $vv['guest_team'];
  160. $match_odds[$kk]['match_date'] = $vv['match_date'];
  161. $match_odds[$kk]['match_time'] = $vv['match_time'];
  162. $match_odds[$kk]['tag'] = $vv['tag'];
  163. $match_odds[$kk]['oddsData'][] = $v;
  164. }
  165. }
  166. }
  167. //排序
  168. sort($match_odds);
  169. $data = [];
  170. foreach ($matchNum as $k=>$v){
  171. foreach ($match_odds as $kk=>$vv){
  172. if($v['lg_id'] == $vv['lg_id']){
  173. $data[$k]['lg_id'] = $v['lg_id'];
  174. $data[$k]['leagueName'] = $v['leagueName'];
  175. $data[$k]['matchData'][] = $vv;
  176. }
  177. }
  178. }
  179. //去重
  180. $data = $this->uniquArr($data,'lg_id');
  181. return $data;
  182. }
  183. return $oddsCodeNum;
  184. }
  185. //根据match_id获取赔率数据
  186. public function getOddsData($game_code,$model_odds,$match_ids,$p_code=''){
  187. $match_id = [];
  188. foreach ($match_ids as $k => $v){
  189. $match_id[] = $v['match_id'];
  190. }
  191. $select = ['match_id','p_code'];
  192. $whereOr = [];
  193. if($p_code){
  194. $whereOr = [['p_code','=',$p_code],['sort','=',0]];
  195. $select = ['match_id','id','p_code','odds_code','condition','odds','odds_only','sort','status'];
  196. }
  197. $oddsData = lm($model_odds,"Sports")
  198. // ->select('match_id','p_code')(
  199. ->select($select)
  200. ->whereIn('match_id', $match_id)
  201. ->where('source',$this->source['source'])
  202. ->where($whereOr)
  203. ->get()
  204. ->toArray();
  205. if($p_code){
  206. return $oddsData;
  207. }
  208. //按父级玩法分组
  209. $oddsCode = [];
  210. foreach ($oddsData as $kk=>$vv){
  211. $oddsCode[$vv['p_code']][]= $vv;
  212. }
  213. // 去重 获取每个玩法下的赛事数量
  214. $oddsCodeNum = [];
  215. $game_num = count($match_ids);
  216. foreach ($oddsCode as $kkk=>$vvv){
  217. $oddsCodeNum[$kkk]['game_code'] = $game_code;
  218. $oddsCodeNum[$kkk]['game_num'] = $game_num;
  219. $oddsCodeNum[$kkk]['p_code'] = $kkk;
  220. $oddsCodeNum[$kkk]['code_num'] = count($this->uniquArr($vvv,'match_id'));
  221. }
  222. sort($oddsCodeNum);
  223. return $oddsCodeNum;
  224. }
  225. /**
  226. * 二维数组根据某个字段去重
  227. * @param array $array 二维数组
  228. * @para array 去重后的数组
  229. */
  230. function uniquArr($array,$key){
  231. $result = array();
  232. foreach($array as $k=>$val){
  233. $code = false;
  234. foreach($result as $_val){
  235. if($_val[$key] == $val[$key]){
  236. $code = true;
  237. break;
  238. }
  239. }
  240. if(!$code){
  241. $result[]=$val;
  242. }
  243. }
  244. return $result;
  245. }
  246. }