WriteSportsController.php 69 KB

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