WriteSportsController.php 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  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. use App\Http\Model\St_area_country as StAreaCountryModel;
  25. use App\Http\Model\St_set_sports_record as St_set_sports_recordModel;
  26. /**
  27. * 体育数据入库接口
  28. */
  29. class WriteSportsController extends BaseController{
  30. /*
  31. * 用户 用户 token
  32. */
  33. public function getToken(Req $req){
  34. if ($req->isMethod('post')) {
  35. $user = new \App\Models\System_user;
  36. $str = $user->adminLogin($_POST['account'], $_POST['password']);
  37. if($str>0){
  38. $token=refreshToken($str['admin_id']);
  39. $str['token']=$token;
  40. $data = $str;
  41. $req->session()->put('adminInfo',$str);
  42. $str=1;
  43. }
  44. $log = array(
  45. session('adminInfo.admin_name'),
  46. $_POST['account']
  47. );
  48. OperationLog(session('adminInfo.admin_id'), 'login', $log);
  49. return Response::success($data);
  50. }
  51. }
  52. /**
  53. * 根据联赛名称 获取 国家 地区
  54. */
  55. public function getArea($leagueName = ''){
  56. //如果联赛名称有括号,去除
  57. $leagueName = preg_replace('/\(.*?\)/', '', $leagueName);
  58. //获取所有国家/地区
  59. $areaData = StAreaCountryModel::select('id','pid','name')->get()->toArray();
  60. //获取当前联赛所属国家
  61. if(strpos($leagueName,'NBA') !== false){
  62. $data = [
  63. 'id'=>247,
  64. 'pid'=>0,
  65. 'name'=>'世界'
  66. ];
  67. return $data;
  68. }
  69. //识别本地 国家地区数据
  70. foreach($areaData as $v => $k){
  71. if(strpos($leagueName,$k['name']) !== false){
  72. return $k;
  73. }
  74. }
  75. //如果不能识别,则返回世界
  76. $data = [
  77. 'id'=>247,
  78. 'pid'=>0,
  79. 'name'=>'世界'
  80. ];
  81. return $data;
  82. }
  83. /*
  84. * 写 联赛 数据
  85. * 每个请求 包含 N个联赛
  86. */
  87. public function setLeague(Req $data){
  88. //验证token
  89. $user = new \App\Models\System_user;
  90. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  91. $token = $user::where(['token'=>$data->token])->value('token');
  92. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  93. try {
  94. //开启事务
  95. DB::beginTransaction();
  96. $obt = $data->data;
  97. //请求 数据 为空
  98. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  99. $getData = $this->getAddData($obt);
  100. //不是 联赛 数据
  101. if($getData['title'] != 'league') throw new \Exception(Response::generate('不是联赛数据,',Response::ABNORMAL));
  102. /*
  103. //写请求数据 日志记录
  104. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  105. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  106. */
  107. //获取球类代码
  108. $game_code = $getData['game_code'];
  109. //获取数据源
  110. $source = $getData['source'];
  111. //获取球类名称
  112. $gameName = gameModel::getGameName($game_code);
  113. //获取 model
  114. $models = commonFunction::getModels($game_code, 1);
  115. //获取 联赛 数据
  116. $leagueData = $getData['data'];
  117. //获取 当前请求 所有 联赛 uuid
  118. $identity = [];
  119. foreach($leagueData as $k=>$v){
  120. $identity[] = $v['uuid'];
  121. }
  122. $identity = array_unique($identity);
  123. sort($identity);
  124. //获取 本地 已存在 联赛
  125. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id')->get()->toArray();
  126. //二维数组去重
  127. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  128. //循环对比 请求identity->本地identity
  129. foreach($l_lg_data as $k=>$v){
  130. foreach($identity as $kk=> $uuid){
  131. if($v['identity'] == $uuid){
  132. unset($identity[$kk]);
  133. }
  134. }
  135. }
  136. //去除本地和请求里都存在的联赛,如果还有剩余联赛uuid,则将其写入
  137. if(!empty($identity)) {
  138. //没有 决赛时间 默认获取本年最后一天
  139. $last_time = date('Y-12-31 23:59:59');
  140. foreach($leagueData as $k=>$v){
  141. foreach($identity as $kk=> $uuid){
  142. if($v['uuid'] == $uuid){
  143. //如有 决赛时间
  144. if($v['last_time']){
  145. $last_time = $v['last_time'];
  146. }
  147. //获取联赛所属国家或地区
  148. $getArea = $this->getArea($v['name_chinese']);
  149. if($getArea['pid'] == 0){
  150. $area_id = $getArea['id'];
  151. $country_id = 0;
  152. }else{
  153. $area_id = $getArea['pid'];
  154. $country_id = $getArea['id'];
  155. }
  156. if(empty($v['uuid'])) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$v['lg_id'].';',Response::LG__UUID_NULL));
  157. $set_lg['name_chinese'] = $v['name_chinese'];
  158. $set_lg['kind'] = $v['kind'];
  159. $set_lg['match_mode'] = $v['match_mode'];
  160. $set_lg['if_stop'] = $v['if_stop'];
  161. $set_lg['identity'] = $v['uuid'];
  162. $set_lg['last_time'] = $last_time;
  163. $set_lg['utime'] = date('Y-m-d H:i:s');
  164. $set_lg['area_id'] = $area_id;
  165. $set_lg['country_id'] = $country_id;
  166. //写入联赛
  167. $id = $models['model_league']::insertGetId($set_lg);
  168. $m_lg_id = $id;
  169. if($m_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$v['lg_id'].';',Response::INSERT_ERROR));
  170. //写入 本地 联赛记录
  171. $set_local = [
  172. 'lg_id'=>$id,
  173. 'others_lg_id'=>$v['lg_id'],
  174. 'source'=>$source,
  175. 'ctime'=>date('Y-m-d H:i:s'),
  176. 'identity'=>$v['uuid'],
  177. ];
  178. $ret = $models['model_local_league']::insertGetId($set_local);
  179. 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'));
  180. }
  181. }
  182. }
  183. }
  184. //提交事务
  185. DB::commit();
  186. return Response::success();
  187. } catch (\Exception $e) {
  188. //回滚事务
  189. DB::rollBack();
  190. return $e->getMessage();
  191. }
  192. }
  193. /*
  194. * 写 赛事 数据
  195. * 每个请求 包含 N个联赛 下 N个赛事
  196. */
  197. public function setMatch(Req $data){
  198. //验证token
  199. $user = new \App\Models\System_user;
  200. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  201. $token = $user::where(['token'=>$data->token])->value('token');
  202. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  203. try {
  204. //开启事务
  205. DB::beginTransaction();
  206. $obt = $data->data;
  207. //请求 数据 为空
  208. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  209. $getData = $this->getAddData($obt);
  210. //不是 赛事 数据
  211. if($getData['title'] != 'match') throw new \Exception(Response::generate('不是赛事数据,',Response::ABNORMAL));
  212. /*
  213. //写请求数据 日志记录
  214. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  215. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  216. */
  217. //获取球类代码
  218. $game_code = $getData['game_code'];
  219. //获取数据源
  220. $source = $getData['source'];
  221. //获取球类名称
  222. $gameName = gameModel::getGameName($game_code);
  223. //获取 model
  224. $models = commonFunction::getModels($game_code, 1);
  225. //获取 赛事 数据
  226. $matchData = $getData['data'];
  227. //获取 当前请求 所有 uuid
  228. $identity = [];
  229. $s_match_ids = [];
  230. foreach($matchData as $k=>$v){
  231. $identity[] = $v['uuid'];
  232. $s_match_ids[] = $v['match_id'];
  233. }
  234. //====验证 赛事 所属 联赛 是否存在====
  235. $identity = array_unique($identity);
  236. sort($identity);
  237. //获取 本地 已存在 联赛
  238. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id','others_lg_id')->get()->toArray();
  239. //二维数组去重
  240. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  241. //如果获取不到本地联赛数据,则返回联赛不存在
  242. if(empty($l_lg_data)) throw new \Exception(Response::generate($gameName.'本次请求的所有赛事都没有联赛数据;',Response::LEAGUE_ERROR));
  243. //获取到本次请求,所有本地有联赛的赛事
  244. $mathData_before = $matchData;//接收原赛事
  245. $matchData = [];//获取新赛事
  246. foreach($l_lg_data as $k=>$v){
  247. foreach($mathData_before as $kk=>$vv){
  248. if($v['identity'] == $vv['uuid']){
  249. $matchData[] = $vv;
  250. }
  251. }
  252. }
  253. //获取当前请求有关联联赛的赛事 match_id
  254. $s_match_ids = [];
  255. foreach($matchData as $k=>$v){
  256. $s_match_ids[] = $v['match_id'];
  257. }
  258. /*
  259. //循环对比 请求uuid->本地identity
  260. foreach($l_lg_data as $k=>$v){
  261. foreach($identity as $kk=> $uuid){
  262. if($v['identity'] == $uuid){
  263. unset($identity[$kk]);
  264. }
  265. }
  266. }
  267. sort($identity);
  268. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常
  269. if(!empty($identity)) throw new \Exception(Response::generate($gameName.'联赛:uuid-'.$identity[0].';',Response::LEAGUE_ERROR));
  270. //如果有剩余联赛,则表示该赛事找不到联赛,将其删除
  271. if(!empty($identity)){
  272. foreach($matchData as $k=>$v){
  273. foreach($identity as $kk=> $uuid){
  274. if($v['uuid'] == $uuid){
  275. unset($matchData[$k]);
  276. }
  277. }
  278. }
  279. sort($matchData);
  280. }
  281. //====end====
  282. */
  283. //====获取 本地 已存在 赛事====
  284. $s_match_ids = array_unique($s_match_ids);
  285. sort($s_match_ids);
  286. $l_match_data = $models['model_local_match']::whereIn('others_match_id',$s_match_ids)->where('source',$source)->select('others_match_id','match_id')->get()->toArray();
  287. //二维数组去重
  288. $l_match_data = commonFunction::uniquArrV2($l_match_data,'others_match_id');
  289. //循环对比 请求match_id->本地others_match_id
  290. //如果本地有 赛事,则取出本地不存在赛事
  291. if(!empty($l_match_data)){
  292. foreach($l_match_data as $k=>$v){
  293. foreach($s_match_ids as $kk=> $s_match_id){
  294. if($v['others_match_id'] == $s_match_id){
  295. unset($s_match_ids[$kk]);
  296. }
  297. }
  298. }
  299. sort($s_match_ids);
  300. }
  301. //剩余 赛事 数据 写入
  302. if(!empty($s_match_ids)){
  303. foreach($matchData as $k=>$v){
  304. foreach($s_match_ids as $kk=> $s_match_id){
  305. if($v['match_id'] == $s_match_id){
  306. $data = $v;
  307. $half_match_id = $v['half_match_id'];
  308. //获取 本地 联赛 ID
  309. //验证本赛事是否有数据源lg_id
  310. if(empty($data['lg_id'])) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::S_LG_ID_NULL));
  311. $lg_id = commonFunction::searcharray($data['uuid'],'identity',$l_lg_data,'lg_id');
  312. //验证 本赛事是否获取到本地lg_id
  313. if(empty($lg_id)) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::L_LG_ID_NULL));
  314. /*
  315. //如果有上半场赛事id 获取上半场赛事是否存在
  316. if(!empty($l_match_data) and $data['half_match_id'] > 0){
  317. $half_match_id = -1;
  318. $half_match_id = $models['model_local_match']::where(['others_match_id'=>$data['half_match_id'],'source'=>$source])
  319. ->value('match_id');
  320. 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'));
  321. }
  322. */
  323. //如果赛事没有开始日期,则为冠军盘口赛事
  324. if(empty($data['match_date'])){
  325. //冠军盘口赛事获取所属联赛结束时间
  326. $last_time = $models['model_league']::where(['id'=>$lg_id])
  327. ->value('last_time');
  328. 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'));
  329. //给冠军盘口赛事 赋值时间
  330. $time = strtotime($last_time);
  331. $data['match_date'] = date('Y-m-d',$time);
  332. $data['match_time'] = date('H:i:s',$time);
  333. }
  334. $set_match = [
  335. 'ctime'=>date('Y-m-d H:i:s'),
  336. 'utime'=>date('Y-m-d H:i:s'),
  337. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  338. 'home_team'=>$data['home_team']?:'',
  339. 'guest_team'=>$data['guest_team']?:'no_team',
  340. 'lg_id'=>$lg_id,
  341. 'status'=>$data['status'],
  342. 'match_date'=>$data['match_date']?:date('Y-m-d'),
  343. 'match_time'=>$data['match_time']?:date('H:i:s'),
  344. // 'tag'=>$data['tag']?:0,
  345. 'is_rollball'=>$data['is_rollball']?:0,
  346. 'is_today'=>$data['is_today']?:0,
  347. 'is_morningplate'=>$data['is_morningplate']?:0,
  348. 'is_stringscene'=>$data['is_stringscene']?:0,
  349. 'us_time'=>$data['us_time']?:commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  350. 'half_match_id'=>$half_match_id?:0,
  351. 'identity' => $data['uuid'],
  352. ];
  353. //写入赛事 返回id
  354. $id = $models['model_match']::insertGetId($set_match);
  355. if($id < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::INSERT_ERROR)) ;
  356. //写关联记录
  357. $set_local = [
  358. 'match_id'=>$id,
  359. 'others_match_id'=>$data['match_id'],
  360. 'source'=>$source,
  361. 'ctime'=>date('Y-m-d H:i:s')
  362. ];
  363. $ret = $models['model_local_match']::insertGetId($set_local);
  364. 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'));
  365. }
  366. }
  367. }
  368. }
  369. //提交事务
  370. DB::commit();
  371. return Response::success();
  372. } catch (\Exception $e) {
  373. //回滚事务
  374. DB::rollBack();
  375. return $e->getMessage();
  376. }
  377. }
  378. /*
  379. * 写 赛事 结果 记录 数据
  380. * 每个请求 包含 N个联赛 下 N个赛事 结果数据
  381. */
  382. public function setMatchResult(Req $data){
  383. //验证token
  384. $user = new \App\Models\System_user;
  385. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  386. $token = $user::where(['token'=>$data->token])->value('token');
  387. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  388. try {
  389. //开启事务
  390. DB::beginTransaction();
  391. $obt = $data->data;
  392. //请求 数据 为空
  393. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  394. $getData = $this->getAddData($obt);
  395. //不是 赛事 结果 记录 数据
  396. if($getData['title'] != 'match_result_r') throw new \Exception(Response::generate('不是赛事结果记录数据,',Response::ABNORMAL));
  397. /*
  398. //写请求数据 日志记录
  399. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  400. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  401. */
  402. //获取球类代码
  403. $game_code = $getData['game_code'];
  404. //获取数据源
  405. $source = $getData['source'];
  406. //获取球类名称
  407. $gameName = gameModel::getGameName($game_code);
  408. //获取 model
  409. $models = commonFunction::getModels($game_code, 1);
  410. //获取 赛事 数据
  411. $match_r_data = $getData['data'];
  412. //获取 当前请求 所有 uuid /match_id
  413. $identity = [];
  414. $s_match_ids = [];
  415. foreach($match_r_data as $k=>$v){
  416. $identity[] = $v['uuid'];
  417. $s_match_ids[] = $v['match_id'];
  418. }
  419. //====验证 赛事 所属 联赛 是否存在====
  420. $identity = array_unique($identity);
  421. sort($identity);
  422. //获取 本地 已存在 联赛
  423. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id')->get()->toArray();
  424. //二维数组去重
  425. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  426. //循环对比 请求uuid->本地identity
  427. foreach($l_lg_data as $k=>$v){
  428. foreach($identity as $kk=> $uuid){
  429. if($v['identity'] == $uuid){
  430. unset($identity[$kk]);
  431. }
  432. }
  433. }
  434. sort($identity);
  435. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常
  436. if(!empty($identity)) throw new \Exception(Response::generate($gameName.'联赛:uuid-'.$identity[0].';',Response::LEAGUE_ERROR));
  437. //====end====
  438. //====验证 赛事结果记录 所属赛事 是否存在====
  439. $s_match_ids = array_unique($s_match_ids);
  440. sort($s_match_ids);
  441. //获取 本地 已存在 赛事
  442. $l_match_data = $models['model_local_match']::whereIn('others_match_id',$s_match_ids)->where('source',$source)->select('others_match_id','match_id')->get()->toArray();
  443. //二维数组去重
  444. $l_match_data = commonFunction::uniquArrV2($l_match_data,'others_match_id');
  445. //循环对比 请求match_id->本地others_match_id
  446. foreach($l_match_data as $k=>$v){
  447. foreach($s_match_ids as $kk=> $s_match_id){
  448. if($v['others_match_id'] == $s_match_id){
  449. unset($s_match_ids[$kk]);
  450. }
  451. }
  452. }
  453. sort($s_match_ids);
  454. //去除本地和请求里都存在的赛事,如果还有剩余赛事id,则返回异常
  455. if(!empty($s_match_ids)) throw new \Exception(Response::generate($gameName.'赛事-match_id'.$s_match_ids[0].';',Response::MATCH_ERROR));
  456. //====end====
  457. //处理 赛事 结果记录 数据
  458. foreach($match_r_data as $k=>$v){
  459. $data = $v;
  460. //获取 本地 联赛 ID
  461. $lg_id = commonFunction::searcharray($data['uuid'],'identity',$l_lg_data,'lg_id');
  462. //获取 本地 赛事 ID
  463. $match_id = commonFunction::searcharray($data['match_id'],'others_match_id',$l_match_data,'match_id');
  464. //根据球类 获取 赛事结果记录字段
  465. $set_match_r = $this->get_match_r($game_code,$lg_id,$match_id,$data);
  466. //写 赛事 结果 记录
  467. $ret = $models['model_result_record']::insert($set_match_r);
  468. if($ret != true) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_R_ERROR));
  469. }
  470. //提交事务
  471. DB::commit();
  472. return Response::success();
  473. } catch (\Exception $e) {
  474. //回滚事务
  475. DB::rollBack();
  476. return $e->getMessage();
  477. }
  478. }
  479. /*
  480. * 写 普通 赔率 数据
  481. * 每个请求 只包含 一场赛事下 N条赔率
  482. */
  483. public function setOdds(Req $data){
  484. //验证token
  485. $user = new \App\Models\System_user;
  486. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  487. $token = $user::where(['token'=>$data->token])->value('token');
  488. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  489. try {
  490. //开启事务
  491. DB::beginTransaction();
  492. $obt = $data->data;
  493. //请求 数据 为空
  494. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  495. $getData = $this->getAddData($obt);
  496. //不是 赔率 数据
  497. if($getData['title'] != 'odds') throw new \Exception(Response::generate('不是赔率数据,',Response::ABNORMAL));
  498. /*
  499. //写请求数据 日志记录
  500. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  501. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  502. */
  503. //获取球类代码
  504. $game_code = $getData['game_code'];
  505. //获取数据源
  506. $source = $getData['source'];
  507. //获取源数据联赛ID
  508. $uuid = $getData['uuid'];
  509. //获取源数据赛事ID
  510. $s_match_id = $getData['match_id'];
  511. //获取赔率所属赛事 tag 值 玩法数量
  512. $tag = $getData['tag']?:99;
  513. //获取球类名称
  514. $gameName = gameModel::getGameName($game_code);
  515. //验证本次请求所属联赛/赛事是否存在 返回本地联赛/赛事ID
  516. $models = commonFunction::getModels($game_code, 1);
  517. $lg_id = $this->leagueVerify($models,$uuid,$source,$gameName);
  518. $match_id = $this->matchVerify($models,$s_match_id,$source,$gameName);
  519. //更新赛事 tag 值
  520. //如果未获取到本地赛事id,则返回异常
  521. if(empty($match_id)) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::L_MATCH_ID_ERR)) ;
  522. $upMatch = $models['model_match']::where(['id'=>$match_id])
  523. ->update(['tag'=>$tag,'utime'=>date('Y-m-d H:i:s')]);
  524. if($upMatch < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::UPMATCHTAG_ERROR)) ;
  525. //获取 赔率数据
  526. $oddsData = $getData['data'];
  527. //获取 当前 所有 可用 赔率 sole
  528. $odds_only = $getData['odds_only'];
  529. //更新赛事下所有 不在本次请求的赔率 状态
  530. $ret= OddsModel::upOddsStatus($models,$s_match_id,'',$source,$odds_only);
  531. if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  532. //获取当前赛事 在本地的所有赔率
  533. $l_odds_data = $models['model_odds']::select('odds_only')->where(['match_id'=>$match_id])->get()->toArray();
  534. $set_odds_r = [];
  535. $set_odds = [];
  536. if(!empty($oddsData)){
  537. //去除本地已有的赔率
  538. if(!empty($l_odds_data)){
  539. foreach($oddsData as $k=>$v){
  540. foreach($l_odds_data as $kk=>$vv){
  541. if($v['odds_only'] == $vv['odds_only']){
  542. unset($oddsData[$k]);
  543. }
  544. }
  545. }
  546. sort($oddsData);
  547. }
  548. //处理待写入 赔率记录数据
  549. foreach($oddsData as $k=>$v){
  550. //组装数据
  551. $set_odds_r[] = [
  552. 'match_id'=> $match_id?:0,
  553. 'others_match_id'=> $v['match_id']?:0,
  554. 'odds_code'=> $v['odds_code']?:'',
  555. 'status'=> $v['status']?:0,
  556. 'sort'=> $v['sort']?:0,
  557. 'p_code'=> $v['p_code']?:'',
  558. 'odds'=> $v['odds']?:0,
  559. 'condition'=> $v['condition'],
  560. 'odds_only'=> $v['odds_only']?:'',
  561. 'source'=> $v['source']?:'',
  562. 'type'=> $v['type']?:0,
  563. 'team'=> $v['team']?:'',
  564. 'lg_id'=> $lg_id,
  565. 'others_lg_id'=> $v['lg_id'],
  566. 'ctime'=> date('Y-m-d H:i:s'),
  567. 'utime'=> date('Y-m-d H:i:s'),
  568. ];
  569. }
  570. //处理待写入 赔率数据
  571. foreach($oddsData as $k=>$v){
  572. //组装数据
  573. $set_odds[] = [
  574. 'match_id'=> $match_id?:0,
  575. 'others_match_id'=> $v['match_id']?:0,
  576. 'odds_code'=> $v['odds_code']?:'',
  577. 'status'=> $v['status']?:0,
  578. 'sort'=> $v['sort']?:0,
  579. 'p_code'=> $v['p_code']?:'',
  580. 'odds'=> $v['odds']?:0,
  581. 'condition'=> $v['condition'],
  582. 'odds_only'=> $v['odds_only']?:'',
  583. 'source'=> $v['source']?:'',
  584. 'type'=> $v['type']?:0,
  585. 'team'=> $v['team']?:'',
  586. 'lg_id'=> $lg_id,
  587. 'others_lg_id'=> $v['lg_id'],
  588. 'ctime'=> date('Y-m-d H:i:s'),
  589. 'utime'=> date('Y-m-d H:i:s'),
  590. 'sole' => $v['sole'],
  591. 'expire_time' => date('Y-m-d H:i:s',time()+60)
  592. ];
  593. }
  594. //写赔率记录数据
  595. $ret = $models['model_odds_record']::insert($set_odds_r);
  596. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率记录;',Response::ADD_ODDS_R_ERROR));
  597. //写赔率 数据
  598. $ret = $models['model_odds']::insert($set_odds);
  599. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率;',Response::ADD_ODDS_ERROR));
  600. }
  601. //提交事务
  602. DB::commit();
  603. return Response::success();
  604. } catch (\Exception $e) {
  605. //回滚事务
  606. DB::rollBack();
  607. return $e->getMessage();
  608. }
  609. }
  610. /*
  611. *写 冠军联赛 赔率 数据
  612. *每个请求 包含 N个联赛 下 N条赔率
  613. */
  614. public function setOddsCH(Req $data){
  615. //验证token
  616. $user = new \App\Models\System_user;
  617. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  618. $token = $user::where(['token'=>$data->token])->value('token');
  619. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  620. try {
  621. //开启事务
  622. DB::beginTransaction();
  623. $obt = $data->data;
  624. //请求 数据 为空
  625. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  626. $getData = $this->getAddData($obt);
  627. //不是 冠军 赔率 数据
  628. if($getData['title'] != 'odds_ch') throw new \Exception(Response::generate('不是冠军赔率数据,',Response::ABNORMAL));
  629. /*
  630. //写请求数据 日志记录
  631. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  632. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  633. */
  634. //获取球类代码
  635. $game_code = $getData['game_code'];
  636. //获取数据源
  637. $source = $getData['source'];
  638. //获取球类名称
  639. $gameName = gameModel::getGameName($game_code);
  640. //获取 model
  641. $models = commonFunction::getModels($game_code, 1);
  642. //获取 赔率数据
  643. $oddsData = $getData['data'];
  644. //获取 当前 所有 可用 赔率 sole
  645. // $odds_only = $getData['odds_only'];
  646. //====验证 请求里 所有赔率 所属联赛 是否存在 ====
  647. //获取 当前请求 所有 联赛 uuid
  648. $uuids = [];
  649. foreach($oddsData as $k=>$v){
  650. $uuids[] = $v['uuid'];
  651. }
  652. $uuids = array_unique($uuids);
  653. sort($uuids);
  654. //更新赛事下所有 不在本次请求的赔率 状态
  655. // $ret= OddsModel::upOddsStatus($models,'',$s_lg_ids,$source,$odds_only);
  656. // if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  657. //获取 本地 已存在 联赛
  658. $l_lg_data = $models['model_local_league']::whereIn('identity',$uuids)->select('identity','lg_id')->get()->toArray();
  659. //二维数组去重
  660. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  661. //循环对比 请求uuid->本地uuid
  662. foreach($l_lg_data as $k=>$v){
  663. foreach($uuids as $kk=> $uuid){
  664. if($v['identity'] == $uuid){
  665. unset($uuids[$kk]);
  666. }
  667. }
  668. }
  669. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常;
  670. if(!empty($uuids)) throw new \Exception(Response::generate($gameName.'冠军联赛:uuid-'.$uuids[0].';',Response::LEAGUE_ERROR));
  671. //====end====
  672. //拼装 待写入 赔率数据
  673. foreach($oddsData as $k=>$v){
  674. //获取 本地 联赛 ID
  675. $lg_id = commonFunction::searcharray($data['uuid'],'identity',$l_lg_data,'lg_id');
  676. //组装数据
  677. $set_odds = [
  678. 'match_id'=>0,
  679. 'others_match_id'=> $v['match_id']?:0,
  680. 'odds_code'=> $v['odds_code']?:'',
  681. 'status'=> $v['status']?:0,
  682. 'sort'=> $v['sort']?:0,
  683. 'p_code'=> $v['p_code']?:'',
  684. 'odds'=> $v['odds']?:0,
  685. 'condition'=> $v['condition'],
  686. 'odds_only'=> $v['odds_only']?:'',
  687. 'source'=> $v['source']?:'',
  688. 'type'=> $v['type']?:1,
  689. 'team'=> $v['team']?:'',
  690. 'lg_id'=> $lg_id,
  691. 'others_lg_id'=> $v['lg_id'],
  692. 'ctime'=> date('Y-m-d H:i:s'),
  693. 'utime'=> date('Y-m-d H:i:s'),
  694. ];
  695. //写赔率记录数据
  696. $ret = $models['model_odds_record']->updateOrCreate(
  697. ['odds_only'=>$v['odds_only']],$set_odds
  698. );
  699. if(empty($ret)) throw new \Exception(Response::generate($gameName.'赔率记录-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_R_ERROR));
  700. //追加参数
  701. $set_odds['sole'] = $v['sole'];
  702. $set_odds['expire_time'] = date('Y-m-d H:i:s',time()+60);
  703. //写赔率 数据
  704. $ret = $models['model_odds']->updateOrCreate(
  705. ['odds_only'=>$v['odds_only']],$set_odds
  706. );
  707. if(empty($ret)) throw new \Exception(Response::generate($gameName.'赔率-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_ERROR));
  708. }
  709. //提交事务
  710. DB::commit();
  711. return Response::success();
  712. } catch (\Exception $e) {
  713. //回滚事务
  714. DB::rollBack();
  715. return $e->getMessage();
  716. }
  717. }
  718. /*
  719. * 写入直播 数据
  720. */
  721. public function setBroadCast(Req $data){
  722. //验证token
  723. $user = new \App\Models\System_user;
  724. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  725. $token = $user::where(['token'=>$data->token])->value('token');
  726. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  727. try {
  728. //开启事务
  729. DB::beginTransaction();
  730. $obt = $data->data;
  731. //请求 数据 为空
  732. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  733. $getData = $this->getAddData($obt);
  734. //不是 直播 数据
  735. if($getData['title'] != 'broad_cast') throw new \Exception(Response::generate('不是直播数据,',Response::ABNORMAL));
  736. /*
  737. //写请求数据 日志记录
  738. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  739. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  740. */
  741. //获取 球类
  742. $game_code = $getData['game_code'];
  743. //获取 直播 数据
  744. $BroadCast = $getData['data'];
  745. $set_broadcast = [];
  746. foreach($BroadCast as $k=>$data){
  747. //组装数据
  748. $set_broadcast[] = [
  749. "doing" => $data['doing'],
  750. "game_type" => $data['game_type'],
  751. "game_code" => $game_code,
  752. "guest_team" => $data['guest_team'],
  753. "host_team" => $data['host_team'],
  754. "league_name" => $data['league_name'],
  755. "shower" => $data['shower'],
  756. "showid" => (int)$data['showid'],
  757. "start_time" => $data['start_time'],
  758. "ctime" =>date('Y-m-d H:i:s'),
  759. "utime" =>date('Y-m-d H:i:s'),
  760. ];
  761. }
  762. //写入 直播 数据
  763. $ret = broadcastModel::insert($set_broadcast);
  764. if($ret == false) throw new \Exception(Response::generate('',Response::BROADCAST_ERROR));
  765. //提交事务
  766. DB::commit();
  767. return Response::success();
  768. } catch (\Exception $e) {
  769. //回滚事务
  770. DB::rollBack();
  771. return $e->getMessage();
  772. }
  773. }
  774. /*
  775. * 验证所属 联赛 是否存在
  776. */
  777. public function leagueVerify($models=[],$uuid='',$source='',$gameName=''){
  778. if(empty($models) || empty($uuid)) throw new \Exception(Response::generate('',Response::ABNORMAL));
  779. $l_lg_id = $models['model_local_league']::where(['identity'=>$uuid])->value('lg_id');
  780. if($l_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛:lg_id-'.$uuid,Response::LEAGUE_ERROR));
  781. return $l_lg_id;
  782. }
  783. /*
  784. * 验证所属 赛事 是否存在
  785. */
  786. public function matchVerify($models=[],$s_match_id='',$source='',$gameName=''){
  787. if(empty($models) || empty($s_match_id)) throw new \Exception(Response::generate('',Response::ABNORMAL));
  788. $l_match_id = $models['model_local_match']::where(['others_match_id'=>$s_match_id,'source'=>$source])->value('match_id');
  789. if($l_match_id < 1) throw new \Exception(Response::generate($gameName.'赛事:match_id-'.$s_match_id,Response::MATCH_ERROR));
  790. return $l_match_id;
  791. }
  792. /*
  793. * 写入数据 接口
  794. */
  795. public function setSports(Req $data){
  796. try {
  797. //开启事务
  798. DB::beginTransaction();
  799. if($data->game_code){
  800. //用于后台 将所有进行中的赛事写入结果
  801. $this->match_result($data->game_code);
  802. }else{
  803. //验证token
  804. $dd = $data->session()->get('adminInfo');
  805. if(empty($data->token) ||($dd['token'] != $data->token)) return Response::generate('',Response::TOKEN_ERR);
  806. //写赛事数据
  807. $obt = $data->data;
  808. if($obt){
  809. $getData = $this->getAddData($obt);
  810. $league = [];
  811. $competition = [];
  812. $odds = [];
  813. $odds_soly = [];
  814. $league_result = [];
  815. $match_result = [];
  816. $match_result_record = [];
  817. $odds_record = [];
  818. $broadcast = [];
  819. //指定排序 联赛->赛事->赔率->联赛结果->赛事结果->赛事结果记录->赔率记录->直播数据
  820. foreach ($getData as $k=>$v){
  821. $game_code = $v['game_code'];
  822. if($v['title'] == 'league') $league[] = $v;
  823. if($v['title'] == 'competition') $competition[] = $v;
  824. if($v['title'] == 'odds') $odds[] = $v;
  825. if($v['title'] == 'odds_sole') $odds_soly[] = $v;
  826. if($v['title'] == 'league_result') $league_result[] = $v;
  827. if($v['title'] == 'match_result') $match_result[] = $v;
  828. if($v['title'] == 'match_result_record') $match_result_record[] = $v;
  829. if($v['title'] == 'odds_record') $odds_record[] = $v;
  830. if($v['title'] == 'broadcast') $broadcast[] = $v;
  831. }
  832. $matchData = [$league,$competition,$odds_soly,$odds,$league_result,$match_result,$match_result_record,$odds_record,$broadcast];
  833. //排空处理
  834. foreach ($matchData as $k=>$v){
  835. if($v == []) unset($matchData[$k]);
  836. }
  837. sort($matchData);
  838. //根据顺序写入数据
  839. //降维数据
  840. $mentData = [];
  841. foreach ($matchData as $k=>$v){
  842. foreach ($v as $kk=>$vv){
  843. $mentData[] = $vv;
  844. }
  845. }
  846. //获取各球类model
  847. $models = commonFunction::getModels($game_code,1);
  848. //获取球类名称
  849. $gameName = gameModel::getGameName($game_code);
  850. //===获取验证数据===
  851. //获取一段时间内所有联赛本地记录
  852. $lg_data = LeagueModel::getLeagueID($models);
  853. //获取一段时间内所有赛事本地记录
  854. $match_data = MatchModel::getMatchID($models);
  855. //获取一段时间内所有赔率
  856. $odds_data = OddsModel::getOddsID($models);
  857. //获取一段时间内所有赔率记录
  858. $odds_record_data = OddsRecordModel::getOddsRecordID($models);
  859. //===获取验证数据===
  860. $others_lg_id = '';//继承 源联赛id
  861. $others_match_id = '';//继承 源赛事id
  862. $s_lg_id = '';//源 联赛ID
  863. $l_lg_id = '';//本地 联赛 ID
  864. $s_match_id = '';//源 赛事ID
  865. $l_match_id = '';//本地 赛事 ID
  866. foreach ($mentData as $kk =>$vv){
  867. switch ($vv['title']){
  868. case 'area'://地区
  869. throw new \Exception(Response::generate('地区数据-area:',Response::AUTH_ERROR)) ;
  870. break;
  871. case 'country'://国家
  872. throw new \Exception(Response::generate('国家数据-country:',Response::AUTH_ERROR));
  873. break;
  874. case 'league'://联赛
  875. $others_lg_id = $vv['data']['lg_id'];
  876. $s_lg_id = $vv['data']['lg_id'];
  877. $source = $vv['data']['source'];
  878. //验证联赛记录是否已存在
  879. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  880. if($ret_lg != false) {
  881. $l_lg_id = $ret_lg;
  882. break;
  883. }
  884. //执行 联赛数据
  885. $lg_data = $this->league($vv,$gameName);
  886. $l_lg_id = $lg_data['lg_id'];
  887. break;
  888. case 'competition'://赛事
  889. $others_match_id = $vv['data']['match_id'];
  890. $s_lg_id = $vv['data']['lg_id'];
  891. $s_match_id = $vv['data']['match_id'];
  892. $source = $vv['data']['source'];
  893. //本次请求是否包含 联赛数据
  894. if(empty($others_lg_id)){
  895. //验证本地是否存在
  896. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  897. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  898. $l_lg_id = $ret_lg;
  899. }else{
  900. //本次有 联赛数据 验证属于同联赛
  901. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  902. }
  903. //验证赛事记录是否已存在
  904. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  905. if($ret_match != false){
  906. $l_match_id = $ret_match;
  907. break;
  908. }
  909. //执行 赛事数据
  910. $match_data = $this->competition($vv,$gameName,$l_lg_id);
  911. $l_match_id = $match_data['match_id'];
  912. break;
  913. case 'odds'://赔率
  914. $s_lg_id = $vv['data']['lg_id'];
  915. $s_match_id = $vv['data']['match_id'];
  916. $source = $vv['data']['source'];
  917. $odds_only = $vv['data']['odds_only'];
  918. $sole = $vv['data']['sole'];
  919. $odds_type = $vv['data']['type'];//0普通 1冠军盘口
  920. //本次请求是否包含 联赛数据
  921. if(empty($others_lg_id)){
  922. //无联赛数据 验证本地是否存在
  923. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  924. //联赛 不存在
  925. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  926. $l_lg_id = $ret_lg;
  927. }else{
  928. //有联赛数据 验证属于同请求联赛
  929. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  930. }
  931. //如果是普通 赔率,则验证所属赛事
  932. if($odds_type == 0){
  933. //验证赛事记录是否已存在
  934. if(empty($others_match_id)){
  935. //无赛事数据 验证本地是否存在
  936. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  937. //赛事 不存在
  938. if($ret_match == false) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  939. $l_match_id = $ret_match;
  940. }else{
  941. //有赛事数据 验证属于同请求赛事
  942. if($s_match_id != $others_match_id) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  943. }
  944. }
  945. //验证赔率 是否存在
  946. $ret_odds = '';
  947. $ret_odds = commonFunction::ver_odds($sole,$source,$odds_data);
  948. //验证赔率记录 是否存在
  949. $ret_odds_record = '';
  950. $ret_odds_record = commonFunction::ver_odds_record($odds_only,$source,$odds_record_data);
  951. $this->odds($vv,$gameName,$l_lg_id,$l_match_id,$ret_odds,$ret_odds_record);
  952. break;
  953. case 'odds_sole':
  954. $this->upOddsStatus($vv,$gameName);
  955. break;
  956. case 'league_result'://联赛结果
  957. $this->league_result($vv,$gameName);
  958. break;
  959. case 'match_result'://赛事结果
  960. throw new \Exception(Response::generate('赛事结果数据-match_result:',Response::AUTH_ERROR));
  961. $this->match_result($vv,$gameName);
  962. break;
  963. case 'match_result_record'://赛事结果记录
  964. $s_lg_id = $vv['data']['lg_id'];
  965. $s_match_id = $vv['data']['match_id'];
  966. $source = $vv['data']['source'];
  967. //如果本次请求没有 联赛数据
  968. if(empty($others_lg_id)){
  969. //验证本地是否存在
  970. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  971. //联赛 不存在
  972. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  973. $l_lg_id = $ret_lg;
  974. }else{
  975. //有联赛数据 验证属于同请求联赛
  976. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  977. }
  978. //验证赛事记录是否已存在
  979. if(empty($others_match_id)){
  980. //无赛事数据 验证本地是否存在
  981. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  982. //联赛 不存在
  983. if($ret_match == false) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  984. $l_match_id = $ret_match;
  985. }else{
  986. //有赛事数据 验证属于同请求赛事
  987. if($s_match_id != $others_match_id) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  988. }
  989. //处理 赛事结果 记录
  990. $this->com_result_record($vv,$gameName,$l_lg_id,$l_match_id);
  991. break;
  992. case 'broadcast'://直播数据
  993. $this->broadcast($vv,$game_code);
  994. break;
  995. default:
  996. throw new \Exception(Response::generate('',Response::ABNORMAL)) ;
  997. }
  998. }
  999. }
  1000. }
  1001. //提交事务
  1002. DB::commit();
  1003. return Response::success();
  1004. } catch (\Exception $e) {
  1005. //回滚事务
  1006. DB::rollBack();
  1007. return $e->getMessage();
  1008. }
  1009. }
  1010. /**
  1011. * @param Req $data
  1012. * @return string
  1013. * @throws \App\Lib\Biz\Sport\Exception
  1014. * 更新赛事状态
  1015. */
  1016. public function upMatch(Req $data){
  1017. //验证token
  1018. $user = new \App\Models\System_user;
  1019. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  1020. $token = $user::where(['token'=>$data->token])->value('token');
  1021. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  1022. try {
  1023. //开启事务
  1024. DB::beginTransaction();
  1025. //获取待更新赛事
  1026. $obt = $data->data;
  1027. if($obt){
  1028. //json转数组
  1029. $data = $this->getAddData($obt);
  1030. //获取 球类代码
  1031. $game_code = $data['game_code'];
  1032. //获取 数据源
  1033. $source = $data['source'];
  1034. //获取所有数据源赛事 match_id
  1035. $others_match_ids = [];
  1036. foreach ($data['data'] as $k=>$v){
  1037. $others_match_ids[] = $v['match_id'];
  1038. }
  1039. //根据球类代码 获取model
  1040. $model =commonFunction::getModels($game_code,1);
  1041. //获取所有赛事 match_id
  1042. $local_match = $model['model_local_match']::SELECT('others_match_id','match_id')
  1043. ->where(['source'=>$source])
  1044. ->whereIn('others_match_id',$others_match_ids)
  1045. ->get()->toArray();
  1046. if(empty($local_match)) throw new \Exception(Response::generate('',Response::MATCHID_NULL));
  1047. //更新状态字段
  1048. foreach ($local_match as $k=>$v){
  1049. $set_status = [
  1050. 'status'=>$v['status'],
  1051. 'is_rollball'=>$v['is_rollball'],
  1052. 'stais_todaytus'=>$v['is_today'],
  1053. 'is_morningplate'=>$v['is_morningplate'],
  1054. 'is_stringscene'=>$v['is_stringscene'],
  1055. 'is_horn'=>$v['is_horn'],
  1056. ];
  1057. $ret = $model['model_match']::where(['id'=>$v['match_id']])
  1058. -> update($set_status);
  1059. if($ret<1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$v['others_match_id'],Response::UPSTATUS_ERROR));
  1060. }
  1061. }
  1062. //提交事务
  1063. DB::commit();
  1064. return Response::success();
  1065. } catch (\Exception $e) {
  1066. //回滚事务
  1067. DB::rollBack();
  1068. return $e->getMessage();
  1069. }
  1070. }
  1071. /*
  1072. * 写入直播 数据
  1073. */
  1074. public function broadcast($opt = [],$game_code=''){
  1075. $data = $opt['data'];
  1076. $set_broadcast = [
  1077. "doing" => $data['doing'],
  1078. "game_type" => $data['game_type'],
  1079. "game_code" => $game_code,
  1080. "guest_team" => $data['guest_team'],
  1081. "host_team" => $data['host_team'],
  1082. "league_name" => $data['league_name'],
  1083. "shower" => $data['shower'],
  1084. "showid" => (int)$data['showid'],
  1085. "start_time" => $data['start_time'],
  1086. "ctime" =>date('Y-m-d H:i:s'),
  1087. "utime" =>date('Y-m-d H:i:s'),
  1088. ];
  1089. $ret = broadcastModel::insert($set_broadcast);
  1090. if($ret == false) throw new \Exception(Response::generate('',Response::BROADCAST_ERROR));
  1091. }
  1092. /*
  1093. * 写入 赛事 初始 结果
  1094. */
  1095. public function match_result($game_code = ''){
  1096. $model =commonFunction::getModels($game_code,1);
  1097. if($game_code == 'zq') ZqResultModel::ZQresult($model);
  1098. if($game_code == 'lq') LqResultModel::LQresult($model);
  1099. if($game_code == 'wq') WqResultModel::WQresult($model);
  1100. if($game_code == 'bq') BqResultModel::BQresult($model);
  1101. }
  1102. /*
  1103. * 写入联赛数据
  1104. */
  1105. public function league($opt = [],$gameName=''){
  1106. $game_code = $opt['game_code'];
  1107. //根据球类代码获取相关model
  1108. $model =commonFunction::getModels($game_code,1);
  1109. $data = $opt['data'];
  1110. //查询联赛是否已存在
  1111. $id = $model['model_league']::where('name_chinese','=',$data['name_chinese'])
  1112. ->value('id');
  1113. //默认获取本年最后一天
  1114. $last_time = date('Y-12-31 23:59:59');
  1115. //决赛时间
  1116. if($data['last_time']){
  1117. $last_time = $data['last_time'];
  1118. }
  1119. $set_lg['name_chinese'] = $data['name_chinese'];
  1120. $set_lg['kind'] = $data['kind'];
  1121. $set_lg['match_mode'] = $data['match_mode'];
  1122. $set_lg['if_stop'] = $data['if_stop'];
  1123. $set_lg['identity'] = $data['uuid']?:'';
  1124. $set_lg['last_time'] = $last_time;
  1125. $set_lg['utime'] = date('Y-m-d H:i:s');
  1126. if(empty($id)){
  1127. //写入联赛
  1128. $id = $model['model_league']::insertGetId($set_lg);
  1129. $m_lg_id = $id;
  1130. if($m_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::INSERT_ERROR));
  1131. }else{
  1132. //更新联赛
  1133. $ret = $model['model_league']::where(['id'=>$id])
  1134. -> update($set_lg);
  1135. if($ret < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::UPDATE_ERROR));
  1136. }
  1137. $set_local = [
  1138. 'lg_id'=>$id,
  1139. 'others_lg_id'=>$data['lg_id'],
  1140. 'source'=>$data['source'],
  1141. 'ctime'=>date('Y-m-d H:i:s')
  1142. ];
  1143. $ret = $model['model_local_league']::insertGetId($set_local);
  1144. 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'));
  1145. //执行成功返回源数据 联赛ID
  1146. $data = ['others_lg_id'=>$data['lg_id'],'lg_id'=>$id];
  1147. return $data;
  1148. }
  1149. /*
  1150. * 写入赛事数据
  1151. */
  1152. public function competition($opt = [],$gameName='',$lg_id=''){
  1153. $game_code = $opt['game_code'];
  1154. //根据球类代码获取相关model
  1155. $model = commonFunction::getModels($game_code,1);
  1156. $data = $opt['data'];
  1157. if(empty($data['lg_id'])) throw new \Exception(Response::generate('',Response::LEAGUE_ERROR)) ;//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  1158. //查询赛事是否存在
  1159. $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']])
  1160. ->value('id');
  1161. $half_match_id = 0;
  1162. //如果有上半场赛事id 获取上半场赛事是否存在
  1163. if(!empty($data['half_match_id'])){
  1164. $half_match_id = $match_id = $model['model_local_match']::where(['others_match_id'=>$data['half_match_id'],'source'=>$data['source']])
  1165. ->value('match_id');
  1166. 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'));
  1167. }
  1168. //如果赛事没有开始日期,则为冠军盘口赛事
  1169. if(empty($data['match_date'])){
  1170. //冠军盘口赛事获取所属联赛结束时间
  1171. $last_time = $model['model_league']::where(['id'=>$lg_id])
  1172. ->value('last_time');
  1173. 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'));
  1174. //给冠军盘口赛事 赋值时间
  1175. $time = strtotime($last_time);
  1176. $data['match_date'] = date('Y-m-d',$time);
  1177. $data['match_time'] = date('H:i:s',$time);
  1178. }
  1179. $set_match = [
  1180. 'ctime'=>date('Y-m-d H:i:s'),
  1181. 'utime'=>date('Y-m-d H:i:s'),
  1182. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  1183. 'home_team'=>$data['home_team']?:'',
  1184. 'guest_team'=>$data['guest_team']?:'no_team',
  1185. 'lg_id'=>$lg_id,
  1186. 'status'=>$data['status'],
  1187. 'match_date'=>$data['match_date']?:date('Y-m-d'),
  1188. 'match_time'=>$data['match_time']?:date('H:i:s'),
  1189. 'tag'=>$data['tag']?:0,
  1190. 'is_rollball'=>$data['is_rollball']?:0,
  1191. 'is_today'=>$data['is_today']?:0,
  1192. 'is_morningplate'=>$data['is_morningplate']?:0,
  1193. 'is_stringscene'=>$data['is_stringscene']?:0,
  1194. 'us_time'=>$data['us_time']?:commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  1195. 'half_match_id'=>$half_match_id?:0,
  1196. 'identity' => $data['uuid'],
  1197. ];
  1198. //写入赛事
  1199. if(empty($id)){
  1200. //写入赛事 返回id
  1201. $id = $model['model_match']::insertGetId($set_match);
  1202. if($id < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::INSERT_ERROR)) ;
  1203. }else{
  1204. //更新赛事
  1205. $ret = $model['model_match']::where(['id'=>$id])->update($set_match);
  1206. if($ret < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::UPDATE_ERROR)) ;
  1207. }
  1208. //写关联记录
  1209. $set_local = [
  1210. 'match_id'=>$id,
  1211. 'others_match_id'=>$data['match_id'],
  1212. 'source'=>$data['source'],
  1213. 'ctime'=>date('Y-m-d H:i:s')
  1214. ];
  1215. $ret = $model['model_local_match']::insertGetId($set_local);
  1216. 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'));
  1217. //返回 源数据 赛事ID
  1218. $data = ['others_match_id'=>$data['match_id'],'match_id'=>$id];
  1219. return $data;
  1220. }
  1221. /*
  1222. * 写入赔率/赔率记录数据
  1223. */
  1224. public function odds($opt=[],$gameName='',$lg_id=0,$match_id=0,$oddsID='',$oddsRecordID=''){
  1225. $game_code = $opt['game_code'];
  1226. //根据球类代码获取相关model
  1227. $model = commonFunction::getModels($game_code,1);
  1228. $data = $opt['data'];
  1229. //获取赛事 本地/源ID
  1230. $others_match_id = $data['match_id'];
  1231. //获取联赛 本地/源ID
  1232. $others_lg_id = $data['lg_id'];
  1233. //===写赔率记录===
  1234. $set_odds_r = [
  1235. 'match_id'=> $match_id?:0,
  1236. 'others_match_id'=> $others_match_id?:0,
  1237. 'odds_code'=> $data['odds_code']?:'',
  1238. 'status'=> $data['status']?:0,
  1239. 'sort'=> $data['sort']?:0,
  1240. 'p_code'=> $data['p_code']?:'',
  1241. 'odds'=> $data['odds']?:0,
  1242. 'condition'=> $data['condition'],
  1243. 'odds_only'=> $data['odds_only']?:'',
  1244. 'source'=> $data['source']?:'',
  1245. 'type'=> $data['type']?:0,
  1246. 'team'=> $data['team']?:'',
  1247. 'lg_id'=> $lg_id,
  1248. 'others_lg_id'=> $others_lg_id,
  1249. 'ctime'=> date('Y-m-d H:i:s'),
  1250. ];
  1251. //更新或写入赔率记录
  1252. if($oddsRecordID != false){
  1253. $ret = $model['model_odds_record']::where(['id'=>$oddsRecordID])
  1254. -> update($set_odds_r);
  1255. 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'));
  1256. }else{
  1257. $ret = $model['model_odds_record']::insert($set_odds_r);
  1258. 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'));
  1259. }
  1260. //===写赔率===
  1261. $set_odds = [
  1262. 'match_id'=> $match_id?:0,
  1263. 'others_match_id'=> $others_match_id?:0,
  1264. 'odds_code'=> $data['odds_code']?:'',
  1265. 'status'=> $data['status']?:0,
  1266. 'sort'=> $data['sort']?:0,
  1267. 'p_code'=> $data['p_code']?:'',
  1268. 'odds'=> $data['odds']?:0,
  1269. 'condition'=> $data['condition'],
  1270. 'odds_only'=> $data['odds_only']?:'',
  1271. 'sole'=> $data['sole']?:'',
  1272. 'source'=> $data['source']?:'',
  1273. 'type'=> $data['type']?:0,
  1274. 'team'=> $data['team']?:'',
  1275. 'lg_id'=> $lg_id,
  1276. 'others_lg_id'=> $others_lg_id,
  1277. 'ctime'=> date('Y-m-d H:i:s'),
  1278. 'utime'=> date('Y-m-d H:i:s'),
  1279. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  1280. ];
  1281. //更新或写入赔率数据
  1282. if($oddsID != false){
  1283. $ret = $model['model_odds']::where(['id'=>$oddsID])
  1284. -> update($set_odds);
  1285. 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'));
  1286. }else{
  1287. $ret = $model['model_odds']::insert($set_odds);
  1288. 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'));
  1289. }
  1290. //===end===
  1291. }
  1292. /*
  1293. * 更新 赛事下 所有不在本次请求的 赔率状态
  1294. */
  1295. public function upOddsStatus($opt = [],$gameName=''){
  1296. //获取球类代码
  1297. $game_code = $opt['game_code'];
  1298. //获取球类model
  1299. $model = commonFunction::getModels($game_code,1);
  1300. //赛事下 所有 赔率sole
  1301. $odds_sole = $opt['data'];
  1302. //数据源 赛事 id
  1303. $others_match_id = $opt['match_id'];
  1304. //数据源
  1305. $source = $opt['source'];
  1306. //更新赛事下所有 不在本次请求的赔率 状态
  1307. $ret= OddsModel::upOddsStatus($model,$others_match_id,'',$source,$odds_sole);
  1308. if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  1309. }
  1310. /*
  1311. * 写入联赛结果
  1312. */
  1313. public function league_result($opt,$gameName){
  1314. $game_code = $opt['game_code'];
  1315. //根据球类代码获取相关model
  1316. $model = commonFunction::getModels($game_code,1);
  1317. $data = $opt['data'];
  1318. //验证结果所属联赛
  1319. $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  1320. ->value('lg_id');
  1321. if($lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::LEAGUE_ERROR));
  1322. $lg_result_id = $model['model_league_result']::where(['lg_id'=>$lg_id,'game_name'=>$data['game_name']])
  1323. ->value('id');
  1324. $set_lg_result = [
  1325. 'lg_id'=>$lg_id,
  1326. 'game_name'=>$data['game_name'],
  1327. 'result'=>json_encode($data['result'],JSON_UNESCAPED_UNICODE),
  1328. 'status'=>$data['status'],
  1329. 'ctime'=> date('Y-m-d H:i:s'),
  1330. 'utime'=> date('Y-m-d H:i:s'),
  1331. ];
  1332. //联赛结果数据处理
  1333. if(!empty($lg_result_id)){
  1334. $ret = $model['model_league_result']::where(['id'=>$lg_result_id])
  1335. -> update($set_lg_result);
  1336. 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'));
  1337. }else{
  1338. $ret = $model['model_league_result']::insert($set_lg_result);
  1339. 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'));
  1340. }
  1341. }
  1342. /*
  1343. * 写入赛事结果
  1344. */
  1345. public function com_result($opt=[],$gameName='',$lg_id='',$match_id=''){
  1346. $game_code = $opt['game_code'];
  1347. //根据球类代码获取相关model
  1348. $model = commonFunction::getModels($game_code,1);
  1349. $data = $opt['data'];
  1350. //查询结果是否存在
  1351. $match_r_id = $model['model_result']::where(['match_id'=>$match_id])
  1352. ->value('id');
  1353. $set_match_r = [
  1354. "home_team"=>$data['home_team'],
  1355. "guest_team"=>$data['guest_team'],
  1356. "lg_id"=>$lg_id,
  1357. "home_rate"=> $data['home_rate'],
  1358. "guest_rate"=> $data['guest_rate'],
  1359. "home_score"=> $data['home_score'],
  1360. "guest_score"=> $data['guest_score'],
  1361. "all_goal"=> $data['all_goal'],
  1362. "status"=>$data['status'],
  1363. "first_score"=>$data['first_score'],
  1364. "last_score"=> $data['last_score'],
  1365. "match_score"=> $data['match_score'],
  1366. "match_winer"=> $data['match_winer'],
  1367. "match_time"=> $data['match_time'],
  1368. "match_process"=> $data['match_process'],
  1369. "tag"=> $data['tag'],
  1370. "match_id"=> $match_id,
  1371. "u_home_score"=> $data['u_home_score'],
  1372. "u_guest_score"=> $data['u_guest_score'],
  1373. "p_code"=> $data['p_code'],
  1374. "update_time"=>date('Y-m-d H:i:s')
  1375. ];
  1376. //赛事结果数据处理
  1377. if(!empty($match_r_id)){
  1378. $ret = $model['model_result']::where(['id'=>$match_r_id])
  1379. -> update($set_match_r);
  1380. 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'));
  1381. }else{
  1382. $ret = $model['model_result']::insert($set_match_r);
  1383. 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'));
  1384. }
  1385. // return Response::success();
  1386. }
  1387. //写入赛事结果记录
  1388. public function com_result_record($opt=[],$gameName='',$lg_id='',$match_id=''){
  1389. $game_code = $opt['game_code'];
  1390. //根据球类代码获取相关model
  1391. $model = commonFunction::getModels($game_code,1);
  1392. $data = $opt['data'];
  1393. $match_r_id = $model['model_result_record']::where(['match_id'=>$match_id,'match_time'=>$data['match_time']])
  1394. ->value('id');
  1395. //根据球类 获取 赛事结果记录字段
  1396. $set_match_r = $this->get_match_r($game_code,$lg_id,$match_id,$data);
  1397. //赛事结果记录处理
  1398. if($match_r_id > 0){
  1399. $ret = $model['model_result_record']::where(['id'=>$match_r_id])
  1400. ->update($set_match_r);
  1401. 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'));
  1402. }else{
  1403. $ret = $model['model_result_record']::insert($set_match_r);
  1404. 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'));
  1405. }
  1406. }
  1407. /**
  1408. * @param $data
  1409. * @return mixed
  1410. * json转数组
  1411. */
  1412. public function getAddData($data){
  1413. $data = json_decode($data,true);
  1414. return $data;
  1415. }
  1416. /*
  1417. * 根据球类获取 赛事结果记录字段
  1418. */
  1419. public function get_match_r($game_code,$lg_id,$match_id,$data){
  1420. $set_match_r = [];
  1421. if($game_code == 'zq'){
  1422. $set_match_r = [
  1423. "home_team"=>$data['home_team']?:'',
  1424. "guest_team"=>$data['guest_team']?:'',
  1425. "lg_id"=>$lg_id,
  1426. "all_goal"=>$data['all_goal']?:0,
  1427. "home_rate"=> $data['home_rate']?:0,
  1428. "guest_rate"=> $data['guest_rate']?:0,
  1429. "home_score"=> $data['home_score']?:0,
  1430. "guest_score"=> $data['guest_score']?:0,
  1431. "status"=>$data['status']?:0,
  1432. "first_score"=>$data['first_score']?:0,
  1433. "last_score"=> $data['last_score']?:0,
  1434. "match_score"=> $data['match_score']?:0,
  1435. "match_winer"=> $data['match_winer']?:'',
  1436. "match_time"=> $data['match_time']?:0,
  1437. "match_process"=> $data['match_process']?:'',
  1438. // "tag"=> $data['tag']?:0,
  1439. "match_id"=> $match_id,
  1440. "update_time"=>date('Y-m-d H:i:s')
  1441. ];
  1442. };
  1443. if($game_code == 'lq'){
  1444. $set_match_r = [
  1445. "home_team"=>$data['home_team']?:'',
  1446. "guest_team"=>$data['guest_team']?:'',
  1447. "lg_id"=>$lg_id,
  1448. "home_rate"=> $data['home_rate']?:0,
  1449. "guest_rate"=> $data['guest_rate']?:0,
  1450. "home_score"=> $data['home_score']?:0,
  1451. "guest_score"=> $data['guest_score']?:0,
  1452. "status"=>$data['status']?:0,
  1453. "first_score"=>$data['first_score']?:0,
  1454. "last_score"=> $data['last_score']?:0,
  1455. "match_score"=> $data['match_score']?:0,
  1456. "match_winer"=> $data['match_winer']?:'',
  1457. "match_time"=> $data['match_time']?:0,
  1458. "match_process"=> $data['match_process']?:'',
  1459. // "tag"=> $data['tag']?:0,
  1460. "match_id"=> $match_id,
  1461. "update_time"=>date('Y-m-d H:i:s')
  1462. ];
  1463. }
  1464. if($game_code == 'wq'){
  1465. $set_match_r = [
  1466. "home_player_name"=>$data['home_player_name']?:'',
  1467. "guest_player_name"=>$data['guest_player_name']?:'',
  1468. "lg_id"=>$lg_id,
  1469. "home_player_let_plate"=>$data['home_player_let_plate']?:0,
  1470. "guest_player_let_plate"=>$data['guest_player_let_plate']?:0,
  1471. "home_player_let_inning"=>$data['home_player_let_inning']?:0,
  1472. "guest_player_let_inning"=>$data['guest_player_let_inning']?:0,
  1473. "all_inning"=>$data['all_inning']?:0,
  1474. "home_player_score"=>$data['home_player_score']?:0,
  1475. "guest_player_score"=>$data['guest_player_score']?:0,
  1476. "status"=>$data['status']?:0,
  1477. "first_score_player"=>$data['first_score_player']?:'',
  1478. "last_score_player"=>$data['last_score_player']?:'',
  1479. "first_inning_score"=>$data['first_inning_score']?:0,
  1480. "second_inning_score"=>$data['second_inning_score']?:0,
  1481. "third_inning_score"=>$data['third_inning_score']?:0,
  1482. "match_winer_player"=>$data['match_winer_player']?:'',
  1483. "update_time"=>date('Y-m-d H:i:s'),
  1484. "match_time"=>$data['match_time']?:0,
  1485. "match_process"=>$data['match_process']?:'',
  1486. // "tag"=>$data['tag']?:0,
  1487. "match_id"=>$match_id,
  1488. "result_mark" =>$data['result_mark']?:'',
  1489. ];
  1490. }
  1491. if($game_code == 'bq'){
  1492. $set_match_r = [
  1493. "home_team"=>$data['home_team']?:'',
  1494. "guest_team"=>$data['guest_team']?:'',
  1495. "lg_id"=>$lg_id,
  1496. "home_rate"=> $data['home_rate']?:0,
  1497. "guest_rate"=> $data['guest_rate']?:0,
  1498. "home_score"=> $data['home_score']?:0,
  1499. "guest_score"=> $data['guest_score']?:0,
  1500. "status"=>$data['status']?:0,
  1501. "first_score"=>$data['first_score']?:0,
  1502. "last_score"=> $data['last_score']?:0,
  1503. "match_score"=> $data['match_score']?:0,
  1504. "match_winer"=> $data['match_winer']?:'',
  1505. "match_time"=> $data['match_time']?:0,
  1506. "match_process"=> $data['match_process']?:'',
  1507. // "tag"=> $data['tag']?:0,
  1508. "match_id"=> $match_id,
  1509. "all_inning"=>$data['all_inning']?:9,
  1510. "update_time"=>date('Y-m-d H:i:s'),
  1511. "result_mark" =>$data['result_mark']?:'',
  1512. ];
  1513. }
  1514. return $set_match_r;
  1515. }
  1516. }