WriteSportsController.php 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703
  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. //拼装 待写入 赔率数据
  606. $ret_status_odds = [];
  607. $ret_status_odds_r = [];
  608. foreach($oddsData as $k=>$v){
  609. //组装数据
  610. $set_odds = [
  611. 'match_id'=> $match_id?:0,
  612. 'others_match_id'=> $v['match_id']?:0,
  613. 'odds_code'=> $v['odds_code']?:'',
  614. 'status'=> $v['status']?:0,
  615. 'sort'=> $v['sort']?:0,
  616. 'p_code'=> $v['p_code']?:'',
  617. 'odds'=> $v['odds']?:0,
  618. 'condition'=> $v['condition'],
  619. 'odds_only'=> $v['odds_only']?:'',
  620. 'source'=> $v['source']?:'',
  621. 'type'=> $v['type']?:0,
  622. 'team'=> $v['team']?:'',
  623. 'lg_id'=> $lg_id,
  624. 'others_lg_id'=> $v['lg_id'],
  625. 'ctime'=> date('Y-m-d H:i:s'),
  626. 'utime'=> date('Y-m-d H:i:s'),
  627. ];
  628. //写赔率记录数据
  629. // $ret = $models['model_odds_record']->updateOrCreate(
  630. // ['odds_only'=>$v['odds_only']],$set_odds
  631. // );
  632. // if($ret->id < 1) {
  633. // $ret_status_odds_r[] = 0;
  634. // }else{
  635. // $ret_status_odds_r[] = 1;
  636. // }
  637. // if($ret->id < 1) throw new \Exception(Response::generate($gameName.'赔率记录-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_R_ERROR));
  638. $ret = $models['model_odds_record']::insert($set_odds);
  639. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率记录-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_R_ERROR));
  640. //追加参数
  641. $set_odds['sole'] = $v['sole'];
  642. $set_odds['expire_time'] = date('Y-m-d H:i:s',time()+60);
  643. //写赔率 数据
  644. // $ret = $models['model_odds']->updateOrCreate(
  645. // ['odds_only'=>$v['odds_only']],$set_odds
  646. // );
  647. // if($ret->id > 0) {
  648. // $ret_status_odds[] = 1;
  649. // }
  650. // if($ret->id < 1) throw new \Exception(Response::generate($gameName.'赔率-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_ERROR));
  651. $ret = $models['model_odds']::insert($set_odds);
  652. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_ERROR));
  653. }
  654. // dd($ret_status_odds);
  655. // if(in_array(0,$ret_status_odds_r) == true) throw new \Exception(Response::generate('',Response::ADD_ODDS_R_ERROR));
  656. // if(count($ret_status_odds) != count($oddsData)) throw new \Exception(Response::generate('',Response::ADD_ODDS_ERROR));
  657. }
  658. //提交事务
  659. DB::commit();
  660. return Response::success();
  661. } catch (\Exception $e) {
  662. //回滚事务
  663. DB::rollBack();
  664. return $e->getMessage();
  665. }
  666. }
  667. /*
  668. *写 冠军联赛 赔率 数据
  669. *每个请求 包含 N个联赛 下 N条赔率
  670. */
  671. public function setOddsCH(Req $data){
  672. //验证token
  673. $user = new \App\Models\System_user;
  674. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  675. $token = $user::where(['token'=>$data->token])->value('token');
  676. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  677. try {
  678. //开启事务
  679. DB::beginTransaction();
  680. $obt = $data->data;
  681. //请求 数据 为空
  682. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  683. $getData = $this->getAddData($obt);
  684. //不是 冠军 赔率 数据
  685. if($getData['title'] != 'odds_ch') throw new \Exception(Response::generate('不是冠军赔率数据,',Response::ABNORMAL));
  686. //写请求数据 日志记录
  687. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt);
  688. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  689. //获取球类代码
  690. $game_code = $getData['game_code'];
  691. //获取数据源
  692. $source = $getData['source'];
  693. //获取球类名称
  694. $gameName = gameModel::getGameName($game_code);
  695. //获取 model
  696. $models = commonFunction::getModels($game_code, 1);
  697. //获取 赔率数据
  698. $oddsData = $getData['data'];
  699. //获取 当前 所有 可用 赔率 sole
  700. // $odds_only = $getData['odds_only'];
  701. //====验证 请求里 所有赔率 所属联赛 是否存在 ====
  702. //获取 当前请求 所有 联赛 uuid
  703. $uuids = [];
  704. foreach($oddsData as $k=>$v){
  705. $uuids[] = $v['uuid'];
  706. }
  707. $uuids = array_unique($uuids);
  708. sort($uuids);
  709. //更新赛事下所有 不在本次请求的赔率 状态
  710. // $ret= OddsModel::upOddsStatus($models,'',$s_lg_ids,$source,$odds_only);
  711. // if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  712. //获取 本地 已存在 联赛
  713. $l_lg_data = $models['model_local_league']::whereIn('identity',$uuids)->select('identity','lg_id')->get()->toArray();
  714. //二维数组去重
  715. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  716. //循环对比 请求uuid->本地uuid
  717. foreach($l_lg_data as $k=>$v){
  718. foreach($uuids as $kk=> $uuid){
  719. if($v['identity'] == $uuid){
  720. unset($uuids[$kk]);
  721. }
  722. }
  723. }
  724. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常;
  725. if(!empty($uuids)) throw new \Exception(Response::generate($gameName.'冠军联赛:uuid-'.$uuids[0].';',Response::LEAGUE_ERROR));
  726. //====end====
  727. //拼装 待写入 赔率数据
  728. foreach($oddsData as $k=>$v){
  729. //获取 本地 联赛 ID
  730. $lg_id = commonFunction::searcharray($data['uuid'],'identity',$l_lg_data,'lg_id');
  731. //组装数据
  732. $set_odds = [
  733. 'match_id'=>0,
  734. 'others_match_id'=> $v['match_id']?:0,
  735. 'odds_code'=> $v['odds_code']?:'',
  736. 'status'=> $v['status']?:0,
  737. 'sort'=> $v['sort']?:0,
  738. 'p_code'=> $v['p_code']?:'',
  739. 'odds'=> $v['odds']?:0,
  740. 'condition'=> $v['condition'],
  741. 'odds_only'=> $v['odds_only']?:'',
  742. 'source'=> $v['source']?:'',
  743. 'type'=> $v['type']?:1,
  744. 'team'=> $v['team']?:'',
  745. 'lg_id'=> $lg_id,
  746. 'others_lg_id'=> $v['lg_id'],
  747. 'ctime'=> date('Y-m-d H:i:s'),
  748. 'utime'=> date('Y-m-d H:i:s'),
  749. ];
  750. //写赔率记录数据
  751. $ret = $models['model_odds_record']->updateOrCreate(
  752. ['odds_only'=>$v['odds_only']],$set_odds
  753. );
  754. if(empty($ret)) throw new \Exception(Response::generate($gameName.'赔率记录-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_R_ERROR));
  755. //追加参数
  756. $set_odds['sole'] = $v['sole'];
  757. $set_odds['expire_time'] = date('Y-m-d H:i:s',time()+60);
  758. //写赔率 数据
  759. $ret = $models['model_odds']->updateOrCreate(
  760. ['odds_only'=>$v['odds_only']],$set_odds
  761. );
  762. if(empty($ret)) throw new \Exception(Response::generate($gameName.'赔率-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_ERROR));
  763. }
  764. //提交事务
  765. DB::commit();
  766. return Response::success();
  767. } catch (\Exception $e) {
  768. //回滚事务
  769. DB::rollBack();
  770. return $e->getMessage();
  771. }
  772. }
  773. /*
  774. * 写入直播 数据
  775. */
  776. public function setBroadCast(Req $data){
  777. //验证token
  778. $user = new \App\Models\System_user;
  779. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  780. $token = $user::where(['token'=>$data->token])->value('token');
  781. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  782. try {
  783. //开启事务
  784. DB::beginTransaction();
  785. $obt = $data->data;
  786. //请求 数据 为空
  787. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  788. $getData = $this->getAddData($obt);
  789. //不是 直播 数据
  790. if($getData['title'] != 'broad_cast') throw new \Exception(Response::generate('不是直播数据,',Response::ABNORMAL));
  791. //写请求数据 日志记录
  792. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt);
  793. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  794. //获取 球类
  795. $game_code = $getData['game_code'];
  796. //获取 直播 数据
  797. $BroadCast = $getData['data'];
  798. $set_broadcast = [];
  799. foreach($BroadCast as $k=>$data){
  800. //组装数据
  801. $set_broadcast[] = [
  802. "doing" => $data['doing'],
  803. "game_type" => $data['game_type'],
  804. "game_code" => $game_code,
  805. "guest_team" => $data['guest_team'],
  806. "host_team" => $data['host_team'],
  807. "league_name" => $data['league_name'],
  808. "shower" => $data['shower'],
  809. "showid" => (int)$data['showid'],
  810. "start_time" => $data['start_time'],
  811. "ctime" =>date('Y-m-d H:i:s'),
  812. "utime" =>date('Y-m-d H:i:s'),
  813. ];
  814. }
  815. //写入 直播 数据
  816. $ret = broadcastModel::insert($set_broadcast);
  817. if($ret == false) throw new \Exception(Response::generate('',Response::BROADCAST_ERROR));
  818. //提交事务
  819. DB::commit();
  820. return Response::success();
  821. } catch (\Exception $e) {
  822. //回滚事务
  823. DB::rollBack();
  824. return $e->getMessage();
  825. }
  826. }
  827. /*
  828. * 验证所属 联赛 是否存在
  829. */
  830. public function leagueVerify($models=[],$uuid='',$source='',$gameName=''){
  831. if(empty($models) || empty($uuid)) throw new \Exception(Response::generate('',Response::ABNORMAL));
  832. $l_lg_id = $models['model_local_league']::where(['identity'=>$uuid])->value('lg_id');
  833. if($l_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛:lg_id-'.$uuid,Response::LEAGUE_ERROR));
  834. return $l_lg_id;
  835. }
  836. /*
  837. * 验证所属 赛事 是否存在
  838. */
  839. public function matchVerify($models=[],$s_match_id='',$source='',$gameName=''){
  840. if(empty($models) || empty($s_match_id)) throw new \Exception(Response::generate('',Response::ABNORMAL));
  841. $l_match_id = $models['model_local_match']::where(['others_match_id'=>$s_match_id,'source'=>$source])->value('match_id');
  842. if($l_match_id < 1) throw new \Exception(Response::generate($gameName.'赛事:match_id-'.$s_match_id,Response::MATCH_ERROR));
  843. return $l_match_id;
  844. }
  845. /*
  846. * 写入数据 接口
  847. */
  848. public function setSports(Req $data){
  849. try {
  850. //开启事务
  851. DB::beginTransaction();
  852. if($data->game_code){
  853. //用于后台 将所有进行中的赛事写入结果
  854. $this->match_result($data->game_code);
  855. }else{
  856. //验证token
  857. $dd = $data->session()->get('adminInfo');
  858. if(empty($data->token) ||($dd['token'] != $data->token)) return Response::generate('',Response::TOKEN_ERR);
  859. //写赛事数据
  860. $obt = $data->data;
  861. if($obt){
  862. $getData = $this->getAddData($obt);
  863. $league = [];
  864. $competition = [];
  865. $odds = [];
  866. $odds_soly = [];
  867. $league_result = [];
  868. $match_result = [];
  869. $match_result_record = [];
  870. $odds_record = [];
  871. $broadcast = [];
  872. //指定排序 联赛->赛事->赔率->联赛结果->赛事结果->赛事结果记录->赔率记录->直播数据
  873. foreach ($getData as $k=>$v){
  874. $game_code = $v['game_code'];
  875. if($v['title'] == 'league') $league[] = $v;
  876. if($v['title'] == 'competition') $competition[] = $v;
  877. if($v['title'] == 'odds') $odds[] = $v;
  878. if($v['title'] == 'odds_sole') $odds_soly[] = $v;
  879. if($v['title'] == 'league_result') $league_result[] = $v;
  880. if($v['title'] == 'match_result') $match_result[] = $v;
  881. if($v['title'] == 'match_result_record') $match_result_record[] = $v;
  882. if($v['title'] == 'odds_record') $odds_record[] = $v;
  883. if($v['title'] == 'broadcast') $broadcast[] = $v;
  884. }
  885. $matchData = [$league,$competition,$odds_soly,$odds,$league_result,$match_result,$match_result_record,$odds_record,$broadcast];
  886. //排空处理
  887. foreach ($matchData as $k=>$v){
  888. if($v == []) unset($matchData[$k]);
  889. }
  890. sort($matchData);
  891. //根据顺序写入数据
  892. //降维数据
  893. $mentData = [];
  894. foreach ($matchData as $k=>$v){
  895. foreach ($v as $kk=>$vv){
  896. $mentData[] = $vv;
  897. }
  898. }
  899. //获取各球类model
  900. $models = commonFunction::getModels($game_code,1);
  901. //获取球类名称
  902. $gameName = gameModel::getGameName($game_code);
  903. //===获取验证数据===
  904. //获取一段时间内所有联赛本地记录
  905. $lg_data = LeagueModel::getLeagueID($models);
  906. //获取一段时间内所有赛事本地记录
  907. $match_data = MatchModel::getMatchID($models);
  908. //获取一段时间内所有赔率
  909. $odds_data = OddsModel::getOddsID($models);
  910. //获取一段时间内所有赔率记录
  911. $odds_record_data = OddsRecordModel::getOddsRecordID($models);
  912. //===获取验证数据===
  913. $others_lg_id = '';//继承 源联赛id
  914. $others_match_id = '';//继承 源赛事id
  915. $s_lg_id = '';//源 联赛ID
  916. $l_lg_id = '';//本地 联赛 ID
  917. $s_match_id = '';//源 赛事ID
  918. $l_match_id = '';//本地 赛事 ID
  919. foreach ($mentData as $kk =>$vv){
  920. switch ($vv['title']){
  921. case 'area'://地区
  922. throw new \Exception(Response::generate('地区数据-area:',Response::AUTH_ERROR)) ;
  923. break;
  924. case 'country'://国家
  925. throw new \Exception(Response::generate('国家数据-country:',Response::AUTH_ERROR));
  926. break;
  927. case 'league'://联赛
  928. $others_lg_id = $vv['data']['lg_id'];
  929. $s_lg_id = $vv['data']['lg_id'];
  930. $source = $vv['data']['source'];
  931. //验证联赛记录是否已存在
  932. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  933. if($ret_lg != false) {
  934. $l_lg_id = $ret_lg;
  935. break;
  936. }
  937. //执行 联赛数据
  938. $lg_data = $this->league($vv,$gameName);
  939. $l_lg_id = $lg_data['lg_id'];
  940. break;
  941. case 'competition'://赛事
  942. $others_match_id = $vv['data']['match_id'];
  943. $s_lg_id = $vv['data']['lg_id'];
  944. $s_match_id = $vv['data']['match_id'];
  945. $source = $vv['data']['source'];
  946. //本次请求是否包含 联赛数据
  947. if(empty($others_lg_id)){
  948. //验证本地是否存在
  949. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  950. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  951. $l_lg_id = $ret_lg;
  952. }else{
  953. //本次有 联赛数据 验证属于同联赛
  954. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  955. }
  956. //验证赛事记录是否已存在
  957. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  958. if($ret_match != false){
  959. $l_match_id = $ret_match;
  960. break;
  961. }
  962. //执行 赛事数据
  963. $match_data = $this->competition($vv,$gameName,$l_lg_id);
  964. $l_match_id = $match_data['match_id'];
  965. break;
  966. case 'odds'://赔率
  967. $s_lg_id = $vv['data']['lg_id'];
  968. $s_match_id = $vv['data']['match_id'];
  969. $source = $vv['data']['source'];
  970. $odds_only = $vv['data']['odds_only'];
  971. $sole = $vv['data']['sole'];
  972. $odds_type = $vv['data']['type'];//0普通 1冠军盘口
  973. //本次请求是否包含 联赛数据
  974. if(empty($others_lg_id)){
  975. //无联赛数据 验证本地是否存在
  976. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  977. //联赛 不存在
  978. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  979. $l_lg_id = $ret_lg;
  980. }else{
  981. //有联赛数据 验证属于同请求联赛
  982. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  983. }
  984. //如果是普通 赔率,则验证所属赛事
  985. if($odds_type == 0){
  986. //验证赛事记录是否已存在
  987. if(empty($others_match_id)){
  988. //无赛事数据 验证本地是否存在
  989. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  990. //赛事 不存在
  991. if($ret_match == false) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  992. $l_match_id = $ret_match;
  993. }else{
  994. //有赛事数据 验证属于同请求赛事
  995. if($s_match_id != $others_match_id) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  996. }
  997. }
  998. //验证赔率 是否存在
  999. $ret_odds = '';
  1000. $ret_odds = commonFunction::ver_odds($sole,$source,$odds_data);
  1001. //验证赔率记录 是否存在
  1002. $ret_odds_record = '';
  1003. $ret_odds_record = commonFunction::ver_odds_record($odds_only,$source,$odds_record_data);
  1004. $this->odds($vv,$gameName,$l_lg_id,$l_match_id,$ret_odds,$ret_odds_record);
  1005. break;
  1006. case 'odds_sole':
  1007. $this->upOddsStatus($vv,$gameName);
  1008. break;
  1009. case 'league_result'://联赛结果
  1010. $this->league_result($vv,$gameName);
  1011. break;
  1012. case 'match_result'://赛事结果
  1013. throw new \Exception(Response::generate('赛事结果数据-match_result:',Response::AUTH_ERROR));
  1014. $this->match_result($vv,$gameName);
  1015. break;
  1016. case 'match_result_record'://赛事结果记录
  1017. $s_lg_id = $vv['data']['lg_id'];
  1018. $s_match_id = $vv['data']['match_id'];
  1019. $source = $vv['data']['source'];
  1020. //如果本次请求没有 联赛数据
  1021. if(empty($others_lg_id)){
  1022. //验证本地是否存在
  1023. $ret_lg = commonFunction::ver_league($s_lg_id,$source,$lg_data);
  1024. //联赛 不存在
  1025. if($ret_lg == false) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1026. $l_lg_id = $ret_lg;
  1027. }else{
  1028. //有联赛数据 验证属于同请求联赛
  1029. if($s_lg_id != $others_lg_id) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$s_lg_id.';',Response::LEAGUE_ERROR));
  1030. }
  1031. //验证赛事记录是否已存在
  1032. if(empty($others_match_id)){
  1033. //无赛事数据 验证本地是否存在
  1034. $ret_match = commonFunction::ver_match($s_match_id,$source,$match_data);
  1035. //联赛 不存在
  1036. if($ret_match == false) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  1037. $l_match_id = $ret_match;
  1038. }else{
  1039. //有赛事数据 验证属于同请求赛事
  1040. if($s_match_id != $others_match_id) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::MATCH_ERROR));
  1041. }
  1042. //处理 赛事结果 记录
  1043. $this->com_result_record($vv,$gameName,$l_lg_id,$l_match_id);
  1044. break;
  1045. case 'broadcast'://直播数据
  1046. $this->broadcast($vv,$game_code);
  1047. break;
  1048. default:
  1049. throw new \Exception(Response::generate('',Response::ABNORMAL)) ;
  1050. }
  1051. }
  1052. }
  1053. }
  1054. //提交事务
  1055. DB::commit();
  1056. return Response::success();
  1057. } catch (\Exception $e) {
  1058. //回滚事务
  1059. DB::rollBack();
  1060. return $e->getMessage();
  1061. }
  1062. }
  1063. /**
  1064. * @param Req $data
  1065. * @return string
  1066. * @throws \App\Lib\Biz\Sport\Exception
  1067. * 更新赛事状态
  1068. */
  1069. public function upMatch(Req $data){
  1070. //验证token
  1071. $user = new \App\Models\System_user;
  1072. if(empty($data->token)) return Response::generate('未获取到token',Response::TOKEN_ERR);
  1073. $token = $user::where(['token'=>$data->token])->value('token');
  1074. if(empty($token)) return Response::generate('token验证不通过',Response::TOKEN_ERR);
  1075. try {
  1076. //开启事务
  1077. DB::beginTransaction();
  1078. //获取待更新赛事
  1079. $obt = $data->data;
  1080. if($obt){
  1081. //json转数组
  1082. $data = $this->getAddData($obt);
  1083. //获取 球类代码
  1084. $game_code = $data['game_code'];
  1085. //获取 数据源
  1086. $source = $data['source'];
  1087. //获取所有数据源赛事 match_id
  1088. $others_match_ids = [];
  1089. foreach ($data['data'] as $k=>$v){
  1090. $others_match_ids[] = $v['match_id'];
  1091. }
  1092. //根据球类代码 获取model
  1093. $model =commonFunction::getModels($game_code,1);
  1094. //获取所有赛事 match_id
  1095. $local_match = $model['model_local_match']::SELECT('others_match_id','match_id')
  1096. ->where(['source'=>$source])
  1097. ->whereIn('others_match_id',$others_match_ids)
  1098. ->get()->toArray();
  1099. if(empty($local_match)) throw new \Exception(Response::generate('',Response::MATCHID_NULL));
  1100. //更新状态字段
  1101. foreach ($local_match as $k=>$v){
  1102. $set_status = [
  1103. 'status'=>$v['status'],
  1104. 'is_rollball'=>$v['is_rollball'],
  1105. 'stais_todaytus'=>$v['is_today'],
  1106. 'is_morningplate'=>$v['is_morningplate'],
  1107. 'is_stringscene'=>$v['is_stringscene'],
  1108. 'is_horn'=>$v['is_horn'],
  1109. ];
  1110. $ret = $model['model_match']::where(['id'=>$v['match_id']])
  1111. -> update($set_status);
  1112. if($ret<1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$v['others_match_id'],Response::UPSTATUS_ERROR));
  1113. }
  1114. }
  1115. //提交事务
  1116. DB::commit();
  1117. return Response::success();
  1118. } catch (\Exception $e) {
  1119. //回滚事务
  1120. DB::rollBack();
  1121. return $e->getMessage();
  1122. }
  1123. }
  1124. /*
  1125. * 写入直播 数据
  1126. */
  1127. public function broadcast($opt = [],$game_code=''){
  1128. $data = $opt['data'];
  1129. $set_broadcast = [
  1130. "doing" => $data['doing'],
  1131. "game_type" => $data['game_type'],
  1132. "game_code" => $game_code,
  1133. "guest_team" => $data['guest_team'],
  1134. "host_team" => $data['host_team'],
  1135. "league_name" => $data['league_name'],
  1136. "shower" => $data['shower'],
  1137. "showid" => (int)$data['showid'],
  1138. "start_time" => $data['start_time'],
  1139. "ctime" =>date('Y-m-d H:i:s'),
  1140. "utime" =>date('Y-m-d H:i:s'),
  1141. ];
  1142. $ret = broadcastModel::insert($set_broadcast);
  1143. if($ret == false) throw new \Exception(Response::generate('',Response::BROADCAST_ERROR));
  1144. }
  1145. /*
  1146. * 写入 赛事 初始 结果
  1147. */
  1148. public function match_result($game_code = ''){
  1149. $model =commonFunction::getModels($game_code,1);
  1150. if($game_code == 'zq') ZqResultModel::ZQresult($model);
  1151. if($game_code == 'lq') LqResultModel::LQresult($model);
  1152. if($game_code == 'wq') WqResultModel::WQresult($model);
  1153. if($game_code == 'bq') BqResultModel::BQresult($model);
  1154. }
  1155. /*
  1156. * 写入联赛数据
  1157. */
  1158. public function league($opt = [],$gameName=''){
  1159. $game_code = $opt['game_code'];
  1160. //根据球类代码获取相关model
  1161. $model =commonFunction::getModels($game_code,1);
  1162. $data = $opt['data'];
  1163. //查询联赛是否已存在
  1164. $id = $model['model_league']::where('name_chinese','=',$data['name_chinese'])
  1165. ->value('id');
  1166. //默认获取本年最后一天
  1167. $last_time = date('Y-12-31 23:59:59');
  1168. //决赛时间
  1169. if($data['last_time']){
  1170. $last_time = $data['last_time'];
  1171. }
  1172. $set_lg['name_chinese'] = $data['name_chinese'];
  1173. $set_lg['kind'] = $data['kind'];
  1174. $set_lg['match_mode'] = $data['match_mode'];
  1175. $set_lg['if_stop'] = $data['if_stop'];
  1176. $set_lg['identity'] = $data['uuid']?:'';
  1177. $set_lg['last_time'] = $last_time;
  1178. $set_lg['utime'] = date('Y-m-d H:i:s');
  1179. if(empty($id)){
  1180. //写入联赛
  1181. $id = $model['model_league']::insertGetId($set_lg);
  1182. $m_lg_id = $id;
  1183. if($m_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::INSERT_ERROR));
  1184. }else{
  1185. //更新联赛
  1186. $ret = $model['model_league']::where(['id'=>$id])
  1187. -> update($set_lg);
  1188. if($ret < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::UPDATE_ERROR));
  1189. }
  1190. $set_local = [
  1191. 'lg_id'=>$id,
  1192. 'others_lg_id'=>$data['lg_id'],
  1193. 'source'=>$data['source'],
  1194. 'ctime'=>date('Y-m-d H:i:s')
  1195. ];
  1196. $ret = $model['model_local_league']::insertGetId($set_local);
  1197. if($ret < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::LOCAL_LEAGUE_ERROR)) ;//Render([], '10017', lang('Tips','Sports')->get('local_league_error'));
  1198. //执行成功返回源数据 联赛ID
  1199. $data = ['others_lg_id'=>$data['lg_id'],'lg_id'=>$id];
  1200. return $data;
  1201. }
  1202. /*
  1203. * 写入赛事数据
  1204. */
  1205. public function competition($opt = [],$gameName='',$lg_id=''){
  1206. $game_code = $opt['game_code'];
  1207. //根据球类代码获取相关model
  1208. $model = commonFunction::getModels($game_code,1);
  1209. $data = $opt['data'];
  1210. if(empty($data['lg_id'])) throw new \Exception(Response::generate('',Response::LEAGUE_ERROR)) ;//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  1211. //查询赛事是否存在
  1212. $id = $model['model_match']::where(['home_team'=>$data['home_team'],'guest_team'=>$data['guest_team'],'match_date'=>$data['match_date'],'match_time'=>$data['match_time']])
  1213. ->value('id');
  1214. $half_match_id = 0;
  1215. //如果有上半场赛事id 获取上半场赛事是否存在
  1216. if(!empty($data['half_match_id'])){
  1217. $half_match_id = $match_id = $model['model_local_match']::where(['others_match_id'=>$data['half_match_id'],'source'=>$data['source']])
  1218. ->value('match_id');
  1219. if(empty($half_match_id)) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::HALF_MATCH_ERROR)) ;//Render([], '10024', lang('Tips','Sports')->get('half_match_error'));
  1220. }
  1221. //如果赛事没有开始日期,则为冠军盘口赛事
  1222. if(empty($data['match_date'])){
  1223. //冠军盘口赛事获取所属联赛结束时间
  1224. $last_time = $model['model_league']::where(['id'=>$lg_id])
  1225. ->value('last_time');
  1226. if(empty($last_time)) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::LG_LASTTIME_ERROR)) ;//Render([], '10023', lang('Tips','Sports')->get('lg_lastTime_error'));
  1227. //给冠军盘口赛事 赋值时间
  1228. $time = strtotime($last_time);
  1229. $data['match_date'] = date('Y-m-d',$time);
  1230. $data['match_time'] = date('H:i:s',$time);
  1231. }
  1232. $set_match = [
  1233. 'ctime'=>date('Y-m-d H:i:s'),
  1234. 'utime'=>date('Y-m-d H:i:s'),
  1235. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  1236. 'home_team'=>$data['home_team']?:'',
  1237. 'guest_team'=>$data['guest_team']?:'no_team',
  1238. 'lg_id'=>$lg_id,
  1239. 'status'=>$data['status'],
  1240. 'match_date'=>$data['match_date']?:date('Y-m-d'),
  1241. 'match_time'=>$data['match_time']?:date('H:i:s'),
  1242. 'tag'=>$data['tag']?:0,
  1243. 'is_rollball'=>$data['is_rollball']?:0,
  1244. 'is_today'=>$data['is_today']?:0,
  1245. 'is_morningplate'=>$data['is_morningplate']?:0,
  1246. 'is_stringscene'=>$data['is_stringscene']?:0,
  1247. 'us_time'=>$data['us_time']?:commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  1248. 'half_match_id'=>$half_match_id?:0,
  1249. 'identity' => $data['uuid'],
  1250. ];
  1251. //写入赛事
  1252. if(empty($id)){
  1253. //写入赛事 返回id
  1254. $id = $model['model_match']::insertGetId($set_match);
  1255. if($id < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::INSERT_ERROR)) ;
  1256. }else{
  1257. //更新赛事
  1258. $ret = $model['model_match']::where(['id'=>$id])->update($set_match);
  1259. if($ret < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::UPDATE_ERROR)) ;
  1260. }
  1261. //写关联记录
  1262. $set_local = [
  1263. 'match_id'=>$id,
  1264. 'others_match_id'=>$data['match_id'],
  1265. 'source'=>$data['source'],
  1266. 'ctime'=>date('Y-m-d H:i:s')
  1267. ];
  1268. $ret = $model['model_local_match']::insertGetId($set_local);
  1269. if($ret < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::LOCAL_MATCH_ERROR)) ;//Render([], '10018', lang('Tips','Sports')->get('local_match_error'));
  1270. //返回 源数据 赛事ID
  1271. $data = ['others_match_id'=>$data['match_id'],'match_id'=>$id];
  1272. return $data;
  1273. }
  1274. /*
  1275. * 写入赔率/赔率记录数据
  1276. */
  1277. public function odds($opt=[],$gameName='',$lg_id=0,$match_id=0,$oddsID='',$oddsRecordID=''){
  1278. $game_code = $opt['game_code'];
  1279. //根据球类代码获取相关model
  1280. $model = commonFunction::getModels($game_code,1);
  1281. $data = $opt['data'];
  1282. //获取赛事 本地/源ID
  1283. $others_match_id = $data['match_id'];
  1284. //获取联赛 本地/源ID
  1285. $others_lg_id = $data['lg_id'];
  1286. //===写赔率记录===
  1287. $set_odds_r = [
  1288. 'match_id'=> $match_id?:0,
  1289. 'others_match_id'=> $others_match_id?:0,
  1290. 'odds_code'=> $data['odds_code']?:'',
  1291. 'status'=> $data['status']?:0,
  1292. 'sort'=> $data['sort']?:0,
  1293. 'p_code'=> $data['p_code']?:'',
  1294. 'odds'=> $data['odds']?:0,
  1295. 'condition'=> $data['condition'],
  1296. 'odds_only'=> $data['odds_only']?:'',
  1297. 'source'=> $data['source']?:'',
  1298. 'type'=> $data['type']?:0,
  1299. 'team'=> $data['team']?:'',
  1300. 'lg_id'=> $lg_id,
  1301. 'others_lg_id'=> $others_lg_id,
  1302. 'ctime'=> date('Y-m-d H:i:s'),
  1303. ];
  1304. //更新或写入赔率记录
  1305. if($oddsRecordID != false){
  1306. $ret = $model['model_odds_record']::where(['id'=>$oddsRecordID])
  1307. -> update($set_odds_r);
  1308. if($ret < 1) throw new \Exception(Response::generate($gameName.'赔率记录-odds_only:'.$data['odds_only'].';',Response::ADD_ODDS_R_ERROR)) ;//Render([], '10020', lang('Tips','Sports')->get('add_odds_r_error'));
  1309. }else{
  1310. $ret = $model['model_odds_record']::insert($set_odds_r);
  1311. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率记录-odds_only:'.$data['odds_only'].';',Response::ADD_ODDS_R_ERROR));//Render([], '10020', lang('Tips','Sports')->get('add_odds_r_error'));
  1312. }
  1313. //===写赔率===
  1314. $set_odds = [
  1315. 'match_id'=> $match_id?:0,
  1316. 'others_match_id'=> $others_match_id?:0,
  1317. 'odds_code'=> $data['odds_code']?:'',
  1318. 'status'=> $data['status']?:0,
  1319. 'sort'=> $data['sort']?:0,
  1320. 'p_code'=> $data['p_code']?:'',
  1321. 'odds'=> $data['odds']?:0,
  1322. 'condition'=> $data['condition'],
  1323. 'odds_only'=> $data['odds_only']?:'',
  1324. 'sole'=> $data['sole']?:'',
  1325. 'source'=> $data['source']?:'',
  1326. 'type'=> $data['type']?:0,
  1327. 'team'=> $data['team']?:'',
  1328. 'lg_id'=> $lg_id,
  1329. 'others_lg_id'=> $others_lg_id,
  1330. 'ctime'=> date('Y-m-d H:i:s'),
  1331. 'utime'=> date('Y-m-d H:i:s'),
  1332. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  1333. ];
  1334. //更新或写入赔率数据
  1335. if($oddsID != false){
  1336. $ret = $model['model_odds']::where(['id'=>$oddsID])
  1337. -> update($set_odds);
  1338. if($ret < 1) throw new \Exception(Response::generate($gameName.'赔率-odds_only:'.$data['odds_only'].';',Response::ADD_ODDS_ERROR));//Render([], '10019', lang('Tips','Sports')->get('add_odds_error'));
  1339. }else{
  1340. $ret = $model['model_odds']::insert($set_odds);
  1341. if($ret != true) throw new \Exception(Response::generate($gameName.'赔率-odds_only:'.$data['odds_only'].';',Response::ADD_ODDS_ERROR));//Render([], '10019', lang('Tips','Sports')->get('add_odds_error'));
  1342. }
  1343. //===end===
  1344. }
  1345. /*
  1346. * 更新 赛事下 所有不在本次请求的 赔率状态
  1347. */
  1348. public function upOddsStatus($opt = [],$gameName=''){
  1349. //获取球类代码
  1350. $game_code = $opt['game_code'];
  1351. //获取球类model
  1352. $model = commonFunction::getModels($game_code,1);
  1353. //赛事下 所有 赔率sole
  1354. $odds_sole = $opt['data'];
  1355. //数据源 赛事 id
  1356. $others_match_id = $opt['match_id'];
  1357. //数据源
  1358. $source = $opt['source'];
  1359. //更新赛事下所有 不在本次请求的赔率 状态
  1360. $ret= OddsModel::upOddsStatus($model,$others_match_id,'',$source,$odds_sole);
  1361. if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  1362. }
  1363. /*
  1364. * 写入联赛结果
  1365. */
  1366. public function league_result($opt,$gameName){
  1367. $game_code = $opt['game_code'];
  1368. //根据球类代码获取相关model
  1369. $model = commonFunction::getModels($game_code,1);
  1370. $data = $opt['data'];
  1371. //验证结果所属联赛
  1372. $lg_id = $model['model_local_league']::where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
  1373. ->value('lg_id');
  1374. if($lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::LEAGUE_ERROR));
  1375. $lg_result_id = $model['model_league_result']::where(['lg_id'=>$lg_id,'game_name'=>$data['game_name']])
  1376. ->value('id');
  1377. $set_lg_result = [
  1378. 'lg_id'=>$lg_id,
  1379. 'game_name'=>$data['game_name'],
  1380. 'result'=>json_encode($data['result'],JSON_UNESCAPED_UNICODE),
  1381. 'status'=>$data['status'],
  1382. 'ctime'=> date('Y-m-d H:i:s'),
  1383. 'utime'=> date('Y-m-d H:i:s'),
  1384. ];
  1385. //联赛结果数据处理
  1386. if(!empty($lg_result_id)){
  1387. $ret = $model['model_league_result']::where(['id'=>$lg_result_id])
  1388. -> update($set_lg_result);
  1389. if($ret < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::ADD_LG_R_ERROR)) ;//Render([], '10021', lang('Tips','Sports')->get('add_lg_r_error'));
  1390. }else{
  1391. $ret = $model['model_league_result']::insert($set_lg_result);
  1392. if($ret != true) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$data['lg_id'].';',Response::ADD_LG_R_ERROR));//Render([], '10021', lang('Tips','Sports')->get('add_lg_r_error'));
  1393. }
  1394. }
  1395. /*
  1396. * 写入赛事结果
  1397. */
  1398. public function com_result($opt=[],$gameName='',$lg_id='',$match_id=''){
  1399. $game_code = $opt['game_code'];
  1400. //根据球类代码获取相关model
  1401. $model = commonFunction::getModels($game_code,1);
  1402. $data = $opt['data'];
  1403. //查询结果是否存在
  1404. $match_r_id = $model['model_result']::where(['match_id'=>$match_id])
  1405. ->value('id');
  1406. $set_match_r = [
  1407. "home_team"=>$data['home_team'],
  1408. "guest_team"=>$data['guest_team'],
  1409. "lg_id"=>$lg_id,
  1410. "home_rate"=> $data['home_rate'],
  1411. "guest_rate"=> $data['guest_rate'],
  1412. "home_score"=> $data['home_score'],
  1413. "guest_score"=> $data['guest_score'],
  1414. "all_goal"=> $data['all_goal'],
  1415. "status"=>$data['status'],
  1416. "first_score"=>$data['first_score'],
  1417. "last_score"=> $data['last_score'],
  1418. "match_score"=> $data['match_score'],
  1419. "match_winer"=> $data['match_winer'],
  1420. "match_time"=> $data['match_time'],
  1421. "match_process"=> $data['match_process'],
  1422. "tag"=> $data['tag'],
  1423. "match_id"=> $match_id,
  1424. "u_home_score"=> $data['u_home_score'],
  1425. "u_guest_score"=> $data['u_guest_score'],
  1426. "p_code"=> $data['p_code'],
  1427. "update_time"=>date('Y-m-d H:i:s')
  1428. ];
  1429. //赛事结果数据处理
  1430. if(!empty($match_r_id)){
  1431. $ret = $model['model_result']::where(['id'=>$match_r_id])
  1432. -> update($set_match_r);
  1433. if($ret < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_ERROR)) ;//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
  1434. }else{
  1435. $ret = $model['model_result']::insert($set_match_r);
  1436. if($ret != true) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_ERROR)) ;//Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
  1437. }
  1438. // return Response::success();
  1439. }
  1440. //写入赛事结果记录
  1441. public function com_result_record($opt=[],$gameName='',$lg_id='',$match_id=''){
  1442. $game_code = $opt['game_code'];
  1443. //根据球类代码获取相关model
  1444. $model = commonFunction::getModels($game_code,1);
  1445. $data = $opt['data'];
  1446. $match_r_id = $model['model_result_record']::where(['match_id'=>$match_id,'match_time'=>$data['match_time']])
  1447. ->value('id');
  1448. //根据球类 获取 赛事结果记录字段
  1449. $set_match_r = $this->get_match_r($game_code,$lg_id,$match_id,$data);
  1450. //赛事结果记录处理
  1451. if($match_r_id > 0){
  1452. $ret = $model['model_result_record']::where(['id'=>$match_r_id])
  1453. ->update($set_match_r);
  1454. if($ret < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_r_error'));
  1455. }else{
  1456. $ret = $model['model_result_record']::insert($set_match_r);
  1457. if($ret != true) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_R_ERROR));//Render([], '10022', lang('Tips','Sports')->get('add_match_r_r_error'));
  1458. }
  1459. }
  1460. /**
  1461. * @param $data
  1462. * @return mixed
  1463. * json转数组
  1464. */
  1465. public function getAddData($data){
  1466. $data = json_decode($data,true);
  1467. return $data;
  1468. }
  1469. /*
  1470. * 根据球类获取 赛事结果记录字段
  1471. */
  1472. public function get_match_r($game_code,$lg_id,$match_id,$data){
  1473. $set_match_r = [];
  1474. if($game_code == 'zq'){
  1475. $set_match_r = [
  1476. "home_team"=>$data['home_team']?:'',
  1477. "guest_team"=>$data['guest_team']?:'',
  1478. "lg_id"=>$lg_id,
  1479. "all_goal"=>$data['all_goal']?:0,
  1480. "home_rate"=> $data['home_rate']?:0,
  1481. "guest_rate"=> $data['guest_rate']?:0,
  1482. "home_score"=> $data['home_score']?:0,
  1483. "guest_score"=> $data['guest_score']?:0,
  1484. "status"=>$data['status']?:0,
  1485. "first_score"=>$data['first_score']?:0,
  1486. "last_score"=> $data['last_score']?:0,
  1487. "match_score"=> $data['match_score']?:0,
  1488. "match_winer"=> $data['match_winer']?:'',
  1489. "match_time"=> $data['match_time']?:0,
  1490. "match_process"=> $data['match_process']?:'',
  1491. // "tag"=> $data['tag']?:0,
  1492. "match_id"=> $match_id,
  1493. "update_time"=>date('Y-m-d H:i:s')
  1494. ];
  1495. };
  1496. if($game_code == 'lq'){
  1497. $set_match_r = [
  1498. "home_team"=>$data['home_team']?:'',
  1499. "guest_team"=>$data['guest_team']?:'',
  1500. "lg_id"=>$lg_id,
  1501. "home_rate"=> $data['home_rate']?:0,
  1502. "guest_rate"=> $data['guest_rate']?:0,
  1503. "home_score"=> $data['home_score']?:0,
  1504. "guest_score"=> $data['guest_score']?:0,
  1505. "status"=>$data['status']?:0,
  1506. "first_score"=>$data['first_score']?:0,
  1507. "last_score"=> $data['last_score']?:0,
  1508. "match_score"=> $data['match_score']?:0,
  1509. "match_winer"=> $data['match_winer']?:'',
  1510. "match_time"=> $data['match_time']?:0,
  1511. "match_process"=> $data['match_process']?:'',
  1512. // "tag"=> $data['tag']?:0,
  1513. "match_id"=> $match_id,
  1514. "update_time"=>date('Y-m-d H:i:s')
  1515. ];
  1516. }
  1517. if($game_code == 'wq'){
  1518. $set_match_r = [
  1519. "home_player_name"=>$data['home_player_name']?:'',
  1520. "guest_player_name"=>$data['guest_player_name']?:'',
  1521. "lg_id"=>$lg_id,
  1522. "home_player_let_plate"=>$data['home_player_let_plate']?:0,
  1523. "guest_player_let_plate"=>$data['guest_player_let_plate']?:0,
  1524. "home_player_let_inning"=>$data['home_player_let_inning']?:0,
  1525. "guest_player_let_inning"=>$data['guest_player_let_inning']?:0,
  1526. "all_inning"=>$data['all_inning']?:0,
  1527. "home_player_score"=>$data['home_player_score']?:0,
  1528. "guest_player_score"=>$data['guest_player_score']?:0,
  1529. "status"=>$data['status']?:0,
  1530. "first_score_player"=>$data['first_score_player']?:'',
  1531. "last_score_player"=>$data['last_score_player']?:'',
  1532. "first_inning_score"=>$data['first_inning_score']?:0,
  1533. "second_inning_score"=>$data['second_inning_score']?:0,
  1534. "third_inning_score"=>$data['third_inning_score']?:0,
  1535. "match_winer_player"=>$data['match_winer_player']?:'',
  1536. "update_time"=>date('Y-m-d H:i:s'),
  1537. "match_time"=>$data['match_time']?:0,
  1538. "match_process"=>$data['match_process']?:'',
  1539. // "tag"=>$data['tag']?:0,
  1540. "match_id"=>$match_id,
  1541. "result_mark" =>$data['result_mark']?:'',
  1542. ];
  1543. }
  1544. if($game_code == 'bq'){
  1545. $set_match_r = [
  1546. "home_team"=>$data['home_team']?:'',
  1547. "guest_team"=>$data['guest_team']?:'',
  1548. "lg_id"=>$lg_id,
  1549. "home_rate"=> $data['home_rate']?:0,
  1550. "guest_rate"=> $data['guest_rate']?:0,
  1551. "home_score"=> $data['home_score']?:0,
  1552. "guest_score"=> $data['guest_score']?:0,
  1553. "status"=>$data['status']?:0,
  1554. "first_score"=>$data['first_score']?:0,
  1555. "last_score"=> $data['last_score']?:0,
  1556. "match_score"=> $data['match_score']?:0,
  1557. "match_winer"=> $data['match_winer']?:'',
  1558. "match_time"=> $data['match_time']?:0,
  1559. "match_process"=> $data['match_process']?:'',
  1560. // "tag"=> $data['tag']?:0,
  1561. "match_id"=> $match_id,
  1562. "all_inning"=>$data['all_inning']?:9,
  1563. "update_time"=>date('Y-m-d H:i:s'),
  1564. "result_mark" =>$data['result_mark']?:'',
  1565. ];
  1566. }
  1567. return $set_match_r;
  1568. }
  1569. }