DataLogic.php 63 KB

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