WriteSportsController.php 132 KB

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