WriteSportsController.php 139 KB

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