DataLogic.php 55 KB

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