GetmatchData.php 48 KB

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