WriteSportsController.php 135 KB

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