DataLogic.php 56 KB

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