GetmatchData.php 37 KB

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