<?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\Models\Comendnotice  as ComendnoticeModel;

use  App\Lib\Settlement\Adapter\ZqRule;
use  App\Lib\Settlement\Adapter\LqRule;
use  App\Lib\Settlement\Adapter\WqRule;
use  App\Lib\Settlement\Adapter\BqRule ;


class SettlementSql
{

    //用户的期末基本资料表 [uid]['account'=>$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 $match_id = 0 ;  //比赛ID
    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 = 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])->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){
            $money = 0 ;
            $allgame_status = 0 ;
            foreach ($buymatchKeyModles as $sub_batch_id=>$fsval){

                    foreach ($fsval as $sval) {
                        $winorfalse = $this->winOrfalseInfo($sval);
                        if (!in_array($winorfalse, [-1, 1, 2, 3, 4])) {
                            throw  new  \Exception('胜负规则结果数据错!', 4005);
                        }
                        $this->makesql_up_buymatch_winorfalse($sval->id, $winorfalse, date("Y-m-d H:i:s"));

                        if ($winorfalse != -1) {
                            $peilvretarr = $this->winOddsCalculation([['odds' => $sval->odds, 'winOrLose' => $winorfalse]]);
                            $money += $peilvretarr['returnMoney'] * $sval->bet_money;
                            $allgame_status++;
                        }
                    }
            }

            $allgame_status = ($allgame_status>0 ) ?  1 : 2 ;

            $sql = " update  money_buy_simplex   set   game_status=$allgame_status,wait_match_num=0,gain_money=$money where   match_id=$this->match_id and  batch_id='$batch_id'   ";
            $this->sqlArray[] = $sql ;



            if ($money >0){
                $this->sql_accountdetail_money($val->account_identity,$money);
                $this->sql_moneydetail_log($val,null,$money);
            }

            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);
                       if (!in_array($winorfalse,[-1,1,2,3,4])){     throw  new  \Exception('胜负规则结果数据错!',4005);     }

                       $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 ";
                       $this->sqlArray[] = $sql ;
                       $sval->result = $winorfalse ;

                       if ($winorfalse == -1){
                              //一条出错，全部作为错误处理
                              $this->makesql_up_serial_lose($sval);
                              break;
                       }

                       if (!isset($buyKeyModels[$key]['0'])){ throw  new  \Exception('购买主记录不存在，数据有问题');}
                       if ($buyKeyModels[$key]['0']->wait_match_num <=1 ){
                           $oddsarr = [];
                           //串式最后一条记录了，需要做结算处理了
                           foreach (  $matchKeyModels[$key] as $fval){
                                    $oddsarr[] = ['odds'=>$fval->odds,'winOrLose'=>$fval->result] ;
                                    $peilvArray = $this->winOddsCalculation($oddsarr);
                           }
                           foreach  ($buyKeyModels[$key] as $buyModel){
                               $money = sprintf("%.2f",$buyModel->money * $peilvArray['returnMoney']) ;
                               $sql = "update  money_buy_str set gain_money=$money,game_status=1  where  batch_id='$key' and id=$buyModel->id ";
                               $this->sqlArray[] = $sql ;

                               $this->sql_accountdetail_money($buyModel->account_identity,$money);
                               $this->sql_moneydetail_log($buyModel,null,$money);
                           }
                           break;
                   }
               }

           }


    }

    //输赢结果判断 $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);
            }
        }

        return $winorfalse ;
    }

    /**
     * 注单返现计算
     * @param mixed $dataArray 结果表数据 [['odds'=>xx,'winOrLose'=>1],[...]];
     * @return array 注单返现额
     */
    private function   winOddsCalculation($dataArray){
        $returnMoney = 1;
        // 循环计算每注返现赔率
        foreach ($dataArray as $value) {
            $odds = floatval($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_detailed  set available_cash=available_cash+$moneycg,cash=cash+$moneycg where   account_identity='$acount' ";
            $this->sqlArray[] = $sql ;
    }

    private function sql_moneydetail_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_identity)->first();
            if (!$detailModel){
                throw  new   \Exception('查无此用户数据account_detail='.$allbuyModel->account_identity,41002);
            }
            $money_cash =  $detailModel->cash ;

            $SQL  = "insert into money_details(identity,trade_id,account_name,account_identity,money,money_time,money_type,trade_desc,status,money_cash,trade_type) values";
            $SQL .= "('$uuid','$allbuyModel->order_id','$allbuyModel->account_name','$allbuyModel->account_identity','$money_diff','$dtime',1,'中奖收入','1','$money_cash',4)" ;

            $this->sqlArray[] = $SQL ;

    }

    //生成串式失败的逻辑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  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;
    }




    //获取到某个用户的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')->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_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;
        }

        /*
        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;

    }


    //冠军比赛结果
    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)){
            $this->gjModel = $model ;
        }
        return $model ;
    }


}