WriteSportsController.php 26 KB

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