GetmatchData.php 42 KB

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