WriteSportsController.php 135 KB

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