WriteSportsController.php 33 KB

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