WriteSports.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. $match_id = lm($model['model_local_match'],'Sports')
  171. ->select('m_match_id')
  172. ->where(['match_id'=>$data['match_id'],'source'=>$data['source']])
  173. ->first()->m_match_id;
  174. //更新操作
  175. if(empty($match_id)){
  176. $ret = lm($model['model_match'],'Sports')
  177. -> where(['match_id'=>$data['match_id']])
  178. -> update($data);
  179. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  180. }else{
  181. //写入操作
  182. $ret = lm($model['model_match'],'Sports')->insert($data);
  183. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  184. }
  185. Render([], '1', lang('Tips','Sports')->get('success'));
  186. }
  187. //写入赛事数据 弃用
  188. public function __competition($opt = []){
  189. $game_code = $opt['game_code'];
  190. //根据球类代码获取相关model
  191. $model = $this->commonFunction->getModels($game_code,1);
  192. $data = $opt['data'];
  193. // foreach ($opt['data'] as $k => $v){
  194. //验证赛事所属联赛
  195. if(!empty($data['lg_id'])){
  196. $lg = lm($model['model_league'],'Sports')
  197. ->where(['lg_id'=>$data['lg_id']])
  198. ->count();
  199. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  200. }
  201. $post = lm($model['model_match'],'Sports')
  202. ->where(['match_id'=>$data['match_id']])
  203. ->count();
  204. //更新操作
  205. if($post > 0){
  206. $ret = lm($model['model_match'],'Sports')
  207. -> where(['match_id'=>$data['match_id']])
  208. -> update($data);
  209. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  210. }else{
  211. //写入操作
  212. $ret = lm($model['model_match'],'Sports')->insert($data);
  213. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  214. }
  215. // }
  216. Render([], '1', lang('Tips','Sports')->get('success'));
  217. }
  218. //写入赔率数据
  219. public function odds($opt){
  220. $game_code = $opt['game_code'];
  221. //根据球类代码获取相关model
  222. $model = $this->commonFunction->getModels($game_code,1);
  223. $data = $opt['data'];
  224. // foreach ($opt['data'] as $k => $v){
  225. //验证赔率所属赛事
  226. if(!empty($data['match_id'])){
  227. $match = lm($model['model_match'],'Sports')
  228. ->where(['match_id'=>$data['match_id']])
  229. ->count();
  230. if($match < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  231. }
  232. //验证赔率所属联赛 冠军盘口
  233. if(!empty($data['lg_id'])){
  234. $lg = lm($model['model_league'],'Sports')
  235. ->where(['lg_id'=>$data['lg_id']])
  236. ->count();
  237. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  238. }
  239. $post = lm($model['model_odds'],'Sports')
  240. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  241. ->count();
  242. //更新操作
  243. if($post > 0){
  244. $ret = lm($model['model_odds'],'Sports')
  245. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  246. -> update($data);
  247. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  248. }else{
  249. //写入操作
  250. $ret = lm($model['model_odds'],'Sports')->insert($data);
  251. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  252. }
  253. // }
  254. Render([], '1', lang('Tips','Sports')->get('success'));
  255. }
  256. //写入联赛结果
  257. public function league_result($opt){
  258. $game_code = $opt['game_code'];
  259. //根据球类代码获取相关model
  260. $model = $this->commonFunction->getModels($game_code,1);
  261. $data = $opt['data'];
  262. // foreach ($opt['data'] as $k => $v){
  263. //验证结果所属联赛
  264. if(!empty($data['lg_id'])){
  265. $lg = lm($model['model_league'],'Sports')
  266. ->where(['lg_id'=>$data['lg_id']])
  267. ->count();
  268. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  269. }
  270. $post = lm($model['model_league_result'],'Sports')
  271. ->where(['lg_id'=>$data['lg_id'],'game_name'=>$data['game_name']])
  272. ->count();
  273. //更新操作
  274. if($post > 0){
  275. $ret = lm($model['model_league_result'],'Sports')
  276. ->where(['lg_id'=>$data['lg_id'],'game_name'=>$data['game_name']])
  277. -> update($data);
  278. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  279. }else{
  280. //写入操作
  281. $ret = lm($model['model_league_result'],'Sports')->insert($data);
  282. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  283. }
  284. // }
  285. Render([], '1', lang('Tips','Sports')->get('success'));
  286. }
  287. //写入赛事结果
  288. public function com_result($opt){
  289. $game_code = $opt['game_code'];
  290. //根据球类代码获取相关model
  291. $model = $this->commonFunction->getModels($game_code,1);
  292. $data = $opt['data'];
  293. // foreach ($opt['data'] as $k => $v){
  294. //验证结果所属联赛
  295. if(!empty($data['lg_id'])){
  296. $lg = lm($model['model_league'],'Sports')
  297. ->where(['lg_id'=>$data['lg_id']])
  298. ->count();
  299. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  300. }
  301. //验证结果所属赛事
  302. if(!empty($data['match_id'])){
  303. $lg = lm($model['model_match'],'Sports')
  304. ->where(['match_id'=>$data['match_id']])
  305. ->count();
  306. if($lg < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  307. }
  308. $post = lm($model['model_result'],'Sports')
  309. ->where(['match_id'=>$data['match_id']])
  310. ->count();
  311. //更新操作
  312. if($post > 0){
  313. $ret = lm($model['model_result'],'Sports')
  314. -> where(['match_id'=>$data['match_id']])
  315. -> update($data);
  316. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  317. }else{
  318. //写入操作
  319. $ret = lm($model['model_result'],'Sports')->insert($data);
  320. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  321. }
  322. // }
  323. Render([], '1', lang('Tips','Sports')->get('success'));
  324. }
  325. //写入赛事结果记录
  326. public function com_result_record($opt){
  327. $game_code = $opt['game_code'];
  328. //根据球类代码获取相关model
  329. $model = $this->commonFunction->getModels($game_code,1);
  330. $data = $opt['data'];
  331. // foreach ($opt['data'] as $k => $v){
  332. //验证结果所属联赛
  333. if(!empty($data['lg_id'])){
  334. $lg = lm($model['model_league'],'Sports')
  335. ->where(['lg_id'=>$data['lg_id']])
  336. ->count();
  337. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  338. }
  339. //验证结果所属赛事
  340. if(!empty($data['match_id'])){
  341. $lg = lm($model['model_match'],'Sports')
  342. ->where(['match_id'=>$data['match_id']])
  343. ->count();
  344. if($lg < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  345. }
  346. $post = lm($model['model_result_record'],'Sports')
  347. ->where(['match_id'=>$data['match_id'],'match_time'=>$data['match_time']])
  348. ->count();
  349. //更新操作
  350. if($post > 0){
  351. $ret = lm($model['model_result_record'],'Sports')
  352. ->where(['match_id'=>$data['match_id'],'match_time'=>$data['match_time']])
  353. -> update($data);
  354. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  355. }else{
  356. //写入操作
  357. $ret = lm($model['model_result_record'],'Sports')->insert($data);
  358. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  359. }
  360. // }
  361. Render([], '1', lang('Tips','Sports')->get('success'));
  362. }
  363. //写入赔率记录
  364. public function odds_record($opt){
  365. $game_code = $opt['game_code'];
  366. //根据球类代码获取相关model
  367. $model = $this->commonFunction->getModels($game_code,1);
  368. $data = $opt['data'];
  369. // foreach ($opt['data'] as $k => $v){
  370. //验证赔率所属赛事
  371. if(!empty($data['match_id'])){
  372. $match = lm($model['model_match'],'Sports')
  373. ->where(['match_id'=>$data['match_id']])
  374. ->count();
  375. if($match < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  376. }
  377. //验证赔率所属联赛 冠军盘口
  378. if(!empty($data['lg_id'])){
  379. $lg = lm($model['model_league'],'Sports')
  380. ->where(['lg_id'=>$data['lg_id']])
  381. ->count();
  382. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  383. }
  384. $post = lm($model['model_odds_record'],'Sports')
  385. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  386. ->count();
  387. //更新操作
  388. if($post > 0){
  389. $ret = lm($model['model_odds_record'],'Sports')
  390. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  391. ->update($data);
  392. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  393. }else{
  394. //写入操作
  395. $ret = lm($model['model_odds_record'],'Sports')->insert($data);
  396. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  397. }
  398. // }
  399. Render([], '1', lang('Tips','Sports')->get('success'));
  400. }
  401. /**
  402. * @param $data
  403. * @return mixed
  404. * json转数组
  405. */
  406. public function getAddData($data){
  407. $data = json_decode($data,true);
  408. return $data;
  409. }
  410. }