WriteSportsController.php 129 KB

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