WriteSportsController.php 78 KB

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