WriteSportsController.php 137 KB

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