WriteSports.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. class WriteSports extends Controller{
  11. public function init() {
  12. $this->commonFunction = C()->get('commonFunction');
  13. }
  14. /**
  15. * 体育数据入库接口
  16. */
  17. public function setSports(){
  18. $obt = $_REQUEST['data'];
  19. $data = $this->getAddData($obt);
  20. //验证二维数组内是否有空值
  21. // foreach ($data['data'] as $v){
  22. // if(in_array('', $v)) Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR'));
  23. // }
  24. switch ($data['title']){
  25. case 'area'://地区
  26. $ret = $this->area($data['data']);
  27. break;
  28. case 'country'://国家
  29. $ret = $this->country($data['data']);
  30. break;
  31. case 'league'://联赛
  32. $ret = $this->league($data);
  33. break;
  34. case 'competition'://赛事
  35. $ret = $this->competition($data);
  36. break;
  37. case 'odds'://赔率
  38. $ret = $this->odds($data);
  39. break;
  40. case 'league_result'://联赛结果
  41. $ret = $this->league_result($data);
  42. break;
  43. case 'competition_result'://赛事结果
  44. $ret = $this->com_result($data);
  45. break;
  46. case 'competition_result_record'://赛事结果记录
  47. $ret = $this->com_result_record($data);
  48. break;
  49. case 'odds_record'://赔率记录
  50. $ret = $this->odds_record($data);
  51. break;
  52. default:
  53. Render([], '10007', lang('Tips','Sports')->get('abnormal'));
  54. }
  55. //写入成功
  56. if($ret) Render([], '1', lang('Tips','Sports')->get('success'));
  57. //写入失败
  58. Render([], '10010', lang('Tips','Sports')->get('HANDLE_ERRORS'));
  59. }
  60. //写入地区数据
  61. public function area($opt = []){
  62. $ret = lm('st_area','Sports')->insert($opt);
  63. return $ret;
  64. }
  65. //写入国家数据
  66. public function country($opt = []){
  67. $ret = lm('st_country','Sports')->insert($opt);
  68. return $ret;
  69. }
  70. //写入联赛数据
  71. public function league($opt = []){
  72. $game_code = $opt['game_code'];
  73. //根据球类代码获取相关model
  74. $model = $this->commonFunction->getModels($game_code,1);
  75. $data = $opt['data'];
  76. // foreach ($opt['data'] as $k => $v){
  77. //验证联赛所属国家
  78. if(!empty($data['country_id'])){
  79. $country = lm('st_country','Sports')
  80. ->where(['country_id'=>$data['country_id']])
  81. ->count();
  82. if($country < 1) Render([], '10013', lang('Tips','Sports')->get('country_error'));
  83. }
  84. //验证联赛所属地区
  85. if(!empty($data['area_id'])){
  86. $area = lm('st_area','Sports')
  87. ->where(['id'=>$data['area_id']])
  88. ->count();
  89. if($area < 1) Render([], '10014', lang('Tips','Sports')->get('area_error'));
  90. }
  91. $post = lm($model['model_league'],'Sports')
  92. ->where(['lg_id'=>$data['lg_id']])
  93. ->count();
  94. //更新操作
  95. if($post > 0){
  96. $ret = lm($model['model_league'],'Sports')
  97. -> where(['lg_id'=>$data['lg_id']])
  98. -> update($data);
  99. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  100. }else{
  101. //写入操作
  102. $ret = lm($model['model_league'],'Sports')->insert($data);
  103. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  104. }
  105. // }
  106. Render([], '1', lang('Tips','Sports')->get('success'));
  107. }
  108. //写入赛事数据
  109. public function competition($opt = []){
  110. $game_code = $opt['game_code'];
  111. //根据球类代码获取相关model
  112. $model = $this->commonFunction->getModels($game_code,1);
  113. $data = $opt['data'];
  114. // foreach ($opt['data'] as $k => $v){
  115. //验证赛事所属联赛
  116. if(!empty($data['lg_id'])){
  117. $lg = lm($model['model_league'],'Sports')
  118. ->where(['lg_id'=>$data['lg_id']])
  119. ->count();
  120. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  121. }
  122. $post = lm($model['model_match'],'Sports')
  123. ->where(['match_id'=>$data['match_id']])
  124. ->count();
  125. //更新操作
  126. if($post > 0){
  127. $ret = lm($model['model_match'],'Sports')
  128. -> where(['match_id'=>$data['match_id']])
  129. -> update($data);
  130. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  131. }else{
  132. //写入操作
  133. $ret = lm($model['model_match'],'Sports')->insert($data);
  134. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  135. }
  136. // }
  137. Render([], '1', lang('Tips','Sports')->get('success'));
  138. }
  139. //写入赔率数据
  140. public function odds($opt){
  141. $game_code = $opt['game_code'];
  142. //根据球类代码获取相关model
  143. $model = $this->commonFunction->getModels($game_code,1);
  144. $data = $opt['data'];
  145. // foreach ($opt['data'] as $k => $v){
  146. //验证赔率所属赛事
  147. if(!empty($data['match_id'])){
  148. $match = lm($model['model_match'],'Sports')
  149. ->where(['match_id'=>$data['match_id']])
  150. ->count();
  151. if($match < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  152. }
  153. //验证赔率所属联赛 冠军盘口
  154. if(!empty($data['lg_id'])){
  155. $lg = lm($model['model_league'],'Sports')
  156. ->where(['lg_id'=>$data['lg_id']])
  157. ->count();
  158. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  159. }
  160. $post = lm($model['model_odds'],'Sports')
  161. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  162. ->count();
  163. //更新操作
  164. if($post > 0){
  165. $ret = lm($model['model_odds'],'Sports')
  166. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  167. -> update($data);
  168. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  169. }else{
  170. //写入操作
  171. $ret = lm($model['model_odds'],'Sports')->insert($data);
  172. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  173. }
  174. // }
  175. Render([], '1', lang('Tips','Sports')->get('success'));
  176. }
  177. //写入联赛结果
  178. public function league_result($opt){
  179. $game_code = $opt['game_code'];
  180. //根据球类代码获取相关model
  181. $model = $this->commonFunction->getModels($game_code,1);
  182. $data = $opt['data'];
  183. // foreach ($opt['data'] as $k => $v){
  184. //验证结果所属联赛
  185. if(!empty($data['lg_id'])){
  186. $lg = lm($model['model_league'],'Sports')
  187. ->where(['lg_id'=>$data['lg_id']])
  188. ->count();
  189. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  190. }
  191. $post = lm($model['model_league_result'],'Sports')
  192. ->where(['lg_id'=>$data['lg_id'],'game_name'=>$data['game_name']])
  193. ->count();
  194. //更新操作
  195. if($post > 0){
  196. $ret = lm($model['model_league_result'],'Sports')
  197. ->where(['lg_id'=>$data['lg_id'],'game_name'=>$data['game_name']])
  198. -> update($data);
  199. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  200. }else{
  201. //写入操作
  202. $ret = lm($model['model_league_result'],'Sports')->insert($data);
  203. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  204. }
  205. // }
  206. Render([], '1', lang('Tips','Sports')->get('success'));
  207. }
  208. //写入赛事结果
  209. public function com_result($opt){
  210. $game_code = $opt['game_code'];
  211. //根据球类代码获取相关model
  212. $model = $this->commonFunction->getModels($game_code,1);
  213. $data = $opt['data'];
  214. // foreach ($opt['data'] as $k => $v){
  215. //验证结果所属联赛
  216. if(!empty($data['lg_id'])){
  217. $lg = lm($model['model_league'],'Sports')
  218. ->where(['lg_id'=>$data['lg_id']])
  219. ->count();
  220. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  221. }
  222. //验证结果所属赛事
  223. if(!empty($data['match_id'])){
  224. $lg = lm($model['model_match'],'Sports')
  225. ->where(['match_id'=>$data['match_id']])
  226. ->count();
  227. if($lg < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  228. }
  229. $post = lm($model['model_result'],'Sports')
  230. ->where(['match_id'=>$data['match_id']])
  231. ->count();
  232. //更新操作
  233. if($post > 0){
  234. $ret = lm($model['model_result'],'Sports')
  235. -> where(['match_id'=>$data['match_id']])
  236. -> update($data);
  237. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  238. }else{
  239. //写入操作
  240. $ret = lm($model['model_result'],'Sports')->insert($data);
  241. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  242. }
  243. // }
  244. Render([], '1', lang('Tips','Sports')->get('success'));
  245. }
  246. //写入赛事结果记录
  247. public function com_result_record($opt){
  248. $game_code = $opt['game_code'];
  249. //根据球类代码获取相关model
  250. $model = $this->commonFunction->getModels($game_code,1);
  251. $data = $opt['data'];
  252. // foreach ($opt['data'] as $k => $v){
  253. //验证结果所属联赛
  254. if(!empty($data['lg_id'])){
  255. $lg = lm($model['model_league'],'Sports')
  256. ->where(['lg_id'=>$data['lg_id']])
  257. ->count();
  258. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  259. }
  260. //验证结果所属赛事
  261. if(!empty($data['match_id'])){
  262. $lg = lm($model['model_match'],'Sports')
  263. ->where(['match_id'=>$data['match_id']])
  264. ->count();
  265. if($lg < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  266. }
  267. $post = lm($model['model_result_record'],'Sports')
  268. ->where(['match_id'=>$data['match_id'],'match_time'=>$data['match_time']])
  269. ->count();
  270. //更新操作
  271. if($post > 0){
  272. $ret = lm($model['model_result_record'],'Sports')
  273. ->where(['match_id'=>$data['match_id'],'match_time'=>$data['match_time']])
  274. -> update($data);
  275. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  276. }else{
  277. //写入操作
  278. $ret = lm($model['model_result_record'],'Sports')->insert($data);
  279. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  280. }
  281. // }
  282. Render([], '1', lang('Tips','Sports')->get('success'));
  283. }
  284. //写入赔率记录
  285. public function odds_record($opt){
  286. $game_code = $opt['game_code'];
  287. //根据球类代码获取相关model
  288. $model = $this->commonFunction->getModels($game_code,1);
  289. $data = $opt['data'];
  290. // foreach ($opt['data'] as $k => $v){
  291. //验证赔率所属赛事
  292. if(!empty($data['match_id'])){
  293. $match = lm($model['model_match'],'Sports')
  294. ->where(['match_id'=>$data['match_id']])
  295. ->count();
  296. if($match < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  297. }
  298. //验证赔率所属联赛 冠军盘口
  299. if(!empty($data['lg_id'])){
  300. $lg = lm($model['model_league'],'Sports')
  301. ->where(['lg_id'=>$data['lg_id']])
  302. ->count();
  303. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  304. }
  305. $post = lm($model['model_odds_record'],'Sports')
  306. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  307. ->count();
  308. //更新操作
  309. if($post > 0){
  310. $ret = lm($model['model_odds_record'],'Sports')
  311. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  312. ->update($data);
  313. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  314. }else{
  315. //写入操作
  316. $ret = lm($model['model_odds_record'],'Sports')->insert($data);
  317. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  318. }
  319. // }
  320. Render([], '1', lang('Tips','Sports')->get('success'));
  321. }
  322. /**
  323. * @param $data
  324. * @return mixed
  325. * json转数组
  326. */
  327. public function getAddData($data){
  328. $data = json_decode($data,true);
  329. return $data;
  330. }
  331. }