WriteSportsController.php 139 KB

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