GetmatchData.php 37 KB

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