WriteSportsController.php 77 KB

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