| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Jun.peng
- * Date: 2019/5/13
- * Time: 10:19
- */
- namespace App\Sports\Controller;
- use BaseController\Controller;
- use App\Sports\Model\St_area_country as St_area_countryModel;
- class WriteSports extends Controller{
- public function init() {
- $this->commonFunction = C()->get('commonFunction');
- //当前时间
- $this->newTime = date('Y-m-d H:m:i');
- }
- /**
- * 体育数据入库接口
- */
- public function setSports(){
- $obt = $_POST['data'];
- //写文件
- // file_put_contents("C:/Users/Jun.peng/Desktop/abc/as.txt",$obt);
- $data = $this->getAddData($obt);
- //验证二维数组内是否有空值
- // foreach ($data['data'] as $v){
- // if(in_array('', $v)) Render([], '10001', lang('Tips','Sports')->get('PARAM_ERROR'));
- // }
- switch ($data['title']){
- case 'area'://地区
- Render([], '10025', lang('Tips','Sports')->get('auth_error'));
- // $ret = $this->area($data['data']);
- break;
- case 'country'://国家
- Render([], '10025', lang('Tips','Sports')->get('auth_error'));
- // $ret = $this->country($data['data']);
- break;
- case 'league'://联赛
- $ret = $this->league($data);
- break;
- case 'competition'://赛事
- $ret = $this->competition($data);
- break;
- case 'odds'://赔率
- $ret = $this->odds($data);
- break;
- case 'league_result'://联赛结果
- Render([], '10025', lang('Tips','Sports')->get('auth_error'));
- // $ret = $this->league_result($data);
- break;
- case 'match_result'://赛事结果
- Render([], '10025', lang('Tips','Sports')->get('auth_error'));
- // $ret = $this->com_result($data);
- break;
- case 'match_result_record'://赛事结果记录
- $ret = $this->com_result_record($data);
- break;
- case 'odds_record'://赔率记录
- $ret = $this->odds_record($data);
- break;
- case ''://将所有进行中的赛事写入结果
- if(empty($data['game_code'])) Render([], '10007', lang('Tips','Sports')->get('abnormal'));
- // if($data['game_code'] != 'zq' || $data['game_code'] != 'lq' || $data['game_code'] != 'wq' || $data['game_code'] != 'bq') Render([], '10007', lang('Tips','Sports')->get('abnormal'));
- $ret = $this->match_result($data);
- default:
- Render([], '10007', lang('Tips','Sports')->get('abnormal'));
- }
- //写入成功
- if($ret) Render([], '1', lang('Tips','Sports')->get('success'));
- //写入失败
- Render([], '10010', lang('Tips','Sports')->get('HANDLE_ERRORS'));
- }
- //计算滚球 赛事进行时间
- private function secTime($sec=0){
- $min = floor($sec/60);
- $res = $min.':'.($sec-$min*60);
- return $res;
- }
- //将进行中赛事写入 赛事结果
- public function match_result($opt = []){
- $game_code = $opt['game_code'];
- $data = $opt['data'];
- $model = $this->commonFunction->getModels($game_code,1);
- if(empty($data)){
- //获取赛事表所有滚球
- $matchData = lm($model['model_match'],'Sports')
- ->select('id','home_team','guest_team','lg_id','status','tag','match_time')
- ->where(['status'=> 1])
- ->get()
- ->toarray();
- // dd($matchData);
- //没有滚球数据,无需操作
- if(empty($matchData)) Render([], '1', lang('Tips','Sports')->get('success'));
- //获取赛事结果表 所有滚球
- $matchData_r = lm($model['model_result'],'Sports')
- ->select()
- ->where(['status'=> 1])
- ->get()
- ->toarray();
- //没有滚球结果,直接插入结果表
- if(empty($matchData_r)){
- foreach ($matchData as $k=>$v){
- $time = time()-strtotime($v['match_time']);
- $match_time = $this->secTime($time);
- $set_match_r[] = [
- "match_id"=> $v['id'],
- "home_team"=>$v['home_team'],
- "guest_team"=>$v['guest_team'],
- "lg_id"=>$v['lg_id'],
- "status"=>$v['status'],
- "tag"=> $v['tag'],
- 'match_time'=>$match_time,
- "update_time"=>$this->newTime
- ];
- }
- $ret = lm($model['model_result'],'Sports')->insert($set_match_r);
- if($ret != true) Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
- }
- //如果结果表有数据,则获取结果表没有的赛事
- foreach ($matchData as $k=>$v){
- foreach ($matchData_r as $kk=>$vv){
- if($v['id'] == $vv['match_id']){
- unset($matchData[$k]);
- }
- }
- }
- //如果还有未写入赛事
- if(!empty($matchData)){
- //写入结果表不存在赛事
- foreach ($matchData as $k=>$v){
- $time = time()-strtotime($v['match_time']);
- $match_time = $this->secTime($time);
- $set_match_r[] = [
- "match_id"=> $v['id'],
- "home_team"=>$v['home_team'],
- "guest_team"=>$v['guest_team'],
- "lg_id"=>$v['lg_id'],
- "status"=>$v['status'],
- "tag"=> $v['tag'],
- 'match_time'=>$match_time,
- "update_time"=>$this->newTime
- ];
- }
- $ret = lm($model['model_result'],'Sports')->insert($set_match_r);
- if($ret != true) Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
- }
- }
- Render([], '1', lang('Tips','Sports')->get('success'));
- }
- //写入地区数据 弃用
- public function area($opt = []){
- $ret = lm('st_area','Sports')->insert($opt);
- return $ret;
- }
- //写入国家数据 弃用
- public function country($opt = []){
- $ret = lm('st_country','Sports')->insert($opt);
- return $ret;
- }
- //写入联赛数据
- public function league($opt = []){
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = $this->commonFunction->getModels($game_code,1);
- $data = $opt['data'];
- if(empty($data['belong'])) Render([], '10013', lang('Tips','Sports')->get('country_error'));
- //获取联赛所属 国家/地区id
- $belong = St_area_countryModel::getID($data['belong']);
- $set_lg['area_id'] = $belong['area_id'];
- $set_lg['country_id'] = $belong['country_id'];
- //查询中间表 是否已记录
- $lg_id = lm($model['model_local_league'],'Sports')
- ->select('lg_id')
- ->where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
- ->first()->lg_id;
- //如果没有记录
- if(empty($lg_id)){
- //查询联赛是否已存在
- $id = lm($model['model_league'],'Sports')
- ->select('id')
- ->where('name_chinese','=',$data['name_chinese'])
- ->first()->id;
- if(empty($id)){
- $set_lg['name_chinese'] = $data['name_chinese'];
- $set_lg['kind'] = $data['kind'];
- $set_lg['match_mode'] = $data['match_mode'];
- $set_lg['if_stop'] = $data['if_stop'];
- $set_lg['last_time'] = $data['last_time'];
- $set_lg['utime'] = $this->newTime;
- $id = lm($model['model_league'],'Sports')->insertGetId($set_lg);
- $m_lg_id = $id;
- if($m_lg_id < 1) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
- }
- $set_local = [
- 'lg_id'=>$id,
- 'others_lg_id'=>$data['lg_id'],
- 'source'=>$data['source'],
- ];
- $ret = lm($model['model_local_league'],'Sports')-> insertGetId($set_local);
- if($ret < 1) Render([], '10017', lang('Tips','Sports')->get('local_league_error'));
- }
- Render([], '1', lang('Tips','Sports')->get('success'));
- }
- //写入赛事数据
- public function competition($opt = []){
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = $this->commonFunction->getModels($game_code,1);
- $data = $opt['data'];
- if(empty($data['lg_id'])) Render([], '10015', lang('Tips','Sports')->get('league_error'));
- //根据源联赛ID 获取本地关联id
- $lg_id = lm($model['model_local_league'],'Sports')
- ->select('lg_id')
- ->where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
- ->first()->lg_id;
- if(empty($lg_id)) Render([], '10015', lang('Tips','Sports')->get('league_error'));
- //查询关联记录是否存在
- $match_id = lm($model['model_local_match'],'Sports')
- ->where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
- ->value('match_id');
- if(empty($match_id)){
- //查询赛事是否存在
- $id = lm($model['model_match'],'Sports')
- ->where(['home_team'=>$data['home_team'],'guest_team'=>$data['guest_team'],'match_date'=>$data['match_date'],'match_time'=>$data['match_time']])
- ->value('id');
- //如果有上半场赛事id 获取上半场赛事是否存在
- if(!empty($data['half_match_id'])){
- $half_match_id = $match_id = lm($model['model_local_match'],'Sports')
- ->where(['others_match_id'=>$data['half_match_id'],'source'=>$data['source']])
- ->value('match_id');
- if(empty($half_match_id)) Render([], '10024', lang('Tips','Sports')->get('half_match_error'));
- }
- //写入赛事
- if(empty($id)){
- //如果赛事没有开始日期,则为冠军盘口赛事
- if(empty($data['match_date'])){
- //冠军盘口赛事获取所属联赛结束时间
- $last_time = lm($model['model_league'],'Sports')
- ->where(['id'=>$lg_id])
- ->value('last_time');
- if(empty($last_time)) Render([], '10023', lang('Tips','Sports')->get('lg_lastTime_error'));
- //给冠军盘口赛事 赋值时间
- $time = strtotime($last_time);
- $data['match_date'] = date('Y-m-d',$time);
- $data['match_time'] = date('H:m:i',$time);
- }
- $set_match = [
- 'ctime'=>$this->newTime,
- 'utime'=>$this->newTime,
- 'expire_time'=>date('Y-m-d H:m:i',time()+60),
- 'home_team'=>$data['home_team']?:'',
- 'guest_team'=>$data['guest_team']?:'no_team',
- 'lg_id'=>$lg_id,
- 'status'=>$data['status'],
- 'match_date'=>$data['match_date']?:'',
- 'match_time'=>$data['match_time']?:'',
- 'tag'=>$data['tag']?:0,
- 'is_rollball'=>$data['is_rollball']?:0,
- 'is_today'=>$data['is_today']?:0,
- 'is_morningplate'=>$data['is_morningplate']?:0,
- 'is_stringscene'=>$data['is_stringscene']?:0,
- 'us_time'=>$data['us_time']?:'',
- 'half_match_id'=>$half_match_id?:0,
- ];
- //写入赛事 返回id
- $id = lm($model['model_match'],'Sports')->insertGetId($set_match);
- if($id < 1) Render([], '10012', lang('Tips','Sports')->get('insert_error'));
- }
- $set_local = [
- 'match_id'=>$id,
- 'others_match_id'=>$data['match_id'],
- 'source'=>$data['source'],
- ];
- $ret = lm($model['model_local_match'],'Sports')-> insertGetId($set_local);
- if($ret < 1) Render([], '10018', lang('Tips','Sports')->get('local_match_error'));
- }
- Render([], '1', lang('Tips','Sports')->get('success'));
- }
- //写入赔率数据
- public function odds($opt){
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = $this->commonFunction->getModels($game_code,1);
- $data = $opt['data'];
- $match = lm($model['model_local_match'],'Sports')
- ->select('match_id','others_match_id')
- ->where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
- ->first();
- if(count($match) < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
- //获取赛事 本地/源ID
- $others_match_id = $match->others_match_id;
- $match_id = $match->match_id;
- $lg = lm($model['model_local_league'],'Sports')
- ->select('lg_id','others_lg_id')
- ->where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
- ->first();
- if(count($lg) < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
- //获取联赛 本地/源ID
- $others_lg_id = $lg->others_lg_id;
- $lg_id = $lg->lg_id;
- //查询 赔率数据是否存在
- $oddsID = lm($model['model_odds'],'Sports')
- ->where(['lg_id'=>$lg_id,'others_lg_id'=>$others_lg_id,'match_id'=>$match_id,'others_match_id'=>$others_match_id,'odds_code'=>$data['odds_code'],'sort'=>$data['sort']])
- ->value('id');
- $set_odds = [
- 'match_id'=> $match_id,
- 'others_match_id'=> $others_match_id,
- 'odds_code'=> $data['odds_code'],
- 'status'=> $data['status'],
- 'sort'=> $data['sort'],
- 'p_code'=> $data['p_code'],
- 'odds'=> $data['odds'],
- 'condition'=> $data['condition'],
- 'odds_only'=> $data['odds_only'],
- 'sole'=> $data['sole'],
- 'source'=> $data['source'],
- 'type'=> $data['type'],
- 'team'=> $data['team'],
- 'lg_id'=> $lg_id,
- 'others_lg_id'=> $others_lg_id,
- 'ctime'=> $this->newTime,
- 'utime'=> $this->newTime,
- 'expire_time'=>date('Y-m-d H:m:i',time()+60),
- ];
- //更新或写入赔率数据
- if(!empty($oddsID)){
- $ret = lm($model['model_odds'],'Sports')
- ->where(['id'=>$oddsID])
- -> update($set_odds);
- if($ret < 1) Render([], '10019', lang('Tips','Sports')->get('add_odds_error'));
- }else{
- $ret = lm($model['model_odds'],'Sports')->insert($set_odds);
- if($ret != true) Render([], '10019', lang('Tips','Sports')->get('add_odds_error'));
- }
- Render([], '1', lang('Tips','Sports')->get('success'));
- }
- //写入联赛结果
- public function league_result($opt){
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = $this->commonFunction->getModels($game_code,1);
- $data = $opt['data'];
- //验证结果所属联赛
- $lg_id = lm($model['model_local_league'],'Sports')
- ->select('lg_id')
- ->where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
- ->first()->lg_id;
- if($lg_id < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
- $lg_result_id = lm($model['model_league_result'],'Sports')
- ->where(['lg_id'=>$lg_id,'game_name'=>$data['game_name']])
- ->value('id');
- $set_lg_result = [
- 'lg_id'=>$lg_id,
- 'game_name'=>$data['game_name'],
- 'result'=>json_encode($data['result'],JSON_UNESCAPED_UNICODE),
- 'status'=>$data['status'],
- 'ctime'=> $this->newTime,
- 'utime'=> $this->newTime,
- ];
- //联赛结果数据处理
- if(!empty($lg_result_id)){
- $ret = lm($model['model_league_result'],'Sports')
- ->where(['id'=>$lg_result_id])
- -> update($set_lg_result);
- if($ret < 1) Render([], '10021', lang('Tips','Sports')->get('add_lg_r_error'));
- }else{
- $ret = lm($model['model_league_result'],'Sports')->insert($set_lg_result);
- if($ret != true) Render([], '10021', lang('Tips','Sports')->get('add_lg_r_error'));
- }
- Render([], '1', lang('Tips','Sports')->get('success'));
- }
- //写入赛事结果
- public function com_result($opt){
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = $this->commonFunction->getModels($game_code,1);
- $data = $opt['data'];
- //验证结果所属联赛
- $lg_id = lm($model['model_local_league'],'Sports')
- ->select('lg_id')
- ->where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
- ->first()->lg_id;
- if($lg_id < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
- //验证结果所属赛事
- $match_id = lm($model['model_local_match'],'Sports')
- ->select('match_id')
- ->where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
- ->value('match_id');
- if($match_id < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
- //查询结果是否存在
- $match_r_id = lm($model['model_result'],'Sports')
- ->where(['match_id'=>$match_id])
- ->value('id');
- $set_match_r = [
- "home_team"=>$data['home_team'],
- "guest_team"=>$data['guest_team'],
- "lg_id"=>$lg_id,
- "home_rate"=> $data['home_rate'],
- "guest_rate"=> $data['guest_rate'],
- "home_score"=> $data['home_score'],
- "guest_score"=> $data['guest_score'],
- "all_goal"=> $data['all_goal'],
- "status"=>$data['status'],
- "first_score"=>$data['first_score'],
- "last_score"=> $data['last_score'],
- "match_score"=> $data['match_score'],
- "match_winer"=> $data['match_winer'],
- "match_time"=> $data['match_time'],
- "match_process"=> $data['match_process'],
- "tag"=> $data['tag'],
- "match_id"=> $match_id,
- "u_home_score"=> $data['u_home_score'],
- "u_guest_score"=> $data['u_guest_score'],
- "p_code"=> $data['p_code'],
- "update_time"=>$this->newTime
- ];
- //赛事结果数据处理
- if(!empty($match_r_id)){
- $ret = lm($model['model_result'],'Sports')
- -> where(['id'=>$match_r_id])
- -> update($set_match_r);
- if($ret < 1) Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
- }else{
- $ret = lm($model['model_result'],'Sports')->insert($set_match_r);
- if($ret != true) Render([], '10022', lang('Tips','Sports')->get('add_match_r_error'));
- }
- Render([], '1', lang('Tips','Sports')->get('success'));
- }
- //写入赛事结果记录
- public function com_result_record($opt){
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = $this->commonFunction->getModels($game_code,1);
- $data = $opt['data'];
- //验证结果所属联赛
- $lg_id = lm($model['model_local_league'],'Sports')
- ->select('lg_id')
- ->where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
- ->first()->lg_id;
- if($lg_id < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
- //验证结果所属赛事
- $match_id = lm($model['model_local_match'],'Sports')
- ->select('match_id')
- ->where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
- ->value('match_id');
- if($match_id < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
- $match_r_id = lm($model['model_result_record'],'Sports')
- ->where(['match_id'=>$match_id,'match_time'=>$data['match_time']])
- ->value('id');
- $set_match_r = [
- "home_team"=>$data['home_team'],
- "guest_team"=>$data['guest_team'],
- "lg_id"=>$lg_id,
- "home_rate"=> $data['home_rate'],
- "guest_rate"=> $data['guest_rate'],
- "home_score"=> $data['home_score'],
- "guest_score"=> $data['guest_score'],
- "all_goal"=> $data['all_goal'],
- "status"=>$data['status'],
- "first_score"=>$data['first_score'],
- "last_score"=> $data['last_score'],
- "match_score"=> $data['match_score'],
- "match_winer"=> $data['match_winer'],
- "match_time"=> $data['match_time'],
- "match_process"=> $data['match_process'],
- "tag"=> $data['tag'],
- "match_id"=> $match_id,
- "p_code"=> $data['p_code'],
- "update_time"=>$this->newTime
- ];
- //赛事结果记录处理
- if($match_r_id > 0){
- $ret = lm($model['model_result_record'],'Sports')
- ->where(['id'=>$match_r_id])
- ->update($set_match_r);
- if($ret < 1) Render([], '10022', lang('Tips','Sports')->get('add_match_r_r_error'));
- }else{
- $ret = lm($model['model_result_record'],'Sports')->insert($set_match_r);
- if($ret != true) Render([], '10022', lang('Tips','Sports')->get('add_match_r_r_error'));
- }
- Render([], '1', lang('Tips','Sports')->get('success'));
- }
- //写入赔率记录
- public function odds_record($opt){
- $game_code = $opt['game_code'];
- //根据球类代码获取相关model
- $model = $this->commonFunction->getModels($game_code,1);
- $data = $opt['data'];
- $match = lm($model['model_local_match'],'Sports')
- ->select('match_id','others_match_id')
- ->where(['others_match_id'=>$data['match_id'],'source'=>$data['source']])
- ->first();
- if(count($match) < 1) Render([], '10016', lang('Tips','Sports')->get('match_error'));
- //获取赛事 本地/源ID
- $others_match_id = $match->others_match_id;
- $match_id = $match->match_id;
- $lg = lm($model['model_local_league'],'Sports')
- ->select('lg_id','others_lg_id')
- ->where(['others_lg_id'=>$data['lg_id'],'source'=>$data['source']])
- ->first();
- if(count($lg) < 1) Render([], '10015', lang('Tips','Sports')->get('league_error'));
- //获取联赛 本地/源ID
- $others_lg_id = $lg->others_lg_id;
- $lg_id = $lg->lg_id;
- //查询 赔率数据是否存在
- $oddsID = lm($model['model_odds_record'],'Sports')
- ->where(['odds_only'=>$data['odds_only']])
- ->value('id');
- $set_odds = [
- 'match_id'=> $match_id,
- 'others_match_id'=> $others_match_id,
- 'odds_code'=> $data['odds_code'],
- 'status'=> $data['status'],
- 'sort'=> $data['sort'],
- 'p_code'=> $data['p_code'],
- 'odds'=> $data['odds'],
- 'condition'=> $data['condition'],
- 'odds_only'=> $data['odds_only'],
- 'source'=> $data['source'],
- 'type'=> $data['type'],
- 'team'=> $data['team'],
- 'lg_id'=> $lg_id,
- 'others_lg_id'=> $others_lg_id,
- 'ctime'=> $this->newTime,
- 'utime'=> $this->newTime,
- ];
- //更新或写入赔率记录
- if(!empty($oddsID)){
- $ret = lm($model['model_odds_record'],'Sports')
- ->where(['id'=>$oddsID])
- -> update($set_odds);
- if($ret < 1) Render([], '10020', lang('Tips','Sports')->get('add_odds_r_error'));
- }else{
- $ret = lm($model['model_odds_record'],'Sports')->insert($set_odds);
- if($ret != true) Render([], '10020', lang('Tips','Sports')->get('add_odds_r_error'));
- }
- Render([], '1', lang('Tips','Sports')->get('success'));
- }
- /**
- * @param $data
- * @return mixed
- * json转数组
- */
- public function getAddData($data){
- $data = json_decode($data,true);
- return $data;
- }
- }
|