WriteSportsController.php 81 KB

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