WriteSportsController.php 83 KB

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