WriteSportsController.php 134 KB

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