WriteSportsController.php 128 KB

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