WriteSportsController.php 82 KB

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