DataLogic.php 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  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, '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. $Comendnotice = new \App\Models\Comendnotice();
  937. $ret_add = $Comendnotice->addcomendnotice($v['match_id'], $game_code, 0, $game_start_time);
  938. if ($ret_add != 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $v['others_match_id'], Response::ADD_NOTICE_ERR));
  939. }
  940. //===end===
  941. }
  942. }
  943. }
  944. $this->writeLog($sdata, Response::success());
  945. //提交事务
  946. DB::commit();
  947. return Response::success();
  948. } catch (\Exception $e) {
  949. //回滚事务
  950. DB::rollBack();
  951. $this->writeLog($sdata, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  952. return $e->getMessage();
  953. }
  954. }
  955. /*
  956. *写 冠军联赛 赔率 数据
  957. *每个请求 包含 N个联赛 下 N条赔率
  958. */
  959. public function setOddsCH_v1($data)
  960. {
  961. try {
  962. //开启事务
  963. DB::beginTransaction();
  964. $obt = $data['data'];
  965. //请求 数据 为空
  966. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  967. $getData = $this->getAddData($obt);
  968. //不是 冠军 赔率 数据
  969. if ($getData['title'] != 'odds_ch') throw new \Exception(Response::generate('不是冠军赔率数据,', Response::ABNORMAL));
  970. //写请求数据 日志记录
  971. if ($this->isRecord) {
  972. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'], $obt, $getData);
  973. if ($setSportsRecord < 1) throw new \Exception(Response::generate('', Response::SPORTS_RECORD_ERR));
  974. }
  975. //获取球类代码
  976. $game_code = $getData['game_code'];
  977. //获取数据源
  978. $source = $getData['source'];
  979. //获取球类名称
  980. $gameName = gameModel::getGameName($game_code);
  981. //获取 model
  982. $models = commonFunction::getModels($game_code, 1);
  983. //获取 赔率数据
  984. $oddsData = $getData['data'];
  985. //获取 当前 所有 可用 赔率 sole
  986. $odds_only = $getData['odds_only'];
  987. //====验证 请求里 所有赔率 所属联赛 是否存在 ====
  988. //获取 当前请求 所有 联赛 uuid
  989. $uuids = [];
  990. $s_lg_ids = [];
  991. foreach ($oddsData as $k => $v) {
  992. $uuids[] = $v['uuid'];
  993. $s_lg_ids[] = $v['lg_id'];
  994. }
  995. $s_lg_ids = array_unique($s_lg_ids);
  996. $uuids = array_unique($uuids);
  997. sort($uuids);
  998. sort($s_lg_ids);
  999. //更新联赛下所有 不在本次请求的赔率 状态
  1000. $ret = OddsModel::upOddsStatus($models, '', $s_lg_ids, $source, $odds_only);
  1001. if ($ret != true) throw new \Exception(Response::generate($gameName, Response::ODDS_SOLE_ERR));
  1002. //获取 本地 已存在 联赛
  1003. $l_lg_data = $models['model_local_league']::whereIn('identity', $uuids)->select('identity', 'lg_id')->get()->toArray();
  1004. //二维数组去重
  1005. $l_lg_data = commonFunction::uniquArrV2($l_lg_data, 'identity');
  1006. //循环对比 请求uuid->本地uuid
  1007. foreach ($l_lg_data as $k => $v) {
  1008. foreach ($uuids as $kk => $uuid) {
  1009. if ($v['identity'] == $uuid) {
  1010. unset($uuids[$kk]);
  1011. }
  1012. }
  1013. }
  1014. //去除本地和请求里都存在的联赛,如果还有剩余联赛id,则返回异常;
  1015. if (!empty($uuids)) throw new \Exception(Response::generate($gameName . '冠军联赛:uuid-' . $uuids[0] . ';', Response::LEAGUE_ERROR));
  1016. //====end====
  1017. //赔率记录数据
  1018. $set_odds_r = [];
  1019. //赔率数据
  1020. $set_odds = [];
  1021. if (!empty($oddsData)) {
  1022. foreach ($oddsData as $k => $v) {
  1023. //获取 本地 联赛 ID
  1024. $lg_id = commonFunction::searcharray($v['uuid'], 'identity', $l_lg_data, 'lg_id');
  1025. //组装赔率记录数据
  1026. $set_odds_r[] = [
  1027. 'match_id' => 0,
  1028. 'others_match_id' => $v['match_id'] ?: 0,
  1029. 'odds_code' => $v['odds_code'] ?: '',
  1030. 'status' => $v['status'] ?: 0,
  1031. 'sort' => $v['sort'] ?: 0,
  1032. 'p_code' => $v['p_code'] ?: '',
  1033. 'odds' => $v['odds'] ?: 0,
  1034. 'condition' => $v['condition'],
  1035. 'odds_only' => $v['odds_only'] ?: '',
  1036. 'source' => $v['source'] ?: '',
  1037. 'type' => $v['type'] ?: 1,
  1038. 'team' => $v['team'] ?: '',
  1039. 'lg_id' => $lg_id,
  1040. 'others_lg_id' => $v['lg_id'],
  1041. 'ctime' => date('Y-m-d H:i:s'),
  1042. 'utime' => date('Y-m-d H:i:s'),
  1043. ];
  1044. }
  1045. foreach ($oddsData as $k => $v) {
  1046. //获取 本地 联赛 ID
  1047. $lg_id = commonFunction::searcharray($v['uuid'], 'identity', $l_lg_data, 'lg_id');
  1048. //组装赔率数据
  1049. $set_odds[] = [
  1050. 'match_id' => 0,
  1051. 'others_match_id' => $v['match_id'] ?: 0,
  1052. 'odds_code' => $v['odds_code'] ?: '',
  1053. 'status' => $v['status'] ?: 0,
  1054. 'sort' => $v['sort'] ?: 0,
  1055. 'p_code' => $v['p_code'] ?: '',
  1056. 'odds' => $v['odds'] ?: 0,
  1057. 'condition' => $v['condition'],
  1058. 'odds_only' => $v['odds_only'] ?: '',
  1059. 'source' => $v['source'] ?: '',
  1060. 'type' => $v['type'] ?: 1,
  1061. 'team' => $v['team'] ?: '',
  1062. 'lg_id' => $lg_id,
  1063. 'others_lg_id' => $v['lg_id'],
  1064. 'ctime' => date('Y-m-d H:i:s'),
  1065. 'utime' => date('Y-m-d H:i:s'),
  1066. 'sole' => $v['sole'],
  1067. 'expire_time' => date('Y-m-d H:i:s', time() + 60)
  1068. ];
  1069. }
  1070. //写赔率记录数据
  1071. $ret = $models['model_odds_record']::insert($set_odds_r);
  1072. if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率记录;', Response::ADD_ODDS_R_ERROR));
  1073. //写赔率 数据
  1074. $ret = $models['model_odds']::insert($set_odds);
  1075. if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率;', Response::ADD_ODDS_ERROR));
  1076. }
  1077. $this->writeLog($data, Response::success());
  1078. //提交事务
  1079. DB::commit();
  1080. return Response::success();
  1081. } catch (\Exception $e) {
  1082. //回滚事务
  1083. DB::rollBack();
  1084. $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  1085. return $e->getMessage();
  1086. }
  1087. }
  1088. /*
  1089. *写 冠军联赛 赔率 数据
  1090. *每个请求 包含 1个联赛 下 N条赔率
  1091. *追加赔率过滤
  1092. */
  1093. public function setOddsCH($data)
  1094. {
  1095. try {
  1096. //开启事务
  1097. DB::beginTransaction();
  1098. $obt = $data['data'];
  1099. //请求 数据 为空
  1100. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  1101. $getData = $this->getAddData($obt);
  1102. //不是 冠军 赔率 数据
  1103. if ($getData['title'] != 'odds_ch') throw new \Exception(Response::generate('不是冠军赔率数据,', Response::ABNORMAL));
  1104. /*
  1105. //写请求数据 日志记录
  1106. if($this->isRecord){
  1107. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'],$obt,$getData);
  1108. if($setSportsRecord < 1) throw new \Exception(Response::generate('',Response::SPORTS_RECORD_ERR));
  1109. }
  1110. */
  1111. //获取球类代码
  1112. $game_code = $getData['game_code'];
  1113. //获取数据源
  1114. $source = $getData['source'];
  1115. //获取球类名称
  1116. $gameName = gameModel::getGameName($game_code);
  1117. //获取数据源联赛ID
  1118. $s_lg_id = $getData['lg_id'];
  1119. //获取源数据联赛UUID
  1120. $uuid = $getData['uuid'];
  1121. //获取 model
  1122. $models = commonFunction::getModels($game_code, 1);
  1123. //获取 赔率数据
  1124. $oddsData = $getData['data'];
  1125. //验证本次请求所属联赛/赛事是否存在 返回本地联赛/赛事ID
  1126. $lg_id = $this->leagueVerify($models, $uuid, $source, $gameName);
  1127. //===追加处理赔率过滤===
  1128. //获取当前联赛 在本地的所有赔率
  1129. $l_odds_data = $models['model_odds']::select('odds_only')->where(['lg_id' => $lg_id, 'type' => 1])->get()->toArray();
  1130. //获取请求待写入数据
  1131. $setOddsData = [];
  1132. //获取本地待删除数据
  1133. $delOddsData = [];
  1134. //本地没有该赛事赔率,则直接全部写入
  1135. if (empty($l_odds_data)) {
  1136. $setOddsData = $oddsData;
  1137. } //本地有该赛事赔率,则分别获取需写入/删除的数据
  1138. else {
  1139. $newodds_t = $this->ArrayKeySet($oddsData, 'odds_only');
  1140. $oldodds_t = $this->ArrayKeySet($l_odds_data, 'odds_only');
  1141. foreach ($newodds_t as $key => $val) {
  1142. if (isset($oldodds_t[$key])) {
  1143. unset($newodds_t[$key]);
  1144. unset($oldodds_t[$key]);
  1145. }
  1146. }
  1147. $oddsData = $newodds_t;
  1148. $l_odds_data = $oldodds_t;
  1149. sort($oddsData);
  1150. sort($l_odds_data);
  1151. //需写入请求数据
  1152. $setOddsData = $oddsData;
  1153. //需删除本地数据
  1154. $delOddsData = $l_odds_data;
  1155. }
  1156. //===end===
  1157. //删除本地不在请求中的数据
  1158. if (!empty($delOddsData)) {
  1159. //所有需删除数据odds_only
  1160. $odds_onlys = [];
  1161. foreach ($delOddsData as $k => $v) {
  1162. $odds_onlys[] = $v['odds_only'];
  1163. }
  1164. $del = $models['model_odds']::whereIn('odds_only', $odds_onlys)->delete();
  1165. if ($del != count($odds_onlys)) throw new \Exception(Response::generate($gameName, Response::DEL_ODDS_ERR));
  1166. }
  1167. //写入请求中本地不存在的数据
  1168. if (!empty($setOddsData)) {
  1169. foreach ($setOddsData as $k => $v) {
  1170. //组装赔率记录数据
  1171. $set_odds_r[] = [
  1172. 'match_id' => 0,
  1173. 'others_match_id' => $v['match_id'] ?: 0,
  1174. 'odds_code' => $v['odds_code'] ?: '',
  1175. 'status' => $v['status'] ?: 0,
  1176. 'sort' => $v['sort'] ?: 0,
  1177. 'p_code' => $v['p_code'] ?: '',
  1178. 'odds' => $v['odds'] ?: 0,
  1179. 'condition' => $v['condition'],
  1180. 'odds_only' => $v['odds_only'] ?: '',
  1181. 'source' => $v['source'] ?: '',
  1182. 'type' => $v['type'] ?: 1,
  1183. 'team' => $v['team'] ?: '',
  1184. 'lg_id' => $lg_id,
  1185. 'others_lg_id' => $v['lg_id'],
  1186. 'ctime' => date('Y-m-d H:i:s'),
  1187. 'utime' => date('Y-m-d H:i:s'),
  1188. ];
  1189. }
  1190. foreach ($oddsData as $k => $v) {
  1191. //组装赔率数据
  1192. $set_odds[] = [
  1193. 'match_id' => 0,
  1194. 'others_match_id' => $v['match_id'] ?: 0,
  1195. 'odds_code' => $v['odds_code'] ?: '',
  1196. 'status' => $v['status'] ?: 0,
  1197. 'sort' => $v['sort'] ?: 0,
  1198. 'p_code' => $v['p_code'] ?: '',
  1199. 'odds' => $v['odds'] ?: 0,
  1200. 'condition' => $v['condition'],
  1201. 'odds_only' => $v['odds_only'] ?: '',
  1202. 'source' => $v['source'] ?: '',
  1203. 'type' => $v['type'] ?: 1,
  1204. 'team' => $v['team'] ?: '',
  1205. 'lg_id' => $lg_id,
  1206. 'others_lg_id' => $v['lg_id'],
  1207. 'ctime' => date('Y-m-d H:i:s'),
  1208. 'utime' => date('Y-m-d H:i:s'),
  1209. 'sole' => $v['sole'],
  1210. 'expire_time' => date('Y-m-d H:i:s', time() + 60)
  1211. ];
  1212. }
  1213. //写赔率记录数据
  1214. $ret = $models['model_odds_record']::insert($set_odds_r);
  1215. if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率记录;', Response::ADD_ODDS_R_ERROR));
  1216. //写赔率 数据
  1217. $ret = $models['model_odds']::insert($set_odds);
  1218. if ($ret != true) throw new \Exception(Response::generate($gameName . '赔率;', Response::ADD_ODDS_ERROR));
  1219. }
  1220. $this->writeLog($data, Response::success());
  1221. //提交事务
  1222. DB::commit();
  1223. return Response::success();
  1224. } catch (\Exception $e) {
  1225. //回滚事务
  1226. DB::rollBack();
  1227. $this->writeLog($data, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  1228. return $e->getMessage();
  1229. }
  1230. }
  1231. /*
  1232. * 写入直播 数据
  1233. */
  1234. public function setBroadCast($data)
  1235. {
  1236. $data_s = $data['data'];
  1237. try {
  1238. //开启事务
  1239. DB::beginTransaction();
  1240. $obt = $data['data'];
  1241. //请求 数据 为空
  1242. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  1243. $getData = $this->getAddData($obt);
  1244. //不是 直播 数据
  1245. if ($getData['title'] != 'broad_cast') throw new \Exception(Response::generate('不是直播数据,', Response::ABNORMAL));
  1246. //写请求数据 日志记录
  1247. if ($this->isRecord) {
  1248. $setSportsRecord = St_set_sports_recordModel::setSportsRecord($getData['title'], $obt, $getData);
  1249. if ($setSportsRecord < 1) throw new \Exception(Response::generate('', Response::SPORTS_RECORD_ERR));
  1250. }
  1251. //获取 球类
  1252. $game_code = $getData['game_code'];
  1253. //删除当前球类已存在的直播数据
  1254. $del = broadcastModel::where('game_code', $game_code)->delete();
  1255. if ($del != true) throw new \Exception(Response::generate('', Response::DEL_PAST_BROADCAST_ERR));
  1256. //获取 直播 数据
  1257. $BroadCast = $getData['data'];
  1258. $set_broadcast = [];
  1259. foreach ($BroadCast as $k => $data) {
  1260. //组装数据
  1261. $set_broadcast[] = [
  1262. "doing" => $data['doing'],
  1263. "game_type" => $data['game_type'],
  1264. "game_code" => $game_code,
  1265. "guest_team" => $data['guest_team'],
  1266. "host_team" => $data['host_team'],
  1267. "league_name" => $data['league_name'],
  1268. "shower" => $data['shower'],
  1269. "showid" => (int)$data['showid'],
  1270. "start_time" => $data['start_time'],
  1271. "ctime" => date('Y-m-d H:i:s'),
  1272. "utime" => date('Y-m-d H:i:s'),
  1273. ];
  1274. }
  1275. //写入 直播 数据
  1276. $ret = broadcastModel::insert($set_broadcast);
  1277. if ($ret == false) throw new \Exception(Response::generate('', Response::BROADCAST_ERROR));
  1278. $this->writeLog($data_s, Response::success());
  1279. //提交事务
  1280. DB::commit();
  1281. return Response::success();
  1282. } catch (\Exception $e) {
  1283. //回滚事务
  1284. DB::rollBack();
  1285. $this->writeLog($data_s, ['file' => $e->getFile(), 'line' => $e->getLine(), 'msg' => $e->getMessage()]);
  1286. return $e->getMessage();
  1287. }
  1288. }
  1289. /**
  1290. * 写赛事危险球数据
  1291. */
  1292. public function setMatchWarn($data)
  1293. {
  1294. try {
  1295. //开启事务
  1296. DB::beginTransaction();
  1297. $obt = $data['data'];
  1298. //请求 数据 为空
  1299. if (empty($obt)) throw new \Exception(Response::generate('请求数据为空,', Response::ABNORMAL));
  1300. $getData = $this->getAddData($obt);
  1301. //不是 赛事 危险球 数据
  1302. if ($getData['title'] != 'match_warn') throw new \Exception(Response::generate('不是危险球数据,', Response::ABNORMAL));
  1303. //获取球类代码
  1304. $game_code = $getData['game_code'];
  1305. //获取数据源
  1306. $source = $getData['source'];
  1307. //获取数据源赛事id
  1308. $others_match_id = $getData['match_id'];
  1309. //获取数据源赛事uuid
  1310. $match_identity = $getData['match_identity'];
  1311. //获取球类名称
  1312. $gameName = gameModel::getGameName($game_code);
  1313. //获取 model
  1314. $models = commonFunction::getModels($game_code, 1);
  1315. //获取 赛事 数据
  1316. $warn_data = $getData['data'];
  1317. //获取赛事 match_id
  1318. $match_id = $models['model_local_match']::SELECT('others_match_id', 'match_id')
  1319. //->where(['source' => $source, 'identity' => $match_identity])
  1320. ->where(['identity' => $match_identity])
  1321. ->first()->match_id;
  1322. if (!empty($match_id)) {
  1323. //更新 危险球
  1324. $ret = ZqResultModel::set_result_warn($match_id, $warn_data);
  1325. if ($ret < 1) throw new \Exception(Response::generate('', Response::SET_WARN_ERR));
  1326. //追加 滚球投注 危险球审核
  1327. SportsNoteListModel::WarnHandle($match_id, $warn_data);
  1328. }
  1329. //提交事务
  1330. DB::commit();
  1331. //写请求数据 日志记录
  1332. $this->writeLog($data, Response::success());
  1333. return Response::success();
  1334. } catch (\Exception $e) {
  1335. //回滚事务
  1336. DB::rollBack();
  1337. //写请求数据 日志记录
  1338. if ($this->isRecord) commonFunction::SET_SPORTS_RECORD($game_code, $getData, $getData['title'], $e->getMessage());
  1339. return $e->getMessage();
  1340. }
  1341. }
  1342. /**
  1343. * 定时执行 滚球投注危险球审核
  1344. */
  1345. public function HandleOrder()
  1346. {
  1347. try {
  1348. //开启事务
  1349. DB::beginTransaction();
  1350. $time = 60;
  1351. //处理指定时间内的滚球待审核订单
  1352. SportsNoteListModel::getOrderData($time);
  1353. //提交事务
  1354. DB::commit();
  1355. //写请求数据 日志记录
  1356. $this->writeLog(['HandleOrder' => 'ok'], Response::success());
  1357. return Response::success();
  1358. } catch (\Exception $e) {
  1359. //回滚事务
  1360. DB::rollBack();
  1361. //写请求数据 日志记录
  1362. $this->writeLog(['HandleOrder' => 'no', 'msg' => $e->getMessage()], Response::success());
  1363. return $e->getMessage();
  1364. }
  1365. }
  1366. /*
  1367. * 验证所属 联赛 是否存在
  1368. */
  1369. public function leagueVerify($models = [], $uuid = '', $source = '', $gameName = '')
  1370. {
  1371. if (empty($models) || empty($uuid)) throw new \Exception(Response::generate('', Response::ABNORMAL));
  1372. $l_lg_id = $models['model_local_league']::where(['identity' => $uuid])->value('lg_id');
  1373. if ($l_lg_id < 1) throw new \Exception(Response::generate($gameName . '联赛:lg_id-' . $uuid, Response::LEAGUE_ERROR));
  1374. return $l_lg_id;
  1375. }
  1376. /*
  1377. * 验证所属 赛事 是否存在
  1378. */
  1379. public function matchVerify($models = [], $s_match_id = '', $source = '', $gameName = '')
  1380. {
  1381. if (empty($models) || empty($s_match_id)) throw new \Exception(Response::generate('', Response::ABNORMAL));
  1382. $l_match_id = $models['model_local_match']::where(['others_match_id' => $s_match_id, 'source' => $source])->value('match_id');
  1383. if ($l_match_id < 1) throw new \Exception(Response::generate($gameName . '赛事:match_id-' . $s_match_id, Response::MATCH_ERROR));
  1384. return $l_match_id;
  1385. }
  1386. /*
  1387. * 验证所属 赛事 是否存在
  1388. */
  1389. public function matchVerify_v2($models = [], $match_identity = '', $source = '', $gameName = '')
  1390. {
  1391. if (empty($models) || empty($match_identity)) throw new \Exception(Response::generate('', Response::ABNORMAL));
  1392. //$l_match_id = $models['model_local_match']::where(['identity' => $match_identity, 'source' => $source])->value('match_id');
  1393. $l_match_id = $models['model_local_match']::where(['identity' => $match_identity])->value('match_id');
  1394. if ($l_match_id < 1) throw new \Exception(Response::generate($gameName . 'match_identity-' . $match_identity, Response::MATCH_ERROR));
  1395. return $l_match_id;
  1396. }
  1397. /*
  1398. * 写入直播 数据
  1399. */
  1400. public function broadcast($opt = [], $game_code = '')
  1401. {
  1402. $data = $opt['data'];
  1403. $set_broadcast = [
  1404. "doing" => $data['doing'],
  1405. "game_type" => $data['game_type'],
  1406. "game_code" => $game_code,
  1407. "guest_team" => $data['guest_team'],
  1408. "host_team" => $data['host_team'],
  1409. "league_name" => $data['league_name'],
  1410. "shower" => $data['shower'],
  1411. "showid" => (int)$data['showid'],
  1412. "start_time" => $data['start_time'],
  1413. "ctime" => date('Y-m-d H:i:s'),
  1414. "utime" => date('Y-m-d H:i:s'),
  1415. ];
  1416. $ret = broadcastModel::insert($set_broadcast);
  1417. if ($ret == false) throw new \Exception(Response::generate('', Response::BROADCAST_ERROR));
  1418. }
  1419. /*
  1420. * 写入 赛事 初始 结果
  1421. */
  1422. public function match_result($game_code = '', $match_id = 0)
  1423. {
  1424. $model = commonFunction::getModels($game_code, 1);
  1425. if ($game_code == 'zq') ZqResultModel::ZQresult_v2($model, $match_id);
  1426. if ($game_code == 'lq') LqResultModel::LQresult_v2($model, $match_id);
  1427. if ($game_code == 'wq') WqResultModel::WQresult_v2($model, $match_id);
  1428. if ($game_code == 'bq') BqResultModel::BQresult_v2($model, $match_id);
  1429. }
  1430. /*
  1431. * 写入联赛数据
  1432. */
  1433. public function league($opt = [], $gameName = '')
  1434. {
  1435. $game_code = $opt['game_code'];
  1436. //根据球类代码获取相关model
  1437. $model = commonFunction::getModels($game_code, 1);
  1438. $data = $opt['data'];
  1439. //查询联赛是否已存在
  1440. $id = $model['model_league']::where('name_chinese', '=', $data['name_chinese'])
  1441. ->value('id');
  1442. //默认获取本年最后一天
  1443. $last_time = date('Y-12-31 23:59:59');
  1444. //决赛时间
  1445. if ($data['last_time']) {
  1446. $last_time = $data['last_time'];
  1447. }
  1448. $set_lg['name_chinese'] = $data['name_chinese'];
  1449. $set_lg['kind'] = $data['kind'];
  1450. $set_lg['match_mode'] = $data['match_mode'];
  1451. $set_lg['if_stop'] = $data['if_stop'];
  1452. $set_lg['identity'] = $data['uuid'] ?: '';
  1453. $set_lg['last_time'] = $last_time;
  1454. $set_lg['utime'] = date('Y-m-d H:i:s');
  1455. if (empty($id)) {
  1456. //写入联赛
  1457. $id = $model['model_league']::insertGetId($set_lg);
  1458. $m_lg_id = $id;
  1459. if ($m_lg_id < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $data['lg_id'] . ';', Response::INSERT_ERROR));
  1460. } else {
  1461. //更新联赛
  1462. $ret = $model['model_league']::where(['id' => $id])
  1463. ->update($set_lg);
  1464. if ($ret < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $data['lg_id'] . ';', Response::UPDATE_ERROR));
  1465. }
  1466. $set_local = [
  1467. 'lg_id' => $id,
  1468. 'others_lg_id' => $data['lg_id'],
  1469. 'source' => $data['source'],
  1470. 'ctime' => date('Y-m-d H:i:s')
  1471. ];
  1472. $ret = $model['model_local_league']::insertGetId($set_local);
  1473. 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'));
  1474. //执行成功返回源数据 联赛ID
  1475. $data = ['others_lg_id' => $data['lg_id'], 'lg_id' => $id];
  1476. return $data;
  1477. }
  1478. /*
  1479. * 写入赛事数据
  1480. */
  1481. public function competition($opt = [], $gameName = '', $lg_id = '')
  1482. {
  1483. $game_code = $opt['game_code'];
  1484. //根据球类代码获取相关model
  1485. $model = commonFunction::getModels($game_code, 1);
  1486. $data = $opt['data'];
  1487. if (empty($data['lg_id'])) throw new \Exception(Response::generate('', Response::LEAGUE_ERROR));//Render([], '10015', lang('Tips','Sports')->get('league_error'));
  1488. //查询赛事是否存在
  1489. $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']])
  1490. ->value('id');
  1491. $half_match_id = 0;
  1492. //如果有上半场赛事id 获取上半场赛事是否存在
  1493. if (!empty($data['half_match_id'])) {
  1494. $half_match_id = $match_id = $model['model_local_match']::where(['others_match_id' => $data['half_match_id'], 'source' => $data['source']])
  1495. ->value('match_id');
  1496. 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'));
  1497. }
  1498. //如果赛事没有开始日期,则为冠军盘口赛事
  1499. if (empty($data['match_date'])) {
  1500. //冠军盘口赛事获取所属联赛结束时间
  1501. $last_time = $model['model_league']::where(['id' => $lg_id])
  1502. ->value('last_time');
  1503. 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'));
  1504. //给冠军盘口赛事 赋值时间
  1505. $time = strtotime($last_time);
  1506. $data['match_date'] = date('Y-m-d', $time);
  1507. $data['match_time'] = date('H:i:s', $time);
  1508. }
  1509. $set_match = [
  1510. 'ctime' => date('Y-m-d H:i:s'),
  1511. 'utime' => date('Y-m-d H:i:s'),
  1512. 'expire_time' => date('Y-m-d H:i:s', time() + 60),
  1513. 'home_team' => $data['home_team'] ?: '',
  1514. 'guest_team' => $data['guest_team'] ?: 'no_team',
  1515. 'lg_id' => $lg_id,
  1516. 'status' => $data['status'],
  1517. 'match_date' => $data['match_date'] ?: date('Y-m-d'),
  1518. 'match_time' => $data['match_time'] ?: date('H:i:s'),
  1519. 'tag' => $data['tag'] ?: 0,
  1520. 'is_rollball' => $data['is_rollball'] ?: 0,
  1521. 'is_today' => $data['is_today'] ?: 0,
  1522. 'is_morningplate' => $data['is_morningplate'] ?: 0,
  1523. 'is_stringscene' => $data['is_stringscene'] ?: 0,
  1524. 'us_time' => $data['us_time'] ?: commonFunction::qgmdate('Y-m-d H:i:s', '', -4),
  1525. 'half_match_id' => $half_match_id ?: 0,
  1526. 'identity' => $data['uuid'],
  1527. ];
  1528. //写入赛事
  1529. if (empty($id)) {
  1530. //写入赛事 返回id
  1531. $id = $model['model_match']::insertGetId($set_match);
  1532. if ($id < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::INSERT_ERROR));
  1533. } else {
  1534. //更新赛事
  1535. $ret = $model['model_match']::where(['id' => $id])->update($set_match);
  1536. if ($ret < 1) throw new \Exception(Response::generate($gameName . '赛事-match_id:' . $data['match_id'] . ';', Response::UPDATE_ERROR));
  1537. }
  1538. //写关联记录
  1539. $set_local = [
  1540. 'match_id' => $id,
  1541. 'others_match_id' => $data['match_id'],
  1542. 'source' => $data['source'],
  1543. 'ctime' => date('Y-m-d H:i:s')
  1544. ];
  1545. $ret = $model['model_local_match']::insertGetId($set_local);
  1546. 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'));
  1547. //返回 源数据 赛事ID
  1548. $data = ['others_match_id' => $data['match_id'], 'match_id' => $id];
  1549. return $data;
  1550. }
  1551. /*
  1552. * 写入赔率/赔率记录数据
  1553. */
  1554. public function odds($opt = [], $gameName = '', $lg_id = 0, $match_id = 0, $oddsID = '', $oddsRecordID = '')
  1555. {
  1556. $game_code = $opt['game_code'];
  1557. //根据球类代码获取相关model
  1558. $model = commonFunction::getModels($game_code, 1);
  1559. $data = $opt['data'];
  1560. //获取赛事 本地/源ID
  1561. $others_match_id = $data['match_id'];
  1562. //获取联赛 本地/源ID
  1563. $others_lg_id = $data['lg_id'];
  1564. //===写赔率记录===
  1565. $set_odds_r = [
  1566. 'match_id' => $match_id ?: 0,
  1567. 'others_match_id' => $others_match_id ?: 0,
  1568. 'odds_code' => $data['odds_code'] ?: '',
  1569. 'status' => $data['status'] ?: 0,
  1570. 'sort' => $data['sort'] ?: 0,
  1571. 'p_code' => $data['p_code'] ?: '',
  1572. 'odds' => $data['odds'] ?: 0,
  1573. 'condition' => $data['condition'],
  1574. 'odds_only' => $data['odds_only'] ?: '',
  1575. 'source' => $data['source'] ?: '',
  1576. 'type' => $data['type'] ?: 0,
  1577. 'team' => $data['team'] ?: '',
  1578. 'lg_id' => $lg_id,
  1579. 'others_lg_id' => $others_lg_id,
  1580. 'ctime' => date('Y-m-d H:i:s'),
  1581. ];
  1582. //更新或写入赔率记录
  1583. if ($oddsRecordID != false) {
  1584. $ret = $model['model_odds_record']::where(['id' => $oddsRecordID])
  1585. ->update($set_odds_r);
  1586. 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'));
  1587. } else {
  1588. $ret = $model['model_odds_record']::insert($set_odds_r);
  1589. 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'));
  1590. }
  1591. //===写赔率===
  1592. $set_odds = [
  1593. 'match_id' => $match_id ?: 0,
  1594. 'others_match_id' => $others_match_id ?: 0,
  1595. 'odds_code' => $data['odds_code'] ?: '',
  1596. 'status' => $data['status'] ?: 0,
  1597. 'sort' => $data['sort'] ?: 0,
  1598. 'p_code' => $data['p_code'] ?: '',
  1599. 'odds' => $data['odds'] ?: 0,
  1600. 'condition' => $data['condition'],
  1601. 'odds_only' => $data['odds_only'] ?: '',
  1602. 'sole' => $data['sole'] ?: '',
  1603. 'source' => $data['source'] ?: '',
  1604. 'type' => $data['type'] ?: 0,
  1605. 'team' => $data['team'] ?: '',
  1606. 'lg_id' => $lg_id,
  1607. 'others_lg_id' => $others_lg_id,
  1608. 'ctime' => date('Y-m-d H:i:s'),
  1609. 'utime' => date('Y-m-d H:i:s'),
  1610. 'expire_time' => date('Y-m-d H:i:s', time() + 60),
  1611. ];
  1612. //更新或写入赔率数据
  1613. if ($oddsID != false) {
  1614. $ret = $model['model_odds']::where(['id' => $oddsID])
  1615. ->update($set_odds);
  1616. 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'));
  1617. } else {
  1618. $ret = $model['model_odds']::insert($set_odds);
  1619. 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'));
  1620. }
  1621. //===end===
  1622. }
  1623. /*
  1624. * 更新 赛事下 所有不在本次请求的 赔率状态
  1625. */
  1626. public function upOddsStatus($opt = [], $gameName = '')
  1627. {
  1628. //获取球类代码
  1629. $game_code = $opt['game_code'];
  1630. //获取球类model
  1631. $model = commonFunction::getModels($game_code, 1);
  1632. //赛事下 所有 赔率sole
  1633. $odds_sole = $opt['data'];
  1634. //数据源 赛事 id
  1635. $others_match_id = $opt['match_id'];
  1636. //数据源
  1637. $source = $opt['source'];
  1638. //更新赛事下所有 不在本次请求的赔率 状态
  1639. $ret = OddsModel::getMatchSole($model, $others_match_id, '', $source, $odds_sole);
  1640. if ($ret != true) throw new \Exception(Response::generate($gameName, Response::ODDS_SOLE_ERR));
  1641. }
  1642. /*
  1643. * 写入联赛结果
  1644. */
  1645. public function league_result($opt, $gameName)
  1646. {
  1647. $game_code = $opt['game_code'];
  1648. //根据球类代码获取相关model
  1649. $model = commonFunction::getModels($game_code, 1);
  1650. $data = $opt['data'];
  1651. //验证结果所属联赛
  1652. $lg_id = $model['model_local_league']::where(['others_lg_id' => $data['lg_id'], 'source' => $data['source']])
  1653. ->value('lg_id');
  1654. if ($lg_id < 1) throw new \Exception(Response::generate($gameName . '联赛-lg_id:' . $data['lg_id'] . ';', Response::LEAGUE_ERROR));
  1655. $lg_result_id = $model['model_league_result']::where(['lg_id' => $lg_id, 'game_name' => $data['game_name']])
  1656. ->value('id');
  1657. $set_lg_result = [
  1658. 'lg_id' => $lg_id,
  1659. 'game_name' => $data['game_name'],
  1660. 'result' => json_encode($data['result'], JSON_UNESCAPED_UNICODE),
  1661. 'status' => $data['status'],
  1662. 'ctime' => date('Y-m-d H:i:s'),
  1663. 'utime' => date('Y-m-d H:i:s'),
  1664. ];
  1665. //联赛结果数据处理
  1666. if (!empty($lg_result_id)) {
  1667. $ret = $model['model_league_result']::where(['id' => $lg_result_id])
  1668. ->update($set_lg_result);
  1669. 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'));
  1670. } else {
  1671. $ret = $model['model_league_result']::insert($set_lg_result);
  1672. 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'));
  1673. }
  1674. }
  1675. /*
  1676. * 写入赛事结果
  1677. */
  1678. public function com_result($opt = [], $gameName = '', $lg_id = '', $match_id = '')
  1679. {
  1680. $game_code = $opt['game_code'];
  1681. //根据球类代码获取相关model
  1682. $model = commonFunction::getModels($game_code, 1);
  1683. $data = $opt['data'];
  1684. //查询结果是否存在
  1685. $match_r_id = $model['model_result']::where(['match_id' => $match_id])
  1686. ->value('id');
  1687. $set_match_r = [
  1688. "home_team" => $data['home_team'],
  1689. "guest_team" => $data['guest_team'],
  1690. "lg_id" => $lg_id,
  1691. "home_rate" => $data['home_rate'],
  1692. "guest_rate" => $data['guest_rate'],
  1693. "home_score" => $data['home_score'],
  1694. "guest_score" => $data['guest_score'],
  1695. "all_goal" => $data['all_goal'],
  1696. "status" => $data['status'],
  1697. "first_score" => $data['first_score'],
  1698. "last_score" => $data['last_score'],
  1699. "match_score" => $data['match_score'],
  1700. "match_winer" => $data['match_winer'],
  1701. "match_time" => $data['match_time'],
  1702. "match_process" => $data['match_process'],
  1703. "tag" => $data['tag'],
  1704. "match_id" => $match_id,
  1705. "u_home_score" => $data['u_home_score'],
  1706. "u_guest_score" => $data['u_guest_score'],
  1707. "p_code" => $data['p_code'],
  1708. "update_time" => date('Y-m-d H:i:s')
  1709. ];
  1710. //赛事结果数据处理
  1711. if (!empty($match_r_id)) {
  1712. $ret = $model['model_result']::where(['id' => $match_r_id])
  1713. ->update($set_match_r);
  1714. 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'));
  1715. } else {
  1716. $ret = $model['model_result']::insert($set_match_r);
  1717. 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'));
  1718. }
  1719. // return Response::success();
  1720. }
  1721. //写入赛事结果记录
  1722. public function com_result_record($opt = [], $gameName = '', $lg_id = '', $match_id = '')
  1723. {
  1724. $game_code = $opt['game_code'];
  1725. //根据球类代码获取相关model
  1726. $model = commonFunction::getModels($game_code, 1);
  1727. $data = $opt['data'];
  1728. $match_r_id = $model['model_result_record']::where(['match_id' => $match_id, 'match_time' => $data['match_time']])
  1729. ->value('id');
  1730. //根据球类 获取 赛事结果记录字段
  1731. $set_match_r = $this->get_match_r($game_code, $lg_id, $match_id, $data);
  1732. //赛事结果记录处理
  1733. if ($match_r_id > 0) {
  1734. $ret = $model['model_result_record']::where(['id' => $match_r_id])
  1735. ->update($set_match_r);
  1736. 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'));
  1737. } else {
  1738. $ret = $model['model_result_record']::insert($set_match_r);
  1739. 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'));
  1740. }
  1741. }
  1742. /**
  1743. * @param $data
  1744. * @return mixed
  1745. * json转数组
  1746. */
  1747. public function getAddData($data)
  1748. {
  1749. $data = json_decode($data, true);
  1750. return $data;
  1751. }
  1752. /*
  1753. * 根据球类获取 赛事结果记录字段
  1754. */
  1755. public function get_match_r($game_code, $lg_id, $match_id, $data)
  1756. {
  1757. $set_match_r = [];
  1758. if ($game_code == 'zq') {
  1759. $set_match_r = [
  1760. "home_team" => $data['home_team'] ?: '',
  1761. "guest_team" => $data['guest_team'] ?: '',
  1762. "lg_id" => $lg_id,
  1763. "all_goal" => $data['all_goal'] ?: 0,
  1764. "home_rate" => $data['home_rate'] ?: 0,
  1765. "guest_rate" => $data['guest_rate'] ?: 0,
  1766. "home_score" => $data['home_score'] ?: 0,
  1767. "guest_score" => $data['guest_score'] ?: 0,
  1768. "status" => $data['status'] ?: 0,
  1769. "first_score" => $data['first_score'] ?: 0,
  1770. "last_score" => $data['last_score'] ?: 0,
  1771. "match_score" => $data['match_score'] ?: 0,
  1772. "match_winer" => $data['match_winer'] ?: '',
  1773. "match_time" => $data['match_time'] ?: 0,
  1774. "match_process" => $data['match_process'] ?: '',
  1775. // "tag"=> $data['tag']?:0,
  1776. "match_id" => $match_id,
  1777. "update_time" => date('Y-m-d H:i:s')
  1778. ];
  1779. };
  1780. if ($game_code == 'lq') {
  1781. $set_match_r = [
  1782. "home_team" => $data['home_team'] ?: '',
  1783. "guest_team" => $data['guest_team'] ?: '',
  1784. "lg_id" => $lg_id,
  1785. "home_rate" => $data['home_rate'] ?: 0,
  1786. "guest_rate" => $data['guest_rate'] ?: 0,
  1787. "home_score" => $data['home_score'] ?: 0,
  1788. "guest_score" => $data['guest_score'] ?: 0,
  1789. "status" => $data['status'] ?: 0,
  1790. "first_score" => $data['first_score'] ?: 0,
  1791. "last_score" => $data['last_score'] ?: 0,
  1792. "match_score" => $data['match_score'] ?: 0,
  1793. "match_winer" => $data['match_winer'] ?: '',
  1794. "match_time" => $data['match_time'] ?: 0,
  1795. "match_process" => $data['match_process'] ?: '',
  1796. // "tag"=> $data['tag']?:0,
  1797. "match_id" => $match_id,
  1798. "update_time" => date('Y-m-d H:i:s'),
  1799. "result_mark" => json_encode($data['result_mark']) ?: '',
  1800. ];
  1801. }
  1802. if ($game_code == 'wq') {
  1803. $set_match_r = [
  1804. "home_player_name" => $data['home_player_name'] ?: '',
  1805. "guest_player_name" => $data['guest_player_name'] ?: '',
  1806. "lg_id" => $lg_id,
  1807. "home_player_let_plate" => $data['home_player_let_plate'] ?: 0,
  1808. "guest_player_let_plate" => $data['guest_player_let_plate'] ?: 0,
  1809. "home_player_let_inning" => $data['home_player_let_inning'] ?: 0,
  1810. "guest_player_let_inning" => $data['guest_player_let_inning'] ?: 0,
  1811. "all_inning" => $data['all_inning'] ?: 0,
  1812. "home_player_score" => $data['home_player_score'] ?: 0,
  1813. "guest_player_score" => $data['guest_player_score'] ?: 0,
  1814. "status" => $data['status'] ?: 0,
  1815. "first_score_player" => $data['first_score_player'] ?: '',
  1816. "last_score_player" => $data['last_score_player'] ?: '',
  1817. "first_inning_score" => $data['first_inning_score'] ?: 0,
  1818. "second_inning_score" => $data['second_inning_score'] ?: 0,
  1819. "third_inning_score" => $data['third_inning_score'] ?: 0,
  1820. "match_winer_player" => $data['match_winer_player'] ?: '',
  1821. "update_time" => date('Y-m-d H:i:s'),
  1822. "match_time" => $data['match_time'] ?: 0,
  1823. "match_process" => $data['match_process'] ?: '',
  1824. // "tag"=>$data['tag']?:0,
  1825. "match_id" => $match_id,
  1826. "result_mark" => json_encode($data['result_mark']) ?: '',
  1827. ];
  1828. }
  1829. if ($game_code == 'bq') {
  1830. $set_match_r = [
  1831. "home_team" => $data['home_team'] ?: '',
  1832. "guest_team" => $data['guest_team'] ?: '',
  1833. "lg_id" => $lg_id,
  1834. "home_rate" => $data['home_rate'] ?: 0,
  1835. "guest_rate" => $data['guest_rate'] ?: 0,
  1836. "home_score" => $data['home_score'] ?: 0,
  1837. "guest_score" => $data['guest_score'] ?: 0,
  1838. "status" => $data['status'] ?: 0,
  1839. "first_score" => $data['first_score'] ?: 0,
  1840. "last_score" => $data['last_score'] ?: 0,
  1841. "match_score" => $data['match_score'] ?: 0,
  1842. "match_winer" => $data['match_winer'] ?: '',
  1843. "match_time" => $data['match_time'] ?: 0,
  1844. "match_process" => $data['match_process'] ?: '',
  1845. // "tag"=> $data['tag']?:0,
  1846. "match_id" => $match_id,
  1847. "all_inning" => $data['all_inning'] ?: 9,
  1848. "update_time" => date('Y-m-d H:i:s'),
  1849. "result_mark" => json_encode($data['result_mark']) ?: '',
  1850. ];
  1851. }
  1852. return $set_match_r;
  1853. }
  1854. /**
  1855. * 定时执行 处理到时间应该开始而未开始的赛事
  1856. * 每分钟执行一次
  1857. */
  1858. public function HandleMatch()
  1859. {
  1860. try {
  1861. //开启事务
  1862. DB::beginTransaction();
  1863. $time = 60;//60 秒
  1864. //处理指定时间内的赛事
  1865. StMatchModel::HandleMatch($time);
  1866. //提交事务
  1867. DB::commit();
  1868. //写请求数据 日志记录
  1869. if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('all', [], 'HandleMatch', Response::success());
  1870. return Response::success();
  1871. } catch (\Exception $e) {
  1872. //回滚事务
  1873. DB::rollBack();
  1874. //写请求数据 日志记录
  1875. if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('all', [], 'HandleMatch', $e->getMessage());
  1876. return $e->getMessage();
  1877. }
  1878. }
  1879. /**
  1880. * 根据联赛名称 获取 国家 地区
  1881. */
  1882. public function getArea($leagueName = '')
  1883. {
  1884. //如果联赛名称有括号,去除
  1885. $leagueName = preg_replace('/\(.*?\)/', '', $leagueName);
  1886. //获取所有国家/地区
  1887. $areaData = StAreaCountryModel::select('id', 'pid', 'name')->get()->toArray();
  1888. //获取当前联赛所属国家
  1889. if (strpos($leagueName, 'NBA') !== false) {
  1890. $data = [
  1891. 'id' => 247,
  1892. 'pid' => 0,
  1893. 'name' => '世界'
  1894. ];
  1895. return $data;
  1896. }
  1897. //识别本地 国家地区数据
  1898. foreach ($areaData as $v => $k) {
  1899. if (strpos($leagueName, $k['name']) !== false) {
  1900. return $k;
  1901. }
  1902. }
  1903. //如果不能识别,则返回未知
  1904. $data = [
  1905. 'id' => 0,
  1906. 'pid' => 0,
  1907. 'name' => '未知国家或地区'
  1908. ];
  1909. return $data;
  1910. }
  1911. //写文本日志
  1912. private function writeLog($body, $ret)
  1913. {
  1914. go(function () use ($body, $ret) {
  1915. $json_data = json_encode($body, JSON_UNESCAPED_UNICODE);
  1916. $data = json_decode($body['data'], true);
  1917. $game_code = isset($data['game_code']) ? $data['game_code'] : '';
  1918. $title = isset($data['title']) ? $data['title'] : '';
  1919. $msg = is_string($ret) ? $ret : json_encode($ret, 256);
  1920. $now = explode(" ", microtime());
  1921. $wdata = date("Y-m-d", $now[1]);
  1922. $path = LOG_PATH . DS . $wdata . DS . $game_code . DS;
  1923. if (!file_exists($path)) {
  1924. $ret = mkdir($path, '0755', true);
  1925. if (!$ret) {
  1926. echo "$path --- Log File Create false \n";
  1927. return;
  1928. }
  1929. }
  1930. $lasttxt = date('Y-m-d H:i:s', $now[1]) . substr($now[0], 1, 5) . ' - ' . $msg . ' - ' . $json_data . "\n\n";
  1931. $file = $path . DS . $game_code . '_' . $title . '.log';
  1932. file_put_contents($file, $lasttxt, FILE_APPEND | LOCK_EX);
  1933. return;
  1934. });
  1935. return;
  1936. }
  1937. private function writeLog_db($body, $ret)
  1938. {
  1939. if (!is_string($body)) {
  1940. $body = json_encode([$body], 256);
  1941. }
  1942. if (!is_string($ret)) {
  1943. $ret = json_encode([$ret], 256);
  1944. }
  1945. $data = ['ctime' => date("Y-m-d H:i:s"), 'body' => $body, 'ret' => $ret];
  1946. $return = DB::table('datainf_log')->insert($data);
  1947. return $return;
  1948. }
  1949. }