DataLogic.php 84 KB

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