WriteSportsController.php 25 KB

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