| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/26
- * Time: 17:10
- */
- namespace App\Logic;
- use datainf\pplus\Instance;
- use App\Http\Response\Response;
- use App\Lib\ModelBase;
- use Illuminate\Database\Capsule\Manager as DB;
- use App\Lib\Biz\Sport\Common as commonFunction;
- use App\Http\Model\StBqResult as BqResultModel;
- use App\Http\Model\StBroadcast as broadcastModel;
- use App\Http\Model\StGameType as gameModel;
- use App\Http\Model\StLqResult as LqResultModel;
- use App\Http\Model\StWqResult as WqResultModel;
- use App\Http\Model\StZqLocalLeague as LeagueModel;
- use App\Http\Model\StZqLocalMatch as MatchModel;
- use App\Http\Model\StZqOdds as OddsModel;
- use App\Http\Model\StZqOddsRecord as OddsRecordModel;
- use App\Http\Model\StZqResult as ZqResultModel;
- use App\Http\Model\St_area_country as StAreaCountryModel;
- use App\Http\Model\St_set_sports_record as St_set_sports_recordModel;
- use App\Http\Model\St_team as StTeamModel;
- use App\Models\SportsNoteList as SportsNoteListModel;
- use App\Http\Model\StZqMatch as StMatchModel;
- class DataLogic
- {
- use Instance;
- private $isRecord = 0;
- /*
- * 写 联赛 数据
- * 每个请求 包含 N个联赛
- */
- public function setLeague($data)
- {
- try {
- //开启事务
- DB::beginTransaction();
- $obt = $data['data'];
- //请求 数据 为空
- if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
- $getData = $this->getAddData($obt);
- //不是 联赛 数据
- if ($getData['title'] != 'league') throw new \Exception(Response::generate('不是联赛数据,', Response::ABNORMAL));
- /*
- //写请求数据 日志记录
- if($this->isRecord){
- commonFunction::SET_SPORTS_RECORD($obt,$getData['title']);
- $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
- if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
- }
- */
- //获取球类代码
- $game_code = $getData['game_code'];
- //获取数据源
- $source = $getData['source'];
- //获取球类名称
- $gameName = gameModel::getGameName($game_code);
- //获取 model
- $models = commonFunction::getModels($game_code, 1);
- //获取 联赛 数据
- $leagueData = $getData['data'];
- //获取 当前请求 所有 联赛 uuid
- $identity = [];
- foreach ($leagueData as $k => $v) {
- $identity[] = $v['uuid'];
- }
- $identity = array_unique($identity);
- sort($identity);
- //获取 本地 已存在 联赛
- $l_lg_data = $models['model_local_league']::whereIn('identity', $identity)->select('identity', 'lg_id')->get()->toArray();
- //二维数组去重
- $l_lg_data = commonFunction::uniquArrV2($l_lg_data, 'identity');
- //循环对比 请求identity->本地identity
- foreach ($l_lg_data as $k => $v) {
- foreach ($identity as $kk => $uuid) {
- if ($v['identity'] == $uuid) {
- unset($identity[$kk]);
- }
- }
- }
- //去除本地和请求里都存在的联赛,如果还有剩余联赛uuid,则将其写入
- if (!empty($identity)) {
- //没有 决赛时间 默认获取本年最后一天
- $last_time = date('Y-12-31 23:59:59');
- foreach ($leagueData as $k => $v) {
- foreach ($identity as $kk => $uuid) {
- if ($v['uuid'] == $uuid) {
- //如有 决赛时间
- if ($v['last_time']) {
- $last_time = $v['last_time'];
- }
- //获取联赛所属国家或地区
- $getArea = $this->getArea($v['name_chinese']);
- if ($getArea['pid'] == 0) {
- $area_id = $getArea['id'];
- $country_id = 0;
- } else {
- $area_id = $getArea['pid'];
- $country_id = $getArea['id'];
- }
- if (empty($v['uuid'])) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $v['lg_id'] . ';', Response::LG__UUID_NULL));
- $set_lg['name_chinese'] = $v['name_chinese'];
- $set_lg['kind'] = $v['kind'];
- $set_lg['match_mode'] = $v['match_mode'];
- $set_lg['if_stop'] = $v['if_stop'];
- $set_lg['identity'] = $v['uuid'];
- $set_lg['last_time'] = $last_time;
- $set_lg['utime'] = date('Y-m-d H:i:s');
- $set_lg['area_id'] = $area_id ?: 247;
- $set_lg['country_id'] = $country_id ?: 0;
- //写入联赛
- $id = $models['model_league']::insertGetId($set_lg);
- $m_lg_id = $id;
- if ($m_lg_id < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $v['lg_id'] . ';', Response::INSERT_ERROR));
- //写入 本地 联赛记录
- $set_local = [
- 'lg_id' => $id,
- 'others_lg_id' => $v['lg_id'],
- 'source' => $source,
- 'ctime' => date('Y-m-d H:i:s'),
- 'identity' => $v['uuid'],
- ];
- $ret = $models['model_local_league']::insertGetId($set_local);
- 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'));
- }
- }
- }
- }
- $this->writeLog($data, Response::success());
- //提交事务
- DB::commit();
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
- return $e->getMessage();
- }
- }
- //写赛果接口
- public function setResultExpress($data)
- {
- $ret = $this->setResultExpress_v1($data);
- return $ret;
- }
- /**
- * 写赛果数据
- */
- public function setResultExpress_v1($data)
- {
- try {
- //开启事务
- DB::beginTransaction();
- $obt = $data['data'];
- //请求 数据 为空
- if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
- $getData = $this->getAddData($obt);
- //不是 赛事 结果 记录 数据
- if ($getData['title'] != 'result') throw new \Exception(Response::generate('不是赛果数据,', Response::ABNORMAL));
- //获取球类代码
- $game_code = $getData['game_code'];
- //获取数据源
- $source = $getData['source'];
- //获取球类名称
- $gameName = gameModel::getGameName($game_code);
- //获取 model
- $models = commonFunction::getModels($game_code, 1);
- //获取 赛事 数据
- $match_r_data = $getData['data'];
- if (!empty($match_r_data)) {
- foreach ($match_r_data as $k => $v) {
- $del = $models['model_result_express']::where('match_id', $v['match_id'])->delete();
- //==处理 赛事时间==
- // dd($v['match_date'].' '.$v['match_time']);
- $match_time_o = strtotime($v['match_date'] . ' ' . $v['match_time']);//原始美东时间 时间戳
- $match_time_new = strtotime(commonFunction::qgmdate('Y-m-d H:i:s', $match_time_o, 20));//本地时间 时间戳 20=8+12
- $match_date = date('Y-m-d', $match_time_new);
- $match_time = date('H:i:s', $match_time_new);
- //==end==
- $v['match_date'] = $match_date;
- $v['match_time'] = $match_time;
- $v['c_time'] = date('Y-m-d H:i:s', time());
- $v['u_time'] = date('Y-m-d H:i:s', time());
- $v['source'] = $source;
- $v['play_data'] = json_encode($v['play_data'], JSON_UNESCAPED_UNICODE);
- $ret = $models['model_result_express']::insert($v);
- if ($ret != true) throw new \Exception(Response::generate($gameName . ';', Response::SET_MATCH_R_ERR));
- }
- //写 赛事 结果 记录
- }
- $this->writeLog($data, Response::success());
- //提交事务
- DB::commit();
- //写请求数据 日志记录
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
- return $e->getMessage();
- }
- }
- /*
- * 写 赛事 数据
- * 每个请求 包含 N个联赛 下 N个赛事
- */
- public function setMatch($data)
- {
- $logdata = $data;
- try {
- //开启事务
- DB::beginTransaction();
- $obt = $data['data'];
- //请求 数据 为空
- if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
- $getData = $this->getAddData($obt);
- //不是 赛事 数据
- if ($getData['title'] != 'match') throw new \Exception(Response::generate('不是赛事数据,', Response::ABNORMAL));
- /*
- //写请求数据 日志记录
- if($this->isRecord){
- $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
- if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
- }
- */
- //获取球类代码
- $game_code = $getData['game_code'];
- //获取数据源
- $source = $getData['source'];
- //获取球类名称
- $gameName = gameModel::getGameName($game_code);
- //获取 model
- $models = commonFunction::getModels($game_code, 1);
- //获取 赛事 数据
- $matchData = $getData['data'];
- //获取 当前请求 所有 uuid
- $identity = [];
- $s_match_ids = [];
- $match_uuids = [];
- foreach ($matchData as $k => $v) {
- $identity[] = $v['uuid'];
- $s_match_ids[] = $v['match_id'];
- }
- //====验证 赛事 所属 联赛 是否存在====
- $identity = array_unique($identity);
- sort($identity);
- //获取 本地 已存在 联赛
- $l_lg_data = $models['model_local_league']::whereIn('identity', $identity)->select('identity', 'lg_id', 'others_lg_id')->get()->toArray();
- //二维数组去重
- $l_lg_data = commonFunction::uniquArrV2($l_lg_data, 'identity');
- //如果获取不到本地联赛数据,则返回联赛不存在
- if (empty($l_lg_data)) throw new \Exception(Response::generate($gameName . '本次请求的所有赛事都没有联赛数据;', Response::LEAGUE_ERROR));
- //获取到本次请求,所有本地有联赛的赛事
- $mathData_before = $matchData;//接收原赛事
- $matchData = [];//获取新赛事
- foreach ($l_lg_data as $k => $v) {
- foreach ($mathData_before as $kk => $vv) {
- if ($v['identity'] == $vv['uuid']) {
- $matchData[] = $vv;
- }
- }
- }
- //获取当前请求有关联联赛的赛事 match_id
- $s_match_ids = [];
- foreach ($matchData as $k => $v) {
- $s_match_ids[] = $v['match_id'];
- //追加获取赛事uuid
- $match_uuids[] = $v['match_identity'];
- }
- //====获取 本地 已存在 赛事====
- $match_uuids = array_unique($match_uuids);
- sort($match_uuids);
- //$l_match_data = $models['model_local_match']::whereIn('identity', $match_uuids)->where('source', $source)->select('match_id', 'identity')->get()->toArray();
- $l_match_data = $models['model_local_match']::whereIn('identity', $match_uuids)->select('match_id', 'identity')->get()->toArray();
- //二维数组去重
- $l_match_data = commonFunction::uniquArrV2($l_match_data, 'identity');
- //循环对比 请求match_id->本地others_match_id
- //如果本地有 赛事,则分别获取本地已存在/不存在赛事
- if (!empty($l_match_data)) {
- //本地已存在赛事,用于更新
- $matchData_y = [];
- foreach ($l_match_data as $k => $v) {
- foreach ($match_uuids as $kk => $match_identity) {
- if ($v['identity'] == $match_identity) {
- $matchData_y[$k]['match_identity'] = $match_identity;
- $matchData_y[$k]['match_id'] = $v['match_id'];
- unset($match_uuids[$kk]);
- }
- }
- }
- sort($match_uuids);
- }
- //如果 请求中 有本地赛事,则执行更新
- if (!empty($matchData_y)) {
- foreach ($matchData as $k => $v) {
- foreach ($matchData_y as $kk => $vv) {
- if ($v['match_identity'] == $vv['match_identity']) {
- $data = $v;
- $up_match = [];
- $up_match['utime'] = date('Y-m-d H:i:s');
- $up_match['status'] = $data['status'];
- //获取需更新字段
- if ($data['is_rollball'] == 1) {
- $up_match['is_rollball'] = $data['is_rollball'];
- }
- if ($data['is_today'] == 1) {
- $up_match['is_today'] = $data['is_today'];
- }
- if ($data['is_morningplate'] == 1) {
- $up_match['is_morningplate'] = $data['is_morningplate'];
- }
- if ($data['is_stringscene'] == 1) {
- $up_match['is_stringscene'] = $data['is_stringscene'];
- }
- //追加更新开赛日期/时间
- $up_match['match_date'] = $data['match_date'];
- $up_match['match_time'] = $data['match_time'];
- $up_match['us_time'] = $data['us_time'];
- //如果是网球 追加rule 字段
- if ($game_code == 'wq' and !empty($data['rule'])) {
- $up_match['rule'] = $data['rule'];
- }
- //更新赛事 返回成功数量
- $set = $models['model_match']::where(['id' => $vv['match_id']])
- ->update($up_match);
- if ($set < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::UPSTATUS_ERROR));
- }
- }
- }
- }
- //剩余 赛事 数据 写入
- if (!empty($match_uuids)) {
- foreach ($matchData as $k => $v) {
- foreach ($match_uuids as $kk => $match_identity) {
- if ($v['match_identity'] == $match_identity) {
- $data = $v;
- //===追加查询 该赛事 本地是否存在 同联赛同球队 记录
- //查询赛事 如果存在 则更新为 已取消
- $matchData = $models['model_match']::where(['home_team' => $data['home_team'], 'guest_team' => $data['guest_team'], 'identity' => $data['uuid']])
- ->update(['status' => 6, 'utime' => date('Y-m-d H:i:s'), 'handle_mark' => '重复赛事取消']);
- //===end====
- $half_match_id = $v['half_match_id'];
- //获取 本地 联赛 ID
- //验证本赛事是否有数据源lg_id
- if (empty($data['lg_id'])) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::S_LG_ID_NULL));
- $lg_id = commonFunction::searcharray($data['uuid'], 'identity', $l_lg_data, 'lg_id');
- //验证 本赛事是否获取到本地lg_id
- if (empty($lg_id)) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::L_LG_ID_NULL));
- //如果赛事没有开始日期,则为冠军盘口赛事
- if (empty($data['match_date'])) {
- //冠军盘口赛事获取所属联赛结束时间
- $last_time = $models['model_league']::where(['id' => $lg_id])
- ->value('last_time');
- 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'));
- //给冠军盘口赛事 赋值时间
- $time = strtotime($last_time);
- $data['match_date'] = date('Y-m-d', $time);
- $data['match_time'] = date('H:i:s', $time);
- }
- $set_match = [
- 'ctime' => date('Y-m-d H:i:s'),
- 'utime' => date('Y-m-d H:i:s'),
- 'expire_time' => date('Y-m-d H:i:s', time() + 60),
- 'home_team' => $data['home_team'] ?: '',
- 'guest_team' => $data['guest_team'] ?: 'no_team',
- 'lg_id' => $lg_id,
- 'status' => $data['status'],
- 'match_date' => $data['match_date'] ?: date('Y-m-d'),
- 'match_time' => $data['match_time'] ?: date('H:i:s'),
- 'is_rollball' => $data['is_rollball'] ?: 0,
- 'is_today' => $data['is_today'] ?: 0,
- 'is_morningplate' => $data['is_morningplate'] ?: 0,
- 'is_stringscene' => $data['is_stringscene'] ?: 0,
- 'is_horn' => $data['is_horn'] ?: 0,
- 'us_time' => $data['us_time'] ?: commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
- 'half_match_id' => $half_match_id ?: 0,
- 'identity' => $data['uuid'],
- 'match_identity' => $data['match_identity'],
- ];
- //如果是网球 追加rule 字段
- if ($game_code == 'wq') {
- $set_match['rule'] = $data['rule'];
- }
- //写入赛事 返回id
- $id = $models['model_match']::insertGetId($set_match);
- if ($id < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::INSERT_ERROR));
- //写关联记录
- $set_local = [
- 'match_id' => $id,
- 'others_match_id' => $data['match_id'],
- 'source' => $source,
- 'identity' => $data['match_identity'],
- 'ctime' => date('Y-m-d H:i:s')
- ];
- $ret = $models['model_local_match']::insertGetId($set_local);
- if ($ret < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::LOCAL_MATCH_ERROR));
- //追加写球队名称
- $team = [
- 'team_h' => $data['home_team'],
- 'team_g' => $data['guest_team'],
- ];
- StTeamModel::setTeam($game_code, $team, $source);
- //追加写赛事结果
- $opt = [
- 'data' => $data,
- 'lg_id' => $lg_id,
- 'match_id' => $id,
- 'models' => $models,
- 'gameName' => $gameName,
- 'game_code' => $game_code,
- ];
- ZqResultModel::set_result($opt);
- }
- }
- }
- }
- $this->writeLog($logdata, Response::success());
- //提交事务
- DB::commit();
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- $this->writeLog($logdata, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
- return $e->getMessage();
- }
- }
- /*
- * 写 赛事 结果 记录 数据
- * 每个请求 包含 N个联赛 下 N个赛事 结果数据
- */
- public function setMatchResult($data)
- {
- $logdata = $data;
- try {
- //开启事务
- DB::beginTransaction();
- $obt = $data['data'];
- //请求 数据 为空
- if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
- $getData = $this->getAddData($obt);
- //不是 赛事 结果 记录 数据
- if ($getData['title'] != 'match_result_r') throw new \Exception(Response::generate('不是赛事结果记录数据,', Response::ABNORMAL));
- /*
- //写请求数据 日志记录
- if($this->isRecord){
- $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
- if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
- }
- */
- //获取球类代码
- $game_code = $getData['game_code'];
- //获取数据源
- $source = $getData['source'];
- //获取球类名称
- $gameName = gameModel::getGameName($game_code);
- //获取 model
- $models = commonFunction::getModels($game_code, 1);
- //获取 赛事 数据
- $match_r_data = $getData['data'];
- //获取 当前请求 所有 uuid /match_id
- $identity = [];
- $s_match_ids = [];
- $match_uuids = [];
- foreach ($match_r_data as $k => $v) {
- $identity[] = $v['uuid'];
- $s_match_ids[] = $v['match_id'];
- //追加获取赛事uuid
- $match_uuids[] = $v['match_identity'];
- }
- //====验证 赛事 所属 联赛 是否存在====
- $identity = array_unique($identity);
- sort($identity);
- //获取 本地 已存在 联赛
- $l_lg_data = $models['model_local_league']::whereIn('identity', $identity)->select('identity', 'lg_id')->get()->toArray();
- //二维数组去重
- $l_lg_data = commonFunction::uniquArrV2($l_lg_data, 'identity');
- //循环对比 请求uuid->本地identity
- foreach ($l_lg_data as $k => $v) {
- foreach ($identity as $kk => $uuid) {
- if ($v['identity'] == $uuid) {
- unset($identity[$kk]);
- }
- }
- }
- sort($identity);
- //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常
- if (!empty($identity)) throw new \Exception(Response::generate($gameName . '联赛:uuid-' . $identity[0] . ';', Response::LEAGUE_ERROR));
- //====end====
- //====验证 赛事结果记录 所属赛事 是否存在====
- $match_uuids = array_unique($match_uuids);
- sort($match_uuids);
- //获取 本地 已存在 赛事
- // $l_match_data = $models['model_local_match']::whereIn('identity', $match_uuids)->where('source', $source)->select('identity', 'match_id')->get()->toArray();
- $l_match_data = $models['model_local_match']::whereIn('identity', $match_uuids)->select('identity', 'match_id')->get()->toArray();
- //二维数组去重
- $l_match_data = commonFunction::uniquArrV2($l_match_data, 'identity');
- //循环对比 请求identity->本地identity
- foreach ($l_match_data as $k => $v) {
- foreach ($match_uuids as $kk => $match_identity) {
- if ($v['identity'] == $match_identity) {
- unset($match_uuids[$kk]);
- }
- }
- }
- sort($match_uuids);
- //去除本地和请求里都存在的赛事,如果还有剩余赛事id,则返回异常
- if (!empty($match_uuids)) throw new \Exception(Response::generate($gameName . '赛事-match_id' . $match_uuids[0] . ';', Response::MATCH_ERROR));
- //====end====
- //处理 赛事 结果记录 数据
- foreach ($match_r_data as $k => $v) {
- $data = $v;
- //获取 本地 联赛 ID
- $lg_id = commonFunction::searcharray($data['uuid'], 'identity', $l_lg_data, 'lg_id');
- //获取 本地 赛事 ID
- $match_id = commonFunction::searcharray($data['match_identity'], 'identity', $l_match_data, 'match_id');
- //根据球类 获取 赛事结果记录字段
- $set_match_r = $this->get_match_r($game_code, $lg_id, $match_id, $data);
- //写 赛事 结果 记录
- $ret = $models['model_result_record']::insert($set_match_r);
- if ($ret != true) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::ADD_MATCH_R_R_ERROR));
- }
- $this->writeLog($logdata, Response::success());
- //提交事务
- DB::commit();
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- $this->writeLog($logdata, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
- return $e->getMessage();
- }
- }
- //将数组按按指定健名,重新生成新的数组,key在存在并且 值不能重复
- private function ArrayKeySet(Array $array, $key)
- {
- $ret = [];
- foreach ($array as $val) {
- $ret[$val[$key]] = $val;
- }
- return $ret;
- }
- /*
- * 写 普通 赔率 数据
- * 每个请求 只包含 一场赛事下 N条赔率
- */
- public function setOdds($data)
- {
- try {
- //开启事务
- DB::beginTransaction();
- $obt = $data['data'];
- //请求 数据 为空
- if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
- $getData = $this->getAddData($obt);
- //不是 赔率 数据
- if ($getData['title'] != 'odds') throw new \Exception(Response::generate('不是赔率数据,', Response::ABNORMAL));
- //获取球类代码
- $game_code = $getData['game_code'];
- //获取数据源
- $source = $getData['source'];
- //获取源数据联赛ID
- $uuid = $getData['uuid'];
- //获取源数据赛事ID
- $s_match_id = $getData['match_id'];
- //获取赔率所属赛事 tag 值 玩法数量
- $tag = $getData['tag'] ?: 99;
- //获取赔率是否是串场
- $is_stringscene = $getData['is_stringscene'];
- //获取数据源赛事uuid
- $match_identity = $getData['match_identity'];
- //获取球类名称
- $gameName = gameModel::getGameName($game_code);
- //验证本次请求所属联赛/赛事是否存在 返回本地联赛/赛事ID
- $models = commonFunction::getModels($game_code, 1);
- $lg_id = $this->leagueVerify($models, $uuid, $source, $gameName);
- $match_id = $this->matchVerify_v2($models, $match_identity, $source, $gameName);
- //更新赛事 tag 值
- //如果未获取到本地赛事id,则返回异常
- if (empty($match_id)) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $s_match_id . ';', Response::L_MATCH_ID_ERR));
- $upMatch = $models['model_match']::where(['id' => $match_id])
- ->update(['tag' => $tag, 'utime' => date('Y-m-d H:i:s')]);
- if ($upMatch < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $s_match_id . ';', Response::UPMATCHTAG_ERROR));
- //获取 赔率数据
- $oddsData = $getData['data'];
- //===追加处理赔率过滤===
- //获取当前赛事 在本地的所有赔率
- $l_odds_data = $models['model_odds']::select('odds_only')->where(['match_id' => $match_id, 'is_stringscene' => $is_stringscene])->get()->toArray();
- //获取请求待写入数据
- $setOddsData = [];
- //获取本地待删除数据
- $delOddsData = [];
- //本地没有该赛事赔率,则直接全部写入
- if (empty($l_odds_data)) {
- $setOddsData = $oddsData;
- } //本地有该赛事赔率,则分别获取需写入/删除的数据
- else {
- $newodds_t = $this->ArrayKeySet($oddsData, 'odds_only');
- $oldodds_t = $this->ArrayKeySet($l_odds_data, 'odds_only');
- foreach ($newodds_t as $key => $val) {
- if (isset($oldodds_t[$key])) {
- unset($newodds_t[$key]);
- unset($oldodds_t[$key]);
- }
- }
- $oddsData = $newodds_t;
- $l_odds_data = $oldodds_t;
- /*
- foreach ($oddsData as $k => $v) {
- foreach ($l_odds_data as $kk => $vv) {
- if ($v['odds_only'] == $vv['odds_only']) {
- unset($oddsData[$k]);
- unset($l_odds_data[$kk]);
- }
- }
- }
- */
- sort($oddsData);
- sort($l_odds_data);
- //需写入请求数据
- $setOddsData = $oddsData;
- //需删除本地数据
- $delOddsData = $l_odds_data;
- }
- //===end===
- //删除本地不在请求中的数据
- if (!empty($delOddsData)) {
- //所有需删除数据odds_only
- $odds_onlys = [];
- foreach ($delOddsData as $k => $v) {
- $odds_onlys[] = $v['odds_only'];
- }
- $del = $models['model_odds']::whereIn('odds_only', $odds_onlys)->delete();
- if ($del != count($odds_onlys)) throw new \Exception(Response::generate($gameName, Response::DEL_ODDS_ERR));
- }
- //写入请求中本地不存在的数据
- if (!empty($setOddsData)) {
- //处理待写入 赔率记录数据
- foreach ($setOddsData as $k => $v) {
- //组装数据
- $set_odds_r[] = [
- 'match_id' => $match_id ?: 0,
- 'others_match_id' => $v['match_id'] ?: 0,
- 'odds_code' => $v['odds_code'] ?: '',
- 'status' => $v['status'] ?: 0,
- 'sort' => $v['sort'] ?: 0,
- 'p_code' => $v['p_code'] ?: '',
- 'odds' => $v['odds'] ?: 0,
- 'condition' => $v['condition'],
- 'odds_only' => $v['odds_only'] ?: '',
- 'source' => $v['source'] ?: '',
- 'type' => $v['type'] ?: 0,
- 'team' => $v['team'] ?: '',
- 'lg_id' => $lg_id,
- 'is_stringscene' => $is_stringscene,
- 'others_lg_id' => $v['lg_id'],
- 'ctime' => date('Y-m-d H:i:s'),
- 'utime' => date('Y-m-d H:i:s'),
- ];
- }
- //处理待写入 赔率数据
- foreach ($oddsData as $k => $v) {
- //组装数据
- $set_odds[] = [
- 'match_id' => $match_id ?: 0,
- 'others_match_id' => $v['match_id'] ?: 0,
- 'odds_code' => $v['odds_code'] ?: '',
- 'status' => $v['status'] ?: 0,
- 'sort' => $v['sort'] ?: 0,
- 'p_code' => $v['p_code'] ?: '',
- 'odds' => $v['odds'] ?: 0,
- 'condition' => $v['condition'],
- 'odds_only' => $v['odds_only'] ?: '',
- 'source' => $v['source'] ?: '',
- 'type' => $v['type'] ?: 0,
- 'team' => $v['team'] ?: '',
- 'lg_id' => $lg_id,
- 'is_stringscene' => $is_stringscene,
- 'others_lg_id' => $v['lg_id'],
- 'ctime' => date('Y-m-d H:i:s'),
- 'utime' => date('Y-m-d H:i:s'),
- 'sole' => $v['sole'],
- 'expire_time' => date('Y-m-d H:i:s', time() + 60)
- ];
- }
- //写赔率记录数据
- $ret = $models['model_odds_record']::insert($set_odds_r);
- if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率记录;', Response::ADD_ODDS_R_ERROR));
- //写赔率 数据
- $ret = $models['model_odds']::insert($set_odds);
- if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率;', Response::ADD_ODDS_ERROR));
- }
- $this->writeLog($data, Response::success());
- //提交事务
- DB::commit();
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
- return $e->getMessage();
- }
- }
- /*
- * 写 普通 赔率 数据
- * 每个请求 只包含 一场赛事下 N条赔率
- */
- public function setOdds_old($data)
- {
- try {
- //开启事务
- DB::beginTransaction();
- $obt = $data['data'];
- //请求 数据 为空
- if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
- $getData = $this->getAddData($obt);
- //不是 赔率 数据
- if ($getData['title'] != 'odds') throw new \Exception(Response::generate('不是赔率数据,', Response::ABNORMAL));
- /*
- //写请求数据 日志记录
- if($this->isRecord){
- $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
- if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
- }
- */
- //获取球类代码
- $game_code = $getData['game_code'];
- //获取数据源
- $source = $getData['source'];
- //获取源数据联赛ID
- $uuid = $getData['uuid'];
- //获取源数据赛事ID
- $s_match_id = $getData['match_id'];
- //获取赔率所属赛事 tag 值 玩法数量
- $tag = $getData['tag'] ?: 99;
- //获取赔率是否是串场
- $is_stringscene = $getData['is_stringscene'];
- //获取球类名称
- $gameName = gameModel::getGameName($game_code);
- //验证本次请求所属联赛/赛事是否存在 返回本地联赛/赛事ID
- $models = commonFunction::getModels($game_code, 1);
- $lg_id = $this->leagueVerify($models, $uuid, $source, $gameName);
- $match_id = $this->matchVerify($models, $s_match_id, $source, $gameName);
- //更新赛事 tag 值
- //如果未获取到本地赛事id,则返回异常
- if (empty($match_id)) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $s_match_id . ';', Response::L_MATCH_ID_ERR));
- $upMatch = $models['model_match']::where(['id' => $match_id])
- ->update(['tag' => $tag, 'utime' => date('Y-m-d H:i:s')]);
- if ($upMatch < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $s_match_id . ';', Response::UPMATCHTAG_ERROR));
- //获取 赔率数据
- $oddsData = $getData['data'];
- //获取 当前 所有 可用 赔率 sole
- $odds_only = $getData['odds_only'];
- //更新赛事下所有 不在本次请求的赔率 状态
- $ret = OddsModel::upOddsStatus($models, $s_match_id, '', $source, $odds_only, $is_stringscene);
- if ($ret != true) throw new \Exception(Response::generate($gameName, Response::ODDS_SOLE_ERR));
- //获取当前赛事 在本地的所有赔率
- $l_odds_data = $models['model_odds']::select('odds_only')->where(['match_id' => $match_id])->get()->toArray();
- $set_odds_r = [];
- $set_odds = [];
- if (!empty($oddsData)) {
- //去除本地已有的赔率
- if (!empty($l_odds_data)) {
- foreach ($oddsData as $k => $v) {
- foreach ($l_odds_data as $kk => $vv) {
- if ($v['odds_only'] == $vv['odds_only']) {
- unset($oddsData[$k]);
- }
- }
- }
- sort($oddsData);
- }
- //处理待写入 赔率记录数据
- foreach ($oddsData as $k => $v) {
- //组装数据
- $set_odds_r[] = [
- 'match_id' => $match_id ?: 0,
- 'others_match_id' => $v['match_id'] ?: 0,
- 'odds_code' => $v['odds_code'] ?: '',
- 'status' => $v['status'] ?: 0,
- 'sort' => $v['sort'] ?: 0,
- 'p_code' => $v['p_code'] ?: '',
- 'odds' => $v['odds'] ?: 0,
- 'condition' => $v['condition'],
- 'odds_only' => $v['odds_only'] ?: '',
- 'source' => $v['source'] ?: '',
- 'type' => $v['type'] ?: 0,
- 'team' => $v['team'] ?: '',
- 'lg_id' => $lg_id,
- 'is_stringscene' => $is_stringscene,
- 'others_lg_id' => $v['lg_id'],
- 'ctime' => date('Y-m-d H:i:s'),
- 'utime' => date('Y-m-d H:i:s'),
- ];
- }
- //处理待写入 赔率数据
- foreach ($oddsData as $k => $v) {
- //组装数据
- $set_odds[] = [
- 'match_id' => $match_id ?: 0,
- 'others_match_id' => $v['match_id'] ?: 0,
- 'odds_code' => $v['odds_code'] ?: '',
- 'status' => $v['status'] ?: 0,
- 'sort' => $v['sort'] ?: 0,
- 'p_code' => $v['p_code'] ?: '',
- 'odds' => $v['odds'] ?: 0,
- 'condition' => $v['condition'],
- 'odds_only' => $v['odds_only'] ?: '',
- 'source' => $v['source'] ?: '',
- 'type' => $v['type'] ?: 0,
- 'team' => $v['team'] ?: '',
- 'lg_id' => $lg_id,
- 'is_stringscene' => $is_stringscene,
- 'others_lg_id' => $v['lg_id'],
- 'ctime' => date('Y-m-d H:i:s'),
- 'utime' => date('Y-m-d H:i:s'),
- 'sole' => $v['sole'],
- 'expire_time' => date('Y-m-d H:i:s', time() + 60)
- ];
- }
- //写赔率记录数据
- $ret = $models['model_odds_record']::insert($set_odds_r);
- if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率记录;', Response::ADD_ODDS_R_ERROR));
- //写赔率 数据
- $ret = $models['model_odds']::insert($set_odds);
- if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率;', Response::ADD_ODDS_ERROR));
- }
- $this->writeLog($data, Response::success());
- //提交事务
- DB::commit();
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
- return $e->getMessage();
- }
- }
- /**
- * @param Req $data
- * @return string
- * @throws \App\Lib\Biz\Sport\Exception
- * 更新赛事状态
- */
- public function upMatch($data)
- {
- $sdata = $data;
- try {
- //开启事务
- DB::beginTransaction();
- //获取待更新赛事
- $obt = $data['data'];
- //请求 数据 为空
- if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
- $data = $this->getAddData($obt);
- //不是更新赛事状态数据
- if ($data['title'] != 'match_status') throw new \Exception(Response::generate('不是更新赛事状态数据', Response::ABNORMAL));
- /*
- //写请求数据 日志记录
- if($this->isRecord){
- $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
- if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
- }
- */
- //获取 球类代码
- $game_code = $data['game_code'];
- //获取球类 名称
- $gameName = gameModel::getGameName($game_code);
- //获取 数据源
- $source = $data['source'];
- //获取 当前请求 所有 uuid /match_id
- $others_match_ids = [];
- $match_uuids = [];
- foreach ($data['data'] as $k => $v) {
- $others_match_ids[] = $v['match_id'];
- //追加获取赛事uuid
- $match_uuids[] = $v['match_identity'];
- }
- //根据球类代码 获取model
- $model = commonFunction::getModels($game_code, 1);
- //获取所有赛事 match_id
- $local_match = $model['model_local_match']::SELECT('others_match_id', 'match_id')
- //->where(['source' => $source])
- ->whereIn('identity', $match_uuids)
- ->get()->toArray();
- if (empty($local_match)) throw new \Exception(Response::generate('', Response::MATCHID_NULL));
- //设置各球类 判断结束时间
- if ($game_code == 'zq') {
- //足球 60分钟
- $handle_time = 60 * 60;
- }
- if ($game_code == 'lq') {
- //篮球 40分钟
- $handle_time = 60 * 40;
- }
- if ($game_code == 'wq') {
- //网球 2小时
- $handle_time = 60 * 60 * 2;
- }
- if ($game_code == 'bq') {
- //棒球 2小时
- $handle_time = 60 * 60 * 2;
- }
- //取值 更新字段
- foreach ($local_match as $k => $v) {
- foreach ($data['data'] as $kk => $vv) {
- if ($v['others_match_id'] == $vv['match_id']) {
- $set_status = [
- 'status' => $vv['status'],
- 'is_rollball' => $vv['is_rollball'],
- 'is_today' => $vv['is_today'],
- 'is_morningplate' => $vv['is_morningplate'],
- 'is_stringscene' => $vv['is_stringscene'],
- 'utime' => date('Y-m-d H:i:s'),
- ];
- //如果是足球 追加角球状态
- if ($game_code == 'zq') {
- $set_status['is_horn'] = $vv['is_horn'];
- }
- $ret = $model['model_match']::where(['id' => $v['match_id']])
- ->update($set_status);
- if ($ret < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $v['others_match_id'], Response::UPSTATUS_ERROR));
- //获取赛事开始时间
- $game_start_time = $model['model_result']::where(['match_id' => $v['match_id']])->first()->start_time;
- //开赛时间 时间戳
- $game_start_time_unx = strtotime($game_start_time);
- //===如果赛事已结束 并且 开赛时间< 当前时间-各球类处理时间 ===
- if ($vv['status'] == 2 and $game_start_time_unx < (time() - $handle_time)) {
- // 新增 更新 赛事结果
- $this->match_result($game_code, $v['match_id']);
- // 更新 赛事结果表 状态 为已结束
- $ret_up_r = $model['model_result']::where(['match_id' => $v['match_id']])
- ->update(["status" => 2, 'is_correct' => -1, "update_time" => date('Y-m-d H:i:s')]);
- if ($ret_up_r < 1) throw new \Exception(Response::generate('', Response::ADD_MATCH_R_ERROR));
- // 写入结算通知表 用于自动结算
- $Comendnotice = new \App\Models\Comendnotice();
- $ret_add = $Comendnotice->addcomendnotice($v['match_id'], $game_code, 0, $game_start_time);
- if ($ret_add != 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $v['others_match_id'], Response::ADD_NOTICE_ERR));
- }
- //===end===
- }
- }
- }
- $this->writeLog($sdata, Response::success());
- //提交事务
- DB::commit();
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- $this->writeLog($sdata, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
- return $e->getMessage();
- }
- }
- /*
- *写 冠军联赛 赔率 数据
- *每个请求 包含 N个联赛 下 N条赔率
- */
- public function setOddsCH_v1($data)
- {
- try {
- //开启事务
- DB::beginTransaction();
- $obt = $data['data'];
- //请求 数据 为空
- if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
- $getData = $this->getAddData($obt);
- //不是 冠军 赔率 数据
- if ($getData['title'] != 'odds_ch') throw new \Exception(Response::generate('不是冠军赔率数据,', Response::ABNORMAL));
- //写请求数据 日志记录
- if ($this->isRecord) {
- $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'], $obt, $getData);
- if ($setSportsRecord < 1) throw new \Exception(Response::generate('', Response::SPORTS_RECORD_ERR));
- }
- //获取球类代码
- $game_code = $getData['game_code'];
- //获取数据源
- $source = $getData['source'];
- //获取球类名称
- $gameName = gameModel::getGameName($game_code);
- //获取 model
- $models = commonFunction::getModels($game_code, 1);
- //获取 赔率数据
- $oddsData = $getData['data'];
- //获取 当前 所有 可用 赔率 sole
- $odds_only = $getData['odds_only'];
- //====验证 请求里 所有赔率 所属联赛 是否存在 ====
- //获取 当前请求 所有 联赛 uuid
- $uuids = [];
- $s_lg_ids = [];
- foreach ($oddsData as $k => $v) {
- $uuids[] = $v['uuid'];
- $s_lg_ids[] = $v['lg_id'];
- }
- $s_lg_ids = array_unique($s_lg_ids);
- $uuids = array_unique($uuids);
- sort($uuids);
- sort($s_lg_ids);
- //更新联赛下所有 不在本次请求的赔率 状态
- $ret = OddsModel::upOddsStatus($models, '', $s_lg_ids, $source, $odds_only);
- if ($ret != true) throw new \Exception(Response::generate($gameName, Response::ODDS_SOLE_ERR));
- //获取 本地 已存在 联赛
- $l_lg_data = $models['model_local_league']::whereIn('identity', $uuids)->select('identity', 'lg_id')->get()->toArray();
- //二维数组去重
- $l_lg_data = commonFunction::uniquArrV2($l_lg_data, 'identity');
- //循环对比 请求uuid->本地uuid
- foreach ($l_lg_data as $k => $v) {
- foreach ($uuids as $kk => $uuid) {
- if ($v['identity'] == $uuid) {
- unset($uuids[$kk]);
- }
- }
- }
- //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常;
- if (!empty($uuids)) throw new \Exception(Response::generate($gameName . '冠军联赛:uuid-' . $uuids[0] . ';', Response::LEAGUE_ERROR));
- //====end====
- //赔率记录数据
- $set_odds_r = [];
- //赔率数据
- $set_odds = [];
- if (!empty($oddsData)) {
- foreach ($oddsData as $k => $v) {
- //获取 本地 联赛 ID
- $lg_id = commonFunction::searcharray($v['uuid'], 'identity', $l_lg_data, 'lg_id');
- //组装赔率记录数据
- $set_odds_r[] = [
- 'match_id' => 0,
- 'others_match_id' => $v['match_id'] ?: 0,
- 'odds_code' => $v['odds_code'] ?: '',
- 'status' => $v['status'] ?: 0,
- 'sort' => $v['sort'] ?: 0,
- 'p_code' => $v['p_code'] ?: '',
- 'odds' => $v['odds'] ?: 0,
- 'condition' => $v['condition'],
- 'odds_only' => $v['odds_only'] ?: '',
- 'source' => $v['source'] ?: '',
- 'type' => $v['type'] ?: 1,
- 'team' => $v['team'] ?: '',
- 'lg_id' => $lg_id,
- 'others_lg_id' => $v['lg_id'],
- 'ctime' => date('Y-m-d H:i:s'),
- 'utime' => date('Y-m-d H:i:s'),
- ];
- }
- foreach ($oddsData as $k => $v) {
- //获取 本地 联赛 ID
- $lg_id = commonFunction::searcharray($v['uuid'], 'identity', $l_lg_data, 'lg_id');
- //组装赔率数据
- $set_odds[] = [
- 'match_id' => 0,
- 'others_match_id' => $v['match_id'] ?: 0,
- 'odds_code' => $v['odds_code'] ?: '',
- 'status' => $v['status'] ?: 0,
- 'sort' => $v['sort'] ?: 0,
- 'p_code' => $v['p_code'] ?: '',
- 'odds' => $v['odds'] ?: 0,
- 'condition' => $v['condition'],
- 'odds_only' => $v['odds_only'] ?: '',
- 'source' => $v['source'] ?: '',
- 'type' => $v['type'] ?: 1,
- 'team' => $v['team'] ?: '',
- 'lg_id' => $lg_id,
- 'others_lg_id' => $v['lg_id'],
- 'ctime' => date('Y-m-d H:i:s'),
- 'utime' => date('Y-m-d H:i:s'),
- 'sole' => $v['sole'],
- 'expire_time' => date('Y-m-d H:i:s', time() + 60)
- ];
- }
- //写赔率记录数据
- $ret = $models['model_odds_record']::insert($set_odds_r);
- if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率记录;', Response::ADD_ODDS_R_ERROR));
- //写赔率 数据
- $ret = $models['model_odds']::insert($set_odds);
- if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率;', Response::ADD_ODDS_ERROR));
- }
- $this->writeLog($data, Response::success());
- //提交事务
- DB::commit();
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
- return $e->getMessage();
- }
- }
- /*
- *写 冠军联赛 赔率 数据
- *每个请求 包含 1个联赛 下 N条赔率
- *追加赔率过滤
- */
- public function setOddsCH($data)
- {
- try {
- //开启事务
- DB::beginTransaction();
- $obt = $data['data'];
- //请求 数据 为空
- if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
- $getData = $this->getAddData($obt);
- //不是 冠军 赔率 数据
- if ($getData['title'] != 'odds_ch') throw new \Exception(Response::generate('不是冠军赔率数据,', Response::ABNORMAL));
- /*
- //写请求数据 日志记录
- if($this->isRecord){
- $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
- if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
- }
- */
- //获取球类代码
- $game_code = $getData['game_code'];
- //获取数据源
- $source = $getData['source'];
- //获取球类名称
- $gameName = gameModel::getGameName($game_code);
- //获取数据源联赛ID
- $s_lg_id = $getData['lg_id'];
- //获取源数据联赛UUID
- $uuid = $getData['uuid'];
- //获取 model
- $models = commonFunction::getModels($game_code, 1);
- //获取 赔率数据
- $oddsData = $getData['data'];
- //验证本次请求所属联赛/赛事是否存在 返回本地联赛/赛事ID
- $lg_id = $this->leagueVerify($models, $uuid, $source, $gameName);
- //===追加处理赔率过滤===
- //获取当前联赛 在本地的所有赔率
- $l_odds_data = $models['model_odds']::select('odds_only')->where(['lg_id' => $lg_id, 'type' => 1])->get()->toArray();
- //获取请求待写入数据
- $setOddsData = [];
- //获取本地待删除数据
- $delOddsData = [];
- //本地没有该赛事赔率,则直接全部写入
- if (empty($l_odds_data)) {
- $setOddsData = $oddsData;
- } //本地有该赛事赔率,则分别获取需写入/删除的数据
- else {
- $newodds_t = $this->ArrayKeySet($oddsData, 'odds_only');
- $oldodds_t = $this->ArrayKeySet($l_odds_data, 'odds_only');
- foreach ($newodds_t as $key => $val) {
- if (isset($oldodds_t[$key])) {
- unset($newodds_t[$key]);
- unset($oldodds_t[$key]);
- }
- }
- $oddsData = $newodds_t;
- $l_odds_data = $oldodds_t;
- sort($oddsData);
- sort($l_odds_data);
- //需写入请求数据
- $setOddsData = $oddsData;
- //需删除本地数据
- $delOddsData = $l_odds_data;
- }
- //===end===
- //删除本地不在请求中的数据
- if (!empty($delOddsData)) {
- //所有需删除数据odds_only
- $odds_onlys = [];
- foreach ($delOddsData as $k => $v) {
- $odds_onlys[] = $v['odds_only'];
- }
- $del = $models['model_odds']::whereIn('odds_only', $odds_onlys)->delete();
- if ($del != count($odds_onlys)) throw new \Exception(Response::generate($gameName, Response::DEL_ODDS_ERR));
- }
- //写入请求中本地不存在的数据
- if (!empty($setOddsData)) {
- foreach ($setOddsData as $k => $v) {
- //组装赔率记录数据
- $set_odds_r[] = [
- 'match_id' => 0,
- 'others_match_id' => $v['match_id'] ?: 0,
- 'odds_code' => $v['odds_code'] ?: '',
- 'status' => $v['status'] ?: 0,
- 'sort' => $v['sort'] ?: 0,
- 'p_code' => $v['p_code'] ?: '',
- 'odds' => $v['odds'] ?: 0,
- 'condition' => $v['condition'],
- 'odds_only' => $v['odds_only'] ?: '',
- 'source' => $v['source'] ?: '',
- 'type' => $v['type'] ?: 1,
- 'team' => $v['team'] ?: '',
- 'lg_id' => $lg_id,
- 'others_lg_id' => $v['lg_id'],
- 'ctime' => date('Y-m-d H:i:s'),
- 'utime' => date('Y-m-d H:i:s'),
- ];
- }
- foreach ($oddsData as $k => $v) {
- //组装赔率数据
- $set_odds[] = [
- 'match_id' => 0,
- 'others_match_id' => $v['match_id'] ?: 0,
- 'odds_code' => $v['odds_code'] ?: '',
- 'status' => $v['status'] ?: 0,
- 'sort' => $v['sort'] ?: 0,
- 'p_code' => $v['p_code'] ?: '',
- 'odds' => $v['odds'] ?: 0,
- 'condition' => $v['condition'],
- 'odds_only' => $v['odds_only'] ?: '',
- 'source' => $v['source'] ?: '',
- 'type' => $v['type'] ?: 1,
- 'team' => $v['team'] ?: '',
- 'lg_id' => $lg_id,
- 'others_lg_id' => $v['lg_id'],
- 'ctime' => date('Y-m-d H:i:s'),
- 'utime' => date('Y-m-d H:i:s'),
- 'sole' => $v['sole'],
- 'expire_time' => date('Y-m-d H:i:s', time() + 60)
- ];
- }
- //写赔率记录数据
- $ret = $models['model_odds_record']::insert($set_odds_r);
- if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率记录;', Response::ADD_ODDS_R_ERROR));
- //写赔率 数据
- $ret = $models['model_odds']::insert($set_odds);
- if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率;', Response::ADD_ODDS_ERROR));
- }
- $this->writeLog($data, Response::success());
- //提交事务
- DB::commit();
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
- return $e->getMessage();
- }
- }
- /*
- * 写入直播 数据
- */
- public function setBroadCast($data)
- {
- $data_s = $data['data'];
- try {
- //开启事务
- DB::beginTransaction();
- $obt = $data['data'];
- //请求 数据 为空
- if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
- $getData = $this->getAddData($obt);
- //不是 直播 数据
- if ($getData['title'] != 'broad_cast') throw new \Exception(Response::generate('不是直播数据,', Response::ABNORMAL));
- //写请求数据 日志记录
- if ($this->isRecord) {
- $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'], $obt, $getData);
- if ($setSportsRecord < 1) throw new \Exception(Response::generate('', Response::SPORTS_RECORD_ERR));
- }
- //获取 球类
- $game_code = $getData['game_code'];
- //删除当前球类已存在的直播数据
- $del = broadcastModel::where('game_code', $game_code)->delete();
- if ($del != true) throw new \Exception(Response::generate('', Response::DEL_PAST_BROADCAST_ERR));
- //获取 直播 数据
- $BroadCast = $getData['data'];
- $set_broadcast = [];
- foreach ($BroadCast as $k => $data) {
- //组装数据
- $set_broadcast[] = [
- "doing" => $data['doing'],
- "game_type" => $data['game_type'],
- "game_code" => $game_code,
- "guest_team" => $data['guest_team'],
- "host_team" => $data['host_team'],
- "league_name" => $data['league_name'],
- "shower" => $data['shower'],
- "showid" => (int)$data['showid'],
- "start_time" => $data['start_time'],
- "ctime" => date('Y-m-d H:i:s'),
- "utime" => date('Y-m-d H:i:s'),
- ];
- }
- //写入 直播 数据
- $ret = broadcastModel::insert($set_broadcast);
- if ($ret == false) throw new \Exception(Response::generate('', Response::BROADCAST_ERROR));
- $this->writeLog($data_s, Response::success());
- //提交事务
- DB::commit();
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- $this->writeLog($data_s, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
- return $e->getMessage();
- }
- }
- /**
- * 写赛事危险球数据
- */
- public function setMatchWarn($data)
- {
- try {
- //开启事务
- DB::beginTransaction();
- $obt = $data['data'];
- //请求 数据 为空
- if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
- $getData = $this->getAddData($obt);
- //不是 赛事 危险球 数据
- if ($getData['title'] != 'match_warn') throw new \Exception(Response::generate('不是危险球数据,', Response::ABNORMAL));
- //获取球类代码
- $game_code = $getData['game_code'];
- //获取数据源
- $source = $getData['source'];
- //获取数据源赛事id
- $others_match_id = $getData['match_id'];
- //获取数据源赛事uuid
- $match_identity = $getData['match_identity'];
- //获取球类名称
- $gameName = gameModel::getGameName($game_code);
- //获取 model
- $models = commonFunction::getModels($game_code, 1);
- //获取 赛事 数据
- $warn_data = $getData['data'];
- //获取赛事 match_id
- $match_id = $models['model_local_match']::SELECT('others_match_id', 'match_id')
- //->where(['source' => $source, 'identity' => $match_identity])
- ->where(['identity' => $match_identity])
- ->first()->match_id;
- if (!empty($match_id)) {
- //更新 危险球
- $ret = ZqResultModel::set_result_warn($match_id, $warn_data);
- if ($ret < 1) throw new \Exception(Response::generate('', Response::SET_WARN_ERR));
- //追加 滚球投注 危险球审核
- SportsNoteListModel::WarnHandle($match_id, $warn_data);
- }
- //提交事务
- DB::commit();
- //写请求数据 日志记录
- $this->writeLog($data, Response::success());
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- //写请求数据 日志记录
- if ($this->isRecord) commonFunction::SET_SPORTS_RECORD($game_code, $getData, $getData['title'], $e->getMessage());
- return $e->getMessage();
- }
- }
- /**
- * 定时执行 滚球投注危险球审核
- */
- public function HandleOrder()
- {
- try {
- //开启事务
- DB::beginTransaction();
- $time = 60;
- //处理指定时间内的滚球待审核订单
- SportsNoteListModel::getOrderData($time);
- //提交事务
- DB::commit();
- //写请求数据 日志记录
- $this->writeLog(['HandleOrder' => 'ok'], Response::success());
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- //写请求数据 日志记录
- $this->writeLog(['HandleOrder' => 'no', 'msg' => $e->getMessage()], Response::success());
- return $e->getMessage();
- }
- }
- /*
- * 验证所属 联赛 是否存在
- */
- public function leagueVerify($models = [], $uuid = '', $source = '', $gameName = '')
- {
- if (empty($models) || empty($uuid)) throw new \Exception(Response::generate('', Response::ABNORMAL));
- $l_lg_id = $models['model_local_league']::where(['identity' => $uuid])->value('lg_id');
- if ($l_lg_id < 1) throw new \Exception(Response::generate($gameName . '联赛:lg_id-' . $uuid, Response::LEAGUE_ERROR));
- return $l_lg_id;
- }
- /*
- * 验证所属 赛事 是否存在
- */
- public function matchVerify($models = [], $s_match_id = '', $source = '', $gameName = '')
- {
- if (empty($models) || empty($s_match_id)) throw new \Exception(Response::generate('', Response::ABNORMAL));
- $l_match_id = $models['model_local_match']::where(['others_match_id' => $s_match_id, 'source' => $source])->value('match_id');
- if ($l_match_id < 1) throw new \Exception(Response::generate($gameName . '赛事:match_id-' . $s_match_id, Response::MATCH_ERROR));
- return $l_match_id;
- }
- /*
- * 验证所属 赛事 是否存在
- */
- public function matchVerify_v2($models = [], $match_identity = '', $source = '', $gameName = '')
- {
- if (empty($models) || empty($match_identity)) throw new \Exception(Response::generate('', Response::ABNORMAL));
- //$l_match_id = $models['model_local_match']::where(['identity' => $match_identity, 'source' => $source])->value('match_id');
- $l_match_id = $models['model_local_match']::where(['identity' => $match_identity])->value('match_id');
- if ($l_match_id < 1) throw new \Exception(Response::generate($gameName . 'match_identity-' . $match_identity, Response::MATCH_ERROR));
- return $l_match_id;
- }
- /*
- * 写入直播 数据
- */
- public function broadcast($opt = [], $game_code = '')
- {
- $data = $opt['data'];
- $set_broadcast = [
- "doing" => $data['doing'],
- "game_type" => $data['game_type'],
- "game_code" => $game_code,
- "guest_team" => $data['guest_team'],
- "host_team" => $data['host_team'],
- "league_name" => $data['league_name'],
- "shower" => $data['shower'],
- "showid" => (int)$data['showid'],
- "start_time" => $data['start_time'],
- "ctime" => date('Y-m-d H:i:s'),
- "utime" => date('Y-m-d H:i:s'),
- ];
- $ret = broadcastModel::insert($set_broadcast);
- if ($ret == false) throw new \Exception(Response::generate('', Response::BROADCAST_ERROR));
- }
- /*
- * 写入 赛事 初始 结果
- */
- public function match_result($game_code = '', $match_id = 0)
- {
- $model = commonFunction::getModels($game_code, 1);
- if ($game_code == 'zq') ZqResultModel::ZQresult_v2($model, $match_id);
- if ($game_code == 'lq') LqResultModel::LQresult_v2($model, $match_id);
- if ($game_code == 'wq') WqResultModel::WQresult_v2($model, $match_id);
- if ($game_code == 'bq') BqResultModel::BQresult_v2($model, $match_id);
- }
- /*
- * 写入联赛数据
- */
- public function league($opt = [], $gameName = '')
- {
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = commonFunction::getModels($game_code, 1);
- $data = $opt['data'];
- //查询联赛是否已存在
- $id = $model['model_league']::where('name_chinese', '=', $data['name_chinese'])
- ->value('id');
- //默认获取本年最后一天
- $last_time = date('Y-12-31 23:59:59');
- //决赛时间
- if ($data['last_time']) {
- $last_time = $data['last_time'];
- }
- $set_lg['name_chinese'] = $data['name_chinese'];
- $set_lg['kind'] = $data['kind'];
- $set_lg['match_mode'] = $data['match_mode'];
- $set_lg['if_stop'] = $data['if_stop'];
- $set_lg['identity'] = $data['uuid'] ?: '';
- $set_lg['last_time'] = $last_time;
- $set_lg['utime'] = date('Y-m-d H:i:s');
- if (empty($id)) {
- //写入联赛
- $id = $model['model_league']::insertGetId($set_lg);
- $m_lg_id = $id;
- if ($m_lg_id < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $data['lg_id'] . ';', Response::INSERT_ERROR));
- } else {
- //更新联赛
- $ret = $model['model_league']::where(['id' => $id])
- ->update($set_lg);
- if ($ret < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $data['lg_id'] . ';', Response::UPDATE_ERROR));
- }
- $set_local = [
- 'lg_id' => $id,
- 'others_lg_id' => $data['lg_id'],
- 'source' => $data['source'],
- 'ctime' => date('Y-m-d H:i:s')
- ];
- $ret = $model['model_local_league']::insertGetId($set_local);
- 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'));
- //执行成功返回源数据 联赛ID
- $data = ['others_lg_id' => $data['lg_id'], 'lg_id' => $id];
- return $data;
- }
- /*
- * 写入赛事数据
- */
- public function competition($opt = [], $gameName = '', $lg_id = '')
- {
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = commonFunction::getModels($game_code, 1);
- $data = $opt['data'];
- if (empty($data['lg_id'])) throw new \Exception(Response::generate('', Response::LEAGUE_ERROR));//Render([], '10015', lang('Tips','Sports')->get('league_error'));
- //查询赛事是否存在
- $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']])
- ->value('id');
- $half_match_id = 0;
- //如果有上半场赛事id 获取上半场赛事是否存在
- if (!empty($data['half_match_id'])) {
- $half_match_id = $match_id = $model['model_local_match']::where(['others_match_id' => $data['half_match_id'], 'source' => $data['source']])
- ->value('match_id');
- 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'));
- }
- //如果赛事没有开始日期,则为冠军盘口赛事
- if (empty($data['match_date'])) {
- //冠军盘口赛事获取所属联赛结束时间
- $last_time = $model['model_league']::where(['id' => $lg_id])
- ->value('last_time');
- 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'));
- //给冠军盘口赛事 赋值时间
- $time = strtotime($last_time);
- $data['match_date'] = date('Y-m-d', $time);
- $data['match_time'] = date('H:i:s', $time);
- }
- $set_match = [
- 'ctime' => date('Y-m-d H:i:s'),
- 'utime' => date('Y-m-d H:i:s'),
- 'expire_time' => date('Y-m-d H:i:s', time() + 60),
- 'home_team' => $data['home_team'] ?: '',
- 'guest_team' => $data['guest_team'] ?: 'no_team',
- 'lg_id' => $lg_id,
- 'status' => $data['status'],
- 'match_date' => $data['match_date'] ?: date('Y-m-d'),
- 'match_time' => $data['match_time'] ?: date('H:i:s'),
- 'tag' => $data['tag'] ?: 0,
- 'is_rollball' => $data['is_rollball'] ?: 0,
- 'is_today' => $data['is_today'] ?: 0,
- 'is_morningplate' => $data['is_morningplate'] ?: 0,
- 'is_stringscene' => $data['is_stringscene'] ?: 0,
- 'us_time' => $data['us_time'] ?: commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
- 'half_match_id' => $half_match_id ?: 0,
- 'identity' => $data['uuid'],
- ];
- //写入赛事
- if (empty($id)) {
- //写入赛事 返回id
- $id = $model['model_match']::insertGetId($set_match);
- if ($id < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::INSERT_ERROR));
- } else {
- //更新赛事
- $ret = $model['model_match']::where(['id' => $id])->update($set_match);
- if ($ret < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::UPDATE_ERROR));
- }
- //写关联记录
- $set_local = [
- 'match_id' => $id,
- 'others_match_id' => $data['match_id'],
- 'source' => $data['source'],
- 'ctime' => date('Y-m-d H:i:s')
- ];
- $ret = $model['model_local_match']::insertGetId($set_local);
- 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'));
- //返回 源数据 赛事ID
- $data = ['others_match_id' => $data['match_id'], 'match_id' => $id];
- return $data;
- }
- /*
- * 写入赔率/赔率记录数据
- */
- public function odds($opt = [], $gameName = '', $lg_id = 0, $match_id = 0, $oddsID = '', $oddsRecordID = '')
- {
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = commonFunction::getModels($game_code, 1);
- $data = $opt['data'];
- //获取赛事 本地/源ID
- $others_match_id = $data['match_id'];
- //获取联赛 本地/源ID
- $others_lg_id = $data['lg_id'];
- //===写赔率记录===
- $set_odds_r = [
- 'match_id' => $match_id ?: 0,
- 'others_match_id' => $others_match_id ?: 0,
- 'odds_code' => $data['odds_code'] ?: '',
- 'status' => $data['status'] ?: 0,
- 'sort' => $data['sort'] ?: 0,
- 'p_code' => $data['p_code'] ?: '',
- 'odds' => $data['odds'] ?: 0,
- 'condition' => $data['condition'],
- 'odds_only' => $data['odds_only'] ?: '',
- 'source' => $data['source'] ?: '',
- 'type' => $data['type'] ?: 0,
- 'team' => $data['team'] ?: '',
- 'lg_id' => $lg_id,
- 'others_lg_id' => $others_lg_id,
- 'ctime' => date('Y-m-d H:i:s'),
- ];
- //更新或写入赔率记录
- if ($oddsRecordID != false) {
- $ret = $model['model_odds_record']::where(['id' => $oddsRecordID])
- ->update($set_odds_r);
- 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'));
- } else {
- $ret = $model['model_odds_record']::insert($set_odds_r);
- 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'));
- }
- //===写赔率===
- $set_odds = [
- 'match_id' => $match_id ?: 0,
- 'others_match_id' => $others_match_id ?: 0,
- 'odds_code' => $data['odds_code'] ?: '',
- 'status' => $data['status'] ?: 0,
- 'sort' => $data['sort'] ?: 0,
- 'p_code' => $data['p_code'] ?: '',
- 'odds' => $data['odds'] ?: 0,
- 'condition' => $data['condition'],
- 'odds_only' => $data['odds_only'] ?: '',
- 'sole' => $data['sole'] ?: '',
- 'source' => $data['source'] ?: '',
- 'type' => $data['type'] ?: 0,
- 'team' => $data['team'] ?: '',
- 'lg_id' => $lg_id,
- 'others_lg_id' => $others_lg_id,
- 'ctime' => date('Y-m-d H:i:s'),
- 'utime' => date('Y-m-d H:i:s'),
- 'expire_time' => date('Y-m-d H:i:s', time() + 60),
- ];
- //更新或写入赔率数据
- if ($oddsID != false) {
- $ret = $model['model_odds']::where(['id' => $oddsID])
- ->update($set_odds);
- 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'));
- } else {
- $ret = $model['model_odds']::insert($set_odds);
- 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'));
- }
- //===end===
- }
- /*
- * 更新 赛事下 所有不在本次请求的 赔率状态
- */
- public function upOddsStatus($opt = [], $gameName = '')
- {
- //获取球类代码
- $game_code = $opt['game_code'];
- //获取球类model
- $model = commonFunction::getModels($game_code, 1);
- //赛事下 所有 赔率sole
- $odds_sole = $opt['data'];
- //数据源 赛事 id
- $others_match_id = $opt['match_id'];
- //数据源
- $source = $opt['source'];
- //更新赛事下所有 不在本次请求的赔率 状态
- $ret = OddsModel::getMatchSole($model, $others_match_id, '', $source, $odds_sole);
- if ($ret != true) throw new \Exception(Response::generate($gameName, Response::ODDS_SOLE_ERR));
- }
- /*
- * 写入联赛结果
- */
- public function league_result($opt, $gameName)
- {
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = commonFunction::getModels($game_code, 1);
- $data = $opt['data'];
- //验证结果所属联赛
- $lg_id = $model['model_local_league']::where(['others_lg_id' => $data['lg_id'], 'source' => $data['source']])
- ->value('lg_id');
- if ($lg_id < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $data['lg_id'] . ';', Response::LEAGUE_ERROR));
- $lg_result_id = $model['model_league_result']::where(['lg_id' => $lg_id, 'game_name' => $data['game_name']])
- ->value('id');
- $set_lg_result = [
- 'lg_id' => $lg_id,
- 'game_name' => $data['game_name'],
- 'result' => json_encode($data['result'], JSON_UNESCAPED_UNICODE),
- 'status' => $data['status'],
- 'ctime' => date('Y-m-d H:i:s'),
- 'utime' => date('Y-m-d H:i:s'),
- ];
- //联赛结果数据处理
- if (!empty($lg_result_id)) {
- $ret = $model['model_league_result']::where(['id' => $lg_result_id])
- ->update($set_lg_result);
- 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'));
- } else {
- $ret = $model['model_league_result']::insert($set_lg_result);
- 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'));
- }
- }
- /*
- * 写入赛事结果
- */
- public function com_result($opt = [], $gameName = '', $lg_id = '', $match_id = '')
- {
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = commonFunction::getModels($game_code, 1);
- $data = $opt['data'];
- //查询结果是否存在
- $match_r_id = $model['model_result']::where(['match_id' => $match_id])
- ->value('id');
- $set_match_r = [
- "home_team" => $data['home_team'],
- "guest_team" => $data['guest_team'],
- "lg_id" => $lg_id,
- "home_rate" => $data['home_rate'],
- "guest_rate" => $data['guest_rate'],
- "home_score" => $data['home_score'],
- "guest_score" => $data['guest_score'],
- "all_goal" => $data['all_goal'],
- "status" => $data['status'],
- "first_score" => $data['first_score'],
- "last_score" => $data['last_score'],
- "match_score" => $data['match_score'],
- "match_winer" => $data['match_winer'],
- "match_time" => $data['match_time'],
- "match_process" => $data['match_process'],
- "tag" => $data['tag'],
- "match_id" => $match_id,
- "u_home_score" => $data['u_home_score'],
- "u_guest_score" => $data['u_guest_score'],
- "p_code" => $data['p_code'],
- "update_time" => date('Y-m-d H:i:s')
- ];
- //赛事结果数据处理
- if (!empty($match_r_id)) {
- $ret = $model['model_result']::where(['id' => $match_r_id])
- ->update($set_match_r);
- 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'));
- } else {
- $ret = $model['model_result']::insert($set_match_r);
- 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'));
- }
- // return Response::success();
- }
- //写入赛事结果记录
- public function com_result_record($opt = [], $gameName = '', $lg_id = '', $match_id = '')
- {
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = commonFunction::getModels($game_code, 1);
- $data = $opt['data'];
- $match_r_id = $model['model_result_record']::where(['match_id' => $match_id, 'match_time' => $data['match_time']])
- ->value('id');
- //根据球类 获取 赛事结果记录字段
- $set_match_r = $this->get_match_r($game_code, $lg_id, $match_id, $data);
- //赛事结果记录处理
- if ($match_r_id > 0) {
- $ret = $model['model_result_record']::where(['id' => $match_r_id])
- ->update($set_match_r);
- 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'));
- } else {
- $ret = $model['model_result_record']::insert($set_match_r);
- 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'));
- }
- }
- /**
- * @param $data
- * @return mixed
- * json转数组
- */
- public function getAddData($data)
- {
- $data = json_decode($data, true);
- return $data;
- }
- /*
- * 根据球类获取 赛事结果记录字段
- */
- public function get_match_r($game_code, $lg_id, $match_id, $data)
- {
- $set_match_r = [];
- if ($game_code == 'zq') {
- $set_match_r = [
- "home_team" => $data['home_team'] ?: '',
- "guest_team" => $data['guest_team'] ?: '',
- "lg_id" => $lg_id,
- "all_goal" => $data['all_goal'] ?: 0,
- "home_rate" => $data['home_rate'] ?: 0,
- "guest_rate" => $data['guest_rate'] ?: 0,
- "home_score" => $data['home_score'] ?: 0,
- "guest_score" => $data['guest_score'] ?: 0,
- "status" => $data['status'] ?: 0,
- "first_score" => $data['first_score'] ?: 0,
- "last_score" => $data['last_score'] ?: 0,
- "match_score" => $data['match_score'] ?: 0,
- "match_winer" => $data['match_winer'] ?: '',
- "match_time" => $data['match_time'] ?: 0,
- "match_process" => $data['match_process'] ?: '',
- // "tag"=> $data['tag']?:0,
- "match_id" => $match_id,
- "update_time" => date('Y-m-d H:i:s'),
- "corner_result" => json_encode($data['corner_result']),//角球
- "penalty_result" => json_encode($data['penalty_result']),//罚牌
- ];
- };
- if ($game_code == 'lq') {
- $set_match_r = [
- "home_team" => $data['home_team'] ?: '',
- "guest_team" => $data['guest_team'] ?: '',
- "lg_id" => $lg_id,
- "home_rate" => $data['home_rate'] ?: 0,
- "guest_rate" => $data['guest_rate'] ?: 0,
- "home_score" => $data['home_score'] ?: 0,
- "guest_score" => $data['guest_score'] ?: 0,
- "status" => $data['status'] ?: 0,
- "first_score" => $data['first_score'] ?: 0,
- "last_score" => $data['last_score'] ?: 0,
- "match_score" => $data['match_score'] ?: 0,
- "match_winer" => $data['match_winer'] ?: '',
- "match_time" => $data['match_time'] ?: 0,
- "match_process" => $data['match_process'] ?: '',
- // "tag"=> $data['tag']?:0,
- "match_id" => $match_id,
- "update_time" => date('Y-m-d H:i:s'),
- "result_mark" => json_encode($data['result_mark']) ?: '',
- ];
- }
- if ($game_code == 'wq') {
- $set_match_r = [
- "home_player_name" => $data['home_player_name'] ?: '',
- "guest_player_name" => $data['guest_player_name'] ?: '',
- "lg_id" => $lg_id,
- "home_player_let_plate" => $data['home_player_let_plate'] ?: 0,
- "guest_player_let_plate" => $data['guest_player_let_plate'] ?: 0,
- "home_player_let_inning" => $data['home_player_let_inning'] ?: 0,
- "guest_player_let_inning" => $data['guest_player_let_inning'] ?: 0,
- "all_inning" => $data['all_inning'] ?: 0,
- "home_player_score" => $data['home_player_score'] ?: 0,
- "guest_player_score" => $data['guest_player_score'] ?: 0,
- "status" => $data['status'] ?: 0,
- "first_score_player" => $data['first_score_player'] ?: '',
- "last_score_player" => $data['last_score_player'] ?: '',
- "first_inning_score" => $data['first_inning_score'] ?: 0,
- "second_inning_score" => $data['second_inning_score'] ?: 0,
- "third_inning_score" => $data['third_inning_score'] ?: 0,
- "match_winer_player" => $data['match_winer_player'] ?: '',
- "update_time" => date('Y-m-d H:i:s'),
- "match_time" => $data['match_time'] ?: 0,
- "match_process" => $data['match_process'] ?: '',
- // "tag"=>$data['tag']?:0,
- "match_id" => $match_id,
- "result_mark" => json_encode($data['result_mark']) ?: '',
- ];
- }
- if ($game_code == 'bq') {
- $set_match_r = [
- "home_team" => $data['home_team'] ?: '',
- "guest_team" => $data['guest_team'] ?: '',
- "lg_id" => $lg_id,
- "home_rate" => $data['home_rate'] ?: 0,
- "guest_rate" => $data['guest_rate'] ?: 0,
- "home_score" => $data['home_score'] ?: 0,
- "guest_score" => $data['guest_score'] ?: 0,
- "status" => $data['status'] ?: 0,
- "first_score" => $data['first_score'] ?: 0,
- "last_score" => $data['last_score'] ?: 0,
- "match_score" => $data['match_score'] ?: 0,
- "match_winer" => $data['match_winer'] ?: '',
- "match_time" => $data['match_time'] ?: 0,
- "match_process" => $data['match_process'] ?: '',
- // "tag"=> $data['tag']?:0,
- "match_id" => $match_id,
- "all_inning" => $data['all_inning'] ?: 9,
- "update_time" => date('Y-m-d H:i:s'),
- "result_mark" => json_encode($data['result_mark']) ?: '',
- ];
- }
- return $set_match_r;
- }
- /**
- * 定时执行 处理到时间应该开始而未开始的赛事
- * 每分钟执行一次
- */
- public function HandleMatch()
- {
- try {
- //开启事务
- DB::beginTransaction();
- $time = 60;//60 秒
- //处理指定时间内的赛事
- StMatchModel::HandleMatch($time);
- //提交事务
- DB::commit();
- //写请求数据 日志记录
- if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('all', [], 'HandleMatch', Response::success());
- return Response::success();
- } catch (\Exception $e) {
- //回滚事务
- DB::rollBack();
- //写请求数据 日志记录
- if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('all', [], 'HandleMatch', $e->getMessage());
- return $e->getMessage();
- }
- }
- /**
- * 根据联赛名称 获取 国家 地区
- */
- public function getArea($leagueName = '')
- {
- //如果联赛名称有括号,去除
- $leagueName = preg_replace('/\(.*?\)/', '', $leagueName);
- //获取所有国家/地区
- $areaData = StAreaCountryModel::select('id', 'pid', 'name')->get()->toArray();
- //获取当前联赛所属国家
- if (strpos($leagueName, 'NBA') !== false) {
- $data = [
- 'id' => 247,
- 'pid' => 0,
- 'name' => '世界'
- ];
- return $data;
- }
- //识别本地 国家地区数据
- foreach ($areaData as $v => $k) {
- if (strpos($leagueName, $k['name']) !== false) {
- return $k;
- }
- }
- //如果不能识别,则返回未知
- $data = [
- 'id' => 0,
- 'pid' => 0,
- 'name' => '未知国家或地区'
- ];
- return $data;
- }
- //写文本日志
- private function writeLog($body, $ret)
- {
- go(function () use ($body, $ret) {
- $json_data = json_encode($body, JSON_UNESCAPED_UNICODE);
- $data = json_decode($body['data'], true);
- $game_code = isset($data['game_code']) ? $data['game_code'] : '';
- $title = isset($data['title']) ? $data['title'] : '';
- $msg = is_string($ret) ? $ret : json_encode($ret, 256);
- $now = explode(" ", microtime());
- $wdata = date("Y-m-d", $now[1]);
- $path = LOG_PATH . DS . $wdata . DS . $game_code . DS;
- if (!file_exists($path)) {
- $ret = mkdir($path, '0755', true);
- if (!$ret) {
- echo "$path --- Log File Create false \n";
- return;
- }
- }
- $lasttxt = date('Y-m-d H:i:s', $now[1]) . substr($now[0], 1, 5) . ' - ' . $msg . ' - ' . $json_data . "\n\n";
- $file = $path . DS . $game_code . '_' . $title . '.log';
- file_put_contents($file, $lasttxt, FILE_APPEND | LOCK_EX);
- return;
- });
- return;
- }
- private function writeLog_db($body, $ret)
- {
- if (!is_string($body)) {
- $body = json_encode([$body], 256);
- }
- if (!is_string($ret)) {
- $ret = json_encode([$ret], 256);
- }
- $data = ['ctime' => date("Y-m-d H:i:s"), 'body' => $body, 'ret' => $ret];
- $return = DB::table('datainf_log')->insert($data);
- return $return;
- }
- }
|