WriteSportsController.php 135 KB

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