WriteSportsController.php 131 KB

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