WriteSportsController.php 69 KB

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