WriteSportsController.php 109 KB

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