WriteSportsController.php 138 KB

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