WriteSportsController.php 136 KB

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