WriteSportsController.php 80 KB

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