DataLogic.php 94 KB

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