GetmatchData.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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. if($time != date("Y-m-d")){//大于今日
  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. }else{//如果是今天,则获取今天未开始
  422. $country[$k]['league_count'][$kk]['count'] = lm($model_match, 'Sports')
  423. ->select("match_id")
  424. ->where($source)
  425. ->where('lg_id',$vv['lg_id'])
  426. ->where('is_morningplate','=',1)
  427. ->where('match_time','>',date("H:i:s", time()))
  428. ->where('match_date',$time)
  429. ->count("*");
  430. }
  431. }
  432. }
  433. }
  434. if($country[$k]['league_count'][$kk]['count'] == 0){
  435. unset($country[$k]['league_count'][$kk]);
  436. }
  437. }
  438. }
  439. }
  440. //统计 地区 联赛-赛事/盘口数量
  441. if(!empty($area)){
  442. foreach($area as $k => $v) {
  443. $area[$k]['league_count'] = lm($model_league, 'Sports')
  444. ->select('lg_id', 'name_chinese as league')
  445. ->where($source)
  446. ->where('area_id', $v['area_id'])
  447. ->where($model_league.'.name_chinese','like','%'.$search.'%')
  448. ->get()->toarray();
  449. }
  450. foreach ($area as $k => $v) {
  451. foreach($v['league_count'] as $kk => $vv ){
  452. if(!empty($model_match)){//非冠军玩法,获取联赛下赛事数量
  453. if(empty($time)){
  454. $area[$k]['league_count'][$kk]['count'] = lm($model_match, 'Sports')
  455. ->select("match_id")
  456. ->where($source)
  457. ->where('lg_id',$vv['lg_id'])
  458. ->where($where)
  459. ->count("*");
  460. }else {
  461. if ($time == 'other') {// 今日 一周后
  462. $area[$k]['league_count'][$kk]['count'] = lm($model_match, 'Sports')
  463. ->select("match_id")
  464. ->where($source)
  465. ->where('lg_id', $vv['lg_id'])
  466. ->where($where)
  467. ->where('match_date', '>', date("Y-m-d", strtotime("+1weeks", strtotime(date('Y-m-d', time())))))
  468. ->count("*");
  469. } else {//今日 大于今日
  470. if($time != date("Y-m-d")){//如果查询时间不是今天,则大于今天
  471. $area[$k]['league_count'][$kk]['count'] = lm($model_match, 'Sports')
  472. ->select("match_id")
  473. ->where($source)
  474. ->where('lg_id',$vv['lg_id'])
  475. ->where($where)
  476. ->where('match_date',$time)
  477. ->count("*");
  478. }else{//如果是今天,则获取今天未开始
  479. $area[$k]['league_count'][$kk]['count'] = lm($model_match, 'Sports')
  480. ->select("match_id")
  481. ->where($source)
  482. ->where('lg_id',$vv['lg_id'])
  483. ->where('is_morningplate','=',1)
  484. ->where('match_time','>',date("H:i:s", time()))
  485. ->where('match_date',$time)
  486. ->count("*");
  487. }
  488. }
  489. }
  490. }
  491. if($area[$k]['league_count'][$kk]['count'] == 0){
  492. unset($area[$k]['league_count'][$kk]);
  493. }
  494. }
  495. }
  496. }
  497. //处理空联赛国家
  498. $countryData = handleArr($country);
  499. $areaData = handleArr($area);
  500. //合并数组
  501. $data = array_merge($countryData,$areaData);
  502. return $data;
  503. }
  504. /**
  505. * 获取联赛下赛事 数量
  506. * 冠军盘口
  507. */
  508. public function getMatchStChampionNum($source,$country,$area,$model_league,$model_match='',$model_odds='',$where,$search,$time='',$type=0){
  509. $leagueData = lm($model_league,'Sports')
  510. ->select('id','lg_id','name_chinese as league','country_id','area_id')
  511. ->get()
  512. ->where($source)
  513. ->toArray();
  514. $oddsData = lm($model_odds,'Sports')
  515. ->select('id','lg_id','p_code')
  516. ->distinct("p_code")//去重
  517. ->where($where)
  518. ->where($source)
  519. ->get()
  520. ->toArray();
  521. //按国家
  522. $countryData = [];
  523. if(!empty($country) and !empty($leagueData) and($oddsData)){
  524. foreach ($country as $k=>$v){
  525. if($v['country_id']){
  526. $countryData[$k] = $v;
  527. foreach ($leagueData as $kk=>$vv){
  528. if($v['country_id'] == $vv['country_id']){
  529. $countryData[$k]['league_count'][] = $vv;
  530. }
  531. }
  532. }
  533. }
  534. }
  535. //去除无联赛 国家
  536. foreach ($countryData as $k=>$v){
  537. if(count($v) == 2){
  538. unset($countryData[$k]);
  539. }
  540. }
  541. //按地区
  542. $areaData = [];
  543. if(!empty($area) and !empty($leagueData) and($oddsData)){
  544. foreach ($area as $k=>$v){
  545. if($v['area_id']){
  546. $areaData[$k] = $v;
  547. foreach ($leagueData as $kk=>$vv){
  548. if($v['area_id'] == $vv['area_id']){
  549. $areaData[$k]['league_count'][] = $vv;
  550. }
  551. }
  552. }
  553. }
  554. }
  555. //去除无联赛 地区
  556. foreach ($areaData as $k=>$v){
  557. if(count($v) == 2){
  558. unset($areaData[$k]);
  559. }
  560. }
  561. $countryData = array_gj_tt($countryData,$oddsData);
  562. $areaData = array_gj_tt($areaData,$oddsData);
  563. $data = array_merge($countryData,$areaData);
  564. return $data;
  565. }
  566. /**
  567. * 获取联赛下 赛事赔率
  568. */
  569. public function getMatchOdds($source,$model_league,$model_match,$model_odds,$where,$search,$lg_id,$time=''){
  570. $leagueData = lm($model_league,"Sports")
  571. ->select('lg_id','name_chinese as leagueName')
  572. ->where($source)
  573. ->where('lg_id',$lg_id)
  574. ->first();
  575. if(!empty($time)){//早盘 按时间查询
  576. if($time == 'other'){//早盘 一周后
  577. $matchData = lm($model_match,"Sports")
  578. ->select($model_match.'.match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team')
  579. ->where($source)
  580. ->where($model_match.'.lg_id',$lg_id)
  581. ->where($where)
  582. ->where('match_date','>',date("Y-m-d",strtotime("+1weeks",strtotime(date('Y-m-d',time())))))
  583. ->where(function($query)use ($model_match,$search){
  584. $query->where($model_match.'.home_team','like','%'.$search.'%')
  585. ->orWhere(function($query)use ($model_match,$search) {
  586. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  587. });
  588. })
  589. ->get()
  590. ->toarray();
  591. }else{
  592. if($time == date('Y-m-d')){//早盘今天
  593. $matchData = lm($model_match,"Sports")
  594. ->select($model_match.'.match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team')
  595. ->where($source)
  596. ->where($model_match.'.lg_id',$lg_id)
  597. ->where('is_morningplate','=',1)
  598. ->where('match_date',$time)
  599. ->where('match_time','>',date("H:i:s", time()))
  600. ->where(function($query)use ($model_match,$search){
  601. $query->where($model_match.'.home_team','like','%'.$search.'%')
  602. ->orWhere(function($query)use ($model_match,$search) {
  603. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  604. });
  605. })
  606. ->get()
  607. ->toarray();
  608. }else{//早盘 今天以后
  609. $matchData = lm($model_match,"Sports")
  610. ->select($model_match.'.match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team')
  611. ->where($source)
  612. ->where($model_match.'.lg_id',$lg_id)
  613. ->where($where)
  614. ->where('match_date',$time)
  615. ->where(function($query)use ($model_match,$search){
  616. $query->where($model_match.'.home_team','like','%'.$search.'%')
  617. ->orWhere(function($query)use ($model_match,$search) {
  618. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  619. });
  620. })
  621. ->get()
  622. ->toarray();
  623. }
  624. }
  625. }else{
  626. $matchData = lm($model_match,"Sports")
  627. ->select($model_match.'.match_id',$model_match.'.tag','match_date',$model_match.'.match_time',$model_match.'.home_team',$model_match.'.guest_team')
  628. ->where($source)
  629. ->where($model_match.'.lg_id',$lg_id)
  630. ->where($where)
  631. ->where(function($query)use ($model_match,$search){
  632. $query->where($model_match.'.home_team','like','%'.$search.'%')
  633. ->orWhere(function($query)use ($model_match,$search) {
  634. $query->where($model_match . '.guest_team', 'like', '%' . $search . '%');
  635. });
  636. })
  637. ->get()
  638. ->toarray();
  639. }
  640. foreach($matchData as $kk => $vv) {
  641. $oddsData = lm($model_match, "Sports")
  642. ->leftjoin($model_odds, $model_odds . '.match_id', $model_match . '.match_id')
  643. ->select($model_odds . '.id', 'odds_only','p_code', 'odds_code', $model_odds . '.status', 'odds', 'condition', 'sort')
  644. ->where($model_match.'.source',$source['source'])
  645. ->where([$model_odds . '.match_id' => $vv['match_id'], $model_odds . '.type' => 0])
  646. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  647. ->where(function ($query) use ($model_odds) {
  648. $query->where($model_odds . '.odds_code', 'concede_home')
  649. ->orWhere(function ($query) use ($model_odds) {
  650. $query->where($model_odds . '.odds_code', 'concede_guest');
  651. })
  652. ->orWhere(function ($query) use ($model_odds) {
  653. $query->where($model_odds . '.odds_code', 'size_home');
  654. })
  655. ->orWhere(function ($query) use ($model_odds) {
  656. $query->where($model_odds . '.odds_code', 'size_guest');
  657. });
  658. })
  659. ->get()
  660. ->toarray();
  661. //根据 排序 获取 最新让球/大小玩法赔率
  662. $sortData = array_column($oddsData,'sort');
  663. array_multisort($sortData,SORT_DESC,$oddsData);
  664. $zu = [];
  665. foreach ($oddsData as $key1 =>$item1){
  666. $zu[$item1['p_code']][] = $item1;
  667. }
  668. $c_s = array_slice($zu['concede_size'],0,4);//让球/大小 前四条 放入
  669. $matchData[$kk]['oddsData'] = $c_s;
  670. }
  671. $leagueData->matchNum = count($matchData);
  672. $leagueData->matchData = $matchData;
  673. return $leagueData;
  674. }
  675. /**
  676. * @param $source 数据源
  677. * @param $where 该状态 查询条件
  678. * @return array
  679. * @throws \Exception
  680. * 获取所有即将开赛 数据
  681. */
  682. public function getAllSoon($source,$where){
  683. $game = lm('GameType', 'Sports')->select('id','game_name','game_code','game_ico_url')->where('status',1)->get()->toArray();
  684. $data=[];
  685. foreach ($game as $key=>$item){
  686. if($item['game_code'] !='gj'){
  687. $model = getModels($item['game_code']);
  688. $matchData = lm($model['model_match'], 'Sports')
  689. ->select('match_id','home_team','guest_team','match_time')
  690. ->where($source)
  691. ->where($where)
  692. ->get()->toArray();
  693. foreach ($matchData as $key1 => $item1){
  694. //获取等待时间
  695. $wait_time = ceil(((strtotime($item1['match_time']))-time())/60);
  696. $matchData[$key1]['wait_time'] = $wait_time;
  697. }
  698. $matchNum = count($matchData);
  699. if($matchNum > 0){
  700. $data[$item['game_code']]['matchData'] = $matchData;
  701. $data[$item['game_code']]['matchNum'] = $matchNum;
  702. $data[$item['game_code']]['gameName'] = $item['game_name'];
  703. $data[$item['game_code']]['gameCode'] = $item['game_code'];
  704. }
  705. }
  706. }
  707. return $data;
  708. }
  709. /**
  710. * 获取欧冠 各状态赛事数据
  711. */
  712. public function getUEFAChampions($source){
  713. $model = getModels('gj',1);
  714. $model_league = $model['model_league'];
  715. $leagueData = lm($model_league,'Sports')
  716. ->select('name_chinese','lg_id','last_time')
  717. ->where($model_league.'.source',$source['source'])
  718. ->where($model_league.'.name_chinese','欧洲冠军杯')
  719. ->first();
  720. //获取滚球数据
  721. $StRollBall = $this->getUEFAStRollBall($model,'StRollBall',$source,$leagueData->lg_id);
  722. //获取今日数据
  723. $StToday = $this->getUEFAStToday($model,'StToday',$source,$leagueData->lg_id);
  724. //获取早盘数据
  725. $StMorningPlate = $this->getUEFAStMorningPlate($model,'StMorningPlate',$source,$leagueData->lg_id);
  726. //获取冠军盘口数据
  727. $StChampion = $this->getUEFAStChampion($model,'StChampion',$source,$leagueData->lg_id,$leagueData->last_time);
  728. $data = [
  729. 'leagueName'=> $leagueData->name_chinese,
  730. 'StRollBall'=>[//滚球数据
  731. 'typeName'=>'滚球',
  732. 'matchNum'=>count($StRollBall),
  733. 'matchData'=>$StRollBall
  734. ],
  735. 'StToday'=>[//今日数据
  736. 'typeName'=>'今日',
  737. 'matchNum'=>count($StToday),
  738. 'matchData'=>$StToday
  739. ],
  740. 'StMorningPlate'=>[//早盘数据
  741. 'typeName'=>'早盘',
  742. 'matchNum'=>count($StMorningPlate),
  743. 'matchData'=>$StMorningPlate
  744. ],
  745. 'StChampion'=>[//冠军盘口
  746. 'typeName'=>'冠军盘口',
  747. 'matchNum'=>count($StChampion),
  748. 'matchData'=>$StChampion
  749. ]
  750. ];
  751. return $data;
  752. }
  753. /**
  754. * 获取欧冠 滚球数据
  755. */
  756. public function getUEFAStRollBall($model,$type_code,$source,$lg_id){
  757. $model_match = $model['model_match'];
  758. $model_odds = $model['model_odds'];
  759. $model_result = $model['model_result'];
  760. $where = getState($type_code,$model_match);
  761. $matchData = lm($model_match,"Sports")
  762. ->leftjoin($model_result,$model_result.'.match_id',$model_match.'.match_id')
  763. ->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')
  764. ->where($model_match.'.source',$source['source'])
  765. ->where($model_match.'.lg_id',$lg_id)
  766. ->where($where)
  767. ->get()
  768. ->toarray();
  769. foreach($matchData as $kk => $vv){
  770. $oddsData= lm($model_match,"Sports")
  771. ->leftjoin($model_odds,$model_odds.'.match_id',$model_match.'.match_id')
  772. ->select($model_odds.'.id','p_code','odds_code',$model_odds.'.status','odds','condition','sort')
  773. ->where($model_match.'.source',$source['source'])
  774. ->where([$model_odds.'.match_id' => $vv['match_id'],$model_odds.'.type'=>0])
  775. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  776. ->where(function($query)use ($model_odds){
  777. $query->where($model_odds.'.odds_code','concede_home')
  778. ->orWhere(function($query)use ($model_odds){
  779. $query->where($model_odds.'.odds_code','concede_guest');
  780. })
  781. ->orWhere(function($query)use ($model_odds){
  782. $query->where($model_odds.'.odds_code','size_home');
  783. })
  784. ->orWhere(function($query)use ($model_odds){
  785. $query->where($model_odds.'.odds_code','size_guest');
  786. })
  787. ->orWhere(function($query)use ($model_odds){
  788. $query->where($model_odds.'.odds_code','capot_home');
  789. })
  790. ->orWhere(function($query)use ($model_odds){
  791. $query->where($model_odds.'.odds_code','capot_dogfall');
  792. })
  793. ->orWhere(function($query)use ($model_odds){
  794. $query->where($model_odds.'.odds_code','capot_guest');
  795. });
  796. })
  797. ->get()
  798. ->toarray();
  799. //根据 排序 获取 最新让球/大小玩法赔率
  800. $sortData = array_column($oddsData,'sort');
  801. array_multisort($sortData,SORT_DESC,$oddsData);
  802. $zu = [];
  803. foreach ($oddsData as $key1 =>$item1){
  804. $zu[$item1['p_code']][] = $item1;
  805. }
  806. $c_s = array_slice($zu['concede_size'],0,4);//让球/大小 前四条 放入
  807. $capot = $zu['capot'];//独赢
  808. $matchData[$kk]['oddsData'] = [$c_s,$capot];
  809. }
  810. return $matchData;
  811. }
  812. /**
  813. * 获取欧冠 今日数据
  814. */
  815. public function getUEFAStToday($model,$type_code,$source,$lg_id){
  816. $model_match = $model['model_match'];
  817. $model_odds = $model['model_odds'];
  818. $where = getState($type_code);
  819. $matchData = lm($model_match,'Sports')
  820. ->select('match_id','match_date','match_time','tag','home_team','guest_team')
  821. ->where($model_match.'.source',$source['source'])
  822. ->where($where)
  823. ->where('lg_id',$lg_id)
  824. ->get()->toArray();
  825. foreach($matchData as $kk => $vv) {
  826. $oddsData = lm($model_match, "Sports")
  827. ->leftjoin($model_odds, $model_odds . '.match_id', $model_match . '.match_id')
  828. ->select($model_odds . '.id','p_code', 'odds_code','odds_only', $model_odds . '.status', 'odds', 'condition', 'sort')
  829. ->where($model_match.'.source',$source['source'])
  830. ->where([$model_odds . '.match_id' => $vv['match_id']])
  831. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  832. ->groupBy($model_odds . '.id','p_code', 'odds_code','odds_only', $model_odds . '.status', 'odds', 'condition', 'sort')
  833. ->orderBy($model_odds . '.id','desc')
  834. ->get()
  835. ->toarray();
  836. $matchData[$kk]['oddsData'] = $oddsData;
  837. }
  838. return $matchData;
  839. }
  840. /**
  841. * 获取欧冠 早盘数据
  842. */
  843. public function getUEFAStMorningPlate($model,$type_code,$source,$lg_id){
  844. $model_match = $model['model_match'];
  845. $model_odds = $model['model_odds'];
  846. $where = getState($type_code);
  847. $matchData = lm($model_match,'Sports')
  848. ->select('match_id','match_date','match_time','tag','home_team','guest_team')
  849. ->where($model_match.'.source',$source['source'])
  850. ->where($where)
  851. ->where('lg_id',$lg_id)
  852. ->get()->toArray();
  853. $data = $this->getOddsData->getOddsZQ($matchData,$model_odds,$source);
  854. return $data;
  855. }
  856. /**
  857. * 获取 欧冠 冠军盘口数据
  858. */
  859. public function getUEFAStChampion($model,$type_code,$source,$lg_id,$last_time=''){
  860. $model_odds = $model['model_odds'];
  861. $where = getState($type_code);
  862. $championData = lm($model_odds, 'Sports')
  863. ->select("id","lg_id","p_code","odds_code","team","odds","sort","status","odds_only")
  864. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  865. ->where($source)
  866. ->where('lg_id',$lg_id)
  867. ->where($where)
  868. ->groupBy("id","lg_id","p_code","odds_code","team","odds","sort","status","odds_only")
  869. ->get()->toArray();
  870. //按p_code分组
  871. $p_code=array();
  872. foreach($championData as $k=>$v){
  873. $p_code[$v['p_code']][]=$v;
  874. }
  875. $data = [];
  876. foreach ($p_code as $k1=>$v1){
  877. foreach ($v1 as $k2=>$v2){
  878. $data[$v2['p_code']]['last_time'] =$last_time;
  879. $data[$v2['p_code']][$v2['odds_code']][] = $v2;
  880. }
  881. }
  882. return $data;
  883. }
  884. }