WriteSportsController.php 35 KB

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