WriteSportsController.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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);
  182. break;
  183. case 'match_result'://赛事结果
  184. throw new \Exception(Response::generate('赛事结果数据-match_result:',Response::AUTH_ERROR));
  185. $this->match_result($vv);
  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 'odds_record'://赔率记录
  217. throw new \Exception(Response::generate('赔率记录数据-odds_record:',Response::AUTH_ERROR));
  218. $this->odds_record($vv);
  219. break;
  220. case 'broadcast'://直播数据
  221. $this->broadcast($vv,$game_code);
  222. break;
  223. default:
  224. throw new \Exception(Response::generate('',Response::ABNORMAL)) ;
  225. }
  226. }
  227. }
  228. //提交事务
  229. DB::commit();
  230. return Response::success();
  231. } catch (\Exception $e) {
  232. //回滚事务
  233. DB::rollBack();
  234. return $e->getMessage();
  235. }
  236. }
  237. /**
  238. * @param Req $data
  239. * @return string
  240. * @throws \App\Lib\Biz\Sport\Exception
  241. * 更新赛事状态
  242. */
  243. public function upMatch(Req $data){
  244. try {
  245. //开启事务
  246. DB::beginTransaction();
  247. //获取待更新赛事
  248. $obt = $data->data;
  249. if($obt){
  250. //json转数组
  251. $data = $this->getAddData($obt);
  252. //获取所有数据源赛事 match_id
  253. $others_match_ids = [];
  254. foreach ($data as $k=>$v){
  255. $game_code = $v['game_code'];
  256. $source = $v['source'];
  257. $others_match_ids[] = $v['match_id'];
  258. }
  259. //根据球类代码 获取model
  260. $model =commonFunction::getModels($game_code,1);
  261. //获取所有赛事 match_id
  262. $local_match = $model['model_local_match']::SELECT('others_match_id','match_id')
  263. ->where(['source'=>$source])
  264. ->whereIn('others_match_id',$others_match_ids)
  265. ->get()->toArray();
  266. if(empty($local_match)) throw new \Exception(Response::generate('',Response::MATCHID_NULL));
  267. //更新状态字段
  268. $set_status = ['status'=>2];
  269. foreach ($local_match as $k=>$v){
  270. $ret = $model['model_match']::where(['id'=>$v['match_id']])
  271. -> update($set_status);
  272. if($ret<1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$v['others_match_id'],Response::UPSTATUS_ERROR));
  273. }
  274. }
  275. //提交事务
  276. DB::commit();
  277. return Response::success();
  278. } catch (\Exception $e) {
  279. //回滚事务
  280. DB::rollBack();
  281. return $e->getMessage();
  282. }
  283. }
  284. //写入直播 数据
  285. public function broadcast($opt = [],$game_code=''){
  286. $data = $opt['data'];
  287. $set_broadcast = [
  288. "doing" => $data['doing'],
  289. "game_type" => $data['game_type'],
  290. "game_code" => $game_code,
  291. "guest_team" => $data['guest_team'],
  292. "host_team" => $data['host_team'],
  293. "league_name" => $data['league_name'],
  294. "shower" => $data['shower'],
  295. "showid" => (int)$data['showid'],
  296. "start_time" => $data['start_time'],
  297. "ctime" =>date('Y-m-d H:i:s'),
  298. "utime" =>date('Y-m-d H:i:s'),
  299. ];
  300. $ret = broadcastModel::insert($set_broadcast);
  301. if($ret == false) throw new \Exception(Response::generate('',Response::BROADCAST_ERROR));
  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. public function area($opt = []){
  313. $ret = lm('st_area','Sports')->insert($opt);
  314. return $ret;
  315. }
  316. //写入国家数据 弃用
  317. public function country($opt = []){
  318. $ret = lm('st_country','Sports')->insert($opt);
  319. return $ret;
  320. }
  321. //写入联赛数据
  322. public function league($opt = [],$gameName=''){
  323. $game_code = $opt['game_code'];
  324. //根据球类代码获取相关model
  325. $model =commonFunction::getModels($game_code,1);
  326. $data = $opt['data'];
  327. // if(empty($data['belong'])) throw new \Exception(Response::generate(Response::COUNTRY_ERROR)) ;//Render([], '10013', lang('Tips','Sports')->get('country_error'));
  328. // //获取联赛所属 国家/地区id
  329. // $belong = St_area_countryModel::getID($data['belong']);
  330. $set_lg['area_id'] = 0;//$belong['area_id'];
  331. $set_lg['country_id'] = 0;// $belong['country_id'];
  332. //查询联赛是否已存在
  333. $id = $model['model_league']::where('name_chinese','=',$data['name_chinese'])
  334. ->value('id');
  335. //默认获取本年最后一天
  336. $last_time = date('Y-12-31 23:59:59');
  337. //决赛时间
  338. if($data['last_time']){
  339. $last_time = $data['last_time'];
  340. }
  341. $set_lg['name_chinese'] = $data['name_chinese'];
  342. $set_lg['kind'] = $data['kind'];
  343. $set_lg['match_mode'] = $data['match_mode'];
  344. $set_lg['if_stop'] = $data['if_stop'];
  345. $set_lg['identity'] = $data['uuid'];
  346. $set_lg['last_time'] = $last_time;
  347. $set_lg['utime'] = date('Y-m-d H:i:s');
  348. if(empty($id)){
  349. //写入联赛
  350. $id = $model['model_league']::insertGetId($set_lg);
  351. $m_lg_id = $id;
  352. if($m_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::INSERT_ERROR));
  353. }else{
  354. //更新联赛
  355. $ret = $model['model_league']::where(['id'=>$id])
  356. -> update($set_lg);
  357. if($ret < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::UPDATE_ERROR));
  358. }
  359. $set_local = [
  360. 'lg_id'=>$id,
  361. 'others_lg_id'=>$data['lg_id'],
  362. 'source'=>$data['source'],
  363. 'ctime'=>date('Y-m-d H:i:s')
  364. ];
  365. $ret = $model['model_local_league']::insertGetId($set_local);
  366. 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'));
  367. //执行成功返回源数据 联赛ID
  368. $data = ['others_lg_id'=>$data['lg_id'],'lg_id'=>$id];
  369. return $data;
  370. }
  371. //写入赛事数据
  372. public function competition($opt = [],$gameName='',$lg_id=''){
  373. $game_code = $opt['game_code'];
  374. //根据球类代码获取相关model
  375. $model = commonFunction::getModels($game_code,1);
  376. $data = $opt['data'];
  377. if(empty($data['lg_id'])) throw new \Exception(Response::generate('',Response::LEAGUE_ERROR)) ;//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  378. //查询赛事是否存在
  379. $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']])
  380. ->value('id');
  381. $half_match_id = 0;
  382. //如果有上半场赛事id 获取上半场赛事是否存在
  383. if(!empty($data['half_match_id'])){
  384. $half_match_id = $match_id = $model['model_local_match']::where(['others_match_id'=>$data['half_match_id'],'source'=>$data['source']])
  385. ->value('match_id');
  386. 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'));
  387. }
  388. //如果赛事没有开始日期,则为冠军盘口赛事
  389. if(empty($data['match_date'])){
  390. //冠军盘口赛事获取所属联赛结束时间
  391. $last_time = $model['model_league']::where(['id'=>$lg_id])
  392. ->value('last_time');
  393. 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'));
  394. //给冠军盘口赛事 赋值时间
  395. $time = strtotime($last_time);
  396. $data['match_date'] = date('Y-m-d',$time);
  397. $data['match_time'] = date('H:i:s',$time);
  398. }
  399. $set_match = [
  400. 'ctime'=>date('Y-m-d H:i:s'),
  401. 'utime'=>date('Y-m-d H:i:s'),
  402. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  403. 'home_team'=>$data['home_team']?:'',
  404. 'guest_team'=>$data['guest_team']?:'no_team',
  405. 'lg_id'=>$lg_id,
  406. 'status'=>$data['status'],
  407. 'match_date'=>$data['match_date']?:date('Y-m-d'),
  408. 'match_time'=>$data['match_time']?:date('H:i:s'),
  409. 'tag'=>$data['tag']?:0,
  410. 'is_rollball'=>$data['is_rollball']?:0,
  411. 'is_today'=>$data['is_today']?:0,
  412. 'is_morningplate'=>$data['is_morningplate']?:0,
  413. 'is_stringscene'=>$data['is_stringscene']?:0,
  414. 'us_time'=>$data['us_time']?:commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  415. 'half_match_id'=>$half_match_id?:0,
  416. 'identity' => $data['uuid'],
  417. ];
  418. //写入赛事
  419. if(empty($id)){
  420. //写入赛事 返回id
  421. $id = $model['model_match']::insertGetId($set_match);
  422. if($id < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::INSERT_ERROR)) ;
  423. }else{
  424. //更新赛事
  425. $ret = $model['model_match']::where(['id'=>$id])->update($set_match);
  426. if($ret < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::UPDATE_ERROR)) ;
  427. }
  428. //写关联记录
  429. $set_local = [
  430. 'match_id'=>$id,
  431. 'others_match_id'=>$data['match_id'],
  432. 'source'=>$data['source'],
  433. 'ctime'=>date('Y-m-d H:i:s')
  434. ];
  435. $ret = $model['model_local_match']::insertGetId($set_local);
  436. 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'));
  437. //返回 源数据 赛事ID
  438. $data = ['others_match_id'=>$data['match_id'],'match_id'=>$id];
  439. return $data;
  440. }
  441. //写入赔率数据
  442. public function odds($opt=[],$gameName='',$lg_id=0,$match_id=0,$oddsID='',$oddsRecordID=''){
  443. $game_code = $opt['game_code'];
  444. //根据球类代码获取相关model
  445. $model = commonFunction::getModels($game_code,1);
  446. $data = $opt['data'];
  447. //获取赛事 本地/源ID
  448. $others_match_id = $data['match_id'];
  449. //获取联赛 本地/源ID
  450. $others_lg_id = $data['lg_id'];
  451. //===写赔率记录===
  452. $set_odds_r = [
  453. 'match_id'=> $match_id?:0,
  454. 'others_match_id'=> $others_match_id?:0,
  455. 'odds_code'=> $data['odds_code']?:'',
  456. 'status'=> $data['status']?:0,
  457. 'sort'=> $data['sort']?:0,
  458. 'p_code'=> $data['p_code']?:'',
  459. 'odds'=> $data['odds']?:0,
  460. 'condition'=> $data['condition'],
  461. 'odds_only'=> $data['odds_only']?:'',
  462. 'source'=> $data['source']?:'',
  463. 'type'=> $data['type']?:0,
  464. 'team'=> $data['team']?:'',
  465. 'lg_id'=> $lg_id,
  466. 'others_lg_id'=> $others_lg_id,
  467. 'ctime'=> date('Y-m-d H:i:s'),
  468. ];
  469. //更新或写入赔率记录
  470. if($oddsRecordID != false){
  471. $ret = $model['model_odds_record']::where(['id'=>$oddsRecordID])
  472. -> update($set_odds_r);
  473. 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'));
  474. }else{
  475. $ret = $model['model_odds_record']::insert($set_odds_r);
  476. 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'));
  477. }
  478. //===写赔率===
  479. $set_odds = [
  480. 'match_id'=> $match_id?:0,
  481. 'others_match_id'=> $others_match_id?:0,
  482. 'odds_code'=> $data['odds_code']?:'',
  483. 'status'=> $data['status']?:0,
  484. 'sort'=> $data['sort']?:0,
  485. 'p_code'=> $data['p_code']?:'',
  486. 'odds'=> $data['odds']?:0,
  487. 'condition'=> $data['condition'],
  488. 'odds_only'=> $data['odds_only']?:'',
  489. 'sole'=> $data['sole']?:'',
  490. 'source'=> $data['source']?:'',
  491. 'type'=> $data['type']?:0,
  492. 'team'=> $data['team']?:'',
  493. 'lg_id'=> $lg_id,
  494. 'others_lg_id'=> $others_lg_id,
  495. 'ctime'=> date('Y-m-d H:i:s'),
  496. 'utime'=> date('Y-m-d H:i:s'),
  497. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  498. ];
  499. //更新或写入赔率数据
  500. if($oddsID != false){
  501. $ret = $model['model_odds']::where(['id'=>$oddsID])
  502. -> update($set_odds);
  503. 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'));
  504. }else{
  505. $ret = $model['model_odds']::insert($set_odds);
  506. 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'));
  507. }
  508. //===end===
  509. }
  510. //写入联赛结果
  511. public function league_result($opt){
  512. $game_code = $opt['game_code'];
  513. //根据球类代码获取相关model
  514. $model = commonFunction::getModels($game_code,1);
  515. $data = $opt['data'];
  516. //验证结果所属联赛
  517. $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  518. ->value('lg_id');
  519. 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'));
  520. $lg_result_id = $model['model_league_result']::where(['lg_id'=>$lg_id,'game_name'=>$data['game_name']])
  521. ->value('id');
  522. $set_lg_result = [
  523. 'lg_id'=>$lg_id,
  524. 'game_name'=>$data['game_name'],
  525. 'result'=>json_encode($data['result'],JSON_UNESCAPED_UNICODE),
  526. 'status'=>$data['status'],
  527. 'ctime'=> date('Y-m-d H:i:s'),
  528. 'utime'=> date('Y-m-d H:i:s'),
  529. ];
  530. //联赛结果数据处理
  531. if(!empty($lg_result_id)){
  532. $ret = $model['model_league_result']::where(['id'=>$lg_result_id])
  533. -> update($set_lg_result);
  534. 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'));
  535. }else{
  536. $ret = $model['model_league_result']::insert($set_lg_result);
  537. 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'));
  538. }
  539. // return Response::success();
  540. }
  541. //写入赛事结果
  542. public function com_result($opt=[],$gameName='',$lg_id='',$match_id=''){
  543. $game_code = $opt['game_code'];
  544. //根据球类代码获取相关model
  545. $model = commonFunction::getModels($game_code,1);
  546. $data = $opt['data'];
  547. // //验证结果所属联赛
  548. // $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  549. // ->value('lg_id');
  550. // 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'));
  551. // //验证结果所属赛事
  552. // $match_id = $model['model_local_match']::where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
  553. // ->value('match_id');
  554. // 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'));
  555. //查询结果是否存在
  556. $match_r_id = $model['model_result']::where(['match_id'=>$match_id])
  557. ->value('id');
  558. $set_match_r = [
  559. "home_team"=>$data['home_team'],
  560. "guest_team"=>$data['guest_team'],
  561. "lg_id"=>$lg_id,
  562. "home_rate"=> $data['home_rate'],
  563. "guest_rate"=> $data['guest_rate'],
  564. "home_score"=> $data['home_score'],
  565. "guest_score"=> $data['guest_score'],
  566. "all_goal"=> $data['all_goal'],
  567. "status"=>$data['status'],
  568. "first_score"=>$data['first_score'],
  569. "last_score"=> $data['last_score'],
  570. "match_score"=> $data['match_score'],
  571. "match_winer"=> $data['match_winer'],
  572. "match_time"=> $data['match_time'],
  573. "match_process"=> $data['match_process'],
  574. "tag"=> $data['tag'],
  575. "match_id"=> $match_id,
  576. "u_home_score"=> $data['u_home_score'],
  577. "u_guest_score"=> $data['u_guest_score'],
  578. "p_code"=> $data['p_code'],
  579. "update_time"=>date('Y-m-d H:i:s')
  580. ];
  581. //赛事结果数据处理
  582. if(!empty($match_r_id)){
  583. $ret = $model['model_result']::where(['id'=>$match_r_id])
  584. -> update($set_match_r);
  585. 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'));
  586. }else{
  587. $ret = $model['model_result']::insert($set_match_r);
  588. 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'));
  589. }
  590. // return Response::success();
  591. }
  592. //写入赛事结果记录
  593. public function com_result_record($opt=[],$gameName='',$lg_id='',$match_id=''){
  594. $game_code = $opt['game_code'];
  595. //根据球类代码获取相关model
  596. $model = commonFunction::getModels($game_code,1);
  597. $data = $opt['data'];
  598. //验证结果所属联赛
  599. // $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  600. // ->value('lg_id');
  601. // 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'));
  602. // //验证结果所属赛事
  603. // $match_id = $model['model_local_match']::where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
  604. // ->value('match_id');
  605. // 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'));
  606. $match_r_id = $model['model_result_record']::where(['match_id'=>$match_id,'match_time'=>$data['match_time']])
  607. ->value('id');
  608. //根据球类 获取 赛事结果记录字段
  609. $set_match_r = $this->get_match_r($game_code,$lg_id,$match_id,$data);
  610. //赛事结果记录处理
  611. if($match_r_id > 0){
  612. $ret = $model['model_result_record']::where(['id'=>$match_r_id])
  613. ->update($set_match_r);
  614. 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'));
  615. }else{
  616. $ret = $model['model_result_record']::insert($set_match_r);
  617. 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'));
  618. }
  619. }
  620. //写入赔率记录
  621. public function odds_record($opt){
  622. $game_code = $opt['game_code'];
  623. //根据球类代码获取相关model
  624. $model = commonFunction::getModels($game_code,1);
  625. $data = $opt['data'];
  626. $match = $model['model_local_match']::select('match_id','others_match_id')
  627. ->where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
  628. ->first();
  629. 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'));
  630. //获取赛事 本地/源ID
  631. $others_match_id = $match->others_match_id;
  632. $match_id = $match->match_id;
  633. $lg = $model['model_local_league']::select('lg_id','others_lg_id')
  634. ->where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  635. ->first();
  636. 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'));
  637. //获取联赛 本地/源ID
  638. $others_lg_id = $lg->others_lg_id;
  639. $lg_id = $lg->lg_id;
  640. //查询 赔率数据是否存在
  641. $oddsID = $model['model_odds_record']::where(['odds_only'=>$data['odds_only']])
  642. ->value('id');
  643. $set_odds = [
  644. 'match_id'=> $match_id,
  645. 'others_match_id'=> $others_match_id,
  646. 'odds_code'=> $data['odds_code'],
  647. 'status'=> $data['status'],
  648. 'sort'=> $data['sort'],
  649. 'p_code'=> $data['p_code'],
  650. 'odds'=> $data['odds'],
  651. 'condition'=> $data['condition'],
  652. 'odds_only'=> $data['odds_only'],
  653. 'source'=> $data['source'],
  654. 'type'=> $data['type'],
  655. 'team'=> $data['team'],
  656. 'lg_id'=> $lg_id,
  657. 'others_lg_id'=> $others_lg_id,
  658. 'ctime'=> date('Y-m-d H:i:s'),
  659. 'utime'=> date('Y-m-d H:i:s'),
  660. ];
  661. //更新或写入赔率记录
  662. if(!empty($oddsID)){
  663. $ret = $model['model_odds_record']::where(['id'=>$oddsID])
  664. -> update($set_odds);
  665. 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'));
  666. }else{
  667. $ret = $model['model_odds_record']::insert($set_odds);
  668. 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'));
  669. }
  670. // return Response::success();
  671. }
  672. /**
  673. * @param $data
  674. * @return mixed
  675. * json转数组
  676. */
  677. public function getAddData($data){
  678. $data = json_decode($data,true);
  679. return $data;
  680. }
  681. //根据球类获取 赛事结果记录字段
  682. public function get_match_r($game_code,$lg_id,$match_id,$data){
  683. $set_match_r = [];
  684. if($game_code == 'zq'){
  685. $set_match_r = [
  686. "home_team"=>$data['home_team']?:'',
  687. "guest_team"=>$data['guest_team']?:'',
  688. "lg_id"=>$lg_id,
  689. "all_goal"=>$data['all_goal']?:0,
  690. "home_rate"=> $data['home_rate']?:0,
  691. "guest_rate"=> $data['guest_rate']?:0,
  692. "home_score"=> $data['home_score']?:0,
  693. "guest_score"=> $data['guest_score']?:0,
  694. "status"=>$data['status']?:0,
  695. "first_score"=>$data['first_score']?:0,
  696. "last_score"=> $data['last_score']?:0,
  697. "match_score"=> $data['match_score']?:0,
  698. "match_winer"=> $data['match_winer']?:'',
  699. "match_time"=> $data['match_time']?:0,
  700. "match_process"=> $data['match_process']?:'',
  701. "tag"=> $data['tag']?:0,
  702. "match_id"=> $match_id,
  703. "update_time"=>date('Y-m-d H:i:s')
  704. ];
  705. };
  706. if($game_code == 'lq'){
  707. $set_match_r = [
  708. "home_team"=>$data['home_team']?:'',
  709. "guest_team"=>$data['guest_team']?:'',
  710. "lg_id"=>$lg_id,
  711. "home_rate"=> $data['home_rate']?:0,
  712. "guest_rate"=> $data['guest_rate']?:0,
  713. "home_score"=> $data['home_score']?:0,
  714. "guest_score"=> $data['guest_score']?:0,
  715. "status"=>$data['status']?:0,
  716. "first_score"=>$data['first_score']?:0,
  717. "last_score"=> $data['last_score']?:0,
  718. "match_score"=> $data['match_score']?:0,
  719. "match_winer"=> $data['match_winer']?:'',
  720. "match_time"=> $data['match_time']?:0,
  721. "match_process"=> $data['match_process']?:'',
  722. "tag"=> $data['tag']?:0,
  723. "match_id"=> $match_id,
  724. "update_time"=>date('Y-m-d H:i:s')
  725. ];
  726. }
  727. if($game_code == 'wq'){
  728. $set_match_r = [
  729. "home_player_name"=>$data['home_player_name']?:'',
  730. "guest_player_name"=>$data['guest_player_name']?:'',
  731. "lg_id"=>$lg_id,
  732. "home_player_let_plate"=>$data['home_player_let_plate']?:0,
  733. "guest_player_let_plate"=>$data['guest_player_let_plate']?:0,
  734. "home_player_let_inning"=>$data['home_player_let_inning']?:0,
  735. "guest_player_let_inning"=>$data['guest_player_let_inning']?:0,
  736. "all_inning"=>$data['all_inning']?:0,
  737. "home_player_score"=>$data['home_player_score']?:0,
  738. "guest_player_score"=>$data['guest_player_score']?:0,
  739. "status"=>$data['status']?:0,
  740. "first_score_player"=>$data['first_score_player']?:'',
  741. "last_score_player"=>$data['last_score_player']?:'',
  742. "first_inning_score"=>$data['first_inning_score']?:0,
  743. "second_inning_score"=>$data['second_inning_score']?:0,
  744. "third_inning_score"=>$data['third_inning_score']?:0,
  745. "match_winer_player"=>$data['match_winer_player']?:'',
  746. "update_time"=>date('Y-m-d H:i:s'),
  747. "match_time"=>$data['match_time']?:0,
  748. "match_process"=>$data['match_process']?:'',
  749. "tag"=>$data['tag']?:0,
  750. "match_id"=>$match_id,
  751. "result_mark" =>$data['result_mark']?:'',
  752. ];
  753. }
  754. if($game_code == 'bq'){
  755. $set_match_r = [
  756. "home_team"=>$data['home_team']?:'',
  757. "guest_team"=>$data['guest_team']?:'',
  758. "lg_id"=>$lg_id,
  759. "home_rate"=> $data['home_rate']?:0,
  760. "guest_rate"=> $data['guest_rate']?:0,
  761. "home_score"=> $data['home_score']?:0,
  762. "guest_score"=> $data['guest_score']?:0,
  763. "status"=>$data['status']?:0,
  764. "first_score"=>$data['first_score']?:0,
  765. "last_score"=> $data['last_score']?:0,
  766. "match_score"=> $data['match_score']?:0,
  767. "match_winer"=> $data['match_winer']?:'',
  768. "match_time"=> $data['match_time']?:0,
  769. "match_process"=> $data['match_process']?:'',
  770. "tag"=> $data['tag']?:0,
  771. "match_id"=> $match_id,
  772. "all_inning"=>$data['all_inning']?:9,
  773. "update_time"=>date('Y-m-d H:i:s'),
  774. "result_mark" =>$data['result_mark']?:'',
  775. ];
  776. }
  777. return $set_match_r;
  778. }
  779. }