DataLogic.php 95 KB

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