WriteSportsController.php 128 KB

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