DataLogic.php 67 KB

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