WriteSportsController.php 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  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. /**
  27. * 体育数据入库接口
  28. */
  29. class WriteSportsController extends BaseController{
  30. /*
  31. * 用户 用户 token
  32. */
  33. public function getToken(Req $req){
  34. if ($req->isMethod('post')) {
  35. $user = new \App\Models\System_user;
  36. $str = $user->adminLogin($_POST['account'], $_POST['password']);
  37. if($str>0){
  38. $token=refreshToken($str['admin_id']);
  39. $str['token']=$token;
  40. $data = $str;
  41. $req->session()->put('adminInfo',$str);
  42. $str=1;
  43. }
  44. $log = array(
  45. session('adminInfo.admin_name'),
  46. $_POST['account']
  47. );
  48. OperationLog(session('adminInfo.admin_id'), 'login', $log);
  49. return Response::success($data);
  50. }
  51. }
  52. /**
  53. * 根据联赛名称 获取 国家 地区
  54. */
  55. public function getArea($leagueName = ''){
  56. //如果联赛名称有括号,去除
  57. $leagueName = preg_replace('/\(.*?\)/', '', $leagueName);
  58. //获取所有国家/地区
  59. $areaData = StAreaCountryModel::select('id','pid','name')->get()->toArray();
  60. //获取当前联赛所属国家
  61. if(strpos($leagueName,'NBA') !== false){
  62. $data = [
  63. 'id'=>247,
  64. 'pid'=>0,
  65. 'name'=>'世界'
  66. ];
  67. return $data;
  68. }
  69. //识别本地 国家地区数据
  70. foreach($areaData as $v => $k){
  71. if(strpos($leagueName,$k['name']) !== false){
  72. return $k;
  73. }
  74. }
  75. //如果不能识别,则返回世界
  76. $data = [
  77. 'id'=>247,
  78. 'pid'=>0,
  79. 'name'=>'世界'
  80. ];
  81. return $data;
  82. }
  83. /*
  84. * 写 联赛 数据
  85. * 每个请求 包含 N个联赛
  86. */
  87. public function setLeague(Req $data){
  88. //验证token
  89. $user = new \App\Models\System_user;
  90. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  91. $token = $user::where(['token'=>$data->token])->value('token');
  92. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  93. try {
  94. //开启事务
  95. DB::beginTransaction();
  96. $obt = $data->data;
  97. //请求 数据 为空
  98. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  99. $getData = $this->getAddData($obt);
  100. //不是 联赛 数据
  101. if($getData['title'] != 'league') throw new \Exception(Response::generate('不是联赛数据,',Response::ABNORMAL));
  102. /*
  103. //写请求数据 日志记录
  104. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  105. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  106. */
  107. //获取球类代码
  108. $game_code = $getData['game_code'];
  109. //获取数据源
  110. $source = $getData['source'];
  111. //获取球类名称
  112. $gameName = gameModel::getGameName($game_code);
  113. //获取 model
  114. $models = commonFunction::getModels($game_code, 1);
  115. //获取 联赛 数据
  116. $leagueData = $getData['data'];
  117. //获取 当前请求 所有 联赛 uuid
  118. $identity = [];
  119. foreach($leagueData as $k=>$v){
  120. $identity[] = $v['uuid'];
  121. }
  122. $identity = array_unique($identity);
  123. sort($identity);
  124. //获取 本地 已存在 联赛
  125. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id')->get()->toArray();
  126. //二维数组去重
  127. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  128. //循环对比 请求identity->本地identity
  129. foreach($l_lg_data as $k=>$v){
  130. foreach($identity as $kk=> $uuid){
  131. if($v['identity'] == $uuid){
  132. unset($identity[$kk]);
  133. }
  134. }
  135. }
  136. //去除本地和请求里都存在的联赛,如果还有剩余联赛uuid,则将其写入
  137. if(!empty($identity)) {
  138. //没有 决赛时间 默认获取本年最后一天
  139. $last_time = date('Y-12-31 23:59:59');
  140. foreach($leagueData as $k=>$v){
  141. foreach($identity as $kk=> $uuid){
  142. if($v['uuid'] == $uuid){
  143. //如有 决赛时间
  144. if($v['last_time']){
  145. $last_time = $v['last_time'];
  146. }
  147. //获取联赛所属国家或地区
  148. $getArea = $this->getArea($v['name_chinese']);
  149. if($getArea['pid'] == 0){
  150. $area_id = $getArea['id'];
  151. $country_id = 0;
  152. }else{
  153. $area_id = $getArea['pid'];
  154. $country_id = $getArea['id'];
  155. }
  156. if(empty($v['uuid'])) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$v['lg_id'].';',Response::LG__UUID_NULL));
  157. $set_lg['name_chinese'] = $v['name_chinese'];
  158. $set_lg['kind'] = $v['kind'];
  159. $set_lg['match_mode'] = $v['match_mode'];
  160. $set_lg['if_stop'] = $v['if_stop'];
  161. $set_lg['identity'] = $v['uuid'];
  162. $set_lg['last_time'] = $last_time;
  163. $set_lg['utime'] = date('Y-m-d H:i:s');
  164. $set_lg['area_id'] = $area_id;
  165. $set_lg['country_id'] = $country_id;
  166. //写入联赛
  167. $id = $models['model_league']::insertGetId($set_lg);
  168. $m_lg_id = $id;
  169. if($m_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$v['lg_id'].';',Response::INSERT_ERROR));
  170. //写入 本地 联赛记录
  171. $set_local = [
  172. 'lg_id'=>$id,
  173. 'others_lg_id'=>$v['lg_id'],
  174. 'source'=>$source,
  175. 'ctime'=>date('Y-m-d H:i:s'),
  176. 'identity'=>$v['uuid'],
  177. ];
  178. $ret = $models['model_local_league']::insertGetId($set_local);
  179. 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'));
  180. }
  181. }
  182. }
  183. }
  184. //提交事务
  185. DB::commit();
  186. return Response::success();
  187. } catch (\Exception $e) {
  188. //回滚事务
  189. DB::rollBack();
  190. return $e->getMessage();
  191. }
  192. }
  193. /*
  194. * 写 赛事 数据
  195. * 每个请求 包含 N个联赛 下 N个赛事
  196. */
  197. public function setMatch(Req $data){
  198. //验证token
  199. $user = new \App\Models\System_user;
  200. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  201. $token = $user::where(['token'=>$data->token])->value('token');
  202. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  203. try {
  204. //开启事务
  205. DB::beginTransaction();
  206. $obt = $data->data;
  207. //请求 数据 为空
  208. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  209. $getData = $this->getAddData($obt);
  210. //不是 赛事 数据
  211. if($getData['title'] != 'match') throw new \Exception(Response::generate('不是赛事数据,',Response::ABNORMAL));
  212. /*
  213. //写请求数据 日志记录
  214. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  215. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  216. */
  217. //获取球类代码
  218. $game_code = $getData['game_code'];
  219. //获取数据源
  220. $source = $getData['source'];
  221. //获取球类名称
  222. $gameName = gameModel::getGameName($game_code);
  223. //获取 model
  224. $models = commonFunction::getModels($game_code, 1);
  225. //获取 赛事 数据
  226. $matchData = $getData['data'];
  227. //获取 当前请求 所有 uuid
  228. $identity = [];
  229. $s_match_ids = [];
  230. foreach($matchData as $k=>$v){
  231. $identity[] = $v['uuid'];
  232. $s_match_ids[] = $v['match_id'];
  233. }
  234. //====验证 赛事 所属 联赛 是否存在====
  235. $identity = array_unique($identity);
  236. sort($identity);
  237. //获取 本地 已存在 联赛
  238. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id','others_lg_id')->get()->toArray();
  239. //二维数组去重
  240. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  241. //如果获取不到本地联赛数据,则返回联赛不存在
  242. if(empty($l_lg_data)) throw new \Exception(Response::generate($gameName.'本次请求的所有赛事都没有联赛数据;',Response::LEAGUE_ERROR));
  243. //获取到本次请求,所有本地有联赛的赛事
  244. $mathData_before = $matchData;//接收原赛事
  245. $matchData = [];//获取新赛事
  246. foreach($l_lg_data as $k=>$v){
  247. foreach($mathData_before as $kk=>$vv){
  248. if($v['identity'] == $vv['uuid']){
  249. $matchData[] = $vv;
  250. }
  251. }
  252. }
  253. //获取当前请求有关联联赛的赛事 match_id
  254. $s_match_ids = [];
  255. foreach($matchData as $k=>$v){
  256. $s_match_ids[] = $v['match_id'];
  257. }
  258. /*
  259. //循环对比 请求uuid->本地identity
  260. foreach($l_lg_data as $k=>$v){
  261. foreach($identity as $kk=> $uuid){
  262. if($v['identity'] == $uuid){
  263. unset($identity[$kk]);
  264. }
  265. }
  266. }
  267. sort($identity);
  268. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常
  269. if(!empty($identity)) throw new \Exception(Response::generate($gameName.'联赛:uuid-'.$identity[0].';',Response::LEAGUE_ERROR));
  270. //如果有剩余联赛,则表示该赛事找不到联赛,将其删除
  271. if(!empty($identity)){
  272. foreach($matchData as $k=>$v){
  273. foreach($identity as $kk=> $uuid){
  274. if($v['uuid'] == $uuid){
  275. unset($matchData[$k]);
  276. }
  277. }
  278. }
  279. sort($matchData);
  280. }
  281. //====end====
  282. */
  283. //====获取 本地 已存在 赛事====
  284. $s_match_ids = array_unique($s_match_ids);
  285. sort($s_match_ids);
  286. $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();
  287. //二维数组去重
  288. $l_match_data = commonFunction::uniquArrV2($l_match_data,'others_match_id');
  289. //循环对比 请求match_id->本地others_match_id
  290. //如果本地有 赛事,则取出本地不存在赛事
  291. if(!empty($l_match_data)){
  292. foreach($l_match_data as $k=>$v){
  293. foreach($s_match_ids as $kk=> $s_match_id){
  294. if($v['others_match_id'] == $s_match_id){
  295. unset($s_match_ids[$kk]);
  296. }
  297. }
  298. }
  299. sort($s_match_ids);
  300. }
  301. //剩余 赛事 数据 写入
  302. if(!empty($s_match_ids)){
  303. foreach($matchData as $k=>$v){
  304. foreach($s_match_ids as $kk=> $s_match_id){
  305. if($v['match_id'] == $s_match_id){
  306. $data = $v;
  307. $half_match_id = $v['half_match_id'];
  308. //获取 本地 联赛 ID
  309. //验证本赛事是否有数据源lg_id
  310. if(empty($data['lg_id'])) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::S_LG_ID_NULL));
  311. $lg_id = commonFunction::searcharray($data['uuid'],'identity',$l_lg_data,'lg_id');
  312. //验证 本赛事是否获取到本地lg_id
  313. if(empty($lg_id)) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::L_LG_ID_NULL));
  314. /*
  315. //如果有上半场赛事id 获取上半场赛事是否存在
  316. if(!empty($l_match_data) and $data['half_match_id'] > 0){
  317. $half_match_id = -1;
  318. $half_match_id = $models['model_local_match']::where(['others_match_id'=>$data['half_match_id'],'source'=>$source])
  319. ->value('match_id');
  320. 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'));
  321. }
  322. */
  323. //如果赛事没有开始日期,则为冠军盘口赛事
  324. if(empty($data['match_date'])){
  325. //冠军盘口赛事获取所属联赛结束时间
  326. $last_time = $models['model_league']::where(['id'=>$lg_id])
  327. ->value('last_time');
  328. 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'));
  329. //给冠军盘口赛事 赋值时间
  330. $time = strtotime($last_time);
  331. $data['match_date'] = date('Y-m-d',$time);
  332. $data['match_time'] = date('H:i:s',$time);
  333. }
  334. $set_match = [
  335. 'ctime'=>date('Y-m-d H:i:s'),
  336. 'utime'=>date('Y-m-d H:i:s'),
  337. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  338. 'home_team'=>$data['home_team']?:'',
  339. 'guest_team'=>$data['guest_team']?:'no_team',
  340. 'lg_id'=>$lg_id,
  341. 'status'=>$data['status'],
  342. 'match_date'=>$data['match_date']?:date('Y-m-d'),
  343. 'match_time'=>$data['match_time']?:date('H:i:s'),
  344. // 'tag'=>$data['tag']?:0,
  345. 'is_rollball'=>$data['is_rollball']?:0,
  346. 'is_today'=>$data['is_today']?:0,
  347. 'is_morningplate'=>$data['is_morningplate']?:0,
  348. 'is_stringscene'=>$data['is_stringscene']?:0,
  349. 'us_time'=>$data['us_time']?:commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  350. 'half_match_id'=>$half_match_id?:0,
  351. 'identity' => $data['uuid'],
  352. ];
  353. //写入赛事 返回id
  354. $id = $models['model_match']::insertGetId($set_match);
  355. if($id < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::INSERT_ERROR)) ;
  356. //写关联记录
  357. $set_local = [
  358. 'match_id'=>$id,
  359. 'others_match_id'=>$data['match_id'],
  360. 'source'=>$source,
  361. 'ctime'=>date('Y-m-d H:i:s')
  362. ];
  363. $ret = $models['model_local_match']::insertGetId($set_local);
  364. 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'));
  365. }
  366. }
  367. }
  368. }
  369. //提交事务
  370. DB::commit();
  371. return Response::success();
  372. } catch (\Exception $e) {
  373. //回滚事务
  374. DB::rollBack();
  375. return $e->getMessage();
  376. }
  377. }
  378. /*
  379. * 写 赛事 结果 记录 数据
  380. * 每个请求 包含 N个联赛 下 N个赛事 结果数据
  381. */
  382. public function setMatchResult(Req $data){
  383. //验证token
  384. $user = new \App\Models\System_user;
  385. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  386. $token = $user::where(['token'=>$data->token])->value('token');
  387. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  388. try {
  389. //开启事务
  390. DB::beginTransaction();
  391. $obt = $data->data;
  392. //请求 数据 为空
  393. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  394. $getData = $this->getAddData($obt);
  395. //不是 赛事 结果 记录 数据
  396. if($getData['title'] != 'match_result_r') throw new \Exception(Response::generate('不是赛事结果记录数据,',Response::ABNORMAL));
  397. /*
  398. //写请求数据 日志记录
  399. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  400. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  401. */
  402. //获取球类代码
  403. $game_code = $getData['game_code'];
  404. //获取数据源
  405. $source = $getData['source'];
  406. //获取球类名称
  407. $gameName = gameModel::getGameName($game_code);
  408. //获取 model
  409. $models = commonFunction::getModels($game_code, 1);
  410. //获取 赛事 数据
  411. $match_r_data = $getData['data'];
  412. //获取 当前请求 所有 uuid /match_id
  413. $identity = [];
  414. $s_match_ids = [];
  415. foreach($match_r_data as $k=>$v){
  416. $identity[] = $v['uuid'];
  417. $s_match_ids[] = $v['match_id'];
  418. }
  419. //====验证 赛事 所属 联赛 是否存在====
  420. $identity = array_unique($identity);
  421. sort($identity);
  422. //获取 本地 已存在 联赛
  423. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id')->get()->toArray();
  424. //二维数组去重
  425. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  426. //循环对比 请求uuid->本地identity
  427. foreach($l_lg_data as $k=>$v){
  428. foreach($identity as $kk=> $uuid){
  429. if($v['identity'] == $uuid){
  430. unset($identity[$kk]);
  431. }
  432. }
  433. }
  434. sort($identity);
  435. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常
  436. if(!empty($identity)) throw new \Exception(Response::generate($gameName.'联赛:uuid-'.$identity[0].';',Response::LEAGUE_ERROR));
  437. //====end====
  438. //====验证 赛事结果记录 所属赛事 是否存在====
  439. $s_match_ids = array_unique($s_match_ids);
  440. sort($s_match_ids);
  441. //获取 本地 已存在 赛事
  442. $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();
  443. //二维数组去重
  444. $l_match_data = commonFunction::uniquArrV2($l_match_data,'others_match_id');
  445. //循环对比 请求match_id->本地others_match_id
  446. foreach($l_match_data as $k=>$v){
  447. foreach($s_match_ids as $kk=> $s_match_id){
  448. if($v['others_match_id'] == $s_match_id){
  449. unset($s_match_ids[$kk]);
  450. }
  451. }
  452. }
  453. sort($s_match_ids);
  454. //去除本地和请求里都存在的赛事,如果还有剩余赛事id,则返回异常
  455. if(!empty($s_match_ids)) throw new \Exception(Response::generate($gameName.'赛事-match_id'.$s_match_ids[0].';',Response::MATCH_ERROR));
  456. //====end====
  457. //处理 赛事 结果记录 数据
  458. foreach($match_r_data as $k=>$v){
  459. $data = $v;
  460. //获取 本地 联赛 ID
  461. $lg_id = commonFunction::searcharray($data['uuid'],'identity',$l_lg_data,'lg_id');
  462. //获取 本地 赛事 ID
  463. $match_id = commonFunction::searcharray($data['match_id'],'others_match_id',$l_match_data,'match_id');
  464. //根据球类 获取 赛事结果记录字段
  465. $set_match_r = $this->get_match_r($game_code,$lg_id,$match_id,$data);
  466. //写 赛事 结果 记录
  467. $ret = $models['model_result_record']::insert($set_match_r);
  468. if($ret != true) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_R_ERROR));
  469. }
  470. //提交事务
  471. DB::commit();
  472. return Response::success();
  473. } catch (\Exception $e) {
  474. //回滚事务
  475. DB::rollBack();
  476. return $e->getMessage();
  477. }
  478. }
  479. /*
  480. * 写 普通 赔率 数据
  481. * 每个请求 只包含 一场赛事下 N条赔率
  482. */
  483. public function setOdds(Req $data){
  484. //验证token
  485. $user = new \App\Models\System_user;
  486. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  487. $token = $user::where(['token'=>$data->token])->value('token');
  488. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  489. try {
  490. //开启事务
  491. DB::beginTransaction();
  492. $obt = $data->data;
  493. //请求 数据 为空
  494. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  495. $getData = $this->getAddData($obt);
  496. //不是 赔率 数据
  497. if($getData['title'] != 'odds') throw new \Exception(Response::generate('不是赔率数据,',Response::ABNORMAL));
  498. //写请求数据 日志记录
  499. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  500. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  501. //获取球类代码
  502. $game_code = $getData['game_code'];
  503. //获取数据源
  504. $source = $getData['source'];
  505. //获取源数据联赛ID
  506. $uuid = $getData['uuid'];
  507. //获取源数据赛事ID
  508. $s_match_id = $getData['match_id'];
  509. //获取赔率所属赛事 tag 值 玩法数量
  510. $tag = $getData['tag']?:99;
  511. //获取球类名称
  512. $gameName = gameModel::getGameName($game_code);
  513. //验证本次请求所属联赛/赛事是否存在 返回本地联赛/赛事ID
  514. $models = commonFunction::getModels($game_code, 1);
  515. $lg_id = $this->leagueVerify($models,$uuid,$source,$gameName);
  516. $match_id = $this->matchVerify($models,$s_match_id,$source,$gameName);
  517. //更新赛事 tag 值
  518. //如果未获取到本地赛事id,则返回异常
  519. if(empty($match_id)) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::L_MATCH_ID_ERR)) ;
  520. $upMatch = $models['model_match']::where(['id'=>$match_id])
  521. ->update(['tag'=>$tag,'utime'=>date('Y-m-d H:i:s')]);
  522. if($upMatch < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::UPMATCHTAG_ERROR)) ;
  523. //获取 赔率数据
  524. $oddsData = $getData['data'];
  525. //获取 当前 所有 可用 赔率 sole
  526. $odds_only = $getData['odds_only'];
  527. //更新赛事下所有 不在本次请求的赔率 状态
  528. $ret= OddsModel::upOddsStatus($models,$s_match_id,'',$source,$odds_only);
  529. if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  530. //获取当前赛事 在本地的所有赔率
  531. $l_odds_data = $models['model_odds']::select('odds_only')->where(['match_id'=>$match_id])->get()->toArray();
  532. $set_odds_r = [];
  533. $set_odds = [];
  534. if(!empty($oddsData)){
  535. //去除本地已有的赔率
  536. if(!empty($l_odds_data)){
  537. foreach($oddsData as $k=>$v){
  538. foreach($l_odds_data as $kk=>$vv){
  539. if($v['odds_only'] == $vv['odds_only']){
  540. unset($oddsData[$k]);
  541. }
  542. }
  543. }
  544. sort($oddsData);
  545. }
  546. //处理待写入 赔率记录数据
  547. foreach($oddsData as $k=>$v){
  548. //组装数据
  549. $set_odds_r[] = [
  550. 'match_id'=> $match_id?:0,
  551. 'others_match_id'=> $v['match_id']?:0,
  552. 'odds_code'=> $v['odds_code']?:'',
  553. 'status'=> $v['status']?:0,
  554. 'sort'=> $v['sort']?:0,
  555. 'p_code'=> $v['p_code']?:'',
  556. 'odds'=> $v['odds']?:0,
  557. 'condition'=> $v['condition'],
  558. 'odds_only'=> $v['odds_only']?:'',
  559. 'source'=> $v['source']?:'',
  560. 'type'=> $v['type']?:0,
  561. 'team'=> $v['team']?:'',
  562. 'lg_id'=> $lg_id,
  563. 'others_lg_id'=> $v['lg_id'],
  564. 'ctime'=> date('Y-m-d H:i:s'),
  565. 'utime'=> date('Y-m-d H:i:s'),
  566. ];
  567. }
  568. //处理待写入 赔率数据
  569. foreach($oddsData as $k=>$v){
  570. //组装数据
  571. $set_odds[] = [
  572. 'match_id'=> $match_id?:0,
  573. 'others_match_id'=> $v['match_id']?:0,
  574. 'odds_code'=> $v['odds_code']?:'',
  575. 'status'=> $v['status']?:0,
  576. 'sort'=> $v['sort']?:0,
  577. 'p_code'=> $v['p_code']?:'',
  578. 'odds'=> $v['odds']?:0,
  579. 'condition'=> $v['condition'],
  580. 'odds_only'=> $v['odds_only']?:'',
  581. 'source'=> $v['source']?:'',
  582. 'type'=> $v['type']?:0,
  583. 'team'=> $v['team']?:'',
  584. 'lg_id'=> $lg_id,
  585. 'others_lg_id'=> $v['lg_id'],
  586. 'ctime'=> date('Y-m-d H:i:s'),
  587. 'utime'=> date('Y-m-d H:i:s'),
  588. 'sole' => $v['sole'],
  589. 'expire_time' => date('Y-m-d H:i:s',time()+60)
  590. ];
  591. }
  592. //写赔率记录数据
  593. $ret = $models['model_odds_record']::insert($set_odds_r);
  594. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率记录;',Response::ADD_ODDS_R_ERROR));
  595. //写赔率 数据
  596. $ret = $models['model_odds']::insert($set_odds);
  597. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率;',Response::ADD_ODDS_ERROR));
  598. }
  599. //提交事务
  600. DB::commit();
  601. return Response::success();
  602. } catch (\Exception $e) {
  603. //回滚事务
  604. DB::rollBack();
  605. return $e->getMessage();
  606. }
  607. }
  608. /*
  609. *写 冠军联赛 赔率 数据
  610. *每个请求 包含 N个联赛 下 N条赔率
  611. */
  612. public function setOddsCH(Req $data){
  613. //验证token
  614. $user = new \App\Models\System_user;
  615. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  616. $token = $user::where(['token'=>$data->token])->value('token');
  617. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  618. try {
  619. //开启事务
  620. DB::beginTransaction();
  621. $obt = $data->data;
  622. //请求 数据 为空
  623. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  624. $getData = $this->getAddData($obt);
  625. //不是 冠军 赔率 数据
  626. if($getData['title'] != 'odds_ch') throw new \Exception(Response::generate('不是冠军赔率数据,',Response::ABNORMAL));
  627. //写请求数据 日志记录
  628. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  629. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  630. //获取球类代码
  631. $game_code = $getData['game_code'];
  632. //获取数据源
  633. $source = $getData['source'];
  634. //获取球类名称
  635. $gameName = gameModel::getGameName($game_code);
  636. //获取 model
  637. $models = commonFunction::getModels($game_code, 1);
  638. //获取 赔率数据
  639. $oddsData = $getData['data'];
  640. //获取 当前 所有 可用 赔率 sole
  641. $odds_only = $getData['odds_only'];
  642. //====验证 请求里 所有赔率 所属联赛 是否存在 ====
  643. //获取 当前请求 所有 联赛 uuid
  644. $uuids = [];
  645. $s_lg_ids = [];
  646. foreach($oddsData as $k=>$v){
  647. $uuids[] = $v['uuid'];
  648. $s_lg_ids[] = $v['lg_id'];
  649. }
  650. $s_lg_ids = array_unique($s_lg_ids);
  651. $uuids = array_unique($uuids);
  652. sort($uuids);
  653. sort($s_lg_ids);
  654. //更新联赛下所有 不在本次请求的赔率 状态
  655. $ret= OddsModel::upOddsStatus($models,'',$s_lg_ids,$source,$odds_only);
  656. if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  657. //获取 本地 已存在 联赛
  658. $l_lg_data = $models['model_local_league']::whereIn('identity',$uuids)->select('identity','lg_id')->get()->toArray();
  659. //二维数组去重
  660. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  661. //循环对比 请求uuid->本地uuid
  662. foreach($l_lg_data as $k=>$v){
  663. foreach($uuids as $kk=> $uuid){
  664. if($v['identity'] == $uuid){
  665. unset($uuids[$kk]);
  666. }
  667. }
  668. }
  669. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常;
  670. if(!empty($uuids)) throw new \Exception(Response::generate($gameName.'冠军联赛:uuid-'.$uuids[0].';',Response::LEAGUE_ERROR));
  671. //====end====
  672. //赔率记录数据
  673. $set_odds_r = [];
  674. //赔率数据
  675. $set_odds = [];
  676. if(!empty($oddsData)){
  677. foreach($oddsData as $k=>$v){
  678. //获取 本地 联赛 ID
  679. $lg_id = commonFunction::searcharray($v['uuid'],'identity',$l_lg_data,'lg_id');
  680. //组装赔率记录数据
  681. $set_odds_r[] = [
  682. 'match_id'=>0,
  683. 'others_match_id'=> $v['match_id']?:0,
  684. 'odds_code'=> $v['odds_code']?:'',
  685. 'status'=> $v['status']?:0,
  686. 'sort'=> $v['sort']?:0,
  687. 'p_code'=> $v['p_code']?:'',
  688. 'odds'=> $v['odds']?:0,
  689. 'condition'=> $v['condition'],
  690. 'odds_only'=> $v['odds_only']?:'',
  691. 'source'=> $v['source']?:'',
  692. 'type'=> $v['type']?:1,
  693. 'team'=> $v['team']?:'',
  694. 'lg_id'=> $lg_id,
  695. 'others_lg_id'=> $v['lg_id'],
  696. 'ctime'=> date('Y-m-d H:i:s'),
  697. 'utime'=> date('Y-m-d H:i:s'),
  698. ];
  699. }
  700. foreach($oddsData as $k=>$v){
  701. //获取 本地 联赛 ID
  702. $lg_id = commonFunction::searcharray($v['uuid'],'identity',$l_lg_data,'lg_id');
  703. //组装赔率数据
  704. $set_odds[] = [
  705. 'match_id'=>0,
  706. 'others_match_id'=> $v['match_id']?:0,
  707. 'odds_code'=> $v['odds_code']?:'',
  708. 'status'=> $v['status']?:0,
  709. 'sort'=> $v['sort']?:0,
  710. 'p_code'=> $v['p_code']?:'',
  711. 'odds'=> $v['odds']?:0,
  712. 'condition'=> $v['condition'],
  713. 'odds_only'=> $v['odds_only']?:'',
  714. 'source'=> $v['source']?:'',
  715. 'type'=> $v['type']?:1,
  716. 'team'=> $v['team']?:'',
  717. 'lg_id'=> $lg_id,
  718. 'others_lg_id'=> $v['lg_id'],
  719. 'ctime'=> date('Y-m-d H:i:s'),
  720. 'utime'=> date('Y-m-d H:i:s'),
  721. 'sole' => $v['sole'],
  722. 'expire_time' => date('Y-m-d H:i:s',time()+60)
  723. ];
  724. }
  725. //写赔率记录数据
  726. $ret = $models['model_odds_record']::insert($set_odds_r);
  727. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率记录;',Response::ADD_ODDS_R_ERROR));
  728. //写赔率 数据
  729. $ret = $models['model_odds']::insert($set_odds);
  730. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率;',Response::ADD_ODDS_ERROR));
  731. }
  732. //提交事务
  733. DB::commit();
  734. return Response::success();
  735. } catch (\Exception $e) {
  736. //回滚事务
  737. DB::rollBack();
  738. return $e->getMessage();
  739. }
  740. }
  741. /*
  742. * 写入直播 数据
  743. */
  744. public function setBroadCast(Req $data){
  745. //验证token
  746. $user = new \App\Models\System_user;
  747. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  748. $token = $user::where(['token'=>$data->token])->value('token');
  749. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  750. try {
  751. //开启事务
  752. DB::beginTransaction();
  753. $obt = $data->data;
  754. //请求 数据 为空
  755. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  756. $getData = $this->getAddData($obt);
  757. //不是 直播 数据
  758. if($getData['title'] != 'broad_cast') throw new \Exception(Response::generate('不是直播数据,',Response::ABNORMAL));
  759. /*
  760. //写请求数据 日志记录
  761. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  762. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  763. */
  764. //获取 球类
  765. $game_code = $getData['game_code'];
  766. //获取 直播 数据
  767. $BroadCast = $getData['data'];
  768. $set_broadcast = [];
  769. foreach($BroadCast as $k=>$data){
  770. //组装数据
  771. $set_broadcast[] = [
  772. "doing" => $data['doing'],
  773. "game_type" => $data['game_type'],
  774. "game_code" => $game_code,
  775. "guest_team" => $data['guest_team'],
  776. "host_team" => $data['host_team'],
  777. "league_name" => $data['league_name'],
  778. "shower" => $data['shower'],
  779. "showid" => (int)$data['showid'],
  780. "start_time" => $data['start_time'],
  781. "ctime" =>date('Y-m-d H:i:s'),
  782. "utime" =>date('Y-m-d H:i:s'),
  783. ];
  784. }
  785. //写入 直播 数据
  786. $ret = broadcastModel::insert($set_broadcast);
  787. if($ret == false) throw new \Exception(Response::generate('',Response::BROADCAST_ERROR));
  788. //提交事务
  789. DB::commit();
  790. return Response::success();
  791. } catch (\Exception $e) {
  792. //回滚事务
  793. DB::rollBack();
  794. return $e->getMessage();
  795. }
  796. }
  797. /*
  798. * 验证所属 联赛 是否存在
  799. */
  800. public function leagueVerify($models=[],$uuid='',$source='',$gameName=''){
  801. if(empty($models) || empty($uuid)) throw new \Exception(Response::generate('',Response::ABNORMAL));
  802. $l_lg_id = $models['model_local_league']::where(['identity'=>$uuid])->value('lg_id');
  803. if($l_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛:lg_id-'.$uuid,Response::LEAGUE_ERROR));
  804. return $l_lg_id;
  805. }
  806. /*
  807. * 验证所属 赛事 是否存在
  808. */
  809. public function matchVerify($models=[],$s_match_id='',$source='',$gameName=''){
  810. if(empty($models) || empty($s_match_id)) throw new \Exception(Response::generate('',Response::ABNORMAL));
  811. $l_match_id = $models['model_local_match']::where(['others_match_id'=>$s_match_id,'source'=>$source])->value('match_id');
  812. if($l_match_id < 1) throw new \Exception(Response::generate($gameName.'赛事:match_id-'.$s_match_id,Response::MATCH_ERROR));
  813. return $l_match_id;
  814. }
  815. /*
  816. * 写入数据 接口
  817. */
  818. public function setSports(Req $data){
  819. try {
  820. //开启事务
  821. DB::beginTransaction();
  822. if($data->game_code){
  823. //用于后台 将所有进行中的赛事写入结果
  824. $this->match_result($data->game_code);
  825. }else{
  826. //验证token
  827. $dd = $data->session()->get('adminInfo');
  828. if(empty($data->token) ||($dd['token'] != $data->token)) return Response::generate('',Response::TOKEN_ERR);
  829. //写赛事数据
  830. $obt = $data->data;
  831. if($obt){
  832. $getData = $this->getAddData($obt);
  833. $league = [];
  834. $competition = [];
  835. $odds = [];
  836. $odds_soly = [];
  837. $league_result = [];
  838. $match_result = [];
  839. $match_result_record = [];
  840. $odds_record = [];
  841. $broadcast = [];
  842. //指定排序 联赛->赛事->赔率->联赛结果->赛事结果->赛事结果记录->赔率记录->直播数据
  843. foreach ($getData as $k=>$v){
  844. $game_code = $v['game_code'];
  845. if($v['title'] == 'league') $league[] = $v;
  846. if($v['title'] == 'competition') $competition[] = $v;
  847. if($v['title'] == 'odds') $odds[] = $v;
  848. if($v['title'] == 'odds_sole') $odds_soly[] = $v;
  849. if($v['title'] == 'league_result') $league_result[] = $v;
  850. if($v['title'] == 'match_result') $match_result[] = $v;
  851. if($v['title'] == 'match_result_record') $match_result_record[] = $v;
  852. if($v['title'] == 'odds_record') $odds_record[] = $v;
  853. if($v['title'] == 'broadcast') $broadcast[] = $v;
  854. }
  855. $matchData = [$league,$competition,$odds_soly,$odds,$league_result,$match_result,$match_result_record,$odds_record,$broadcast];
  856. //排空处理
  857. foreach ($matchData as $k=>$v){
  858. if($v == []) unset($matchData[$k]);
  859. }
  860. sort($matchData);
  861. //根据顺序写入数据
  862. //降维数据
  863. $mentData = [];
  864. foreach ($matchData as $k=>$v){
  865. foreach ($v as $kk=>$vv){
  866. $mentData[] = $vv;
  867. }
  868. }
  869. //获取各球类model
  870. $models = commonFunction::getModels($game_code,1);
  871. //获取球类名称
  872. $gameName = gameModel::getGameName($game_code);
  873. //===获取验证数据===
  874. //获取一段时间内所有联赛本地记录
  875. $lg_data = LeagueModel::getLeagueID($models);
  876. //获取一段时间内所有赛事本地记录
  877. $match_data = MatchModel::getMatchID($models);
  878. //获取一段时间内所有赔率
  879. $odds_data = OddsModel::getOddsID($models);
  880. //获取一段时间内所有赔率记录
  881. $odds_record_data = OddsRecordModel::getOddsRecordID($models);
  882. //===获取验证数据===
  883. $others_lg_id = '';//继承 源联赛id
  884. $others_match_id = '';//继承 源赛事id
  885. $s_lg_id = '';//源 联赛ID
  886. $l_lg_id = '';//本地 联赛 ID
  887. $s_match_id = '';//源 赛事ID
  888. $l_match_id = '';//本地 赛事 ID
  889. foreach ($mentData as $kk =>$vv){
  890. switch ($vv['title']){
  891. case 'area'://地区
  892. throw new \Exception(Response::generate('地区数据-area:',Response::AUTH_ERROR)) ;
  893. break;
  894. case 'country'://国家
  895. throw new \Exception(Response::generate('国家数据-country:',Response::AUTH_ERROR));
  896. break;
  897. case 'league'://联赛
  898. $others_lg_id = $vv['data']['lg_id'];
  899. $s_lg_id = $vv['data']['lg_id'];
  900. $source = $vv['data']['source'];
  901. //验证联赛记录是否已存在
  902. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  903. if($ret_lg != false) {
  904. $l_lg_id = $ret_lg;
  905. break;
  906. }
  907. //执行 联赛数据
  908. $lg_data = $this->league($vv,$gameName);
  909. $l_lg_id = $lg_data['lg_id'];
  910. break;
  911. case 'competition'://赛事
  912. $others_match_id = $vv['data']['match_id'];
  913. $s_lg_id = $vv['data']['lg_id'];
  914. $s_match_id = $vv['data']['match_id'];
  915. $source = $vv['data']['source'];
  916. //本次请求是否包含 联赛数据
  917. if(empty($others_lg_id)){
  918. //验证本地是否存在
  919. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  920. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  921. $l_lg_id = $ret_lg;
  922. }else{
  923. //本次有 联赛数据 验证属于同联赛
  924. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  925. }
  926. //验证赛事记录是否已存在
  927. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  928. if($ret_match != false){
  929. $l_match_id = $ret_match;
  930. break;
  931. }
  932. //执行 赛事数据
  933. $match_data = $this->competition($vv,$gameName,$l_lg_id);
  934. $l_match_id = $match_data['match_id'];
  935. break;
  936. case 'odds'://赔率
  937. $s_lg_id = $vv['data']['lg_id'];
  938. $s_match_id = $vv['data']['match_id'];
  939. $source = $vv['data']['source'];
  940. $odds_only = $vv['data']['odds_only'];
  941. $sole = $vv['data']['sole'];
  942. $odds_type = $vv['data']['type'];//0普通 1冠军盘口
  943. //本次请求是否包含 联赛数据
  944. if(empty($others_lg_id)){
  945. //无联赛数据 验证本地是否存在
  946. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  947. //联赛 不存在
  948. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  949. $l_lg_id = $ret_lg;
  950. }else{
  951. //有联赛数据 验证属于同请求联赛
  952. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  953. }
  954. //如果是普通 赔率,则验证所属赛事
  955. if($odds_type == 0){
  956. //验证赛事记录是否已存在
  957. if(empty($others_match_id)){
  958. //无赛事数据 验证本地是否存在
  959. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  960. //赛事 不存在
  961. if($ret_match == false) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  962. $l_match_id = $ret_match;
  963. }else{
  964. //有赛事数据 验证属于同请求赛事
  965. if($s_match_id != $others_match_id) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  966. }
  967. }
  968. //验证赔率 是否存在
  969. $ret_odds = '';
  970. $ret_odds = commonFunction::ver_odds($sole,$source,$odds_data);
  971. //验证赔率记录 是否存在
  972. $ret_odds_record = '';
  973. $ret_odds_record = commonFunction::ver_odds_record($odds_only,$source,$odds_record_data);
  974. $this->odds($vv,$gameName,$l_lg_id,$l_match_id,$ret_odds,$ret_odds_record);
  975. break;
  976. case 'odds_sole':
  977. $this->upOddsStatus($vv,$gameName);
  978. break;
  979. case 'league_result'://联赛结果
  980. $this->league_result($vv,$gameName);
  981. break;
  982. case 'match_result'://赛事结果
  983. throw new \Exception(Response::generate('赛事结果数据-match_result:',Response::AUTH_ERROR));
  984. $this->match_result($vv,$gameName);
  985. break;
  986. case 'match_result_record'://赛事结果记录
  987. $s_lg_id = $vv['data']['lg_id'];
  988. $s_match_id = $vv['data']['match_id'];
  989. $source = $vv['data']['source'];
  990. //如果本次请求没有 联赛数据
  991. if(empty($others_lg_id)){
  992. //验证本地是否存在
  993. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  994. //联赛 不存在
  995. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  996. $l_lg_id = $ret_lg;
  997. }else{
  998. //有联赛数据 验证属于同请求联赛
  999. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1000. }
  1001. //验证赛事记录是否已存在
  1002. if(empty($others_match_id)){
  1003. //无赛事数据 验证本地是否存在
  1004. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  1005. //联赛 不存在
  1006. if($ret_match == false) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  1007. $l_match_id = $ret_match;
  1008. }else{
  1009. //有赛事数据 验证属于同请求赛事
  1010. if($s_match_id != $others_match_id) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  1011. }
  1012. //处理 赛事结果 记录
  1013. $this->com_result_record($vv,$gameName,$l_lg_id,$l_match_id);
  1014. break;
  1015. case 'broadcast'://直播数据
  1016. $this->broadcast($vv,$game_code);
  1017. break;
  1018. default:
  1019. throw new \Exception(Response::generate('',Response::ABNORMAL)) ;
  1020. }
  1021. }
  1022. }
  1023. }
  1024. //提交事务
  1025. DB::commit();
  1026. return Response::success();
  1027. } catch (\Exception $e) {
  1028. //回滚事务
  1029. DB::rollBack();
  1030. return $e->getMessage();
  1031. }
  1032. }
  1033. /**
  1034. * @param Req $data
  1035. * @return string
  1036. * @throws \App\Lib\Biz\Sport\Exception
  1037. * 更新赛事状态
  1038. */
  1039. public function upMatch(Req $data){
  1040. //验证token
  1041. $user = new \App\Models\System_user;
  1042. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  1043. $token = $user::where(['token'=>$data->token])->value('token');
  1044. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  1045. try {
  1046. //开启事务
  1047. DB::beginTransaction();
  1048. //获取待更新赛事
  1049. $obt = $data->data;
  1050. if($obt){
  1051. //json转数组
  1052. $data = $this->getAddData($obt);
  1053. //获取 球类代码
  1054. $game_code = $data['game_code'];
  1055. //获取 数据源
  1056. $source = $data['source'];
  1057. //获取所有数据源赛事 match_id
  1058. $others_match_ids = [];
  1059. foreach ($data['data'] as $k=>$v){
  1060. $others_match_ids[] = $v['match_id'];
  1061. }
  1062. //根据球类代码 获取model
  1063. $model =commonFunction::getModels($game_code,1);
  1064. //获取所有赛事 match_id
  1065. $local_match = $model['model_local_match']::SELECT('others_match_id','match_id')
  1066. ->where(['source'=>$source])
  1067. ->whereIn('others_match_id',$others_match_ids)
  1068. ->get()->toArray();
  1069. if(empty($local_match)) throw new \Exception(Response::generate('',Response::MATCHID_NULL));
  1070. //更新状态字段
  1071. foreach ($local_match as $k=>$v){
  1072. $set_status = [
  1073. 'status'=>$v['status'],
  1074. 'is_rollball'=>$v['is_rollball'],
  1075. 'stais_todaytus'=>$v['is_today'],
  1076. 'is_morningplate'=>$v['is_morningplate'],
  1077. 'is_stringscene'=>$v['is_stringscene'],
  1078. 'is_horn'=>$v['is_horn'],
  1079. ];
  1080. $ret = $model['model_match']::where(['id'=>$v['match_id']])
  1081. -> update($set_status);
  1082. if($ret<1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$v['others_match_id'],Response::UPSTATUS_ERROR));
  1083. }
  1084. }
  1085. //提交事务
  1086. DB::commit();
  1087. return Response::success();
  1088. } catch (\Exception $e) {
  1089. //回滚事务
  1090. DB::rollBack();
  1091. return $e->getMessage();
  1092. }
  1093. }
  1094. /*
  1095. * 写入直播 数据
  1096. */
  1097. public function broadcast($opt = [],$game_code=''){
  1098. $data = $opt['data'];
  1099. $set_broadcast = [
  1100. "doing" => $data['doing'],
  1101. "game_type" => $data['game_type'],
  1102. "game_code" => $game_code,
  1103. "guest_team" => $data['guest_team'],
  1104. "host_team" => $data['host_team'],
  1105. "league_name" => $data['league_name'],
  1106. "shower" => $data['shower'],
  1107. "showid" => (int)$data['showid'],
  1108. "start_time" => $data['start_time'],
  1109. "ctime" =>date('Y-m-d H:i:s'),
  1110. "utime" =>date('Y-m-d H:i:s'),
  1111. ];
  1112. $ret = broadcastModel::insert($set_broadcast);
  1113. if($ret == false) throw new \Exception(Response::generate('',Response::BROADCAST_ERROR));
  1114. }
  1115. /*
  1116. * 写入 赛事 初始 结果
  1117. */
  1118. public function match_result($game_code = ''){
  1119. $model =commonFunction::getModels($game_code,1);
  1120. if($game_code == 'zq') ZqResultModel::ZQresult($model);
  1121. if($game_code == 'lq') LqResultModel::LQresult($model);
  1122. if($game_code == 'wq') WqResultModel::WQresult($model);
  1123. if($game_code == 'bq') BqResultModel::BQresult($model);
  1124. }
  1125. /*
  1126. * 写入联赛数据
  1127. */
  1128. public function league($opt = [],$gameName=''){
  1129. $game_code = $opt['game_code'];
  1130. //根据球类代码获取相关model
  1131. $model =commonFunction::getModels($game_code,1);
  1132. $data = $opt['data'];
  1133. //查询联赛是否已存在
  1134. $id = $model['model_league']::where('name_chinese','=',$data['name_chinese'])
  1135. ->value('id');
  1136. //默认获取本年最后一天
  1137. $last_time = date('Y-12-31 23:59:59');
  1138. //决赛时间
  1139. if($data['last_time']){
  1140. $last_time = $data['last_time'];
  1141. }
  1142. $set_lg['name_chinese'] = $data['name_chinese'];
  1143. $set_lg['kind'] = $data['kind'];
  1144. $set_lg['match_mode'] = $data['match_mode'];
  1145. $set_lg['if_stop'] = $data['if_stop'];
  1146. $set_lg['identity'] = $data['uuid']?:'';
  1147. $set_lg['last_time'] = $last_time;
  1148. $set_lg['utime'] = date('Y-m-d H:i:s');
  1149. if(empty($id)){
  1150. //写入联赛
  1151. $id = $model['model_league']::insertGetId($set_lg);
  1152. $m_lg_id = $id;
  1153. if($m_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::INSERT_ERROR));
  1154. }else{
  1155. //更新联赛
  1156. $ret = $model['model_league']::where(['id'=>$id])
  1157. -> update($set_lg);
  1158. if($ret < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::UPDATE_ERROR));
  1159. }
  1160. $set_local = [
  1161. 'lg_id'=>$id,
  1162. 'others_lg_id'=>$data['lg_id'],
  1163. 'source'=>$data['source'],
  1164. 'ctime'=>date('Y-m-d H:i:s')
  1165. ];
  1166. $ret = $model['model_local_league']::insertGetId($set_local);
  1167. 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'));
  1168. //执行成功返回源数据 联赛ID
  1169. $data = ['others_lg_id'=>$data['lg_id'],'lg_id'=>$id];
  1170. return $data;
  1171. }
  1172. /*
  1173. * 写入赛事数据
  1174. */
  1175. public function competition($opt = [],$gameName='',$lg_id=''){
  1176. $game_code = $opt['game_code'];
  1177. //根据球类代码获取相关model
  1178. $model = commonFunction::getModels($game_code,1);
  1179. $data = $opt['data'];
  1180. if(empty($data['lg_id'])) throw new \Exception(Response::generate('',Response::LEAGUE_ERROR)) ;//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  1181. //查询赛事是否存在
  1182. $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']])
  1183. ->value('id');
  1184. $half_match_id = 0;
  1185. //如果有上半场赛事id 获取上半场赛事是否存在
  1186. if(!empty($data['half_match_id'])){
  1187. $half_match_id = $match_id = $model['model_local_match']::where(['others_match_id'=>$data['half_match_id'],'source'=>$data['source']])
  1188. ->value('match_id');
  1189. 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'));
  1190. }
  1191. //如果赛事没有开始日期,则为冠军盘口赛事
  1192. if(empty($data['match_date'])){
  1193. //冠军盘口赛事获取所属联赛结束时间
  1194. $last_time = $model['model_league']::where(['id'=>$lg_id])
  1195. ->value('last_time');
  1196. 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'));
  1197. //给冠军盘口赛事 赋值时间
  1198. $time = strtotime($last_time);
  1199. $data['match_date'] = date('Y-m-d',$time);
  1200. $data['match_time'] = date('H:i:s',$time);
  1201. }
  1202. $set_match = [
  1203. 'ctime'=>date('Y-m-d H:i:s'),
  1204. 'utime'=>date('Y-m-d H:i:s'),
  1205. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  1206. 'home_team'=>$data['home_team']?:'',
  1207. 'guest_team'=>$data['guest_team']?:'no_team',
  1208. 'lg_id'=>$lg_id,
  1209. 'status'=>$data['status'],
  1210. 'match_date'=>$data['match_date']?:date('Y-m-d'),
  1211. 'match_time'=>$data['match_time']?:date('H:i:s'),
  1212. 'tag'=>$data['tag']?:0,
  1213. 'is_rollball'=>$data['is_rollball']?:0,
  1214. 'is_today'=>$data['is_today']?:0,
  1215. 'is_morningplate'=>$data['is_morningplate']?:0,
  1216. 'is_stringscene'=>$data['is_stringscene']?:0,
  1217. 'us_time'=>$data['us_time']?:commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  1218. 'half_match_id'=>$half_match_id?:0,
  1219. 'identity' => $data['uuid'],
  1220. ];
  1221. //写入赛事
  1222. if(empty($id)){
  1223. //写入赛事 返回id
  1224. $id = $model['model_match']::insertGetId($set_match);
  1225. if($id < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::INSERT_ERROR)) ;
  1226. }else{
  1227. //更新赛事
  1228. $ret = $model['model_match']::where(['id'=>$id])->update($set_match);
  1229. if($ret < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::UPDATE_ERROR)) ;
  1230. }
  1231. //写关联记录
  1232. $set_local = [
  1233. 'match_id'=>$id,
  1234. 'others_match_id'=>$data['match_id'],
  1235. 'source'=>$data['source'],
  1236. 'ctime'=>date('Y-m-d H:i:s')
  1237. ];
  1238. $ret = $model['model_local_match']::insertGetId($set_local);
  1239. 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'));
  1240. //返回 源数据 赛事ID
  1241. $data = ['others_match_id'=>$data['match_id'],'match_id'=>$id];
  1242. return $data;
  1243. }
  1244. /*
  1245. * 写入赔率/赔率记录数据
  1246. */
  1247. public function odds($opt=[],$gameName='',$lg_id=0,$match_id=0,$oddsID='',$oddsRecordID=''){
  1248. $game_code = $opt['game_code'];
  1249. //根据球类代码获取相关model
  1250. $model = commonFunction::getModels($game_code,1);
  1251. $data = $opt['data'];
  1252. //获取赛事 本地/源ID
  1253. $others_match_id = $data['match_id'];
  1254. //获取联赛 本地/源ID
  1255. $others_lg_id = $data['lg_id'];
  1256. //===写赔率记录===
  1257. $set_odds_r = [
  1258. 'match_id'=> $match_id?:0,
  1259. 'others_match_id'=> $others_match_id?:0,
  1260. 'odds_code'=> $data['odds_code']?:'',
  1261. 'status'=> $data['status']?:0,
  1262. 'sort'=> $data['sort']?:0,
  1263. 'p_code'=> $data['p_code']?:'',
  1264. 'odds'=> $data['odds']?:0,
  1265. 'condition'=> $data['condition'],
  1266. 'odds_only'=> $data['odds_only']?:'',
  1267. 'source'=> $data['source']?:'',
  1268. 'type'=> $data['type']?:0,
  1269. 'team'=> $data['team']?:'',
  1270. 'lg_id'=> $lg_id,
  1271. 'others_lg_id'=> $others_lg_id,
  1272. 'ctime'=> date('Y-m-d H:i:s'),
  1273. ];
  1274. //更新或写入赔率记录
  1275. if($oddsRecordID != false){
  1276. $ret = $model['model_odds_record']::where(['id'=>$oddsRecordID])
  1277. -> update($set_odds_r);
  1278. 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'));
  1279. }else{
  1280. $ret = $model['model_odds_record']::insert($set_odds_r);
  1281. 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'));
  1282. }
  1283. //===写赔率===
  1284. $set_odds = [
  1285. 'match_id'=> $match_id?:0,
  1286. 'others_match_id'=> $others_match_id?:0,
  1287. 'odds_code'=> $data['odds_code']?:'',
  1288. 'status'=> $data['status']?:0,
  1289. 'sort'=> $data['sort']?:0,
  1290. 'p_code'=> $data['p_code']?:'',
  1291. 'odds'=> $data['odds']?:0,
  1292. 'condition'=> $data['condition'],
  1293. 'odds_only'=> $data['odds_only']?:'',
  1294. 'sole'=> $data['sole']?:'',
  1295. 'source'=> $data['source']?:'',
  1296. 'type'=> $data['type']?:0,
  1297. 'team'=> $data['team']?:'',
  1298. 'lg_id'=> $lg_id,
  1299. 'others_lg_id'=> $others_lg_id,
  1300. 'ctime'=> date('Y-m-d H:i:s'),
  1301. 'utime'=> date('Y-m-d H:i:s'),
  1302. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  1303. ];
  1304. //更新或写入赔率数据
  1305. if($oddsID != false){
  1306. $ret = $model['model_odds']::where(['id'=>$oddsID])
  1307. -> update($set_odds);
  1308. 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'));
  1309. }else{
  1310. $ret = $model['model_odds']::insert($set_odds);
  1311. 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'));
  1312. }
  1313. //===end===
  1314. }
  1315. /*
  1316. * 更新 赛事下 所有不在本次请求的 赔率状态
  1317. */
  1318. public function upOddsStatus($opt = [],$gameName=''){
  1319. //获取球类代码
  1320. $game_code = $opt['game_code'];
  1321. //获取球类model
  1322. $model = commonFunction::getModels($game_code,1);
  1323. //赛事下 所有 赔率sole
  1324. $odds_sole = $opt['data'];
  1325. //数据源 赛事 id
  1326. $others_match_id = $opt['match_id'];
  1327. //数据源
  1328. $source = $opt['source'];
  1329. //更新赛事下所有 不在本次请求的赔率 状态
  1330. $ret= OddsModel::upOddsStatus($model,$others_match_id,'',$source,$odds_sole);
  1331. if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  1332. }
  1333. /*
  1334. * 写入联赛结果
  1335. */
  1336. public function league_result($opt,$gameName){
  1337. $game_code = $opt['game_code'];
  1338. //根据球类代码获取相关model
  1339. $model = commonFunction::getModels($game_code,1);
  1340. $data = $opt['data'];
  1341. //验证结果所属联赛
  1342. $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  1343. ->value('lg_id');
  1344. if($lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::LEAGUE_ERROR));
  1345. $lg_result_id = $model['model_league_result']::where(['lg_id'=>$lg_id,'game_name'=>$data['game_name']])
  1346. ->value('id');
  1347. $set_lg_result = [
  1348. 'lg_id'=>$lg_id,
  1349. 'game_name'=>$data['game_name'],
  1350. 'result'=>json_encode($data['result'],JSON_UNESCAPED_UNICODE),
  1351. 'status'=>$data['status'],
  1352. 'ctime'=> date('Y-m-d H:i:s'),
  1353. 'utime'=> date('Y-m-d H:i:s'),
  1354. ];
  1355. //联赛结果数据处理
  1356. if(!empty($lg_result_id)){
  1357. $ret = $model['model_league_result']::where(['id'=>$lg_result_id])
  1358. -> update($set_lg_result);
  1359. 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'));
  1360. }else{
  1361. $ret = $model['model_league_result']::insert($set_lg_result);
  1362. 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'));
  1363. }
  1364. }
  1365. /*
  1366. * 写入赛事结果
  1367. */
  1368. public function com_result($opt=[],$gameName='',$lg_id='',$match_id=''){
  1369. $game_code = $opt['game_code'];
  1370. //根据球类代码获取相关model
  1371. $model = commonFunction::getModels($game_code,1);
  1372. $data = $opt['data'];
  1373. //查询结果是否存在
  1374. $match_r_id = $model['model_result']::where(['match_id'=>$match_id])
  1375. ->value('id');
  1376. $set_match_r = [
  1377. "home_team"=>$data['home_team'],
  1378. "guest_team"=>$data['guest_team'],
  1379. "lg_id"=>$lg_id,
  1380. "home_rate"=> $data['home_rate'],
  1381. "guest_rate"=> $data['guest_rate'],
  1382. "home_score"=> $data['home_score'],
  1383. "guest_score"=> $data['guest_score'],
  1384. "all_goal"=> $data['all_goal'],
  1385. "status"=>$data['status'],
  1386. "first_score"=>$data['first_score'],
  1387. "last_score"=> $data['last_score'],
  1388. "match_score"=> $data['match_score'],
  1389. "match_winer"=> $data['match_winer'],
  1390. "match_time"=> $data['match_time'],
  1391. "match_process"=> $data['match_process'],
  1392. "tag"=> $data['tag'],
  1393. "match_id"=> $match_id,
  1394. "u_home_score"=> $data['u_home_score'],
  1395. "u_guest_score"=> $data['u_guest_score'],
  1396. "p_code"=> $data['p_code'],
  1397. "update_time"=>date('Y-m-d H:i:s')
  1398. ];
  1399. //赛事结果数据处理
  1400. if(!empty($match_r_id)){
  1401. $ret = $model['model_result']::where(['id'=>$match_r_id])
  1402. -> update($set_match_r);
  1403. 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'));
  1404. }else{
  1405. $ret = $model['model_result']::insert($set_match_r);
  1406. 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'));
  1407. }
  1408. // return Response::success();
  1409. }
  1410. //写入赛事结果记录
  1411. public function com_result_record($opt=[],$gameName='',$lg_id='',$match_id=''){
  1412. $game_code = $opt['game_code'];
  1413. //根据球类代码获取相关model
  1414. $model = commonFunction::getModels($game_code,1);
  1415. $data = $opt['data'];
  1416. $match_r_id = $model['model_result_record']::where(['match_id'=>$match_id,'match_time'=>$data['match_time']])
  1417. ->value('id');
  1418. //根据球类 获取 赛事结果记录字段
  1419. $set_match_r = $this->get_match_r($game_code,$lg_id,$match_id,$data);
  1420. //赛事结果记录处理
  1421. if($match_r_id > 0){
  1422. $ret = $model['model_result_record']::where(['id'=>$match_r_id])
  1423. ->update($set_match_r);
  1424. 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'));
  1425. }else{
  1426. $ret = $model['model_result_record']::insert($set_match_r);
  1427. 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'));
  1428. }
  1429. }
  1430. /**
  1431. * @param $data
  1432. * @return mixed
  1433. * json转数组
  1434. */
  1435. public function getAddData($data){
  1436. $data = json_decode($data,true);
  1437. return $data;
  1438. }
  1439. /*
  1440. * 根据球类获取 赛事结果记录字段
  1441. */
  1442. public function get_match_r($game_code,$lg_id,$match_id,$data){
  1443. $set_match_r = [];
  1444. if($game_code == 'zq'){
  1445. $set_match_r = [
  1446. "home_team"=>$data['home_team']?:'',
  1447. "guest_team"=>$data['guest_team']?:'',
  1448. "lg_id"=>$lg_id,
  1449. "all_goal"=>$data['all_goal']?:0,
  1450. "home_rate"=> $data['home_rate']?:0,
  1451. "guest_rate"=> $data['guest_rate']?:0,
  1452. "home_score"=> $data['home_score']?:0,
  1453. "guest_score"=> $data['guest_score']?:0,
  1454. "status"=>$data['status']?:0,
  1455. "first_score"=>$data['first_score']?:0,
  1456. "last_score"=> $data['last_score']?:0,
  1457. "match_score"=> $data['match_score']?:0,
  1458. "match_winer"=> $data['match_winer']?:'',
  1459. "match_time"=> $data['match_time']?:0,
  1460. "match_process"=> $data['match_process']?:'',
  1461. // "tag"=> $data['tag']?:0,
  1462. "match_id"=> $match_id,
  1463. "update_time"=>date('Y-m-d H:i:s')
  1464. ];
  1465. };
  1466. if($game_code == 'lq'){
  1467. $set_match_r = [
  1468. "home_team"=>$data['home_team']?:'',
  1469. "guest_team"=>$data['guest_team']?:'',
  1470. "lg_id"=>$lg_id,
  1471. "home_rate"=> $data['home_rate']?:0,
  1472. "guest_rate"=> $data['guest_rate']?:0,
  1473. "home_score"=> $data['home_score']?:0,
  1474. "guest_score"=> $data['guest_score']?:0,
  1475. "status"=>$data['status']?:0,
  1476. "first_score"=>$data['first_score']?:0,
  1477. "last_score"=> $data['last_score']?:0,
  1478. "match_score"=> $data['match_score']?:0,
  1479. "match_winer"=> $data['match_winer']?:'',
  1480. "match_time"=> $data['match_time']?:0,
  1481. "match_process"=> $data['match_process']?:'',
  1482. // "tag"=> $data['tag']?:0,
  1483. "match_id"=> $match_id,
  1484. "update_time"=>date('Y-m-d H:i:s')
  1485. ];
  1486. }
  1487. if($game_code == 'wq'){
  1488. $set_match_r = [
  1489. "home_player_name"=>$data['home_player_name']?:'',
  1490. "guest_player_name"=>$data['guest_player_name']?:'',
  1491. "lg_id"=>$lg_id,
  1492. "home_player_let_plate"=>$data['home_player_let_plate']?:0,
  1493. "guest_player_let_plate"=>$data['guest_player_let_plate']?:0,
  1494. "home_player_let_inning"=>$data['home_player_let_inning']?:0,
  1495. "guest_player_let_inning"=>$data['guest_player_let_inning']?:0,
  1496. "all_inning"=>$data['all_inning']?:0,
  1497. "home_player_score"=>$data['home_player_score']?:0,
  1498. "guest_player_score"=>$data['guest_player_score']?:0,
  1499. "status"=>$data['status']?:0,
  1500. "first_score_player"=>$data['first_score_player']?:'',
  1501. "last_score_player"=>$data['last_score_player']?:'',
  1502. "first_inning_score"=>$data['first_inning_score']?:0,
  1503. "second_inning_score"=>$data['second_inning_score']?:0,
  1504. "third_inning_score"=>$data['third_inning_score']?:0,
  1505. "match_winer_player"=>$data['match_winer_player']?:'',
  1506. "update_time"=>date('Y-m-d H:i:s'),
  1507. "match_time"=>$data['match_time']?:0,
  1508. "match_process"=>$data['match_process']?:'',
  1509. // "tag"=>$data['tag']?:0,
  1510. "match_id"=>$match_id,
  1511. "result_mark" =>$data['result_mark']?:'',
  1512. ];
  1513. }
  1514. if($game_code == 'bq'){
  1515. $set_match_r = [
  1516. "home_team"=>$data['home_team']?:'',
  1517. "guest_team"=>$data['guest_team']?:'',
  1518. "lg_id"=>$lg_id,
  1519. "home_rate"=> $data['home_rate']?:0,
  1520. "guest_rate"=> $data['guest_rate']?:0,
  1521. "home_score"=> $data['home_score']?:0,
  1522. "guest_score"=> $data['guest_score']?:0,
  1523. "status"=>$data['status']?:0,
  1524. "first_score"=>$data['first_score']?:0,
  1525. "last_score"=> $data['last_score']?:0,
  1526. "match_score"=> $data['match_score']?:0,
  1527. "match_winer"=> $data['match_winer']?:'',
  1528. "match_time"=> $data['match_time']?:0,
  1529. "match_process"=> $data['match_process']?:'',
  1530. // "tag"=> $data['tag']?:0,
  1531. "match_id"=> $match_id,
  1532. "all_inning"=>$data['all_inning']?:9,
  1533. "update_time"=>date('Y-m-d H:i:s'),
  1534. "result_mark" =>$data['result_mark']?:'',
  1535. ];
  1536. }
  1537. return $set_match_r;
  1538. }
  1539. }