WriteSportsController.php 36 KB

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