WriteSportsController.php 77 KB

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