WriteSportsController.php 80 KB

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