WriteSportsController.php 33 KB

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