WriteSportsController.php 84 KB

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