WriteSportsController.php 79 KB

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