WriteSportsController.php 79 KB

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