DataLogic.php 57 KB

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