DataLogic.php 67 KB

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