DataLogic.php 88 KB

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