DataLogic.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/26
  6. * Time: 17:10
  7. */
  8. namespace App\Logic;
  9. use datainf\pplus\Instance;
  10. use App\Http\Response\Response;
  11. use App\Lib\ModelBase;
  12. use Illuminate\Database\Capsule\Manager as 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. class DataLogic
  25. {
  26. use Instance;
  27. /*
  28. * 写 联赛 数据
  29. * 每个请求 包含 N个联赛
  30. */
  31. public function setLeague($data)
  32. {
  33. try {
  34. //开启事务
  35. DB::beginTransaction();
  36. $obt = $data['data'];
  37. //请求 数据 为空
  38. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  39. $getData = $this->getAddData($obt);
  40. //不是 联赛 数据
  41. if($getData['title'] != 'league') throw new \Exception(Response::generate('不是联赛数据,',Response::ABNORMAL));
  42. //获取球类代码
  43. $game_code = $getData['game_code'];
  44. //获取数据源
  45. $source = $getData['source'];
  46. //获取球类名称
  47. $gameName = gameModel::getGameName($game_code);
  48. //获取 model
  49. $models = commonFunction::getModels($game_code, 1);
  50. //获取 联赛 数据
  51. $leagueData = $getData['data'];
  52. //获取 当前请求 所有 联赛 uuid
  53. $identity = [];
  54. foreach($leagueData as $k=>$v){
  55. $identity[] = $v['uuid'];
  56. }
  57. $identity = array_unique($identity);
  58. sort($identity);
  59. //获取 本地 已存在 联赛
  60. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id')->get()->toArray();
  61. //二维数组去重
  62. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  63. //循环对比 请求identity->本地identity
  64. foreach($l_lg_data as $k=>$v){
  65. foreach($identity as $kk=> $uuid){
  66. if($v['identity'] == $uuid){
  67. unset($identity[$kk]);
  68. }
  69. }
  70. }
  71. //去除本地和请求里都存在的联赛,如果还有剩余联赛uuid,则将其写入
  72. if(!empty($identity)) {
  73. //没有 决赛时间 默认获取本年最后一天
  74. $last_time = date('Y-12-31 23:59:59');
  75. foreach($leagueData as $k=>$v){
  76. foreach($identity as $kk=> $uuid){
  77. if($v['uuid'] == $uuid){
  78. //如有 决赛时间
  79. if($v['last_time']){
  80. $last_time = $v['last_time'];
  81. }
  82. if(empty($v['uuid'])) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$v['lg_id'].';',Response::LG__UUID_NULL));
  83. $set_lg['name_chinese'] = $v['name_chinese'];
  84. $set_lg['kind'] = $v['kind'];
  85. $set_lg['match_mode'] = $v['match_mode'];
  86. $set_lg['if_stop'] = $v['if_stop'];
  87. $set_lg['identity'] = $v['uuid'];
  88. $set_lg['last_time'] = $last_time;
  89. $set_lg['utime'] = date('Y-m-d H:i:s');
  90. //写入联赛
  91. $id = $models['model_league']::insertGetId($set_lg);
  92. $m_lg_id = $id;
  93. if($m_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛-lg_id:'.$v['lg_id'].';',Response::INSERT_ERROR));
  94. //写入 本地 联赛记录
  95. $set_local = [
  96. 'lg_id'=>$id,
  97. 'others_lg_id'=>$v['lg_id'],
  98. 'source'=>$source,
  99. 'ctime'=>date('Y-m-d H:i:s'),
  100. 'identity'=>$v['uuid'],
  101. ];
  102. $ret = $models['model_local_league']::insertGetId($set_local);
  103. 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'));
  104. }
  105. }
  106. }
  107. }
  108. $this->writeLog($data,Response::success());
  109. //提交事务
  110. DB::commit();
  111. return Response::success();
  112. } catch (\Exception $e) {
  113. //回滚事务
  114. DB::rollBack();
  115. $this->writeLog($data,$e->getMessage());
  116. return $e->getMessage();
  117. }
  118. }
  119. /*
  120. * 写 赛事 数据
  121. * 每个请求 包含 N个联赛 下 N个赛事
  122. */
  123. public function setMatch($data)
  124. {
  125. try {
  126. //开启事务
  127. DB::beginTransaction();
  128. $obt = $data['data'];
  129. //请求 数据 为空
  130. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  131. $getData = $this->getAddData($obt);
  132. //不是 赛事 数据
  133. if($getData['title'] != 'match') throw new \Exception(Response::generate('不是赛事数据,',Response::ABNORMAL));
  134. //获取球类代码
  135. $game_code = $getData['game_code'];
  136. //获取数据源
  137. $source = $getData['source'];
  138. //获取球类名称
  139. $gameName = gameModel::getGameName($game_code);
  140. //获取 model
  141. $models = commonFunction::getModels($game_code, 1);
  142. //获取 赛事 数据
  143. $matchData = $getData['data'];
  144. //获取 当前请求 所有 uuid /match_id
  145. $identity = [];
  146. $s_match_ids = [];
  147. foreach($matchData as $k=>$v){
  148. $identity[] = $v['uuid'];
  149. $s_match_ids[] = $v['match_id'];
  150. }
  151. //====验证 赛事 所属 联赛 是否存在====
  152. $identity = array_unique($identity);
  153. sort($identity);
  154. //获取 本地 已存在 联赛
  155. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id')->get()->toArray();
  156. //二维数组去重
  157. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  158. //循环对比 请求uuid->本地identity
  159. foreach($l_lg_data as $k=>$v){
  160. foreach($identity as $kk=> $uuid){
  161. if($v['identity'] == $uuid){
  162. unset($identity[$kk]);
  163. }
  164. }
  165. }
  166. sort($identity);
  167. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常
  168. if(!empty($identity)) throw new \Exception(Response::generate($gameName.'联赛:uuid-'.$identity[0].';',Response::LEAGUE_ERROR));
  169. //====end====
  170. //====获取 本地 已存在 赛事====
  171. $s_match_ids = array_unique($s_match_ids);
  172. sort($s_match_ids);
  173. $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();
  174. //二维数组去重
  175. $l_match_data = commonFunction::uniquArrV2($l_match_data,'others_match_id');
  176. //循环对比 请求match_id->本地others_match_id
  177. foreach($l_match_data as $k=>$v){
  178. foreach($s_match_ids as $kk=> $s_match_id){
  179. if($v['others_match_id'] == $s_match_id){
  180. unset($s_match_ids[$kk]);
  181. }
  182. }
  183. }
  184. //剩余 赛事 数据 写入
  185. if(!empty($s_match_ids)){
  186. foreach($matchData as $k=>$v){
  187. foreach($s_match_ids as $kk=> $s_match_id){
  188. if($v['match_id'] == $s_match_id){
  189. $data = $v;
  190. $half_match_id = 0;
  191. //获取 本地 联赛 ID
  192. $lg_id = commonFunction::searcharray($data['lg_id'],'others_lg_id',$l_lg_data,'lg_id');
  193. //如果有上半场赛事id 获取上半场赛事是否存在
  194. if(!empty($data['half_match_id'])){
  195. $half_match_id = $models['model_local_match']::where(['others_match_id'=>$data['half_match_id'],'source'=>$source])
  196. ->value('match_id');
  197. 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'));
  198. }
  199. //如果赛事没有开始日期,则为冠军盘口赛事
  200. if(empty($data['match_date'])){
  201. //冠军盘口赛事获取所属联赛结束时间
  202. $last_time = $models['model_league']::where(['id'=>$lg_id])
  203. ->value('last_time');
  204. 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'));
  205. //给冠军盘口赛事 赋值时间
  206. $time = strtotime($last_time);
  207. $data['match_date'] = date('Y-m-d',$time);
  208. $data['match_time'] = date('H:i:s',$time);
  209. }
  210. $set_match = [
  211. 'ctime'=>date('Y-m-d H:i:s'),
  212. 'utime'=>date('Y-m-d H:i:s'),
  213. 'expire_time'=>date('Y-m-d H:i:s',time()+60),
  214. 'home_team'=>$data['home_team']?:'',
  215. 'guest_team'=>$data['guest_team']?:'no_team',
  216. 'lg_id'=>$lg_id,
  217. 'status'=>$data['status'],
  218. 'match_date'=>$data['match_date']?:date('Y-m-d'),
  219. 'match_time'=>$data['match_time']?:date('H:i:s'),
  220. // 'tag'=>$data['tag']?:0,
  221. 'is_rollball'=>$data['is_rollball']?:0,
  222. 'is_today'=>$data['is_today']?:0,
  223. 'is_morningplate'=>$data['is_morningplate']?:0,
  224. 'is_stringscene'=>$data['is_stringscene']?:0,
  225. 'us_time'=>$data['us_time']?:commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  226. 'half_match_id'=>$half_match_id?:0,
  227. 'identity' => $data['uuid'],
  228. ];
  229. //写入赛事 返回id
  230. $id = $models['model_match']::insertGetId($set_match);
  231. if($id < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::INSERT_ERROR)) ;
  232. //写关联记录
  233. $set_local = [
  234. 'match_id'=>$id,
  235. 'others_match_id'=>$data['match_id'],
  236. 'source'=>$source,
  237. 'ctime'=>date('Y-m-d H:i:s')
  238. ];
  239. $ret = $models['model_local_match']::insertGetId($set_local);
  240. 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'));
  241. }
  242. }
  243. }
  244. }
  245. $this->writeLog($data,Response::success());
  246. //提交事务
  247. DB::commit();
  248. return Response::success();
  249. } catch (\Exception $e) {
  250. //回滚事务
  251. DB::rollBack();
  252. $this->writeLog($data,$e->getMessage());
  253. return $e->getMessage();
  254. }
  255. }
  256. /*
  257. * 写 赛事 结果 记录 数据
  258. * 每个请求 包含 N个联赛 下 N个赛事 结果数据
  259. */
  260. public function setMatchResult($data)
  261. {
  262. try {
  263. //开启事务
  264. DB::beginTransaction();
  265. $obt = $data['data'];
  266. //请求 数据 为空
  267. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  268. $getData = $this->getAddData($obt);
  269. //不是 赛事 结果 记录 数据
  270. if($getData['title'] != 'match_result_r') throw new \Exception(Response::generate('不是赛事结果记录数据,',Response::ABNORMAL));
  271. //获取球类代码
  272. $game_code = $getData['game_code'];
  273. //获取数据源
  274. $source = $getData['source'];
  275. //获取球类名称
  276. $gameName = gameModel::getGameName($game_code);
  277. //获取 model
  278. $models = commonFunction::getModels($game_code, 1);
  279. //获取 赛事 数据
  280. $match_r_data = $getData['data'];
  281. //获取 当前请求 所有 uuid /match_id
  282. $identity = [];
  283. $s_match_ids = [];
  284. foreach($match_r_data as $k=>$v){
  285. $identity[] = $v['uuid'];
  286. $s_match_ids[] = $v['match_id'];
  287. }
  288. //====验证 赛事 所属 联赛 是否存在====
  289. $identity = array_unique($identity);
  290. sort($identity);
  291. //获取 本地 已存在 联赛
  292. $l_lg_data = $models['model_local_league']::whereIn('identity',$identity)->select('identity','lg_id')->get()->toArray();
  293. //二维数组去重
  294. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  295. //循环对比 请求uuid->本地identity
  296. foreach($l_lg_data as $k=>$v){
  297. foreach($identity as $kk=> $uuid){
  298. if($v['identity'] == $uuid){
  299. unset($identity[$kk]);
  300. }
  301. }
  302. }
  303. sort($identity);
  304. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常
  305. if(!empty($identity)) throw new \Exception(Response::generate($gameName.'联赛:uuid-'.$identity[0].';',Response::LEAGUE_ERROR));
  306. //====end====
  307. //====验证 赛事结果记录 所属赛事 是否存在====
  308. $s_match_ids = array_unique($s_match_ids);
  309. sort($s_match_ids);
  310. //获取 本地 已存在 赛事
  311. $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();
  312. //二维数组去重
  313. $l_match_data = commonFunction::uniquArrV2($l_match_data,'others_match_id');
  314. //循环对比 请求match_id->本地others_match_id
  315. foreach($l_match_data as $k=>$v){
  316. foreach($s_match_ids as $kk=> $s_match_id){
  317. if($v['others_match_id'] == $s_match_id){
  318. unset($s_match_ids[$kk]);
  319. }
  320. }
  321. }
  322. sort($s_match_ids);
  323. //去除本地和请求里都存在的赛事,如果还有剩余赛事id,则返回异常
  324. if(!empty($s_match_ids)) throw new \Exception(Response::generate($gameName.'赛事-match_id'.$s_match_ids[0].';',Response::MATCH_ERROR));
  325. //====end====
  326. //处理 赛事 结果记录 数据
  327. foreach($match_r_data as $k=>$v){
  328. $data = $v;
  329. //获取 本地 联赛 ID
  330. $lg_id = commonFunction::searcharray($data['lg_id'],'others_lg_id',$l_lg_data,'lg_id');
  331. //获取 本地 赛事 ID
  332. $match_id = commonFunction::searcharray($data['match_id'],'others_match_id',$l_match_data,'match_id');
  333. //根据球类 获取 赛事结果记录字段
  334. $set_match_r = $this->get_match_r($game_code,$lg_id,$match_id,$data);
  335. //写 赛事 结果 记录
  336. $ret = $models['model_result_record']::insert($set_match_r);
  337. if($ret != true) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$data['match_id'].';',Response::ADD_MATCH_R_R_ERROR));
  338. }
  339. $this->writeLog($data,Response::success());
  340. //提交事务
  341. DB::commit();
  342. return Response::success();
  343. } catch (\Exception $e) {
  344. //回滚事务
  345. DB::rollBack();
  346. $this->writeLog($data,$e->getMessage());
  347. return $e->getMessage();
  348. }
  349. }
  350. /*
  351. * 写 普通 赔率 数据
  352. * 每个请求 只包含 一场赛事下 N条赔率
  353. */
  354. public function setOdds($data)
  355. {
  356. try {
  357. //开启事务
  358. DB::beginTransaction();
  359. $obt = $data['data'];
  360. //请求 数据 为空
  361. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  362. $getData = $this->getAddData($obt);
  363. //不是 赔率 数据
  364. if($getData['title'] != 'odds') throw new \Exception(Response::generate('不是赔率数据,',Response::ABNORMAL));
  365. //获取球类代码
  366. $game_code = $getData['game_code'];
  367. //获取数据源
  368. $source = $getData['source'];
  369. //获取源数据联赛ID
  370. $uuid = $getData['uuid'];
  371. //获取源数据赛事ID
  372. $s_match_id = $getData['match_id'];
  373. //获取赔率所属赛事 tag 值 玩法数量
  374. $tag = $getData['tag'];
  375. //获取球类名称
  376. $gameName = gameModel::getGameName($game_code);
  377. //验证本次请求所属联赛/赛事是否存在 返回本地联赛/赛事ID
  378. $models = commonFunction::getModels($game_code, 1);
  379. $lg_id = $this->leagueVerify($models,$uuid,$source,$gameName);
  380. $match_id = $this->matchVerify($models,$s_match_id,$source,$gameName);
  381. //更新赛事 tag 值
  382. $upMatch = $models['model_match']::where(['id'=>$match_id])
  383. ->update(['tag'=>$tag,'utime'=>date('Y-m-d H:i:s')]);
  384. if($upMatch < 1) throw new \Exception(Response::generate($gameName.'赛事-match_id:'.$s_match_id.';',Response::UPMATCHTAG_ERROR)) ;
  385. //获取 赔率数据
  386. $oddsData = $getData['data'];
  387. if(!empty($oddsData)){
  388. //获取 当前 所有 可用 赔率 sole
  389. $odds_only = $getData['odds_only'];
  390. //更新赛事下所有 不在本次请求的赔率 状态
  391. $ret= OddsModel::upOddsStatus($models,$s_match_id,'',$source,$odds_only);
  392. if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  393. //拼装 待写入 赔率数据
  394. foreach($oddsData as $k=>$v){
  395. //组装数据
  396. $set_odds = [
  397. 'match_id'=> $match_id?:0,
  398. 'others_match_id'=> $v['match_id']?:0,
  399. 'odds_code'=> $v['odds_code']?:'',
  400. 'status'=> $v['status']?:0,
  401. 'sort'=> $v['sort']?:0,
  402. 'p_code'=> $v['p_code']?:'',
  403. 'odds'=> $v['odds']?:0,
  404. 'condition'=> $v['condition'],
  405. 'odds_only'=> $v['odds_only']?:'',
  406. 'source'=> $v['source']?:'',
  407. 'type'=> $v['type']?:0,
  408. 'team'=> $v['team']?:'',
  409. 'lg_id'=> $lg_id,
  410. 'others_lg_id'=> $v['lg_id'],
  411. 'ctime'=> date('Y-m-d H:i:s'),
  412. 'utime'=> date('Y-m-d H:i:s'),
  413. ];
  414. //写赔率记录数据
  415. $ret = $models['model_odds_record']->updateOrCreate(
  416. ['odds_only'=>$v['odds_only']],$set_odds
  417. );
  418. if(empty($ret)) throw new \Exception(Response::generate($gameName.'赔率记录-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_R_ERROR));
  419. //追加参数
  420. $set_odds['sole'] = $v['sole'];
  421. $set_odds['expire_time'] = date('Y-m-d H:i:s',time()+60);
  422. //写赔率 数据
  423. $ret = $models['model_odds']->updateOrCreate(
  424. ['odds_only'=>$v['odds_only']],$set_odds
  425. );
  426. if(empty($ret)) throw new \Exception(Response::generate($gameName.'赔率-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_ERROR));
  427. }
  428. }
  429. $this->writeLog($data,Response::success());
  430. //提交事务
  431. DB::commit();
  432. return Response::success();
  433. } catch (\Exception $e) {
  434. //回滚事务
  435. DB::rollBack();
  436. $this->writeLog($data,$e->getMessage());
  437. return $e->getMessage();
  438. }
  439. }
  440. /**
  441. * @param Req $data
  442. * @return string
  443. * @throws \App\Lib\Biz\Sport\Exception
  444. * 更新赛事状态
  445. */
  446. public function upMatch($data){
  447. try {
  448. //开启事务
  449. DB::beginTransaction();
  450. //获取待更新赛事
  451. $obt = $data->data;
  452. if($obt){
  453. //json转数组
  454. $data = $this->getAddData($obt);
  455. //获取 球类代码
  456. $game_code = $data['game_code'];
  457. //获取 数据源
  458. $source = $data['source'];
  459. //获取所有数据源赛事 match_id
  460. $others_match_ids = [];
  461. foreach ($data['data'] as $k=>$v){
  462. $others_match_ids[] = $v['match_id'];
  463. }
  464. //根据球类代码 获取model
  465. $model =commonFunction::getModels($game_code,1);
  466. //获取所有赛事 match_id
  467. $local_match = $model['model_local_match']::SELECT('others_match_id','match_id')
  468. ->where(['source'=>$source])
  469. ->whereIn('others_match_id',$others_match_ids)
  470. ->get()->toArray();
  471. if(empty($local_match)) throw new \Exception(Response::generate('',Response::MATCHID_NULL));
  472. //更新状态字段
  473. foreach ($local_match as $k=>$v){
  474. $set_status = [
  475. 'status'=>$v['status'],
  476. 'is_rollball'=>$v['is_rollball'],
  477. 'stais_todaytus'=>$v['is_today'],
  478. 'is_morningplate'=>$v['is_morningplate'],
  479. 'is_stringscene'=>$v['is_stringscene'],
  480. 'is_horn'=>$v['is_horn'],
  481. ];
  482. $ret = $model['model_match']::where(['id'=>$v['match_id']])
  483. -> update($set_status);
  484. if($ret<1) throw new \Exception(Response::generate(gameModel::getGameName($game_code).'赛事-match_id:'.$v['others_match_id'],Response::UPSTATUS_ERROR));
  485. }
  486. }
  487. $this->writeLog($data,Response::success());
  488. //提交事务
  489. DB::commit();
  490. return Response::success();
  491. } catch (\Exception $e) {
  492. //回滚事务
  493. DB::rollBack();
  494. $this->writeLog($data,$e->getMessage());
  495. return $e->getMessage();
  496. }
  497. }
  498. /*
  499. *写 冠军联赛 赔率 数据
  500. *每个请求 包含 N个联赛 下 N条赔率
  501. */
  502. public function setOddsCH($data)
  503. {
  504. try {
  505. //开启事务
  506. DB::beginTransaction();
  507. $obt = $data['data'];
  508. //请求 数据 为空
  509. if(empty($obt)) throw new \Exception(Response::generate('请求数据为空,',Response::ABNORMAL));
  510. $getData = $this->getAddData($obt);
  511. //不是 冠军 赔率 数据
  512. if($getData['title'] != 'odds_ch') throw new \Exception(Response::generate('不是冠军赔率数据,',Response::ABNORMAL));
  513. //获取球类代码
  514. $game_code = $getData['game_code'];
  515. //获取数据源
  516. $source = $getData['source'];
  517. //获取球类名称
  518. $gameName = gameModel::getGameName($game_code);
  519. //获取 model
  520. $models = commonFunction::getModels($game_code, 1);
  521. //获取 赔率数据
  522. $oddsData = $getData['data'];
  523. //获取 当前 所有 可用 赔率 sole
  524. // $odds_only = $getData['odds_only'];
  525. //====验证 请求里 所有赔率 所属联赛 是否存在 ====
  526. //获取 当前请求 所有 联赛 uuid
  527. $uuids = [];
  528. foreach($oddsData as $k=>$v){
  529. $uuids[] = $v['uuid'];
  530. }
  531. $uuids = array_unique($uuids);
  532. sort($uuids);
  533. //更新赛事下所有 不在本次请求的赔率 状态
  534. // $ret= OddsModel::upOddsStatus($models,'',$s_lg_ids,$source,$odds_only);
  535. // if($ret != true) throw new \Exception(Response::generate($gameName,Response::ODDS_SOLE_ERR));
  536. //获取 本地 已存在 联赛
  537. $l_lg_data = $models['model_local_league']::whereIn('identity',$uuids)->select('identity','lg_id')->get()->toArray();
  538. //二维数组去重
  539. $l_lg_data = commonFunction::uniquArrV2($l_lg_data,'identity');
  540. //循环对比 请求uuid->本地uuid
  541. foreach($l_lg_data as $k=>$v){
  542. foreach($uuids as $kk=> $uuid){
  543. if($v['identity'] == $uuid){
  544. unset($uuids[$kk]);
  545. }
  546. }
  547. }
  548. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常;
  549. if(!empty($uuids)) throw new \Exception(Response::generate($gameName.'冠军联赛:uuid-'.$uuids[0].';',Response::LEAGUE_ERROR));
  550. //====end====
  551. //拼装 待写入 赔率数据
  552. foreach($oddsData as $k=>$v){
  553. //获取 本地 联赛 ID
  554. $lg_id = commonFunction::searcharray($v['lg_id'],'others_lg_id',$l_lg_data,'lg_id');
  555. //组装数据
  556. $set_odds = [
  557. 'match_id'=>0,
  558. 'others_match_id'=> $v['match_id']?:0,
  559. 'odds_code'=> $v['odds_code']?:'',
  560. 'status'=> $v['status']?:0,
  561. 'sort'=> $v['sort']?:0,
  562. 'p_code'=> $v['p_code']?:'',
  563. 'odds'=> $v['odds']?:0,
  564. 'condition'=> $v['condition'],
  565. 'odds_only'=> $v['odds_only']?:'',
  566. 'source'=> $v['source']?:'',
  567. 'type'=> $v['type']?:1,
  568. 'team'=> $v['team']?:'',
  569. 'lg_id'=> $lg_id,
  570. 'others_lg_id'=> $v['lg_id'],
  571. 'ctime'=> date('Y-m-d H:i:s'),
  572. 'utime'=> date('Y-m-d H:i:s'),
  573. ];
  574. //写赔率记录数据
  575. $ret = $models['model_odds_record']->updateOrCreate(
  576. ['odds_only'=>$v['odds_only']],$set_odds
  577. );
  578. if(empty($ret)) throw new \Exception(Response::generate($gameName.'赔率记录-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_R_ERROR));
  579. //追加参数
  580. $set_odds['sole'] = $v['sole'];
  581. $set_odds['expire_time'] = date('Y-m-d H:i:s',time()+60);
  582. //写赔率 数据
  583. $ret = $models['model_odds']->updateOrCreate(
  584. ['odds_only'=>$v['odds_only']],$set_odds
  585. );
  586. if(empty($ret)) throw new \Exception(Response::generate($gameName.'赔率-odds_only:'.$v['odds_only'].';',Response::ADD_ODDS_ERROR));
  587. }
  588. $this->writeLog($data,Response::success());
  589. //提交事务
  590. DB::commit();
  591. return Response::success();
  592. } catch (\Exception $e) {
  593. //回滚事务
  594. DB::rollBack();
  595. $this->writeLog($data,$e->getMessage());
  596. return $e->getMessage();
  597. }
  598. }
  599. /*
  600. * 写入直播 数据
  601. */
  602. public function setBroadCast($data)
  603. {
  604. try {
  605. //开启事务
  606. DB::beginTransaction();
  607. $obt = $data['data'];
  608. //请求 数据 为空
  609. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  610. $getData = $this->getAddData($obt);
  611. //不是 直播 数据
  612. if ($getData['title'] != 'broad_cast') throw new \Exception(Response::generate('不是直播数据,', Response::ABNORMAL));
  613. //获取 球类
  614. $game_code = $getData['game_code'];
  615. //获取 直播 数据
  616. $BroadCast = $getData['data'];
  617. $set_broadcast = [];
  618. foreach ($BroadCast as $k => $data) {
  619. //组装数据
  620. $set_broadcast[] = [
  621. "doing" => $data['doing'],
  622. "game_type" => $data['game_type'],
  623. "game_code" => $game_code,
  624. "guest_team" => $data['guest_team'],
  625. "host_team" => $data['host_team'],
  626. "league_name" => $data['league_name'],
  627. "shower" => $data['shower'],
  628. "showid" => (int)$data['showid'],
  629. "start_time" => $data['start_time'],
  630. "ctime" => date('Y-m-d H:i:s'),
  631. "utime" => date('Y-m-d H:i:s'),
  632. ];
  633. }
  634. //写入 直播 数据
  635. $ret = broadcastModel::insert($set_broadcast);
  636. if ($ret == false) throw new \Exception(Response::generate('', Response::BROADCAST_ERROR));
  637. $this->writeLog($data,Response::success());
  638. //提交事务
  639. DB::commit();
  640. return Response::success();
  641. } catch (\Exception $e) {
  642. //回滚事务
  643. DB::rollBack();
  644. $this->writeLog($data,$e->getMessage());
  645. return $e->getMessage();
  646. }
  647. }
  648. /*
  649. * 验证所属 联赛 是否存在
  650. */
  651. public function leagueVerify($models=[],$uuid='',$source='',$gameName=''){
  652. if(empty($models) || empty($uuid)) throw new \Exception(Response::generate('',Response::ABNORMAL));
  653. $l_lg_id = $models['model_local_league']::where(['identity'=>$uuid])->value('lg_id');
  654. if($l_lg_id < 1) throw new \Exception(Response::generate($gameName.'联赛:lg_id-'.$uuid,Response::LEAGUE_ERROR));
  655. return $l_lg_id;
  656. }
  657. /*
  658. * 验证所属 赛事 是否存在
  659. */
  660. public function matchVerify($models = [], $s_match_id = '', $source = '', $gameName = '')
  661. {
  662. if (empty($models) || empty($s_match_id)) throw new \Exception(Response::generate('', Response::ABNORMAL));
  663. $l_match_id = $models['model_local_match']::where(['others_match_id' => $s_match_id, 'source' => $source])->value('match_id');
  664. if ($l_match_id < 1) throw new \Exception(Response::generate($gameName . '赛事:match_id-' . $s_match_id, Response::MATCH_ERROR));
  665. return $l_match_id;
  666. }
  667. /*
  668. * 写入直播 数据
  669. */
  670. public function broadcast($opt = [], $game_code = '')
  671. {
  672. $data = $opt['data'];
  673. $set_broadcast = [
  674. "doing" => $data['doing'],
  675. "game_type" => $data['game_type'],
  676. "game_code" => $game_code,
  677. "guest_team" => $data['guest_team'],
  678. "host_team" => $data['host_team'],
  679. "league_name" => $data['league_name'],
  680. "shower" => $data['shower'],
  681. "showid" => (int)$data['showid'],
  682. "start_time" => $data['start_time'],
  683. "ctime" => date('Y-m-d H:i:s'),
  684. "utime" => date('Y-m-d H:i:s'),
  685. ];
  686. $ret = broadcastModel::insert($set_broadcast);
  687. if ($ret == false) throw new \Exception(Response::generate('', Response::BROADCAST_ERROR));
  688. }
  689. /*
  690. * 写入 赛事 初始 结果
  691. */
  692. public function match_result($game_code = '')
  693. {
  694. $model = commonFunction::getModels($game_code, 1);
  695. if ($game_code == 'zq') ZqResultModel::ZQresult($model);
  696. if ($game_code == 'lq') LqResultModel::LQresult($model);
  697. if ($game_code == 'wq') WqResultModel::WQresult($model);
  698. if ($game_code == 'bq') BqResultModel::BQresult($model);
  699. }
  700. /*
  701. * 写入联赛数据
  702. */
  703. public function league($opt = [], $gameName = '')
  704. {
  705. $game_code = $opt['game_code'];
  706. //根据球类代码获取相关model
  707. $model = commonFunction::getModels($game_code, 1);
  708. $data = $opt['data'];
  709. //查询联赛是否已存在
  710. $id = $model['model_league']::where('name_chinese', '=', $data['name_chinese'])
  711. ->value('id');
  712. //默认获取本年最后一天
  713. $last_time = date('Y-12-31 23:59:59');
  714. //决赛时间
  715. if ($data['last_time']) {
  716. $last_time = $data['last_time'];
  717. }
  718. $set_lg['name_chinese'] = $data['name_chinese'];
  719. $set_lg['kind'] = $data['kind'];
  720. $set_lg['match_mode'] = $data['match_mode'];
  721. $set_lg['if_stop'] = $data['if_stop'];
  722. $set_lg['identity'] = $data['uuid'] ?: '';
  723. $set_lg['last_time'] = $last_time;
  724. $set_lg['utime'] = date('Y-m-d H:i:s');
  725. if (empty($id)) {
  726. //写入联赛
  727. $id = $model['model_league']::insertGetId($set_lg);
  728. $m_lg_id = $id;
  729. if ($m_lg_id < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $data['lg_id'] . ';', Response::INSERT_ERROR));
  730. } else {
  731. //更新联赛
  732. $ret = $model['model_league']::where(['id' => $id])
  733. ->update($set_lg);
  734. if ($ret < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $data['lg_id'] . ';', Response::UPDATE_ERROR));
  735. }
  736. $set_local = [
  737. 'lg_id' => $id,
  738. 'others_lg_id' => $data['lg_id'],
  739. 'source' => $data['source'],
  740. 'ctime' => date('Y-m-d H:i:s')
  741. ];
  742. $ret = $model['model_local_league']::insertGetId($set_local);
  743. 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'));
  744. //执行成功返回源数据 联赛ID
  745. $data = ['others_lg_id' => $data['lg_id'], 'lg_id' => $id];
  746. return $data;
  747. }
  748. /*
  749. * 写入赛事数据
  750. */
  751. public function competition($opt = [], $gameName = '', $lg_id = '')
  752. {
  753. $game_code = $opt['game_code'];
  754. //根据球类代码获取相关model
  755. $model = commonFunction::getModels($game_code, 1);
  756. $data = $opt['data'];
  757. if (empty($data['lg_id'])) throw new \Exception(Response::generate('', Response::LEAGUE_ERROR));//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  758. //查询赛事是否存在
  759. $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']])
  760. ->value('id');
  761. $half_match_id = 0;
  762. //如果有上半场赛事id 获取上半场赛事是否存在
  763. if (!empty($data['half_match_id'])) {
  764. $half_match_id = $match_id = $model['model_local_match']::where(['others_match_id' => $data['half_match_id'], 'source' => $data['source']])
  765. ->value('match_id');
  766. 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'));
  767. }
  768. //如果赛事没有开始日期,则为冠军盘口赛事
  769. if (empty($data['match_date'])) {
  770. //冠军盘口赛事获取所属联赛结束时间
  771. $last_time = $model['model_league']::where(['id' => $lg_id])
  772. ->value('last_time');
  773. 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'));
  774. //给冠军盘口赛事 赋值时间
  775. $time = strtotime($last_time);
  776. $data['match_date'] = date('Y-m-d', $time);
  777. $data['match_time'] = date('H:i:s', $time);
  778. }
  779. $set_match = [
  780. 'ctime' => date('Y-m-d H:i:s'),
  781. 'utime' => date('Y-m-d H:i:s'),
  782. 'expire_time' => date('Y-m-d H:i:s', time() + 60),
  783. 'home_team' => $data['home_team'] ?: '',
  784. 'guest_team' => $data['guest_team'] ?: 'no_team',
  785. 'lg_id' => $lg_id,
  786. 'status' => $data['status'],
  787. 'match_date' => $data['match_date'] ?: date('Y-m-d'),
  788. 'match_time' => $data['match_time'] ?: date('H:i:s'),
  789. 'tag' => $data['tag'] ?: 0,
  790. 'is_rollball' => $data['is_rollball'] ?: 0,
  791. 'is_today' => $data['is_today'] ?: 0,
  792. 'is_morningplate' => $data['is_morningplate'] ?: 0,
  793. 'is_stringscene' => $data['is_stringscene'] ?: 0,
  794. 'us_time' => $data['us_time'] ?: commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  795. 'half_match_id' => $half_match_id ?: 0,
  796. 'identity' => $data['uuid'],
  797. ];
  798. //写入赛事
  799. if (empty($id)) {
  800. //写入赛事 返回id
  801. $id = $model['model_match']::insertGetId($set_match);
  802. if ($id < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::INSERT_ERROR));
  803. } else {
  804. //更新赛事
  805. $ret = $model['model_match']::where(['id' => $id])->update($set_match);
  806. if ($ret < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::UPDATE_ERROR));
  807. }
  808. //写关联记录
  809. $set_local = [
  810. 'match_id' => $id,
  811. 'others_match_id' => $data['match_id'],
  812. 'source' => $data['source'],
  813. 'ctime' => date('Y-m-d H:i:s')
  814. ];
  815. $ret = $model['model_local_match']::insertGetId($set_local);
  816. 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'));
  817. //返回 源数据 赛事ID
  818. $data = ['others_match_id' => $data['match_id'], 'match_id' => $id];
  819. return $data;
  820. }
  821. /*
  822. * 写入赔率/赔率记录数据
  823. */
  824. public function odds($opt = [], $gameName = '', $lg_id = 0, $match_id = 0, $oddsID = '', $oddsRecordID = '')
  825. {
  826. $game_code = $opt['game_code'];
  827. //根据球类代码获取相关model
  828. $model = commonFunction::getModels($game_code, 1);
  829. $data = $opt['data'];
  830. //获取赛事 本地/源ID
  831. $others_match_id = $data['match_id'];
  832. //获取联赛 本地/源ID
  833. $others_lg_id = $data['lg_id'];
  834. //===写赔率记录===
  835. $set_odds_r = [
  836. 'match_id' => $match_id ?: 0,
  837. 'others_match_id' => $others_match_id ?: 0,
  838. 'odds_code' => $data['odds_code'] ?: '',
  839. 'status' => $data['status'] ?: 0,
  840. 'sort' => $data['sort'] ?: 0,
  841. 'p_code' => $data['p_code'] ?: '',
  842. 'odds' => $data['odds'] ?: 0,
  843. 'condition' => $data['condition'],
  844. 'odds_only' => $data['odds_only'] ?: '',
  845. 'source' => $data['source'] ?: '',
  846. 'type' => $data['type'] ?: 0,
  847. 'team' => $data['team'] ?: '',
  848. 'lg_id' => $lg_id,
  849. 'others_lg_id' => $others_lg_id,
  850. 'ctime' => date('Y-m-d H:i:s'),
  851. ];
  852. //更新或写入赔率记录
  853. if ($oddsRecordID != false) {
  854. $ret = $model['model_odds_record']::where(['id' => $oddsRecordID])
  855. ->update($set_odds_r);
  856. 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'));
  857. } else {
  858. $ret = $model['model_odds_record']::insert($set_odds_r);
  859. 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'));
  860. }
  861. //===写赔率===
  862. $set_odds = [
  863. 'match_id' => $match_id ?: 0,
  864. 'others_match_id' => $others_match_id ?: 0,
  865. 'odds_code' => $data['odds_code'] ?: '',
  866. 'status' => $data['status'] ?: 0,
  867. 'sort' => $data['sort'] ?: 0,
  868. 'p_code' => $data['p_code'] ?: '',
  869. 'odds' => $data['odds'] ?: 0,
  870. 'condition' => $data['condition'],
  871. 'odds_only' => $data['odds_only'] ?: '',
  872. 'sole' => $data['sole'] ?: '',
  873. 'source' => $data['source'] ?: '',
  874. 'type' => $data['type'] ?: 0,
  875. 'team' => $data['team'] ?: '',
  876. 'lg_id' => $lg_id,
  877. 'others_lg_id' => $others_lg_id,
  878. 'ctime' => date('Y-m-d H:i:s'),
  879. 'utime' => date('Y-m-d H:i:s'),
  880. 'expire_time' => date('Y-m-d H:i:s', time() + 60),
  881. ];
  882. //更新或写入赔率数据
  883. if ($oddsID != false) {
  884. $ret = $model['model_odds']::where(['id' => $oddsID])
  885. ->update($set_odds);
  886. 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'));
  887. } else {
  888. $ret = $model['model_odds']::insert($set_odds);
  889. 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'));
  890. }
  891. //===end===
  892. }
  893. /*
  894. * 更新 赛事下 所有不在本次请求的 赔率状态
  895. */
  896. public function upOddsStatus($opt = [], $gameName = '')
  897. {
  898. //获取球类代码
  899. $game_code = $opt['game_code'];
  900. //获取球类model
  901. $model = commonFunction::getModels($game_code, 1);
  902. //赛事下 所有 赔率sole
  903. $odds_sole = $opt['data'];
  904. //数据源 赛事 id
  905. $others_match_id = $opt['match_id'];
  906. //数据源
  907. $source = $opt['source'];
  908. //更新赛事下所有 不在本次请求的赔率 状态
  909. $ret = OddsModel::getMatchSole($model, $others_match_id, '', $source, $odds_sole);
  910. if ($ret != true) throw new \Exception(Response::generate($gameName, Response::ODDS_SOLE_ERR));
  911. }
  912. /*
  913. * 写入联赛结果
  914. */
  915. public function league_result($opt, $gameName)
  916. {
  917. $game_code = $opt['game_code'];
  918. //根据球类代码获取相关model
  919. $model = commonFunction::getModels($game_code, 1);
  920. $data = $opt['data'];
  921. //验证结果所属联赛
  922. $lg_id = $model['model_local_league']::where(['others_lg_id' => $data['lg_id'], 'source' => $data['source']])
  923. ->value('lg_id');
  924. if ($lg_id < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $data['lg_id'] . ';', Response::LEAGUE_ERROR));
  925. $lg_result_id = $model['model_league_result']::where(['lg_id' => $lg_id, 'game_name' => $data['game_name']])
  926. ->value('id');
  927. $set_lg_result = [
  928. 'lg_id' => $lg_id,
  929. 'game_name' => $data['game_name'],
  930. 'result' => json_encode($data['result'], JSON_UNESCAPED_UNICODE),
  931. 'status' => $data['status'],
  932. 'ctime' => date('Y-m-d H:i:s'),
  933. 'utime' => date('Y-m-d H:i:s'),
  934. ];
  935. //联赛结果数据处理
  936. if (!empty($lg_result_id)) {
  937. $ret = $model['model_league_result']::where(['id' => $lg_result_id])
  938. ->update($set_lg_result);
  939. 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'));
  940. } else {
  941. $ret = $model['model_league_result']::insert($set_lg_result);
  942. 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'));
  943. }
  944. }
  945. /*
  946. * 写入赛事结果
  947. */
  948. public function com_result($opt = [], $gameName = '', $lg_id = '', $match_id = '')
  949. {
  950. $game_code = $opt['game_code'];
  951. //根据球类代码获取相关model
  952. $model = commonFunction::getModels($game_code, 1);
  953. $data = $opt['data'];
  954. //查询结果是否存在
  955. $match_r_id = $model['model_result']::where(['match_id' => $match_id])
  956. ->value('id');
  957. $set_match_r = [
  958. "home_team" => $data['home_team'],
  959. "guest_team" => $data['guest_team'],
  960. "lg_id" => $lg_id,
  961. "home_rate" => $data['home_rate'],
  962. "guest_rate" => $data['guest_rate'],
  963. "home_score" => $data['home_score'],
  964. "guest_score" => $data['guest_score'],
  965. "all_goal" => $data['all_goal'],
  966. "status" => $data['status'],
  967. "first_score" => $data['first_score'],
  968. "last_score" => $data['last_score'],
  969. "match_score" => $data['match_score'],
  970. "match_winer" => $data['match_winer'],
  971. "match_time" => $data['match_time'],
  972. "match_process" => $data['match_process'],
  973. "tag" => $data['tag'],
  974. "match_id" => $match_id,
  975. "u_home_score" => $data['u_home_score'],
  976. "u_guest_score" => $data['u_guest_score'],
  977. "p_code" => $data['p_code'],
  978. "update_time" => date('Y-m-d H:i:s')
  979. ];
  980. //赛事结果数据处理
  981. if (!empty($match_r_id)) {
  982. $ret = $model['model_result']::where(['id' => $match_r_id])
  983. ->update($set_match_r);
  984. 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'));
  985. } else {
  986. $ret = $model['model_result']::insert($set_match_r);
  987. 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'));
  988. }
  989. // return Response::success();
  990. }
  991. //写入赛事结果记录
  992. public function com_result_record($opt = [], $gameName = '', $lg_id = '', $match_id = '')
  993. {
  994. $game_code = $opt['game_code'];
  995. //根据球类代码获取相关model
  996. $model = commonFunction::getModels($game_code, 1);
  997. $data = $opt['data'];
  998. $match_r_id = $model['model_result_record']::where(['match_id' => $match_id, 'match_time' => $data['match_time']])
  999. ->value('id');
  1000. //根据球类 获取 赛事结果记录字段
  1001. $set_match_r = $this->get_match_r($game_code, $lg_id, $match_id, $data);
  1002. //赛事结果记录处理
  1003. if ($match_r_id > 0) {
  1004. $ret = $model['model_result_record']::where(['id' => $match_r_id])
  1005. ->update($set_match_r);
  1006. 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'));
  1007. } else {
  1008. $ret = $model['model_result_record']::insert($set_match_r);
  1009. 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'));
  1010. }
  1011. }
  1012. /**
  1013. * @param $data
  1014. * @return mixed
  1015. * json转数组
  1016. */
  1017. public function getAddData($data)
  1018. {
  1019. $data = json_decode($data, true);
  1020. return $data;
  1021. }
  1022. /*
  1023. * 根据球类获取 赛事结果记录字段
  1024. */
  1025. public function get_match_r($game_code, $lg_id, $match_id, $data)
  1026. {
  1027. $set_match_r = [];
  1028. if ($game_code == 'zq') {
  1029. $set_match_r = [
  1030. "home_team" => $data['home_team'] ?: '',
  1031. "guest_team" => $data['guest_team'] ?: '',
  1032. "lg_id" => $lg_id,
  1033. "all_goal" => $data['all_goal'] ?: 0,
  1034. "home_rate" => $data['home_rate'] ?: 0,
  1035. "guest_rate" => $data['guest_rate'] ?: 0,
  1036. "home_score" => $data['home_score'] ?: 0,
  1037. "guest_score" => $data['guest_score'] ?: 0,
  1038. "status" => $data['status'] ?: 0,
  1039. "first_score" => $data['first_score'] ?: 0,
  1040. "last_score" => $data['last_score'] ?: 0,
  1041. "match_score" => $data['match_score'] ?: 0,
  1042. "match_winer" => $data['match_winer'] ?: '',
  1043. "match_time" => $data['match_time'] ?: 0,
  1044. "match_process" => $data['match_process'] ?: '',
  1045. //"tag" => $data['tag'] ?: 0,
  1046. "match_id" => $match_id,
  1047. "update_time" => date('Y-m-d H:i:s')
  1048. ];
  1049. };
  1050. if ($game_code == 'lq') {
  1051. $set_match_r = [
  1052. "home_team" => $data['home_team'] ?: '',
  1053. "guest_team" => $data['guest_team'] ?: '',
  1054. "lg_id" => $lg_id,
  1055. "home_rate" => $data['home_rate'] ?: 0,
  1056. "guest_rate" => $data['guest_rate'] ?: 0,
  1057. "home_score" => $data['home_score'] ?: 0,
  1058. "guest_score" => $data['guest_score'] ?: 0,
  1059. "status" => $data['status'] ?: 0,
  1060. "first_score" => $data['first_score'] ?: 0,
  1061. "last_score" => $data['last_score'] ?: 0,
  1062. "match_score" => $data['match_score'] ?: 0,
  1063. "match_winer" => $data['match_winer'] ?: '',
  1064. "match_time" => $data['match_time'] ?: 0,
  1065. "match_process" => $data['match_process'] ?: '',
  1066. //"tag" => $data['tag'] ?: 0,
  1067. "match_id" => $match_id,
  1068. "update_time" => date('Y-m-d H:i:s')
  1069. ];
  1070. }
  1071. if ($game_code == 'wq') {
  1072. $set_match_r = [
  1073. "home_player_name" => $data['home_player_name'] ?: '',
  1074. "guest_player_name" => $data['guest_player_name'] ?: '',
  1075. "lg_id" => $lg_id,
  1076. "home_player_let_plate" => $data['home_player_let_plate'] ?: 0,
  1077. "guest_player_let_plate" => $data['guest_player_let_plate'] ?: 0,
  1078. "home_player_let_inning" => $data['home_player_let_inning'] ?: 0,
  1079. "guest_player_let_inning" => $data['guest_player_let_inning'] ?: 0,
  1080. "all_inning" => $data['all_inning'] ?: 0,
  1081. "home_player_score" => $data['home_player_score'] ?: 0,
  1082. "guest_player_score" => $data['guest_player_score'] ?: 0,
  1083. "status" => $data['status'] ?: 0,
  1084. "first_score_player" => $data['first_score_player'] ?: '',
  1085. "last_score_player" => $data['last_score_player'] ?: '',
  1086. "first_inning_score" => $data['first_inning_score'] ?: 0,
  1087. "second_inning_score" => $data['second_inning_score'] ?: 0,
  1088. "third_inning_score" => $data['third_inning_score'] ?: 0,
  1089. "match_winer_player" => $data['match_winer_player'] ?: '',
  1090. "update_time" => date('Y-m-d H:i:s'),
  1091. "match_time" => $data['match_time'] ?: 0,
  1092. "match_process" => $data['match_process'] ?: '',
  1093. //"tag" => $data['tag'] ?: 0,
  1094. "match_id" => $match_id,
  1095. "result_mark" => $data['result_mark'] ?: '',
  1096. ];
  1097. }
  1098. if ($game_code == 'bq') {
  1099. $set_match_r = [
  1100. "home_team" => $data['home_team'] ?: '',
  1101. "guest_team" => $data['guest_team'] ?: '',
  1102. "lg_id" => $lg_id,
  1103. "home_rate" => $data['home_rate'] ?: 0,
  1104. "guest_rate" => $data['guest_rate'] ?: 0,
  1105. "home_score" => $data['home_score'] ?: 0,
  1106. "guest_score" => $data['guest_score'] ?: 0,
  1107. "status" => $data['status'] ?: 0,
  1108. "first_score" => $data['first_score'] ?: 0,
  1109. "last_score" => $data['last_score'] ?: 0,
  1110. "match_score" => $data['match_score'] ?: 0,
  1111. "match_winer" => $data['match_winer'] ?: '',
  1112. "match_time" => $data['match_time'] ?: 0,
  1113. "match_process" => $data['match_process'] ?: '',
  1114. //"tag" => $data['tag'] ?: 0,
  1115. "match_id" => $match_id,
  1116. "all_inning" => $data['all_inning'] ?: 9,
  1117. "update_time" => date('Y-m-d H:i:s'),
  1118. "result_mark" => $data['result_mark'] ?: '',
  1119. ];
  1120. }
  1121. return $set_match_r;
  1122. }
  1123. private function writeLog($body, $ret)
  1124. {
  1125. if (!is_string($body)) {
  1126. $body = json_encode([$body], 256);
  1127. }
  1128. $data = ['ctime' => date("Y-m-d H:i:s"), 'body' => $body, 'ret' => $ret];
  1129. $return = DB::table('datainf_log')->insert($data);
  1130. return $return;
  1131. }
  1132. }