WriteSportsController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Jun.peng
  5. * Date: 2019/5/13
  6. * Time: 10:19
  7. */
  8. namespace App\Http\Controllers\Api;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Routing\Controller as BaseController;
  11. use Illuminate\Http\Request as Req;
  12. use App\Http\Response\Response;
  13. use App\Lib\Biz\Sport\Common as commonFunction;
  14. use App\Http\Model\St_area_country as St_area_countryModel;
  15. use App\Http\Model\StBroadcast as broadcastModel;
  16. use App\Http\Model\StGameType as gameModel;
  17. class WriteSportsController extends BaseController{
  18. /**
  19. * 体育数据入库接口
  20. */
  21. public function setSports(Req $data){
  22. try {
  23. //开启事务
  24. DB::beginTransaction();
  25. if($data->game_code){
  26. //用于后台 将所有进行中的赛事写入结果
  27. $this->match_result($data->game_code);
  28. }
  29. //写赛事数据
  30. $obt = $data->data;
  31. if($obt){
  32. $getData = $this->getAddData($obt);
  33. $league = [];
  34. $competition = [];
  35. $odds = [];
  36. $league_result = [];
  37. $match_result = [];
  38. $match_result_record = [];
  39. $odds_record = [];
  40. $broadcast = [];
  41. //指定排序
  42. foreach ($getData as $k=>$v){
  43. if($v['title'] == 'league') $league[] = $v;
  44. if($v['title'] == 'competition') $competition[] = $v;
  45. if($v['title'] == 'odds') $odds[] = $v;
  46. if($v['title'] == 'league_result') $league_result[] = $v;
  47. if($v['title'] == 'match_result') $match_result[] = $v;
  48. if($v['title'] == 'match_result_record') $match_result_record[] = $v;
  49. if($v['title'] == 'odds_record') $odds_record[] = $v;
  50. if($v['title'] == 'broadcast') $broadcast[] = $v;
  51. }
  52. $matchData = [$league,$competition,$odds,$league_result,$match_result,$match_result_record,$odds_record,$broadcast];
  53. //排空处理
  54. foreach ($matchData as $k=>$v){
  55. if($v == []) unset($matchData[$k]);
  56. }
  57. //根据顺序写入数据
  58. foreach ($matchData as $k=>$v){
  59. foreach ($v as $kk =>$vv){
  60. switch ($vv['title']){
  61. case 'area'://地区
  62. throw new \Exception(Response::generate(Response::AUTH_ERROR)) ;
  63. break;
  64. case 'country'://国家
  65. throw new \Exception(Response::generate(Response::AUTH_ERROR));
  66. break;
  67. case 'league'://联赛
  68. $this->league($vv);
  69. break;
  70. case 'competition'://赛事
  71. $this->competition($vv);
  72. break;
  73. case 'odds'://赔率
  74. $this->odds($vv);
  75. break;
  76. case 'league_result'://联赛结果
  77. $this->league_result($vv);
  78. break;
  79. case 'match_result'://赛事结果
  80. throw new \Exception(Response::generate('',Response::AUTH_ERROR));
  81. $this->match_result($vv);
  82. break;
  83. case 'match_result_record'://赛事结果记录
  84. $this->com_result_record($vv);
  85. break;
  86. case 'odds_record'://赔率记录
  87. throw new \Exception(Response::generate('',Response::AUTH_ERROR));
  88. $this->odds_record($vv);
  89. break;
  90. case 'broadcast'://直播数据
  91. $this->broadcast($vv);
  92. break;
  93. default:
  94. throw new \Exception(Response::generate('',Response::ABNORMAL)) ;
  95. }
  96. }
  97. }
  98. }
  99. //提交事务
  100. DB::commit();
  101. return Response::success();
  102. } catch (\Exception $e) {
  103. //回滚事务
  104. DB::rollBack();
  105. return $e->getMessage();
  106. }
  107. }
  108. //计算滚球 赛事进行时间
  109. private function secTime($sec=0){
  110. $min = floor($sec/60);
  111. $res = $min.':'.($sec-$min*60);
  112. return $res;
  113. }
  114. //写入直播 数据
  115. public function broadcast($opt = []){
  116. $data = $opt['data'];
  117. $set_broadcast = [
  118. "doing" => $data['doing'],
  119. "game_type" => $data['game_type'],
  120. "guest_team" => $data['guest_team'],
  121. "host_team" => $data['host_team'],
  122. "league_name" => $data['league_name'],
  123. "shower" => $data['shower'],
  124. "showid" => (int)$data['showid'],
  125. "start_time" => $data['start_time'],
  126. "ctime" =>date('Y-m-d H:m:i'),
  127. "utime" =>date('Y-m-d H:m:i'),
  128. ];
  129. $ret = broadcastModel::insert($set_broadcast);
  130. if($ret == false) throw new \Exception(Response::generate('',Response::BROADCAST_ERROR));
  131. }
  132. //将进行中赛事写入 赛事结果
  133. public function match_result($game_code = ''){
  134. $model =commonFunction::getModels($game_code,1);
  135. //获取赛事表所有滚球
  136. $matchData = $model['model_match']::select('id','home_team','guest_team','lg_id','status','tag','match_time')
  137. ->where(['status'=> 1])
  138. ->get()
  139. ->toarray();
  140. //没有滚球数据,无需操作
  141. if(empty($matchData)) return Response::success();
  142. //获取赛事结果表 所有滚球
  143. $matchData_r = $model['model_result']::select('match_id')
  144. ->where(['status'=> 1])
  145. ->get()
  146. ->toarray();
  147. //没有滚球结果,直接插入结果表
  148. if(empty($matchData_r)){
  149. foreach ($matchData as $k=>$v){
  150. $time = time()-strtotime($v['match_time']);
  151. $match_time = $this->secTime($time);
  152. $set_match_r[] = [
  153. "match_id"=> $v['id'],
  154. "home_team"=>$v['home_team'],
  155. "guest_team"=>$v['guest_team'],
  156. "lg_id"=>$v['lg_id'],
  157. "status"=>$v['status'],
  158. "tag"=> $v['tag'],
  159. 'match_time'=>$match_time,
  160. "update_time"=>date('Y-m-d H:m:i')
  161. ];
  162. }
  163. $ret = $model['model_result']::insert($set_match_r);
  164. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));
  165. }
  166. //如果结果表有数据,则获取结果表没有的赛事
  167. foreach ($matchData as $k=>$v){
  168. foreach ($matchData_r as $kk=>$vv){
  169. if($v['id'] == $vv['match_id']){
  170. unset($matchData[$k]);
  171. }
  172. }
  173. }
  174. //如果还有未写入赛事
  175. if(!empty($matchData)){
  176. //写入结果表不存在赛事
  177. foreach ($matchData as $k=>$v){
  178. $time = time()-strtotime($v['match_time']);
  179. $match_time = $this->secTime($time);
  180. $set_match_r[] = [
  181. "match_id"=> $v['id'],
  182. "home_team"=>$v['home_team'],
  183. "guest_team"=>$v['guest_team'],
  184. "lg_id"=>$v['lg_id'],
  185. "status"=>$v['status'],
  186. "tag"=> $v['tag'],
  187. 'match_time'=>$match_time,
  188. "update_time"=>date('Y-m-d H:m:i')
  189. ];
  190. }
  191. $ret = $model['model_result']::insert($set_match_r);
  192. if($ret != true) throw new \Exception( Response::generate('',Response::ADD_MATCH_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
  193. }
  194. }
  195. //写入地区数据 弃用
  196. public function area($opt = []){
  197. $ret = lm('st_area','Sports')->insert($opt);
  198. return $ret;
  199. }
  200. //写入国家数据 弃用
  201. public function country($opt = []){
  202. $ret = lm('st_country','Sports')->insert($opt);
  203. return $ret;
  204. }
  205. //写入联赛数据
  206. public function league($opt = []){
  207. $game_code = $opt['game_code'];
  208. //根据球类代码获取相关model
  209. $model =commonFunction::getModels($game_code,1);
  210. $data = $opt['data'];
  211. // if(empty($data['belong'])) throw new \Exception(Response::generate(Response::COUNTRY_ERROR)) ;//Render([], '10013', lang('Tips','Sports')->get('country_error'));
  212. // //获取联赛所属 国家/地区id
  213. // $belong = St_area_countryModel::getID($data['belong']);
  214. $set_lg['area_id'] = 0;//$belong['area_id'];
  215. $set_lg['country_id'] = 0;// $belong['country_id'];
  216. //查询中间表 是否已记录
  217. $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  218. ->value('lg_id');
  219. //如果没有记录
  220. if(empty($lg_id)){
  221. //查询联赛是否已存在
  222. $id = $model['model_league']::where('name_chinese','=',$data['name_chinese'])
  223. ->value('id');
  224. $last_time = '2019-12-31 23:59:59';
  225. //决赛时间
  226. if($data['last_time']){
  227. $last_time = $data['last_time'];
  228. }
  229. if(empty($id)){
  230. $set_lg['name_chinese'] = $data['name_chinese'];
  231. $set_lg['kind'] = $data['kind'];
  232. $set_lg['match_mode'] = $data['match_mode'];
  233. $set_lg['if_stop'] = $data['if_stop'];
  234. $set_lg['last_time'] = $last_time;
  235. $set_lg['utime'] = date('Y-m-d H:m:i');
  236. $id = $model['model_league']::insertGetId($set_lg);
  237. $m_lg_id = $id;
  238. if($m_lg_id < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'联赛-lg_id:'.$data['lg_id'].';',Response::INSERT_ERROR));
  239. }
  240. $set_local = [
  241. 'lg_id'=>$id,
  242. 'others_lg_id'=>$data['lg_id'],
  243. 'source'=>$data['source'],
  244. ];
  245. $ret = $model['model_local_league']::insertGetId($set_local);
  246. if($ret < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'联赛-lg_id:'.$data['lg_id'].';',Response::LOCAL_LEAGUE_ERROR)) ;//Render([], '10017', lang('Tips','Sports')->get('local_league_error'));
  247. }
  248. }
  249. //写入赛事数据
  250. public function competition($opt = []){
  251. $game_code = $opt['game_code'];
  252. //根据球类代码获取相关model
  253. $model = commonFunction::getModels($game_code,1);
  254. $data = $opt['data'];
  255. if(empty($data['lg_id'])) throw new \Exception(Response::generate('',Response::LEAGUE_ERROR)) ;//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  256. //根据源联赛ID 获取本地关联id
  257. $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  258. ->value('lg_id');
  259. if(empty($lg_id)) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'联赛-lg_id:'.$data['lg_id'].';',Response::LEAGUE_ERROR)) ;//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  260. //查询关联记录是否存在
  261. $match_id = $model['model_local_match']::where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
  262. ->value('match_id');
  263. if(empty($match_id)){
  264. //查询赛事是否存在
  265. $id = $model['model_match']::where(['home_team'=>$data['home_team'],'guest_team'=>$data['guest_team'],'match_date'=>$data['match_date'],'match_time'=>$data['match_time']])
  266. ->value('id');
  267. $half_match_id = 0;
  268. //如果有上半场赛事id 获取上半场赛事是否存在
  269. if(!empty($data['half_match_id'])){
  270. $half_match_id = $match_id = $model['model_local_match']::where(['others_match_id'=>$data['half_match_id'],'source'=>$data['source']])
  271. ->value('match_id');
  272. if(empty($half_match_id)) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$data['match_id'].';',Response::HALF_MATCH_ERROR)) ;//Render([], '10024', lang('Tips','Sports')->get('half_match_error'));
  273. }
  274. //写入赛事
  275. if(empty($id)){
  276. //如果赛事没有开始日期,则为冠军盘口赛事
  277. if(empty($data['match_date'])){
  278. //冠军盘口赛事获取所属联赛结束时间
  279. $last_time = $model['model_league']::where(['id'=>$lg_id])
  280. ->value('last_time');
  281. if(empty($last_time)) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'联赛-lg_id:'.$data['lg_id'].';',Response::LG_LASTTIME_ERROR)) ;//Render([], '10023', lang('Tips','Sports')->get('lg_lastTime_error'));
  282. //给冠军盘口赛事 赋值时间
  283. $time = strtotime($last_time);
  284. $data['match_date'] = date('Y-m-d',$time);
  285. $data['match_time'] = date('H:m:i',$time);
  286. }
  287. $set_match = [
  288. 'ctime'=>date('Y-m-d H:m:i'),
  289. 'utime'=>date('Y-m-d H:m:i'),
  290. 'expire_time'=>date('Y-m-d H:m:i',time()+60),
  291. 'home_team'=>$data['home_team']?:'',
  292. 'guest_team'=>$data['guest_team']?:'no_team',
  293. 'lg_id'=>$lg_id,
  294. 'status'=>$data['status'],
  295. 'match_date'=>$data['match_date']?:'',
  296. 'match_time'=>$data['match_time']?:'',
  297. 'tag'=>$data['tag']?:0,
  298. 'is_rollball'=>$data['is_rollball']?:0,
  299. 'is_today'=>$data['is_today']?:0,
  300. 'is_morningplate'=>$data['is_morningplate']?:0,
  301. 'is_stringscene'=>$data['is_stringscene']?:0,
  302. 'us_time'=>$data['us_time']?:'',
  303. 'half_match_id'=>$half_match_id?:0,
  304. ];
  305. //写入赛事 返回id
  306. $id = $model['model_match']::insertGetId($set_match);
  307. if($id < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$data['match_id'].';',Response::INSERT_ERROR)) ;//Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  308. }
  309. $set_local = [
  310. 'match_id'=>$id,
  311. 'others_match_id'=>$data['match_id'],
  312. 'source'=>$data['source'],
  313. ];
  314. $ret = $model['model_local_match']::insertGetId($set_local);
  315. if($ret < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$data['match_id'].';',Response::LOCAL_MATCH_ERROR)) ;//Render([], '10018', lang('Tips','Sports')->get('local_match_error'));
  316. }
  317. // return Response::success();
  318. }
  319. //写入赔率数据
  320. public function odds($opt){
  321. $game_code = $opt['game_code'];
  322. //根据球类代码获取相关model
  323. $model = commonFunction::getModels($game_code,1);
  324. $data = $opt['data'];
  325. $match = $model['model_local_match']::select('match_id','others_match_id')
  326. ->where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
  327. ->first();
  328. if(count($match) < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$data['match_id'].';',Response::MATCH_ERROR)) ;//Render([], '10016', lang('Tips','Sports')->get('match_error'));
  329. //获取赛事 本地/源ID
  330. $others_match_id = $match->others_match_id;
  331. $match_id = $match->match_id;
  332. $lg = $model['model_local_league']::select('lg_id','others_lg_id')
  333. ->where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  334. ->first();
  335. if(count($lg) < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'联赛-lg_id:'.$data['lg_id'].';',Response::LEAGUE_ERROR));
  336. //获取联赛 本地/源ID
  337. $others_lg_id = $lg->others_lg_id;
  338. $lg_id = $lg->lg_id;
  339. //===写赔率记录===
  340. //查询赔率记录是否存在
  341. $oddsRecordID = $model['model_odds_record']::where(['odds_only'=>$data['odds_only']])
  342. ->value('id');
  343. $set_odds_r = [
  344. 'match_id'=> $match_id,
  345. 'others_match_id'=> $others_match_id,
  346. 'odds_code'=> $data['odds_code'],
  347. 'status'=> $data['status'],
  348. 'sort'=> $data['sort'],
  349. 'p_code'=> $data['p_code'],
  350. 'odds'=> $data['odds'],
  351. 'condition'=> $data['condition'],
  352. 'odds_only'=> $data['odds_only'],
  353. 'sole'=> $data['sole'],
  354. 'source'=> $data['source'],
  355. 'type'=> $data['type'],
  356. 'team'=> $data['team'],
  357. 'lg_id'=> $lg_id,
  358. 'others_lg_id'=> $others_lg_id,
  359. 'ctime'=> date('Y-m-d H:m:i'),
  360. 'utime'=> date('Y-m-d H:m:i'),
  361. ];
  362. //更新或写入赔率记录
  363. if(!empty($oddsID)){
  364. $ret = $model['model_odds_record']::where(['id'=>$oddsRecordID])
  365. -> update($set_odds_r);
  366. if($ret < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赔率记录-odds_only:'.$data['odds_only'].';',Response::ADD_ODDS_R_ERROR)) ;//Render([], '10020', lang('Tips','Sports')->get('add_odds_r_error'));
  367. }else{
  368. $ret = $model['model_odds_record']::insert($set_odds_r);
  369. if($ret != true) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赔率记录-odds_only:'.$data['odds_only'].';',Response::ADD_ODDS_R_ERROR));//Render([], '10020', lang('Tips','Sports')->get('add_odds_r_error'));
  370. }
  371. //===写赔率===
  372. //查询 赔率数据是否存在
  373. $oddsID = $model['model_odds']::where(['sole'=>$data['sole']])
  374. ->value('id');
  375. $set_odds = [
  376. 'match_id'=> $match_id,
  377. 'others_match_id'=> $others_match_id,
  378. 'odds_code'=> $data['odds_code'],
  379. 'status'=> $data['status'],
  380. 'sort'=> $data['sort'],
  381. 'p_code'=> $data['p_code'],
  382. 'odds'=> $data['odds'],
  383. 'condition'=> $data['condition'],
  384. 'odds_only'=> $data['odds_only'],
  385. 'sole'=> $data['sole'],
  386. 'source'=> $data['source'],
  387. 'type'=> $data['type'],
  388. 'team'=> $data['team'],
  389. 'lg_id'=> $lg_id,
  390. 'others_lg_id'=> $others_lg_id,
  391. 'ctime'=> date('Y-m-d H:m:i'),
  392. 'utime'=> date('Y-m-d H:m:i'),
  393. 'expire_time'=>date('Y-m-d H:m:i',time()+60),
  394. ];
  395. //更新或写入赔率数据
  396. if(!empty($oddsID)){
  397. $ret = $model['model_odds']::where(['sole'=>$data['sole']])
  398. -> update($set_odds);
  399. if($ret < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赔率-odds_only:'.$data['odds_only'].';',Response::ADD_ODDS_ERROR));//Render([], '10019', lang('Tips','Sports')->get('add_odds_error'));
  400. }else{
  401. $ret = $model['model_odds']::insert($set_odds);
  402. if($ret != true) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赔率-odds_only:'.$data['odds_only'].';',Response::ADD_ODDS_ERROR));//Render([], '10019', lang('Tips','Sports')->get('add_odds_error'));
  403. }
  404. //===end===
  405. }
  406. //写入联赛结果
  407. public function league_result($opt){
  408. $game_code = $opt['game_code'];
  409. //根据球类代码获取相关model
  410. $model = commonFunction::getModels($game_code,1);
  411. $data = $opt['data'];
  412. //验证结果所属联赛
  413. $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  414. ->value('lg_id');
  415. if($lg_id < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'联赛-lg_id:'.$data['lg_id'].';',Response::LEAGUE_ERROR));//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  416. $lg_result_id = $model['model_league_result']::where(['lg_id'=>$lg_id,'game_name'=>$data['game_name']])
  417. ->value('id');
  418. $set_lg_result = [
  419. 'lg_id'=>$lg_id,
  420. 'game_name'=>$data['game_name'],
  421. 'result'=>json_encode($data['result'],JSON_UNESCAPED_UNICODE),
  422. 'status'=>$data['status'],
  423. 'ctime'=> date('Y-m-d H:m:i'),
  424. 'utime'=> date('Y-m-d H:m:i'),
  425. ];
  426. //联赛结果数据处理
  427. if(!empty($lg_result_id)){
  428. $ret = $model['model_league_result']::where(['id'=>$lg_result_id])
  429. -> update($set_lg_result);
  430. if($ret < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'联赛-lg_id:'.$data['lg_id'].';',Response::ADD_LG_R_ERROR)) ;//Render([], '10021', lang('Tips','Sports')->get('add_lg_r_error'));
  431. }else{
  432. $ret = $model['model_league_result']::insert($set_lg_result);
  433. if($ret != true) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'联赛-lg_id:'.$data['lg_id'].';',Response::ADD_LG_R_ERROR));//Render([], '10021', lang('Tips','Sports')->get('add_lg_r_error'));
  434. }
  435. // return Response::success();
  436. }
  437. //写入赛事结果
  438. public function com_result($opt){
  439. $game_code = $opt['game_code'];
  440. //根据球类代码获取相关model
  441. $model = commonFunction::getModels($game_code,1);
  442. $data = $opt['data'];
  443. //验证结果所属联赛
  444. $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  445. ->value('lg_id');
  446. if($lg_id < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'联赛-lg_id:'.$data['lg_id'].';',Response::LEAGUE_ERROR)) ;//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  447. //验证结果所属赛事
  448. $match_id = $model['model_local_match']::where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
  449. ->value('match_id');
  450. if($match_id < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$data['match_id'].';',Response::MATCH_ERROR));//Render([], '10016', lang('Tips','Sports')->get('match_error'));
  451. //查询结果是否存在
  452. $match_r_id = $model['model_result']::where(['match_id'=>$match_id])
  453. ->value('id');
  454. $set_match_r = [
  455. "home_team"=>$data['home_team'],
  456. "guest_team"=>$data['guest_team'],
  457. "lg_id"=>$lg_id,
  458. "home_rate"=> $data['home_rate'],
  459. "guest_rate"=> $data['guest_rate'],
  460. "home_score"=> $data['home_score'],
  461. "guest_score"=> $data['guest_score'],
  462. "all_goal"=> $data['all_goal'],
  463. "status"=>$data['status'],
  464. "first_score"=>$data['first_score'],
  465. "last_score"=> $data['last_score'],
  466. "match_score"=> $data['match_score'],
  467. "match_winer"=> $data['match_winer'],
  468. "match_time"=> $data['match_time'],
  469. "match_process"=> $data['match_process'],
  470. "tag"=> $data['tag'],
  471. "match_id"=> $match_id,
  472. "u_home_score"=> $data['u_home_score'],
  473. "u_guest_score"=> $data['u_guest_score'],
  474. "p_code"=> $data['p_code'],
  475. "update_time"=>date('Y-m-d H:m:i')
  476. ];
  477. //赛事结果数据处理
  478. if(!empty($match_r_id)){
  479. $ret = $model['model_result']::where(['id'=>$match_r_id])
  480. -> update($set_match_r);
  481. if($ret < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_ERROR)) ;//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
  482. }else{
  483. $ret = $model['model_result']::insert($set_match_r);
  484. if($ret != true) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_ERROR)) ;//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
  485. }
  486. // return Response::success();
  487. }
  488. //写入赛事结果记录
  489. public function com_result_record($opt){
  490. $game_code = $opt['game_code'];
  491. //根据球类代码获取相关model
  492. $model = commonFunction::getModels($game_code,1);
  493. $data = $opt['data'];
  494. //验证结果所属联赛
  495. $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  496. ->value('lg_id');
  497. if($lg_id < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'联赛-lg_id:'.$data['lg_id'].';',Response::LEAGUE_ERROR)) ;//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  498. //验证结果所属赛事
  499. $match_id = $model['model_local_match']::where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
  500. ->value('match_id');
  501. if($match_id < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$data['match_id'].';',Response::MATCH_ERROR));//Render([], '10016', lang('Tips','Sports')->get('match_error'));
  502. $match_r_id = $model['model_result_record']::where(['match_id'=>$match_id,'match_time'=>$data['match_time']])
  503. ->value('id');
  504. $set_match_r = [
  505. "home_team"=>$data['home_team'],
  506. "guest_team"=>$data['guest_team'],
  507. "lg_id"=>$lg_id,
  508. "home_rate"=> $data['home_rate'],
  509. "guest_rate"=> $data['guest_rate'],
  510. "home_score"=> $data['home_score'],
  511. "guest_score"=> $data['guest_score'],
  512. "all_goal"=> $data['all_goal'],
  513. "status"=>$data['status'],
  514. "first_score"=>$data['first_score'],
  515. "last_score"=> $data['last_score'],
  516. "match_score"=> $data['match_score'],
  517. "match_winer"=> $data['match_winer'],
  518. "match_time"=> $data['match_time'],
  519. "match_process"=> $data['match_process'],
  520. "tag"=> $data['tag'],
  521. "match_id"=> $match_id,
  522. "p_code"=> $data['p_code'],
  523. "update_time"=>date('Y-m-d H:m:i')
  524. ];
  525. //赛事结果记录处理
  526. if($match_r_id > 0){
  527. $ret = $model['model_result_record']::where(['id'=>$match_r_id])
  528. ->update($set_match_r);
  529. if($ret < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_r_error'));
  530. }else{
  531. $ret = $model['model_result_record']::insert($set_match_r);
  532. if($ret != true) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_r_error'));
  533. }
  534. // return Response::success();
  535. }
  536. //写入赔率记录
  537. public function odds_record($opt){
  538. $game_code = $opt['game_code'];
  539. //根据球类代码获取相关model
  540. $model = commonFunction::getModels($game_code,1);
  541. $data = $opt['data'];
  542. $match = $model['model_local_match']::select('match_id','others_match_id')
  543. ->where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
  544. ->first();
  545. if(count($match) < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$data['match_id'].';',Response::MATCH_ERROR)) ;//Render([], '10016', lang('Tips','Sports')->get('match_error'));
  546. //获取赛事 本地/源ID
  547. $others_match_id = $match->others_match_id;
  548. $match_id = $match->match_id;
  549. $lg = $model['model_local_league']::select('lg_id','others_lg_id')
  550. ->where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  551. ->first();
  552. if(count($lg) < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'联赛-lg_id:'.$data['lg_id'].';',Response::LEAGUE_ERROR)) ;//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  553. //获取联赛 本地/源ID
  554. $others_lg_id = $lg->others_lg_id;
  555. $lg_id = $lg->lg_id;
  556. //查询 赔率数据是否存在
  557. $oddsID = $model['model_odds_record']::where(['odds_only'=>$data['odds_only']])
  558. ->value('id');
  559. $set_odds = [
  560. 'match_id'=> $match_id,
  561. 'others_match_id'=> $others_match_id,
  562. 'odds_code'=> $data['odds_code'],
  563. 'status'=> $data['status'],
  564. 'sort'=> $data['sort'],
  565. 'p_code'=> $data['p_code'],
  566. 'odds'=> $data['odds'],
  567. 'condition'=> $data['condition'],
  568. 'odds_only'=> $data['odds_only'],
  569. 'source'=> $data['source'],
  570. 'type'=> $data['type'],
  571. 'team'=> $data['team'],
  572. 'lg_id'=> $lg_id,
  573. 'others_lg_id'=> $others_lg_id,
  574. 'ctime'=> date('Y-m-d H:m:i'),
  575. 'utime'=> date('Y-m-d H:m:i'),
  576. ];
  577. //更新或写入赔率记录
  578. if(!empty($oddsID)){
  579. $ret = $model['model_odds_record']::where(['id'=>$oddsID])
  580. -> update($set_odds);
  581. if($ret < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赔率记录-odds_only:'.$data['odds_only'].';',Response::ADD_ODDS_R_ERROR)) ;//Render([], '10020', lang('Tips','Sports')->get('add_odds_r_error'));
  582. }else{
  583. $ret = $model['model_odds_record']::insert($set_odds);
  584. if($ret != true) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赔率记录-odds_only:'.$data['odds_only'].';',Response::ADD_ODDS_R_ERROR));//Render([], '10020', lang('Tips','Sports')->get('add_odds_r_error'));
  585. }
  586. // return Response::success();
  587. }
  588. /**
  589. * @param $data
  590. * @return mixed
  591. * json转数组
  592. */
  593. public function getAddData($data){
  594. $data = json_decode($data,true);
  595. return $data;
  596. }
  597. }