GetmatchData.php 37 KB

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