WriteSportsController.php 140 KB

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