<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/23
 * Time: 14:36
 */

namespace Biz\Game;


//获取某个代理下的体育项目订单相关的数据
class SportbetRecordLogic
{
    //代理Model
    private  $wagentModel = '';
    //订单ID
    private  $vendorid =  null ;
    //是否结算
    private  $isjs = 0 ;
    //返回数据
    private  $retDataArray  = [] ;
    //db obj ;
    private  $db ;
    //
    private  $buyModel = null ;

    public function  __construct($wagModel,$vendorid,$isjs)
    {
        $this->wagentModel = $wagModel;
        $this->vendorid = $vendorid;
        $this->isjs = ($isjs==1) ? 2 : 1  ;
        $this->retDataArray = $this->getDefaultBackData();
        $this->db = $GLOBALS['DB'] ;
    }


    public  function  dorun(){
        $buyModel = lm("Money_buy_simplex",'Commons')->where(['order_id'=>$this->vendorid,'settle_status'=>$this->isjs])->first();

        if ( empty($buyModel) ) {
            $buyModel = lm("Money_buy_str",'Commons')->where(['order_id'=>$this->vendorid,'settle_status'=>$this->isjs])->first();
            if (empty($buyModel)){
                return  false;
            }
        }
        $this->buyModel = $buyModel ;


        $this->Batchsetval();
        $ret = $this->retDataArray;
        return  $ret;
    }

    //批量设置数据
    public function Batchsetval(){
        $buyModel = $this->buyModel ;
        if ($buyModel){
            $this->Setval('isjs',$buyModel->settle_status==2 ? 1 :0);
            $this->Setval('tzmoney',$buyModel->money);
            //$this->Setval('username',strlen($this->wagentModel->agent_pre)>0 ?  (substr($buyModel->account_name,strlen($this->wagentModel->agent_pre))): substr($buyModel->account_name ) );
            $this->Setval('username',$buyModel->account_name );
            $this->Setval('win',($buyModel->settle_status==2) ? $buyModel->prize_money : 0 );
            $this->Setval('updatetime',$buyModel->money_time);
            $this->Setval('validamount',$buyModel->money);
            $this->Setval('tztype',$buyModel->game_name);
            $this->Setval('orderid',$buyModel->order_id);


            $this->Setval('iscancel',($buyModel->status==4) ? 1 : 0 );

            switch ( strtolower($buyModel->game_name) ){
                case  'zq':
                    $this->Setval('sportid', 0);
                    break;
                case 'lq':
                    $this->Setval('sportid', 3);
                    break;
                case 'wq':
                    $this->Setval('sportid', 1);
                    break;
                default;
                    $this->Setval('sportid', 16);
            }

            if ($buyModel->settle_status==2  &&  intval($buyModel->prize_money)==0){      $this->Setval('lose', $buyModel->money );   }



            $tmpArray = json_decode($buyModel->codes,true);
            if ( is_array($tmpArray) &&  isset($tmpArray['0']) ){
                $tmpArray = $tmpArray['0'];
                if ( isset($tmpArray['bettingTime']) &&  $tmpArray['bettingTime']!='' &&  $buyModel->settle_status==2){
                    $this->Setval('thisdate',$tmpArray['bettingTime']);
                    $this->Setval('balltime',$tmpArray['bettingTime']);
                }
                if ( isset($tmpArray['odds']) &&  $tmpArray['odds']!='' ){  $this->Setval('odds',$tmpArray['curpl']); }
                if ( isset($tmpArray['match_id']) &&  $tmpArray['match_id']!='' ){  $this->Setval('matchid',$tmpArray['match_id']); }
            }
        }


    }


    //设置返回参数值
    private  function   Setval($key,$val=''){
        if  (isset($this->retDataArray[$key])){
            $this->retDataArray[$key] = $val;
        }
    }

    //返回数据
    private   function  makeret($status=1,$msg='success',$datas=[]){
        return [
            'status'=>$status,
            'msg'=>$msg,
            'datas'=>$datas
        ];
    }

    /*
     * ballid	场次id号
        balltime	结算时间
        curpl	赔率
        ds	单双
        dxc	大小个数
        isbk	无用
        iscancel	是否取消
        isdanger	是否危险球
        isjs	是否结算,1表示结算,0表示未结算
        lose	输的钱
        matchid	联赛id号
        moneyrate	会员使用的货币比率
        orderid	注单号
        redcard	红牌
        result	赛事结果
        rowguid	唯一性id号，自动生成，orderid为每个平台的id号，多平台可能不唯一
        rqc	让球个数
        rqteam	让球队伍,比如主队让球H,客对让球A
        sportid	下注球类id号
        tballtime	走地时间
        thisdate	结算日期
        truewin	0.5赢半，1全赢，-0.5输半，-1输，0和
        tzip	投注ip,IBC不提供，为空就行
        tzmoney	下注金额
        tzteam	下注队伍，比如主队H,客队A
        tztype	下注类别
        updatetime	下注时间
        username	会员名
        win	赢的钱
        zdbf	走地比分
        content	下注中文简体内容,需要用UrlDecode解码
        vendorid	注单更新顺序号
        validamount	有效投注额
     *
     * */
    private  function  getDefaultBackData(){
        $array = [
            "ballid"=>'',
            "balltime"=>'',
            "curpl"=>'',
            "ds"=>'',
            "dxc"=>'',
            "isbk"=>'',
            "iscancel"=>'',
            "isdanger"=>'',
            "isjs"=>'',
            "lose"=>'',
            "matchid"=>'',
            "moneyrate"=>'',
            "orderid"=>'',
            "redcard"=>'',
            "result"=>'',
            "rowguid"=>'',
            "rqc"=>'',
            "rqteam"=>'',
            "sportid"=>'',
            "tballtime"=>'',
            "thisdate"=>'',
            "truewin"=>'',
            "tzip"=>'',
            "tzmoney"=>'',
            "tzteam"=>'',
            "tztype"=>'',
            "updatetime"=>'',
            "username"=>'',
            "win"=>'',
            "zdbf"=>'',
            "content"=>'',
            "vendorid"=>'',
            "validamount"=>'',
        ];
        return $array ;
    }

}