$obj,'detail'=>$obj]; public $AccountArrays = [] ; //buy_match 里batch_id 相同的数据 public $MatchBatchIdArrays = [] ; //buy_str 批次号相同的记录 public $BuyStrBatchIdArrays = [] ; //所有比赛场次号相同的记录 public $AllMatchIdArrays = []; //比赛结束消息通知模型 public $ComendNoticModel = null ; //返回的sql数组 private $sqlArray = [] ; public $gameType = '' ; //类型 public $resultId = 0 ; //比赛最终结果ID public $resultModel = null ; //比赛最终结果对像MODEL public $resultRecords = [] ; //比赛中间结果对像数组; //类型映射 public $gameAllMap = [ 'zq'=>'ZqRule', 'lq'=>'LqRule', 'wq'=>'WqRule', 'bq'=>'BqRule', ]; private $AdapterObj = null ; private $RefClass = null ; public function GetSQL(){ return $this->makeData(1,'success',$this->sqlArray); } public function doRun(){ try { DB::beginTransaction (); $ComendNoticModel = $this->getComendNoticeModel(); if (!$ComendNoticModel) { DB::commit (); return $this->makeData(1,'没有要处理的数据,直接退出'); } $this->writeStatusBegin($ComendNoticModel); $allmatchs = $this->getAllMatchIdArrays($ComendNoticModel->game_code, $ComendNoticModel->match_id, 0); if (empty($allmatchs)) { $this->writeStatusEndOk($ComendNoticModel); DB::commit(); return $this->makeData(1,'本赛事无订单数据,退出'); } $this->setAdapterObj($ComendNoticModel->game_code); $this->RefClass = new \ReflectionClass( $this->AdapterObj ) ; $this->getCompResult($ComendNoticModel->game_code,$ComendNoticModel->match_id); foreach ($allmatchs as $buymatchmodel){ $this->domatchPresess($buymatchmodel); } DB::rollback(); return $this->makeData(1,'success-all'); }catch (\Exception $e){ DB::rollBack(); return $this->makeData(0,'false',['msg'=>$e->getMessage(),'code'=>$e->getCode()]) ; } } //拼装 逻辑 sql; private function domatchPresess($model){ $fun = $model->odds_code ; if ( $this->RefClass->hasMethod( $fun)){ $winorfalse = $this->AdapterObj->$fun($model,$this->resultModel,$this->resultRecords); $this->makesql_up_buymatch_winorfalse($model->id,$winorfalse,date("Y-m-d H:i:s")); if ($model->bet_type ==2){ if ($winorfalse == -1){ //串式,一个错,全部错了..... $this->makesql_up_serial_lose($model); return ; } } $mainmodels = $this->GetBuyStrBatchIdArrays($model->batch_id,$model->bet_type); $lessCount = $mainmodels[0]->wait_match_num; if ($lessCount > 0 ){ //还有没结束的赛事,不急做结算处理 $this->wait_match_num_desc($model->batch_id,$model->bet_type); return true ; } $this->doSettlement($model); }else{ throw new \Exception('找不到此玩法的胜负规则逻辑!',4005); } } private function wait_match_num_desc($batch_id,$type){ $table = ($type ==1)? 'money_buy_simplex': 'money_buy_str'; $sql = "update $table set wait_match_num=wait_match_num-1 where batch_id='$batch_id' "; $this->sqlArray[] = $sql; } //实现结算相关的逻辑 //逐个计算每个下单情况给用户加钱 {加用户余额,写流水详细} private function doSettlement($model){ $batchid = $model->batch_id ; $bettype = $model->bet_type ; $allbatchModels = $this->getAllBatchIdArrays($model->game_code,$batchid); //存在相同比赛不同玩法串式,先前已经输的情况下,此时没有数据了.... if (empty($allbatchModels)){ return true; } $allbuyModels = $this->GetBuyStrBatchIdArrays($batchid,$bettype); $account_identity = ''; if ($bettype ==2){ //串式投注 $con_arr = [] ; foreach ($allbatchModels as $batchModel){ $con_arr[] = ['odds'=>$batchModel->odds,'winorlose'=>$batchModel->result,'money'=>1]; } $peilvArray = $this->winOddsCalculation(2,$con_arr); foreach ($allbuyModels as $buyModel){ if ($account_identity==''){ $account_identity = $buyModel->account_identity; } $money = sprintf("%.2f",$buyModel->money * $peilvArray['returnMoney']) ; $sql = "update money_buy_str set gain_money=$money where batch_id='$batchid' and id=$buyModel->id "; $this->sqlArray[] = $sql ; } }else{ //单式投注 $money = 0 ; if ( $account_identity ==''){ $account_identity = $allbuyModels['0']->account_identity; } $money = 0 ; foreach ($allbatchModels as $batchModel){ $con_arr[] = ['odds'=>$model->odds,'winorlose'=>$batchModel->result,'money'=>$allbuyModels['0']->money]; $money_odds_array = $this->winOddsCalculation(1,$con_arr); $money += sprintf("%.2f",$batchModel->bet_money * $money_odds_array['returnMoney'] ) ; } $sql = "update money_buy_simplex set gain_money=$money where batch_id=$batchid "; $this->sqlArray[] = $sql ; } $money_diff = $this->get_money_chage($batchid,$bettype); $this->sql_accountdetail_money($account_identity,$money_diff); $this->sql_moneydetqil_log($allbuyModels['0'],$model,$money_diff); } /** * 注单返现计算 * @param mixed $dataArray 结果表数据 [['odds'=>xx,'winOrLose'=>1],[...]]; * @return array 注单返现额 */ private function winOddsCalculation($dataArray){ $returnMoney = 1; // 循环计算每注返现赔率 foreach ($dataArray as $value) { $odds = intval($value['odds']); // 因结果不同更改赔率 switch (intval($value['winOrLose'])) { case 1: $oddsDiscount = 1 + $odds * 1; break; case 2: $oddsDiscount = 1; break; case 3: $oddsDiscount = 1 + $odds * 0.5; break; default: $oddsDiscount = 0.5; } // 计算每注返现 $returnMoney = $returnMoney * $oddsDiscount; } // 计算赚钱 $earnMoney = $returnMoney - 1; return ['earnMoney' => $earnMoney, 'returnMoney' => $returnMoney]; } /** * UUID 生成 */ public static function UUID() { $prefix = ''; $uuid = ''; $str = md5(uniqid(mt_rand(), true)); $uuid = substr($str, 0, 8) . '-'; $uuid .= substr($str, 8, 4) . '-'; $uuid .= substr($str, 12, 4) . '-'; $uuid .= substr($str, 16, 4) . '-'; $uuid .= substr($str, 20, 12); return $prefix . $uuid; } private function sql_accountdetail_money($acount ,$moneycg){ $sql = "update account_detail set account_detailed set available_cash=available_cash+$moneycg,cash=cash+$moneycg where account_identity='$acount' "; $this->sql[] = $sql ; } private function sql_moneydetqil_log($allbuyModel,$model,$money_diff){ $uuid = self::UUID(); $dtime = date("Y-m-d H:i:s"); $detailModel = DB::table('Account_detailed')->where(['account_identity',$allbuyModel->account_indentity])->first(); if (!$detailModel){ throw new \Exception('查无此用户数据account_detail='.$allbuyModel->account_indentity,41002); } $money_cash = $detailModel->cash ; $SQL = "inser into account_detailed set identity='$uuid',trade_id='$allbuyModel->order_id',account_name='$allbuyModel->account_name', " ; $SQL .= "account_identity=='$allbuyModel->account_indentity',money='$money_diff',money_time='$dtime',money_type=1, "; $SQL .= "trade_desc='中奖收入',status='成功',money_cash=$money_cash " ; $this->sql[] = $SQL ; } //计算 某个比赛结束后 对应订单的资金变化 private function get_money_chage($batch_id,$type){ if ($type ==1 ){ $ret = DB::select("select sum(gain_money-money) as dfmoney from money_buy_simplex where batch_id='$batch_id'"); if (!isset( $ret['0']['dfmoney'])){ throw new \Exception("资金变化不正确!"); } $moneydiff = sprintf("%.2f",$ret['0']['dfmoney']); }else{ $ret = DB::select("select sum(gain_money-money) as dfmoney from money_buy_str where batch_id='$batch_id'"); if (!isset( $ret['0']['dfmoney'])){ throw new \Exception("资金变化不正确!"); } $moneydiff = sprintf("%.2f",$ret['0']['dfmoney']); } return $moneydiff ; } //生成串式失败的逻辑sql; private function makesql_up_serial_lose($model){ $batchid = $model->batch_id ; $sql = "update money_buy_match set result=-1 where bet_type=2 and batch_id=$batchid "; $this->sqlArray[] = $sql; $sql = "update money_buy_str set game_status=3,wait_match_num=0 where batch_id=$batchid "; $this->sqlArray[] = $sql ; return true ; } private function makesql_up_buymatch_winorfalse($id,$result,$utime){ $sql = " update money_buy_match set result=$result,utime=$utime where id=$id limit 1"; $this->sqlArray[] = $sql; return true; } /* private function masql_up_buymatch_false($batch_id,$type,$match_id=0){ $uptime = date("Y-m-d H:i:s"); $sql = " update money_buy_match set result=-1 ,utime=$uptime,status=0 where batch_id=$batch_id "; $this->sqlArray[] = $sql; if ($type==2){ $sql = "update money_buy_str set settle_status=1,game_status=3 ,wait_match_num=0 where batch_id=$batch_id " ; }else{ $sql = "update money_buy_simplex set settle_status=1,game_status=3 ,wait_match_num=wait_match_num-1 where batch_id=$batch_id and match_id=$match_id " ; } $this->sqlArray[] = $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->pret = 1 ; $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 getAllMatchIdArrays( $type,$matchid , $result = 0 ){ if (is_null($result)){ $ret = DB::table('money_buy_match')->where(['game_code'=>$type,'match_id'=>$matchid])->get(); }else{ $ret = DB::table('money_buy_match')->where(['game_code'=>$type,'match_id'=>$matchid,'result'=>$result])->get(); } $this->AllMatchIdArrays = $ret; return $ret; } public function getAllBatchIdArrays($type,$batchid,$owhere=[]){ if (!empty($owhere)){ $where = array_merge(['game_code'=>$type,'batch_id'=>$batchid],$owhere); }else{ $where = ['game_code'=>$type,'batch_id'=>$batchid] ; } $ret = DB::table('money_buy_match')->where($where)->order('id','asc')->get(); //if (empty($ret)){ throw new \Exception('没有记录' ,4008);} return $ret; } //获取到某个用户的account模型和detail模型,用于用户的余额计算等使用 public function getUserInfo($account_name){ if (isset($this->AccountArrays[$account_name])){ return $this->AccountArrays['$account_name']; } $accountModel = DB::table('Account')->where(['account',$account_name])->first(); if (!$accountModel){ throw new \Exception('查无此用户数据account='.$account_name,41002); } $detailModel = DB::table('Account_detailed','Commons')->where(['account_identity',$accountModel->identity])->first(); if (!$detailModel){ throw new \Exception('查无此用户数据account_detail='.$accountModel->identity,41002); } $ret = [ 'account'=>$detailModel, 'detail' => $detailModel , ]; $this->AccountArrays[$account_name] = $ret ; $this->AccountArrays[$accountModel->identity] = $ret ; return $ret; } //获取到某个用户某个赛事的下单记录集[] public function GetMatchBatchIdArrays($batch_id,$type){ if (isset($this->MatchBatchIdArrays[$type][$batch_id])){ // return $this->MatchBatchIdArrays[$type][$batch_id]; } $all = DB::table("money_buy_match")->where(['batch_id',$batch_id,'result'=>0])->get(); if (!$all) { throw new \Exception('无效的Money_buy_str->batch_id='.$batch_id , 41001); } $this->MatchBatchIdArrays[$type][$batch_id] = $all ; return $all ; } //根据购买批次 获取下单信息 public function GetBuyStrBatchIdArrays($batch_id,$type){ if (isset($this->BuyStrBatchIdArrays[$batch_id])){ // return $this->BuyStrBatchIdArrays[$batch_id]; } if ($type==2) { $all = DB::table("money_buy_str")->where(['batch_id'=>$batch_id,'game_status'=>0])->get(); }else{ $all = DB::table("money_buy_simplex")->where(['batch_id'=>$batch_id,'game_status'=>0])->get(); } if (!$all) { throw new \Exception('无效的batch_id='.$batch_id , 41001); } // $this->BuyStrBatchIdArrays[$batch_id] = $all ; return $all ; } //得到比赛最终结果记录 和 中间结果记录 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_resultn')->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; } if (empty($model) || empty($models)){ throw new \Exception('没找到比赛结果记录match_id_'.$type.'-'.$match_id,4007); return false; } $this->resultModel = $model; $this->resultRecords = $models ; $ret = [ 'result' => $model, 'records' => $models , ]; return $ret; } }