WriteSportsController.php 80 KB

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