DataLogic.php 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984
  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. public function setResultExpress(Req $data)
  144. {
  145. $ret = $this->setResultExpress_v1($data);
  146. return $ret;
  147. }
  148. /**
  149. * 写赛果数据
  150. */
  151. public function setResultExpress_v1($data)
  152. {
  153. try {
  154. //开启事务
  155. DB::beginTransaction();
  156. $obt = $data['data;'];
  157. //请求 数据 为空
  158. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  159. $getData = $this->getAddData($obt);
  160. //不是 赛事 结果 记录 数据
  161. if ($getData['title'] != 'result') throw new \Exception(Response::generate('不是赛果数据,', Response::ABNORMAL));
  162. //获取球类代码
  163. $game_code = $getData['game_code'];
  164. //获取数据源
  165. $source = $getData['source'];
  166. //获取球类名称
  167. $gameName = gameModel::getGameName($game_code);
  168. //获取 model
  169. $models = commonFunction::getModels($game_code, 1);
  170. //获取 赛事 数据
  171. $match_r_data = $getData['data'];
  172. if (!empty($match_r_data)) {
  173. foreach ($match_r_data as $k => $v) {
  174. $del = $models['model_result_express']::where('match_id', $v['match_id'])->delete();
  175. $v['c_time'] = date('Y-m-d H:i:s', time());
  176. $v['u_time'] = date('Y-m-d H:i:s', time());
  177. $v['source'] = $source;
  178. $v['play_data'] = json_encode($v['play_data'], JSON_UNESCAPED_UNICODE);
  179. $ret = $models['model_result_express']::insert($v);
  180. if ($ret != true) throw new \Exception(Response::generate($gameName . ';', Response::SET_MATCH_R_ERR));
  181. }
  182. //写 赛事 结果 记录
  183. }
  184. $this->writeLog($data, Response::success());
  185. //提交事务
  186. DB::commit();
  187. //写请求数据 日志记录
  188. return Response::success();
  189. } catch (\Exception $e) {
  190. //回滚事务
  191. DB::rollBack();
  192. $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  193. return $e->getMessage();
  194. }
  195. }
  196. /*
  197. * 写 赛事 数据
  198. * 每个请求 包含 N个联赛 下 N个赛事
  199. */
  200. public function setMatch($data)
  201. {
  202. $logdata = $data;
  203. try {
  204. //开启事务
  205. DB::beginTransaction();
  206. $obt = $data['data'];
  207. //请求 数据 为空
  208. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  209. $getData = $this->getAddData($obt);
  210. //不是 赛事 数据
  211. if ($getData['title'] != 'match') throw new \Exception(Response::generate('不是赛事数据,', Response::ABNORMAL));
  212. /*
  213. //写请求数据 日志记录
  214. if($this->isRecord){
  215. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  216. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  217. }
  218. */
  219. //获取球类代码
  220. $game_code = $getData['game_code'];
  221. //获取数据源
  222. $source = $getData['source'];
  223. //获取球类名称
  224. $gameName = gameModel::getGameName($game_code);
  225. //获取 model
  226. $models = commonFunction::getModels($game_code, 1);
  227. //获取 赛事 数据
  228. $matchData = $getData['data'];
  229. //获取 当前请求 所有 uuid
  230. $identity = [];
  231. $s_match_ids = [];
  232. foreach ($matchData as $k => $v) {
  233. $identity[] = $v['uuid'];
  234. $s_match_ids[] = $v['match_id'];
  235. }
  236. //====验证 赛事 所属 联赛 是否存在====
  237. $identity = array_unique($identity);
  238. sort($identity);
  239. //获取 本地 已存在 联赛
  240. $l_lg_data = $models['model_local_league']::whereIn('identity', $identity)->select('identity', 'lg_id', 'others_lg_id')->get()->toArray();
  241. //二维数组去重
  242. $l_lg_data = commonFunction::uniquArrV2($l_lg_data, 'identity');
  243. //如果获取不到本地联赛数据,则返回联赛不存在
  244. if (empty($l_lg_data)) throw new \Exception(Response::generate($gameName . '本次请求的所有赛事都没有联赛数据;', Response::LEAGUE_ERROR));
  245. //获取到本次请求,所有本地有联赛的赛事
  246. $mathData_before = $matchData;//接收原赛事
  247. $matchData = [];//获取新赛事
  248. foreach ($l_lg_data as $k => $v) {
  249. foreach ($mathData_before as $kk => $vv) {
  250. if ($v['identity'] == $vv['uuid']) {
  251. $matchData[] = $vv;
  252. }
  253. }
  254. }
  255. //获取当前请求有关联联赛的赛事 match_id
  256. $s_match_ids = [];
  257. foreach ($matchData as $k => $v) {
  258. $s_match_ids[] = $v['match_id'];
  259. }
  260. //====获取 本地 已存在 赛事====
  261. $s_match_ids = array_unique($s_match_ids);
  262. sort($s_match_ids);
  263. $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();
  264. //二维数组去重
  265. $l_match_data = commonFunction::uniquArrV2($l_match_data, 'others_match_id');
  266. //循环对比 请求match_id->本地others_match_id
  267. //如果本地有 赛事,则分别获取本地已存在/不存在赛事
  268. if (!empty($l_match_data)) {
  269. //本地已存在赛事,用于更新
  270. $matchData_y = [];
  271. foreach ($l_match_data as $k => $v) {
  272. foreach ($s_match_ids as $kk => $s_match_id) {
  273. if ($v['others_match_id'] == $s_match_id) {
  274. $matchData_y[$k]['others_match_id'] = $s_match_id;
  275. $matchData_y[$k]['match_id'] = $v['match_id'];
  276. unset($s_match_ids[$kk]);
  277. }
  278. }
  279. }
  280. sort($s_match_ids);
  281. }
  282. //如果 请求中 有本地赛事,则执行更新
  283. if (!empty($matchData_y)) {
  284. foreach ($matchData as $k => $v) {
  285. foreach ($matchData_y as $kk => $vv) {
  286. if ($v['match_id'] == $vv['others_match_id']) {
  287. $data = $v;
  288. $up_match = [];
  289. $up_match['utime'] = date('Y-m-d H:i:s');
  290. $up_match['status'] = $data['status'];
  291. //获取需更新字段
  292. if ($data['is_rollball'] == 1) {
  293. $up_match['is_rollball'] = $data['is_rollball'];
  294. }
  295. if ($data['is_today'] == 1) {
  296. $up_match['is_today'] = $data['is_today'];
  297. }
  298. if ($data['is_morningplate'] == 1) {
  299. $up_match['is_morningplate'] = $data['is_morningplate'];
  300. }
  301. if ($data['is_stringscene'] == 1) {
  302. $up_match['is_stringscene'] = $data['is_stringscene'];
  303. }
  304. //追加更新开赛日期/时间
  305. $up_match['match_date'] = $data['match_date'];
  306. $up_match['match_time'] = $data['match_time'];
  307. $up_match['us_time'] = $data['us_time'];
  308. //如果是网球 追加rule 字段
  309. if ($game_code == 'wq' and !empty($data['rule'])) {
  310. $up_match['rule'] = $data['rule'];
  311. }
  312. //更新赛事 返回成功数量
  313. $set = $models['model_match']::where(['id' => $vv['match_id']])
  314. ->update($up_match);
  315. if ($set < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::UPSTATUS_ERROR));
  316. }
  317. }
  318. }
  319. }
  320. //剩余 赛事 数据 写入
  321. if (!empty($s_match_ids)) {
  322. foreach ($matchData as $k => $v) {
  323. foreach ($s_match_ids as $kk => $s_match_id) {
  324. if ($v['match_id'] == $s_match_id) {
  325. $data = $v;
  326. $half_match_id = $v['half_match_id'];
  327. //获取 本地 联赛 ID
  328. //验证本赛事是否有数据源lg_id
  329. if (empty($data['lg_id'])) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::S_LG_ID_NULL));
  330. $lg_id = commonFunction::searcharray($data['uuid'], 'identity', $l_lg_data, 'lg_id');
  331. //验证 本赛事是否获取到本地lg_id
  332. if (empty($lg_id)) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::L_LG_ID_NULL));
  333. //如果赛事没有开始日期,则为冠军盘口赛事
  334. if (empty($data['match_date'])) {
  335. //冠军盘口赛事获取所属联赛结束时间
  336. $last_time = $models['model_league']::where(['id' => $lg_id])
  337. ->value('last_time');
  338. 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'));
  339. //给冠军盘口赛事 赋值时间
  340. $time = strtotime($last_time);
  341. $data['match_date'] = date('Y-m-d', $time);
  342. $data['match_time'] = date('H:i:s', $time);
  343. }
  344. $set_match = [
  345. 'ctime' => date('Y-m-d H:i:s'),
  346. 'utime' => date('Y-m-d H:i:s'),
  347. 'expire_time' => date('Y-m-d H:i:s', time() + 60),
  348. 'home_team' => $data['home_team'] ?: '',
  349. 'guest_team' => $data['guest_team'] ?: 'no_team',
  350. 'lg_id' => $lg_id,
  351. 'status' => $data['status'],
  352. 'match_date' => $data['match_date'] ?: date('Y-m-d'),
  353. 'match_time' => $data['match_time'] ?: date('H:i:s'),
  354. 'is_rollball' => $data['is_rollball'] ?: 0,
  355. 'is_today' => $data['is_today'] ?: 0,
  356. 'is_morningplate' => $data['is_morningplate'] ?: 0,
  357. 'is_stringscene' => $data['is_stringscene'] ?: 0,
  358. 'is_horn' => $data['is_horn'] ?: 0,
  359. 'us_time' => $data['us_time'] ?: commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  360. 'half_match_id' => $half_match_id ?: 0,
  361. 'identity' => $data['uuid'],
  362. ];
  363. //如果是网球 追加rule 字段
  364. if ($game_code == 'wq') {
  365. $set_match['rule'] = $data['rule'];
  366. }
  367. //写入赛事 返回id
  368. $id = $models['model_match']::insertGetId($set_match);
  369. if ($id < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::INSERT_ERROR));
  370. //写关联记录
  371. $set_local = [
  372. 'match_id' => $id,
  373. 'others_match_id' => $data['match_id'],
  374. 'source' => $source,
  375. 'ctime' => date('Y-m-d H:i:s')
  376. ];
  377. $ret = $models['model_local_match']::insertGetId($set_local);
  378. 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'));
  379. //追加写球队名称
  380. $team = [
  381. 'team_h' => $data['home_team'],
  382. 'team_g' => $data['guest_team'],
  383. ];
  384. StTeamModel::setTeam($game_code, $team, $source);
  385. //追加写赛事结果
  386. $opt = [
  387. 'data' => $data,
  388. 'lg_id' => $lg_id,
  389. 'match_id' => $id,
  390. 'models' => $models,
  391. 'gameName' => $gameName,
  392. 'game_code' => $game_code,
  393. ];
  394. ZqResultModel::set_result($opt);
  395. }
  396. }
  397. }
  398. }
  399. $this->writeLog($logdata, Response::success());
  400. //提交事务
  401. DB::commit();
  402. return Response::success();
  403. } catch (\Exception $e) {
  404. //回滚事务
  405. DB::rollBack();
  406. $this->writeLog($logdata, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  407. return $e->getMessage();
  408. }
  409. }
  410. /*
  411. * 写 赛事 结果 记录 数据
  412. * 每个请求 包含 N个联赛 下 N个赛事 结果数据
  413. */
  414. public function setMatchResult($data)
  415. {
  416. $logdata = $data;
  417. try {
  418. //开启事务
  419. DB::beginTransaction();
  420. $obt = $data['data'];
  421. //请求 数据 为空
  422. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  423. $getData = $this->getAddData($obt);
  424. //不是 赛事 结果 记录 数据
  425. if ($getData['title'] != 'match_result_r') throw new \Exception(Response::generate('不是赛事结果记录数据,', Response::ABNORMAL));
  426. /*
  427. //写请求数据 日志记录
  428. if($this->isRecord){
  429. // $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  430. // if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  431. }
  432. */
  433. //获取球类代码
  434. $game_code = $getData['game_code'];
  435. //获取数据源
  436. $source = $getData['source'];
  437. //获取球类名称
  438. $gameName = gameModel::getGameName($game_code);
  439. //获取 model
  440. $models = commonFunction::getModels($game_code, 1);
  441. //获取 赛事 数据
  442. $match_r_data = $getData['data'];
  443. //获取 当前请求 所有 uuid /match_id
  444. $identity = [];
  445. $s_match_ids = [];
  446. $match_uuids = [];
  447. foreach ($match_r_data as $k => $v) {
  448. $identity[] = $v['uuid'];
  449. $s_match_ids[] = $v['match_id'];
  450. }
  451. //====验证 赛事 所属 联赛 是否存在====
  452. $identity = array_unique($identity);
  453. sort($identity);
  454. //获取 本地 已存在 联赛
  455. $l_lg_data = $models['model_local_league']::whereIn('identity', $identity)->select('identity', 'lg_id')->get()->toArray();
  456. //二维数组去重
  457. $l_lg_data = commonFunction::uniquArrV2($l_lg_data, 'identity');
  458. //循环对比 请求uuid->本地identity
  459. foreach ($l_lg_data as $k => $v) {
  460. foreach ($identity as $kk => $uuid) {
  461. if ($v['identity'] == $uuid) {
  462. unset($identity[$kk]);
  463. }
  464. }
  465. }
  466. sort($identity);
  467. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常
  468. if (!empty($identity)) throw new \Exception(Response::generate($gameName . '联赛:uuid-' . $identity[0] . ';', Response::LEAGUE_ERROR));
  469. //====end====
  470. //====验证 赛事结果记录 所属赛事 是否存在====$match_uuids
  471. $s_match_ids = array_unique($s_match_ids);
  472. sort($s_match_ids);
  473. //获取 本地 已存在 赛事
  474. $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();
  475. //二维数组去重
  476. $l_match_data = commonFunction::uniquArrV2($l_match_data, 'others_match_id');
  477. //循环对比 请求match_id->本地others_match_id
  478. foreach ($l_match_data as $k => $v) {
  479. foreach ($s_match_ids as $kk => $s_match_id) {
  480. if ($v['others_match_id'] == $s_match_id) {
  481. unset($s_match_ids[$kk]);
  482. }
  483. }
  484. }
  485. sort($s_match_ids);
  486. //去除本地和请求里都存在的赛事,如果还有剩余赛事id,则返回异常
  487. if (!empty($s_match_ids)) throw new \Exception(Response::generate($gameName . '赛事-match_id' . $s_match_ids[0] . ';', Response::MATCH_ERROR));
  488. //====end====
  489. //处理 赛事 结果记录 数据
  490. foreach ($match_r_data as $k => $v) {
  491. $data = $v;
  492. //获取 本地 联赛 ID
  493. $lg_id = commonFunction::searcharray($data['uuid'], 'identity', $l_lg_data, 'lg_id');
  494. //获取 本地 赛事 ID
  495. $match_id = commonFunction::searcharray($data['match_id'], 'others_match_id', $l_match_data, 'match_id');
  496. //根据球类 获取 赛事结果记录字段
  497. $set_match_r = $this->get_match_r($game_code, $lg_id, $match_id, $data);
  498. //写 赛事 结果 记录
  499. $ret = $models['model_result_record']::insert($set_match_r);
  500. if ($ret != true) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::ADD_MATCH_R_R_ERROR));
  501. }
  502. $this->writeLog($logdata, Response::success());
  503. //提交事务
  504. DB::commit();
  505. return Response::success();
  506. } catch (\Exception $e) {
  507. //回滚事务
  508. DB::rollBack();
  509. $this->writeLog($logdata, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  510. return $e->getMessage();
  511. }
  512. }
  513. //将数组按按指定健名,重新生成新的数组,key在存在并且 值不能重复
  514. private function ArrayKeySet(Array $array, $key)
  515. {
  516. $ret = [];
  517. foreach ($array as $val) {
  518. $ret[$val[$key]] = $val;
  519. }
  520. return $ret;
  521. }
  522. /*
  523. * 写 普通 赔率 数据
  524. * 每个请求 只包含 一场赛事下 N条赔率
  525. */
  526. public function setOdds($data)
  527. {
  528. try {
  529. //开启事务
  530. DB::beginTransaction();
  531. $obt = $data['data'];
  532. //请求 数据 为空
  533. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  534. $getData = $this->getAddData($obt);
  535. //不是 赔率 数据
  536. if ($getData['title'] != 'odds') throw new \Exception(Response::generate('不是赔率数据,', Response::ABNORMAL));
  537. //获取球类代码
  538. $game_code = $getData['game_code'];
  539. //获取数据源
  540. $source = $getData['source'];
  541. //获取源数据联赛ID
  542. $uuid = $getData['uuid'];
  543. //获取源数据赛事ID
  544. $s_match_id = $getData['match_id'];
  545. //获取赔率所属赛事 tag 值 玩法数量
  546. $tag = $getData['tag'] ?: 99;
  547. //获取赔率是否是串场
  548. $is_stringscene = $getData['is_stringscene'];
  549. //获取球类名称
  550. $gameName = gameModel::getGameName($game_code);
  551. //验证本次请求所属联赛/赛事是否存在 返回本地联赛/赛事ID
  552. $models = commonFunction::getModels($game_code, 1);
  553. $lg_id = $this->leagueVerify($models, $uuid, $source, $gameName);
  554. $match_id = $this->matchVerify($models, $s_match_id, $source, $gameName);
  555. //更新赛事 tag 值
  556. //如果未获取到本地赛事id,则返回异常
  557. if (empty($match_id)) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $s_match_id . ';', Response::L_MATCH_ID_ERR));
  558. $upMatch = $models['model_match']::where(['id' => $match_id])
  559. ->update(['tag' => $tag, 'utime' => date('Y-m-d H:i:s')]);
  560. if ($upMatch < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $s_match_id . ';', Response::UPMATCHTAG_ERROR));
  561. //获取 赔率数据
  562. $oddsData = $getData['data'];
  563. //===追加处理赔率过滤===
  564. //获取当前赛事 在本地的所有赔率
  565. $l_odds_data = $models['model_odds']::select('odds_only')->where(['match_id' => $match_id, 'is_stringscene' => $is_stringscene])->get()->toArray();
  566. //获取请求待写入数据
  567. $setOddsData = [];
  568. //获取本地待删除数据
  569. $delOddsData = [];
  570. //本地没有该赛事赔率,则直接全部写入
  571. if (empty($l_odds_data)) {
  572. $setOddsData = $oddsData;
  573. } //本地有该赛事赔率,则分别获取需写入/删除的数据
  574. else {
  575. $newodds_t = $this->ArrayKeySet($oddsData, 'odds_only');
  576. $oldodds_t = $this->ArrayKeySet($l_odds_data, 'odds_only');
  577. foreach ($newodds_t as $key => $val) {
  578. if (isset($oldodds_t[$key])) {
  579. unset($newodds_t[$key]);
  580. unset($oldodds_t[$key]);
  581. }
  582. }
  583. $oddsData = $newodds_t;
  584. $l_odds_data = $oldodds_t;
  585. /*
  586. foreach ($oddsData as $k => $v) {
  587. foreach ($l_odds_data as $kk => $vv) {
  588. if ($v['odds_only'] == $vv['odds_only']) {
  589. unset($oddsData[$k]);
  590. unset($l_odds_data[$kk]);
  591. }
  592. }
  593. }
  594. */
  595. sort($oddsData);
  596. sort($l_odds_data);
  597. //需写入请求数据
  598. $setOddsData = $oddsData;
  599. //需删除本地数据
  600. $delOddsData = $l_odds_data;
  601. }
  602. //===end===
  603. //删除本地不在请求中的数据
  604. if (!empty($delOddsData)) {
  605. //所有需删除数据odds_only
  606. $odds_onlys = [];
  607. foreach ($delOddsData as $k => $v) {
  608. $odds_onlys[] = $v['odds_only'];
  609. }
  610. $del = $models['model_odds']::whereIn('odds_only', $odds_onlys)->delete();
  611. if ($del != count($odds_onlys)) throw new \Exception(Response::generate($gameName, Response::DEL_ODDS_ERR));
  612. }
  613. //写入请求中本地不存在的数据
  614. if (!empty($setOddsData)) {
  615. //处理待写入 赔率记录数据
  616. foreach ($setOddsData as $k => $v) {
  617. //组装数据
  618. $set_odds_r[] = [
  619. 'match_id' => $match_id ?: 0,
  620. 'others_match_id' => $v['match_id'] ?: 0,
  621. 'odds_code' => $v['odds_code'] ?: '',
  622. 'status' => $v['status'] ?: 0,
  623. 'sort' => $v['sort'] ?: 0,
  624. 'p_code' => $v['p_code'] ?: '',
  625. 'odds' => $v['odds'] ?: 0,
  626. 'condition' => $v['condition'],
  627. 'odds_only' => $v['odds_only'] ?: '',
  628. 'source' => $v['source'] ?: '',
  629. 'type' => $v['type'] ?: 0,
  630. 'team' => $v['team'] ?: '',
  631. 'lg_id' => $lg_id,
  632. 'is_stringscene' => $is_stringscene,
  633. 'others_lg_id' => $v['lg_id'],
  634. 'ctime' => date('Y-m-d H:i:s'),
  635. 'utime' => date('Y-m-d H:i:s'),
  636. ];
  637. }
  638. //处理待写入 赔率数据
  639. foreach ($oddsData as $k => $v) {
  640. //组装数据
  641. $set_odds[] = [
  642. 'match_id' => $match_id ?: 0,
  643. 'others_match_id' => $v['match_id'] ?: 0,
  644. 'odds_code' => $v['odds_code'] ?: '',
  645. 'status' => $v['status'] ?: 0,
  646. 'sort' => $v['sort'] ?: 0,
  647. 'p_code' => $v['p_code'] ?: '',
  648. 'odds' => $v['odds'] ?: 0,
  649. 'condition' => $v['condition'],
  650. 'odds_only' => $v['odds_only'] ?: '',
  651. 'source' => $v['source'] ?: '',
  652. 'type' => $v['type'] ?: 0,
  653. 'team' => $v['team'] ?: '',
  654. 'lg_id' => $lg_id,
  655. 'is_stringscene' => $is_stringscene,
  656. 'others_lg_id' => $v['lg_id'],
  657. 'ctime' => date('Y-m-d H:i:s'),
  658. 'utime' => date('Y-m-d H:i:s'),
  659. 'sole' => $v['sole'],
  660. 'expire_time' => date('Y-m-d H:i:s', time() + 60)
  661. ];
  662. }
  663. //写赔率记录数据
  664. $ret = $models['model_odds_record']::insert($set_odds_r);
  665. if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率记录;', Response::ADD_ODDS_R_ERROR));
  666. //写赔率 数据
  667. $ret = $models['model_odds']::insert($set_odds);
  668. if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率;', Response::ADD_ODDS_ERROR));
  669. }
  670. $this->writeLog($data, Response::success());
  671. //提交事务
  672. DB::commit();
  673. return Response::success();
  674. } catch (\Exception $e) {
  675. //回滚事务
  676. DB::rollBack();
  677. $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  678. return $e->getMessage();
  679. }
  680. }
  681. /*
  682. * 写 普通 赔率 数据
  683. * 每个请求 只包含 一场赛事下 N条赔率
  684. */
  685. public function setOdds_old($data)
  686. {
  687. try {
  688. //开启事务
  689. DB::beginTransaction();
  690. $obt = $data['data'];
  691. //请求 数据 为空
  692. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  693. $getData = $this->getAddData($obt);
  694. //不是 赔率 数据
  695. if ($getData['title'] != 'odds') throw new \Exception(Response::generate('不是赔率数据,', Response::ABNORMAL));
  696. /*
  697. //写请求数据 日志记录
  698. if($this->isRecord){
  699. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  700. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  701. }
  702. */
  703. //获取球类代码
  704. $game_code = $getData['game_code'];
  705. //获取数据源
  706. $source = $getData['source'];
  707. //获取源数据联赛ID
  708. $uuid = $getData['uuid'];
  709. //获取源数据赛事ID
  710. $s_match_id = $getData['match_id'];
  711. //获取赔率所属赛事 tag 值 玩法数量
  712. $tag = $getData['tag'] ?: 99;
  713. //获取赔率是否是串场
  714. $is_stringscene = $getData['is_stringscene'];
  715. //获取球类名称
  716. $gameName = gameModel::getGameName($game_code);
  717. //验证本次请求所属联赛/赛事是否存在 返回本地联赛/赛事ID
  718. $models = commonFunction::getModels($game_code, 1);
  719. $lg_id = $this->leagueVerify($models, $uuid, $source, $gameName);
  720. $match_id = $this->matchVerify($models, $s_match_id, $source, $gameName);
  721. //更新赛事 tag 值
  722. //如果未获取到本地赛事id,则返回异常
  723. if (empty($match_id)) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $s_match_id . ';', Response::L_MATCH_ID_ERR));
  724. $upMatch = $models['model_match']::where(['id' => $match_id])
  725. ->update(['tag' => $tag, 'utime' => date('Y-m-d H:i:s')]);
  726. if ($upMatch < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $s_match_id . ';', Response::UPMATCHTAG_ERROR));
  727. //获取 赔率数据
  728. $oddsData = $getData['data'];
  729. //获取 当前 所有 可用 赔率 sole
  730. $odds_only = $getData['odds_only'];
  731. //更新赛事下所有 不在本次请求的赔率 状态
  732. $ret = OddsModel::upOddsStatus($models, $s_match_id, '', $source, $odds_only, $is_stringscene);
  733. if ($ret != true) throw new \Exception(Response::generate($gameName, Response::ODDS_SOLE_ERR));
  734. //获取当前赛事 在本地的所有赔率
  735. $l_odds_data = $models['model_odds']::select('odds_only')->where(['match_id' => $match_id])->get()->toArray();
  736. $set_odds_r = [];
  737. $set_odds = [];
  738. if (!empty($oddsData)) {
  739. //去除本地已有的赔率
  740. if (!empty($l_odds_data)) {
  741. foreach ($oddsData as $k => $v) {
  742. foreach ($l_odds_data as $kk => $vv) {
  743. if ($v['odds_only'] == $vv['odds_only']) {
  744. unset($oddsData[$k]);
  745. }
  746. }
  747. }
  748. sort($oddsData);
  749. }
  750. //处理待写入 赔率记录数据
  751. foreach ($oddsData as $k => $v) {
  752. //组装数据
  753. $set_odds_r[] = [
  754. 'match_id' => $match_id ?: 0,
  755. 'others_match_id' => $v['match_id'] ?: 0,
  756. 'odds_code' => $v['odds_code'] ?: '',
  757. 'status' => $v['status'] ?: 0,
  758. 'sort' => $v['sort'] ?: 0,
  759. 'p_code' => $v['p_code'] ?: '',
  760. 'odds' => $v['odds'] ?: 0,
  761. 'condition' => $v['condition'],
  762. 'odds_only' => $v['odds_only'] ?: '',
  763. 'source' => $v['source'] ?: '',
  764. 'type' => $v['type'] ?: 0,
  765. 'team' => $v['team'] ?: '',
  766. 'lg_id' => $lg_id,
  767. 'is_stringscene' => $is_stringscene,
  768. 'others_lg_id' => $v['lg_id'],
  769. 'ctime' => date('Y-m-d H:i:s'),
  770. 'utime' => date('Y-m-d H:i:s'),
  771. ];
  772. }
  773. //处理待写入 赔率数据
  774. foreach ($oddsData as $k => $v) {
  775. //组装数据
  776. $set_odds[] = [
  777. 'match_id' => $match_id ?: 0,
  778. 'others_match_id' => $v['match_id'] ?: 0,
  779. 'odds_code' => $v['odds_code'] ?: '',
  780. 'status' => $v['status'] ?: 0,
  781. 'sort' => $v['sort'] ?: 0,
  782. 'p_code' => $v['p_code'] ?: '',
  783. 'odds' => $v['odds'] ?: 0,
  784. 'condition' => $v['condition'],
  785. 'odds_only' => $v['odds_only'] ?: '',
  786. 'source' => $v['source'] ?: '',
  787. 'type' => $v['type'] ?: 0,
  788. 'team' => $v['team'] ?: '',
  789. 'lg_id' => $lg_id,
  790. 'is_stringscene' => $is_stringscene,
  791. 'others_lg_id' => $v['lg_id'],
  792. 'ctime' => date('Y-m-d H:i:s'),
  793. 'utime' => date('Y-m-d H:i:s'),
  794. 'sole' => $v['sole'],
  795. 'expire_time' => date('Y-m-d H:i:s', time() + 60)
  796. ];
  797. }
  798. //写赔率记录数据
  799. $ret = $models['model_odds_record']::insert($set_odds_r);
  800. if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率记录;', Response::ADD_ODDS_R_ERROR));
  801. //写赔率 数据
  802. $ret = $models['model_odds']::insert($set_odds);
  803. if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率;', Response::ADD_ODDS_ERROR));
  804. }
  805. $this->writeLog($data, Response::success());
  806. //提交事务
  807. DB::commit();
  808. return Response::success();
  809. } catch (\Exception $e) {
  810. //回滚事务
  811. DB::rollBack();
  812. $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  813. return $e->getMessage();
  814. }
  815. }
  816. /**
  817. * @param Req $data
  818. * @return string
  819. * @throws \App\Lib\Biz\Sport\Exception
  820. * 更新赛事状态
  821. */
  822. public function upMatch($data)
  823. {
  824. $sdata = $data;
  825. try {
  826. //开启事务
  827. DB::beginTransaction();
  828. //获取待更新赛事
  829. $obt = $data['data'];
  830. //请求 数据 为空
  831. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  832. $data = $this->getAddData($obt);
  833. //不是 直播 数据
  834. if ($data['title'] != 'match_status') throw new \Exception(Response::generate('不是更新赛事状态数据', Response::ABNORMAL));
  835. /*
  836. //写请求数据 日志记录
  837. if($this->isRecord){
  838. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  839. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  840. }
  841. */
  842. //获取 球类代码
  843. $game_code = $data['game_code'];
  844. //获取 数据源
  845. $source = $data['source'];
  846. //获取所有数据源赛事 match_id
  847. $others_match_ids = [];
  848. foreach ($data['data'] as $k => $v) {
  849. $others_match_ids[] = $v['match_id'];
  850. }
  851. //根据球类代码 获取model
  852. $model = commonFunction::getModels($game_code, 1);
  853. //获取所有赛事 match_id
  854. $local_match = $model['model_local_match']::SELECT('others_match_id', 'match_id')
  855. ->where(['source' => $source])
  856. ->whereIn('others_match_id', $others_match_ids)
  857. ->get()->toArray();
  858. if (empty($local_match)) throw new \Exception(Response::generate('', Response::MATCHID_NULL));
  859. //更新状态字段
  860. if ($game_code == 'zq') {
  861. foreach ($local_match as $k => $v) {
  862. foreach ($data['data'] as $kk => $vv) {
  863. if ($v['others_match_id'] == $vv['match_id']) {
  864. $set_status = [
  865. 'status' => $vv['status'],
  866. 'is_rollball' => $vv['is_rollball'],
  867. 'is_today' => $vv['is_today'],
  868. 'is_morningplate' => $vv['is_morningplate'],
  869. 'is_stringscene' => $vv['is_stringscene'],
  870. 'is_horn' => $vv['is_horn'],
  871. 'utime' => date('Y-m-d H:i:s'),
  872. ];
  873. $ret = $model['model_match']::where(['id' => $v['match_id']])
  874. ->update($set_status);
  875. if ($ret < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code) . '赛事-match_id:' . $v['others_match_id'], Response::UPSTATUS_ERROR));
  876. }
  877. }
  878. }
  879. } else {
  880. foreach ($local_match as $k => $v) {
  881. foreach ($data['data'] as $kk => $vv) {
  882. if ($v['others_match_id'] == $vv['match_id']) {
  883. $set_status = [
  884. 'status' => $vv['status'],
  885. 'is_rollball' => $vv['is_rollball'],
  886. 'is_today' => $vv['is_today'],
  887. 'is_morningplate' => $vv['is_morningplate'],
  888. 'is_stringscene' => $vv['is_stringscene'],
  889. 'utime' => date('Y-m-d H:i:s'),
  890. ];
  891. $ret = $model['model_match']::where(['id' => $v['match_id']])
  892. ->update($set_status);
  893. if ($ret < 1) throw new \Exception(Response::generate(gameModel::getGameName($game_code) . '赛事-match_id:' . $v['others_match_id'], Response::UPSTATUS_ERROR));
  894. }
  895. }
  896. }
  897. }
  898. $this->writeLog($sdata, Response::success());
  899. //提交事务
  900. DB::commit();
  901. return Response::success();
  902. } catch (\Exception $e) {
  903. //回滚事务
  904. DB::rollBack();
  905. $this->writeLog($sdata, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  906. return $e->getMessage();
  907. }
  908. }
  909. /*
  910. *写 冠军联赛 赔率 数据
  911. *每个请求 包含 N个联赛 下 N条赔率
  912. */
  913. public function setOddsCH_v1($data)
  914. {
  915. try {
  916. //开启事务
  917. DB::beginTransaction();
  918. $obt = $data['data'];
  919. //请求 数据 为空
  920. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  921. $getData = $this->getAddData($obt);
  922. //不是 冠军 赔率 数据
  923. if ($getData['title'] != 'odds_ch') throw new \Exception(Response::generate('不是冠军赔率数据,', Response::ABNORMAL));
  924. //写请求数据 日志记录
  925. if ($this->isRecord) {
  926. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'], $obt, $getData);
  927. if ($setSportsRecord < 1) throw new \Exception(Response::generate('', Response::SPORTS_RECORD_ERR));
  928. }
  929. //获取球类代码
  930. $game_code = $getData['game_code'];
  931. //获取数据源
  932. $source = $getData['source'];
  933. //获取球类名称
  934. $gameName = gameModel::getGameName($game_code);
  935. //获取 model
  936. $models = commonFunction::getModels($game_code, 1);
  937. //获取 赔率数据
  938. $oddsData = $getData['data'];
  939. //获取 当前 所有 可用 赔率 sole
  940. $odds_only = $getData['odds_only'];
  941. //====验证 请求里 所有赔率 所属联赛 是否存在 ====
  942. //获取 当前请求 所有 联赛 uuid
  943. $uuids = [];
  944. $s_lg_ids = [];
  945. foreach ($oddsData as $k => $v) {
  946. $uuids[] = $v['uuid'];
  947. $s_lg_ids[] = $v['lg_id'];
  948. }
  949. $s_lg_ids = array_unique($s_lg_ids);
  950. $uuids = array_unique($uuids);
  951. sort($uuids);
  952. sort($s_lg_ids);
  953. //更新联赛下所有 不在本次请求的赔率 状态
  954. $ret = OddsModel::upOddsStatus($models, '', $s_lg_ids, $source, $odds_only);
  955. if ($ret != true) throw new \Exception(Response::generate($gameName, Response::ODDS_SOLE_ERR));
  956. //获取 本地 已存在 联赛
  957. $l_lg_data = $models['model_local_league']::whereIn('identity', $uuids)->select('identity', 'lg_id')->get()->toArray();
  958. //二维数组去重
  959. $l_lg_data = commonFunction::uniquArrV2($l_lg_data, 'identity');
  960. //循环对比 请求uuid->本地uuid
  961. foreach ($l_lg_data as $k => $v) {
  962. foreach ($uuids as $kk => $uuid) {
  963. if ($v['identity'] == $uuid) {
  964. unset($uuids[$kk]);
  965. }
  966. }
  967. }
  968. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常;
  969. if (!empty($uuids)) throw new \Exception(Response::generate($gameName . '冠军联赛:uuid-' . $uuids[0] . ';', Response::LEAGUE_ERROR));
  970. //====end====
  971. //赔率记录数据
  972. $set_odds_r = [];
  973. //赔率数据
  974. $set_odds = [];
  975. if (!empty($oddsData)) {
  976. foreach ($oddsData as $k => $v) {
  977. //获取 本地 联赛 ID
  978. $lg_id = commonFunction::searcharray($v['uuid'], 'identity', $l_lg_data, 'lg_id');
  979. //组装赔率记录数据
  980. $set_odds_r[] = [
  981. 'match_id' => 0,
  982. 'others_match_id' => $v['match_id'] ?: 0,
  983. 'odds_code' => $v['odds_code'] ?: '',
  984. 'status' => $v['status'] ?: 0,
  985. 'sort' => $v['sort'] ?: 0,
  986. 'p_code' => $v['p_code'] ?: '',
  987. 'odds' => $v['odds'] ?: 0,
  988. 'condition' => $v['condition'],
  989. 'odds_only' => $v['odds_only'] ?: '',
  990. 'source' => $v['source'] ?: '',
  991. 'type' => $v['type'] ?: 1,
  992. 'team' => $v['team'] ?: '',
  993. 'lg_id' => $lg_id,
  994. 'others_lg_id' => $v['lg_id'],
  995. 'ctime' => date('Y-m-d H:i:s'),
  996. 'utime' => date('Y-m-d H:i:s'),
  997. ];
  998. }
  999. foreach ($oddsData as $k => $v) {
  1000. //获取 本地 联赛 ID
  1001. $lg_id = commonFunction::searcharray($v['uuid'], 'identity', $l_lg_data, 'lg_id');
  1002. //组装赔率数据
  1003. $set_odds[] = [
  1004. 'match_id' => 0,
  1005. 'others_match_id' => $v['match_id'] ?: 0,
  1006. 'odds_code' => $v['odds_code'] ?: '',
  1007. 'status' => $v['status'] ?: 0,
  1008. 'sort' => $v['sort'] ?: 0,
  1009. 'p_code' => $v['p_code'] ?: '',
  1010. 'odds' => $v['odds'] ?: 0,
  1011. 'condition' => $v['condition'],
  1012. 'odds_only' => $v['odds_only'] ?: '',
  1013. 'source' => $v['source'] ?: '',
  1014. 'type' => $v['type'] ?: 1,
  1015. 'team' => $v['team'] ?: '',
  1016. 'lg_id' => $lg_id,
  1017. 'others_lg_id' => $v['lg_id'],
  1018. 'ctime' => date('Y-m-d H:i:s'),
  1019. 'utime' => date('Y-m-d H:i:s'),
  1020. 'sole' => $v['sole'],
  1021. 'expire_time' => date('Y-m-d H:i:s', time() + 60)
  1022. ];
  1023. }
  1024. //写赔率记录数据
  1025. $ret = $models['model_odds_record']::insert($set_odds_r);
  1026. if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率记录;', Response::ADD_ODDS_R_ERROR));
  1027. //写赔率 数据
  1028. $ret = $models['model_odds']::insert($set_odds);
  1029. if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率;', Response::ADD_ODDS_ERROR));
  1030. }
  1031. $this->writeLog($data, Response::success());
  1032. //提交事务
  1033. DB::commit();
  1034. return Response::success();
  1035. } catch (\Exception $e) {
  1036. //回滚事务
  1037. DB::rollBack();
  1038. $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  1039. return $e->getMessage();
  1040. }
  1041. }
  1042. /*
  1043. *写 冠军联赛 赔率 数据
  1044. *每个请求 包含 1个联赛 下 N条赔率
  1045. *追加赔率过滤
  1046. */
  1047. public function setOddsCH($data)
  1048. {
  1049. try {
  1050. //开启事务
  1051. DB::beginTransaction();
  1052. $obt = $data['data'];
  1053. //请求 数据 为空
  1054. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  1055. $getData = $this->getAddData($obt);
  1056. //不是 冠军 赔率 数据
  1057. if ($getData['title'] != 'odds_ch') throw new \Exception(Response::generate('不是冠军赔率数据,', Response::ABNORMAL));
  1058. /*
  1059. //写请求数据 日志记录
  1060. if($this->isRecord){
  1061. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  1062. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  1063. }
  1064. */
  1065. //获取球类代码
  1066. $game_code = $getData['game_code'];
  1067. //获取数据源
  1068. $source = $getData['source'];
  1069. //获取球类名称
  1070. $gameName = gameModel::getGameName($game_code);
  1071. //获取数据源联赛ID
  1072. $s_lg_id = $getData['lg_id'];
  1073. //获取源数据联赛UUID
  1074. $uuid = $getData['uuid'];
  1075. //获取 model
  1076. $models = commonFunction::getModels($game_code, 1);
  1077. //获取 赔率数据
  1078. $oddsData = $getData['data'];
  1079. //验证本次请求所属联赛/赛事是否存在 返回本地联赛/赛事ID
  1080. $lg_id = $this->leagueVerify($models, $uuid, $source, $gameName);
  1081. //===追加处理赔率过滤===
  1082. //获取当前联赛 在本地的所有赔率
  1083. $l_odds_data = $models['model_odds']::select('odds_only')->where(['lg_id' => $lg_id, 'type' => 1])->get()->toArray();
  1084. //获取请求待写入数据
  1085. $setOddsData = [];
  1086. //获取本地待删除数据
  1087. $delOddsData = [];
  1088. //本地没有该赛事赔率,则直接全部写入
  1089. if (empty($l_odds_data)) {
  1090. $setOddsData = $oddsData;
  1091. } //本地有该赛事赔率,则分别获取需写入/删除的数据
  1092. else {
  1093. $newodds_t = $this->ArrayKeySet($oddsData, 'odds_only');
  1094. $oldodds_t = $this->ArrayKeySet($l_odds_data, 'odds_only');
  1095. foreach ($newodds_t as $key => $val) {
  1096. if (isset($oldodds_t[$key])) {
  1097. unset($newodds_t[$key]);
  1098. unset($oldodds_t[$key]);
  1099. }
  1100. }
  1101. $oddsData = $newodds_t;
  1102. $l_odds_data = $oldodds_t;
  1103. sort($oddsData);
  1104. sort($l_odds_data);
  1105. //需写入请求数据
  1106. $setOddsData = $oddsData;
  1107. //需删除本地数据
  1108. $delOddsData = $l_odds_data;
  1109. }
  1110. //===end===
  1111. //删除本地不在请求中的数据
  1112. if (!empty($delOddsData)) {
  1113. //所有需删除数据odds_only
  1114. $odds_onlys = [];
  1115. foreach ($delOddsData as $k => $v) {
  1116. $odds_onlys[] = $v['odds_only'];
  1117. }
  1118. $del = $models['model_odds']::whereIn('odds_only', $odds_onlys)->delete();
  1119. if ($del != count($odds_onlys)) throw new \Exception(Response::generate($gameName, Response::DEL_ODDS_ERR));
  1120. }
  1121. //写入请求中本地不存在的数据
  1122. if (!empty($setOddsData)) {
  1123. foreach ($setOddsData as $k => $v) {
  1124. //组装赔率记录数据
  1125. $set_odds_r[] = [
  1126. 'match_id' => 0,
  1127. 'others_match_id' => $v['match_id'] ?: 0,
  1128. 'odds_code' => $v['odds_code'] ?: '',
  1129. 'status' => $v['status'] ?: 0,
  1130. 'sort' => $v['sort'] ?: 0,
  1131. 'p_code' => $v['p_code'] ?: '',
  1132. 'odds' => $v['odds'] ?: 0,
  1133. 'condition' => $v['condition'],
  1134. 'odds_only' => $v['odds_only'] ?: '',
  1135. 'source' => $v['source'] ?: '',
  1136. 'type' => $v['type'] ?: 1,
  1137. 'team' => $v['team'] ?: '',
  1138. 'lg_id' => $lg_id,
  1139. 'others_lg_id' => $v['lg_id'],
  1140. 'ctime' => date('Y-m-d H:i:s'),
  1141. 'utime' => date('Y-m-d H:i:s'),
  1142. ];
  1143. }
  1144. foreach ($oddsData as $k => $v) {
  1145. //组装赔率数据
  1146. $set_odds[] = [
  1147. 'match_id' => 0,
  1148. 'others_match_id' => $v['match_id'] ?: 0,
  1149. 'odds_code' => $v['odds_code'] ?: '',
  1150. 'status' => $v['status'] ?: 0,
  1151. 'sort' => $v['sort'] ?: 0,
  1152. 'p_code' => $v['p_code'] ?: '',
  1153. 'odds' => $v['odds'] ?: 0,
  1154. 'condition' => $v['condition'],
  1155. 'odds_only' => $v['odds_only'] ?: '',
  1156. 'source' => $v['source'] ?: '',
  1157. 'type' => $v['type'] ?: 1,
  1158. 'team' => $v['team'] ?: '',
  1159. 'lg_id' => $lg_id,
  1160. 'others_lg_id' => $v['lg_id'],
  1161. 'ctime' => date('Y-m-d H:i:s'),
  1162. 'utime' => date('Y-m-d H:i:s'),
  1163. 'sole' => $v['sole'],
  1164. 'expire_time' => date('Y-m-d H:i:s', time() + 60)
  1165. ];
  1166. }
  1167. //写赔率记录数据
  1168. $ret = $models['model_odds_record']::insert($set_odds_r);
  1169. if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率记录;', Response::ADD_ODDS_R_ERROR));
  1170. //写赔率 数据
  1171. $ret = $models['model_odds']::insert($set_odds);
  1172. if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率;', Response::ADD_ODDS_ERROR));
  1173. }
  1174. $this->writeLog($data, Response::success());
  1175. //提交事务
  1176. DB::commit();
  1177. return Response::success();
  1178. } catch (\Exception $e) {
  1179. //回滚事务
  1180. DB::rollBack();
  1181. $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  1182. return $e->getMessage();
  1183. }
  1184. }
  1185. /*
  1186. * 写入直播 数据
  1187. */
  1188. public function setBroadCast($data)
  1189. {
  1190. $data_s = $data['data'];
  1191. try {
  1192. //开启事务
  1193. DB::beginTransaction();
  1194. $obt = $data['data'];
  1195. //请求 数据 为空
  1196. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  1197. $getData = $this->getAddData($obt);
  1198. //不是 直播 数据
  1199. if ($getData['title'] != 'broad_cast') throw new \Exception(Response::generate('不是直播数据,', Response::ABNORMAL));
  1200. //写请求数据 日志记录
  1201. if ($this->isRecord) {
  1202. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'], $obt, $getData);
  1203. if ($setSportsRecord < 1) throw new \Exception(Response::generate('', Response::SPORTS_RECORD_ERR));
  1204. }
  1205. //获取 球类
  1206. $game_code = $getData['game_code'];
  1207. //删除当前球类已存在的直播数据
  1208. $del = broadcastModel::where('game_code', $game_code)->delete();
  1209. if ($del != true) throw new \Exception(Response::generate('', Response::DEL_PAST_BROADCAST_ERR));
  1210. //获取 直播 数据
  1211. $BroadCast = $getData['data'];
  1212. $set_broadcast = [];
  1213. foreach ($BroadCast as $k => $data) {
  1214. //组装数据
  1215. $set_broadcast[] = [
  1216. "doing" => $data['doing'],
  1217. "game_type" => $data['game_type'],
  1218. "game_code" => $game_code,
  1219. "guest_team" => $data['guest_team'],
  1220. "host_team" => $data['host_team'],
  1221. "league_name" => $data['league_name'],
  1222. "shower" => $data['shower'],
  1223. "showid" => (int)$data['showid'],
  1224. "start_time" => $data['start_time'],
  1225. "ctime" => date('Y-m-d H:i:s'),
  1226. "utime" => date('Y-m-d H:i:s'),
  1227. ];
  1228. }
  1229. //写入 直播 数据
  1230. $ret = broadcastModel::insert($set_broadcast);
  1231. if ($ret == false) throw new \Exception(Response::generate('', Response::BROADCAST_ERROR));
  1232. $this->writeLog($data_s, Response::success());
  1233. //提交事务
  1234. DB::commit();
  1235. return Response::success();
  1236. } catch (\Exception $e) {
  1237. //回滚事务
  1238. DB::rollBack();
  1239. $this->writeLog($data_s, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  1240. return $e->getMessage();
  1241. }
  1242. }
  1243. /*
  1244. * 验证所属 联赛 是否存在
  1245. */
  1246. public function leagueVerify($models = [], $uuid = '', $source = '', $gameName = '')
  1247. {
  1248. if (empty($models) || empty($uuid)) throw new \Exception(Response::generate('', Response::ABNORMAL));
  1249. $l_lg_id = $models['model_local_league']::where(['identity' => $uuid])->value('lg_id');
  1250. if ($l_lg_id < 1) throw new \Exception(Response::generate($gameName . '联赛:lg_id-' . $uuid, Response::LEAGUE_ERROR));
  1251. return $l_lg_id;
  1252. }
  1253. /*
  1254. * 验证所属 赛事 是否存在
  1255. */
  1256. public function matchVerify($models = [], $s_match_id = '', $source = '', $gameName = '')
  1257. {
  1258. if (empty($models) || empty($s_match_id)) throw new \Exception(Response::generate('', Response::ABNORMAL));
  1259. $l_match_id = $models['model_local_match']::where(['others_match_id' => $s_match_id, 'source' => $source])->value('match_id');
  1260. if ($l_match_id < 1) throw new \Exception(Response::generate($gameName . '赛事:match_id-' . $s_match_id, Response::MATCH_ERROR));
  1261. return $l_match_id;
  1262. }
  1263. /*
  1264. * 写入直播 数据
  1265. */
  1266. public function broadcast($opt = [], $game_code = '')
  1267. {
  1268. $data = $opt['data'];
  1269. $set_broadcast = [
  1270. "doing" => $data['doing'],
  1271. "game_type" => $data['game_type'],
  1272. "game_code" => $game_code,
  1273. "guest_team" => $data['guest_team'],
  1274. "host_team" => $data['host_team'],
  1275. "league_name" => $data['league_name'],
  1276. "shower" => $data['shower'],
  1277. "showid" => (int)$data['showid'],
  1278. "start_time" => $data['start_time'],
  1279. "ctime" => date('Y-m-d H:i:s'),
  1280. "utime" => date('Y-m-d H:i:s'),
  1281. ];
  1282. $ret = broadcastModel::insert($set_broadcast);
  1283. if ($ret == false) throw new \Exception(Response::generate('', Response::BROADCAST_ERROR));
  1284. }
  1285. /*
  1286. * 写入 赛事 初始 结果
  1287. */
  1288. public function match_result($game_code = '')
  1289. {
  1290. $model = commonFunction::getModels($game_code, 1);
  1291. if ($game_code == 'zq') ZqResultModel::ZQresult($model);
  1292. if ($game_code == 'lq') LqResultModel::LQresult($model);
  1293. if ($game_code == 'wq') WqResultModel::WQresult($model);
  1294. if ($game_code == 'bq') BqResultModel::BQresult($model);
  1295. }
  1296. /*
  1297. * 写入联赛数据
  1298. */
  1299. public function league($opt = [], $gameName = '')
  1300. {
  1301. $game_code = $opt['game_code'];
  1302. //根据球类代码获取相关model
  1303. $model = commonFunction::getModels($game_code, 1);
  1304. $data = $opt['data'];
  1305. //查询联赛是否已存在
  1306. $id = $model['model_league']::where('name_chinese', '=', $data['name_chinese'])
  1307. ->value('id');
  1308. //默认获取本年最后一天
  1309. $last_time = date('Y-12-31 23:59:59');
  1310. //决赛时间
  1311. if ($data['last_time']) {
  1312. $last_time = $data['last_time'];
  1313. }
  1314. $set_lg['name_chinese'] = $data['name_chinese'];
  1315. $set_lg['kind'] = $data['kind'];
  1316. $set_lg['match_mode'] = $data['match_mode'];
  1317. $set_lg['if_stop'] = $data['if_stop'];
  1318. $set_lg['identity'] = $data['uuid'] ?: '';
  1319. $set_lg['last_time'] = $last_time;
  1320. $set_lg['utime'] = date('Y-m-d H:i:s');
  1321. if (empty($id)) {
  1322. //写入联赛
  1323. $id = $model['model_league']::insertGetId($set_lg);
  1324. $m_lg_id = $id;
  1325. if ($m_lg_id < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $data['lg_id'] . ';', Response::INSERT_ERROR));
  1326. } else {
  1327. //更新联赛
  1328. $ret = $model['model_league']::where(['id' => $id])
  1329. ->update($set_lg);
  1330. if ($ret < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $data['lg_id'] . ';', Response::UPDATE_ERROR));
  1331. }
  1332. $set_local = [
  1333. 'lg_id' => $id,
  1334. 'others_lg_id' => $data['lg_id'],
  1335. 'source' => $data['source'],
  1336. 'ctime' => date('Y-m-d H:i:s')
  1337. ];
  1338. $ret = $model['model_local_league']::insertGetId($set_local);
  1339. 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'));
  1340. //执行成功返回源数据 联赛ID
  1341. $data = ['others_lg_id' => $data['lg_id'], 'lg_id' => $id];
  1342. return $data;
  1343. }
  1344. /*
  1345. * 写入赛事数据
  1346. */
  1347. public function competition($opt = [], $gameName = '', $lg_id = '')
  1348. {
  1349. $game_code = $opt['game_code'];
  1350. //根据球类代码获取相关model
  1351. $model = commonFunction::getModels($game_code, 1);
  1352. $data = $opt['data'];
  1353. if (empty($data['lg_id'])) throw new \Exception(Response::generate('', Response::LEAGUE_ERROR));//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  1354. //查询赛事是否存在
  1355. $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']])
  1356. ->value('id');
  1357. $half_match_id = 0;
  1358. //如果有上半场赛事id 获取上半场赛事是否存在
  1359. if (!empty($data['half_match_id'])) {
  1360. $half_match_id = $match_id = $model['model_local_match']::where(['others_match_id' => $data['half_match_id'], 'source' => $data['source']])
  1361. ->value('match_id');
  1362. 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'));
  1363. }
  1364. //如果赛事没有开始日期,则为冠军盘口赛事
  1365. if (empty($data['match_date'])) {
  1366. //冠军盘口赛事获取所属联赛结束时间
  1367. $last_time = $model['model_league']::where(['id' => $lg_id])
  1368. ->value('last_time');
  1369. 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'));
  1370. //给冠军盘口赛事 赋值时间
  1371. $time = strtotime($last_time);
  1372. $data['match_date'] = date('Y-m-d', $time);
  1373. $data['match_time'] = date('H:i:s', $time);
  1374. }
  1375. $set_match = [
  1376. 'ctime' => date('Y-m-d H:i:s'),
  1377. 'utime' => date('Y-m-d H:i:s'),
  1378. 'expire_time' => date('Y-m-d H:i:s', time() + 60),
  1379. 'home_team' => $data['home_team'] ?: '',
  1380. 'guest_team' => $data['guest_team'] ?: 'no_team',
  1381. 'lg_id' => $lg_id,
  1382. 'status' => $data['status'],
  1383. 'match_date' => $data['match_date'] ?: date('Y-m-d'),
  1384. 'match_time' => $data['match_time'] ?: date('H:i:s'),
  1385. 'tag' => $data['tag'] ?: 0,
  1386. 'is_rollball' => $data['is_rollball'] ?: 0,
  1387. 'is_today' => $data['is_today'] ?: 0,
  1388. 'is_morningplate' => $data['is_morningplate'] ?: 0,
  1389. 'is_stringscene' => $data['is_stringscene'] ?: 0,
  1390. 'us_time' => $data['us_time'] ?: commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  1391. 'half_match_id' => $half_match_id ?: 0,
  1392. 'identity' => $data['uuid'],
  1393. ];
  1394. //写入赛事
  1395. if (empty($id)) {
  1396. //写入赛事 返回id
  1397. $id = $model['model_match']::insertGetId($set_match);
  1398. if ($id < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::INSERT_ERROR));
  1399. } else {
  1400. //更新赛事
  1401. $ret = $model['model_match']::where(['id' => $id])->update($set_match);
  1402. if ($ret < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::UPDATE_ERROR));
  1403. }
  1404. //写关联记录
  1405. $set_local = [
  1406. 'match_id' => $id,
  1407. 'others_match_id' => $data['match_id'],
  1408. 'source' => $data['source'],
  1409. 'ctime' => date('Y-m-d H:i:s')
  1410. ];
  1411. $ret = $model['model_local_match']::insertGetId($set_local);
  1412. 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'));
  1413. //返回 源数据 赛事ID
  1414. $data = ['others_match_id' => $data['match_id'], 'match_id' => $id];
  1415. return $data;
  1416. }
  1417. /*
  1418. * 写入赔率/赔率记录数据
  1419. */
  1420. public function odds($opt = [], $gameName = '', $lg_id = 0, $match_id = 0, $oddsID = '', $oddsRecordID = '')
  1421. {
  1422. $game_code = $opt['game_code'];
  1423. //根据球类代码获取相关model
  1424. $model = commonFunction::getModels($game_code, 1);
  1425. $data = $opt['data'];
  1426. //获取赛事 本地/源ID
  1427. $others_match_id = $data['match_id'];
  1428. //获取联赛 本地/源ID
  1429. $others_lg_id = $data['lg_id'];
  1430. //===写赔率记录===
  1431. $set_odds_r = [
  1432. 'match_id' => $match_id ?: 0,
  1433. 'others_match_id' => $others_match_id ?: 0,
  1434. 'odds_code' => $data['odds_code'] ?: '',
  1435. 'status' => $data['status'] ?: 0,
  1436. 'sort' => $data['sort'] ?: 0,
  1437. 'p_code' => $data['p_code'] ?: '',
  1438. 'odds' => $data['odds'] ?: 0,
  1439. 'condition' => $data['condition'],
  1440. 'odds_only' => $data['odds_only'] ?: '',
  1441. 'source' => $data['source'] ?: '',
  1442. 'type' => $data['type'] ?: 0,
  1443. 'team' => $data['team'] ?: '',
  1444. 'lg_id' => $lg_id,
  1445. 'others_lg_id' => $others_lg_id,
  1446. 'ctime' => date('Y-m-d H:i:s'),
  1447. ];
  1448. //更新或写入赔率记录
  1449. if ($oddsRecordID != false) {
  1450. $ret = $model['model_odds_record']::where(['id' => $oddsRecordID])
  1451. ->update($set_odds_r);
  1452. 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'));
  1453. } else {
  1454. $ret = $model['model_odds_record']::insert($set_odds_r);
  1455. 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'));
  1456. }
  1457. //===写赔率===
  1458. $set_odds = [
  1459. 'match_id' => $match_id ?: 0,
  1460. 'others_match_id' => $others_match_id ?: 0,
  1461. 'odds_code' => $data['odds_code'] ?: '',
  1462. 'status' => $data['status'] ?: 0,
  1463. 'sort' => $data['sort'] ?: 0,
  1464. 'p_code' => $data['p_code'] ?: '',
  1465. 'odds' => $data['odds'] ?: 0,
  1466. 'condition' => $data['condition'],
  1467. 'odds_only' => $data['odds_only'] ?: '',
  1468. 'sole' => $data['sole'] ?: '',
  1469. 'source' => $data['source'] ?: '',
  1470. 'type' => $data['type'] ?: 0,
  1471. 'team' => $data['team'] ?: '',
  1472. 'lg_id' => $lg_id,
  1473. 'others_lg_id' => $others_lg_id,
  1474. 'ctime' => date('Y-m-d H:i:s'),
  1475. 'utime' => date('Y-m-d H:i:s'),
  1476. 'expire_time' => date('Y-m-d H:i:s', time() + 60),
  1477. ];
  1478. //更新或写入赔率数据
  1479. if ($oddsID != false) {
  1480. $ret = $model['model_odds']::where(['id' => $oddsID])
  1481. ->update($set_odds);
  1482. 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'));
  1483. } else {
  1484. $ret = $model['model_odds']::insert($set_odds);
  1485. 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'));
  1486. }
  1487. //===end===
  1488. }
  1489. /*
  1490. * 更新 赛事下 所有不在本次请求的 赔率状态
  1491. */
  1492. public function upOddsStatus($opt = [], $gameName = '')
  1493. {
  1494. //获取球类代码
  1495. $game_code = $opt['game_code'];
  1496. //获取球类model
  1497. $model = commonFunction::getModels($game_code, 1);
  1498. //赛事下 所有 赔率sole
  1499. $odds_sole = $opt['data'];
  1500. //数据源 赛事 id
  1501. $others_match_id = $opt['match_id'];
  1502. //数据源
  1503. $source = $opt['source'];
  1504. //更新赛事下所有 不在本次请求的赔率 状态
  1505. $ret = OddsModel::getMatchSole($model, $others_match_id, '', $source, $odds_sole);
  1506. if ($ret != true) throw new \Exception(Response::generate($gameName, Response::ODDS_SOLE_ERR));
  1507. }
  1508. /*
  1509. * 写入联赛结果
  1510. */
  1511. public function league_result($opt, $gameName)
  1512. {
  1513. $game_code = $opt['game_code'];
  1514. //根据球类代码获取相关model
  1515. $model = commonFunction::getModels($game_code, 1);
  1516. $data = $opt['data'];
  1517. //验证结果所属联赛
  1518. $lg_id = $model['model_local_league']::where(['others_lg_id' => $data['lg_id'], 'source' => $data['source']])
  1519. ->value('lg_id');
  1520. if ($lg_id < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $data['lg_id'] . ';', Response::LEAGUE_ERROR));
  1521. $lg_result_id = $model['model_league_result']::where(['lg_id' => $lg_id, 'game_name' => $data['game_name']])
  1522. ->value('id');
  1523. $set_lg_result = [
  1524. 'lg_id' => $lg_id,
  1525. 'game_name' => $data['game_name'],
  1526. 'result' => json_encode($data['result'], JSON_UNESCAPED_UNICODE),
  1527. 'status' => $data['status'],
  1528. 'ctime' => date('Y-m-d H:i:s'),
  1529. 'utime' => date('Y-m-d H:i:s'),
  1530. ];
  1531. //联赛结果数据处理
  1532. if (!empty($lg_result_id)) {
  1533. $ret = $model['model_league_result']::where(['id' => $lg_result_id])
  1534. ->update($set_lg_result);
  1535. 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'));
  1536. } else {
  1537. $ret = $model['model_league_result']::insert($set_lg_result);
  1538. 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'));
  1539. }
  1540. }
  1541. /*
  1542. * 写入赛事结果
  1543. */
  1544. public function com_result($opt = [], $gameName = '', $lg_id = '', $match_id = '')
  1545. {
  1546. $game_code = $opt['game_code'];
  1547. //根据球类代码获取相关model
  1548. $model = commonFunction::getModels($game_code, 1);
  1549. $data = $opt['data'];
  1550. //查询结果是否存在
  1551. $match_r_id = $model['model_result']::where(['match_id' => $match_id])
  1552. ->value('id');
  1553. $set_match_r = [
  1554. "home_team" => $data['home_team'],
  1555. "guest_team" => $data['guest_team'],
  1556. "lg_id" => $lg_id,
  1557. "home_rate" => $data['home_rate'],
  1558. "guest_rate" => $data['guest_rate'],
  1559. "home_score" => $data['home_score'],
  1560. "guest_score" => $data['guest_score'],
  1561. "all_goal" => $data['all_goal'],
  1562. "status" => $data['status'],
  1563. "first_score" => $data['first_score'],
  1564. "last_score" => $data['last_score'],
  1565. "match_score" => $data['match_score'],
  1566. "match_winer" => $data['match_winer'],
  1567. "match_time" => $data['match_time'],
  1568. "match_process" => $data['match_process'],
  1569. "tag" => $data['tag'],
  1570. "match_id" => $match_id,
  1571. "u_home_score" => $data['u_home_score'],
  1572. "u_guest_score" => $data['u_guest_score'],
  1573. "p_code" => $data['p_code'],
  1574. "update_time" => date('Y-m-d H:i:s')
  1575. ];
  1576. //赛事结果数据处理
  1577. if (!empty($match_r_id)) {
  1578. $ret = $model['model_result']::where(['id' => $match_r_id])
  1579. ->update($set_match_r);
  1580. 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'));
  1581. } else {
  1582. $ret = $model['model_result']::insert($set_match_r);
  1583. 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'));
  1584. }
  1585. // return Response::success();
  1586. }
  1587. //写入赛事结果记录
  1588. public function com_result_record($opt = [], $gameName = '', $lg_id = '', $match_id = '')
  1589. {
  1590. $game_code = $opt['game_code'];
  1591. //根据球类代码获取相关model
  1592. $model = commonFunction::getModels($game_code, 1);
  1593. $data = $opt['data'];
  1594. $match_r_id = $model['model_result_record']::where(['match_id' => $match_id, 'match_time' => $data['match_time']])
  1595. ->value('id');
  1596. //根据球类 获取 赛事结果记录字段
  1597. $set_match_r = $this->get_match_r($game_code, $lg_id, $match_id, $data);
  1598. //赛事结果记录处理
  1599. if ($match_r_id > 0) {
  1600. $ret = $model['model_result_record']::where(['id' => $match_r_id])
  1601. ->update($set_match_r);
  1602. 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'));
  1603. } else {
  1604. $ret = $model['model_result_record']::insert($set_match_r);
  1605. 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'));
  1606. }
  1607. }
  1608. /**
  1609. * @param $data
  1610. * @return mixed
  1611. * json转数组
  1612. */
  1613. public function getAddData($data)
  1614. {
  1615. $data = json_decode($data, true);
  1616. return $data;
  1617. }
  1618. /*
  1619. * 根据球类获取 赛事结果记录字段
  1620. */
  1621. public function get_match_r($game_code, $lg_id, $match_id, $data)
  1622. {
  1623. $set_match_r = [];
  1624. if ($game_code == 'zq') {
  1625. $set_match_r = [
  1626. "home_team" => $data['home_team'] ?: '',
  1627. "guest_team" => $data['guest_team'] ?: '',
  1628. "lg_id" => $lg_id,
  1629. "all_goal" => $data['all_goal'] ?: 0,
  1630. "home_rate" => $data['home_rate'] ?: 0,
  1631. "guest_rate" => $data['guest_rate'] ?: 0,
  1632. "home_score" => $data['home_score'] ?: 0,
  1633. "guest_score" => $data['guest_score'] ?: 0,
  1634. "status" => $data['status'] ?: 0,
  1635. "first_score" => $data['first_score'] ?: 0,
  1636. "last_score" => $data['last_score'] ?: 0,
  1637. "match_score" => $data['match_score'] ?: 0,
  1638. "match_winer" => $data['match_winer'] ?: '',
  1639. "match_time" => $data['match_time'] ?: 0,
  1640. "match_process" => $data['match_process'] ?: '',
  1641. // "tag"=> $data['tag']?:0,
  1642. "match_id" => $match_id,
  1643. "update_time" => date('Y-m-d H:i:s')
  1644. ];
  1645. };
  1646. if ($game_code == 'lq') {
  1647. $set_match_r = [
  1648. "home_team" => $data['home_team'] ?: '',
  1649. "guest_team" => $data['guest_team'] ?: '',
  1650. "lg_id" => $lg_id,
  1651. "home_rate" => $data['home_rate'] ?: 0,
  1652. "guest_rate" => $data['guest_rate'] ?: 0,
  1653. "home_score" => $data['home_score'] ?: 0,
  1654. "guest_score" => $data['guest_score'] ?: 0,
  1655. "status" => $data['status'] ?: 0,
  1656. "first_score" => $data['first_score'] ?: 0,
  1657. "last_score" => $data['last_score'] ?: 0,
  1658. "match_score" => $data['match_score'] ?: 0,
  1659. "match_winer" => $data['match_winer'] ?: '',
  1660. "match_time" => $data['match_time'] ?: 0,
  1661. "match_process" => $data['match_process'] ?: '',
  1662. // "tag"=> $data['tag']?:0,
  1663. "match_id" => $match_id,
  1664. "update_time" => date('Y-m-d H:i:s'),
  1665. "result_mark" => json_encode($data['result_mark']) ?: '',
  1666. ];
  1667. }
  1668. if ($game_code == 'wq') {
  1669. $set_match_r = [
  1670. "home_player_name" => $data['home_player_name'] ?: '',
  1671. "guest_player_name" => $data['guest_player_name'] ?: '',
  1672. "lg_id" => $lg_id,
  1673. "home_player_let_plate" => $data['home_player_let_plate'] ?: 0,
  1674. "guest_player_let_plate" => $data['guest_player_let_plate'] ?: 0,
  1675. "home_player_let_inning" => $data['home_player_let_inning'] ?: 0,
  1676. "guest_player_let_inning" => $data['guest_player_let_inning'] ?: 0,
  1677. "all_inning" => $data['all_inning'] ?: 0,
  1678. "home_player_score" => $data['home_player_score'] ?: 0,
  1679. "guest_player_score" => $data['guest_player_score'] ?: 0,
  1680. "status" => $data['status'] ?: 0,
  1681. "first_score_player" => $data['first_score_player'] ?: '',
  1682. "last_score_player" => $data['last_score_player'] ?: '',
  1683. "first_inning_score" => $data['first_inning_score'] ?: 0,
  1684. "second_inning_score" => $data['second_inning_score'] ?: 0,
  1685. "third_inning_score" => $data['third_inning_score'] ?: 0,
  1686. "match_winer_player" => $data['match_winer_player'] ?: '',
  1687. "update_time" => date('Y-m-d H:i:s'),
  1688. "match_time" => $data['match_time'] ?: 0,
  1689. "match_process" => $data['match_process'] ?: '',
  1690. // "tag"=>$data['tag']?:0,
  1691. "match_id" => $match_id,
  1692. "result_mark" => json_encode($data['result_mark']) ?: '',
  1693. ];
  1694. }
  1695. if ($game_code == 'bq') {
  1696. $set_match_r = [
  1697. "home_team" => $data['home_team'] ?: '',
  1698. "guest_team" => $data['guest_team'] ?: '',
  1699. "lg_id" => $lg_id,
  1700. "home_rate" => $data['home_rate'] ?: 0,
  1701. "guest_rate" => $data['guest_rate'] ?: 0,
  1702. "home_score" => $data['home_score'] ?: 0,
  1703. "guest_score" => $data['guest_score'] ?: 0,
  1704. "status" => $data['status'] ?: 0,
  1705. "first_score" => $data['first_score'] ?: 0,
  1706. "last_score" => $data['last_score'] ?: 0,
  1707. "match_score" => $data['match_score'] ?: 0,
  1708. "match_winer" => $data['match_winer'] ?: '',
  1709. "match_time" => $data['match_time'] ?: 0,
  1710. "match_process" => $data['match_process'] ?: '',
  1711. // "tag"=> $data['tag']?:0,
  1712. "match_id" => $match_id,
  1713. "all_inning" => $data['all_inning'] ?: 9,
  1714. "update_time" => date('Y-m-d H:i:s'),
  1715. "result_mark" => json_encode($data['result_mark']) ?: '',
  1716. ];
  1717. }
  1718. return $set_match_r;
  1719. }
  1720. /**
  1721. * 根据联赛名称 获取 国家 地区
  1722. */
  1723. public function getArea($leagueName = '')
  1724. {
  1725. //如果联赛名称有括号,去除
  1726. $leagueName = preg_replace('/\(.*?\)/', '', $leagueName);
  1727. //获取所有国家/地区
  1728. $areaData = StAreaCountryModel::select('id', 'pid', 'name')->get()->toArray();
  1729. //获取当前联赛所属国家
  1730. if (strpos($leagueName, 'NBA') !== false) {
  1731. $data = [
  1732. 'id' => 247,
  1733. 'pid' => 0,
  1734. 'name' => '世界'
  1735. ];
  1736. return $data;
  1737. }
  1738. //识别本地 国家地区数据
  1739. foreach ($areaData as $v => $k) {
  1740. if (strpos($leagueName, $k['name']) !== false) {
  1741. return $k;
  1742. }
  1743. }
  1744. //如果不能识别,则返回未知
  1745. $data = [
  1746. 'id' => 0,
  1747. 'pid' => 0,
  1748. 'name' => '未知国家或地区'
  1749. ];
  1750. return $data;
  1751. }
  1752. //写文本日志
  1753. private function writeLog($body, $ret)
  1754. {
  1755. go(function () use ($body, $ret) {
  1756. $json_data = json_encode($body, JSON_UNESCAPED_UNICODE);
  1757. $data = json_decode($body['data'], true);
  1758. $game_code = isset($data['game_code']) ? $data['game_code'] : '';
  1759. $title = isset($data['title']) ? $data['title'] : '';
  1760. $msg = is_string($ret) ? $ret : json_encode($ret, 256);
  1761. $now = explode(" ", microtime());
  1762. $wdata = date("Y-m-d", $now[1]);
  1763. $path = LOG_PATH . DS . $wdata . DS . $game_code . DS;
  1764. if (!file_exists($path)) {
  1765. $ret = mkdir($path, '0755', true);
  1766. if (!$ret) {
  1767. echo "$path --- Log File Create false \n";
  1768. return;
  1769. }
  1770. }
  1771. $lasttxt = date('Y-m-d H:i:s', $now[1]) . substr($now[0], 1, 5) . ' - ' . $msg . ' - ' . $json_data . "\n\n";
  1772. $file = $path . DS . $game_code . '_' . $title . '.log';
  1773. file_put_contents($file, $lasttxt, FILE_APPEND | LOCK_EX);
  1774. return;
  1775. });
  1776. return;
  1777. }
  1778. private function writeLog_db($body, $ret)
  1779. {
  1780. if (!is_string($body)) {
  1781. $body = json_encode([$body], 256);
  1782. }
  1783. if (!is_string($ret)) {
  1784. $ret = json_encode([$ret], 256);
  1785. }
  1786. $data = ['ctime' => date("Y-m-d H:i:s"), 'body' => $body, 'ret' => $ret];
  1787. $return = DB::table('datainf_log')->insert($data);
  1788. return $return;
  1789. }
  1790. }