GetmatchData.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. <?php
  2. /**
  3. * 根据不同状态获取 赛事数据
  4. * User: Jun.peng
  5. * Date: 2019/4/4
  6. * Time: 10:57
  7. */
  8. namespace Biz\Match;
  9. use Biz\Match\GetOddsData;
  10. class GetmatchData {
  11. public function __construct() {
  12. $this->getOddsData = new GetOddsData();
  13. }
  14. /**
  15. * @param $ret 提交参数
  16. * @param $source 数据源条件
  17. * @return array
  18. * @throws \Exception
  19. * 获取不同状态下的赛事相关数据
  20. */
  21. public function typeData($ret,$source){
  22. //根据球类代码 获取相关model
  23. $models = getModels($ret['game_code']);
  24. $model_match = $models['model_match'];
  25. //获取 不同状态的查询条件
  26. $where = getState($ret['type_code'],$model_match);
  27. //根据联赛id查询
  28. if(!empty($ret['lg_id'])){
  29. $lg_id = $ret['lg_id'];
  30. }
  31. //根据搜索查询
  32. if(!empty($ret['search'])){
  33. $search = $ret['search'];
  34. }
  35. switch ($ret['type_code']){
  36. case 'StRollBall'://滚球
  37. $data = $this->getRollBall($source,$models,$where,$ret,$search);
  38. break;
  39. case 'StSoon'://即将
  40. $data = $this->getSoon($source,$models,$where,$lg_id,$search);
  41. break;
  42. case 'StToday'://今日
  43. $data = $this->getToday($source,$models,$where,$lg_id,$search);
  44. break;
  45. case 'StMorningPlate'://早盘
  46. //默认当天
  47. if(empty($ret['match_date'])){
  48. $match_date = date("Y-m-d");
  49. }else{
  50. if($ret['match_date'] != 'other'){
  51. $match_date = $ret['match_date'];
  52. }else{
  53. $match_date = 'other';
  54. }
  55. }
  56. $data = $this->getMorningPlate($source,$models,$where,$lg_id,$search,$match_date);
  57. break;
  58. case 'StStringScene'://串场
  59. if(empty($ret['str_type']) || $ret['str_type'] ==0){//赛事
  60. $data = $this->getStringScene($source,$models,$where,$lg_id,$search);
  61. }
  62. if($ret['str_type'] == 1){//参赛表
  63. }
  64. if($ret['str_type'] == 2){//冠军盘口
  65. $where = getState('StChampion',$model_match);
  66. $data = $this->getChampion($source,$models,$where,$lg_id,$search,'StChampion');
  67. }
  68. break;
  69. case 'StChampion'://冠军
  70. $data = $this->getChampion($source,$models,$where,$lg_id,$search,$ret['type_code']);
  71. break;
  72. default:
  73. throw new \Exception(Render([], '10002', lang('Tips','Sports')->get('PARAM_ERROR')));
  74. }
  75. return $data;
  76. }
  77. /**
  78. * 获取滚球数据
  79. */
  80. public function getRollBall($source,$models,$where,$ret,$search=''){
  81. $model_match = $models['model_match'];
  82. $model_odds = $models['model_odds'];
  83. $model_league = $models['model_league'];
  84. $model_result = $models['model_result'];
  85. //当前状态下所有联赛
  86. $data = lm($model_league,"Sports")
  87. ->join($model_match,$model_match.'.lg_id',$model_league.'.lg_id')
  88. ->join($model_result,$model_result.'.match_id',$model_match.'.match_id')
  89. ->select($model_league.'.lg_id',$model_league.'.name_chinese as leagueName')
  90. ->distinct($model_league.'.name_chinese')
  91. ->where($model_league.'.source',$source['source'])
  92. ->where($where)
  93. ->where(function($query)use ($model_match,$search){
  94. $query->where($model_match.'.home_team','like','%'.$search.'%')
  95. ->orWhere(function($query)use ($model_match,$search) {
  96. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  97. });
  98. })
  99. ->get()
  100. ->toarray();
  101. if(empty($data)){
  102. $data = [];
  103. return $data;
  104. }
  105. //统计联赛下的赛事及查询赛事
  106. foreach($data as $k => $v){
  107. $data[$k]['matchNum'] = lm($model_match,"Sports")
  108. ->where($source)
  109. ->where('lg_id',$v['lg_id'])
  110. ->where($where)
  111. ->count();
  112. $data[$k]['matchData'] = lm($model_match,"Sports")
  113. ->leftjoin($model_result,$model_result.'.match_id',$model_match.'.match_id')
  114. ->select($model_match.'.match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team','home_score','guest_score',$model_result.'.match_time as a_time','match_process')
  115. ->where($model_match.'.source',$source['source'])
  116. ->where($model_match.'.lg_id',$v['lg_id'])
  117. ->where($where)
  118. ->get()
  119. ->toarray();
  120. }
  121. foreach($data as $k => $v){
  122. foreach($v['matchData'] as $kk => $vv){
  123. $oddsData= lm($model_match,"Sports")
  124. ->leftjoin($model_odds,$model_odds.'.match_id',$model_match.'.match_id')
  125. ->select($model_odds.'.id','p_code','odds_code',$model_odds.'.status','odds','condition','sort')
  126. ->where($model_match.'.source',$source['source'])
  127. ->where([$model_odds.'.match_id' => $vv['match_id'],$model_odds.'.type'=>0])
  128. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  129. ->where(function($query)use ($model_odds){
  130. $query->where($model_odds.'.odds_code','concede_home')
  131. ->orWhere(function($query)use ($model_odds){
  132. $query->where($model_odds.'.odds_code','concede_guest');
  133. })
  134. ->orWhere(function($query)use ($model_odds){
  135. $query->where($model_odds.'.odds_code','size_home');
  136. })
  137. ->orWhere(function($query)use ($model_odds){
  138. $query->where($model_odds.'.odds_code','size_guest');
  139. })
  140. ->orWhere(function($query)use ($model_odds){
  141. $query->where($model_odds.'.odds_code','capot_home');
  142. })
  143. ->orWhere(function($query)use ($model_odds){
  144. $query->where($model_odds.'.odds_code','capot_dogfall');
  145. })
  146. ->orWhere(function($query)use ($model_odds){
  147. $query->where($model_odds.'.odds_code','capot_guest');
  148. });
  149. })
  150. ->get()
  151. ->toarray();
  152. //根据 排序 获取 最新让球/大小玩法赔率
  153. $sortData = array_column($oddsData,'sort');
  154. array_multisort($sortData,SORT_DESC,$oddsData);
  155. $zu = [];
  156. foreach ($oddsData as $key1 =>$item1){
  157. $zu[$item1['p_code']][] = $item1;
  158. }
  159. $c_s = array_slice($zu['concede_size'],0,4);//让球/大小 前四条 放入
  160. $capot = $zu['capot'];//独赢
  161. $data[$k]['matchData'][$kk]['oddsData'] = [$c_s,$capot];
  162. }
  163. }
  164. return $data;
  165. }
  166. /**
  167. * 获取即将数据
  168. */
  169. public function getSoon($source,$models,$where,$lg_id=0,$search=''){
  170. $model_match = $models['model_match'];
  171. $model_odds = $models['model_odds'];
  172. $model_league = $models['model_league'];
  173. $model_result = $models['model_result'];
  174. //当前状态下所有联赛
  175. $data = lm($model_league,"Sports")
  176. ->join($model_match,$model_match.'.lg_id',$model_league.'.lg_id')
  177. ->select($model_league.'.lg_id',$model_league.'.name_chinese as leagueName')
  178. ->distinct($model_league.'.name_chinese')
  179. ->where($model_league.'.source',$source['source'])
  180. ->where($where)
  181. ->where(function($query)use ($model_match,$search){
  182. $query->where($model_match.'.home_team','like','%'.$search.'%')
  183. ->orWhere(function($query)use ($model_match,$search) {
  184. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  185. });
  186. })
  187. ->get()
  188. ->toarray();
  189. if(empty($data)){
  190. $data = [];
  191. return $data;
  192. }
  193. //统计联赛下的赛事及查询赛事
  194. foreach($data as $k => $v){
  195. $data[$k]['matchNum'] = lm($model_match,"Sports")
  196. ->where($source)
  197. ->where('lg_id',$v['lg_id'])
  198. ->where($where)
  199. ->count();
  200. $data[$k]['matchData'] = lm($model_match,"Sports")
  201. ->select($model_match.'.match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team')
  202. ->where($source)
  203. ->where($model_match.'.lg_id',$v['lg_id'])
  204. ->where($where)
  205. ->get()
  206. ->toarray();
  207. }
  208. foreach($data as $k => $v){
  209. foreach($v['matchData'] as $kk => $vv){
  210. $wait_time = ceil(((strtotime( $vv['match_time']))-time())/60);
  211. $data[$k]['matchData'][$kk]['wait_time'] = $wait_time;//追加距离开赛时间
  212. $oddsData= lm($model_match,"Sports")
  213. ->leftjoin($model_odds,$model_odds.'.match_id',$model_match.'.match_id')
  214. ->select($model_odds.'.id','odds_only','p_code','odds_code',$model_odds.'.status','odds','condition','sort')
  215. ->where($model_match.'.source',$source['source'])
  216. ->where([$model_odds.'.match_id' => $vv['match_id'],$model_odds.'.type'=>0])
  217. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  218. ->where(function($query)use ($model_odds){
  219. $query->where($model_odds.'.odds_code','concede_home')
  220. ->orWhere(function($query)use ($model_odds){
  221. $query->where($model_odds.'.odds_code','concede_guest');
  222. })
  223. ->orWhere(function($query)use ($model_odds){
  224. $query->where($model_odds.'.odds_code','size_home');
  225. })
  226. ->orWhere(function($query)use ($model_odds){
  227. $query->where($model_odds.'.odds_code','size_guest');
  228. })
  229. ->orWhere(function($query)use ($model_odds){
  230. $query->where($model_odds.'.odds_code','capot_home');
  231. })
  232. ->orWhere(function($query)use ($model_odds){
  233. $query->where($model_odds.'.odds_code','capot_dogfall');
  234. })
  235. ->orWhere(function($query)use ($model_odds){
  236. $query->where($model_odds.'.odds_code','capot_guest');
  237. });
  238. })
  239. ->get()
  240. ->toarray();
  241. //根据 排序 获取 最新让球/大小玩法赔率
  242. $sortData = array_column($oddsData,'sort');
  243. array_multisort($sortData,SORT_DESC,$oddsData);
  244. $zu = [];
  245. foreach ($oddsData as $key1 =>$item1){
  246. $zu[$item1['p_code']][] = $item1;
  247. }
  248. $c_s = array_slice($zu['concede_size'],0,4);//让球/大小 前四条 放入
  249. $capot = $zu['capot'];//独赢
  250. $data[$k]['matchData'][$kk]['oddsData'] = [$c_s,$capot];
  251. }
  252. }
  253. return $data;
  254. }
  255. /**
  256. * 获取今日数据
  257. */
  258. public function getToday($source,$models,$where,$lg_id=0,$search=''){
  259. //获取model
  260. $model_match = $models['model_match'];
  261. $model_odds = $models['model_odds'];
  262. $model_league = $models['model_league'];
  263. $model_result = $models['model_result'];
  264. //根据联赛id 获取联赛下 赛事赔率数据
  265. if($lg_id >0){
  266. $leagueData = $this->getMatchOdds($source,$model_league,$model_match,$model_odds,$where,$search,$lg_id);
  267. return $leagueData;
  268. }
  269. $data = $this->getMatch($source,$model_league,$model_match,'',$search,$where);
  270. return $data;
  271. }
  272. /**
  273. * 获取早盘数据
  274. */
  275. public function getMorningPlate($source,$models,$where,$lg_id=0,$search='',$time=''){
  276. //获取model
  277. $model_match = $models['model_match'];
  278. $model_odds = $models['model_odds'];
  279. $model_league = $models['model_league'];
  280. $model_result = $models['model_result'];
  281. //根据联赛id 获取联赛下 赛事赔率数据
  282. if($lg_id >0){
  283. $leagueData = $this->getMatchOdds($source,$model_league,$model_match,$model_odds,$where,$search,$lg_id,$time);
  284. return $leagueData;
  285. }
  286. $data = $this->getMatch($source,$model_league,$model_match,'',$search,$where,$time);
  287. return $data;
  288. }
  289. /**
  290. * 获取串场数据
  291. */
  292. public function getStringScene($source,$models,$where,$lg_id=0,$search='',$time){
  293. //获取model
  294. $model_match = $models['model_match'];
  295. $model_odds = $models['model_odds'];
  296. $model_league = $models['model_league'];
  297. $model_result = $models['model_result'];
  298. //根据联赛id 获取联赛下 赛事赔率数据
  299. if($lg_id >0){
  300. $leagueData = $this->getMatchOdds($source,$model_league,$model_match,$model_odds,$where,$search,$lg_id,$time);
  301. return $leagueData;
  302. }
  303. $data = $this->getMatch($source,$model_league,$model_match,'',$search,$where,$time);
  304. return $data;
  305. }
  306. /**
  307. * 获取冠军数据
  308. */
  309. public function getChampion($source,$models,$where,$lg_id=0,$search='',$type_code=''){
  310. //获取model
  311. $model_odds = $models['model_odds'];
  312. $model_league = $models['model_league'];
  313. //根据联赛id 获取联赛下 冠军玩法盘口
  314. if($lg_id >0){
  315. $last_time = lm($model_league, 'Sports')
  316. ->select('last_time')
  317. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  318. ->where($source)
  319. ->where('lg_id',$lg_id)
  320. ->first()->last_time;
  321. $championData = lm($model_odds, 'Sports')
  322. ->select("id","lg_id","p_code","odds_code","team","odds","sort","status","odds_only")
  323. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  324. ->where($source)
  325. ->where('lg_id',$lg_id)
  326. ->where($where)
  327. ->groupBy("id","lg_id","p_code","odds_code","team","odds","sort","status","odds_only")
  328. ->get()->toArray();
  329. //按p_code分组
  330. $p_code=array();
  331. foreach($championData as $k=>$v){
  332. $p_code[$v['p_code']][]=$v;
  333. }
  334. $sd = [];
  335. foreach ($p_code as $k1=>$v1){
  336. foreach ($v1 as $k2=>$v2){
  337. $sd[$v2['p_code']]['last_time'] =$last_time;
  338. $sd[$v2['p_code']][$v2['odds_code']][] = $v2;
  339. }
  340. }
  341. return $sd;
  342. }
  343. //获取国家/洲下所有联赛及数量
  344. $data = $this->getMatch($source,$model_league,'',$model_odds,$search,$where,'',$type_code);
  345. return $data;
  346. }
  347. /**
  348. * 获取国家/洲 下联赛
  349. */
  350. public function getMatch($source,$model_league,$model_match='',$model_odds='',$search,$where,$time='',$type_code=''){
  351. //国家下所有联赛
  352. $country = lm($model_league,"Sports")
  353. ->leftjoin('st_country','st_country.country_id',$model_league.'.country_id')
  354. ->select('st_country.country_id','st_country.name_chinese as region')
  355. // ->select('st_country.country_id as region_id','st_country.name_chinese as region',$model_league.'.lg_id', $model_league.'.name_chinese as league')
  356. ->distinct('st_country.name_chinese')
  357. ->where($model_league.'.source',$source['source'])
  358. ->where($model_league.'.name_chinese','like','%'.$search.'%')
  359. ->get()
  360. ->toArray();
  361. //洲下所有联赛
  362. $area = lm($model_league,"Sports")
  363. ->leftjoin('st_area','st_area.id',$model_league.'.area_id')
  364. ->select('st_area.id as area_id','st_area.title as region')
  365. // ->select('st_area.id as region_id','st_area.title as region',$model_league.'.lg_id', $model_league.'.name_chinese as league')
  366. ->distinct('st_area.title')
  367. ->where($model_league.'.source',$source['source'])
  368. ->where($model_league.'.name_chinese','like','%'.$search.'%')
  369. ->get()
  370. ->toArray();
  371. if($type_code != 'StChampion'){//非冠军赛事
  372. $data = $this->getMatchNum($source,$country,$area,$model_league,$model_match,$model_odds,$where,$search,$time);
  373. }else{//冠军赛事
  374. $data = $this->getMatchStChampionNum($source,$country,$area,$model_league,$model_match,$model_odds,$where,$search,$time);
  375. }
  376. return $data;
  377. }
  378. /**
  379. * 获取联赛下赛事 数量
  380. * 非冠军盘口
  381. */
  382. public function getMatchNum($source,$country,$area,$model_league,$model_match='',$model_odds='',$where,$search,$time='',$type=0){
  383. //统计国家下联赛-赛事/盘口数量
  384. if(!empty($country)){
  385. foreach($country as $k => $v) {
  386. $country[$k]['league_count'] = lm($model_league, 'Sports')
  387. ->select('lg_id', 'name_chinese as league')
  388. ->where($source)
  389. ->where('country_id', $v['country_id'])
  390. ->where($model_league.'.name_chinese','like','%'.$search.'%')
  391. ->get()->toarray();
  392. }
  393. foreach ($country as $k => $v) {
  394. foreach($v['league_count'] as $kk => $vv ){
  395. if(!empty($model_match)){//非冠军玩法,获取联赛下赛事数量
  396. if(empty($time)){
  397. $country[$k]['league_count'][$kk]['count'] = lm($model_match, 'Sports')
  398. ->select("match_id")
  399. ->where($source)
  400. ->where('lg_id',$vv['lg_id'])
  401. ->where($where)
  402. ->count("*");
  403. }else{//早盘 下有时间查询
  404. if($time == 'other'){//早盘 一周后
  405. $country[$k]['league_count'][$kk]['count'] = lm($model_match, 'Sports')
  406. ->select("match_id")
  407. ->where($source)
  408. ->where('lg_id',$vv['lg_id'])
  409. ->where($where)
  410. ->where('match_date','>',date("Y-m-d",strtotime("+1weeks",strtotime(date('Y-m-d',time())))))
  411. ->count("*");
  412. }else{
  413. $country[$k]['league_count'][$kk]['count'] = lm($model_match, 'Sports')
  414. ->select("match_id")
  415. ->where($source)
  416. ->where('lg_id',$vv['lg_id'])
  417. ->where($where)
  418. ->where('match_date',$time)
  419. ->count("*");
  420. }
  421. }
  422. }
  423. if($country[$k]['league_count'][$kk]['count'] == 0){
  424. unset($country[$k]['league_count'][$kk]);
  425. }
  426. }
  427. }
  428. }
  429. //统计 地区 联赛-赛事/盘口数量
  430. if(!empty($area)){
  431. foreach($area as $k => $v) {
  432. $area[$k]['league_count'] = lm($model_league, 'Sports')
  433. ->select('lg_id', 'name_chinese as league')
  434. ->where($source)
  435. ->where('area_id', $v['area_id'])
  436. ->where($model_league.'.name_chinese','like','%'.$search.'%')
  437. ->get()->toarray();
  438. }
  439. foreach ($area as $k => $v) {
  440. foreach($v['league_count'] as $kk => $vv ){
  441. if(!empty($model_match)){//非冠军玩法,获取联赛下赛事数量
  442. if(empty($time)){
  443. $area[$k]['league_count'][$kk]['count'] = lm($model_match, 'Sports')
  444. ->select("match_id")
  445. ->where($source)
  446. ->where('lg_id',$vv['lg_id'])
  447. ->where($where)
  448. ->count("*");
  449. }else {
  450. if ($time == 'other') {// 今日 一周后
  451. $area[$k]['league_count'][$kk]['count'] = lm($model_match, 'Sports')
  452. ->select("match_id")
  453. ->where($source)
  454. ->where('lg_id', $vv['lg_id'])
  455. ->where($where)
  456. ->where('match_date', '>', date("Y-m-d", strtotime("+1weeks", strtotime(date('Y-m-d', time())))))
  457. ->count("*");
  458. } else {//今日 大于今日
  459. $area[$k]['league_count'][$kk]['count'] = lm($model_match, 'Sports')
  460. ->select("match_id")
  461. ->where($source)
  462. ->where('lg_id',$vv['lg_id'])
  463. ->where($where)
  464. ->where('match_date',$time)
  465. ->count("*");
  466. }
  467. }
  468. }
  469. if($area[$k]['league_count'][$kk]['count'] == 0){
  470. unset($area[$k]['league_count'][$kk]);
  471. }
  472. }
  473. }
  474. }
  475. //处理空联赛国家
  476. $countryData = handleArr($country);
  477. $areaData = handleArr($area);
  478. //合并数组
  479. $data = array_merge($countryData,$areaData);
  480. return $data;
  481. }
  482. /**
  483. * 获取联赛下赛事 数量
  484. * 冠军盘口
  485. */
  486. public function getMatchStChampionNum($source,$country,$area,$model_league,$model_match='',$model_odds='',$where,$search,$time='',$type=0){
  487. $leagueData = lm($model_league,'Sports')
  488. ->select('id','lg_id','name_chinese as league','country_id','area_id')
  489. ->get()
  490. ->where($source)
  491. ->toArray();
  492. $oddsData = lm($model_odds,'Sports')
  493. ->select('id','lg_id','p_code')
  494. ->distinct("p_code")//去重
  495. ->where($where)
  496. ->where($source)
  497. ->get()
  498. ->toArray();
  499. //按国家
  500. $countryData = [];
  501. if(!empty($country) and !empty($leagueData) and($oddsData)){
  502. foreach ($country as $k=>$v){
  503. if($v['country_id']){
  504. $countryData[$k] = $v;
  505. foreach ($leagueData as $kk=>$vv){
  506. if($v['country_id'] == $vv['country_id']){
  507. $countryData[$k]['league_count'][] = $vv;
  508. }
  509. }
  510. }
  511. }
  512. }
  513. //去除无联赛 国家
  514. foreach ($countryData as $k=>$v){
  515. if(count($v) == 2){
  516. unset($countryData[$k]);
  517. }
  518. }
  519. //按地区
  520. $areaData = [];
  521. if(!empty($area) and !empty($leagueData) and($oddsData)){
  522. foreach ($area as $k=>$v){
  523. if($v['area_id']){
  524. $areaData[$k] = $v;
  525. foreach ($leagueData as $kk=>$vv){
  526. if($v['area_id'] == $vv['area_id']){
  527. $areaData[$k]['league_count'][] = $vv;
  528. }
  529. }
  530. }
  531. }
  532. }
  533. //去除无联赛 地区
  534. foreach ($areaData as $k=>$v){
  535. if(count($v) == 2){
  536. unset($areaData[$k]);
  537. }
  538. }
  539. $countryData = array_gj_tt($countryData,$oddsData);
  540. $areaData = array_gj_tt($areaData,$oddsData);
  541. $data = array_merge($countryData,$areaData);
  542. return $data;
  543. }
  544. /**
  545. * 获取联赛下 赛事赔率
  546. */
  547. public function getMatchOdds($source,$model_league,$model_match,$model_odds,$where,$search,$lg_id,$time=''){
  548. $leagueData = lm($model_league,"Sports")
  549. ->select('lg_id','name_chinese as leagueName')
  550. ->where($source)
  551. ->where('lg_id',$lg_id)
  552. ->first();
  553. if(!empty($time)){//早盘 按时间查询
  554. if($time == 'other'){//早盘 一周后
  555. $matchData = lm($model_match,"Sports")
  556. ->select($model_match.'.match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team')
  557. ->where($source)
  558. ->where($model_match.'.lg_id',$lg_id)
  559. ->where($where)
  560. ->where('match_date','>',date("Y-m-d",strtotime("+1weeks",strtotime(date('Y-m-d',time())))))
  561. ->where(function($query)use ($model_match,$search){
  562. $query->where($model_match.'.home_team','like','%'.$search.'%')
  563. ->orWhere(function($query)use ($model_match,$search) {
  564. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  565. });
  566. })
  567. ->get()
  568. ->toarray();
  569. }else{
  570. if($time == date('Y-m-d')){//早盘今天
  571. $matchData = lm($model_match,"Sports")
  572. ->select($model_match.'.match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team')
  573. ->where($source)
  574. ->where($model_match.'.lg_id',$lg_id)
  575. ->where('is_morningplate','=',1)
  576. ->where('match_date',$time)
  577. ->where('match_time','>',date("H:i:s", time()))
  578. ->where(function($query)use ($model_match,$search){
  579. $query->where($model_match.'.home_team','like','%'.$search.'%')
  580. ->orWhere(function($query)use ($model_match,$search) {
  581. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  582. });
  583. })
  584. ->get()
  585. ->toarray();
  586. }else{//早盘 今天以后
  587. $matchData = lm($model_match,"Sports")
  588. ->select($model_match.'.match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team')
  589. ->where($source)
  590. ->where($model_match.'.lg_id',$lg_id)
  591. ->where($where)
  592. ->where('match_date',$time)
  593. ->where(function($query)use ($model_match,$search){
  594. $query->where($model_match.'.home_team','like','%'.$search.'%')
  595. ->orWhere(function($query)use ($model_match,$search) {
  596. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  597. });
  598. })
  599. ->get()
  600. ->toarray();
  601. }
  602. }
  603. }else{
  604. $matchData = lm($model_match,"Sports")
  605. ->select($model_match.'.match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team')
  606. ->where($source)
  607. ->where($model_match.'.lg_id',$lg_id)
  608. ->where($where)
  609. ->where(function($query)use ($model_match,$search){
  610. $query->where($model_match.'.home_team','like','%'.$search.'%')
  611. ->orWhere(function($query)use ($model_match,$search) {
  612. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  613. });
  614. })
  615. ->get()
  616. ->toarray();
  617. }
  618. foreach($matchData as $kk => $vv) {
  619. $oddsData = lm($model_match, "Sports")
  620. ->leftjoin($model_odds, $model_odds . '.match_id', $model_match . '.match_id')
  621. ->select($model_odds . '.id', 'odds_only','p_code', 'odds_code', $model_odds . '.status', 'odds', 'condition', 'sort')
  622. ->where($model_match.'.source',$source['source'])
  623. ->where([$model_odds . '.match_id' => $vv['match_id'], $model_odds . '.type' => 0])
  624. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  625. ->where(function ($query) use ($model_odds) {
  626. $query->where($model_odds . '.odds_code', 'concede_home')
  627. ->orWhere(function ($query) use ($model_odds) {
  628. $query->where($model_odds . '.odds_code', 'concede_guest');
  629. })
  630. ->orWhere(function ($query) use ($model_odds) {
  631. $query->where($model_odds . '.odds_code', 'size_home');
  632. })
  633. ->orWhere(function ($query) use ($model_odds) {
  634. $query->where($model_odds . '.odds_code', 'size_guest');
  635. });
  636. })
  637. ->get()
  638. ->toarray();
  639. //根据 排序 获取 最新让球/大小玩法赔率
  640. $sortData = array_column($oddsData,'sort');
  641. array_multisort($sortData,SORT_DESC,$oddsData);
  642. $zu = [];
  643. foreach ($oddsData as $key1 =>$item1){
  644. $zu[$item1['p_code']][] = $item1;
  645. }
  646. $c_s = array_slice($zu['concede_size'],0,4);//让球/大小 前四条 放入
  647. $matchData[$kk]['oddsData'] = $c_s;
  648. }
  649. $leagueData->matchNum = count($matchData);
  650. $leagueData->matchData = $matchData;
  651. return $leagueData;
  652. }
  653. /**
  654. * @param $source 数据源
  655. * @param $where 该状态 查询条件
  656. * @return array
  657. * @throws \Exception
  658. * 获取所有即将开赛 数据
  659. */
  660. public function getAllSoon($source,$where){
  661. $game = lm('GameType', 'Sports')->select('id','game_name','game_code','game_ico_url')->where('status',1)->get()->toArray();
  662. $data=[];
  663. foreach ($game as $key=>$item){
  664. if($item['game_code'] !='gj'){
  665. $model = getModels($item['game_code']);
  666. $matchData = lm($model['model_match'], 'Sports')
  667. ->select('match_id','home_team','guest_team','match_time')
  668. ->where($source)
  669. ->where($where)
  670. ->get()->toArray();
  671. foreach ($matchData as $key1 => $item1){
  672. //获取等待时间
  673. $wait_time = ceil(((strtotime($item1['match_time']))-time())/60);
  674. $matchData[$key1]['wait_time'] = $wait_time;
  675. }
  676. $matchNum = count($matchData);
  677. if($matchNum > 0){
  678. $data[$item['game_code']]['matchData'] = $matchData;
  679. $data[$item['game_code']]['matchNum'] = $matchNum;
  680. $data[$item['game_code']]['gameName'] = $item['game_name'];
  681. $data[$item['game_code']]['gameCode'] = $item['game_code'];
  682. }
  683. }
  684. }
  685. return $data;
  686. }
  687. /**
  688. * 获取欧冠 各状态赛事数据
  689. */
  690. public function getUEFAChampions($source){
  691. //根据搜索查询
  692. if(!empty($_GET['search'])){
  693. $search = $_GET['search'];
  694. }
  695. $model = getModels('gj',1);
  696. $model_league = $model['model_league'];
  697. $leagueData = lm($model_league,'Sports')
  698. ->select('name_chinese','lg_id','last_time')
  699. ->where($model_league.'.source',$source['source'])
  700. ->where($model_league.'.name_chinese','欧洲冠军杯')
  701. ->first();
  702. //获取滚球数据
  703. $StRollBall = $this->getUEFAStRollBall($model,'StRollBall',$source,$leagueData->lg_id,$search);
  704. //获取今日数据
  705. $StToday = $this->getUEFAStToday($model,'StToday',$source,$leagueData->lg_id,$search);
  706. //获取早盘数据
  707. $StMorningPlate = $this->getUEFAStMorningPlate($model,'StMorningPlate',$source,$leagueData->lg_id,$search);
  708. //获取冠军盘口数据
  709. $StChampion = $this->getUEFAStChampion($model,'StChampion',$source,$leagueData->lg_id,$leagueData->last_time);
  710. $data = [
  711. 'leagueName'=> $leagueData->name_chinese,
  712. 'StRollBall'=>[//滚球数据
  713. 'typeName'=>'滚球',
  714. 'matchNum'=>count($StRollBall),
  715. 'matchData'=>$StRollBall
  716. ],
  717. 'StToday'=>[//今日数据
  718. 'typeName'=>'今日',
  719. 'matchNum'=>count($StToday),
  720. 'matchData'=>$StToday
  721. ],
  722. 'StMorningPlate'=>[//早盘数据
  723. 'typeName'=>'早盘',
  724. 'matchNum'=>count($StMorningPlate),
  725. 'matchData'=>$StMorningPlate
  726. ],
  727. 'StChampion'=>[//冠军盘口
  728. 'typeName'=>'冠军盘口',
  729. 'matchNum'=>count($StChampion),
  730. 'matchData'=>$StChampion
  731. ]
  732. ];
  733. return $data;
  734. }
  735. /**
  736. * 获取欧冠 滚球数据
  737. */
  738. public function getUEFAStRollBall($model,$type_code,$source,$lg_id,$search=''){
  739. $model_match = $model['model_match'];
  740. $model_odds = $model['model_odds'];
  741. $model_result = $model['model_result'];
  742. $where = getState($type_code,$model_match);
  743. $matchData = lm($model_match,"Sports")
  744. ->leftjoin($model_result,$model_result.'.match_id',$model_match.'.match_id')
  745. ->select($model_match.'.match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team','home_score','guest_score',$model_result.'.match_time as a_time','match_process')
  746. ->where($model_match.'.source',$source['source'])
  747. ->where($model_match.'.lg_id',$lg_id)
  748. ->where($where)
  749. ->where(function($query)use ($model_match,$search){
  750. $query->where($model_match.'.home_team','like','%'.$search.'%')
  751. ->orWhere(function($query)use ($model_match,$search) {
  752. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  753. });
  754. })
  755. ->get()
  756. ->toarray();
  757. foreach($matchData as $kk => $vv){
  758. $oddsData= lm($model_match,"Sports")
  759. ->leftjoin($model_odds,$model_odds.'.match_id',$model_match.'.match_id')
  760. ->select($model_odds.'.id','p_code','odds_code',$model_odds.'.status','odds','condition','sort')
  761. ->where($model_match.'.source',$source['source'])
  762. ->where([$model_odds.'.match_id' => $vv['match_id'],$model_odds.'.type'=>0])
  763. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  764. ->where(function($query)use ($model_odds){
  765. $query->where($model_odds.'.odds_code','concede_home')
  766. ->orWhere(function($query)use ($model_odds){
  767. $query->where($model_odds.'.odds_code','concede_guest');
  768. })
  769. ->orWhere(function($query)use ($model_odds){
  770. $query->where($model_odds.'.odds_code','size_home');
  771. })
  772. ->orWhere(function($query)use ($model_odds){
  773. $query->where($model_odds.'.odds_code','size_guest');
  774. })
  775. ->orWhere(function($query)use ($model_odds){
  776. $query->where($model_odds.'.odds_code','capot_home');
  777. })
  778. ->orWhere(function($query)use ($model_odds){
  779. $query->where($model_odds.'.odds_code','capot_dogfall');
  780. })
  781. ->orWhere(function($query)use ($model_odds){
  782. $query->where($model_odds.'.odds_code','capot_guest');
  783. });
  784. })
  785. ->get()
  786. ->toarray();
  787. //根据 排序 获取 最新让球/大小玩法赔率
  788. $sortData = array_column($oddsData,'sort');
  789. array_multisort($sortData,SORT_DESC,$oddsData);
  790. $zu = [];
  791. foreach ($oddsData as $key1 =>$item1){
  792. $zu[$item1['p_code']][] = $item1;
  793. }
  794. $c_s = array_slice($zu['concede_size'],0,4);//让球/大小 前四条 放入
  795. $capot = $zu['capot'];//独赢
  796. $matchData[$kk]['oddsData'] = [$c_s,$capot];
  797. }
  798. return $matchData;
  799. }
  800. /**
  801. * 获取欧冠 今日数据
  802. */
  803. public function getUEFAStToday($model,$type_code,$source,$lg_id,$search=''){
  804. $model_match = $model['model_match'];
  805. $model_odds = $model['model_odds'];
  806. $where = getState($type_code);
  807. $matchData = lm($model_match,'Sports')
  808. ->select('match_id','match_date','match_time','tag','home_team','guest_team')
  809. ->where($model_match.'.source',$source['source'])
  810. ->where($where)
  811. ->where('lg_id',$lg_id)
  812. ->where(function($query)use ($model_match,$search){
  813. $query->where($model_match.'.home_team','like','%'.$search.'%')
  814. ->orWhere(function($query)use ($model_match,$search) {
  815. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  816. });
  817. })
  818. ->get()->toArray();
  819. foreach($matchData as $kk => $vv) {
  820. $oddsData = lm($model_match, "Sports")
  821. ->leftjoin($model_odds, $model_odds . '.match_id', $model_match . '.match_id')
  822. ->select($model_odds . '.id','p_code', 'odds_code','odds_only', $model_odds . '.status', 'odds', 'condition', 'sort')
  823. ->where($model_match.'.source',$source['source'])
  824. ->where([$model_odds . '.match_id' => $vv['match_id']])
  825. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  826. ->groupBy($model_odds . '.id','p_code', 'odds_code','odds_only', $model_odds . '.status', 'odds', 'condition', 'sort')
  827. ->orderBy($model_odds . '.id','desc')
  828. ->get()
  829. ->toarray();
  830. $matchData[$kk]['oddsData'] = $oddsData;
  831. }
  832. return $matchData;
  833. }
  834. /**
  835. * 获取欧冠 早盘数据
  836. */
  837. public function getUEFAStMorningPlate($model,$type_code,$source,$lg_id,$search=''){
  838. $model_match = $model['model_match'];
  839. $model_odds = $model['model_odds'];
  840. $where = getState($type_code);
  841. $matchData = lm($model_match,'Sports')
  842. ->select('match_id','match_date','match_time','tag','home_team','guest_team')
  843. ->where($model_match.'.source',$source['source'])
  844. ->where($where)
  845. ->where('lg_id',$lg_id)
  846. ->where(function($query)use ($model_match,$search){
  847. $query->where($model_match.'.home_team','like','%'.$search.'%')
  848. ->orWhere(function($query)use ($model_match,$search) {
  849. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  850. });
  851. })
  852. ->get()->toArray();
  853. $data = $this->getOddsData->getOddsZQ($matchData,$model_odds,$source);
  854. return $data;
  855. }
  856. /**
  857. * 获取 欧冠 冠军盘口数据
  858. */
  859. public function getUEFAStChampion($model,$type_code,$source,$lg_id,$last_time=''){
  860. $model_odds = $model['model_odds'];
  861. $where = getState($type_code);
  862. $championData = lm($model_odds, 'Sports')
  863. ->select("id","lg_id","p_code","odds_code","team","odds","sort","status","odds_only")
  864. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  865. ->where($source)
  866. ->where('lg_id',$lg_id)
  867. ->where($where)
  868. ->groupBy("id","lg_id","p_code","odds_code","team","odds","sort","status","odds_only")
  869. ->get()->toArray();
  870. //按p_code分组
  871. $p_code=array();
  872. foreach($championData as $k=>$v){
  873. $p_code[$v['p_code']][]=$v;
  874. }
  875. $data = [];
  876. foreach ($p_code as $k1=>$v1){
  877. foreach ($v1 as $k2=>$v2){
  878. $data[$v2['p_code']]['last_time'] =$last_time;
  879. $data[$v2['p_code']][$v2['odds_code']][] = $v2;
  880. }
  881. }
  882. return $data;
  883. }
  884. }