WriteSports.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. foreach ($opt['data'] as $k => $v){
  76. //验证联赛所属国家
  77. if(!empty($v['country_id'])){
  78. $country = lm('st_country','Sports')
  79. ->where(['country_id'=>$v['country_id']])
  80. ->count();
  81. if($country < 1) Render([], '10013', lang('Tips','Sports')->get('country_error'));
  82. }
  83. //验证联赛所属地区
  84. if(!empty($v['area_id'])){
  85. $area = lm('st_area','Sports')
  86. ->where(['id'=>$v['area_id']])
  87. ->count();
  88. if($area < 1) Render([], '10014', lang('Tips','Sports')->get('area_error'));
  89. }
  90. $post = lm($model['model_league'],'Sports')
  91. ->where(['lg_id'=>$v['lg_id']])
  92. ->count();
  93. //更新操作
  94. if($post > 0){
  95. $ret = lm($model['model_league'],'Sports')
  96. -> where(['lg_id'=>$v['lg_id']])
  97. -> update($v);
  98. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  99. }else{
  100. //写入操作
  101. $ret = lm($model['model_league'],'Sports')->insert($v);
  102. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  103. }
  104. }
  105. Render([], '1', lang('Tips','Sports')->get('success'));
  106. }
  107. //写入赛事数据
  108. public function competition($opt = []){
  109. $game_code = $opt['game_code'];
  110. //根据球类代码获取相关model
  111. $model = $this->commonFunction->getModels($game_code,1);
  112. foreach ($opt['data'] as $k => $v){
  113. //验证赛事所属联赛
  114. if(!empty($v['lg_id'])){
  115. $lg = lm($model['model_league'],'Sports')
  116. ->where(['lg_id'=>$v['lg_id']])
  117. ->count();
  118. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  119. }
  120. $post = lm($model['model_match'],'Sports')
  121. ->where(['match_id'=>$v['match_id']])
  122. ->count();
  123. //更新操作
  124. if($post > 0){
  125. $ret = lm($model['model_match'],'Sports')
  126. -> where(['match_id'=>$v['match_id']])
  127. -> update($v);
  128. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  129. }else{
  130. //写入操作
  131. $ret = lm($model['model_match'],'Sports')->insert($v);
  132. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  133. }
  134. }
  135. Render([], '1', lang('Tips','Sports')->get('success'));
  136. }
  137. //写入赔率数据
  138. public function odds($opt){
  139. $game_code = $opt['game_code'];
  140. //根据球类代码获取相关model
  141. $model = $this->commonFunction->getModels($game_code,1);
  142. foreach ($opt['data'] as $k => $v){
  143. //验证赔率所属赛事
  144. if(!empty($v['match_id'])){
  145. $match = lm($model['model_match'],'Sports')
  146. ->where(['match_id'=>$v['match_id']])
  147. ->count();
  148. if($match < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  149. }
  150. //验证赔率所属联赛 冠军盘口
  151. if(!empty($v['lg_id'])){
  152. $lg = lm($model['model_league'],'Sports')
  153. ->where(['lg_id'=>$v['lg_id']])
  154. ->count();
  155. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  156. }
  157. $post = lm($model['model_odds'],'Sports')
  158. ->where(['match_id'=>$v['match_id'],'odds_code'=>$v['odds_code'],'sort'=>$v['sort']])
  159. ->count();
  160. //更新操作
  161. if($post > 0){
  162. $ret = lm($model['model_odds'],'Sports')
  163. ->where(['match_id'=>$v['match_id'],'odds_code'=>$v['odds_code'],'sort'=>$v['sort']])
  164. -> update($v);
  165. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  166. }else{
  167. //写入操作
  168. $ret = lm($model['model_odds'],'Sports')->insert($v);
  169. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  170. }
  171. }
  172. Render([], '1', lang('Tips','Sports')->get('success'));
  173. }
  174. //写入联赛结果
  175. public function league_result($opt){
  176. $game_code = $opt['game_code'];
  177. //根据球类代码获取相关model
  178. $model = $this->commonFunction->getModels($game_code,1);
  179. foreach ($opt['data'] as $k => $v){
  180. //验证结果所属联赛
  181. if(!empty($v['lg_id'])){
  182. $lg = lm($model['model_league'],'Sports')
  183. ->where(['lg_id'=>$v['lg_id']])
  184. ->count();
  185. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  186. }
  187. $post = lm($model['model_league_result'],'Sports')
  188. ->where(['lg_id'=>$v['lg_id'],'game_name'=>$v['game_name']])
  189. ->count();
  190. //更新操作
  191. if($post > 0){
  192. $ret = lm($model['model_league_result'],'Sports')
  193. ->where(['lg_id'=>$v['lg_id'],'game_name'=>$v['game_name']])
  194. -> update($v);
  195. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  196. }else{
  197. //写入操作
  198. $ret = lm($model['model_league_result'],'Sports')->insert($v);
  199. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  200. }
  201. }
  202. Render([], '1', lang('Tips','Sports')->get('success'));
  203. }
  204. //写入赛事结果
  205. public function com_result($opt){
  206. $game_code = $opt['game_code'];
  207. //根据球类代码获取相关model
  208. $model = $this->commonFunction->getModels($game_code,1);
  209. foreach ($opt['data'] as $k => $v){
  210. //验证结果所属联赛
  211. if(!empty($v['lg_id'])){
  212. $lg = lm($model['model_league'],'Sports')
  213. ->where(['lg_id'=>$v['lg_id']])
  214. ->count();
  215. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  216. }
  217. //验证结果所属赛事
  218. if(!empty($v['match_id'])){
  219. $lg = lm($model['model_match'],'Sports')
  220. ->where(['match_id'=>$v['match_id']])
  221. ->count();
  222. if($lg < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  223. }
  224. $post = lm($model['model_result'],'Sports')
  225. ->where(['match_id'=>$v['match_id']])
  226. ->count();
  227. //更新操作
  228. if($post > 0){
  229. $ret = lm($model['model_result'],'Sports')
  230. -> where(['match_id'=>$v['match_id']])
  231. -> update($v);
  232. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  233. }else{
  234. //写入操作
  235. $ret = lm($model['model_result'],'Sports')->insert($v);
  236. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  237. }
  238. }
  239. Render([], '1', lang('Tips','Sports')->get('success'));
  240. }
  241. //写入赛事结果记录
  242. public function com_result_record($opt){
  243. $game_code = $opt['game_code'];
  244. //根据球类代码获取相关model
  245. $model = $this->commonFunction->getModels($game_code,1);
  246. foreach ($opt['data'] as $k => $v){
  247. //验证结果所属联赛
  248. if(!empty($v['lg_id'])){
  249. $lg = lm($model['model_league'],'Sports')
  250. ->where(['lg_id'=>$v['lg_id']])
  251. ->count();
  252. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  253. }
  254. //验证结果所属赛事
  255. if(!empty($v['match_id'])){
  256. $lg = lm($model['model_match'],'Sports')
  257. ->where(['match_id'=>$v['match_id']])
  258. ->count();
  259. if($lg < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  260. }
  261. $post = lm($model['model_result_record'],'Sports')
  262. ->where(['match_id'=>$v['match_id'],'match_time'=>$v['match_time']])
  263. ->count();
  264. //更新操作
  265. if($post > 0){
  266. $ret = lm($model['model_result_record'],'Sports')
  267. ->where(['match_id'=>$v['match_id'],'match_time'=>$v['match_time']])
  268. -> update($v);
  269. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  270. }else{
  271. //写入操作
  272. $ret = lm($model['model_result_record'],'Sports')->insert($v);
  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 odds_record($opt){
  280. $game_code = $opt['game_code'];
  281. //根据球类代码获取相关model
  282. $model = $this->commonFunction->getModels($game_code,1);
  283. foreach ($opt['data'] as $k => $v){
  284. //验证赔率所属赛事
  285. if(!empty($v['match_id'])){
  286. $match = lm($model['model_match'],'Sports')
  287. ->where(['match_id'=>$v['match_id']])
  288. ->count();
  289. if($match < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  290. }
  291. //验证赔率所属联赛 冠军盘口
  292. if(!empty($v['lg_id'])){
  293. $lg = lm($model['model_league'],'Sports')
  294. ->where(['lg_id'=>$v['lg_id']])
  295. ->count();
  296. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  297. }
  298. $post = lm($model['model_odds_record'],'Sports')
  299. ->where(['match_id'=>$v['match_id'],'odds_code'=>$v['odds_code'],'sort'=>$v['sort']])
  300. ->count();
  301. //更新操作
  302. if($post > 0){
  303. $ret = lm($model['model_odds_record'],'Sports')
  304. ->where(['match_id'=>$v['match_id'],'odds_code'=>$v['odds_code'],'sort'=>$v['sort']])
  305. -> update($v);
  306. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  307. }else{
  308. //写入操作
  309. $ret = lm($model['model_odds_record'],'Sports')->insert($v);
  310. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  311. }
  312. }
  313. Render([], '1', lang('Tips','Sports')->get('success'));
  314. }
  315. /**
  316. * @param $data
  317. * @return mixed
  318. * json转数组
  319. */
  320. public function getAddData($data){
  321. $data = json_decode($data,true);
  322. return $data;
  323. }
  324. }