WriteSportsController.php 80 KB

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