GetmatchData.php 46 KB

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