WriteSportsController.php 135 KB

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