WriteSportsController.php 141 KB

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