MatchList.php 36 KB

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