WriteSportsController.php 79 KB

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