MatchList.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. <?php
  2. namespace App\Sports\Controller;
  3. use BaseController\Controller;
  4. use Biz\Match\GetmatchData;
  5. use Biz\Match\GetOddsData;
  6. /**
  7. * Class RollingData
  8. * @package App\Sports\Controller
  9. * User: tank
  10. * Date: 2019/3/22
  11. */
  12. class MatchList extends Controller {
  13. private $getTypeData;
  14. public function init() {
  15. $this->getTypeData = new GetmatchData();
  16. $this->getOddsData = new GetOddsData();
  17. $this->commonFunction = C()->get('commonFunction');
  18. }
  19. /*
  20. *首页条件筛选(足球参赛表)
  21. */
  22. private function zq_participate (){
  23. $data['type'] = '足球';
  24. $data['game_code'] = 'zq';
  25. $where_search = [];
  26. //查询今日赛事
  27. $where = $this->commonFunction->getState('StToday');
  28. $Orwhere = [];
  29. $data['info'][0]['name'] = '今日赛事';
  30. $data['info'][0]['code'] = 'today';
  31. $data['info'][0]['count'] = $this->getMatchCount($where,$Orwhere,$where_search,$data['game_code']);
  32. //查询明日赛事
  33. $where=[['st_zq_competition.match_date','tomorrow']];
  34. $Orwhere=[];
  35. $data['info'][1]['name'] = '明日赛事';
  36. $data['info'][1]['code'] = 'tomorrow';
  37. $data['info'][1]['count'] = $this->getMatchCount($where,$Orwhere,$where_search,$data['game_code']);
  38. //受欢迎的欧洲赛事
  39. $where =[['area.name_en','Europe'],['st_zq_league.hot',0]];
  40. $Orwhere=[];
  41. $data['info'][2]['name'] = '受欢迎的欧洲赛事';
  42. $data['info'][2]['code'] = 'popular';
  43. $data['info'][2]['count'] = $this->getMatchCount($where,$Orwhere,$where_search,$data['game_code']);
  44. //查询亚洲,澳洲赛事
  45. $where =[];
  46. $Orwhere = function ($query) {
  47. $query->Orwhere('area.name_en','=','Asia')
  48. ->Orwhere('country.name_en','=','Australia');
  49. };
  50. $data['info'][3]['name'] = '亚洲/澳洲赛事';
  51. $data['info'][3]['code'] = 'asia_aus';
  52. $data['info'][3]['count'] = $this->getMatchCount($where,$Orwhere,$where_search,$data['game_code']);
  53. //查询北美,南美赛事
  54. $where = [];
  55. $Orwhere = function ($query) {
  56. $query->Orwhere('area.name_en','=', "South America")
  57. ->Orwhere('area.name_en','=', "North America");
  58. };
  59. $data['info'][4]['name'] = '北美/南美赛事';
  60. $data['info'][4]['code'] = 'south_north';
  61. $data['info'][4]['count'] = $this->getMatchCount($where,$Orwhere,$where_search,$data['game_code']);
  62. //国际赛事
  63. $where = [['area.name_en',"world"]];
  64. $Orwhere = [];
  65. $data['info'][5]['name'] = '国际赛事';
  66. $data['info'][5]['code'] = 'intl';
  67. $data['info'][5]['count'] = $this->getMatchCount($where,$Orwhere,$where_search,$data['game_code']);
  68. return $data;
  69. }
  70. /*
  71. *首页条件筛选(其他球参赛表)
  72. */
  73. private function qt_participate ($typeGame){
  74. //===查询该球类是否存在或启用===
  75. $getModels = $this->commonFunction->getModels($typeGame);
  76. $data['game_code'] = $typeGame;
  77. $data['type'] = lm('GameType','Sports')->where('game_code',$typeGame)->value('game_name');
  78. $st_competition = $getModels['model_match'];
  79. // 所有赛事
  80. $where_search = [];
  81. $where = [];
  82. $Orwhere = [];
  83. $data['info'][0]['name'] = '所有赛事';
  84. $data['info'][0]['code'] = 'all';
  85. $data['info'][0]['count'] = $this->getMatchCount($where,$Orwhere,$where_search,$data['game_code']);
  86. //今日赛事
  87. $where = $this->commonFunction->getState('StToday',$st_competition);
  88. $Orwhere = [];
  89. $data['info'][1]['name'] = '今日赛事';
  90. $data['info'][1]['code'] = 'today';
  91. $data['info'][1]['count']=$this->getMatchCount($where,$Orwhere,$where_search,$data['game_code']);
  92. return $data;
  93. }
  94. /*
  95. * 参赛表数据接口
  96. */
  97. public function participate(){
  98. $type = $_REQUEST['typeGame'];
  99. if($type != 'zq'|| empty($type)){
  100. $data = $this->qt_participate($type);
  101. }else{
  102. $data = $this->zq_participate();
  103. }
  104. Render($data, '1', lang('Tips','Sports')->get('success'));
  105. }
  106. /*
  107. * 参赛列表详情
  108. */
  109. public function participate_details()
  110. {
  111. $game_code = $_REQUEST['game_code'];//游戏代码
  112. $code = $_REQUEST['code'];//参赛代码
  113. $search = $_REQUEST['search'];//参赛代码
  114. $lg_id = $_REQUEST['lg_id'];//参赛代码
  115. //===查询该球类是否存在或启用===
  116. $getModels = $this->commonFunction->getModels($game_code);
  117. $st_league = $getModels['model_league'];
  118. $st_competition = $getModels['model_match'];
  119. $st_odds = $getModels['model_odds'];
  120. $where_search = function($query)use ($st_competition,$search){
  121. $query->where($st_competition.'.home_team','like','%'.$search.'%')
  122. ->orWhere(function($query)use ($st_competition,$search) {
  123. $query->where($st_competition . '.guest_team', 'like', '%' . $search . '%');
  124. });
  125. };
  126. $Orwhere = [];
  127. switch ($code)
  128. {
  129. case $code == 'today':
  130. $where =$this->commonFunction->getState('StToday');
  131. //查询今日赛事
  132. $data = $this->getMatchInfo($where,$Orwhere,$where_search,$game_code);
  133. break;
  134. case $code == 'popular':
  135. $where =[['area.name_en','Europe'],[$st_league.'.hot',0]];
  136. //欧洲最受欢迎的赛事
  137. $data = $this->getMatchInfo($where,$Orwhere,$where_search,$game_code);
  138. break;
  139. case $code == 'asia_aus':
  140. $where = [];
  141. $Orwhere = function ($query) {
  142. $query->Orwhere('area.name_en','=','Asia')
  143. ->Orwhere('country.name_en','=','Australia');
  144. };
  145. //亚洲/澳洲赛事
  146. $data = $this->getMatchInfo($where,$Orwhere,$where_search,$game_code);
  147. break;
  148. case $code == 'south_north':
  149. $where = [];
  150. $Orwhere = function ($query) {
  151. $query->Orwhere('area.name_en','=', "South America")
  152. ->Orwhere('area.name_en','=', "North America");
  153. };
  154. //南美洲/北美洲赛事
  155. $data = $this->getMatchInfo($where,$Orwhere,$where_search,$game_code);
  156. break;
  157. case $code == 'tomorrow':
  158. $where = [[$st_competition.'.match_date','tomorrow']];
  159. //明日赛事
  160. $data = $this->getMatchInfo($where,$Orwhere,$where_search,$game_code);
  161. break;
  162. case $code == 'intl':
  163. $where = [['area.name_en', "world"]];
  164. //国际赛事
  165. $data = $this->getMatchInfo($where,$Orwhere,$where_search,$game_code);
  166. break;
  167. case $code == 'all':
  168. $where = [];
  169. //所有赛事
  170. $data = $this->getMatchInfo($where,$Orwhere,$where_search,$game_code);
  171. break;
  172. case $code == 'qt':
  173. $where = [[$st_league.'.lg_id',$lg_id]];
  174. //指定联赛下的赛事
  175. $data = $this->getMatchInfo($where,$Orwhere,$where_search,$game_code);
  176. break;
  177. }
  178. //根据赛事ID获取赔率数据
  179. $matchData = [];
  180. foreach($data as $k => $v){
  181. foreach($v['match_info'] as $kk => $vv){
  182. $matchData[] =$vv;
  183. }
  184. }
  185. if($game_code == 'zq'){
  186. $matchData = $this->getOddsData->getOddsZQ($matchData,$st_odds,$this->source);
  187. }
  188. if($game_code == 'lq'){
  189. $matchData = $this->getOddsData->getOddsLQ($matchData,$st_odds,$this->source);
  190. }
  191. if($game_code == 'bq'){
  192. $matchData = $this->getOddsData->getOddsBQ($matchData,$st_odds,$this->source);
  193. }
  194. if($game_code == 'wq'){
  195. $matchData = $this->getOddsData->getOddsWQ($matchData,$st_odds,$this->source);
  196. }
  197. $lgData = [];
  198. foreach($data as $k => $v){
  199. $v['match_info'] = [];
  200. foreach($matchData as $kk=>$vv){
  201. if($v['lg_id'] == $vv['lg_id']){
  202. $v['match_info'][] = $vv;
  203. }
  204. }
  205. $data[$k] = $v;
  206. }
  207. // dd($matchData);
  208. $data = array_values($data);
  209. Render($data, '1', lang('Tips','Sports')->get('success'));
  210. }
  211. /*
  212. *首页条件筛选(所有赛事)
  213. */
  214. public function matchData (){
  215. $gameType = $_REQUEST['gameType'];
  216. $search = $_REQUEST['search'];
  217. $date = $_REQUEST['datetime'];
  218. //=获取球了MODEL
  219. $gameType = empty($gameType) ? 'zq':$gameType;
  220. $getModels = $this->commonFunction->getModels($gameType);
  221. $st_league = $getModels['model_league'];
  222. $st_competition = $getModels['model_match'];
  223. //所属球类
  224. $where = [];
  225. if(!empty($date)){
  226. if($date == 'morning'){
  227. $where=[[$st_competition.'.is_morningplate',1],
  228. [$st_competition.'.match_date','>',date("Y-m-d",strtotime("+6 day"))]];
  229. }else if($date == 'today'){
  230. $where = $this->commonFunction->getState('StToday');
  231. }else {
  232. $where[$st_competition.'.match_date'] = $date;
  233. }
  234. }
  235. //查询球类名称
  236. $data['type'] = lm('GameType', 'Sports')
  237. ->where('game_code',$gameType)
  238. ->value('game_name');
  239. //国家联赛数据
  240. $select=['st_country.country_id as region_id','st_country.name_chinese as region',$st_league.'.lg_id',$st_league.'.name_chinese as league'];
  241. $country = $this->getRegionInfo($where,$search,$select,'st_country','country_id','country_id',$gameType);
  242. //洲下所有联赛
  243. $select=['st_area.id as region_id','st_area.title as region',$st_league.'.lg_id',$st_league.'.name_chinese as league'];
  244. $area = $this->getRegionInfo($where,$search,$select,'st_area','id','area_id',$gameType);
  245. $data['info'] = array_merge($country,$area);
  246. Render($data, '1', lang('Tips','Sports')->get('success'));
  247. }
  248. /**
  249. * 根据地区获取信息
  250. * @param [array] $where 条件
  251. * @param [array] $search 搜索条件
  252. * @param [string] $regionModel 地区model
  253. * @param [int] $regionID 地区model
  254. * @param [type] $game_code 球类代码
  255. * @return [array] $data
  256. */
  257. public function getRegionInfo($where=[],$search,$select,$regionModel,$regionID,$a_c_id,$game_code ='zq'){
  258. $getModels = $this->commonFunction->getModels($game_code);
  259. $st_league = $getModels['model_league'];
  260. $st_competition = $getModels['model_match'];
  261. try{
  262. //地区联赛赛事数据
  263. $regionInfo = lm($st_league,"Sports")
  264. ->join($st_competition,$st_competition.'.lg_id',$st_league.'.lg_id')
  265. ->join($regionModel,$regionModel.'.'.$regionID,$st_league.'.'.$a_c_id)
  266. ->select($select)
  267. ->where($st_league.'.name_chinese','like','%'.$search.'%')
  268. ->where($where)
  269. ->where([
  270. [$st_competition.'.home_team','<>',null],
  271. [$st_competition.'.guest_team','<>',null],
  272. [$st_league.'.name_chinese','!=',''],
  273. [$st_competition.'.status','<',2],
  274. [$st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)]
  275. ])
  276. ->get()
  277. ->toArray();
  278. //地区分割
  279. $info = [];
  280. foreach($regionInfo as $k => $v){
  281. $info[$k]['region'] = $v['region'];
  282. $info[$k]['region_id'] = $v['region_id'];
  283. }
  284. //地区去重
  285. $info = array_unique($info, SORT_REGULAR);
  286. //根据地区分类
  287. foreach ($info as $key => $vlue) {
  288. foreach ($regionInfo as $k => $v) {
  289. if ($vlue['region'] == $v['region']) {
  290. $info[$key]['league_count'][$k]['league'] = $v['league'];
  291. $info[$key]['league_count'][$k]['lg_id'] = $v['lg_id'];
  292. }
  293. }
  294. }
  295. //联赛去重
  296. foreach ($info as $key => $vlue) {
  297. $info[$key]['league_count'] = array_unique($vlue['league_count'], SORT_REGULAR);
  298. //统计联赛下赛事
  299. foreach ($vlue['league_count'] as $k => $v) {
  300. foreach ($regionInfo as $kk => $vv) {
  301. if ($v['league'] == $vv['league']) {
  302. if(!empty($info[$key]['league_count'][$kk]['league'])){
  303. $info[$key]['league_count'][$kk]['count'] += 1;
  304. }
  305. }
  306. }
  307. }
  308. }
  309. return $info;
  310. } catch (\Exception $e) {
  311. echo $e->getMessage();
  312. }
  313. }
  314. /**
  315. * 联赛下 赛事及默认赔率数据
  316. */
  317. public function matchDetails(){
  318. $data = $_REQUEST;
  319. try {
  320. if(empty($data['game_code']) || empty($data['leagueID']) || empty($data['oddsType'])){
  321. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  322. }
  323. //根据时间获取联赛下赛事数据
  324. $whereDate = [];
  325. if(!empty($data['matchDate']) ){
  326. if($data['matchDate']== "morning"){
  327. $whereDate = [['is_morningplate',1],
  328. ['match_date','>',date("Y-m-d",strtotime("+6 day"))]];
  329. }else{
  330. $whereDate = ['match_date'=>$data['matchDate']];
  331. }
  332. }
  333. $models = $this->commonFunction->getModels($data['game_code'],0);
  334. $model_match = $models['model_match'];
  335. $model_odds = $models['model_odds'];
  336. $model_league = $models['model_league'];
  337. $model_result = $models['model_result'];
  338. $oddsTypeWhere = $this->getOddsTypeWhere($model_odds,$data['oddsType']);
  339. $source = $this->source;//数据源
  340. $data = $this->getOddsData->getOddsData($data,$whereDate,$source,$oddsTypeWhere);
  341. Render($data, '1', lang('Tips','Sports')->get('success'));
  342. } catch (\Exception $e) {
  343. echo $e->getMessage();
  344. }
  345. }
  346. //获取各状态下有哪些球类
  347. public function getGame(){
  348. $type_code = $_REQUEST['type_code'];
  349. try {
  350. if(empty($type_code)){
  351. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  352. }
  353. //==获取当前可用球类==
  354. $game = lm('GameType', 'Sports')->where('status',1)->select('game_code','game_name','game_ico_url')->get()->toArray();
  355. $gameData = [];
  356. foreach ($game as $key=>$type){
  357. $matchModel = $this->commonFunction->getModels($type['game_code']);
  358. //获取 不同状态的查询条件
  359. $where = $this->commonFunction->getState($type_code,$matchModel['model_match']);
  360. if($type_code != 'StChampion'){
  361. $matchNum = lm($matchModel['model_league'],"Sports")
  362. ->join($matchModel['model_match'],$matchModel['model_match'].'.lg_id',$matchModel['model_league'].'.lg_id')
  363. ->where($matchModel['model_match'].'.source',$this->source['source'])
  364. ->where($where)
  365. ->where([[$matchModel['model_league'].'.name_chinese','!=','']])
  366. ->count("*");
  367. //获取满足状态的球类/去除冠军类
  368. if($matchNum != 0 and $type['game_code']!='gj'){
  369. $gameData[] = $type;
  370. }
  371. }else{
  372. $matchNum = lm($matchModel['model_odds'], 'Sports')
  373. ->where($this->source)
  374. ->where($where)
  375. ->count('*');
  376. //获取满足状态的球类/去除冠军类
  377. if($matchNum != 0 and $type['game_code']!='gj'){
  378. $gameData[] = $type;
  379. }
  380. }
  381. }
  382. Render($gameData, '1', lang('Tips','Sports')->get('success'));
  383. } catch (\Exception $e) {
  384. echo $e->getMessage();
  385. }
  386. }
  387. /**
  388. * @throws \Exception
  389. * 获取不同状态下 各球类赛事数据
  390. * game_code 球类代码
  391. * type_code 类型代码
  392. */
  393. public function matchState(){
  394. $source = $this->source;//数据源 条件
  395. $ret = $_REQUEST;
  396. try {
  397. if(empty($ret['game_code']) || empty($ret['type_code'])){
  398. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  399. }
  400. $data = $this->getTypeData->typeData($ret,$source);
  401. Render($data, '1', lang('Tips','Sports')->get('success'));
  402. } catch (\Exception $e) {
  403. echo $e->getMessage();
  404. }
  405. }
  406. /**
  407. * 根据赛事状态获取赔率查询条件
  408. */
  409. public function getOddsTypeWhere($model_odds,$oddsType){
  410. //获取赛事不同状态下的赔率
  411. if(!empty($oddsType)){
  412. switch ($oddsType)
  413. {
  414. case 'StRollBall'://滚球
  415. $oddsTypeWhere = [[$model_odds.'.is_rollball','=',1]];
  416. break;
  417. case 'StSoon'://即将
  418. $oddsTypeWhere = [[$model_odds.'.is_morningplate','=',1]];
  419. break;
  420. case 'StToday'://今日
  421. $oddsTypeWhere = [[$model_odds.'.is_today','=',1]];
  422. break;
  423. case 'StMorningPlate'://早盘
  424. $oddsTypeWhere = [[$model_odds.'.is_morningplate','=',1]];
  425. break;
  426. case 'StStringScene'://串场
  427. $oddsTypeWhere = [[$model_odds.'.is_stringscene','=',1]];
  428. break;
  429. case 'home'://首页
  430. $oddsTypeWhere = [[$model_odds.'.is_stringscene','=',1]];
  431. break;
  432. default:
  433. $oddsTypeWhere = [];
  434. }
  435. }
  436. return $oddsTypeWhere;
  437. }
  438. /**
  439. * 获取赛事所有玩法赔率数据
  440. */
  441. public function matchOdds(){
  442. $game_code = $_REQUEST['game_code'];
  443. $matchID = $_REQUEST['matchID'];
  444. $status = $_REQUEST['status'];
  445. $oddsType = $_REQUEST['oddsType'];
  446. try {
  447. if(empty($game_code) || empty($matchID) || empty($oddsType)){
  448. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  449. }
  450. //根据球类代码 获取相关model
  451. $models = $this->commonFunction->getModels($game_code,0);
  452. $model_match = $models['model_match'];
  453. $model_odds = $models['model_odds'];
  454. $model_league = $models['model_league'];
  455. $model_result = $models['model_result'];
  456. //取消按状态查询赔率数据
  457. //$oddsTypeWhere = $this->getOddsTypeWhere($model_odds,$oddsType);
  458. $oddsData = lm($model_odds, 'Sports')
  459. ->select('sort','p_code','id','odds_only','odds_code',$model_odds.'.status','odds','condition','sort','source','utime')
  460. ->where($this->source)
  461. ->where([$model_odds.'.match_id'=>$matchID,$model_odds.'.type'=>0])//,$model_odds.'.expire_time'=>$utime
  462. ->orderBy('sort', 'desc')
  463. ->orderBy('p_code','desc')
  464. ->orderBy('odds_code','desc')
  465. ->get()->toArray();
  466. $matchData = lm($model_match, 'Sports')
  467. ->join($model_league,$model_league.'.lg_id',$model_match.'.lg_id')
  468. ->select($model_match.'.lg_id','name_chinese','match_id','home_team','guest_team','match_date','match_time',$model_match.'.status')
  469. ->where($model_match.'.source',$this->source['source'])
  470. ->where(['match_id'=>$matchID])
  471. ->first();
  472. if(empty($matchData)) Render(null, '1', lang('Tips','Sports')->get('success'));
  473. $matchData->ptime = '';//赛事进行时间
  474. $matchData->match_score = '';//赛事比分
  475. //如果该赛事正在进行,则获取进行时间
  476. if($matchData->status ==1){
  477. $result = lm($model_result, 'Sports')->select('match_id','match_time','match_score')
  478. ->where($this->source)
  479. ->where(['match_id'=>$matchData->match_id])
  480. ->first();
  481. $matchData->ptime = $result->match_time;
  482. $matchData->match_score = $result->match_score;
  483. }
  484. //赛事已结束
  485. if($matchData->status ==2){
  486. $oddsData = [];
  487. }
  488. //查询当前联赛下的赛事
  489. $league = [];
  490. if($oddsType != 'StRollBall'){
  491. $where = [
  492. [$model_match.'.home_team','<>',null],
  493. [$model_match.'.guest_team','<>',null],
  494. [$model_match.'.status','<',2],
  495. [$model_match.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)],
  496. [$model_match.'.lg_id',$matchData->lg_id],
  497. [$model_league.'.name_chinese','!=',''],
  498. ];
  499. if($oddsType !='home'){
  500. $getState = $this->commonFunction->getState($oddsType,$model_match);
  501. }else{
  502. $getState = [];
  503. }
  504. $league = lm($model_match,'Sports')
  505. ->join($model_league,$model_league.'.lg_id',$model_match.'.lg_id')
  506. ->select($model_match.'.match_id',$model_match.'.home_team',$model_match.'.guest_team',$model_match.'.status',$model_match.'.match_date',$model_match.'.match_time')
  507. ->where($where)
  508. ->where($getState)
  509. ->get()
  510. ->toarray();
  511. }
  512. $list = array();
  513. foreach ($oddsData as $key=>$item){
  514. $item['team'] = '';
  515. if(strpos($item['odds_code'],'home') !== false) {
  516. $item['team'] = 'home';
  517. }
  518. if(strpos($item['odds_code'],'guest') !== false) {
  519. $item['team'] = 'guest';
  520. }
  521. $list[$key] = $item;
  522. }
  523. $p_code_array = [];
  524. foreach ($oddsData as $key =>$val){
  525. $p_code_array[] = $val['p_code'];
  526. }
  527. $p_code_array = array_keys(array_flip(array_unique($p_code_array)));//p_code 去重排序
  528. $data = [
  529. 'lg_id' =>$matchData->lg_id,
  530. 'leagueName' =>$matchData->name_chinese,
  531. 'match_id'=>$matchData->match_id,
  532. 'home_team'=>$matchData->home_team,
  533. 'guest_team'=>$matchData->guest_team,
  534. 'match_time'=>$matchData->match_date." ".$matchData->match_time,
  535. 'match_ptime'=>$matchData->ptime,
  536. 'match_score'=>$matchData->match_score,
  537. 'p_code_array'=> $p_code_array,
  538. 'oddsData'=>$oddsData,
  539. 'league' => $league,
  540. ];
  541. Render($data, '1', lang('Tips','Sports')->get('success'));
  542. } catch (\Exception $e) {
  543. echo $e->getMessage();
  544. }
  545. }
  546. /**
  547. * 手动 更新 赛事状态
  548. */
  549. public function updateMatch () {
  550. $game_code = $_REQUEST['game_code'];
  551. $match_date = $_REQUEST['match_date'];
  552. try {
  553. if(empty($game_code)){
  554. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  555. }
  556. if(empty($match_date)) $match_date = date('Y-m-d');
  557. //根据球类代码 获取相关model
  558. $models = $this->commonFunction->getModels($game_code);
  559. $model_match = $models['model_match'];
  560. $matchData = lm($model_match,'Sports')->select('match_id','status','match_date','match_time')->where(['match_date'=>$match_date])->get();
  561. foreach ($matchData as $key=>$item){
  562. $time = strtotime($item->match_date.$item->match_time);
  563. if(($time+(60*90)) < time()){ //更新为 已结束
  564. lm($model_match,'Sports')->where(['match_id'=>$item->match_id])->update(['status'=>2]);
  565. }
  566. if(($time+(60*90)) > time() and $time < time()){ //更新为 进行中
  567. lm($model_match,'Sports')->where(['match_id'=>$item->match_id])->update(['status'=>1]);
  568. }
  569. }
  570. Render([], '1', lang('Tips','Sports')->get('success'));
  571. } catch (\Exception $e) {
  572. echo $e->getMessage();
  573. }
  574. }
  575. /**
  576. * 获取即将开赛 所有赛事
  577. * 首页使用
  578. */
  579. public function getSoon(){
  580. $source = $this->source;//数据源 条件
  581. try {
  582. $where = $this->commonFunction->getState('StSoon');
  583. $data = $this->getTypeData->getAllSoon($source,$where);
  584. Render($data, '1', lang('Tips','Sports')->get('success'));
  585. } catch (\Exception $e) {
  586. echo $e->getMessage();
  587. }
  588. }
  589. /**
  590. * 获取球类-玩法-赔率代码
  591. */
  592. public function getOddsCode(){
  593. $type = $_REQUEST['type'];
  594. $game_code = $_REQUEST['game_code'];
  595. if($type == 'p_code'){
  596. $p_code = lm('st_odds_code','Sports')
  597. ->select('game_code','odds_code','odds_name')
  598. ->where(['p_id'=>0])
  599. ->get();
  600. }else{
  601. $p_code = lm('st_odds_code','Sports')
  602. ->select('id','game_code','odds_code','odds_name')
  603. ->where('p_id',0)
  604. ->get()->toarray();
  605. $code = lm('st_odds_code','Sports')
  606. ->select('id','p_id','game_code','odds_code','odds_name')
  607. ->get()->toArray();
  608. foreach ($p_code as $k=>$v){
  609. foreach ($code as $kk=>$vv){
  610. if($v['id'] == $vv['p_id']){
  611. $p_code[$k]['below'][] = $vv;
  612. }
  613. }
  614. }
  615. }
  616. Render($p_code, '1', lang('Tips','Sports')->get('success'));
  617. }
  618. /**
  619. * 获取 欧洲冠军杯数据
  620. */
  621. public function getUEFAChampions (){
  622. $data = $this->getTypeData->getUEFAChampions($this->source);
  623. Render($data, '1', lang('Tips','Sports')->get('success'));
  624. }
  625. /**
  626. * 更新赔率数据
  627. */
  628. public function updateOdds(){
  629. $data = $_REQUEST;
  630. // $data = ['game_code'=>'zq','match_id'=>3095448];
  631. $getModels = $this->commonFunction->getModels($data['game_code']);
  632. $model_match = $getModels['model_match'];
  633. $model_odds = $getModels['model_odds'];
  634. $where[] = [$model_match.'.match_id','=',$data['match_id']];
  635. $oddsData = lm($model_match,"Sports")
  636. ->join($model_odds,$model_odds.'.match_id',$model_match.'.match_id')
  637. ->select($model_match.'.match_id',$model_match.'.match_date',$model_match.'.home_team',$model_match.'.guest_team',$model_odds.'.id as odds_id',$model_odds.'.p_code',$model_odds.'.odds_code',$model_odds.'.condition',$model_odds.'.odds',$model_odds.'.odds_only',$model_odds.'.status',$model_odds.'.sort')
  638. ->where($model_match.'.source',$this->source)
  639. ->where($model_odds.'.type',0)
  640. ->where($where)
  641. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  642. ->get()->toArray();
  643. $p_code_array = [];
  644. foreach ($oddsData as $key =>$val){
  645. $p_code_array[$val['p_code']] = lang('OddsTemp','Sports')->get($val['p_code']);
  646. }
  647. $data = [
  648. 'oddsData'=>$oddsData,
  649. 'p_code_array'=>$p_code_array
  650. ];
  651. Render($data, '1', lang('Tips','Sports')->get('success'));
  652. }
  653. /**
  654. * 获取赛事所有数据
  655. * @param [array] $where 条件
  656. *@param [array] $Orwhere 或者条件
  657. * @param [type] $game_code 球类代码
  658. * @return [array] $data
  659. */
  660. public function getMatchInfo($where=[],$Orwhere=[],$where_search=[],$game_code ='zq'){
  661. $getModels = $this->commonFunction->getModels($game_code);
  662. $st_league = $getModels['model_league'];
  663. $st_competition = $getModels['model_match'];
  664. try{
  665. //查询今日赛事
  666. $data = lm($st_competition,'Sports')
  667. ->join($st_league,$st_league.'.lg_id',$st_competition.'.lg_id')
  668. ->leftjoin('st_area_country as country','country.id',$st_league.'.country_id')
  669. ->leftjoin('st_area_country as area','area.id',$st_league.'.area_id')
  670. ->select($st_league.'.name_chinese',$st_league.'.lg_id',$st_competition.'.match_id',$st_competition.'.home_team',$st_competition.'.guest_team',$st_competition.'.match_date',$st_competition.'.status',$st_competition.'.match_time',$st_competition.'.tag','country.name as country_name','area.name as area_name')
  671. ->where($where)
  672. ->where($Orwhere)
  673. ->where($where_search)
  674. ->where([
  675. [$st_competition.'.home_team','<>',null],
  676. [$st_competition.'.guest_team','<>',null],
  677. [$st_league.'.name_chinese','<>',null],
  678. [$st_competition.'.status','<',2],
  679. [$st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)]
  680. ])
  681. ->distinct($st_competition.'.match_id')
  682. ->get()
  683. ->toarray();
  684. $data = $this->division($data);
  685. return $data;
  686. } catch (\Exception $e) {
  687. echo $e->getMessage();
  688. }
  689. }
  690. /**
  691. * 数据分割器
  692. * @param [array] $matchData 赛事数据
  693. * @param [type] $type 分割类型 1:以联赛分
  694. * @return [array] $league
  695. */
  696. public function division(array $matchData)
  697. {
  698. //联赛分割
  699. $league = [];
  700. foreach ($matchData as $k => $v) {
  701. $league[$k]['name_chinese'] = $v['name_chinese'];
  702. $league[$k]['lg_id'] = $v['lg_id'];
  703. }
  704. //联赛去重
  705. $league = array_unique($league, SORT_REGULAR);
  706. //根据联赛分类
  707. foreach ($league as $key => $vlue) {
  708. foreach ($matchData as $k => $v) {
  709. if ($vlue['name_chinese'] == $v['name_chinese']) {
  710. $league[$key]['match_info'][$k] = $v;
  711. }
  712. }
  713. }
  714. //统计联赛下赛事
  715. foreach ($league as $key => $vlue) {
  716. $league[$key]['match_info'] = array_sort($league[$key]['match_info']);
  717. $league[$key]['count'] = count($vlue['match_info']);
  718. }
  719. return $league;
  720. }
  721. /**
  722. * 获取赛事赔率数据
  723. * @param [array] $where 条件
  724. *@param [array] $Orwhere 或者条件
  725. * @param [type] $game_code 球类代码
  726. * @return [array] $data
  727. */
  728. private function getOdds(array $data,$st_odds){
  729. //循环获取赛事赔率
  730. foreach ($data as $key=>$item){
  731. $data[$key]['oddsData'] = lm($st_odds, 'Sports')
  732. ->select('id','p_code','odds_code','status','odds','condition','sort')
  733. ->where($this->source)
  734. ->where(['match_id'=>$item['match_id'],'type'=>0])
  735. ->where(function($query)use ($st_odds){
  736. $query->where($st_odds.'.odds_code','concede_home')
  737. ->orWhere(function($query)use ($st_odds){
  738. $query->where($st_odds.'.odds_code','concede_guest');
  739. })
  740. ->orWhere(function($query)use ($st_odds){
  741. $query->where($st_odds.'.odds_code','goal_size_home');
  742. })
  743. ->orWhere(function($query)use ($st_odds){
  744. $query->where($st_odds.'.odds_code','goal_size_guest');
  745. });
  746. })
  747. ->get()->toArray();
  748. if(!empty($data[$key]['oddsData'])){
  749. //根据 排序 获取 最新让球/大小玩法赔率
  750. $sortData = array_column($data[$key]['oddsData'],'sort');
  751. array_multisort($sortData,SORT_DESC,$data[$key]['oddsData']);
  752. $data[$key]['oddsData'] = array_slice($data[$key]['oddsData'],0,4);//前四条 放入
  753. }else{
  754. $data[$key]['oddsData'] = [];
  755. }
  756. }
  757. return $data;
  758. }
  759. /**
  760. * 获取赛事总数
  761. * @param [array] $where 条件
  762. *@param [array] $Orwhere 或者条件
  763. * @param [type] $game_code 球类代码
  764. * @return [int] $dataCount
  765. */
  766. public function getMatchCount($where=[],$Orwhere=[],$where_search=[],$game_code ='zq'){
  767. $getModels = $this->commonFunction->getModels($game_code);
  768. $st_league = $getModels['model_league'];
  769. $st_competition = $getModels['model_match'];
  770. //查询今日赛事
  771. $dataCount = lm($st_competition,'Sports')
  772. ->join($st_league,$st_league.'.lg_id',$st_competition.'.lg_id')
  773. ->leftjoin('st_area_country as country','country.id',$st_league.'.country_id')
  774. ->leftjoin('st_area_country as area','area.id',$st_league.'.area_id')
  775. ->where($where)
  776. ->where($Orwhere)
  777. ->where($where_search)
  778. ->where([
  779. [$st_competition.'.home_team','<>',null],
  780. [$st_competition.'.guest_team','<>',null],
  781. [$st_league.'.name_chinese','<>',null],
  782. [$st_competition.'.status','<',2],
  783. [$st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)]
  784. ])
  785. ->distinct($st_competition.'.match_id')
  786. ->count('*');
  787. return $dataCount;
  788. }
  789. }