WriteSportsController.php 82 KB

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