WriteSportsController.php 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  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. protected $isRecord;
  32. public function __construct()
  33. {
  34. $this->isRecord = config('record.isRecord');
  35. }
  36. /*
  37. * 用户 用户 token
  38. */
  39. public function getToken(Req $req){
  40. if ($req->isMethod('post')) {
  41. $user = new \App\Models\System_user;
  42. $str = $user->adminLogin($_POST['account'], $_POST['password']);
  43. if($str>0){
  44. $token=refreshToken($str['admin_id']);
  45. $str['token']=$token;
  46. $data = $str;
  47. $req->session()->put('adminInfo',$str);
  48. $str=1;
  49. }
  50. $log = array(
  51. session('adminInfo.admin_name'),
  52. $_POST['account']
  53. );
  54. OperationLog(session('adminInfo.admin_id'), 'login', $log);
  55. return Response::success($data);
  56. }
  57. }
  58. /**
  59. * 根据联赛名称 获取 国家 地区
  60. */
  61. public function getArea($leagueName = ''){
  62. //如果联赛名称有括号,去除
  63. $leagueName = preg_replace('/\(.*?\)/', '', $leagueName);
  64. //获取所有国家/地区
  65. $areaData = StAreaCountryModel::select('id','pid','name')->get()->toArray();
  66. //获取当前联赛所属国家
  67. if(strpos($leagueName,'NBA') !== false){
  68. $data = [
  69. 'id'=>247,
  70. 'pid'=>0,
  71. 'name'=>'世界'
  72. ];
  73. return $data;
  74. }
  75. //识别本地 国家地区数据
  76. foreach($areaData as $v => $k){
  77. if(strpos($leagueName,$k['name']) !== false){
  78. return $k;
  79. }
  80. }
  81. //如果不能识别,则返回世界
  82. $data = [
  83. 'id'=>247,
  84. 'pid'=>0,
  85. 'name'=>'世界'
  86. ];
  87. return $data;
  88. }
  89. /*
  90. * 写 联赛 数据
  91. * 每个请求 包含 N个联赛
  92. */
  93. public function setLeague(Req $data){
  94. //验证token
  95. $user = new \App\Models\System_user;
  96. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  97. $token = $user::where(['token'=>$data->token])->value('token');
  98. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  99. try {
  100. //开启事务
  101. DB::beginTransaction();
  102. $obt = $data->data;
  103. //请求 数据 为空
  104. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  105. $getData = $this->getAddData($obt);
  106. //不是 联赛 数据
  107. if($getData['title'] != 'league') throw new \Exception(Response::generate('不是联赛数据,',Response::ABNORMAL));
  108. /*
  109. //写请求数据 日志记录
  110. if($this->isRecord){
  111. commonFunction::SET_SPORTS_RECORD($obt,$getData['title']);
  112. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  113. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  114. }
  115. */
  116. //获取球类代码
  117. $game_code = $getData['game_code'];
  118. //获取数据源
  119. $source = $getData['source'];
  120. //获取球类名称
  121. $gameName = gameModel::getGameName($game_code);
  122. //获取 model
  123. $models = commonFunction::getModels($game_code, 1);
  124. //获取 联赛 数据
  125. $leagueData = $getData['data'];
  126. //获取 当前请求 所有 联赛 uuid
  127. $identity = [];
  128. foreach($leagueData as $k=>$v){
  129. $identity[] = $v['uuid'];
  130. }
  131. $identity = array_unique($identity);
  132. sort($identity);
  133. //获取 本地 已存在 联赛
  134. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id')->get()->toArray();
  135. //二维数组去重
  136. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  137. //循环对比 请求identity->本地identity
  138. foreach($l_lg_data as $k=>$v){
  139. foreach($identity as $kk=> $uuid){
  140. if($v['identity'] == $uuid){
  141. unset($identity[$kk]);
  142. }
  143. }
  144. }
  145. //去除本地和请求里都存在的联赛,如果还有剩余联赛uuid,则将其写入
  146. if(!empty($identity)) {
  147. //没有 决赛时间 默认获取本年最后一天
  148. $last_time = date('Y-12-31 23:59:59');
  149. foreach($leagueData as $k=>$v){
  150. foreach($identity as $kk=> $uuid){
  151. if($v['uuid'] == $uuid){
  152. //如有 决赛时间
  153. if($v['last_time']){
  154. $last_time = $v['last_time'];
  155. }
  156. //获取联赛所属国家或地区
  157. $getArea = $this->getArea($v['name_chinese']);
  158. if($getArea['pid'] == 0){
  159. $area_id = $getArea['id'];
  160. $country_id = 0;
  161. }else{
  162. $area_id = $getArea['pid'];
  163. $country_id = $getArea['id'];
  164. }
  165. if(empty($v['uuid'])) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$v['lg_id'].';',Response::LG__UUID_NULL));
  166. $set_lg['name_chinese'] = $v['name_chinese'];
  167. $set_lg['kind'] = $v['kind'];
  168. $set_lg['match_mode'] = $v['match_mode'];
  169. $set_lg['if_stop'] = $v['if_stop'];
  170. $set_lg['identity'] = $v['uuid'];
  171. $set_lg['last_time'] = $last_time;
  172. $set_lg['utime'] = date('Y-m-d H:i:s');
  173. $set_lg['area_id'] = $area_id;
  174. $set_lg['country_id'] = $country_id;
  175. //写入联赛
  176. $id = $models['model_league']::insertGetId($set_lg);
  177. $m_lg_id = $id;
  178. if($m_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$v['lg_id'].';',Response::INSERT_ERROR));
  179. //写入 本地 联赛记录
  180. $set_local = [
  181. 'lg_id'=>$id,
  182. 'others_lg_id'=>$v['lg_id'],
  183. 'source'=>$source,
  184. 'ctime'=>date('Y-m-d H:i:s'),
  185. 'identity'=>$v['uuid'],
  186. ];
  187. $ret = $models['model_local_league']::insertGetId($set_local);
  188. 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'));
  189. }
  190. }
  191. }
  192. }
  193. //提交事务
  194. DB::commit();
  195. //写请求数据 日志记录
  196. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],Response::success());
  197. return Response::success();
  198. } catch (\Exception $e) {
  199. //回滚事务
  200. DB::rollBack();
  201. //写请求数据 日志记录
  202. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],$e->getMessage());
  203. return $e->getMessage();
  204. }
  205. }
  206. /*
  207. * 写 赛事 数据
  208. * 每个请求 包含 N个联赛 下 N个赛事
  209. */
  210. public function setMatch(Req $data){
  211. //验证token
  212. $user = new \App\Models\System_user;
  213. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  214. $token = $user::where(['token'=>$data->token])->value('token');
  215. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  216. try {
  217. //开启事务
  218. DB::beginTransaction();
  219. $obt = $data->data;
  220. //请求 数据 为空
  221. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  222. $getData = $this->getAddData($obt);
  223. //不是 赛事 数据
  224. if($getData['title'] != 'match') throw new \Exception(Response::generate('不是赛事数据,',Response::ABNORMAL));
  225. /*
  226. //写请求数据 日志记录
  227. if($this->isRecord){
  228. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  229. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  230. }
  231. */
  232. //获取球类代码
  233. $game_code = $getData['game_code'];
  234. //获取数据源
  235. $source = $getData['source'];
  236. //获取球类名称
  237. $gameName = gameModel::getGameName($game_code);
  238. //获取 model
  239. $models = commonFunction::getModels($game_code, 1);
  240. //获取 赛事 数据
  241. $matchData = $getData['data'];
  242. //获取 当前请求 所有 uuid
  243. $identity = [];
  244. $s_match_ids = [];
  245. $match_uuids = [];
  246. foreach($matchData as $k=>$v){
  247. $identity[] = $v['uuid'];
  248. $s_match_ids[] = $v['match_id'];
  249. }
  250. //====验证 赛事 所属 联赛 是否存在====
  251. $identity = array_unique($identity);
  252. sort($identity);
  253. //获取 本地 已存在 联赛
  254. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id','others_lg_id')->get()->toArray();
  255. //二维数组去重
  256. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  257. //如果获取不到本地联赛数据,则返回联赛不存在
  258. if(empty($l_lg_data)) throw new \Exception(Response::generate($gameName.'本次请求的所有赛事都没有联赛数据;',Response::LEAGUE_ERROR));
  259. //获取到本次请求,所有本地有联赛的赛事
  260. $mathData_before = $matchData;//接收原赛事
  261. $matchData = [];//获取新赛事
  262. foreach($l_lg_data as $k=>$v){
  263. foreach($mathData_before as $kk=>$vv){
  264. if($v['identity'] == $vv['uuid']){
  265. $matchData[] = $vv;
  266. }
  267. }
  268. }
  269. //获取当前请求有关联联赛的赛事 match_id
  270. $s_match_ids = [];
  271. foreach($matchData as $k=>$v){
  272. $s_match_ids[] = $v['match_id'];
  273. //追加获取赛事uuid
  274. $match_uuids[] = $v['match_identity'];
  275. }
  276. //====获取 本地 已存在 赛事====
  277. $match_uuids = array_unique($match_uuids);
  278. sort($match_uuids);
  279. $l_match_data = $models['model_local_match']::whereIn('identity',$match_uuids)->where('source',$source)->select('match_id','identity')->get()->toArray();
  280. //二维数组去重
  281. $l_match_data = commonFunction::uniquArrV2($l_match_data,'others_match_id');
  282. //循环对比 请求match_id->本地others_match_id
  283. //如果本地有 赛事,则分别获取本地已存在/不存在赛事
  284. if(!empty($l_match_data)){
  285. //本地已存在赛事,用于更新
  286. $matchData_y = [];
  287. foreach($l_match_data as $k=>$v){
  288. foreach($match_uuids as $kk=> $match_identity){
  289. if($v['identity'] == $match_identity){
  290. $matchData_y[$k]['match_identity'] = $match_identity;
  291. $matchData_y[$k]['match_id'] = $v['match_id'];
  292. unset($match_uuids[$kk]);
  293. }
  294. }
  295. }
  296. sort($match_uuids);
  297. }
  298. //如果 请求中 有本地赛事,则执行更新
  299. if(!empty($matchData_y)){
  300. foreach($matchData as $k=>$v){
  301. foreach($matchData_y as $kk=> $vv){
  302. if($v['match_identity'] == $vv['match_identity']){
  303. $data = $v;
  304. $up_match=[];
  305. $up_match['utime'] = date('Y-m-d H:i:s');
  306. $up_match['status'] = $data['status'];
  307. //获取需更新字段
  308. if($data['is_rollball'] == 1){
  309. $up_match['is_rollball'] = $data['is_rollball'];
  310. }
  311. if($data['is_today'] == 1){
  312. $up_match['is_today'] = $data['is_today'];
  313. }
  314. if($data['is_morningplate'] == 1){
  315. $up_match['is_morningplate'] = $data['is_morningplate'];
  316. }
  317. if($data['is_stringscene'] == 1){
  318. $up_match['is_stringscene'] = $data['is_stringscene'];
  319. }
  320. //追加更新开赛日期/时间
  321. $up_match['match_date'] = $data['match_date'];
  322. $up_match['match_time'] = $data['match_time'];
  323. $up_match['us_time'] = $data['us_time'];
  324. //如果是网球 追加rule 字段
  325. if($game_code == 'wq' and !empty($data['rule'])){
  326. $up_match['rule'] = $data['rule'];
  327. }
  328. //更新赛事 返回成功数量
  329. $set = $models['model_match']::where(['id'=>$vv['match_id']])
  330. ->update($up_match);
  331. if($set < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::UPSTATUS_ERROR)) ;
  332. }
  333. }
  334. }
  335. }
  336. //剩余 赛事 数据 写入
  337. if(!empty($match_uuids)){
  338. foreach($matchData as $k=>$v){
  339. foreach($match_uuids as $kk=> $match_identity){
  340. if($v['match_identity'] == $match_identity){
  341. $data = $v;
  342. $half_match_id = $v['half_match_id'];
  343. //获取 本地 联赛 ID
  344. //验证本赛事是否有数据源lg_id
  345. if(empty($data['lg_id'])) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::S_LG_ID_NULL));
  346. $lg_id = commonFunction::searcharray($data['uuid'],'identity',$l_lg_data,'lg_id');
  347. //验证 本赛事是否获取到本地lg_id
  348. if(empty($lg_id)) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::L_LG_ID_NULL));
  349. //如果赛事没有开始日期,则为冠军盘口赛事
  350. if(empty($data['match_date'])){
  351. //冠军盘口赛事获取所属联赛结束时间
  352. $last_time = $models['model_league']::where(['id'=>$lg_id])
  353. ->value('last_time');
  354. 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'));
  355. //给冠军盘口赛事 赋值时间
  356. $time = strtotime($last_time);
  357. $data['match_date'] = date('Y-m-d',$time);
  358. $data['match_time'] = date('H:i:s',$time);
  359. }
  360. $set_match = [
  361. 'ctime'=>date('Y-m-d H:i:s'),
  362. 'utime'=>date('Y-m-d H:i:s'),
  363. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  364. 'home_team'=>$data['home_team']?:'',
  365. 'guest_team'=>$data['guest_team']?:'no_team',
  366. 'lg_id'=>$lg_id,
  367. 'status'=>$data['status'],
  368. 'match_date'=>$data['match_date']?:date('Y-m-d'),
  369. 'match_time'=>$data['match_time']?:date('H:i:s'),
  370. 'is_rollball'=>$data['is_rollball']?:0,
  371. 'is_today'=>$data['is_today']?:0,
  372. 'is_morningplate'=>$data['is_morningplate']?:0,
  373. 'is_stringscene'=>$data['is_stringscene']?:0,
  374. 'us_time'=>$data['us_time']?:commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  375. 'half_match_id'=>$half_match_id?:0,
  376. 'identity' => $data['uuid'],
  377. 'match_identity' => $data['match_identity'],
  378. ];
  379. //如果是网球 追加rule 字段
  380. if($game_code == 'wq'){
  381. $set_match['rule'] = $data['rule'];
  382. }
  383. //写入赛事 返回id
  384. $id = $models['model_match']::insertGetId($set_match);
  385. if($id < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::INSERT_ERROR)) ;
  386. //写关联记录
  387. $set_local = [
  388. 'match_id'=>$id,
  389. 'others_match_id'=>$data['match_id'],
  390. 'source'=>$source,
  391. 'identity' => $data['match_identity'],
  392. 'ctime'=>date('Y-m-d H:i:s')
  393. ];
  394. $ret = $models['model_local_match']::insertGetId($set_local);
  395. if($ret < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::LOCAL_MATCH_ERROR)) ;
  396. }
  397. }
  398. }
  399. }
  400. //提交事务
  401. DB::commit();
  402. //写请求数据 日志记录
  403. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],Response::success());
  404. return Response::success();
  405. } catch (\Exception $e) {
  406. //回滚事务
  407. DB::rollBack();
  408. //写请求数据 日志记录
  409. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],$e->getMessage());
  410. return $e->getMessage();
  411. }
  412. }
  413. public function __setMatch(Req $data){
  414. //验证token
  415. $user = new \App\Models\System_user;
  416. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  417. $token = $user::where(['token'=>$data->token])->value('token');
  418. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  419. try {
  420. //开启事务
  421. DB::beginTransaction();
  422. $obt = $data->data;
  423. //请求 数据 为空
  424. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  425. $getData = $this->getAddData($obt);
  426. //不是 赛事 数据
  427. if($getData['title'] != 'match') throw new \Exception(Response::generate('不是赛事数据,',Response::ABNORMAL));
  428. /*
  429. //写请求数据 日志记录
  430. if($this->isRecord){
  431. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  432. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  433. }
  434. */
  435. //获取球类代码
  436. $game_code = $getData['game_code'];
  437. //获取数据源
  438. $source = $getData['source'];
  439. //获取球类名称
  440. $gameName = gameModel::getGameName($game_code);
  441. //获取 model
  442. $models = commonFunction::getModels($game_code, 1);
  443. //获取 赛事 数据
  444. $matchData = $getData['data'];
  445. //获取 当前请求 所有 uuid
  446. $identity = [];
  447. $s_match_ids = [];
  448. foreach($matchData as $k=>$v){
  449. $identity[] = $v['uuid'];
  450. $s_match_ids[] = $v['match_id'];
  451. }
  452. //====验证 赛事 所属 联赛 是否存在====
  453. $identity = array_unique($identity);
  454. sort($identity);
  455. //获取 本地 已存在 联赛
  456. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id','others_lg_id')->get()->toArray();
  457. //二维数组去重
  458. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  459. //如果获取不到本地联赛数据,则返回联赛不存在
  460. if(empty($l_lg_data)) throw new \Exception(Response::generate($gameName.'本次请求的所有赛事都没有联赛数据;',Response::LEAGUE_ERROR));
  461. //获取到本次请求,所有本地有联赛的赛事
  462. $mathData_before = $matchData;//接收原赛事
  463. $matchData = [];//获取新赛事
  464. foreach($l_lg_data as $k=>$v){
  465. foreach($mathData_before as $kk=>$vv){
  466. if($v['identity'] == $vv['uuid']){
  467. $matchData[] = $vv;
  468. }
  469. }
  470. }
  471. //获取当前请求有关联联赛的赛事 match_id
  472. $s_match_ids = [];
  473. foreach($matchData as $k=>$v){
  474. $s_match_ids[] = $v['match_id'];
  475. }
  476. //====获取 本地 已存在 赛事====
  477. $s_match_ids = array_unique($s_match_ids);
  478. sort($s_match_ids);
  479. $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();
  480. //二维数组去重
  481. $l_match_data = commonFunction::uniquArrV2($l_match_data,'others_match_id');
  482. //循环对比 请求match_id->本地others_match_id
  483. //如果本地有 赛事,则分别获取本地已存在/不存在赛事
  484. if(!empty($l_match_data)){
  485. //本地已存在赛事,用于更新
  486. $matchData_y = [];
  487. foreach($l_match_data as $k=>$v){
  488. foreach($s_match_ids as $kk=> $s_match_id){
  489. if($v['others_match_id'] == $s_match_id){
  490. $matchData_y[$k]['others_match_id'] = $s_match_id;
  491. $matchData_y[$k]['match_id'] = $v['match_id'];
  492. unset($s_match_ids[$kk]);
  493. }
  494. }
  495. }
  496. sort($s_match_ids);
  497. }
  498. //如果 请求中 有本地赛事,则执行更新
  499. if(!empty($matchData_y)){
  500. foreach($matchData as $k=>$v){
  501. foreach($matchData_y as $kk=> $vv){
  502. if($v['match_id'] == $vv['others_match_id']){
  503. $data = $v;
  504. $up_match=[];
  505. $up_match['utime'] = date('Y-m-d H:i:s');
  506. $up_match['status'] = $data['status'];
  507. //获取需更新字段
  508. if($data['is_rollball'] == 1){
  509. $up_match['is_rollball'] = $data['is_rollball'];
  510. }
  511. if($data['is_today'] == 1){
  512. $up_match['is_today'] = $data['is_today'];
  513. }
  514. if($data['is_morningplate'] == 1){
  515. $up_match['is_morningplate'] = $data['is_morningplate'];
  516. }
  517. if($data['is_stringscene'] == 1){
  518. $up_match['is_stringscene'] = $data['is_stringscene'];
  519. }
  520. //追加更新开赛日期/时间
  521. $up_match['match_date'] = $data['match_date'];
  522. $up_match['match_time'] = $data['match_time'];
  523. $up_match['us_time'] = $data['us_time'];
  524. //如果是网球 追加rule 字段
  525. if($game_code == 'wq' and !empty($data['rule'])){
  526. $up_match['rule'] = $data['rule'];
  527. }
  528. //更新赛事 返回成功数量
  529. $set = $models['model_match']::where(['id'=>$vv['match_id']])
  530. ->update($up_match);
  531. if($set < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::UPSTATUS_ERROR)) ;
  532. }
  533. }
  534. }
  535. }
  536. //剩余 赛事 数据 写入
  537. if(!empty($s_match_ids)){
  538. foreach($matchData as $k=>$v){
  539. foreach($s_match_ids as $kk=> $s_match_id){
  540. if($v['match_id'] == $s_match_id){
  541. $data = $v;
  542. $half_match_id = $v['half_match_id'];
  543. //获取 本地 联赛 ID
  544. //验证本赛事是否有数据源lg_id
  545. if(empty($data['lg_id'])) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::S_LG_ID_NULL));
  546. $lg_id = commonFunction::searcharray($data['uuid'],'identity',$l_lg_data,'lg_id');
  547. //验证 本赛事是否获取到本地lg_id
  548. if(empty($lg_id)) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::L_LG_ID_NULL));
  549. //如果赛事没有开始日期,则为冠军盘口赛事
  550. if(empty($data['match_date'])){
  551. //冠军盘口赛事获取所属联赛结束时间
  552. $last_time = $models['model_league']::where(['id'=>$lg_id])
  553. ->value('last_time');
  554. 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'));
  555. //给冠军盘口赛事 赋值时间
  556. $time = strtotime($last_time);
  557. $data['match_date'] = date('Y-m-d',$time);
  558. $data['match_time'] = date('H:i:s',$time);
  559. }
  560. $set_match = [
  561. 'ctime'=>date('Y-m-d H:i:s'),
  562. 'utime'=>date('Y-m-d H:i:s'),
  563. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  564. 'home_team'=>$data['home_team']?:'',
  565. 'guest_team'=>$data['guest_team']?:'no_team',
  566. 'lg_id'=>$lg_id,
  567. 'status'=>$data['status'],
  568. 'match_date'=>$data['match_date']?:date('Y-m-d'),
  569. 'match_time'=>$data['match_time']?:date('H:i:s'),
  570. 'is_rollball'=>$data['is_rollball']?:0,
  571. 'is_today'=>$data['is_today']?:0,
  572. 'is_morningplate'=>$data['is_morningplate']?:0,
  573. 'is_stringscene'=>$data['is_stringscene']?:0,
  574. 'us_time'=>$data['us_time']?:commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  575. 'half_match_id'=>$half_match_id?:0,
  576. 'identity' => $data['uuid'],
  577. ];
  578. //如果是网球 追加rule 字段
  579. if($game_code == 'wq'){
  580. $set_match['rule'] = $data['rule'];
  581. }
  582. //写入赛事 返回id
  583. $id = $models['model_match']::insertGetId($set_match);
  584. if($id < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::INSERT_ERROR)) ;
  585. //写关联记录
  586. $set_local = [
  587. 'match_id'=>$id,
  588. 'others_match_id'=>$data['match_id'],
  589. 'source'=>$source,
  590. 'ctime'=>date('Y-m-d H:i:s')
  591. ];
  592. $ret = $models['model_local_match']::insertGetId($set_local);
  593. 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'));
  594. }
  595. }
  596. }
  597. }
  598. //提交事务
  599. DB::commit();
  600. //写请求数据 日志记录
  601. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],Response::success());
  602. return Response::success();
  603. } catch (\Exception $e) {
  604. //回滚事务
  605. DB::rollBack();
  606. //写请求数据 日志记录
  607. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],$e->getMessage());
  608. return $e->getMessage();
  609. }
  610. }
  611. /*
  612. * 写 赛事 结果 记录 数据
  613. * 每个请求 包含 N个联赛 下 N个赛事 结果数据
  614. */
  615. public function setMatchResult(Req $data){
  616. //验证token
  617. $user = new \App\Models\System_user;
  618. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  619. $token = $user::where(['token'=>$data->token])->value('token');
  620. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  621. try {
  622. //开启事务
  623. DB::beginTransaction();
  624. $obt = $data->data;
  625. //请求 数据 为空
  626. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  627. $getData = $this->getAddData($obt);
  628. //不是 赛事 结果 记录 数据
  629. if($getData['title'] != 'match_result_r') throw new \Exception(Response::generate('不是赛事结果记录数据,',Response::ABNORMAL));
  630. /*
  631. //写请求数据 日志记录
  632. if($this->isRecord){
  633. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  634. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  635. }
  636. */
  637. //获取球类代码
  638. $game_code = $getData['game_code'];
  639. //获取数据源
  640. $source = $getData['source'];
  641. //获取球类名称
  642. $gameName = gameModel::getGameName($game_code);
  643. //获取 model
  644. $models = commonFunction::getModels($game_code, 1);
  645. //获取 赛事 数据
  646. $match_r_data = $getData['data'];
  647. //获取 当前请求 所有 uuid /match_id
  648. $identity = [];
  649. $s_match_ids = [];
  650. $match_uuids = [];
  651. foreach($match_r_data as $k=>$v){
  652. $identity[] = $v['uuid'];
  653. $s_match_ids[] = $v['match_id'];
  654. //追加获取赛事uuid
  655. $match_uuids[] = $v['match_identity'];
  656. }
  657. //====验证 赛事 所属 联赛 是否存在====
  658. $identity = array_unique($identity);
  659. sort($identity);
  660. //获取 本地 已存在 联赛
  661. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id')->get()->toArray();
  662. //二维数组去重
  663. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  664. //循环对比 请求uuid->本地identity
  665. foreach($l_lg_data as $k=>$v){
  666. foreach($identity as $kk=> $uuid){
  667. if($v['identity'] == $uuid){
  668. unset($identity[$kk]);
  669. }
  670. }
  671. }
  672. sort($identity);
  673. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常
  674. if(!empty($identity)) throw new \Exception(Response::generate($gameName.'联赛:uuid-'.$identity[0].';',Response::LEAGUE_ERROR));
  675. //====end====
  676. //====验证 赛事结果记录 所属赛事 是否存在====
  677. $match_uuids = array_unique($match_uuids);
  678. sort($match_uuids);
  679. //获取 本地 已存在 赛事
  680. $l_match_data = $models['model_local_match']::whereIn('identity',$match_uuids)->where('source',$source)->select('identity','match_id')->get()->toArray();
  681. //二维数组去重
  682. $l_match_data = commonFunction::uniquArrV2($l_match_data,'identity');
  683. //循环对比 请求identity->本地identity
  684. foreach($l_match_data as $k=>$v){
  685. foreach($match_uuids as $kk=> $match_identity){
  686. if($v['identity'] == $match_identity){
  687. unset($match_uuids[$kk]);
  688. }
  689. }
  690. }
  691. sort($s_match_ids);
  692. //去除本地和请求里都存在的赛事,如果还有剩余赛事id,则返回异常
  693. if(!empty($s_match_ids)) throw new \Exception(Response::generate($gameName.'赛事-match_id'.$s_match_ids[0].';',Response::MATCH_ERROR));
  694. //====end====
  695. //处理 赛事 结果记录 数据
  696. foreach($match_r_data as $k=>$v){
  697. $data = $v;
  698. //获取 本地 联赛 ID
  699. $lg_id = commonFunction::searcharray($data['uuid'],'identity',$l_lg_data,'lg_id');
  700. //获取 本地 赛事 ID
  701. $match_id = commonFunction::searcharray($data['match_id'],'identity',$l_match_data,'match_id');
  702. //根据球类 获取 赛事结果记录字段
  703. $set_match_r = $this->get_match_r($game_code,$lg_id,$match_id,$data);
  704. //写 赛事 结果 记录
  705. $ret = $models['model_result_record']::insert($set_match_r);
  706. if($ret != true) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_R_ERROR));
  707. }
  708. //提交事务
  709. DB::commit();
  710. //写请求数据 日志记录
  711. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],Response::success());
  712. return Response::success();
  713. } catch (\Exception $e) {
  714. //回滚事务
  715. DB::rollBack();
  716. //写请求数据 日志记录
  717. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],$e->getMessage());
  718. return $e->getMessage();
  719. }
  720. }
  721. public function __setMatchResult(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'] != 'match_result_r') throw new \Exception(Response::generate('不是赛事结果记录数据,',Response::ABNORMAL));
  736. /*
  737. //写请求数据 日志记录
  738. if($this->isRecord){
  739. // $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  740. // if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  741. }
  742. */
  743. //获取球类代码
  744. $game_code = $getData['game_code'];
  745. //获取数据源
  746. $source = $getData['source'];
  747. //获取球类名称
  748. $gameName = gameModel::getGameName($game_code);
  749. //获取 model
  750. $models = commonFunction::getModels($game_code, 1);
  751. //获取 赛事 数据
  752. $match_r_data = $getData['data'];
  753. //获取 当前请求 所有 uuid /match_id
  754. $identity = [];
  755. $s_match_ids = [];
  756. $match_uuids = [];
  757. foreach($match_r_data as $k=>$v){
  758. $identity[] = $v['uuid'];
  759. $s_match_ids[] = $v['match_id'];
  760. }
  761. //====验证 赛事 所属 联赛 是否存在====
  762. $identity = array_unique($identity);
  763. sort($identity);
  764. //获取 本地 已存在 联赛
  765. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id')->get()->toArray();
  766. //二维数组去重
  767. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  768. //循环对比 请求uuid->本地identity
  769. foreach($l_lg_data as $k=>$v){
  770. foreach($identity as $kk=> $uuid){
  771. if($v['identity'] == $uuid){
  772. unset($identity[$kk]);
  773. }
  774. }
  775. }
  776. sort($identity);
  777. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常
  778. if(!empty($identity)) throw new \Exception(Response::generate($gameName.'联赛:uuid-'.$identity[0].';',Response::LEAGUE_ERROR));
  779. //====end====
  780. //====验证 赛事结果记录 所属赛事 是否存在====$match_uuids
  781. $s_match_ids = array_unique($s_match_ids);
  782. sort($s_match_ids);
  783. //获取 本地 已存在 赛事
  784. $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();
  785. //二维数组去重
  786. $l_match_data = commonFunction::uniquArrV2($l_match_data,'others_match_id');
  787. //循环对比 请求match_id->本地others_match_id
  788. foreach($l_match_data as $k=>$v){
  789. foreach($s_match_ids as $kk=> $s_match_id){
  790. if($v['others_match_id'] == $s_match_id){
  791. unset($s_match_ids[$kk]);
  792. }
  793. }
  794. }
  795. sort($s_match_ids);
  796. //去除本地和请求里都存在的赛事,如果还有剩余赛事id,则返回异常
  797. if(!empty($s_match_ids)) throw new \Exception(Response::generate($gameName.'赛事-match_id'.$s_match_ids[0].';',Response::MATCH_ERROR));
  798. //====end====
  799. //处理 赛事 结果记录 数据
  800. foreach($match_r_data as $k=>$v){
  801. $data = $v;
  802. //获取 本地 联赛 ID
  803. $lg_id = commonFunction::searcharray($data['uuid'],'identity',$l_lg_data,'lg_id');
  804. //获取 本地 赛事 ID
  805. $match_id = commonFunction::searcharray($data['match_id'],'others_match_id',$l_match_data,'match_id');
  806. //根据球类 获取 赛事结果记录字段
  807. $set_match_r = $this->get_match_r($game_code,$lg_id,$match_id,$data);
  808. //写 赛事 结果 记录
  809. $ret = $models['model_result_record']::insert($set_match_r);
  810. if($ret != true) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_R_ERROR));
  811. }
  812. //提交事务
  813. DB::commit();
  814. //写请求数据 日志记录
  815. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],Response::success());
  816. return Response::success();
  817. } catch (\Exception $e) {
  818. //回滚事务
  819. DB::rollBack();
  820. //写请求数据 日志记录
  821. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],$e->getMessage());
  822. return $e->getMessage();
  823. }
  824. }
  825. /*
  826. * 写 普通 赔率 数据
  827. * 每个请求 只包含 一场赛事下 N条赔率
  828. */
  829. public function setOdds(Req $data){
  830. //验证token
  831. $user = new \App\Models\System_user;
  832. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  833. $token = $user::where(['token'=>$data->token])->value('token');
  834. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  835. try {
  836. //开启事务
  837. DB::beginTransaction();
  838. $obt = $data->data;
  839. //请求 数据 为空
  840. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  841. $getData = $this->getAddData($obt);
  842. //不是 赔率 数据
  843. if($getData['title'] != 'odds') throw new \Exception(Response::generate('不是赔率数据,',Response::ABNORMAL));
  844. /*
  845. //写请求数据 日志记录
  846. if($this->isRecord){
  847. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  848. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  849. }
  850. */
  851. //获取球类代码
  852. $game_code = $getData['game_code'];
  853. //获取数据源
  854. $source = $getData['source'];
  855. //获取源数据联赛ID
  856. $uuid = $getData['uuid'];
  857. //获取源数据赛事ID
  858. $s_match_id = $getData['match_id'];
  859. //获取数据源赛事uuid
  860. $match_identity = $getData['match_identity'];
  861. //获取赔率所属赛事 tag 值 玩法数量
  862. $tag = $getData['tag']?:99;
  863. //获取赔率是否是串场
  864. $is_stringscene = $getData['is_stringscene'];
  865. //获取球类名称
  866. $gameName = gameModel::getGameName($game_code);
  867. //验证本次请求所属联赛/赛事是否存在 返回本地联赛/赛事ID
  868. $models = commonFunction::getModels($game_code, 1);
  869. $lg_id = $this->leagueVerify($models,$uuid,$source,$gameName);
  870. $match_id = $this->matchVerify($models,$match_identity,$source,$gameName);
  871. //更新赛事 tag 值
  872. //如果未获取到本地赛事id,则返回异常
  873. if(empty($match_id)) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::L_MATCH_ID_ERR)) ;
  874. $upMatch = $models['model_match']::where(['id'=>$match_id])
  875. ->update(['tag'=>$tag,'utime'=>date('Y-m-d H:i:s')]);
  876. if($upMatch < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::UPMATCHTAG_ERROR)) ;
  877. //获取 赔率数据
  878. $oddsData = $getData['data'];
  879. //获取 当前 所有 可用 赔率 sole
  880. $odds_only = $getData['odds_only'];
  881. //更新赛事下所有 不在本次请求的赔率 状态
  882. $ret= OddsModel::upOddsStatus($models,$s_match_id,'',$source,$odds_only,$is_stringscene);
  883. if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  884. //获取当前赛事 在本地的所有赔率
  885. $l_odds_data = $models['model_odds']::select('odds_only')->where(['match_id'=>$match_id])->get()->toArray();
  886. $set_odds_r = [];
  887. $set_odds = [];
  888. if(!empty($oddsData)){
  889. //去除本地已有的赔率
  890. if(!empty($l_odds_data)){
  891. foreach($oddsData as $k=>$v){
  892. foreach($l_odds_data as $kk=>$vv){
  893. if($v['odds_only'] == $vv['odds_only']){
  894. unset($oddsData[$k]);
  895. }
  896. }
  897. }
  898. sort($oddsData);
  899. }
  900. //处理待写入 赔率记录数据
  901. foreach($oddsData as $k=>$v){
  902. //组装数据
  903. $set_odds_r[] = [
  904. 'match_id'=> $match_id?:0,
  905. 'others_match_id'=> $v['match_id']?:0,
  906. 'odds_code'=> $v['odds_code']?:'',
  907. 'status'=> $v['status']?:0,
  908. 'sort'=> $v['sort']?:0,
  909. 'p_code'=> $v['p_code']?:'',
  910. 'odds'=> $v['odds']?:0,
  911. 'condition'=> $v['condition'],
  912. 'odds_only'=> $v['odds_only']?:'',
  913. 'source'=> $v['source']?:'',
  914. 'type'=> $v['type']?:0,
  915. 'team'=> $v['team']?:'',
  916. 'lg_id'=> $lg_id,
  917. 'is_stringscene'=>$is_stringscene,
  918. 'others_lg_id'=> $v['lg_id'],
  919. 'ctime'=> date('Y-m-d H:i:s'),
  920. 'utime'=> date('Y-m-d H:i:s'),
  921. ];
  922. }
  923. //处理待写入 赔率数据
  924. foreach($oddsData as $k=>$v){
  925. //组装数据
  926. $set_odds[] = [
  927. 'match_id'=> $match_id?:0,
  928. 'others_match_id'=> $v['match_id']?:0,
  929. 'odds_code'=> $v['odds_code']?:'',
  930. 'status'=> $v['status']?:0,
  931. 'sort'=> $v['sort']?:0,
  932. 'p_code'=> $v['p_code']?:'',
  933. 'odds'=> $v['odds']?:0,
  934. 'condition'=> $v['condition'],
  935. 'odds_only'=> $v['odds_only']?:'',
  936. 'source'=> $v['source']?:'',
  937. 'type'=> $v['type']?:0,
  938. 'team'=> $v['team']?:'',
  939. 'lg_id'=> $lg_id,
  940. 'is_stringscene'=>$is_stringscene,
  941. 'others_lg_id'=> $v['lg_id'],
  942. 'ctime'=> date('Y-m-d H:i:s'),
  943. 'utime'=> date('Y-m-d H:i:s'),
  944. 'sole' => $v['sole'],
  945. 'expire_time' => date('Y-m-d H:i:s',time()+60)
  946. ];
  947. }
  948. //写赔率记录数据
  949. $ret = $models['model_odds_record']::insert($set_odds_r);
  950. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率记录;',Response::ADD_ODDS_R_ERROR));
  951. //写赔率 数据
  952. $ret = $models['model_odds']::insert($set_odds);
  953. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率;',Response::ADD_ODDS_ERROR));
  954. }
  955. //提交事务
  956. DB::commit();
  957. //写请求数据 日志记录
  958. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],Response::success());
  959. return Response::success();
  960. } catch (\Exception $e) {
  961. //回滚事务
  962. DB::rollBack();
  963. //写请求数据 日志记录
  964. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],$e->getMessage());
  965. return $e->getMessage();
  966. }
  967. }
  968. public function __setOdds(Req $data){
  969. //验证token
  970. $user = new \App\Models\System_user;
  971. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  972. $token = $user::where(['token'=>$data->token])->value('token');
  973. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  974. try {
  975. //开启事务
  976. DB::beginTransaction();
  977. $obt = $data->data;
  978. //请求 数据 为空
  979. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  980. $getData = $this->getAddData($obt);
  981. //不是 赔率 数据
  982. if($getData['title'] != 'odds') throw new \Exception(Response::generate('不是赔率数据,',Response::ABNORMAL));
  983. /*
  984. //写请求数据 日志记录
  985. if($this->isRecord){
  986. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  987. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  988. }
  989. */
  990. //获取球类代码
  991. $game_code = $getData['game_code'];
  992. //获取数据源
  993. $source = $getData['source'];
  994. //获取源数据联赛ID
  995. $uuid = $getData['uuid'];
  996. //获取源数据赛事ID
  997. $s_match_id = $getData['match_id'];
  998. //获取赔率所属赛事 tag 值 玩法数量
  999. $tag = $getData['tag']?:99;
  1000. //获取赔率是否是串场
  1001. $is_stringscene = $getData['is_stringscene'];
  1002. //获取球类名称
  1003. $gameName = gameModel::getGameName($game_code);
  1004. //验证本次请求所属联赛/赛事是否存在 返回本地联赛/赛事ID
  1005. $models = commonFunction::getModels($game_code, 1);
  1006. $lg_id = $this->leagueVerify($models,$uuid,$source,$gameName);
  1007. $match_id = $this->matchVerify($models,$s_match_id,$source,$gameName);
  1008. //更新赛事 tag 值
  1009. //如果未获取到本地赛事id,则返回异常
  1010. if(empty($match_id)) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::L_MATCH_ID_ERR)) ;
  1011. $upMatch = $models['model_match']::where(['id'=>$match_id])
  1012. ->update(['tag'=>$tag,'utime'=>date('Y-m-d H:i:s')]);
  1013. if($upMatch < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::UPMATCHTAG_ERROR)) ;
  1014. //获取 赔率数据
  1015. $oddsData = $getData['data'];
  1016. //获取 当前 所有 可用 赔率 sole
  1017. $odds_only = $getData['odds_only'];
  1018. //更新赛事下所有 不在本次请求的赔率 状态
  1019. $ret= OddsModel::upOddsStatus($models,$s_match_id,'',$source,$odds_only,$is_stringscene);
  1020. if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  1021. //获取当前赛事 在本地的所有赔率
  1022. $l_odds_data = $models['model_odds']::select('odds_only')->where(['match_id'=>$match_id])->get()->toArray();
  1023. $set_odds_r = [];
  1024. $set_odds = [];
  1025. if(!empty($oddsData)){
  1026. //去除本地已有的赔率
  1027. if(!empty($l_odds_data)){
  1028. foreach($oddsData as $k=>$v){
  1029. foreach($l_odds_data as $kk=>$vv){
  1030. if($v['odds_only'] == $vv['odds_only']){
  1031. unset($oddsData[$k]);
  1032. }
  1033. }
  1034. }
  1035. sort($oddsData);
  1036. }
  1037. //处理待写入 赔率记录数据
  1038. foreach($oddsData as $k=>$v){
  1039. //组装数据
  1040. $set_odds_r[] = [
  1041. 'match_id'=> $match_id?:0,
  1042. 'others_match_id'=> $v['match_id']?:0,
  1043. 'odds_code'=> $v['odds_code']?:'',
  1044. 'status'=> $v['status']?:0,
  1045. 'sort'=> $v['sort']?:0,
  1046. 'p_code'=> $v['p_code']?:'',
  1047. 'odds'=> $v['odds']?:0,
  1048. 'condition'=> $v['condition'],
  1049. 'odds_only'=> $v['odds_only']?:'',
  1050. 'source'=> $v['source']?:'',
  1051. 'type'=> $v['type']?:0,
  1052. 'team'=> $v['team']?:'',
  1053. 'lg_id'=> $lg_id,
  1054. 'is_stringscene'=>$is_stringscene,
  1055. 'others_lg_id'=> $v['lg_id'],
  1056. 'ctime'=> date('Y-m-d H:i:s'),
  1057. 'utime'=> date('Y-m-d H:i:s'),
  1058. ];
  1059. }
  1060. //处理待写入 赔率数据
  1061. foreach($oddsData as $k=>$v){
  1062. //组装数据
  1063. $set_odds[] = [
  1064. 'match_id'=> $match_id?:0,
  1065. 'others_match_id'=> $v['match_id']?:0,
  1066. 'odds_code'=> $v['odds_code']?:'',
  1067. 'status'=> $v['status']?:0,
  1068. 'sort'=> $v['sort']?:0,
  1069. 'p_code'=> $v['p_code']?:'',
  1070. 'odds'=> $v['odds']?:0,
  1071. 'condition'=> $v['condition'],
  1072. 'odds_only'=> $v['odds_only']?:'',
  1073. 'source'=> $v['source']?:'',
  1074. 'type'=> $v['type']?:0,
  1075. 'team'=> $v['team']?:'',
  1076. 'lg_id'=> $lg_id,
  1077. 'is_stringscene'=>$is_stringscene,
  1078. 'others_lg_id'=> $v['lg_id'],
  1079. 'ctime'=> date('Y-m-d H:i:s'),
  1080. 'utime'=> date('Y-m-d H:i:s'),
  1081. 'sole' => $v['sole'],
  1082. 'expire_time' => date('Y-m-d H:i:s',time()+60)
  1083. ];
  1084. }
  1085. //写赔率记录数据
  1086. $ret = $models['model_odds_record']::insert($set_odds_r);
  1087. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率记录;',Response::ADD_ODDS_R_ERROR));
  1088. //写赔率 数据
  1089. $ret = $models['model_odds']::insert($set_odds);
  1090. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率;',Response::ADD_ODDS_ERROR));
  1091. }
  1092. //提交事务
  1093. DB::commit();
  1094. //写请求数据 日志记录
  1095. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],Response::success());
  1096. return Response::success();
  1097. } catch (\Exception $e) {
  1098. //回滚事务
  1099. DB::rollBack();
  1100. //写请求数据 日志记录
  1101. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],$e->getMessage());
  1102. return $e->getMessage();
  1103. }
  1104. }
  1105. /*
  1106. *写 冠军联赛 赔率 数据
  1107. *每个请求 包含 N个联赛 下 N条赔率
  1108. */
  1109. public function setOddsCH(Req $data){
  1110. //验证token
  1111. $user = new \App\Models\System_user;
  1112. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  1113. $token = $user::where(['token'=>$data->token])->value('token');
  1114. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  1115. try {
  1116. //开启事务
  1117. DB::beginTransaction();
  1118. $obt = $data->data;
  1119. //请求 数据 为空
  1120. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  1121. $getData = $this->getAddData($obt);
  1122. //不是 冠军 赔率 数据
  1123. if($getData['title'] != 'odds_ch') throw new \Exception(Response::generate('不是冠军赔率数据,',Response::ABNORMAL));
  1124. /*
  1125. //写请求数据 日志记录
  1126. if($this->isRecord){
  1127. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  1128. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  1129. }
  1130. */
  1131. //获取球类代码
  1132. $game_code = $getData['game_code'];
  1133. //获取数据源
  1134. $source = $getData['source'];
  1135. //获取球类名称
  1136. $gameName = gameModel::getGameName($game_code);
  1137. //获取 model
  1138. $models = commonFunction::getModels($game_code, 1);
  1139. //获取 赔率数据
  1140. $oddsData = $getData['data'];
  1141. //获取 当前 所有 可用 赔率 sole
  1142. $odds_only = $getData['odds_only'];
  1143. //====验证 请求里 所有赔率 所属联赛 是否存在 ====
  1144. //获取 当前请求 所有 联赛 uuid
  1145. $uuids = [];
  1146. $s_lg_ids = [];
  1147. foreach($oddsData as $k=>$v){
  1148. $uuids[] = $v['uuid'];
  1149. $s_lg_ids[] = $v['lg_id'];
  1150. }
  1151. $s_lg_ids = array_unique($s_lg_ids);
  1152. $uuids = array_unique($uuids);
  1153. sort($uuids);
  1154. sort($s_lg_ids);
  1155. //更新联赛下所有 不在本次请求的赔率 状态
  1156. $ret= OddsModel::upOddsStatus($models,'',$s_lg_ids,$source,$odds_only);
  1157. if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  1158. //获取 本地 已存在 联赛
  1159. $l_lg_data = $models['model_local_league']::whereIn('identity',$uuids)->select('identity','lg_id')->get()->toArray();
  1160. //二维数组去重
  1161. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  1162. //循环对比 请求uuid->本地uuid
  1163. foreach($l_lg_data as $k=>$v){
  1164. foreach($uuids as $kk=> $uuid){
  1165. if($v['identity'] == $uuid){
  1166. unset($uuids[$kk]);
  1167. }
  1168. }
  1169. }
  1170. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常;
  1171. if(!empty($uuids)) throw new \Exception(Response::generate($gameName.'冠军联赛:uuid-'.$uuids[0].';',Response::LEAGUE_ERROR));
  1172. //====end====
  1173. //赔率记录数据
  1174. $set_odds_r = [];
  1175. //赔率数据
  1176. $set_odds = [];
  1177. if(!empty($oddsData)){
  1178. foreach($oddsData as $k=>$v){
  1179. //获取 本地 联赛 ID
  1180. $lg_id = commonFunction::searcharray($v['uuid'],'identity',$l_lg_data,'lg_id');
  1181. //组装赔率记录数据
  1182. $set_odds_r[] = [
  1183. 'match_id'=>0,
  1184. 'others_match_id'=> $v['match_id']?:0,
  1185. 'odds_code'=> $v['odds_code']?:'',
  1186. 'status'=> $v['status']?:0,
  1187. 'sort'=> $v['sort']?:0,
  1188. 'p_code'=> $v['p_code']?:'',
  1189. 'odds'=> $v['odds']?:0,
  1190. 'condition'=> $v['condition'],
  1191. 'odds_only'=> $v['odds_only']?:'',
  1192. 'source'=> $v['source']?:'',
  1193. 'type'=> $v['type']?:1,
  1194. 'team'=> $v['team']?:'',
  1195. 'lg_id'=> $lg_id,
  1196. 'others_lg_id'=> $v['lg_id'],
  1197. 'ctime'=> date('Y-m-d H:i:s'),
  1198. 'utime'=> date('Y-m-d H:i:s'),
  1199. ];
  1200. }
  1201. foreach($oddsData as $k=>$v){
  1202. //获取 本地 联赛 ID
  1203. $lg_id = commonFunction::searcharray($v['uuid'],'identity',$l_lg_data,'lg_id');
  1204. //组装赔率数据
  1205. $set_odds[] = [
  1206. 'match_id'=>0,
  1207. 'others_match_id'=> $v['match_id']?:0,
  1208. 'odds_code'=> $v['odds_code']?:'',
  1209. 'status'=> $v['status']?:0,
  1210. 'sort'=> $v['sort']?:0,
  1211. 'p_code'=> $v['p_code']?:'',
  1212. 'odds'=> $v['odds']?:0,
  1213. 'condition'=> $v['condition'],
  1214. 'odds_only'=> $v['odds_only']?:'',
  1215. 'source'=> $v['source']?:'',
  1216. 'type'=> $v['type']?:1,
  1217. 'team'=> $v['team']?:'',
  1218. 'lg_id'=> $lg_id,
  1219. 'others_lg_id'=> $v['lg_id'],
  1220. 'ctime'=> date('Y-m-d H:i:s'),
  1221. 'utime'=> date('Y-m-d H:i:s'),
  1222. 'sole' => $v['sole'],
  1223. 'expire_time' => date('Y-m-d H:i:s',time()+60)
  1224. ];
  1225. }
  1226. //写赔率记录数据
  1227. $ret = $models['model_odds_record']::insert($set_odds_r);
  1228. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率记录;',Response::ADD_ODDS_R_ERROR));
  1229. //写赔率 数据
  1230. $ret = $models['model_odds']::insert($set_odds);
  1231. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率;',Response::ADD_ODDS_ERROR));
  1232. }
  1233. //提交事务
  1234. DB::commit();
  1235. //写请求数据 日志记录
  1236. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],Response::success());
  1237. return Response::success();
  1238. } catch (\Exception $e) {
  1239. //回滚事务
  1240. DB::rollBack();
  1241. //写请求数据 日志记录
  1242. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],$e->getMessage());
  1243. return $e->getMessage();
  1244. }
  1245. }
  1246. /*
  1247. * 写入直播 数据
  1248. */
  1249. public function setBroadCast(Req $data){
  1250. //验证token
  1251. $user = new \App\Models\System_user;
  1252. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  1253. $token = $user::where(['token'=>$data->token])->value('token');
  1254. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  1255. try {
  1256. //开启事务
  1257. DB::beginTransaction();
  1258. $obt = $data->data;
  1259. //请求 数据 为空
  1260. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  1261. $getData = $this->getAddData($obt);
  1262. //不是 直播 数据
  1263. if($getData['title'] != 'broad_cast') throw new \Exception(Response::generate('不是直播数据,',Response::ABNORMAL));
  1264. /*
  1265. //写请求数据 日志记录
  1266. if($this->isRecord){
  1267. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  1268. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  1269. }
  1270. */
  1271. //获取 球类
  1272. $game_code = $getData['game_code'];
  1273. //删除当前球类已存在的直播数据
  1274. $del = broadcastModel::where('game_code', $game_code)->delete();
  1275. if($del != true) throw new \Exception(Response::generate('',Response::DEL_PAST_BROADCAST_ERR));
  1276. //获取 直播 数据
  1277. $BroadCast = $getData['data'];
  1278. $set_broadcast = [];
  1279. foreach($BroadCast as $k=>$data){
  1280. //组装数据
  1281. $set_broadcast[] = [
  1282. "doing" => $data['doing'],
  1283. "game_type" => $data['game_type'],
  1284. "game_code" => $game_code,
  1285. "guest_team" => $data['guest_team'],
  1286. "host_team" => $data['host_team'],
  1287. "league_name" => $data['league_name'],
  1288. "shower" => $data['shower'],
  1289. "showid" => (int)$data['showid'],
  1290. "start_time" => $data['start_time'],
  1291. "ctime" =>date('Y-m-d H:i:s'),
  1292. "utime" =>date('Y-m-d H:i:s'),
  1293. ];
  1294. }
  1295. //写入 直播 数据
  1296. $ret = broadcastModel::insert($set_broadcast);
  1297. if($ret == false) throw new \Exception(Response::generate('',Response::BROADCAST_ERROR));
  1298. //提交事务
  1299. DB::commit();
  1300. //写请求数据 日志记录
  1301. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],Response::success());
  1302. return Response::success();
  1303. } catch (\Exception $e) {
  1304. //回滚事务
  1305. DB::rollBack();
  1306. //写请求数据 日志记录
  1307. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],$e->getMessage());
  1308. return $e->getMessage();
  1309. }
  1310. }
  1311. /*
  1312. * 验证所属 联赛 是否存在
  1313. */
  1314. public function leagueVerify($models=[],$uuid='',$source='',$gameName=''){
  1315. if(empty($models) || empty($uuid)) throw new \Exception(Response::generate('',Response::ABNORMAL));
  1316. $l_lg_id = $models['model_local_league']::where(['identity'=>$uuid])->value('lg_id');
  1317. if($l_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛:lg_id-'.$uuid,Response::LEAGUE_ERROR));
  1318. return $l_lg_id;
  1319. }
  1320. /*
  1321. * 验证所属 赛事 是否存在
  1322. */
  1323. public function matchVerify($models=[],$match_identity='',$source='',$gameName=''){
  1324. if(empty($models) || empty($match_identity)) throw new \Exception(Response::generate('',Response::ABNORMAL));
  1325. $l_match_id = $models['model_local_match']::where(['identity'=>$match_identity,'source'=>$source])->value('match_id');
  1326. if($l_match_id < 1) throw new \Exception(Response::generate($gameName.'match_identity-'.$match_identity,Response::MATCH_ERROR));
  1327. return $l_match_id;
  1328. }
  1329. public function __matchVerify($models=[],$s_match_id='',$source='',$gameName=''){
  1330. if(empty($models) || empty($s_match_id)) throw new \Exception(Response::generate('',Response::ABNORMAL));
  1331. $l_match_id = $models['model_local_match']::where(['others_match_id'=>$s_match_id,'source'=>$source])->value('match_id');
  1332. if($l_match_id < 1) throw new \Exception(Response::generate($gameName.'赛事:match_id-'.$s_match_id,Response::MATCH_ERROR));
  1333. return $l_match_id;
  1334. }
  1335. /*
  1336. * 写入数据 接口
  1337. */
  1338. public function setSports(Req $data){
  1339. try {
  1340. //开启事务
  1341. DB::beginTransaction();
  1342. if($data->game_code){
  1343. //用于后台 将所有进行中的赛事写入结果
  1344. $this->match_result($data->game_code);
  1345. }else{
  1346. //验证token
  1347. $dd = $data->session()->get('adminInfo');
  1348. if(empty($data->token) ||($dd['token'] != $data->token)) return Response::generate('',Response::TOKEN_ERR);
  1349. //写赛事数据
  1350. $obt = $data->data;
  1351. if($obt){
  1352. $getData = $this->getAddData($obt);
  1353. $league = [];
  1354. $competition = [];
  1355. $odds = [];
  1356. $odds_soly = [];
  1357. $league_result = [];
  1358. $match_result = [];
  1359. $match_result_record = [];
  1360. $odds_record = [];
  1361. $broadcast = [];
  1362. //指定排序 联赛->赛事->赔率->联赛结果->赛事结果->赛事结果记录->赔率记录->直播数据
  1363. foreach ($getData as $k=>$v){
  1364. $game_code = $v['game_code'];
  1365. if($v['title'] == 'league') $league[] = $v;
  1366. if($v['title'] == 'competition') $competition[] = $v;
  1367. if($v['title'] == 'odds') $odds[] = $v;
  1368. if($v['title'] == 'odds_sole') $odds_soly[] = $v;
  1369. if($v['title'] == 'league_result') $league_result[] = $v;
  1370. if($v['title'] == 'match_result') $match_result[] = $v;
  1371. if($v['title'] == 'match_result_record') $match_result_record[] = $v;
  1372. if($v['title'] == 'odds_record') $odds_record[] = $v;
  1373. if($v['title'] == 'broadcast') $broadcast[] = $v;
  1374. }
  1375. $matchData = [$league,$competition,$odds_soly,$odds,$league_result,$match_result,$match_result_record,$odds_record,$broadcast];
  1376. //排空处理
  1377. foreach ($matchData as $k=>$v){
  1378. if($v == []) unset($matchData[$k]);
  1379. }
  1380. sort($matchData);
  1381. //根据顺序写入数据
  1382. //降维数据
  1383. $mentData = [];
  1384. foreach ($matchData as $k=>$v){
  1385. foreach ($v as $kk=>$vv){
  1386. $mentData[] = $vv;
  1387. }
  1388. }
  1389. //获取各球类model
  1390. $models = commonFunction::getModels($game_code,1);
  1391. //获取球类名称
  1392. $gameName = gameModel::getGameName($game_code);
  1393. //===获取验证数据===
  1394. //获取一段时间内所有联赛本地记录
  1395. $lg_data = LeagueModel::getLeagueID($models);
  1396. //获取一段时间内所有赛事本地记录
  1397. $match_data = MatchModel::getMatchID($models);
  1398. //获取一段时间内所有赔率
  1399. $odds_data = OddsModel::getOddsID($models);
  1400. //获取一段时间内所有赔率记录
  1401. $odds_record_data = OddsRecordModel::getOddsRecordID($models);
  1402. //===获取验证数据===
  1403. $others_lg_id = '';//继承 源联赛id
  1404. $others_match_id = '';//继承 源赛事id
  1405. $s_lg_id = '';//源 联赛ID
  1406. $l_lg_id = '';//本地 联赛 ID
  1407. $s_match_id = '';//源 赛事ID
  1408. $l_match_id = '';//本地 赛事 ID
  1409. foreach ($mentData as $kk =>$vv){
  1410. switch ($vv['title']){
  1411. case 'area'://地区
  1412. throw new \Exception(Response::generate('地区数据-area:',Response::AUTH_ERROR)) ;
  1413. break;
  1414. case 'country'://国家
  1415. throw new \Exception(Response::generate('国家数据-country:',Response::AUTH_ERROR));
  1416. break;
  1417. case 'league'://联赛
  1418. $others_lg_id = $vv['data']['lg_id'];
  1419. $s_lg_id = $vv['data']['lg_id'];
  1420. $source = $vv['data']['source'];
  1421. //验证联赛记录是否已存在
  1422. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  1423. if($ret_lg != false) {
  1424. $l_lg_id = $ret_lg;
  1425. break;
  1426. }
  1427. //执行 联赛数据
  1428. $lg_data = $this->league($vv,$gameName);
  1429. $l_lg_id = $lg_data['lg_id'];
  1430. break;
  1431. case 'competition'://赛事
  1432. $others_match_id = $vv['data']['match_id'];
  1433. $s_lg_id = $vv['data']['lg_id'];
  1434. $s_match_id = $vv['data']['match_id'];
  1435. $source = $vv['data']['source'];
  1436. //本次请求是否包含 联赛数据
  1437. if(empty($others_lg_id)){
  1438. //验证本地是否存在
  1439. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  1440. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1441. $l_lg_id = $ret_lg;
  1442. }else{
  1443. //本次有 联赛数据 验证属于同联赛
  1444. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1445. }
  1446. //验证赛事记录是否已存在
  1447. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  1448. if($ret_match != false){
  1449. $l_match_id = $ret_match;
  1450. break;
  1451. }
  1452. //执行 赛事数据
  1453. $match_data = $this->competition($vv,$gameName,$l_lg_id);
  1454. $l_match_id = $match_data['match_id'];
  1455. break;
  1456. case 'odds'://赔率
  1457. $s_lg_id = $vv['data']['lg_id'];
  1458. $s_match_id = $vv['data']['match_id'];
  1459. $source = $vv['data']['source'];
  1460. $odds_only = $vv['data']['odds_only'];
  1461. $sole = $vv['data']['sole'];
  1462. $odds_type = $vv['data']['type'];//0普通 1冠军盘口
  1463. //本次请求是否包含 联赛数据
  1464. if(empty($others_lg_id)){
  1465. //无联赛数据 验证本地是否存在
  1466. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  1467. //联赛 不存在
  1468. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1469. $l_lg_id = $ret_lg;
  1470. }else{
  1471. //有联赛数据 验证属于同请求联赛
  1472. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1473. }
  1474. //如果是普通 赔率,则验证所属赛事
  1475. if($odds_type == 0){
  1476. //验证赛事记录是否已存在
  1477. if(empty($others_match_id)){
  1478. //无赛事数据 验证本地是否存在
  1479. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  1480. //赛事 不存在
  1481. if($ret_match == false) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  1482. $l_match_id = $ret_match;
  1483. }else{
  1484. //有赛事数据 验证属于同请求赛事
  1485. if($s_match_id != $others_match_id) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  1486. }
  1487. }
  1488. //验证赔率 是否存在
  1489. $ret_odds = '';
  1490. $ret_odds = commonFunction::ver_odds($sole,$source,$odds_data);
  1491. //验证赔率记录 是否存在
  1492. $ret_odds_record = '';
  1493. $ret_odds_record = commonFunction::ver_odds_record($odds_only,$source,$odds_record_data);
  1494. $this->odds($vv,$gameName,$l_lg_id,$l_match_id,$ret_odds,$ret_odds_record);
  1495. break;
  1496. case 'odds_sole':
  1497. $this->upOddsStatus($vv,$gameName);
  1498. break;
  1499. case 'league_result'://联赛结果
  1500. $this->league_result($vv,$gameName);
  1501. break;
  1502. case 'match_result'://赛事结果
  1503. throw new \Exception(Response::generate('赛事结果数据-match_result:',Response::AUTH_ERROR));
  1504. $this->match_result($vv,$gameName);
  1505. break;
  1506. case 'match_result_record'://赛事结果记录
  1507. $s_lg_id = $vv['data']['lg_id'];
  1508. $s_match_id = $vv['data']['match_id'];
  1509. $source = $vv['data']['source'];
  1510. //如果本次请求没有 联赛数据
  1511. if(empty($others_lg_id)){
  1512. //验证本地是否存在
  1513. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  1514. //联赛 不存在
  1515. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1516. $l_lg_id = $ret_lg;
  1517. }else{
  1518. //有联赛数据 验证属于同请求联赛
  1519. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1520. }
  1521. //验证赛事记录是否已存在
  1522. if(empty($others_match_id)){
  1523. //无赛事数据 验证本地是否存在
  1524. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  1525. //联赛 不存在
  1526. if($ret_match == false) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  1527. $l_match_id = $ret_match;
  1528. }else{
  1529. //有赛事数据 验证属于同请求赛事
  1530. if($s_match_id != $others_match_id) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  1531. }
  1532. //处理 赛事结果 记录
  1533. $this->com_result_record($vv,$gameName,$l_lg_id,$l_match_id);
  1534. break;
  1535. case 'broadcast'://直播数据
  1536. $this->broadcast($vv,$game_code);
  1537. break;
  1538. default:
  1539. throw new \Exception(Response::generate('',Response::ABNORMAL)) ;
  1540. }
  1541. }
  1542. }
  1543. }
  1544. //提交事务
  1545. DB::commit();
  1546. return Response::success();
  1547. } catch (\Exception $e) {
  1548. //回滚事务
  1549. DB::rollBack();
  1550. return $e->getMessage();
  1551. }
  1552. }
  1553. /**
  1554. * @param Req $data
  1555. * @return string
  1556. * @throws \App\Lib\Biz\Sport\Exception
  1557. * 更新赛事状态
  1558. */
  1559. public function upMatch(Req $data){
  1560. //验证token
  1561. $user = new \App\Models\System_user;
  1562. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  1563. $token = $user::where(['token'=>$data->token])->value('token');
  1564. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  1565. try {
  1566. //开启事务
  1567. DB::beginTransaction();
  1568. //获取待更新赛事
  1569. $obt = $data->data;
  1570. //请求 数据 为空
  1571. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  1572. $getData = $this->getAddData($obt);
  1573. //不是 直播 数据
  1574. if($getData['title'] != 'match_status') throw new \Exception(Response::generate('不是更新赛事状态数据',Response::ABNORMAL));
  1575. /*
  1576. //写请求数据 日志记录
  1577. if($this->isRecord){
  1578. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  1579. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  1580. }
  1581. */
  1582. //获取 球类代码
  1583. $game_code = $data['game_code'];
  1584. //获取 数据源
  1585. $source = $data['source'];
  1586. //获取所有数据源赛事 match_id
  1587. $others_match_ids = [];
  1588. foreach ($data['data'] as $k=>$v){
  1589. $others_match_ids[] = $v['match_id'];
  1590. }
  1591. //根据球类代码 获取model
  1592. $model =commonFunction::getModels($game_code,1);
  1593. //获取所有赛事 match_id
  1594. $local_match = $model['model_local_match']::SELECT('others_match_id','match_id')
  1595. ->where(['source'=>$source])
  1596. ->whereIn('others_match_id',$others_match_ids)
  1597. ->get()->toArray();
  1598. if(empty($local_match)) throw new \Exception(Response::generate('',Response::MATCHID_NULL));
  1599. //更新状态字段
  1600. foreach ($local_match as $k=>$v){
  1601. $set_status = [
  1602. 'status'=>$v['status'],
  1603. 'is_rollball'=>$v['is_rollball'],
  1604. 'stais_todaytus'=>$v['is_today'],
  1605. 'is_morningplate'=>$v['is_morningplate'],
  1606. 'is_stringscene'=>$v['is_stringscene'],
  1607. 'is_horn'=>$v['is_horn'],
  1608. ];
  1609. $ret = $model['model_match']::where(['id'=>$v['match_id']])
  1610. -> update($set_status);
  1611. if($ret<1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$v['others_match_id'],Response::UPSTATUS_ERROR));
  1612. }
  1613. //提交事务
  1614. DB::commit();
  1615. //写请求数据 日志记录
  1616. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],Response::success());
  1617. return Response::success();
  1618. } catch (\Exception $e) {
  1619. //回滚事务
  1620. DB::rollBack();
  1621. //写请求数据 日志记录
  1622. if($this->isRecord) commonFunction::SET_SPORTS_RECORD($obt,$getData['title'],$e->getMessage());
  1623. return $e->getMessage();
  1624. }
  1625. }
  1626. /*
  1627. * 写入直播 数据
  1628. */
  1629. public function broadcast($opt = [],$game_code=''){
  1630. $data = $opt['data'];
  1631. $set_broadcast = [
  1632. "doing" => $data['doing'],
  1633. "game_type" => $data['game_type'],
  1634. "game_code" => $game_code,
  1635. "guest_team" => $data['guest_team'],
  1636. "host_team" => $data['host_team'],
  1637. "league_name" => $data['league_name'],
  1638. "shower" => $data['shower'],
  1639. "showid" => (int)$data['showid'],
  1640. "start_time" => $data['start_time'],
  1641. "ctime" =>date('Y-m-d H:i:s'),
  1642. "utime" =>date('Y-m-d H:i:s'),
  1643. ];
  1644. $ret = broadcastModel::insert($set_broadcast);
  1645. if($ret == false) throw new \Exception(Response::generate('',Response::BROADCAST_ERROR));
  1646. }
  1647. /*
  1648. * 写入 赛事 初始 结果
  1649. */
  1650. public function match_result($game_code = ''){
  1651. $model =commonFunction::getModels($game_code,1);
  1652. if($game_code == 'zq') ZqResultModel::ZQresult($model);
  1653. if($game_code == 'lq') LqResultModel::LQresult($model);
  1654. if($game_code == 'wq') WqResultModel::WQresult($model);
  1655. if($game_code == 'bq') BqResultModel::BQresult($model);
  1656. }
  1657. /*
  1658. * 写入联赛数据
  1659. */
  1660. public function league($opt = [],$gameName=''){
  1661. $game_code = $opt['game_code'];
  1662. //根据球类代码获取相关model
  1663. $model =commonFunction::getModels($game_code,1);
  1664. $data = $opt['data'];
  1665. //查询联赛是否已存在
  1666. $id = $model['model_league']::where('name_chinese','=',$data['name_chinese'])
  1667. ->value('id');
  1668. //默认获取本年最后一天
  1669. $last_time = date('Y-12-31 23:59:59');
  1670. //决赛时间
  1671. if($data['last_time']){
  1672. $last_time = $data['last_time'];
  1673. }
  1674. $set_lg['name_chinese'] = $data['name_chinese'];
  1675. $set_lg['kind'] = $data['kind'];
  1676. $set_lg['match_mode'] = $data['match_mode'];
  1677. $set_lg['if_stop'] = $data['if_stop'];
  1678. $set_lg['identity'] = $data['uuid']?:'';
  1679. $set_lg['last_time'] = $last_time;
  1680. $set_lg['utime'] = date('Y-m-d H:i:s');
  1681. if(empty($id)){
  1682. //写入联赛
  1683. $id = $model['model_league']::insertGetId($set_lg);
  1684. $m_lg_id = $id;
  1685. if($m_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::INSERT_ERROR));
  1686. }else{
  1687. //更新联赛
  1688. $ret = $model['model_league']::where(['id'=>$id])
  1689. -> update($set_lg);
  1690. if($ret < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::UPDATE_ERROR));
  1691. }
  1692. $set_local = [
  1693. 'lg_id'=>$id,
  1694. 'others_lg_id'=>$data['lg_id'],
  1695. 'source'=>$data['source'],
  1696. 'ctime'=>date('Y-m-d H:i:s')
  1697. ];
  1698. $ret = $model['model_local_league']::insertGetId($set_local);
  1699. 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'));
  1700. //执行成功返回源数据 联赛ID
  1701. $data = ['others_lg_id'=>$data['lg_id'],'lg_id'=>$id];
  1702. return $data;
  1703. }
  1704. /*
  1705. * 写入赛事数据
  1706. */
  1707. public function competition($opt = [],$gameName='',$lg_id=''){
  1708. $game_code = $opt['game_code'];
  1709. //根据球类代码获取相关model
  1710. $model = commonFunction::getModels($game_code,1);
  1711. $data = $opt['data'];
  1712. if(empty($data['lg_id'])) throw new \Exception(Response::generate('',Response::LEAGUE_ERROR)) ;//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  1713. //查询赛事是否存在
  1714. $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']])
  1715. ->value('id');
  1716. $half_match_id = 0;
  1717. //如果有上半场赛事id 获取上半场赛事是否存在
  1718. if(!empty($data['half_match_id'])){
  1719. $half_match_id = $match_id = $model['model_local_match']::where(['others_match_id'=>$data['half_match_id'],'source'=>$data['source']])
  1720. ->value('match_id');
  1721. 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'));
  1722. }
  1723. //如果赛事没有开始日期,则为冠军盘口赛事
  1724. if(empty($data['match_date'])){
  1725. //冠军盘口赛事获取所属联赛结束时间
  1726. $last_time = $model['model_league']::where(['id'=>$lg_id])
  1727. ->value('last_time');
  1728. 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'));
  1729. //给冠军盘口赛事 赋值时间
  1730. $time = strtotime($last_time);
  1731. $data['match_date'] = date('Y-m-d',$time);
  1732. $data['match_time'] = date('H:i:s',$time);
  1733. }
  1734. $set_match = [
  1735. 'ctime'=>date('Y-m-d H:i:s'),
  1736. 'utime'=>date('Y-m-d H:i:s'),
  1737. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  1738. 'home_team'=>$data['home_team']?:'',
  1739. 'guest_team'=>$data['guest_team']?:'no_team',
  1740. 'lg_id'=>$lg_id,
  1741. 'status'=>$data['status'],
  1742. 'match_date'=>$data['match_date']?:date('Y-m-d'),
  1743. 'match_time'=>$data['match_time']?:date('H:i:s'),
  1744. 'tag'=>$data['tag']?:0,
  1745. 'is_rollball'=>$data['is_rollball']?:0,
  1746. 'is_today'=>$data['is_today']?:0,
  1747. 'is_morningplate'=>$data['is_morningplate']?:0,
  1748. 'is_stringscene'=>$data['is_stringscene']?:0,
  1749. 'us_time'=>$data['us_time']?:commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  1750. 'half_match_id'=>$half_match_id?:0,
  1751. 'identity' => $data['uuid'],
  1752. ];
  1753. //写入赛事
  1754. if(empty($id)){
  1755. //写入赛事 返回id
  1756. $id = $model['model_match']::insertGetId($set_match);
  1757. if($id < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::INSERT_ERROR)) ;
  1758. }else{
  1759. //更新赛事
  1760. $ret = $model['model_match']::where(['id'=>$id])->update($set_match);
  1761. if($ret < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::UPDATE_ERROR)) ;
  1762. }
  1763. //写关联记录
  1764. $set_local = [
  1765. 'match_id'=>$id,
  1766. 'others_match_id'=>$data['match_id'],
  1767. 'source'=>$data['source'],
  1768. 'ctime'=>date('Y-m-d H:i:s')
  1769. ];
  1770. $ret = $model['model_local_match']::insertGetId($set_local);
  1771. 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'));
  1772. //返回 源数据 赛事ID
  1773. $data = ['others_match_id'=>$data['match_id'],'match_id'=>$id];
  1774. return $data;
  1775. }
  1776. /*
  1777. * 写入赔率/赔率记录数据
  1778. */
  1779. public function odds($opt=[],$gameName='',$lg_id=0,$match_id=0,$oddsID='',$oddsRecordID=''){
  1780. $game_code = $opt['game_code'];
  1781. //根据球类代码获取相关model
  1782. $model = commonFunction::getModels($game_code,1);
  1783. $data = $opt['data'];
  1784. //获取赛事 本地/源ID
  1785. $others_match_id = $data['match_id'];
  1786. //获取联赛 本地/源ID
  1787. $others_lg_id = $data['lg_id'];
  1788. //===写赔率记录===
  1789. $set_odds_r = [
  1790. 'match_id'=> $match_id?:0,
  1791. 'others_match_id'=> $others_match_id?:0,
  1792. 'odds_code'=> $data['odds_code']?:'',
  1793. 'status'=> $data['status']?:0,
  1794. 'sort'=> $data['sort']?:0,
  1795. 'p_code'=> $data['p_code']?:'',
  1796. 'odds'=> $data['odds']?:0,
  1797. 'condition'=> $data['condition'],
  1798. 'odds_only'=> $data['odds_only']?:'',
  1799. 'source'=> $data['source']?:'',
  1800. 'type'=> $data['type']?:0,
  1801. 'team'=> $data['team']?:'',
  1802. 'lg_id'=> $lg_id,
  1803. 'others_lg_id'=> $others_lg_id,
  1804. 'ctime'=> date('Y-m-d H:i:s'),
  1805. ];
  1806. //更新或写入赔率记录
  1807. if($oddsRecordID != false){
  1808. $ret = $model['model_odds_record']::where(['id'=>$oddsRecordID])
  1809. -> update($set_odds_r);
  1810. 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'));
  1811. }else{
  1812. $ret = $model['model_odds_record']::insert($set_odds_r);
  1813. 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'));
  1814. }
  1815. //===写赔率===
  1816. $set_odds = [
  1817. 'match_id'=> $match_id?:0,
  1818. 'others_match_id'=> $others_match_id?:0,
  1819. 'odds_code'=> $data['odds_code']?:'',
  1820. 'status'=> $data['status']?:0,
  1821. 'sort'=> $data['sort']?:0,
  1822. 'p_code'=> $data['p_code']?:'',
  1823. 'odds'=> $data['odds']?:0,
  1824. 'condition'=> $data['condition'],
  1825. 'odds_only'=> $data['odds_only']?:'',
  1826. 'sole'=> $data['sole']?:'',
  1827. 'source'=> $data['source']?:'',
  1828. 'type'=> $data['type']?:0,
  1829. 'team'=> $data['team']?:'',
  1830. 'lg_id'=> $lg_id,
  1831. 'others_lg_id'=> $others_lg_id,
  1832. 'ctime'=> date('Y-m-d H:i:s'),
  1833. 'utime'=> date('Y-m-d H:i:s'),
  1834. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  1835. ];
  1836. //更新或写入赔率数据
  1837. if($oddsID != false){
  1838. $ret = $model['model_odds']::where(['id'=>$oddsID])
  1839. -> update($set_odds);
  1840. 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'));
  1841. }else{
  1842. $ret = $model['model_odds']::insert($set_odds);
  1843. 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'));
  1844. }
  1845. //===end===
  1846. }
  1847. /*
  1848. * 更新 赛事下 所有不在本次请求的 赔率状态
  1849. */
  1850. public function upOddsStatus($opt = [],$gameName=''){
  1851. //获取球类代码
  1852. $game_code = $opt['game_code'];
  1853. //获取球类model
  1854. $model = commonFunction::getModels($game_code,1);
  1855. //赛事下 所有 赔率sole
  1856. $odds_sole = $opt['data'];
  1857. //数据源 赛事 id
  1858. $others_match_id = $opt['match_id'];
  1859. //数据源
  1860. $source = $opt['source'];
  1861. //更新赛事下所有 不在本次请求的赔率 状态
  1862. $ret= OddsModel::upOddsStatus($model,$others_match_id,'',$source,$odds_sole);
  1863. if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  1864. }
  1865. /*
  1866. * 写入联赛结果
  1867. */
  1868. public function league_result($opt,$gameName){
  1869. $game_code = $opt['game_code'];
  1870. //根据球类代码获取相关model
  1871. $model = commonFunction::getModels($game_code,1);
  1872. $data = $opt['data'];
  1873. //验证结果所属联赛
  1874. $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  1875. ->value('lg_id');
  1876. if($lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::LEAGUE_ERROR));
  1877. $lg_result_id = $model['model_league_result']::where(['lg_id'=>$lg_id,'game_name'=>$data['game_name']])
  1878. ->value('id');
  1879. $set_lg_result = [
  1880. 'lg_id'=>$lg_id,
  1881. 'game_name'=>$data['game_name'],
  1882. 'result'=>json_encode($data['result'],JSON_UNESCAPED_UNICODE),
  1883. 'status'=>$data['status'],
  1884. 'ctime'=> date('Y-m-d H:i:s'),
  1885. 'utime'=> date('Y-m-d H:i:s'),
  1886. ];
  1887. //联赛结果数据处理
  1888. if(!empty($lg_result_id)){
  1889. $ret = $model['model_league_result']::where(['id'=>$lg_result_id])
  1890. -> update($set_lg_result);
  1891. 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'));
  1892. }else{
  1893. $ret = $model['model_league_result']::insert($set_lg_result);
  1894. 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'));
  1895. }
  1896. }
  1897. /*
  1898. * 写入赛事结果
  1899. */
  1900. public function com_result($opt=[],$gameName='',$lg_id='',$match_id=''){
  1901. $game_code = $opt['game_code'];
  1902. //根据球类代码获取相关model
  1903. $model = commonFunction::getModels($game_code,1);
  1904. $data = $opt['data'];
  1905. //查询结果是否存在
  1906. $match_r_id = $model['model_result']::where(['match_id'=>$match_id])
  1907. ->value('id');
  1908. $set_match_r = [
  1909. "home_team"=>$data['home_team'],
  1910. "guest_team"=>$data['guest_team'],
  1911. "lg_id"=>$lg_id,
  1912. "home_rate"=> $data['home_rate'],
  1913. "guest_rate"=> $data['guest_rate'],
  1914. "home_score"=> $data['home_score'],
  1915. "guest_score"=> $data['guest_score'],
  1916. "all_goal"=> $data['all_goal'],
  1917. "status"=>$data['status'],
  1918. "first_score"=>$data['first_score'],
  1919. "last_score"=> $data['last_score'],
  1920. "match_score"=> $data['match_score'],
  1921. "match_winer"=> $data['match_winer'],
  1922. "match_time"=> $data['match_time'],
  1923. "match_process"=> $data['match_process'],
  1924. "tag"=> $data['tag'],
  1925. "match_id"=> $match_id,
  1926. "u_home_score"=> $data['u_home_score'],
  1927. "u_guest_score"=> $data['u_guest_score'],
  1928. "p_code"=> $data['p_code'],
  1929. "update_time"=>date('Y-m-d H:i:s')
  1930. ];
  1931. //赛事结果数据处理
  1932. if(!empty($match_r_id)){
  1933. $ret = $model['model_result']::where(['id'=>$match_r_id])
  1934. -> update($set_match_r);
  1935. 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'));
  1936. }else{
  1937. $ret = $model['model_result']::insert($set_match_r);
  1938. 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'));
  1939. }
  1940. // return Response::success();
  1941. }
  1942. //写入赛事结果记录
  1943. public function com_result_record($opt=[],$gameName='',$lg_id='',$match_id=''){
  1944. $game_code = $opt['game_code'];
  1945. //根据球类代码获取相关model
  1946. $model = commonFunction::getModels($game_code,1);
  1947. $data = $opt['data'];
  1948. $match_r_id = $model['model_result_record']::where(['match_id'=>$match_id,'match_time'=>$data['match_time']])
  1949. ->value('id');
  1950. //根据球类 获取 赛事结果记录字段
  1951. $set_match_r = $this->get_match_r($game_code,$lg_id,$match_id,$data);
  1952. //赛事结果记录处理
  1953. if($match_r_id > 0){
  1954. $ret = $model['model_result_record']::where(['id'=>$match_r_id])
  1955. ->update($set_match_r);
  1956. 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'));
  1957. }else{
  1958. $ret = $model['model_result_record']::insert($set_match_r);
  1959. 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'));
  1960. }
  1961. }
  1962. /**
  1963. * @param $data
  1964. * @return mixed
  1965. * json转数组
  1966. */
  1967. public function getAddData($data){
  1968. $data = json_decode($data,true);
  1969. return $data;
  1970. }
  1971. /*
  1972. * 根据球类获取 赛事结果记录字段
  1973. */
  1974. public function get_match_r($game_code,$lg_id,$match_id,$data){
  1975. $set_match_r = [];
  1976. if($game_code == 'zq'){
  1977. $set_match_r = [
  1978. "home_team"=>$data['home_team']?:'',
  1979. "guest_team"=>$data['guest_team']?:'',
  1980. "lg_id"=>$lg_id,
  1981. "all_goal"=>$data['all_goal']?:0,
  1982. "home_rate"=> $data['home_rate']?:0,
  1983. "guest_rate"=> $data['guest_rate']?:0,
  1984. "home_score"=> $data['home_score']?:0,
  1985. "guest_score"=> $data['guest_score']?:0,
  1986. "status"=>$data['status']?:0,
  1987. "first_score"=>$data['first_score']?:0,
  1988. "last_score"=> $data['last_score']?:0,
  1989. "match_score"=> $data['match_score']?:0,
  1990. "match_winer"=> $data['match_winer']?:'',
  1991. "match_time"=> $data['match_time']?:0,
  1992. "match_process"=> $data['match_process']?:'',
  1993. // "tag"=> $data['tag']?:0,
  1994. "match_id"=> $match_id,
  1995. "update_time"=>date('Y-m-d H:i:s')
  1996. ];
  1997. };
  1998. if($game_code == 'lq'){
  1999. $set_match_r = [
  2000. "home_team"=>$data['home_team']?:'',
  2001. "guest_team"=>$data['guest_team']?:'',
  2002. "lg_id"=>$lg_id,
  2003. "home_rate"=> $data['home_rate']?:0,
  2004. "guest_rate"=> $data['guest_rate']?:0,
  2005. "home_score"=> $data['home_score']?:0,
  2006. "guest_score"=> $data['guest_score']?:0,
  2007. "status"=>$data['status']?:0,
  2008. "first_score"=>$data['first_score']?:0,
  2009. "last_score"=> $data['last_score']?:0,
  2010. "match_score"=> $data['match_score']?:0,
  2011. "match_winer"=> $data['match_winer']?:'',
  2012. "match_time"=> $data['match_time']?:0,
  2013. "match_process"=> $data['match_process']?:'',
  2014. // "tag"=> $data['tag']?:0,
  2015. "match_id"=> $match_id,
  2016. "update_time"=>date('Y-m-d H:i:s'),
  2017. "result_mark" =>json_encode($data['result_mark'])?:'',
  2018. ];
  2019. }
  2020. if($game_code == 'wq'){
  2021. $set_match_r = [
  2022. "home_player_name"=>$data['home_player_name']?:'',
  2023. "guest_player_name"=>$data['guest_player_name']?:'',
  2024. "lg_id"=>$lg_id,
  2025. "home_player_let_plate"=>$data['home_player_let_plate']?:0,
  2026. "guest_player_let_plate"=>$data['guest_player_let_plate']?:0,
  2027. "home_player_let_inning"=>$data['home_player_let_inning']?:0,
  2028. "guest_player_let_inning"=>$data['guest_player_let_inning']?:0,
  2029. "all_inning"=>$data['all_inning']?:0,
  2030. "home_player_score"=>$data['home_player_score']?:0,
  2031. "guest_player_score"=>$data['guest_player_score']?:0,
  2032. "status"=>$data['status']?:0,
  2033. "first_score_player"=>$data['first_score_player']?:'',
  2034. "last_score_player"=>$data['last_score_player']?:'',
  2035. "first_inning_score"=>$data['first_inning_score']?:0,
  2036. "second_inning_score"=>$data['second_inning_score']?:0,
  2037. "third_inning_score"=>$data['third_inning_score']?:0,
  2038. "match_winer_player"=>$data['match_winer_player']?:'',
  2039. "update_time"=>date('Y-m-d H:i:s'),
  2040. "match_time"=>$data['match_time']?:0,
  2041. "match_process"=>$data['match_process']?:'',
  2042. // "tag"=>$data['tag']?:0,
  2043. "match_id"=>$match_id,
  2044. "result_mark" =>json_encode($data['result_mark'])?:'',
  2045. ];
  2046. }
  2047. if($game_code == 'bq'){
  2048. $set_match_r = [
  2049. "home_team"=>$data['home_team']?:'',
  2050. "guest_team"=>$data['guest_team']?:'',
  2051. "lg_id"=>$lg_id,
  2052. "home_rate"=> $data['home_rate']?:0,
  2053. "guest_rate"=> $data['guest_rate']?:0,
  2054. "home_score"=> $data['home_score']?:0,
  2055. "guest_score"=> $data['guest_score']?:0,
  2056. "status"=>$data['status']?:0,
  2057. "first_score"=>$data['first_score']?:0,
  2058. "last_score"=> $data['last_score']?:0,
  2059. "match_score"=> $data['match_score']?:0,
  2060. "match_winer"=> $data['match_winer']?:'',
  2061. "match_time"=> $data['match_time']?:0,
  2062. "match_process"=> $data['match_process']?:'',
  2063. // "tag"=> $data['tag']?:0,
  2064. "match_id"=> $match_id,
  2065. "all_inning"=>$data['all_inning']?:9,
  2066. "update_time"=>date('Y-m-d H:i:s'),
  2067. "result_mark" =>json_encode($data['result_mark'])?:'',
  2068. ];
  2069. }
  2070. return $set_match_r;
  2071. }
  2072. }