DataLogic.php 90 KB

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