'ZqRule', 'lq'=>'LqRule', 'wq'=>'WqRule', 'bq'=>'BqRule', ]; private $AdapterObj = null ; private $RefClass = null ; //按赛事批量结算 public function doRun(){ try { $ComendNoticModel = $this->getComendNoticeModel(); if (!$ComendNoticModel) { return $this->makeData(1,'没有要处理的数据,直接退出'); } $this->writeStatusBegin($ComendNoticModel); DB::beginTransaction (); $allmatchs = DB::table('money_buy_match')->where(['game_code'=>$ComendNoticModel->game_code,'match_id'=>$ComendNoticModel->match_id,'result'=>0])->get(); if (empty($allmatchs)) { $this->writeStatusEndOk($ComendNoticModel); DB::commit(); return $this->makeData(1,'本赛事无订单数据,退出'); } $this->gameType = $ComendNoticModel->game_code ; $this->match_id = $ComendNoticModel->match_id ; $this->setAdapterObj($ComendNoticModel->game_code); $this->RefClass = new \ReflectionClass( $this->AdapterObj ) ; $this->getCompResult($ComendNoticModel->game_code,$ComendNoticModel->match_id); $this->Settlement_simplex($this->gameType,$this->match_id); $this->Settlement_str($this->gameType,$this->match_id); $this->writeStatusEndOk($ComendNoticModel); DB::commit(); return $this->makeData(1,'success-all'); }catch (\Exception $e){ DB::rollBack(); return $this->makeData(0,'false',['msg'=>$e->getMessage(),'code'=>$e->getCode()]) ; } } //单式订单结算处理 private function Settlement_simplex($game_type,$match_id){ $buyModels = DB::table('money_buy_simplex')->where(['match_id'=>$match_id,'game_status'=>0])->get(); if (empty($buyModels)){ return true; } $buymatchModles = DB::table('money_buy_match')->where(['match_id'=>$match_id,'bet_type'=>1,'result'=>0,'game_code'=>$game_type])->orderby('batch_id','asc')->get(); if (empty($buymatchModles)) { return true ; } $buyKeyModel = $buymatchKeyModles = [] ; foreach ($buyModels as $val){ $batch_id = $val->batch_id; $buyKeyModel[$batch_id] = $val; } foreach ($buymatchModles as $val){ $batch_id = $val->batch_id; $buymatchKeyModles[$batch_id][] = $val; } foreach ($buyKeyModel as $batch_id=>$val){ foreach ($buymatchKeyModles as $sub_batch_id=>$fsval){ foreach ($fsval as $sval) { $winorfalse = $this->winOrfalseInfo($sval); $this->makesql_up_buymatch_winorfalse($sval->id, $winorfalse, date("Y-m-d H:i:s")); } } } return true; } //串式订单结算处理 private function Settlement_str($game_type,$match_id){ $matchModels = DB::table('money_buy_match')->where(['match_id'=>$match_id,'bet_type'=>2,'game_code'=>$game_type])->get(); if (empty($matchModels)) { return true; } $batch_ids_array = [] ; $matchKeyModels = [] ; foreach ($matchModels as $val){ $matchKeyModels[$val->batch_id][] = $val ; if (!in_array($val->batch_id,$batch_ids_array)){ $batch_ids_array[] = $val->batch_id; } } $buyModels = DB::table('money_buy_str')->whereIn( 'batch_id',$batch_ids_array)->get(); $buyKeyModels = [] ; if (empty($buyModels)) { return true ; } foreach ($buyModels as $val){ $buyKeyModels[$val->batch_id][] = $val ; } foreach ($matchKeyModels as $key=>$sval_2){ foreach ($sval_2 as $sval){ if ( $sval->match_id != $this->match_id ){ continue ; } $winorfalse = $this->winOrfalseInfo($sval); $this->makesql_up_buymatch_winorfalse($sval->id,$winorfalse,date("Y-m-d H:i:s")); $sql = "update money_buy_str set wait_match_num=wait_match_num-1 where batch_id='$sval->batch_id' "; DB::update($sql); } } } //输赢结果判断 $sval 为 money_buy_match Model; 结果为 -1输 1赢 2平 3赢半平半 4输半平半 private function winOrfalseInfo($sval){ $fun = $sval->odds_code ; $fun2 = $sval->p_code ; if ($sval->p_code != 'gj'){ if (empty($this->resultModel) || empty($this->resultRecords)){ throw new \Exception('非冠军比赛结果数据不能为空'); } if ( $this->RefClass->hasMethod($fun) ){ $winorfalse = $this->AdapterObj->$fun($sval,$this->resultModel,$this->resultRecords); }elseif ( $this->RefClass->hasMethod($fun2) ){ $winorfalse = $this->AdapterObj->$fun2($sval,$this->resultModel,$this->resultRecords); }else{ throw new \Exception('没有找到玩法输赢判断规则',40010); } }else{ $this->getGjDatas($sval); if (empty($this->gjModel)){ throw new \Exception('冠军比赛结果数据不能为空'); } if ( $this->RefClass->hasMethod($fun) ){ $winorfalse = $this->AdapterObj->$fun($sval,$this->gjModel,[]); }elseif ( $this->RefClass->hasMethod($fun2) ){ $winorfalse = $this->AdapterObj->$fun2($sval,$this->gjModel,[]); }else{ throw new \Exception('没有找到玩法输赢判断规则',40010); } } if (!in_array($winorfalse,[-1,1,2,3,4])) { throw new \Exception('输赢结果异常!'); } return $winorfalse ; } private function makesql_up_buymatch_winorfalse($id,$result,$utime){ $sql = " update money_buy_match set result=$result,utime='$utime' where id=$id "; DB::update($sql) ; return true; } //设置 输赢 解析规则适配器 private function setAdapterObj($game_type){ $game_type = strtolower($game_type); if ( !isset($this->gameAllMap)){ throw new \Exception('赛事类型错误-'.$game_type,4002); return false; } switch ($game_type){ case 'bq': $this->AdapterObj = new BqRule(); break; case 'lq': $this->AdapterObj = new LqRule(); break; case 'wq': $this->AdapterObj = new WqRule(); break; case 'zq': $this->AdapterObj = new ZqRule(); break; } return true ; } //返回数据 private function makeData($status=1,$message='success',$data=''){ return [ 'status' => $status , 'message' =>$message, 'data' => $data , ] ; } //写处理状态 public function writeStatusBegin($comendnticeModel){ $comendnticeModel->status = 1 ; $comendnticeModel->done_time = date("Y-m-d H:i:s"); $comendnticeModel->pcount ++ ; $comendnticeModel->logs = 'begin|'; $ret = $comendnticeModel->save(); return $ret; } //写处理状态结束 public function writeStatusEndOk($comendnticeModel){ $comendnticeModel->status = 4 ; $comendnticeModel->done_time = date("Y-m-d H:i:s"); $comendnticeModel->logs = $comendnticeModel->logs.'end ok'; $ret = $comendnticeModel->save(); return $ret; } //是否有需要进行结果算处理的赛事记录 public function getComendNoticeModel(){ $ret = (new ComendnoticeModel())->where('status',0)->orderby('id','asc')->first(); $this->ComendNoticModel = $ret; return $ret; } //得到比赛最终结果记录 和 中间结果记录 public function getCompResult($type,$match_id){ $model = null ; switch ($type){ case 'bq': $model = DB::table('st_bq_result')->where('match_id',$match_id)->first(); $models = DB::table('st_bq_result_record')->where('match_id',$match_id)->orderBy('id','asc')->get()->toArray(); break; case 'lq': $model = DB::table('st_lq_result')->where('match_id',$match_id)->first(); $models = DB::table('st_lq_result_record')->where('match_id',$match_id)->orderBy('id','asc')->get()->toArray(); break; case 'wq': $model = DB::table('st_wq_result')->where('match_id',$match_id)->first(); $models = DB::table('st_wq_result_record')->where('match_id',$match_id)->orderBy('id','asc')->get()->toArray(); break; case 'zq': $model = DB::table('st_zq_result')->where('match_id',$match_id)->first(); $models = DB::table('st_zq_result_record')->where('match_id',$match_id)->orderBy('id','asc')->get()->toArray(); break; } $this->resultModel = $model; $this->resultRecords = $models ; $ret = [ 'result' => $model, 'records' => $models , ]; return $ret; } //冠军比赛结果 public function getGjDatas($matchModel){ if ( !empty($this->gjModel) ) { return $this->gjModel ; } $table = 'st_'.$matchModel->game_code.'_league_result'; $where = ['lg_id'=>$matchModel->lg_id,'game_name'=>$matchModel->odds_code]; $model = DB::table($table)->where($where)->first(); if (empty($model)){ throw new \Exception('冠军数据没找到!'); } $this->gjModel = $model ; return $model ; } }