WriteSports.php 19 KB

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