WriteSportsController.php 137 KB

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