WriteSportsController.php 109 KB

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