DataLogic.php 65 KB

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