WriteSportsController.php 138 KB

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