WriteSports.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. $post = lm($model['model_league'],'Sports')
  84. ->select('id as m_lg_id')
  85. ->where('name_chinese','=',$data['name_chinese'])
  86. ->first();
  87. $m_lg_id = $post->m_lg_id;
  88. //如果不存在,则写入联赛表
  89. if(empty($m_lg_id)){
  90. $set_lg['name_chinese'] = $data['name_chinese'];
  91. $set_lg['kind'] = $data['kind'];
  92. $set_lg['match_mode'] = $data['match_mode'];
  93. $set_lg['if_stop'] = $data['if_stop'];
  94. $set_lg['last_time'] = $data['last_time'];
  95. $ret = lm($model['model_league'],'Sports')->insertGetId($set_lg);
  96. $m_lg_id = $ret;
  97. if($m_lg_id < 1) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  98. }
  99. //查询关联表是否 记录
  100. $local_r = lm($model['model_local_league'],'Sports')
  101. ->where(['m_lg_id'=>$m_lg_id,'lg_id'=>$data['lg_id'],'source'=>$data['source']])
  102. ->count('*');
  103. //没有记录则插入
  104. if($local_r == 0){
  105. $set_local = [
  106. 'm_lg_id'=>$m_lg_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. }
  113. Render([], '1', lang('Tips','Sports')->get('success'));
  114. }
  115. //弃用
  116. public function __league($opt = []){
  117. $game_code = $opt['game_code'];
  118. //根据球类代码获取相关model
  119. $model = $this->commonFunction->getModels($game_code,1);
  120. $data = $opt['data'];
  121. // foreach ($opt['data'] as $k => $v){
  122. //验证联赛所属国家
  123. if(!empty($data['country_id'])){
  124. $country = lm('st_country','Sports')
  125. ->where(['country_id'=>$data['country_id']])
  126. ->count();
  127. if($country < 1) Render([], '10013', lang('Tips','Sports')->get('country_error'));
  128. }
  129. //验证联赛所属地区
  130. if(!empty($data['area_id'])){
  131. $area = lm('st_area','Sports')
  132. ->where(['id'=>$data['area_id']])
  133. ->count();
  134. if($area < 1) Render([], '10014', lang('Tips','Sports')->get('area_error'));
  135. }
  136. $post = lm($model['model_league'],'Sports')
  137. ->where(['lg_id'=>$data['lg_id']])
  138. ->count();
  139. //更新操作
  140. if($post > 0){
  141. $ret = lm($model['model_league'],'Sports')
  142. -> where(['lg_id'=>$data['lg_id']])
  143. -> update($data);
  144. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  145. }else{
  146. //写入操作
  147. $ret = lm($model['model_league'],'Sports')->insert($data);
  148. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  149. }
  150. // }
  151. Render([], '1', lang('Tips','Sports')->get('success'));
  152. }
  153. //写入赛事数据
  154. public function competition($opt = []){
  155. $game_code = $opt['game_code'];
  156. //根据球类代码获取相关model
  157. $model = $this->commonFunction->getModels($game_code,1);
  158. $data = $opt['data'];
  159. // foreach ($opt['data'] as $k => $v){
  160. //验证赛事所属联赛
  161. if(!empty($data['lg_id'])){
  162. $lg = lm($model['model_league'],'Sports')
  163. ->where(['lg_id'=>$data['lg_id']])
  164. ->count();
  165. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  166. }
  167. $post = lm($model['model_match'],'Sports')
  168. ->where(['match_id'=>$data['match_id']])
  169. ->count();
  170. //更新操作
  171. if($post > 0){
  172. $ret = lm($model['model_match'],'Sports')
  173. -> where(['match_id'=>$data['match_id']])
  174. -> update($data);
  175. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  176. }else{
  177. //写入操作
  178. $ret = lm($model['model_match'],'Sports')->insert($data);
  179. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  180. }
  181. // }
  182. Render([], '1', lang('Tips','Sports')->get('success'));
  183. }
  184. //写入赔率数据
  185. public function odds($opt){
  186. $game_code = $opt['game_code'];
  187. //根据球类代码获取相关model
  188. $model = $this->commonFunction->getModels($game_code,1);
  189. $data = $opt['data'];
  190. // foreach ($opt['data'] as $k => $v){
  191. //验证赔率所属赛事
  192. if(!empty($data['match_id'])){
  193. $match = lm($model['model_match'],'Sports')
  194. ->where(['match_id'=>$data['match_id']])
  195. ->count();
  196. if($match < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  197. }
  198. //验证赔率所属联赛 冠军盘口
  199. if(!empty($data['lg_id'])){
  200. $lg = lm($model['model_league'],'Sports')
  201. ->where(['lg_id'=>$data['lg_id']])
  202. ->count();
  203. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  204. }
  205. $post = lm($model['model_odds'],'Sports')
  206. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  207. ->count();
  208. //更新操作
  209. if($post > 0){
  210. $ret = lm($model['model_odds'],'Sports')
  211. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  212. -> update($data);
  213. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  214. }else{
  215. //写入操作
  216. $ret = lm($model['model_odds'],'Sports')->insert($data);
  217. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  218. }
  219. // }
  220. Render([], '1', lang('Tips','Sports')->get('success'));
  221. }
  222. //写入联赛结果
  223. public function league_result($opt){
  224. $game_code = $opt['game_code'];
  225. //根据球类代码获取相关model
  226. $model = $this->commonFunction->getModels($game_code,1);
  227. $data = $opt['data'];
  228. // foreach ($opt['data'] as $k => $v){
  229. //验证结果所属联赛
  230. if(!empty($data['lg_id'])){
  231. $lg = lm($model['model_league'],'Sports')
  232. ->where(['lg_id'=>$data['lg_id']])
  233. ->count();
  234. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  235. }
  236. $post = lm($model['model_league_result'],'Sports')
  237. ->where(['lg_id'=>$data['lg_id'],'game_name'=>$data['game_name']])
  238. ->count();
  239. //更新操作
  240. if($post > 0){
  241. $ret = lm($model['model_league_result'],'Sports')
  242. ->where(['lg_id'=>$data['lg_id'],'game_name'=>$data['game_name']])
  243. -> update($data);
  244. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  245. }else{
  246. //写入操作
  247. $ret = lm($model['model_league_result'],'Sports')->insert($data);
  248. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  249. }
  250. // }
  251. Render([], '1', lang('Tips','Sports')->get('success'));
  252. }
  253. //写入赛事结果
  254. public function com_result($opt){
  255. $game_code = $opt['game_code'];
  256. //根据球类代码获取相关model
  257. $model = $this->commonFunction->getModels($game_code,1);
  258. $data = $opt['data'];
  259. // foreach ($opt['data'] as $k => $v){
  260. //验证结果所属联赛
  261. if(!empty($data['lg_id'])){
  262. $lg = lm($model['model_league'],'Sports')
  263. ->where(['lg_id'=>$data['lg_id']])
  264. ->count();
  265. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  266. }
  267. //验证结果所属赛事
  268. if(!empty($data['match_id'])){
  269. $lg = lm($model['model_match'],'Sports')
  270. ->where(['match_id'=>$data['match_id']])
  271. ->count();
  272. if($lg < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  273. }
  274. $post = lm($model['model_result'],'Sports')
  275. ->where(['match_id'=>$data['match_id']])
  276. ->count();
  277. //更新操作
  278. if($post > 0){
  279. $ret = lm($model['model_result'],'Sports')
  280. -> where(['match_id'=>$data['match_id']])
  281. -> update($data);
  282. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  283. }else{
  284. //写入操作
  285. $ret = lm($model['model_result'],'Sports')->insert($data);
  286. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  287. }
  288. // }
  289. Render([], '1', lang('Tips','Sports')->get('success'));
  290. }
  291. //写入赛事结果记录
  292. public function com_result_record($opt){
  293. $game_code = $opt['game_code'];
  294. //根据球类代码获取相关model
  295. $model = $this->commonFunction->getModels($game_code,1);
  296. $data = $opt['data'];
  297. // foreach ($opt['data'] as $k => $v){
  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. //验证结果所属赛事
  306. if(!empty($data['match_id'])){
  307. $lg = lm($model['model_match'],'Sports')
  308. ->where(['match_id'=>$data['match_id']])
  309. ->count();
  310. if($lg < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  311. }
  312. $post = lm($model['model_result_record'],'Sports')
  313. ->where(['match_id'=>$data['match_id'],'match_time'=>$data['match_time']])
  314. ->count();
  315. //更新操作
  316. if($post > 0){
  317. $ret = lm($model['model_result_record'],'Sports')
  318. ->where(['match_id'=>$data['match_id'],'match_time'=>$data['match_time']])
  319. -> update($data);
  320. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  321. }else{
  322. //写入操作
  323. $ret = lm($model['model_result_record'],'Sports')->insert($data);
  324. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  325. }
  326. // }
  327. Render([], '1', lang('Tips','Sports')->get('success'));
  328. }
  329. //写入赔率记录
  330. public function odds_record($opt){
  331. $game_code = $opt['game_code'];
  332. //根据球类代码获取相关model
  333. $model = $this->commonFunction->getModels($game_code,1);
  334. $data = $opt['data'];
  335. // foreach ($opt['data'] as $k => $v){
  336. //验证赔率所属赛事
  337. if(!empty($data['match_id'])){
  338. $match = lm($model['model_match'],'Sports')
  339. ->where(['match_id'=>$data['match_id']])
  340. ->count();
  341. if($match < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
  342. }
  343. //验证赔率所属联赛 冠军盘口
  344. if(!empty($data['lg_id'])){
  345. $lg = lm($model['model_league'],'Sports')
  346. ->where(['lg_id'=>$data['lg_id']])
  347. ->count();
  348. if($lg < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
  349. }
  350. $post = lm($model['model_odds_record'],'Sports')
  351. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  352. ->count();
  353. //更新操作
  354. if($post > 0){
  355. $ret = lm($model['model_odds_record'],'Sports')
  356. ->where(['match_id'=>$data['match_id'],'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
  357. ->update($data);
  358. if($ret < 1) Render([], '10011', lang('Tips','Sports')->get('update_error'));
  359. }else{
  360. //写入操作
  361. $ret = lm($model['model_odds_record'],'Sports')->insert($data);
  362. if($ret != true) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
  363. }
  364. // }
  365. Render([], '1', lang('Tips','Sports')->get('success'));
  366. }
  367. /**
  368. * @param $data
  369. * @return mixed
  370. * json转数组
  371. */
  372. public function getAddData($data){
  373. $data = json_decode($data,true);
  374. return $data;
  375. }
  376. }