WriteSportsController.php 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  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. if(empty($l_odds_data)){
  603. if(!empty($oddsData)){
  604. //处理待写入 赔率数据
  605. foreach($oddsData as $k=>$v){
  606. //组装数据
  607. $set_odds[] = [
  608. 'match_id'=> $match_id?:0,
  609. 'others_match_id'=> $v['match_id']?:0,
  610. 'odds_code'=> $v['odds_code']?:'',
  611. 'status'=> $v['status']?:0,
  612. 'sort'=> $v['sort']?:0,
  613. 'p_code'=> $v['p_code']?:'',
  614. 'odds'=> $v['odds']?:0,
  615. 'condition'=> $v['condition'],
  616. 'odds_only'=> $v['odds_only']?:'',
  617. 'source'=> $v['source']?:'',
  618. 'type'=> $v['type']?:0,
  619. 'team'=> $v['team']?:'',
  620. 'lg_id'=> $lg_id,
  621. 'others_lg_id'=> $v['lg_id'],
  622. 'ctime'=> date('Y-m-d H:i:s'),
  623. 'utime'=> date('Y-m-d H:i:s'),
  624. ];
  625. //写赔率记录数据
  626. $ret = $models['model_odds_record']::insert($set_odds);
  627. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率记录-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_R_ERROR));
  628. //追加参数
  629. $set_odds['sole'] = $v['sole'];
  630. $set_odds['expire_time'] = date('Y-m-d H:i:s',time()+60);
  631. //写赔率 数据
  632. $ret = $models['model_odds']::insert($set_odds);
  633. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_ERROR));
  634. }
  635. }
  636. }else{
  637. //区分本地已有的赔率
  638. if(!empty($oddsData)){
  639. foreach($oddsData as $k=>$v){
  640. foreach($l_odds_data as $kk=>$vv){
  641. if($v['odds_only'] == $vv['odds_only']){
  642. unset($oddsData[$k]);
  643. }
  644. }
  645. }
  646. sort($oddsData);
  647. /*
  648. //执行 更新赔率
  649. if(!empty($up_odds_data)){
  650. foreach($up_odds_data as $k=>$v){
  651. //组装数据
  652. $set_odds = [
  653. 'match_id'=> $match_id?:0,
  654. 'others_match_id'=> $v['match_id']?:0,
  655. 'odds_code'=> $v['odds_code']?:'',
  656. 'status'=> $v['status']?:0,
  657. 'sort'=> $v['sort']?:0,
  658. 'p_code'=> $v['p_code']?:'',
  659. 'odds'=> $v['odds']?:0,
  660. 'condition'=> $v['condition'],
  661. 'odds_only'=> $v['odds_only']?:'',
  662. 'source'=> $v['source']?:'',
  663. 'type'=> $v['type']?:0,
  664. 'team'=> $v['team']?:'',
  665. 'lg_id'=> $lg_id,
  666. 'others_lg_id'=> $v['lg_id'],
  667. 'ctime'=> date('Y-m-d H:i:s'),
  668. 'utime'=> date('Y-m-d H:i:s'),
  669. ];
  670. //写赔率记录数据
  671. $ret = $models['model_odds_record']::where(['odds_only'=>$v['odds_only']])
  672. -> update($set_odds);
  673. if($ret < 1) throw new \Exception(Response::generate($gameName.'赔率记录-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_R_ERROR));
  674. //追加参数
  675. $set_odds['sole'] = $v['sole'];
  676. $set_odds['expire_time'] = date('Y-m-d H:i:s',time()+60);
  677. //写赔率 数据
  678. $ret = $models['model_odds']::where(['odds_only'=>$v['odds_only']])
  679. -> update($set_odds);
  680. if($ret < 1) throw new \Exception(Response::generate($gameName.'赔率-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_ERROR));
  681. }
  682. }
  683. //执行 新增赔率
  684. if(!empty($oddsData)){
  685. foreach($oddsData as $k=>$v){
  686. //组装数据
  687. $set_odds[] = [
  688. 'match_id'=> $match_id?:0,
  689. 'others_match_id'=> $v['match_id']?:0,
  690. 'odds_code'=> $v['odds_code']?:'',
  691. 'status'=> $v['status']?:0,
  692. 'sort'=> $v['sort']?:0,
  693. 'p_code'=> $v['p_code']?:'',
  694. 'odds'=> $v['odds']?:0,
  695. 'condition'=> $v['condition'],
  696. 'odds_only'=> $v['odds_only']?:'',
  697. 'source'=> $v['source']?:'',
  698. 'type'=> $v['type']?:0,
  699. 'team'=> $v['team']?:'',
  700. 'lg_id'=> $lg_id,
  701. 'others_lg_id'=> $v['lg_id'],
  702. 'ctime'=> date('Y-m-d H:i:s'),
  703. 'utime'=> date('Y-m-d H:i:s'),
  704. ];
  705. //写赔率记录数据
  706. $ret = $models['model_odds_record']::insert($set_odds);
  707. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率记录-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_R_ERROR));
  708. //追加参数
  709. $set_odds['sole'] = $v['sole'];
  710. $set_odds['expire_time'] = date('Y-m-d H:i:s',time()+60);
  711. //写赔率 数据
  712. $ret = $models['model_odds']::insert($set_odds);
  713. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_ERROR));
  714. }
  715. }
  716. }
  717. }
  718. */
  719. //提交事务
  720. DB::commit();
  721. return Response::success();
  722. } catch (\Exception $e) {
  723. //回滚事务
  724. DB::rollBack();
  725. return $e->getMessage();
  726. }
  727. }
  728. /*
  729. *写 冠军联赛 赔率 数据
  730. *每个请求 包含 N个联赛 下 N条赔率
  731. */
  732. public function setOddsCH(Req $data){
  733. //验证token
  734. $user = new \App\Models\System_user;
  735. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  736. $token = $user::where(['token'=>$data->token])->value('token');
  737. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  738. try {
  739. //开启事务
  740. DB::beginTransaction();
  741. $obt = $data->data;
  742. //请求 数据 为空
  743. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  744. $getData = $this->getAddData($obt);
  745. //不是 冠军 赔率 数据
  746. if($getData['title'] != 'odds_ch') throw new \Exception(Response::generate('不是冠军赔率数据,',Response::ABNORMAL));
  747. /*
  748. //写请求数据 日志记录
  749. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  750. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  751. */
  752. //获取球类代码
  753. $game_code = $getData['game_code'];
  754. //获取数据源
  755. $source = $getData['source'];
  756. //获取球类名称
  757. $gameName = gameModel::getGameName($game_code);
  758. //获取 model
  759. $models = commonFunction::getModels($game_code, 1);
  760. //获取 赔率数据
  761. $oddsData = $getData['data'];
  762. //获取 当前 所有 可用 赔率 sole
  763. // $odds_only = $getData['odds_only'];
  764. //====验证 请求里 所有赔率 所属联赛 是否存在 ====
  765. //获取 当前请求 所有 联赛 uuid
  766. $uuids = [];
  767. foreach($oddsData as $k=>$v){
  768. $uuids[] = $v['uuid'];
  769. }
  770. $uuids = array_unique($uuids);
  771. sort($uuids);
  772. //更新赛事下所有 不在本次请求的赔率 状态
  773. // $ret= OddsModel::upOddsStatus($models,'',$s_lg_ids,$source,$odds_only);
  774. // if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  775. //获取 本地 已存在 联赛
  776. $l_lg_data = $models['model_local_league']::whereIn('identity',$uuids)->select('identity','lg_id')->get()->toArray();
  777. //二维数组去重
  778. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  779. //循环对比 请求uuid->本地uuid
  780. foreach($l_lg_data as $k=>$v){
  781. foreach($uuids as $kk=> $uuid){
  782. if($v['identity'] == $uuid){
  783. unset($uuids[$kk]);
  784. }
  785. }
  786. }
  787. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常;
  788. if(!empty($uuids)) throw new \Exception(Response::generate($gameName.'冠军联赛:uuid-'.$uuids[0].';',Response::LEAGUE_ERROR));
  789. //====end====
  790. //拼装 待写入 赔率数据
  791. foreach($oddsData as $k=>$v){
  792. //获取 本地 联赛 ID
  793. $lg_id = commonFunction::searcharray($data['uuid'],'identity',$l_lg_data,'lg_id');
  794. //组装数据
  795. $set_odds = [
  796. 'match_id'=>0,
  797. 'others_match_id'=> $v['match_id']?:0,
  798. 'odds_code'=> $v['odds_code']?:'',
  799. 'status'=> $v['status']?:0,
  800. 'sort'=> $v['sort']?:0,
  801. 'p_code'=> $v['p_code']?:'',
  802. 'odds'=> $v['odds']?:0,
  803. 'condition'=> $v['condition'],
  804. 'odds_only'=> $v['odds_only']?:'',
  805. 'source'=> $v['source']?:'',
  806. 'type'=> $v['type']?:1,
  807. 'team'=> $v['team']?:'',
  808. 'lg_id'=> $lg_id,
  809. 'others_lg_id'=> $v['lg_id'],
  810. 'ctime'=> date('Y-m-d H:i:s'),
  811. 'utime'=> date('Y-m-d H:i:s'),
  812. ];
  813. //写赔率记录数据
  814. $ret = $models['model_odds_record']->updateOrCreate(
  815. ['odds_only'=>$v['odds_only']],$set_odds
  816. );
  817. if(empty($ret)) throw new \Exception(Response::generate($gameName.'赔率记录-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_R_ERROR));
  818. //追加参数
  819. $set_odds['sole'] = $v['sole'];
  820. $set_odds['expire_time'] = date('Y-m-d H:i:s',time()+60);
  821. //写赔率 数据
  822. $ret = $models['model_odds']->updateOrCreate(
  823. ['odds_only'=>$v['odds_only']],$set_odds
  824. );
  825. if(empty($ret)) throw new \Exception(Response::generate($gameName.'赔率-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_ERROR));
  826. }
  827. //提交事务
  828. DB::commit();
  829. return Response::success();
  830. } catch (\Exception $e) {
  831. //回滚事务
  832. DB::rollBack();
  833. return $e->getMessage();
  834. }
  835. }
  836. /*
  837. * 写入直播 数据
  838. */
  839. public function setBroadCast(Req $data){
  840. //验证token
  841. $user = new \App\Models\System_user;
  842. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  843. $token = $user::where(['token'=>$data->token])->value('token');
  844. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  845. try {
  846. //开启事务
  847. DB::beginTransaction();
  848. $obt = $data->data;
  849. //请求 数据 为空
  850. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  851. $getData = $this->getAddData($obt);
  852. //不是 直播 数据
  853. if($getData['title'] != 'broad_cast') throw new \Exception(Response::generate('不是直播数据,',Response::ABNORMAL));
  854. /*
  855. //写请求数据 日志记录
  856. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  857. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  858. */
  859. //获取 球类
  860. $game_code = $getData['game_code'];
  861. //获取 直播 数据
  862. $BroadCast = $getData['data'];
  863. $set_broadcast = [];
  864. foreach($BroadCast as $k=>$data){
  865. //组装数据
  866. $set_broadcast[] = [
  867. "doing" => $data['doing'],
  868. "game_type" => $data['game_type'],
  869. "game_code" => $game_code,
  870. "guest_team" => $data['guest_team'],
  871. "host_team" => $data['host_team'],
  872. "league_name" => $data['league_name'],
  873. "shower" => $data['shower'],
  874. "showid" => (int)$data['showid'],
  875. "start_time" => $data['start_time'],
  876. "ctime" =>date('Y-m-d H:i:s'),
  877. "utime" =>date('Y-m-d H:i:s'),
  878. ];
  879. }
  880. //写入 直播 数据
  881. $ret = broadcastModel::insert($set_broadcast);
  882. if($ret == false) throw new \Exception(Response::generate('',Response::BROADCAST_ERROR));
  883. //提交事务
  884. DB::commit();
  885. return Response::success();
  886. } catch (\Exception $e) {
  887. //回滚事务
  888. DB::rollBack();
  889. return $e->getMessage();
  890. }
  891. }
  892. /*
  893. * 验证所属 联赛 是否存在
  894. */
  895. public function leagueVerify($models=[],$uuid='',$source='',$gameName=''){
  896. if(empty($models) || empty($uuid)) throw new \Exception(Response::generate('',Response::ABNORMAL));
  897. $l_lg_id = $models['model_local_league']::where(['identity'=>$uuid])->value('lg_id');
  898. if($l_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛:lg_id-'.$uuid,Response::LEAGUE_ERROR));
  899. return $l_lg_id;
  900. }
  901. /*
  902. * 验证所属 赛事 是否存在
  903. */
  904. public function matchVerify($models=[],$s_match_id='',$source='',$gameName=''){
  905. if(empty($models) || empty($s_match_id)) throw new \Exception(Response::generate('',Response::ABNORMAL));
  906. $l_match_id = $models['model_local_match']::where(['others_match_id'=>$s_match_id,'source'=>$source])->value('match_id');
  907. if($l_match_id < 1) throw new \Exception(Response::generate($gameName.'赛事:match_id-'.$s_match_id,Response::MATCH_ERROR));
  908. return $l_match_id;
  909. }
  910. /*
  911. * 写入数据 接口
  912. */
  913. public function setSports(Req $data){
  914. try {
  915. //开启事务
  916. DB::beginTransaction();
  917. if($data->game_code){
  918. //用于后台 将所有进行中的赛事写入结果
  919. $this->match_result($data->game_code);
  920. }else{
  921. //验证token
  922. $dd = $data->session()->get('adminInfo');
  923. if(empty($data->token) ||($dd['token'] != $data->token)) return Response::generate('',Response::TOKEN_ERR);
  924. //写赛事数据
  925. $obt = $data->data;
  926. if($obt){
  927. $getData = $this->getAddData($obt);
  928. $league = [];
  929. $competition = [];
  930. $odds = [];
  931. $odds_soly = [];
  932. $league_result = [];
  933. $match_result = [];
  934. $match_result_record = [];
  935. $odds_record = [];
  936. $broadcast = [];
  937. //指定排序 联赛->赛事->赔率->联赛结果->赛事结果->赛事结果记录->赔率记录->直播数据
  938. foreach ($getData as $k=>$v){
  939. $game_code = $v['game_code'];
  940. if($v['title'] == 'league') $league[] = $v;
  941. if($v['title'] == 'competition') $competition[] = $v;
  942. if($v['title'] == 'odds') $odds[] = $v;
  943. if($v['title'] == 'odds_sole') $odds_soly[] = $v;
  944. if($v['title'] == 'league_result') $league_result[] = $v;
  945. if($v['title'] == 'match_result') $match_result[] = $v;
  946. if($v['title'] == 'match_result_record') $match_result_record[] = $v;
  947. if($v['title'] == 'odds_record') $odds_record[] = $v;
  948. if($v['title'] == 'broadcast') $broadcast[] = $v;
  949. }
  950. $matchData = [$league,$competition,$odds_soly,$odds,$league_result,$match_result,$match_result_record,$odds_record,$broadcast];
  951. //排空处理
  952. foreach ($matchData as $k=>$v){
  953. if($v == []) unset($matchData[$k]);
  954. }
  955. sort($matchData);
  956. //根据顺序写入数据
  957. //降维数据
  958. $mentData = [];
  959. foreach ($matchData as $k=>$v){
  960. foreach ($v as $kk=>$vv){
  961. $mentData[] = $vv;
  962. }
  963. }
  964. //获取各球类model
  965. $models = commonFunction::getModels($game_code,1);
  966. //获取球类名称
  967. $gameName = gameModel::getGameName($game_code);
  968. //===获取验证数据===
  969. //获取一段时间内所有联赛本地记录
  970. $lg_data = LeagueModel::getLeagueID($models);
  971. //获取一段时间内所有赛事本地记录
  972. $match_data = MatchModel::getMatchID($models);
  973. //获取一段时间内所有赔率
  974. $odds_data = OddsModel::getOddsID($models);
  975. //获取一段时间内所有赔率记录
  976. $odds_record_data = OddsRecordModel::getOddsRecordID($models);
  977. //===获取验证数据===
  978. $others_lg_id = '';//继承 源联赛id
  979. $others_match_id = '';//继承 源赛事id
  980. $s_lg_id = '';//源 联赛ID
  981. $l_lg_id = '';//本地 联赛 ID
  982. $s_match_id = '';//源 赛事ID
  983. $l_match_id = '';//本地 赛事 ID
  984. foreach ($mentData as $kk =>$vv){
  985. switch ($vv['title']){
  986. case 'area'://地区
  987. throw new \Exception(Response::generate('地区数据-area:',Response::AUTH_ERROR)) ;
  988. break;
  989. case 'country'://国家
  990. throw new \Exception(Response::generate('国家数据-country:',Response::AUTH_ERROR));
  991. break;
  992. case 'league'://联赛
  993. $others_lg_id = $vv['data']['lg_id'];
  994. $s_lg_id = $vv['data']['lg_id'];
  995. $source = $vv['data']['source'];
  996. //验证联赛记录是否已存在
  997. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  998. if($ret_lg != false) {
  999. $l_lg_id = $ret_lg;
  1000. break;
  1001. }
  1002. //执行 联赛数据
  1003. $lg_data = $this->league($vv,$gameName);
  1004. $l_lg_id = $lg_data['lg_id'];
  1005. break;
  1006. case 'competition'://赛事
  1007. $others_match_id = $vv['data']['match_id'];
  1008. $s_lg_id = $vv['data']['lg_id'];
  1009. $s_match_id = $vv['data']['match_id'];
  1010. $source = $vv['data']['source'];
  1011. //本次请求是否包含 联赛数据
  1012. if(empty($others_lg_id)){
  1013. //验证本地是否存在
  1014. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  1015. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1016. $l_lg_id = $ret_lg;
  1017. }else{
  1018. //本次有 联赛数据 验证属于同联赛
  1019. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1020. }
  1021. //验证赛事记录是否已存在
  1022. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  1023. if($ret_match != false){
  1024. $l_match_id = $ret_match;
  1025. break;
  1026. }
  1027. //执行 赛事数据
  1028. $match_data = $this->competition($vv,$gameName,$l_lg_id);
  1029. $l_match_id = $match_data['match_id'];
  1030. break;
  1031. case 'odds'://赔率
  1032. $s_lg_id = $vv['data']['lg_id'];
  1033. $s_match_id = $vv['data']['match_id'];
  1034. $source = $vv['data']['source'];
  1035. $odds_only = $vv['data']['odds_only'];
  1036. $sole = $vv['data']['sole'];
  1037. $odds_type = $vv['data']['type'];//0普通 1冠军盘口
  1038. //本次请求是否包含 联赛数据
  1039. if(empty($others_lg_id)){
  1040. //无联赛数据 验证本地是否存在
  1041. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  1042. //联赛 不存在
  1043. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1044. $l_lg_id = $ret_lg;
  1045. }else{
  1046. //有联赛数据 验证属于同请求联赛
  1047. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1048. }
  1049. //如果是普通 赔率,则验证所属赛事
  1050. if($odds_type == 0){
  1051. //验证赛事记录是否已存在
  1052. if(empty($others_match_id)){
  1053. //无赛事数据 验证本地是否存在
  1054. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  1055. //赛事 不存在
  1056. if($ret_match == false) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  1057. $l_match_id = $ret_match;
  1058. }else{
  1059. //有赛事数据 验证属于同请求赛事
  1060. if($s_match_id != $others_match_id) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  1061. }
  1062. }
  1063. //验证赔率 是否存在
  1064. $ret_odds = '';
  1065. $ret_odds = commonFunction::ver_odds($sole,$source,$odds_data);
  1066. //验证赔率记录 是否存在
  1067. $ret_odds_record = '';
  1068. $ret_odds_record = commonFunction::ver_odds_record($odds_only,$source,$odds_record_data);
  1069. $this->odds($vv,$gameName,$l_lg_id,$l_match_id,$ret_odds,$ret_odds_record);
  1070. break;
  1071. case 'odds_sole':
  1072. $this->upOddsStatus($vv,$gameName);
  1073. break;
  1074. case 'league_result'://联赛结果
  1075. $this->league_result($vv,$gameName);
  1076. break;
  1077. case 'match_result'://赛事结果
  1078. throw new \Exception(Response::generate('赛事结果数据-match_result:',Response::AUTH_ERROR));
  1079. $this->match_result($vv,$gameName);
  1080. break;
  1081. case 'match_result_record'://赛事结果记录
  1082. $s_lg_id = $vv['data']['lg_id'];
  1083. $s_match_id = $vv['data']['match_id'];
  1084. $source = $vv['data']['source'];
  1085. //如果本次请求没有 联赛数据
  1086. if(empty($others_lg_id)){
  1087. //验证本地是否存在
  1088. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  1089. //联赛 不存在
  1090. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1091. $l_lg_id = $ret_lg;
  1092. }else{
  1093. //有联赛数据 验证属于同请求联赛
  1094. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1095. }
  1096. //验证赛事记录是否已存在
  1097. if(empty($others_match_id)){
  1098. //无赛事数据 验证本地是否存在
  1099. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  1100. //联赛 不存在
  1101. if($ret_match == false) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  1102. $l_match_id = $ret_match;
  1103. }else{
  1104. //有赛事数据 验证属于同请求赛事
  1105. if($s_match_id != $others_match_id) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  1106. }
  1107. //处理 赛事结果 记录
  1108. $this->com_result_record($vv,$gameName,$l_lg_id,$l_match_id);
  1109. break;
  1110. case 'broadcast'://直播数据
  1111. $this->broadcast($vv,$game_code);
  1112. break;
  1113. default:
  1114. throw new \Exception(Response::generate('',Response::ABNORMAL)) ;
  1115. }
  1116. }
  1117. }
  1118. }
  1119. //提交事务
  1120. DB::commit();
  1121. return Response::success();
  1122. } catch (\Exception $e) {
  1123. //回滚事务
  1124. DB::rollBack();
  1125. return $e->getMessage();
  1126. }
  1127. }
  1128. /**
  1129. * @param Req $data
  1130. * @return string
  1131. * @throws \App\Lib\Biz\Sport\Exception
  1132. * 更新赛事状态
  1133. */
  1134. public function upMatch(Req $data){
  1135. //验证token
  1136. $user = new \App\Models\System_user;
  1137. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  1138. $token = $user::where(['token'=>$data->token])->value('token');
  1139. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  1140. try {
  1141. //开启事务
  1142. DB::beginTransaction();
  1143. //获取待更新赛事
  1144. $obt = $data->data;
  1145. if($obt){
  1146. //json转数组
  1147. $data = $this->getAddData($obt);
  1148. //获取 球类代码
  1149. $game_code = $data['game_code'];
  1150. //获取 数据源
  1151. $source = $data['source'];
  1152. //获取所有数据源赛事 match_id
  1153. $others_match_ids = [];
  1154. foreach ($data['data'] as $k=>$v){
  1155. $others_match_ids[] = $v['match_id'];
  1156. }
  1157. //根据球类代码 获取model
  1158. $model =commonFunction::getModels($game_code,1);
  1159. //获取所有赛事 match_id
  1160. $local_match = $model['model_local_match']::SELECT('others_match_id','match_id')
  1161. ->where(['source'=>$source])
  1162. ->whereIn('others_match_id',$others_match_ids)
  1163. ->get()->toArray();
  1164. if(empty($local_match)) throw new \Exception(Response::generate('',Response::MATCHID_NULL));
  1165. //更新状态字段
  1166. foreach ($local_match as $k=>$v){
  1167. $set_status = [
  1168. 'status'=>$v['status'],
  1169. 'is_rollball'=>$v['is_rollball'],
  1170. 'stais_todaytus'=>$v['is_today'],
  1171. 'is_morningplate'=>$v['is_morningplate'],
  1172. 'is_stringscene'=>$v['is_stringscene'],
  1173. 'is_horn'=>$v['is_horn'],
  1174. ];
  1175. $ret = $model['model_match']::where(['id'=>$v['match_id']])
  1176. -> update($set_status);
  1177. if($ret<1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$v['others_match_id'],Response::UPSTATUS_ERROR));
  1178. }
  1179. }
  1180. //提交事务
  1181. DB::commit();
  1182. return Response::success();
  1183. } catch (\Exception $e) {
  1184. //回滚事务
  1185. DB::rollBack();
  1186. return $e->getMessage();
  1187. }
  1188. }
  1189. /*
  1190. * 写入直播 数据
  1191. */
  1192. public function broadcast($opt = [],$game_code=''){
  1193. $data = $opt['data'];
  1194. $set_broadcast = [
  1195. "doing" => $data['doing'],
  1196. "game_type" => $data['game_type'],
  1197. "game_code" => $game_code,
  1198. "guest_team" => $data['guest_team'],
  1199. "host_team" => $data['host_team'],
  1200. "league_name" => $data['league_name'],
  1201. "shower" => $data['shower'],
  1202. "showid" => (int)$data['showid'],
  1203. "start_time" => $data['start_time'],
  1204. "ctime" =>date('Y-m-d H:i:s'),
  1205. "utime" =>date('Y-m-d H:i:s'),
  1206. ];
  1207. $ret = broadcastModel::insert($set_broadcast);
  1208. if($ret == false) throw new \Exception(Response::generate('',Response::BROADCAST_ERROR));
  1209. }
  1210. /*
  1211. * 写入 赛事 初始 结果
  1212. */
  1213. public function match_result($game_code = ''){
  1214. $model =commonFunction::getModels($game_code,1);
  1215. if($game_code == 'zq') ZqResultModel::ZQresult($model);
  1216. if($game_code == 'lq') LqResultModel::LQresult($model);
  1217. if($game_code == 'wq') WqResultModel::WQresult($model);
  1218. if($game_code == 'bq') BqResultModel::BQresult($model);
  1219. }
  1220. /*
  1221. * 写入联赛数据
  1222. */
  1223. public function league($opt = [],$gameName=''){
  1224. $game_code = $opt['game_code'];
  1225. //根据球类代码获取相关model
  1226. $model =commonFunction::getModels($game_code,1);
  1227. $data = $opt['data'];
  1228. //查询联赛是否已存在
  1229. $id = $model['model_league']::where('name_chinese','=',$data['name_chinese'])
  1230. ->value('id');
  1231. //默认获取本年最后一天
  1232. $last_time = date('Y-12-31 23:59:59');
  1233. //决赛时间
  1234. if($data['last_time']){
  1235. $last_time = $data['last_time'];
  1236. }
  1237. $set_lg['name_chinese'] = $data['name_chinese'];
  1238. $set_lg['kind'] = $data['kind'];
  1239. $set_lg['match_mode'] = $data['match_mode'];
  1240. $set_lg['if_stop'] = $data['if_stop'];
  1241. $set_lg['identity'] = $data['uuid']?:'';
  1242. $set_lg['last_time'] = $last_time;
  1243. $set_lg['utime'] = date('Y-m-d H:i:s');
  1244. if(empty($id)){
  1245. //写入联赛
  1246. $id = $model['model_league']::insertGetId($set_lg);
  1247. $m_lg_id = $id;
  1248. if($m_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::INSERT_ERROR));
  1249. }else{
  1250. //更新联赛
  1251. $ret = $model['model_league']::where(['id'=>$id])
  1252. -> update($set_lg);
  1253. if($ret < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::UPDATE_ERROR));
  1254. }
  1255. $set_local = [
  1256. 'lg_id'=>$id,
  1257. 'others_lg_id'=>$data['lg_id'],
  1258. 'source'=>$data['source'],
  1259. 'ctime'=>date('Y-m-d H:i:s')
  1260. ];
  1261. $ret = $model['model_local_league']::insertGetId($set_local);
  1262. 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'));
  1263. //执行成功返回源数据 联赛ID
  1264. $data = ['others_lg_id'=>$data['lg_id'],'lg_id'=>$id];
  1265. return $data;
  1266. }
  1267. /*
  1268. * 写入赛事数据
  1269. */
  1270. public function competition($opt = [],$gameName='',$lg_id=''){
  1271. $game_code = $opt['game_code'];
  1272. //根据球类代码获取相关model
  1273. $model = commonFunction::getModels($game_code,1);
  1274. $data = $opt['data'];
  1275. if(empty($data['lg_id'])) throw new \Exception(Response::generate('',Response::LEAGUE_ERROR)) ;//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  1276. //查询赛事是否存在
  1277. $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']])
  1278. ->value('id');
  1279. $half_match_id = 0;
  1280. //如果有上半场赛事id 获取上半场赛事是否存在
  1281. if(!empty($data['half_match_id'])){
  1282. $half_match_id = $match_id = $model['model_local_match']::where(['others_match_id'=>$data['half_match_id'],'source'=>$data['source']])
  1283. ->value('match_id');
  1284. 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'));
  1285. }
  1286. //如果赛事没有开始日期,则为冠军盘口赛事
  1287. if(empty($data['match_date'])){
  1288. //冠军盘口赛事获取所属联赛结束时间
  1289. $last_time = $model['model_league']::where(['id'=>$lg_id])
  1290. ->value('last_time');
  1291. 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'));
  1292. //给冠军盘口赛事 赋值时间
  1293. $time = strtotime($last_time);
  1294. $data['match_date'] = date('Y-m-d',$time);
  1295. $data['match_time'] = date('H:i:s',$time);
  1296. }
  1297. $set_match = [
  1298. 'ctime'=>date('Y-m-d H:i:s'),
  1299. 'utime'=>date('Y-m-d H:i:s'),
  1300. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  1301. 'home_team'=>$data['home_team']?:'',
  1302. 'guest_team'=>$data['guest_team']?:'no_team',
  1303. 'lg_id'=>$lg_id,
  1304. 'status'=>$data['status'],
  1305. 'match_date'=>$data['match_date']?:date('Y-m-d'),
  1306. 'match_time'=>$data['match_time']?:date('H:i:s'),
  1307. 'tag'=>$data['tag']?:0,
  1308. 'is_rollball'=>$data['is_rollball']?:0,
  1309. 'is_today'=>$data['is_today']?:0,
  1310. 'is_morningplate'=>$data['is_morningplate']?:0,
  1311. 'is_stringscene'=>$data['is_stringscene']?:0,
  1312. 'us_time'=>$data['us_time']?:commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  1313. 'half_match_id'=>$half_match_id?:0,
  1314. 'identity' => $data['uuid'],
  1315. ];
  1316. //写入赛事
  1317. if(empty($id)){
  1318. //写入赛事 返回id
  1319. $id = $model['model_match']::insertGetId($set_match);
  1320. if($id < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::INSERT_ERROR)) ;
  1321. }else{
  1322. //更新赛事
  1323. $ret = $model['model_match']::where(['id'=>$id])->update($set_match);
  1324. if($ret < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::UPDATE_ERROR)) ;
  1325. }
  1326. //写关联记录
  1327. $set_local = [
  1328. 'match_id'=>$id,
  1329. 'others_match_id'=>$data['match_id'],
  1330. 'source'=>$data['source'],
  1331. 'ctime'=>date('Y-m-d H:i:s')
  1332. ];
  1333. $ret = $model['model_local_match']::insertGetId($set_local);
  1334. 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'));
  1335. //返回 源数据 赛事ID
  1336. $data = ['others_match_id'=>$data['match_id'],'match_id'=>$id];
  1337. return $data;
  1338. }
  1339. /*
  1340. * 写入赔率/赔率记录数据
  1341. */
  1342. public function odds($opt=[],$gameName='',$lg_id=0,$match_id=0,$oddsID='',$oddsRecordID=''){
  1343. $game_code = $opt['game_code'];
  1344. //根据球类代码获取相关model
  1345. $model = commonFunction::getModels($game_code,1);
  1346. $data = $opt['data'];
  1347. //获取赛事 本地/源ID
  1348. $others_match_id = $data['match_id'];
  1349. //获取联赛 本地/源ID
  1350. $others_lg_id = $data['lg_id'];
  1351. //===写赔率记录===
  1352. $set_odds_r = [
  1353. 'match_id'=> $match_id?:0,
  1354. 'others_match_id'=> $others_match_id?:0,
  1355. 'odds_code'=> $data['odds_code']?:'',
  1356. 'status'=> $data['status']?:0,
  1357. 'sort'=> $data['sort']?:0,
  1358. 'p_code'=> $data['p_code']?:'',
  1359. 'odds'=> $data['odds']?:0,
  1360. 'condition'=> $data['condition'],
  1361. 'odds_only'=> $data['odds_only']?:'',
  1362. 'source'=> $data['source']?:'',
  1363. 'type'=> $data['type']?:0,
  1364. 'team'=> $data['team']?:'',
  1365. 'lg_id'=> $lg_id,
  1366. 'others_lg_id'=> $others_lg_id,
  1367. 'ctime'=> date('Y-m-d H:i:s'),
  1368. ];
  1369. //更新或写入赔率记录
  1370. if($oddsRecordID != false){
  1371. $ret = $model['model_odds_record']::where(['id'=>$oddsRecordID])
  1372. -> update($set_odds_r);
  1373. 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'));
  1374. }else{
  1375. $ret = $model['model_odds_record']::insert($set_odds_r);
  1376. 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'));
  1377. }
  1378. //===写赔率===
  1379. $set_odds = [
  1380. 'match_id'=> $match_id?:0,
  1381. 'others_match_id'=> $others_match_id?:0,
  1382. 'odds_code'=> $data['odds_code']?:'',
  1383. 'status'=> $data['status']?:0,
  1384. 'sort'=> $data['sort']?:0,
  1385. 'p_code'=> $data['p_code']?:'',
  1386. 'odds'=> $data['odds']?:0,
  1387. 'condition'=> $data['condition'],
  1388. 'odds_only'=> $data['odds_only']?:'',
  1389. 'sole'=> $data['sole']?:'',
  1390. 'source'=> $data['source']?:'',
  1391. 'type'=> $data['type']?:0,
  1392. 'team'=> $data['team']?:'',
  1393. 'lg_id'=> $lg_id,
  1394. 'others_lg_id'=> $others_lg_id,
  1395. 'ctime'=> date('Y-m-d H:i:s'),
  1396. 'utime'=> date('Y-m-d H:i:s'),
  1397. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  1398. ];
  1399. //更新或写入赔率数据
  1400. if($oddsID != false){
  1401. $ret = $model['model_odds']::where(['id'=>$oddsID])
  1402. -> update($set_odds);
  1403. 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'));
  1404. }else{
  1405. $ret = $model['model_odds']::insert($set_odds);
  1406. 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'));
  1407. }
  1408. //===end===
  1409. }
  1410. /*
  1411. * 更新 赛事下 所有不在本次请求的 赔率状态
  1412. */
  1413. public function upOddsStatus($opt = [],$gameName=''){
  1414. //获取球类代码
  1415. $game_code = $opt['game_code'];
  1416. //获取球类model
  1417. $model = commonFunction::getModels($game_code,1);
  1418. //赛事下 所有 赔率sole
  1419. $odds_sole = $opt['data'];
  1420. //数据源 赛事 id
  1421. $others_match_id = $opt['match_id'];
  1422. //数据源
  1423. $source = $opt['source'];
  1424. //更新赛事下所有 不在本次请求的赔率 状态
  1425. $ret= OddsModel::upOddsStatus($model,$others_match_id,'',$source,$odds_sole);
  1426. if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  1427. }
  1428. /*
  1429. * 写入联赛结果
  1430. */
  1431. public function league_result($opt,$gameName){
  1432. $game_code = $opt['game_code'];
  1433. //根据球类代码获取相关model
  1434. $model = commonFunction::getModels($game_code,1);
  1435. $data = $opt['data'];
  1436. //验证结果所属联赛
  1437. $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  1438. ->value('lg_id');
  1439. if($lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::LEAGUE_ERROR));
  1440. $lg_result_id = $model['model_league_result']::where(['lg_id'=>$lg_id,'game_name'=>$data['game_name']])
  1441. ->value('id');
  1442. $set_lg_result = [
  1443. 'lg_id'=>$lg_id,
  1444. 'game_name'=>$data['game_name'],
  1445. 'result'=>json_encode($data['result'],JSON_UNESCAPED_UNICODE),
  1446. 'status'=>$data['status'],
  1447. 'ctime'=> date('Y-m-d H:i:s'),
  1448. 'utime'=> date('Y-m-d H:i:s'),
  1449. ];
  1450. //联赛结果数据处理
  1451. if(!empty($lg_result_id)){
  1452. $ret = $model['model_league_result']::where(['id'=>$lg_result_id])
  1453. -> update($set_lg_result);
  1454. 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'));
  1455. }else{
  1456. $ret = $model['model_league_result']::insert($set_lg_result);
  1457. 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'));
  1458. }
  1459. }
  1460. /*
  1461. * 写入赛事结果
  1462. */
  1463. public function com_result($opt=[],$gameName='',$lg_id='',$match_id=''){
  1464. $game_code = $opt['game_code'];
  1465. //根据球类代码获取相关model
  1466. $model = commonFunction::getModels($game_code,1);
  1467. $data = $opt['data'];
  1468. //查询结果是否存在
  1469. $match_r_id = $model['model_result']::where(['match_id'=>$match_id])
  1470. ->value('id');
  1471. $set_match_r = [
  1472. "home_team"=>$data['home_team'],
  1473. "guest_team"=>$data['guest_team'],
  1474. "lg_id"=>$lg_id,
  1475. "home_rate"=> $data['home_rate'],
  1476. "guest_rate"=> $data['guest_rate'],
  1477. "home_score"=> $data['home_score'],
  1478. "guest_score"=> $data['guest_score'],
  1479. "all_goal"=> $data['all_goal'],
  1480. "status"=>$data['status'],
  1481. "first_score"=>$data['first_score'],
  1482. "last_score"=> $data['last_score'],
  1483. "match_score"=> $data['match_score'],
  1484. "match_winer"=> $data['match_winer'],
  1485. "match_time"=> $data['match_time'],
  1486. "match_process"=> $data['match_process'],
  1487. "tag"=> $data['tag'],
  1488. "match_id"=> $match_id,
  1489. "u_home_score"=> $data['u_home_score'],
  1490. "u_guest_score"=> $data['u_guest_score'],
  1491. "p_code"=> $data['p_code'],
  1492. "update_time"=>date('Y-m-d H:i:s')
  1493. ];
  1494. //赛事结果数据处理
  1495. if(!empty($match_r_id)){
  1496. $ret = $model['model_result']::where(['id'=>$match_r_id])
  1497. -> update($set_match_r);
  1498. 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'));
  1499. }else{
  1500. $ret = $model['model_result']::insert($set_match_r);
  1501. 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'));
  1502. }
  1503. // return Response::success();
  1504. }
  1505. //写入赛事结果记录
  1506. public function com_result_record($opt=[],$gameName='',$lg_id='',$match_id=''){
  1507. $game_code = $opt['game_code'];
  1508. //根据球类代码获取相关model
  1509. $model = commonFunction::getModels($game_code,1);
  1510. $data = $opt['data'];
  1511. $match_r_id = $model['model_result_record']::where(['match_id'=>$match_id,'match_time'=>$data['match_time']])
  1512. ->value('id');
  1513. //根据球类 获取 赛事结果记录字段
  1514. $set_match_r = $this->get_match_r($game_code,$lg_id,$match_id,$data);
  1515. //赛事结果记录处理
  1516. if($match_r_id > 0){
  1517. $ret = $model['model_result_record']::where(['id'=>$match_r_id])
  1518. ->update($set_match_r);
  1519. 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'));
  1520. }else{
  1521. $ret = $model['model_result_record']::insert($set_match_r);
  1522. 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'));
  1523. }
  1524. }
  1525. /**
  1526. * @param $data
  1527. * @return mixed
  1528. * json转数组
  1529. */
  1530. public function getAddData($data){
  1531. $data = json_decode($data,true);
  1532. return $data;
  1533. }
  1534. /*
  1535. * 根据球类获取 赛事结果记录字段
  1536. */
  1537. public function get_match_r($game_code,$lg_id,$match_id,$data){
  1538. $set_match_r = [];
  1539. if($game_code == 'zq'){
  1540. $set_match_r = [
  1541. "home_team"=>$data['home_team']?:'',
  1542. "guest_team"=>$data['guest_team']?:'',
  1543. "lg_id"=>$lg_id,
  1544. "all_goal"=>$data['all_goal']?:0,
  1545. "home_rate"=> $data['home_rate']?:0,
  1546. "guest_rate"=> $data['guest_rate']?:0,
  1547. "home_score"=> $data['home_score']?:0,
  1548. "guest_score"=> $data['guest_score']?:0,
  1549. "status"=>$data['status']?:0,
  1550. "first_score"=>$data['first_score']?:0,
  1551. "last_score"=> $data['last_score']?:0,
  1552. "match_score"=> $data['match_score']?:0,
  1553. "match_winer"=> $data['match_winer']?:'',
  1554. "match_time"=> $data['match_time']?:0,
  1555. "match_process"=> $data['match_process']?:'',
  1556. // "tag"=> $data['tag']?:0,
  1557. "match_id"=> $match_id,
  1558. "update_time"=>date('Y-m-d H:i:s')
  1559. ];
  1560. };
  1561. if($game_code == 'lq'){
  1562. $set_match_r = [
  1563. "home_team"=>$data['home_team']?:'',
  1564. "guest_team"=>$data['guest_team']?:'',
  1565. "lg_id"=>$lg_id,
  1566. "home_rate"=> $data['home_rate']?:0,
  1567. "guest_rate"=> $data['guest_rate']?:0,
  1568. "home_score"=> $data['home_score']?:0,
  1569. "guest_score"=> $data['guest_score']?:0,
  1570. "status"=>$data['status']?:0,
  1571. "first_score"=>$data['first_score']?:0,
  1572. "last_score"=> $data['last_score']?:0,
  1573. "match_score"=> $data['match_score']?:0,
  1574. "match_winer"=> $data['match_winer']?:'',
  1575. "match_time"=> $data['match_time']?:0,
  1576. "match_process"=> $data['match_process']?:'',
  1577. // "tag"=> $data['tag']?:0,
  1578. "match_id"=> $match_id,
  1579. "update_time"=>date('Y-m-d H:i:s')
  1580. ];
  1581. }
  1582. if($game_code == 'wq'){
  1583. $set_match_r = [
  1584. "home_player_name"=>$data['home_player_name']?:'',
  1585. "guest_player_name"=>$data['guest_player_name']?:'',
  1586. "lg_id"=>$lg_id,
  1587. "home_player_let_plate"=>$data['home_player_let_plate']?:0,
  1588. "guest_player_let_plate"=>$data['guest_player_let_plate']?:0,
  1589. "home_player_let_inning"=>$data['home_player_let_inning']?:0,
  1590. "guest_player_let_inning"=>$data['guest_player_let_inning']?:0,
  1591. "all_inning"=>$data['all_inning']?:0,
  1592. "home_player_score"=>$data['home_player_score']?:0,
  1593. "guest_player_score"=>$data['guest_player_score']?:0,
  1594. "status"=>$data['status']?:0,
  1595. "first_score_player"=>$data['first_score_player']?:'',
  1596. "last_score_player"=>$data['last_score_player']?:'',
  1597. "first_inning_score"=>$data['first_inning_score']?:0,
  1598. "second_inning_score"=>$data['second_inning_score']?:0,
  1599. "third_inning_score"=>$data['third_inning_score']?:0,
  1600. "match_winer_player"=>$data['match_winer_player']?:'',
  1601. "update_time"=>date('Y-m-d H:i:s'),
  1602. "match_time"=>$data['match_time']?:0,
  1603. "match_process"=>$data['match_process']?:'',
  1604. // "tag"=>$data['tag']?:0,
  1605. "match_id"=>$match_id,
  1606. "result_mark" =>$data['result_mark']?:'',
  1607. ];
  1608. }
  1609. if($game_code == 'bq'){
  1610. $set_match_r = [
  1611. "home_team"=>$data['home_team']?:'',
  1612. "guest_team"=>$data['guest_team']?:'',
  1613. "lg_id"=>$lg_id,
  1614. "home_rate"=> $data['home_rate']?:0,
  1615. "guest_rate"=> $data['guest_rate']?:0,
  1616. "home_score"=> $data['home_score']?:0,
  1617. "guest_score"=> $data['guest_score']?:0,
  1618. "status"=>$data['status']?:0,
  1619. "first_score"=>$data['first_score']?:0,
  1620. "last_score"=> $data['last_score']?:0,
  1621. "match_score"=> $data['match_score']?:0,
  1622. "match_winer"=> $data['match_winer']?:'',
  1623. "match_time"=> $data['match_time']?:0,
  1624. "match_process"=> $data['match_process']?:'',
  1625. // "tag"=> $data['tag']?:0,
  1626. "match_id"=> $match_id,
  1627. "all_inning"=>$data['all_inning']?:9,
  1628. "update_time"=>date('Y-m-d H:i:s'),
  1629. "result_mark" =>$data['result_mark']?:'',
  1630. ];
  1631. }
  1632. return $set_match_r;
  1633. }
  1634. }