WriteSportsController.php 128 KB

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