WriteSportsController.php 80 KB

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