GetmatchData.php 42 KB

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