DataLogic.php 67 KB

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