WriteSportsController.php 80 KB

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