GetmatchData.php 47 KB

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