| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/4/25
- * Time: 14:10
- */
- namespace App\Lib\Settlement;
- set_time_limit(600);
- ini_set('memory_limit', '256M');
- use Illuminate\Support\Facades\DB;
- use App\Lib\Settlement\SettlementBase ;
- /**
- 按订单结算或重结算
- */
- class SettlementOrder extends SettlementBase
- {
- private $orderId = '' ; //订单ID
- private $orderType = 1 ; //订单类型 1单式 2串式;
- private $BuyDatasMainModel = [] ; //订单豪华版 单式一条 串式可能多条
- private $BuyDatas = [] ; //订单豪华版 单式一条 串式可能多条
- //返回数据
- public static function makeData($status=1,$message='success',$data=''){
- return [
- 'status' => $status ,
- 'message' =>$message,
- 'data' => $data ,
- ] ;
- }
- public function reSettlement($order_ids,$bettype=2){
- if (empty($order_ids)){ return self::makeData(5,'订单号不能为空'); }
- try{
- DB::beginTransaction ();
- foreach ($order_ids as $order_id){
- $this->BuyDatasMainModel = $this->orderTypeGet($order_id,$bettype);
- if ( $this->BuyDatasMainModel->settle_status==2 ){ continue ; }
- if ($this->orderType==1){
- $this->SingOrder($order_id);
- }else{
- $this->ChuanOrder($order_id);
- }
- }
- DB::commit();
- }catch (\Exception $e){
- DB::rollBack();
- return self::makeData(-2,$e->getMessage()); ;
- }
- return self::makeData();
- }
- public function orderTypeGet($order_id,$bettype){
- if ($bettype==1){
- $datas = DB::table('money_buy_simplex')->where('order_id',$order_id)->first();
- $this->orderType = 1 ;
- }else{
- $datas = DB::table('money_buy_str')->where('order_id',$order_id)->first();
- $this->orderType = 2 ;
- }
- if (!$datas){
- throw new \Exception('没有主订单信息');
- }
- return $datas ;
- }
- /**
- * 单式注单结算
- * @param mixed $order_id 注单ID
- */
- public function singOrder($order_id) {
- // 查询订单下所有的单式注单
- $simplexData = DB :: table('money_buy_simplex')
- -> select('batch_id', 'account_identity', 'order_id', 'money', 'game_code', 'info_identity','match_id')
- -> where(['order_id' => $order_id])
- -> first();
- // 查询单式注单下的所有玩法
- $matchData = DB :: table('money_buy_match') -> select('odds', 'result', 'batch_id', 'bet_money') -> where(['bet_type' => 1, 'batch_id' => $simplexData -> batch_id, 'match_id' => $simplexData->match_id]);
- $matchData = $matchData -> where(function($query) {
- $query = $query -> where(['result' => 1])
- -> orWhere(['result' => 2])
- -> orWhere(['result' => 3])
- -> orWhere(['result' => 4]);
- });
- $matchData = $matchData -> get() -> toArray();
- // 计算总回款
- $settlementBase = new \App\Lib\Settlement\SettlementBase;
- $returnMoney = 0;
- foreach ($matchData as $k => $v) {
- $oddsResult[0]['winOrLose'] = $v -> result;
- $oddsResult[0]['odds'] = $v -> odds;
- $getReturnMoney = $settlementBase -> winOddsCalculation($oddsResult);
- $returnMoney += ($getReturnMoney['returnMoney'] + 1) * $v -> bet_money;
- }
- $returnMoney = sprintf("%.2f",substr(sprintf("%.3f", $returnMoney), 0, -1));
- // 判断盈亏 1 赢 2 输 3 平
- $game_status = $returnMoney > $simplexData -> money ? 1 : ($returnMoney == $simplexData -> money ? 3 : 2);
- // 修改投注表状态及盈亏
- DB :: table('money_buy_simplex')
- -> where(['order_id' => $order_id])
- -> update(['settle_status' => 2, 'game_status' => $game_status, 'gain_money' => $returnMoney]);
- $this -> insertData(
- $order_id,
- $returnMoney,
- $simplexData -> account_identity,
- 1,
- $simplexData -> game_code,
- $simplexData -> info_identity,
- $simplexData -> money
- );
- }
- /**
- * 结算数据填入
- * @param mixed $order_id 注单ID
- * @param mixed $returnMoney 返现金额
- * @param mixed $account_identity 用户ID
- * @param mixed $type 1单式 2串式
- * @param mixed $game_name 游戏名(zq,lq)
- * @param mixed $buy_identity 游戏投注id
- * @param mixed $money 投注金额
- */
- public function insertData($order_id, $returnMoney, $account_identity, $type, $game_name, $buy_identity, $money) {
- // 查询用户当前剩余金额
- $accountInfo = DB :: table('account_detailed')
- -> join('account', 'account_detailed.account_identity', 'account.identity')
- -> select(['available_cash', 'cash', 'account', 'account_identity'])
- -> where(['account_identity' => $account_identity])
- -> first();
- // 计算用户回账后余额
- $available_cash = $accountInfo -> available_cash + $returnMoney;
- $cash = $accountInfo -> cash + $returnMoney;
- // 添加流水记录
- $info_identity = UUID();
- $money_time = date('Y-m-d H:i:s', time());
- $trade_desc = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
- $reason = $type == 1 ? '单式投注订单回款' : '串式投注订单回款';
- DB :: table('money_details') -> insert([
- 'info_identity' => $info_identity,
- 'trade_id' => $order_id,
- 'account_name' => $accountInfo -> account,
- 'account_identity' => $accountInfo -> account_identity,
- 'money' => $returnMoney,
- 'money_time' => $money_time,
- 'money_type' => 1,
- 'money_cash' => $available_cash,
- 'trade_type' => 1,
- 'trade_desc' => $trade_desc,
- 'reason' => $reason,
- 'sysetem_user' => '系统',
- 'status' => '1',
- ]);
- // 修改用余额
- DB:: table('account_detailed')
- -> where(['account_identity' => $account_identity])
- -> update(['available_cash' => $available_cash, 'cash' => $cash]);
- // 新增用户中奖信息
- $content = $type == 1 ? '您的单式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服'
- : '您的串式投注订单' . $order_id . '于' . $money_time . '成功回款' . $returnMoney . '该次投注流程结束,如有疑问请联系客服';
- DB:: table('account_news') -> insert([
- 'identity' => $info_identity,
- 'account_identity' => $account_identity,
- 'title' => '投注订单回款通知',
- 'content' => $content,
- 'details' => $content,
- 'write_time' => $money_time,
- 'read_status' => -1,
- 'type' => 1,
- ]);
- // 新增中奖记录表
- DB:: table('money_prize') -> insert([
- 'info_identity' => $info_identity,
- 'order_id' => $order_id,
- 'account_identity' => $account_identity,
- 'account_name' => $accountInfo -> account,
- 'game_name' => $game_name,
- 'buy_identity' => $buy_identity,
- 'money' => $money,
- 'money_time' => $money_time,
- 'status' => 1,
- 'prize_money' => $returnMoney,
- 'get_money' => $returnMoney - $money,
- ]);
- }
- //单个串式订单的处理
- public function ChuanOrder($order_id){
- $batch_id = $this->BuyDatasMainModel->batch_id ;
- $matchModels = DB::table('money_buy_match')->where(['batch_id'=>$batch_id,'bet_type' => 2])->get();
- if (empty($matchModels)) { throw new \Exception('match 数据异常');}
- $in_array = [] ;
- foreach ($matchModels as $val){
- if (!in_array($val->result,[-1,1,2,3,4])){ throw new \Exception('match 比赛结果异常->'.$val->id); }
- if ($val->result == -1){
- /*
- $ret = DB::table('money_buy_str')->where('id',$this->BuyDatasMainModel->id)->update(['wait_match_num'=>0,'prize_note'=>0,'settle_status'=>2,'gain_money'=>0]);
- if (!$ret){ throw new \Exception('更新数据出错1!'); }
- $ret = DB::update('update money_buy_str set settle_status=2,game_status=3,gain_money=0 where batch_id = ?', [$batch_id]);
- */
- DB::table('money_buy_str')->where('batch_id',$batch_id)->update(['wait_match_num'=>0,'prize_note'=>0,'game_status'=>3,'settle_status'=>2,'gain_money'=>0]);
- //if(!($ret || $ret===0)){ throw new \Exception('更新数据出错2!'); }
- return true ;
- }
- $in_array[] = ['odds'=>$val->odds,'winOrLose'=>$val->result] ;
- }
- $chuanNum = intval(substr($this->BuyDatasMainModel->str_type,0,1));
- $lasPeilv = $this->stringComputing([$in_array,$chuanNum]);
- $money = floatPointDigit($this->BuyDatasMainModel->money * $lasPeilv ) ;
- $ret = DB::update('update money_buy_str set settle_status=2 , game_status=1 , gain_money=? where order_id = ?', [$money,$order_id]);
- if(!($ret || $ret===0)){ throw new \Exception('更新数据出错3!'); }
- $this->insertData($order_id,$money,$this->BuyDatasMainModel->account_identity,2,$val->game_code,$this->BuyDatasMainModel->info_identity,$money);
- return true ;
- }
- }
|