WriteSportsController.php 80 KB

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