DataLogic.php 85 KB

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