GetmatchData.php 38 KB

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