WriteSports.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Jun.peng
  5. * Date: 2019/5/13
  6. * Time: 10:19
  7. */
  8. namespace App\Sports\Controller;
  9. use BaseController\Controller;
  10. use App\Sports\Model\St_area_country as St_area_countryModel;
  11. class WriteSports extends Controller{
  12. public function init() {
  13. $this->commonFunction = C()->get('commonFunction');
  14. //当前时间
  15. $this->newTime = date('Y-m-d H:m:i');
  16. }
  17. /**
  18. * 体育数据入库接口
  19. */
  20. public function setSports(){
  21. $obt = $_REQUEST['data'];
  22. $data = $this->getAddData($obt);
  23. //验证二维数组内是否有空值
  24. // foreach ($data['data'] as $v){
  25. // if(in_array('', $v)) Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR'));
  26. // }
  27. switch ($data['title']){
  28. case 'area'://地区
  29. $ret = $this->area($data['data']);
  30. break;
  31. case 'country'://国家
  32. $ret = $this->country($data['data']);
  33. break;
  34. case 'league'://联赛
  35. $ret = $this->league($data);
  36. break;
  37. case 'competition'://赛事
  38. $ret = $this->competition($data);
  39. break;
  40. case 'odds'://赔率
  41. $ret = $this->odds($data);
  42. break;
  43. case 'league_result'://联赛结果
  44. $ret = $this->league_result($data);
  45. break;
  46. case 'competition_result'://赛事结果
  47. $ret = $this->com_result($data);
  48. break;
  49. case 'competition_result_record'://赛事结果记录
  50. $ret = $this->com_result_record($data);
  51. break;
  52. case 'odds_record'://赔率记录
  53. $ret = $this->odds_record($data);
  54. break;
  55. default:
  56. Render([], '10007', lang('Tips','Sports')->get('abnormal'));
  57. }
  58. //写入成功
  59. if($ret) Render([], '1', lang('Tips','Sports')->get('success'));
  60. //写入失败
  61. Render([], '10010', lang('Tips','Sports')->get('HANDLE_ERRORS'));
  62. }
  63. //写入地区数据
  64. public function area($opt = []){
  65. $ret = lm('st_area','Sports')->insert($opt);
  66. return $ret;
  67. }
  68. //写入国家数据
  69. public function country($opt = []){
  70. $ret = lm('st_country','Sports')->insert($opt);
  71. return $ret;
  72. }
  73. //写入联赛数据
  74. public function league($opt = []){
  75. $game_code = $opt['game_code'];
  76. //根据球类代码获取相关model
  77. $model = $this->commonFunction->getModels($game_code,1);
  78. $data = $opt['data'];
  79. if(empty($data['belong'])) Render([], '10013', lang('Tips','Sports')->get('country_error'));
  80. //获取联赛所属 国家/地区id
  81. $belong = St_area_countryModel::getID($data['belong']);
  82. $set_lg['area_id'] = $belong['area_id'];
  83. $set_lg['country_id'] = $belong['country_id'];
  84. //查询中间表 是否已记录
  85. $m_lg_id = lm($model['model_local_league'],'Sports')
  86. ->select('m_lg_id')
  87. ->where(['lg_id'=>$data['lg_id'],'source'=>$data['source']])
  88. ->first()->m_lg_id;
  89. //如果没有记录
  90. if(empty($m_lg_id)){
  91. //查询联赛是否已存在
  92. $id = lm($model['model_league'],'Sports')
  93. ->select('id')
  94. ->where('name_chinese','=',$data['name_chinese'])
  95. ->first()->id;
  96. if(empty($id)){
  97. $set_lg['name_chinese'] = $data['name_chinese'];
  98. $set_lg['kind'] = $data['kind'];
  99. $set_lg['match_mode'] = $data['match_mode'];
  100. $set_lg['if_stop'] = $data['if_stop'];
  101. $set_lg['last_time'] = $data['last_time'];
  102. $set_lg['utime'] = $this->newTime;
  103. $id = lm($model['model_league'],'Sports')->insertGetId($set_lg);
  104. $m_lg_id = $id;
  105. if($m_lg_id < 1) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  106. }
  107. $set_local = [
  108. 'm_lg_id'=>$id,
  109. 'lg_id'=>$data['lg_id'],
  110. 'source'=>$data['source'],
  111. ];
  112. $ret = lm($model['model_local_league'],'Sports')-> insertGetId($set_local);
  113. if($ret < 1) Render([], '10017', lang('Tips','Sports')->get('local_league_error'));
  114. }
  115. Render([], '1', lang('Tips','Sports')->get('success'));
  116. }
  117. //弃用
  118. public function __league($opt = []){
  119. $game_code = $opt['game_code'];
  120. //根据球类代码获取相关model
  121. $model = $this->commonFunction->getModels($game_code,1);
  122. $data = $opt['data'];
  123. // foreach ($opt['data'] as $k => $v){
  124. //验证联赛所属国家
  125. if(!empty($data['country_id'])){
  126. $country = lm('st_country','Sports')
  127. ->where(['country_id'=>$data['country_id']])
  128. ->count();
  129. if($country < 1) Render([], '10013', lang('Tips','Sports')->get('country_error'));
  130. }
  131. //验证联赛所属地区
  132. if(!empty($data['area_id'])){
  133. $area = lm('st_area','Sports')
  134. ->where(['id'=>$data['area_id']])
  135. ->count();
  136. if($area < 1) Render([], '10014', lang('Tips','Sports')->get('area_error'));
  137. }
  138. $post = lm($model['model_league'],'Sports')
  139. ->where(['lg_id'=>$data['lg_id']])
  140. ->count();
  141. //更新操作
  142. if($post > 0){
  143. $ret = lm($model['model_league'],'Sports')
  144. -> where(['lg_id'=>$data['lg_id']])
  145. -> update($data);
  146. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  147. }else{
  148. //写入操作
  149. $ret = lm($model['model_league'],'Sports')->insert($data);
  150. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  151. }
  152. // }
  153. Render([], '1', lang('Tips','Sports')->get('success'));
  154. }
  155. //写入赛事数据
  156. public function competition($opt = []){
  157. $game_code = $opt['game_code'];
  158. //根据球类代码获取相关model
  159. $model = $this->commonFunction->getModels($game_code,1);
  160. $data = $opt['data'];
  161. if(empty($data['lg_id'])) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  162. //验证赛事所属联赛
  163. if(!empty($data['lg_id'])){
  164. $lg = lm($model['model_local_league'],'Sports')
  165. ->select('m_lg_id')
  166. ->where(['lg_id'=>$data['lg_id'],'source'=>$data['source']])
  167. ->first()->m_lg_id;
  168. if(empty($lg)) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  169. }
  170. //查询关联记录是否存在
  171. $match_id = lm($model['model_local_match'],'Sports')
  172. ->select('m_match_id')
  173. ->where(['match_id'=>$data['match_id'],'source'=>$data['source']])
  174. ->first()->m_match_id;
  175. if(empty($match_id)){
  176. //查询赛事是否存在
  177. $id = lm($model['model_match'],'Sports')
  178. ->select('id')
  179. ->where(['home_team'=>$data['home_team'],'guest_team'=>$data['guest_team'],'match_date'=>$data['match_date'],'match_time'=>$data['match_time']])
  180. ->first()->id;
  181. //写入赛事
  182. if(empty($id)){
  183. $data['ctime'] = $this->newTime;
  184. $data['utime'] = $this->newTime;
  185. $data['expire_time'] = date('Y-m-d H:m:i',(time()+60));
  186. $id = lm($model['model_match'],'Sports')->insertGetId($data);
  187. if($id < 1) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  188. }
  189. $set_local = [
  190. 'm_match_id'=>$id,
  191. 'match_id'=>$data['match_id'],
  192. 'source'=>$data['source'],
  193. ];
  194. $ret = lm($model['model_local_match'],'Sports')-> insertGetId($set_local);
  195. if($ret < 1) Render([], '10018', lang('Tips','Sports')->get('local_match_error'));
  196. }
  197. Render([], '1', lang('Tips','Sports')->get('success'));
  198. }
  199. //写入赛事数据 弃用
  200. public function __competition($opt = []){
  201. $game_code = $opt['game_code'];
  202. //根据球类代码获取相关model
  203. $model = $this->commonFunction->getModels($game_code,1);
  204. $data = $opt['data'];
  205. // foreach ($opt['data'] as $k => $v){
  206. //验证赛事所属联赛
  207. if(!empty($data['lg_id'])){
  208. $lg = lm($model['model_league'],'Sports')
  209. ->where(['lg_id'=>$data['lg_id']])
  210. ->count();
  211. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  212. }
  213. $post = lm($model['model_match'],'Sports')
  214. ->where(['match_id'=>$data['match_id']])
  215. ->count();
  216. //更新操作
  217. if($post > 0){
  218. $ret = lm($model['model_match'],'Sports')
  219. -> where(['match_id'=>$data['match_id']])
  220. -> update($data);
  221. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  222. }else{
  223. //写入操作
  224. $ret = lm($model['model_match'],'Sports')->insert($data);
  225. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  226. }
  227. // }
  228. Render([], '1', lang('Tips','Sports')->get('success'));
  229. }
  230. //写入赔率数据
  231. public function odds($opt){
  232. $game_code = $opt['game_code'];
  233. //根据球类代码获取相关model
  234. $model = $this->commonFunction->getModels($game_code,1);
  235. $data = $opt['data'];
  236. // foreach ($opt['data'] as $k => $v){
  237. //验证赔率所属赛事
  238. if(!empty($data['match_id'])){
  239. $match = lm($model['model_match'],'Sports')
  240. ->where(['match_id'=>$data['match_id']])
  241. ->count();
  242. if($match < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  243. }
  244. //验证赔率所属联赛 冠军盘口
  245. if(!empty($data['lg_id'])){
  246. $lg = lm($model['model_league'],'Sports')
  247. ->where(['lg_id'=>$data['lg_id']])
  248. ->count();
  249. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  250. }
  251. $post = lm($model['model_odds'],'Sports')
  252. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  253. ->count();
  254. //更新操作
  255. if($post > 0){
  256. $ret = lm($model['model_odds'],'Sports')
  257. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  258. -> update($data);
  259. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  260. }else{
  261. //写入操作
  262. $ret = lm($model['model_odds'],'Sports')->insert($data);
  263. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  264. }
  265. // }
  266. Render([], '1', lang('Tips','Sports')->get('success'));
  267. }
  268. //写入联赛结果
  269. public function league_result($opt){
  270. $game_code = $opt['game_code'];
  271. //根据球类代码获取相关model
  272. $model = $this->commonFunction->getModels($game_code,1);
  273. $data = $opt['data'];
  274. // foreach ($opt['data'] as $k => $v){
  275. //验证结果所属联赛
  276. if(!empty($data['lg_id'])){
  277. $lg = lm($model['model_league'],'Sports')
  278. ->where(['lg_id'=>$data['lg_id']])
  279. ->count();
  280. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  281. }
  282. $post = lm($model['model_league_result'],'Sports')
  283. ->where(['lg_id'=>$data['lg_id'],'game_name'=>$data['game_name']])
  284. ->count();
  285. //更新操作
  286. if($post > 0){
  287. $ret = lm($model['model_league_result'],'Sports')
  288. ->where(['lg_id'=>$data['lg_id'],'game_name'=>$data['game_name']])
  289. -> update($data);
  290. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  291. }else{
  292. //写入操作
  293. $ret = lm($model['model_league_result'],'Sports')->insert($data);
  294. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  295. }
  296. // }
  297. Render([], '1', lang('Tips','Sports')->get('success'));
  298. }
  299. //写入赛事结果
  300. public function com_result($opt){
  301. $game_code = $opt['game_code'];
  302. //根据球类代码获取相关model
  303. $model = $this->commonFunction->getModels($game_code,1);
  304. $data = $opt['data'];
  305. // foreach ($opt['data'] as $k => $v){
  306. //验证结果所属联赛
  307. if(!empty($data['lg_id'])){
  308. $lg = lm($model['model_league'],'Sports')
  309. ->where(['lg_id'=>$data['lg_id']])
  310. ->count();
  311. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  312. }
  313. //验证结果所属赛事
  314. if(!empty($data['match_id'])){
  315. $lg = lm($model['model_match'],'Sports')
  316. ->where(['match_id'=>$data['match_id']])
  317. ->count();
  318. if($lg < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  319. }
  320. $post = lm($model['model_result'],'Sports')
  321. ->where(['match_id'=>$data['match_id']])
  322. ->count();
  323. //更新操作
  324. if($post > 0){
  325. $ret = lm($model['model_result'],'Sports')
  326. -> where(['match_id'=>$data['match_id']])
  327. -> update($data);
  328. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  329. }else{
  330. //写入操作
  331. $ret = lm($model['model_result'],'Sports')->insert($data);
  332. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  333. }
  334. // }
  335. Render([], '1', lang('Tips','Sports')->get('success'));
  336. }
  337. //写入赛事结果记录
  338. public function com_result_record($opt){
  339. $game_code = $opt['game_code'];
  340. //根据球类代码获取相关model
  341. $model = $this->commonFunction->getModels($game_code,1);
  342. $data = $opt['data'];
  343. // foreach ($opt['data'] as $k => $v){
  344. //验证结果所属联赛
  345. if(!empty($data['lg_id'])){
  346. $lg = lm($model['model_league'],'Sports')
  347. ->where(['lg_id'=>$data['lg_id']])
  348. ->count();
  349. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  350. }
  351. //验证结果所属赛事
  352. if(!empty($data['match_id'])){
  353. $lg = lm($model['model_match'],'Sports')
  354. ->where(['match_id'=>$data['match_id']])
  355. ->count();
  356. if($lg < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  357. }
  358. $post = lm($model['model_result_record'],'Sports')
  359. ->where(['match_id'=>$data['match_id'],'match_time'=>$data['match_time']])
  360. ->count();
  361. //更新操作
  362. if($post > 0){
  363. $ret = lm($model['model_result_record'],'Sports')
  364. ->where(['match_id'=>$data['match_id'],'match_time'=>$data['match_time']])
  365. -> update($data);
  366. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  367. }else{
  368. //写入操作
  369. $ret = lm($model['model_result_record'],'Sports')->insert($data);
  370. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  371. }
  372. // }
  373. Render([], '1', lang('Tips','Sports')->get('success'));
  374. }
  375. //写入赔率记录
  376. public function odds_record($opt){
  377. $game_code = $opt['game_code'];
  378. //根据球类代码获取相关model
  379. $model = $this->commonFunction->getModels($game_code,1);
  380. $data = $opt['data'];
  381. // foreach ($opt['data'] as $k => $v){
  382. //验证赔率所属赛事
  383. if(!empty($data['match_id'])){
  384. $match = lm($model['model_match'],'Sports')
  385. ->where(['match_id'=>$data['match_id']])
  386. ->count();
  387. if($match < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  388. }
  389. //验证赔率所属联赛 冠军盘口
  390. if(!empty($data['lg_id'])){
  391. $lg = lm($model['model_league'],'Sports')
  392. ->where(['lg_id'=>$data['lg_id']])
  393. ->count();
  394. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  395. }
  396. $post = lm($model['model_odds_record'],'Sports')
  397. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  398. ->count();
  399. //更新操作
  400. if($post > 0){
  401. $ret = lm($model['model_odds_record'],'Sports')
  402. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  403. ->update($data);
  404. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  405. }else{
  406. //写入操作
  407. $ret = lm($model['model_odds_record'],'Sports')->insert($data);
  408. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  409. }
  410. // }
  411. Render([], '1', lang('Tips','Sports')->get('success'));
  412. }
  413. /**
  414. * @param $data
  415. * @return mixed
  416. * json转数组
  417. */
  418. public function getAddData($data){
  419. $data = json_decode($data,true);
  420. return $data;
  421. }
  422. }