DataLogic.php 63 KB

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