WriteSportsController.php 81 KB

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