MatchList.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. foreach($data as $k => $v){
  180. $data[$k]['match_info'] = $this->getOdds($v['match_info'],$st_odds);
  181. $data[$k]['match_info'] = array_values($data[$k]['match_info']);
  182. }
  183. $data = array_values($data);
  184. Render($data, '1', lang('Tips','Sports')->get('success'));
  185. }
  186. /*
  187. *首页条件筛选(所有赛事)
  188. */
  189. public function matchData (){
  190. $gameType = $_REQUEST['gameType'];
  191. $search = $_REQUEST['search'];
  192. $date = $_REQUEST['datetime'];
  193. //=获取球了MODEL
  194. $gameType = empty($gameType) ? 'zq':$gameType;
  195. $getModels = $this->commonFunction->getModels($gameType);
  196. $st_league = $getModels['model_league'];
  197. $st_competition = $getModels['model_match'];
  198. //所属球类
  199. $where = [];
  200. if(!empty($date)){
  201. if($date == 'morning'){
  202. $where=[[$st_competition.'.is_morningplate',1],
  203. [$st_competition.'.match_date','>',date("Y-m-d",strtotime("+6 day"))]];
  204. }else if($date == 'today'){
  205. $where = $this->commonFunction->getState('StToday');
  206. }else {
  207. $where[$st_competition.'.match_date'] = $date;
  208. }
  209. }
  210. //查询球类名称
  211. $data['type'] = lm('GameType', 'Sports')
  212. ->where('game_code',$gameType)
  213. ->value('game_name');
  214. //国家联赛数据
  215. $select=['st_country.country_id as region_id','st_country.name_chinese as region',$st_league.'.lg_id',$st_league.'.name_chinese as league'];
  216. $country = $this->getRegionInfo($where,$search,$select,'st_country','country_id','country_id',$gameType);
  217. //洲下所有联赛
  218. $select=['st_area.id as region_id','st_area.title as region',$st_league.'.lg_id',$st_league.'.name_chinese as league'];
  219. $area = $this->getRegionInfo($where,$search,$select,'st_area','id','area_id',$gameType);
  220. $data['info'] = array_merge($country,$area);
  221. Render($data, '1', lang('Tips','Sports')->get('success'));
  222. }
  223. /**
  224. * 根据地区获取信息
  225. * @param [array] $where 条件
  226. * @param [array] $search 搜索条件
  227. * @param [string] $regionModel 地区model
  228. * @param [int] $regionID 地区model
  229. * @param [type] $game_code 球类代码
  230. * @return [array] $data
  231. */
  232. public function getRegionInfo($where=[],$search,$select,$regionModel,$regionID,$a_c_id,$game_code ='zq'){
  233. $getModels = $this->commonFunction->getModels($game_code);
  234. $st_league = $getModels['model_league'];
  235. $st_competition = $getModels['model_match'];
  236. try{
  237. //地区联赛赛事数据
  238. $regionInfo = lm($st_league,"Sports")
  239. ->join($st_competition,$st_competition.'.lg_id',$st_league.'.lg_id')
  240. ->join($regionModel,$regionModel.'.'.$regionID,$st_league.'.'.$a_c_id)
  241. ->select($select)
  242. ->where($st_league.'.name_chinese','like','%'.$search.'%')
  243. ->where($where)
  244. ->where([
  245. [$st_competition.'.home_team','<>',null],
  246. [$st_competition.'.guest_team','<>',null],
  247. [$st_league.'.name_chinese','!=',''],
  248. [$st_competition.'.status','<',2],
  249. [$st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)]
  250. ])
  251. ->get()
  252. ->toArray();
  253. //地区分割
  254. $info = [];
  255. foreach($regionInfo as $k => $v){
  256. $info[$k]['region'] = $v['region'];
  257. $info[$k]['region_id'] = $v['region_id'];
  258. }
  259. //地区去重
  260. $info = array_unique($info, SORT_REGULAR);
  261. //根据地区分类
  262. foreach ($info as $key => $vlue) {
  263. foreach ($regionInfo as $k => $v) {
  264. if ($vlue['region'] == $v['region']) {
  265. $info[$key]['league_count'][$k]['league'] = $v['league'];
  266. $info[$key]['league_count'][$k]['lg_id'] = $v['lg_id'];
  267. }
  268. }
  269. }
  270. //联赛去重
  271. foreach ($info as $key => $vlue) {
  272. $info[$key]['league_count'] = array_unique($vlue['league_count'], SORT_REGULAR);
  273. //统计联赛下赛事
  274. foreach ($vlue['league_count'] as $k => $v) {
  275. foreach ($regionInfo as $kk => $vv) {
  276. if ($v['league'] == $vv['league']) {
  277. if(!empty($info[$key]['league_count'][$kk]['league'])){
  278. $info[$key]['league_count'][$kk]['count'] += 1;
  279. }
  280. }
  281. }
  282. }
  283. }
  284. return $info;
  285. } catch (\Exception $e) {
  286. echo $e->getMessage();
  287. }
  288. }
  289. /**
  290. * 联赛下 赛事及默认赔率数据
  291. */
  292. public function matchDetails(){
  293. $data = $_REQUEST;
  294. try {
  295. if(empty($data['game_code']) || empty($data['leagueID']) || empty($data['oddsType'])){
  296. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  297. }
  298. //根据时间获取联赛下赛事数据
  299. $whereDate = [];
  300. if(!empty($data['matchDate']) ){
  301. if($data['matchDate']== "morning"){
  302. $whereDate = [['is_morningplate',1],
  303. ['match_date','>',date("Y-m-d",strtotime("+6 day"))]];
  304. }else{
  305. $whereDate = ['match_date'=>$data['matchDate']];
  306. }
  307. }
  308. $models = $this->commonFunction->getModels($data['game_code'],0);
  309. $model_match = $models['model_match'];
  310. $model_odds = $models['model_odds'];
  311. $model_league = $models['model_league'];
  312. $model_result = $models['model_result'];
  313. $oddsTypeWhere = $this->getOddsTypeWhere($model_odds,$data['oddsType']);
  314. $source = $this->source;//数据源
  315. $data = $this->getOddsData->getOddsData($data,$whereDate,$source,$oddsTypeWhere);
  316. Render($data, '1', lang('Tips','Sports')->get('success'));
  317. } catch (\Exception $e) {
  318. echo $e->getMessage();
  319. }
  320. }
  321. //获取各状态下有哪些球类
  322. public function getGame(){
  323. $type_code = $_REQUEST['type_code'];
  324. try {
  325. if(empty($type_code)){
  326. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  327. }
  328. //==获取当前可用球类==
  329. $game = lm('GameType', 'Sports')->where('status',1)->select('game_code','game_name','game_ico_url')->get()->toArray();
  330. //获取 不同状态的查询条件
  331. $where = $this->commonFunction->getState($type_code);
  332. $gameData = [];
  333. foreach ($game as $key=>$type){
  334. if($type_code != 'StChampion'){
  335. $matchModel = $this->commonFunction->getModels($type['game_code']);
  336. $matchNum = lm($matchModel['model_league'],"Sports")
  337. ->join($matchModel['model_match'],$matchModel['model_match'].'.lg_id',$matchModel['model_league'].'.lg_id')
  338. ->where($matchModel['model_match'].'.source',$this->source['source'])
  339. ->where($where)
  340. ->where([[$matchModel['model_league'].'.name_chinese','!=','']])
  341. ->count("*");
  342. //获取满足状态的球类/去除冠军类
  343. if($matchNum != 0 and $type['game_code']!='gj'){
  344. $gameData[] = $type;
  345. }
  346. }else{
  347. $matchModel = $this->commonFunction->getModels($type['game_code']);
  348. $matchNum = lm($matchModel['model_odds'], 'Sports')
  349. ->where($this->source)
  350. ->where($where)
  351. ->count('*');
  352. //获取满足状态的球类/去除冠军类
  353. if($matchNum != 0 and $type['game_code']!='gj'){
  354. $gameData[] = $type;
  355. }
  356. }
  357. }
  358. Render($gameData, '1', lang('Tips','Sports')->get('success'));
  359. } catch (\Exception $e) {
  360. echo $e->getMessage();
  361. }
  362. }
  363. /**
  364. * @throws \Exception
  365. * 获取不同状态下 各球类赛事数据
  366. * game_code 球类代码
  367. * type_code 类型代码
  368. */
  369. public function matchState(){
  370. $source = $this->source;//数据源 条件
  371. $ret = $_REQUEST;
  372. try {
  373. if(empty($ret['game_code']) || empty($ret['type_code'])){
  374. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  375. }
  376. $data = $this->getTypeData->typeData($ret,$source);
  377. Render($data, '1', lang('Tips','Sports')->get('success'));
  378. } catch (\Exception $e) {
  379. echo $e->getMessage();
  380. }
  381. }
  382. /**
  383. * 根据赛事状态获取赔率查询条件
  384. */
  385. public function getOddsTypeWhere($model_odds,$oddsType){
  386. //获取赛事不同状态下的赔率
  387. if(!empty($oddsType)){
  388. switch ($oddsType)
  389. {
  390. case 'StRollBall'://滚球
  391. $oddsTypeWhere = [[$model_odds.'.is_rollball','=',1]];
  392. break;
  393. case 'StSoon'://即将
  394. $oddsTypeWhere = [[$model_odds.'.is_morningplate','=',1]];
  395. break;
  396. case 'StToday'://今日
  397. $oddsTypeWhere = [[$model_odds.'.is_today','=',1]];
  398. break;
  399. case 'StMorningPlate'://早盘
  400. $oddsTypeWhere = [[$model_odds.'.is_morningplate','=',1]];
  401. break;
  402. case 'StStringScene'://串场
  403. $oddsTypeWhere = [[$model_odds.'.is_stringscene','=',1]];
  404. break;
  405. case 'home'://首页
  406. $oddsTypeWhere = [[$model_odds.'.is_stringscene','=',1]];
  407. break;
  408. default:
  409. $oddsTypeWhere = [];
  410. }
  411. }
  412. return $oddsTypeWhere;
  413. }
  414. /**
  415. * 获取赛事所有玩法赔率数据
  416. */
  417. public function matchOdds(){
  418. $game_code = $_REQUEST['game_code'];
  419. $matchID = $_REQUEST['matchID'];
  420. $status = $_REQUEST['status'];
  421. $oddsType = $_REQUEST['oddsType'];
  422. try {
  423. if(empty($game_code) || empty($matchID) || empty($oddsType)){
  424. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  425. }
  426. //根据球类代码 获取相关model
  427. $models = $this->commonFunction->getModels($game_code,0);
  428. $model_match = $models['model_match'];
  429. $model_odds = $models['model_odds'];
  430. $model_league = $models['model_league'];
  431. $model_result = $models['model_result'];
  432. //取消按状态查询赔率数据
  433. //$oddsTypeWhere = $this->getOddsTypeWhere($model_odds,$oddsType);
  434. $oddsData = lm($model_odds, 'Sports')
  435. ->select('sort','p_code','id','odds_only','odds_code',$model_odds.'.status','odds','condition','sort','source','utime')
  436. ->where($this->source)
  437. ->where([$model_odds.'.match_id'=>$matchID,$model_odds.'.type'=>0])//,$model_odds.'.expire_time'=>$utime
  438. ->orderBy('sort', 'desc')
  439. ->orderBy('p_code','desc')
  440. ->orderBy('odds_code','desc')
  441. ->get()->toArray();
  442. $matchData = lm($model_match, 'Sports')
  443. ->join($model_league,$model_league.'.lg_id',$model_match.'.lg_id')
  444. ->select($model_match.'.lg_id','name_chinese','match_id','home_team','guest_team','match_date','match_time',$model_match.'.status')
  445. ->where($model_match.'.source',$this->source['source'])
  446. ->where(['match_id'=>$matchID])
  447. ->first();
  448. if(empty($matchData)) Render(null, '1', lang('Tips','Sports')->get('success'));
  449. $matchData->ptime = '';//赛事进行时间
  450. $matchData->match_score = '';//赛事比分
  451. //如果该赛事正在进行,则获取进行时间
  452. if($matchData->status ==1){
  453. $result = lm($model_result, 'Sports')->select('match_id','match_time','match_score')
  454. ->where($this->source)
  455. ->where(['match_id'=>$matchData->match_id])
  456. ->first();
  457. $matchData->ptime = $result->match_time;
  458. $matchData->match_score = $result->match_score;
  459. }
  460. //赛事已结束
  461. if($matchData->status ==2){
  462. $oddsData = [];
  463. }
  464. //查询当前联赛下的赛事
  465. $league = [];
  466. if($oddsType != 'StRollBall'){
  467. $where = [
  468. [$model_match.'.home_team','<>',null],
  469. [$model_match.'.guest_team','<>',null],
  470. [$model_match.'.status','<',2],
  471. [$model_match.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)],
  472. [$model_match.'.lg_id',$matchData->lg_id],
  473. [$model_league.'.name_chinese','!=',''],
  474. ];
  475. if($oddsType !='home'){
  476. $getState = $this->commonFunction->getState($oddsType,$model_match);
  477. }else{
  478. $getState = [];
  479. }
  480. $league = lm($model_match,'Sports')
  481. ->join($model_league,$model_league.'.lg_id',$model_match.'.lg_id')
  482. ->select($model_match.'.match_id',$model_match.'.home_team',$model_match.'.guest_team',$model_match.'.status',$model_match.'.match_date',$model_match.'.match_time')
  483. ->where($where)
  484. ->where($getState)
  485. ->get()
  486. ->toarray();
  487. }
  488. $list = array();
  489. foreach ($oddsData as $key=>$item){
  490. $item['team'] = '';
  491. if(strpos($item['odds_code'],'home') !== false) {
  492. $item['team'] = 'home';
  493. }
  494. if(strpos($item['odds_code'],'guest') !== false) {
  495. $item['team'] = 'guest';
  496. }
  497. $list[$key] = $item;
  498. }
  499. $p_code_array = [];
  500. foreach ($oddsData as $key =>$val){
  501. $p_code_array[] = $val['p_code'];
  502. }
  503. $p_code_array = array_keys(array_flip(array_unique($p_code_array)));//p_code 去重排序
  504. $data = [
  505. 'lg_id' =>$matchData->lg_id,
  506. 'leagueName' =>$matchData->name_chinese,
  507. 'match_id'=>$matchData->match_id,
  508. 'home_team'=>$matchData->home_team,
  509. 'guest_team'=>$matchData->guest_team,
  510. 'match_time'=>$matchData->match_date." ".$matchData->match_time,
  511. 'match_ptime'=>$matchData->ptime,
  512. 'match_score'=>$matchData->match_score,
  513. 'p_code_array'=> $p_code_array,
  514. 'oddsData'=>$oddsData,
  515. 'league' => $league,
  516. ];
  517. Render($data, '1', lang('Tips','Sports')->get('success'));
  518. } catch (\Exception $e) {
  519. echo $e->getMessage();
  520. }
  521. }
  522. /**
  523. * 手动 更新 赛事状态
  524. */
  525. public function updateMatch () {
  526. $game_code = $_REQUEST['game_code'];
  527. $match_date = $_REQUEST['match_date'];
  528. try {
  529. if(empty($game_code)){
  530. throw new \Exception(Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR')));
  531. }
  532. if(empty($match_date)) $match_date = date('Y-m-d');
  533. //根据球类代码 获取相关model
  534. $models = $this->commonFunction->getModels($game_code);
  535. $model_match = $models['model_match'];
  536. $matchData = lm($model_match,'Sports')->select('match_id','status','match_date','match_time')->where(['match_date'=>$match_date])->get();
  537. foreach ($matchData as $key=>$item){
  538. $time = strtotime($item->match_date.$item->match_time);
  539. if(($time+(60*90)) < time()){ //更新为 已结束
  540. lm($model_match,'Sports')->where(['match_id'=>$item->match_id])->update(['status'=>2]);
  541. }
  542. if(($time+(60*90)) > time() and $time < time()){ //更新为 进行中
  543. lm($model_match,'Sports')->where(['match_id'=>$item->match_id])->update(['status'=>1]);
  544. }
  545. }
  546. Render([], '1', lang('Tips','Sports')->get('success'));
  547. } catch (\Exception $e) {
  548. echo $e->getMessage();
  549. }
  550. }
  551. /**
  552. * 获取即将开赛 所有赛事
  553. * 首页使用
  554. */
  555. public function getSoon(){
  556. $source = $this->source;//数据源 条件
  557. try {
  558. $where = $this->commonFunction->getState('StSoon');
  559. $data = $this->getTypeData->getAllSoon($source,$where);
  560. Render($data, '1', lang('Tips','Sports')->get('success'));
  561. } catch (\Exception $e) {
  562. echo $e->getMessage();
  563. }
  564. }
  565. /**
  566. * 获取球类-玩法-赔率代码
  567. */
  568. public function getOddsCode(){
  569. $type = $_REQUEST['type'];
  570. $game_code = $_REQUEST['game_code'];
  571. if($type == 'p_code'){
  572. $p_code = lm('st_odds_code','Sports')
  573. ->select('game_code','odds_code','odds_name')
  574. ->where(['p_id'=>0])
  575. ->get();
  576. }else{
  577. $p_code = lm('st_odds_code','Sports')
  578. ->select('id','game_code','odds_code','odds_name')
  579. ->where('p_id',0)
  580. ->get()->toarray();
  581. $code = lm('st_odds_code','Sports')
  582. ->select('id','p_id','game_code','odds_code','odds_name')
  583. ->get()->toArray();
  584. foreach ($p_code as $k=>$v){
  585. foreach ($code as $kk=>$vv){
  586. if($v['id'] == $vv['p_id']){
  587. $p_code[$k]['below'][] = $vv;
  588. }
  589. }
  590. }
  591. }
  592. Render($p_code, '1', lang('Tips','Sports')->get('success'));
  593. }
  594. /**
  595. * 获取 欧洲冠军杯数据
  596. */
  597. public function getUEFAChampions (){
  598. $data = $this->getTypeData->getUEFAChampions($this->source);
  599. Render($data, '1', lang('Tips','Sports')->get('success'));
  600. }
  601. /**
  602. * 更新赔率数据
  603. */
  604. public function updateOdds(){
  605. $data = $_REQUEST;
  606. // $data = ['game_code'=>'zq','match_id'=>3095448];
  607. $getModels = $this->commonFunction->getModels($data['game_code']);
  608. $model_match = $getModels['model_match'];
  609. $model_odds = $getModels['model_odds'];
  610. $where[] = [$model_match.'.match_id','=',$data['match_id']];
  611. $oddsData = lm($model_match,"Sports")
  612. ->join($model_odds,$model_odds.'.match_id',$model_match.'.match_id')
  613. ->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')
  614. ->where($model_match.'.source',$this->source)
  615. ->where($model_odds.'.type',0)
  616. ->where($where)
  617. // ->where($model_odds.'.expire_time','>',date("Y-m-d H:i:s"))
  618. ->get()->toArray();
  619. $p_code_array = [];
  620. foreach ($oddsData as $key =>$val){
  621. $p_code_array[$val['p_code']] = lang('OddsTemp','Sports')->get($val['p_code']);
  622. }
  623. $data = [
  624. 'oddsData'=>$oddsData,
  625. 'p_code_array'=>$p_code_array
  626. ];
  627. Render($data, '1', lang('Tips','Sports')->get('success'));
  628. }
  629. /**
  630. * 获取赛事所有数据
  631. * @param [array] $where 条件
  632. *@param [array] $Orwhere 或者条件
  633. * @param [type] $game_code 球类代码
  634. * @return [array] $data
  635. */
  636. public function getMatchInfo($where=[],$Orwhere=[],$where_search=[],$game_code ='zq'){
  637. $getModels = $this->commonFunction->getModels($game_code);
  638. $st_league = $getModels['model_league'];
  639. $st_competition = $getModels['model_match'];
  640. try{
  641. //查询今日赛事
  642. $data = lm($st_competition,'Sports')
  643. ->join($st_league,$st_league.'.lg_id',$st_competition.'.lg_id')
  644. ->leftjoin('st_area_country as country','country.id',$st_league.'.country_id')
  645. ->leftjoin('st_area_country as area','area.id',$st_league.'.area_id')
  646. ->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')
  647. ->where($where)
  648. ->where($Orwhere)
  649. ->where($where_search)
  650. ->where([
  651. [$st_competition.'.home_team','<>',null],
  652. [$st_competition.'.guest_team','<>',null],
  653. [$st_league.'.name_chinese','<>',null],
  654. [$st_competition.'.status','<',2],
  655. [$st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)]
  656. ])
  657. ->distinct($st_competition.'.match_id')
  658. ->get()
  659. ->toarray();
  660. $data = $this->division($data);
  661. return $data;
  662. } catch (\Exception $e) {
  663. echo $e->getMessage();
  664. }
  665. }
  666. /**
  667. * 数据分割器
  668. * @param [array] $matchData 赛事数据
  669. * @param [type] $type 分割类型 1:以联赛分
  670. * @return [array] $league
  671. */
  672. public function division(array $matchData)
  673. {
  674. //联赛分割
  675. $league = [];
  676. foreach ($matchData as $k => $v) {
  677. $league[$k]['name_chinese'] = $v['name_chinese'];
  678. $league[$k]['lg_id'] = $v['lg_id'];
  679. }
  680. //联赛去重
  681. $league = array_unique($league, SORT_REGULAR);
  682. //根据联赛分类
  683. foreach ($league as $key => $vlue) {
  684. foreach ($matchData as $k => $v) {
  685. if ($vlue['name_chinese'] == $v['name_chinese']) {
  686. $league[$key]['match_info'][$k] = $v;
  687. }
  688. }
  689. }
  690. //统计联赛下赛事
  691. foreach ($league as $key => $vlue) {
  692. $league[$key]['match_info'] = array_sort($league[$key]['match_info']);
  693. $league[$key]['count'] = count($vlue['match_info']);
  694. }
  695. return $league;
  696. }
  697. /**
  698. * 获取赛事赔率数据
  699. * @param [array] $where 条件
  700. *@param [array] $Orwhere 或者条件
  701. * @param [type] $game_code 球类代码
  702. * @return [array] $data
  703. */
  704. private function getOdds(array $data,$st_odds){
  705. //循环获取赛事赔率
  706. foreach ($data as $key=>$item){
  707. $data[$key]['oddsData'] = lm($st_odds, 'Sports')
  708. ->select('id','p_code','odds_code','status','odds','condition','sort')
  709. ->where($this->source)
  710. ->where(['match_id'=>$item['match_id'],'type'=>0])
  711. ->where(function($query)use ($st_odds){
  712. $query->where($st_odds.'.odds_code','concede_home')
  713. ->orWhere(function($query)use ($st_odds){
  714. $query->where($st_odds.'.odds_code','concede_guest');
  715. })
  716. ->orWhere(function($query)use ($st_odds){
  717. $query->where($st_odds.'.odds_code','size_home');
  718. })
  719. ->orWhere(function($query)use ($st_odds){
  720. $query->where($st_odds.'.odds_code','size_guest');
  721. });
  722. })
  723. ->get()->toArray();
  724. if(!empty($data[$key]['oddsData'])){
  725. //根据 排序 获取 最新让球/大小玩法赔率
  726. $sortData = array_column($data[$key]['oddsData'],'sort');
  727. array_multisort($sortData,SORT_DESC,$data[$key]['oddsData']);
  728. $data[$key]['oddsData'] = array_slice($data[$key]['oddsData'],0,4);//前四条 放入
  729. }else{
  730. $data[$key]['oddsData'] = [];
  731. }
  732. }
  733. return $data;
  734. }
  735. /**
  736. * 获取赛事总数
  737. * @param [array] $where 条件
  738. *@param [array] $Orwhere 或者条件
  739. * @param [type] $game_code 球类代码
  740. * @return [int] $dataCount
  741. */
  742. public function getMatchCount($where=[],$Orwhere=[],$where_search=[],$game_code ='zq'){
  743. $getModels = $this->commonFunction->getModels($game_code);
  744. $st_league = $getModels['model_league'];
  745. $st_competition = $getModels['model_match'];
  746. //查询今日赛事
  747. $dataCount = lm($st_competition,'Sports')
  748. ->join($st_league,$st_league.'.lg_id',$st_competition.'.lg_id')
  749. ->leftjoin('st_area_country as country','country.id',$st_league.'.country_id')
  750. ->leftjoin('st_area_country as area','area.id',$st_league.'.area_id')
  751. ->where($where)
  752. ->where($Orwhere)
  753. ->where($where_search)
  754. ->where([
  755. [$st_competition.'.home_team','<>',null],
  756. [$st_competition.'.guest_team','<>',null],
  757. [$st_league.'.name_chinese','<>',null],
  758. [$st_competition.'.status','<',2],
  759. [$st_competition.'.us_time','>',qgmdate('Y-m-d H:i:s', '', -4)]
  760. ])
  761. ->distinct($st_competition.'.match_id')
  762. ->count('*');
  763. return $dataCount;
  764. }
  765. }