MatchList.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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'],$data['game_code']);
  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,$typeGame);
  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,$game_code);
  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. $oddsTypeWhere = [[$getModels['model_odds'].'.is_stringscene','=',0]];
  187. if($game_code == 'zq'){
  188. $matchData = $this->getOddsData->getOddsZQ($matchData,$st_odds,$this->source,$oddsTypeWhere);
  189. }
  190. if($game_code == 'lq'){
  191. $matchData = $this->getOddsData->getOddsLQ($matchData,$st_odds,$this->source,$oddsTypeWhere);
  192. }
  193. if($game_code == 'bq'){
  194. $matchData = $this->getOddsData->getOddsBQ($matchData,$st_odds,$this->source,$oddsTypeWhere);
  195. }
  196. if($game_code == 'wq'){
  197. $matchData = $this->getOddsData->getOddsWQ($matchData,$st_odds,$this->source,$oddsTypeWhere);
  198. }
  199. foreach($data as $k => $v){
  200. $v['match_info'] = [];
  201. foreach($matchData as $kk=>$vv){
  202. if($v['lg_id'] == $vv['lg_id']){
  203. $v['match_info'][] = $vv;
  204. }
  205. }
  206. $data[$k] = $v;
  207. }
  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',$st_competition,$gameType);
  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_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'];
  241. $country = $this->getRegionInfo($where,$search,$select,'st_area_country','id','country_id',$gameType);
  242. //洲下所有联赛
  243. $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'];
  244. $area = $this->getRegionInfo($where,$search,$select,'st_area_country','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.'.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. $regionInfo = $this->commonFunction->Handle_Odds_Null($regionInfo,$getModels);
  280. //地区分割
  281. $info = [];
  282. foreach($regionInfo as $k => $v){
  283. $info[$k]['region'] = $v['region'];
  284. $info[$k]['region_id'] = $v['region_id'];
  285. }
  286. //地区去重
  287. $info = array_unique($info, SORT_REGULAR);
  288. //根据地区分类
  289. foreach ($info as $key => $vlue) {
  290. foreach ($regionInfo as $k => $v) {
  291. if ($vlue['region'] == $v['region'] and $v['country_id'] == 0) {
  292. $info[$key]['league_count'][$k]['league'] = $v['league'];
  293. $info[$key]['league_count'][$k]['lg_id'] = $v['lg_id'];
  294. }
  295. }
  296. }
  297. //去除没有联赛的国家或地区
  298. foreach($info as $k =>$v){
  299. if(empty($v['league_count'])){
  300. unset($info[$k]);
  301. }
  302. }
  303. sort($info);
  304. //联赛去重
  305. foreach ($info as $key => $vlue) {
  306. $info[$key]['league_count'] = array_unique($vlue['league_count'], SORT_REGULAR);
  307. //统计联赛下赛事
  308. foreach ($vlue['league_count'] as $k => $v) {
  309. foreach ($regionInfo as $kk => $vv) {
  310. if ($v['league'] == $vv['league']) {
  311. if(!empty($info[$key]['league_count'][$kk]['league'])){
  312. $info[$key]['league_count'][$kk]['count'] += 1;
  313. }
  314. }
  315. }
  316. }
  317. sort($info[$key]['league_count']);
  318. }
  319. return $info;
  320. } catch (\Exception $e) {
  321. echo $e->getMessage();
  322. }
  323. }
  324. /**
  325. * 联赛下 赛事及默认赔率数据
  326. */
  327. public function matchDetails(){
  328. $data = $_REQUEST;
  329. try {
  330. if(empty($data['game_code']) || empty($data['leagueID']) || empty($data['oddsType'])){
  331. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  332. }
  333. //根据时间获取联赛下赛事数据
  334. $whereDate = [];
  335. if(!empty($data['matchDate']) ){
  336. if($data['matchDate']== "morning"){
  337. $whereDate = [['is_morningplate',1],
  338. ['match_date','>',date("Y-m-d",strtotime("+6 day"))]];
  339. }else{
  340. $whereDate = ['match_date'=>$data['matchDate']];
  341. }
  342. }
  343. $models = $this->commonFunction->getModels($data['game_code'],0);
  344. $model_match = $models['model_match'];
  345. $model_odds = $models['model_odds'];
  346. $model_league = $models['model_league'];
  347. $model_result = $models['model_result'];
  348. $oddsTypeWhere = $this->getOddsTypeWhere($model_odds,$data['oddsType']);
  349. $source = $this->source;//数据源
  350. $data = $this->getOddsData->getOddsData($data,$whereDate,$source,$oddsTypeWhere);
  351. if(!empty($data['matchData'])){
  352. foreach($data['matchData'] as $k=>$v){
  353. if(empty($v['oddsData'])) unset($data['matchData'][$k]);
  354. }
  355. sort($data['matchData']);
  356. $data['matchNum'] = count($data['matchData']);
  357. }
  358. Render($data, '1', lang('Tips','Sports')->get('success'));
  359. } catch (\Exception $e) {
  360. echo $e->getMessage();
  361. }
  362. }
  363. //获取各状态下有哪些球类
  364. public function getGame(){
  365. $type_code = $_REQUEST['type_code'];
  366. try {
  367. if(empty($type_code)){
  368. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  369. }
  370. //==获取当前可用球类==
  371. $game = lm('GameType', 'Sports')->where('status',1)->select('game_code','game_name','game_ico_url')->get()->toArray();
  372. $gameData = [];
  373. foreach ($game as $key=>$type){
  374. $matchModel = $this->commonFunction->getModels($type['game_code']);
  375. //获取 不同状态的查询条件
  376. $where = $this->commonFunction->getState($type_code,$matchModel['model_match'],$type['game_code']);
  377. if($type_code != 'StChampion'){
  378. $matchNum = lm($matchModel['model_league'],"Sports")
  379. ->join($matchModel['model_match'],$matchModel['model_match'].'.lg_id',$matchModel['model_league'].'.id')
  380. ->join($matchModel['model_odds'],$matchModel['model_odds'].'.match_id',$matchModel['model_match'].'.id')
  381. ->where($where)
  382. ->where([[$matchModel['model_league'].'.name_chinese','!=','']])
  383. ->count("*");
  384. //获取满足状态的球类/去除冠军类
  385. if($matchNum != 0 and $type['game_code']!='gj'){
  386. $gameData[] = $type;
  387. }
  388. }else{
  389. $matchNum = lm($matchModel['model_odds'], 'Sports')
  390. ->where($where)
  391. ->count('*');
  392. //获取满足状态的球类/去除冠军类
  393. if($matchNum != 0 and $type['game_code']!='gj'){
  394. $gameData[] = $type;
  395. }
  396. }
  397. }
  398. Render($gameData, '1', lang('Tips','Sports')->get('success'));
  399. } catch (\Exception $e) {
  400. echo $e->getMessage();
  401. }
  402. }
  403. /**
  404. * @throws \Exception
  405. * 获取不同状态下 各球类赛事数据
  406. * game_code 球类代码
  407. * type_code 类型代码
  408. */
  409. public function matchState(){
  410. $source = $this->source;//数据源 条件
  411. $ret = $_REQUEST;
  412. try {
  413. if(empty($ret['game_code']) || empty($ret['type_code'])){
  414. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  415. }
  416. $data = $this->getTypeData->typeData($ret,$source);
  417. Render($data, '1', lang('Tips','Sports')->get('success'));
  418. } catch (\Exception $e) {
  419. echo $e->getMessage();
  420. }
  421. }
  422. /**
  423. * 根据赛事状态获取赔率查询条件
  424. */
  425. public function getOddsTypeWhere($model_odds,$oddsType){
  426. //获取赛事不同状态下的赔率
  427. if(!empty($oddsType)){
  428. switch ($oddsType)
  429. {
  430. case 'StRollBall'://滚球
  431. $oddsTypeWhere = [[$model_odds.'.is_stringscene','=',0]];
  432. break;
  433. case 'StSoon'://即将
  434. $oddsTypeWhere = [[$model_odds.'.is_stringscene','=',0]];
  435. break;
  436. case 'StToday'://今日
  437. $oddsTypeWhere = [[$model_odds.'.is_stringscene','=',0]];
  438. break;
  439. case 'StMorningPlate'://早盘
  440. $oddsTypeWhere = [[$model_odds.'.is_stringscene','=',0]];
  441. break;
  442. case 'StStringScene'://串场
  443. $oddsTypeWhere = [[$model_odds.'.is_stringscene','=',1]];
  444. break;
  445. case 'home'://首页
  446. $oddsTypeWhere = [[$model_odds.'.is_stringscene','=',0]];
  447. break;
  448. default:
  449. $oddsTypeWhere = [];
  450. }
  451. }
  452. return $oddsTypeWhere;
  453. }
  454. /**
  455. * 获取赛事所有玩法赔率数据
  456. */
  457. public function matchOdds(){
  458. $game_code = $_REQUEST['game_code'];
  459. $matchID = $_REQUEST['matchID'];
  460. $status = $_REQUEST['status'];
  461. $oddsType = $_REQUEST['oddsType'];
  462. try {
  463. if(empty($game_code) || empty($matchID) || empty($oddsType)){
  464. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  465. }
  466. //根据球类代码 获取相关model
  467. $models = $this->commonFunction->getModels($game_code,1);
  468. $model_match = $models['model_match'];
  469. $model_odds = $models['model_odds'];
  470. $model_league = $models['model_league'];
  471. $model_result = $models['model_result'];
  472. $where[] = ['status','=',0];
  473. //如果是串场,则获取串场赔率
  474. if($oddsType == 'StStringScene'){
  475. $where[] = ['is_stringscene','=',1];
  476. }else{
  477. $where[] = ['is_stringscene','=',0];
  478. }
  479. //根据球类追加获取限定玩法
  480. $p_code = $this->commonFunction->getOddsPcode($game_code);
  481. $oddsData = lm($model_odds, 'Sports')
  482. ->select('sort','p_code','id','match_id','odds_only','odds_code',$model_odds.'.status','odds','condition','sort','source','utime')
  483. ->where([$model_odds.'.match_id'=>$matchID,$model_odds.'.type'=>0])
  484. ->where($where)
  485. ->whereIn('p_code',$p_code)
  486. ->orderBy('sort', 'desc')
  487. ->orderBy('p_code','desc')
  488. ->orderBy('odds_code','desc')
  489. ->get()->toArray();
  490. $matchData = lm($model_match, 'Sports')
  491. ->join($model_league,$model_league.'.id',$model_match.'.lg_id')
  492. ->select($model_match.'.lg_id','name_chinese',$model_match.'.id as match_id','home_team','guest_team','match_date','match_time',$model_match.'.status')
  493. ->where([$model_match.'.id'=>$matchID])
  494. ->first();
  495. if(empty($matchData)) Render(null, '1', lang('Tips','Sports')->get('success'));
  496. $matchData->ptime = '';//赛事进行时间
  497. $matchData->match_score = '';//赛事比分
  498. $matchData->result_mark = '';//赛事实时数据json
  499. //通过开赛日期和时间判断是否是进行中滚球
  500. 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())){
  501. $matchData->status = 1;
  502. }
  503. //如果该赛事正在进行,则获取进行时间
  504. if($matchData->status ==1){
  505. $result_record = $models['model_result_record'];
  506. $match_id = $matchData->match_id;
  507. //如果是棒球
  508. if($result_record == 'st_bq_result_record' || $result_record == 'st_lq_result_record'){
  509. $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,
  510. (select match_id,max(id) id from $result_record where match_id =$match_id group by match_id)b
  511. where a.match_id = b.match_id and a.id = b.id ";
  512. //查询 赛事结果记录最新的一条
  513. $match_result_record = S ('DB')->select ($sql);
  514. $home_score = $match_result_record[0]->home_score?:0;
  515. $guest_score = $match_result_record[0]->guest_score?:0;
  516. $matchData->match_score = $home_score.'-'.$guest_score;
  517. $matchData->result_mark = $match_result_record[0]->result_mark;
  518. }
  519. //如果是网球
  520. else if($result_record == 'st_wq_result_record'){
  521. $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,
  522. (select match_id,max(id) id from $result_record where match_id =$match_id group by match_id)b
  523. where a.match_id = b.match_id and a.id = b.id ";
  524. //查询 赛事结果记录最新的一条
  525. $match_result_record = S ('DB')->select ($sql);
  526. $home_score = $match_result_record[0]->home_player_score?:0;
  527. $guest_score = $match_result_record[0]->guest_player_score?:0;
  528. $matchData->match_score = $home_score.'-'.$guest_score;
  529. $matchData->result_mark = $match_result_record[0]->result_mark;
  530. }else{
  531. //其他球类
  532. $sql = "select a.match_id,a.home_score,a.guest_score,a.match_time as a_time,a.match_process from $result_record a,
  533. (select match_id,max(id) id from $result_record where match_id = $match_id group by match_id)b
  534. where a.match_id = b.match_id and a.id = b.id ";
  535. //查询 赛事结果记录最新的一条
  536. $match_result_record = S ('DB')->select ($sql);
  537. $home_score = $match_result_record[0]->home_score?:0;
  538. $guest_score = $match_result_record[0]->guest_score?:0;
  539. $matchData->match_score = $home_score.'-'.$guest_score;
  540. $matchData->result_mark = '';
  541. }
  542. $matchData->ptime = $match_result_record[0]->a_time?:0;
  543. }
  544. //赛事已结束
  545. if($matchData->status ==2){
  546. $oddsData = [];
  547. }
  548. //查询当前联赛下的赛事
  549. $league = [];
  550. if($oddsType != 'StRollBall'){
  551. $where = [
  552. [$model_match.'.home_team','<>',null],
  553. [$model_match.'.guest_team','<>',null],
  554. [$model_match.'.status','<',2],
  555. [$model_match.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)],
  556. [$model_match.'.lg_id',$matchData->lg_id],
  557. [$model_league.'.name_chinese','!=',''],
  558. ];
  559. if($oddsType !='home'){
  560. $getState = $this->commonFunction->getState($oddsType,$model_match,$game_code);
  561. }else{
  562. $getState = [];
  563. }
  564. $league = lm($model_match,'Sports')
  565. ->join($model_league,$model_league.'.id',$model_match.'.lg_id')
  566. ->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')
  567. ->where($where)
  568. ->where($getState)
  569. ->get()
  570. ->toArray();
  571. //===获取当前赛事是否有赔率,如果没有则去除===
  572. $league = $this->commonFunction->Handle_Odds_Null($league,$models);
  573. }
  574. $list = array();
  575. foreach ($oddsData as $key=>$item){
  576. //获取数据源ID
  577. $item['sourceID'] = $this->commonFunction->getSourceID($item['source']);
  578. $item['mark'] = $game_code.'-'.$item['match_id'].'-'.$item['p_code'].'-'.$item['odds_code'].'-'.$item['sort'];
  579. unset($item['source']);
  580. //追加所属赔率状态
  581. $item['oddsType'] = '';
  582. if($oddsType == 'StRollBall'){
  583. $item['oddsType'] = 'StRollBall';
  584. }
  585. $item['team'] = '';
  586. if(strpos($item['odds_code'],'home') !== false) {
  587. $item['team'] = 'home';
  588. }
  589. if(strpos($item['odds_code'],'guest') !== false) {
  590. $item['team'] = 'guest';
  591. }
  592. $list[$key] = $item;
  593. }
  594. $p_code_array = [];
  595. foreach ($oddsData as $key =>$val){
  596. $p_code_array[] = $val['p_code'];
  597. }
  598. $p_code_array = array_keys(array_flip(array_unique($p_code_array)));//p_code 去重排序
  599. $data = [
  600. 'lg_id' =>$matchData->lg_id,
  601. 'leagueName' =>$matchData->name_chinese,
  602. 'match_id'=>$matchData->match_id,
  603. 'home_team'=>$matchData->home_team,
  604. 'guest_team'=>$matchData->guest_team,
  605. 'match_time'=>$matchData->match_date." ".$matchData->match_time,
  606. 'match_ptime'=>$matchData->ptime,
  607. 'match_score'=>$matchData->match_score,
  608. 'p_code_array'=> $p_code_array,
  609. 'result_mark'=>$matchData->result_mark,
  610. 'oddsData'=>$list,
  611. 'league' => $league,
  612. ];
  613. Render($data, '1', lang('Tips','Sports')->get('success'));
  614. } catch (\Exception $e) {
  615. echo $e->getMessage();
  616. }
  617. }
  618. /**
  619. * 手动 更新 赛事状态
  620. */
  621. public function updateMatch () {
  622. $game_code = $_REQUEST['game_code'];
  623. $match_date = $_REQUEST['match_date'];
  624. try {
  625. if(empty($game_code)){
  626. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  627. }
  628. if(empty($match_date)) $match_date = date('Y-m-d');
  629. //根据球类代码 获取相关model
  630. $models = $this->commonFunction->getModels($game_code);
  631. $model_match = $models['model_match'];
  632. $matchData = lm($model_match,'Sports')->select('id as match_id','status','match_date','match_time')->where(['match_date'=>$match_date])->get();
  633. foreach ($matchData as $key=>$item){
  634. $time = strtotime($item->match_date.$item->match_time);
  635. if(($time+(60*90)) < time()){ //更新为 已结束
  636. lm($model_match,'Sports')->where(['id'=>$item->match_id])->update(['status'=>2]);
  637. }
  638. if(($time+(60*90)) > time() and $time < time()){ //更新为 进行中
  639. lm($model_match,'Sports')->where(['id'=>$item->match_id])->update(['status'=>1]);
  640. }
  641. }
  642. Render([], '1', lang('Tips','Sports')->get('success'));
  643. } catch (\Exception $e) {
  644. echo $e->getMessage();
  645. }
  646. }
  647. /**
  648. * 获取即将开赛 所有赛事
  649. * 首页使用
  650. */
  651. public function getSoon(){
  652. $source = $this->source;//数据源 条件
  653. try {
  654. $where = $this->commonFunction->getState('StSoon');
  655. $data = $this->getTypeData->getAllSoon($source,$where);
  656. Render($data, '1', lang('Tips','Sports')->get('success'));
  657. } catch (\Exception $e) {
  658. echo $e->getMessage();
  659. }
  660. }
  661. /**
  662. * 获取球类-玩法-赔率代码
  663. */
  664. public function getOddsCode(){
  665. $type = $_REQUEST['type'];
  666. $game_code = $_REQUEST['game_code'];
  667. if($type == 'p_code'){
  668. $p_code = lm('st_odds_code','Sports')
  669. ->select('game_code','odds_code','odds_name')
  670. ->where(['p_id'=>0])
  671. ->get();
  672. }else{
  673. $p_code = lm('st_odds_code','Sports')
  674. ->select('id','game_code','odds_code','odds_name')
  675. ->where('p_id',0)
  676. ->get()->toarray();
  677. $code = lm('st_odds_code','Sports')
  678. ->select('id','p_id','game_code','odds_code','odds_name')
  679. ->get()->toArray();
  680. foreach ($p_code as $k=>$v){
  681. foreach ($code as $kk=>$vv){
  682. if($v['id'] == $vv['p_id']){
  683. $p_code[$k]['below'][] = $vv;
  684. }
  685. }
  686. }
  687. }
  688. Render($p_code, '1', lang('Tips','Sports')->get('success'));
  689. }
  690. /**
  691. * 获取 欧洲冠军杯数据
  692. */
  693. public function getUEFAChampions (){
  694. $data = $this->getTypeData->getUEFAChampions($this->source);
  695. Render($data, '1', lang('Tips','Sports')->get('success'));
  696. }
  697. /**
  698. * 更新赔率数据
  699. */
  700. public function updateOdds(){
  701. $data = $_REQUEST;
  702. $getModels = $this->commonFunction->getModels($data['game_code']);
  703. $model_match = $getModels['model_match'];
  704. $model_odds = $getModels['model_odds'];
  705. $where[] = [$model_match.'.id','=',$data['match_id']];
  706. $oddsData = lm($model_match,"Sports")
  707. ->join($model_odds,$model_odds.'.match_id',$model_match.'.id')
  708. ->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')
  709. ->where($model_odds.'.type',0)
  710. ->where([$model_odds.'.status'=>0])
  711. ->where($where)
  712. ->get()->toArray();
  713. $p_code_array = [];
  714. foreach ($oddsData as $key =>$val){
  715. $p_code_array[$val['p_code']] = lang('OddsTemp','Sports')->get($val['p_code']);
  716. }
  717. $data = [
  718. 'oddsData'=>$oddsData,
  719. 'p_code_array'=>$p_code_array
  720. ];
  721. Render($data, '1', lang('Tips','Sports')->get('success'));
  722. }
  723. /**
  724. * 获取赛事所有数据
  725. * @param [array] $where 条件
  726. *@param [array] $Orwhere 或者条件
  727. * @param [type] $game_code 球类代码
  728. * @return [array] $data
  729. */
  730. public function getMatchInfo($where=[],$Orwhere=[],$where_search=[],$game_code ='zq'){
  731. $getModels = $this->commonFunction->getModels($game_code);
  732. $st_league = $getModels['model_league'];
  733. $st_competition = $getModels['model_match'];
  734. try{
  735. //查询今日赛事
  736. $data = lm($st_competition,'Sports')
  737. ->join($st_league,$st_league.'.id',$st_competition.'.lg_id')
  738. ->leftjoin('st_area_country as country','country.id',$st_league.'.country_id')
  739. ->leftjoin('st_area_country as area','area.id',$st_league.'.area_id')
  740. ->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')
  741. ->where($where)
  742. ->where($Orwhere)
  743. ->where($where_search)
  744. ->where([
  745. [$st_competition.'.home_team','<>',null],
  746. [$st_competition.'.guest_team','<>',null],
  747. [$st_league.'.name_chinese','<>',null],
  748. [$st_competition.'.status','<',2],
  749. [$st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)]
  750. ])
  751. ->distinct($st_competition.'.id')
  752. ->get()
  753. ->toarray();
  754. //===获取当前赛事是否有赔率,如果没有则去除===
  755. $data = $this->commonFunction->Handle_Odds_Null($data,$getModels);
  756. $data = $this->division($data);
  757. return $data;
  758. } catch (\Exception $e) {
  759. echo $e->getMessage();
  760. }
  761. }
  762. /**
  763. * 数据分割器
  764. * @param [array] $matchData 赛事数据
  765. * @param [type] $type 分割类型 1:以联赛分
  766. * @return [array] $league
  767. */
  768. public function division(array $matchData)
  769. {
  770. //联赛分割
  771. $league = [];
  772. foreach ($matchData as $k => $v) {
  773. $league[$k]['name_chinese'] = $v['name_chinese'];
  774. $league[$k]['lg_id'] = $v['lg_id'];
  775. }
  776. //联赛去重
  777. $league = array_unique($league, SORT_REGULAR);
  778. //根据联赛分类
  779. foreach ($league as $key => $vlue) {
  780. foreach ($matchData as $k => $v) {
  781. if ($vlue['name_chinese'] == $v['name_chinese']) {
  782. $league[$key]['match_info'][$k] = $v;
  783. }
  784. }
  785. }
  786. //统计联赛下赛事
  787. foreach ($league as $key => $vlue) {
  788. $league[$key]['match_info'] = array_sort($league[$key]['match_info']);
  789. $league[$key]['count'] = count($vlue['match_info']);
  790. }
  791. return $league;
  792. }
  793. /**
  794. * 获取赛事总数
  795. * @param [array] $where 条件
  796. *@param [array] $Orwhere 或者条件
  797. * @param [type] $game_code 球类代码
  798. * @return [int] $dataCount
  799. */
  800. public function getMatchCount($where=[],$Orwhere=[],$where_search=[],$game_code ='zq'){
  801. $getModels = $this->commonFunction->getModels($game_code);
  802. $st_league = $getModels['model_league'];
  803. $st_competition = $getModels['model_match'];
  804. //查询今日赛事
  805. $dataCount = lm($st_competition,'Sports')
  806. ->join($st_league,$st_league.'.id',$st_competition.'.lg_id')
  807. ->leftjoin('st_area_country as country','country.id',$st_league.'.country_id')
  808. ->leftjoin('st_area_country as area','area.id',$st_league.'.area_id')
  809. ->select($st_competition.'.id as match_id')
  810. ->where($where)
  811. ->where($Orwhere)
  812. ->where($where_search)
  813. ->where([
  814. [$st_competition.'.home_team','<>',null],
  815. [$st_competition.'.guest_team','<>',null],
  816. [$st_league.'.name_chinese','<>',null],
  817. [$st_competition.'.status','<',2],
  818. [$st_competition.'.us_time','>',$this->commonFunction->qgmdate('Y-m-d H:i:s', '', -4)]
  819. ])
  820. ->distinct($st_competition.'.id')
  821. ->get()->toArray();
  822. //===获取当前赛事是否有赔率,如果没有则去除===
  823. $dataCount = $this->commonFunction->Handle_Odds_Null($dataCount,$getModels);
  824. return count($dataCount);
  825. }
  826. }