DataLogic.php 67 KB

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