<?php
/**
 * Created by PhpStorm.
 * User: Jonlin
 * Date: 2019/4/9
 * Time: 9:18
 */
namespace App\Models;
use Illuminate\Support\Facades\DB;
class SportsNoteList extends BaseModel {
    protected $table = "money_buy_simplex";
    public $timestamps = false;

    function getinfo($list = 10, $page, $where = '',$type)
    {
        $data = $this
            ->join('money_details','money_details.info_identity','=','money_buy_simplex.info_identity')
            ->join('st_'.$type.'_competition','st_'.$type.'_competition.match_id','=','money_buy_simplex.match_id')
            ->join('st_'.$type.'_league','st_'.$type.'_competition.lg_id','=','st_'.$type.'_league.lg_id')
            ->select('money_buy_simplex.id','money_buy_simplex.account_name','money_buy_simplex.account_identity','money_buy_simplex.order_id','money_buy_simplex.money','money_buy_simplex.prize_money','money_buy_simplex.status','money_buy_simplex.money_time','money_buy_simplex.settle_status','money_buy_simplex.gain_money','money_buy_simplex.game_code','money_buy_simplex.match_id','money_buy_simplex.game_status','st_'.$type.'_competition.home_team','st_'.$type.'_competition.guest_team','st_'.$type.'_competition.match_date','st_'.$type.'_competition.match_time','st_'.$type.'_competition.lg_id','st_'.$type.'_competition.status as match_status','money_details.money_cash','money_buy_simplex.batch_id','st_'.$type.'_league.name_chinese','st_'.$type.'_league.last_time')
            ->where($where)
            ->orderby('money_buy_simplex.money_time','desc')
            ->paginate ($list);
        if (!$data < 0) {
            return -2021052003; //
        }

        //反水
        $water_return_money = \App\Models\Money_details::where('trade_type','7')->get();

        //注单作废
        $invalid_money = \App\Models\Money_details::where('trade_type','3')->get();

        //订单投注的玩法
        $content = \App\Models\MoneyBuyMatch::where('bet_type','1')->get();
        for($d=0;$d<count($content);$d++){
            if($content[$d]->result==0){
                $content[$d]->result = '未处理';
            }
            if($content[$d]->result==1){
                $content[$d]->result = '赢';
            }
            if($content[$d]->result==-1){
                $content[$d]->result = '输';
            }
            if($content[$d]->result==2){
                $content[$d]->result = '平';
            }
            if($content[$d]->result==3){
                $content[$d]->result = '赢半平半';
            }
            if($content[$d]->result==4){
                $content[$d]->result = '输半平半';
            }
        }

        //玩法
        $result = \App\Models\Matchcode::where('game_code','zq')->get();
        //$result = \App\Model\Matchcode::get();


        for($i=0;$i<count($data);$i++){
            if($data[$i]->status==1 && $data[$i]->game_status==0){
                $data[$i]->order_status = '投注'.'<br><a class="layui-btn layui-btn-sm invalid" lay-event="detail" pid="id" uri="/admin/SoccerNoteList/invalid/?id=" href="javascript:invalid(\'/admin/SoccerNoteList/invalid/?id='.$data[$i]->id.'\');"> 作废 </a>';
            }else if($data[$i]->status==1 && $data[$i]->game_status!=0){
                $data[$i]->order_status = '投注';
            }else if($data[$i]->status==2){
                $data[$i]->order_status = '作废'.'<br><a class="layui-btn layui-btn-sm audit" lay-event="detail" pid="id" uri="" href="javascript:void(0)" style="background-color: grey;"> 已作废 </a>';
            }else if($data[$i]->status==3){
                $data[$i]->order_status = '撤单'.'<br><a class="layui-btn layui-btn-sm audit" lay-event="detail" pid="id" uri="" href="javascript:void(0)" style="background-color: grey;"> 已撤单 </a>';
            }
            if($data[$i]->settle_status==1){
                $data[$i]->settle_status = '未结算';
            }else if($data[$i]->settle_status==2){
                $data[$i]->settle_status = '已结算';
            }
            if($data[$i]->game_status==0){
                $data[$i]->game_status = '未处理';
            }else{
                $data[$i]->game_status = '已处理';
            }

            $data[$i]->match_id_order = '<span>'.$data[$i]->match_id.'</span> (<span>'.$data[$i]->account_name.'</span>)<br>'.$data[$i]->order_id;
            $data[$i]->league = $data[$i]->name_chinese;

            $data[$i]->home_guest = $data[$i]->home_team.' VS '.$data[$i]->guest_team;
            $data[$i]->time = '开赛：'.$data[$i]->match_date.' '.$data[$i]->match_time;
            $data[$i]->money_match_time = '下注：'.$data[$i]->money_time.'<br>'.$data[$i]->time;

            //反水
            $data[$i]->water_return = -1;
            for($a=0;$a<count($water_return_money);$a++){
                if($data[$i]->order_id == $water_return_money[$a]->trade_id){
                    if($data[$i]->status == 2 || $data[$i]->status == 3){
                        $data[$i]->water_return = 0;
                    }else{
                        $data[$i]->water_return = $water_return_money[$a]->money;
                    }
                    //用户投注后账户金额
                    $data[$i]->frozen_cash = $water_return_money[$a]->money_cash;
                }
            }
            if($data[$i]->water_return == -1){
                $data[$i]->water_return = 0;
                //用户投注后账户金额
                $data[$i]->frozen_cash = $data[$i]->money_cash;
            }
            //此时结果金额
            $data[$i]->result = $data[$i]->water_return+$data[$i]->gain_money;
            //用户投注前账户金额
            $data[$i]->available_cash = $data[$i]->money_cash+$data[$i]->money;
            //投注前后账户金额
            $data[$i]->account_money = $data[$i]->available_cash.'<br><span>'.$data[$i]->money.'</span><br>'.$data[$i]->frozen_cash;
            if($data[$i]->status == 2 || $data[$i]->status == 3){
                for($b=0;$b<count($invalid_money);$b++){
                    if($data[$i]->order_id == $invalid_money[$b]->trade_id){
                        $data[$i]->available_cash = $invalid_money[$b]->money_cash;
                        $data[$i]->account_money = $data[$i]->available_cash;
                    }
                }
            }

            for($b=0;$b<count($content);$b++){
                $data[$i]->str = 0;
                $data[$i]->str1 = 0;
                $data[$i]->team = '';
                if(!empty($content[$b]->odds_code)){
                    if(!empty(stristr($content[$b]->odds_code,'home'))){
                        $data[$i]->team = $data[$i]->home_team;
                    }
                    if(!empty(stristr($content[$b]->odds_code,'guest'))){
                        $data[$i]->team = $data[$i]->guest_team;
                    }

                    if($content[$b]->p_code == 'half_full'){
                        $string = str_replace('home',$data[$i]->home_team,$content[$b]->odds_code);
                        $string = str_replace('guest',$data[$i]->guest_team,$string);
                        $string = str_replace('dogfall','和局',$string);
                        $string = explode('_',$string);
                        $data[$i]->team = $string['2'].'/'.$string['3'];
                    }
                }
                if($data[$i]->match_id == $content[$b]->match_id && $data[$i]->batch_id == $content[$b]->batch_id){
                    if($content[$b]->p_code == 'first_last_ball' || $content[$b]->p_code == 'half_full' || $content[$b]->p_code == 'capot' || $content[$b]->p_code == 'two_sides' || $content[$b]->p_code == 'total_goal'){
                        $content[$b]->condition = '';
                    }

                    $data[$i]->game_status = $data[$i]->game_status;

                    //投注详情
                    if(!empty($data[$i]->match_time)){
                        $data[$i]->content1 = '['.$data[$i]->match_time.']';
                        //$data[$i]->match_time = $data[$i]->match_time;
                    }else{
                        if(!empty($data[$i]->last_time)){
                            $datetime = explode(' ',$data[$i]->last_time);
                            //$data[$i]->match_time = $datetime[1];
                            $data[$i]->content1 = '['.$datetime[1].']';
                        }
                    }
                    for($c=0;$c<count($result);$c++){
                        if($content[$b]->odds_code == $result[$c]->odds_code){
                            $data[$i]->str1 = 1;
                            if($content[$b]->result=='未处理'){
                                $data[$i]->content = $data[$i]->content.$result[$c]->odds_name.'<br>'.$content[$b]->condition.' '.$data[$i]->team.'@'.$content[$b]->odds.' ￥'.$content[$b]->bet_money.'<br>';
                            }else{
                                $data[$i]->content = $data[$i]->content.$result[$c]->odds_name.'<br>'.$content[$b]->condition.' '.$data[$i]->team.'@'.$content[$b]->odds.' ￥'.$content[$b]->bet_money.'<br><span>'.$content[$b]->result.'</span><br>';
                            }

                        }
                    }
                    if($data[$i]->str1 == 0){
                        if($content[$b]->result=='未处理'){
                            $data[$i]->content = $data[$i]->content.$content[$b]->odds_code.'<br>'.$content[$b]->condition.' '.$data[$i]->team.'@'.$content[$b]->odds.' ￥'.$content[$b]->bet_money.'<br>';
                        }else{
                            $data[$i]->content = $data[$i]->content.$content[$b]->odds_code.'<br>'.$content[$b]->condition.' '.$data[$i]->team.'@'.$content[$b]->odds.' ￥'.$content[$b]->bet_money.'<br><span>'.$content[$b]->result.'</span><br>';
                        }

                    }
                }
            }

            if($data[$i]->status == 1){
                if($data[$i]->match_status == 0 || $data[$i]->match_status == 1){
                    $data[$i]->game_status = $data[$i]->game_status.'<br><a class="layui-btn layui-btn-sm audit" lay-event="detail" pid="id" uri="" href="javascript:void(0)" style="background-color: grey;"> 结算 </a>';
                }
                if($data[$i]->match_status == 2 || $data[$i]->match_status == 5){
                    $data[$i]->game_status = $data[$i]->game_status.'<br><a class="layui-btn layui-btn-sm settlement" lay-event="detail" pid="id" uri="/admin/SoccerNoteList/settlement/?id=" href="javascript:settlement(\'/admin/SoccerNoteList/settlement/?id='.$data[$i]->id.'\');"> 结算 </a>';
                }
                if($data[$i]->match_status == 3){
                    $data[$i]->game_status = $data[$i]->game_status.'<br><a class="layui-btn layui-btn-sm resettlement" lay-event="detail" pid="id" uri="/admin/SoccerNoteList/resettlement/?id=" href="javascript:resettlement(\'/admin/SoccerNoteList/resettlement/?id='.$data[$i]->id.'\');"> 重新结算 </a>';
                }
            }else{
                $data[$i]->game_status = $data[$i]->game_status.'<br><a class="layui-btn layui-btn-sm audit" lay-event="detail" pid="id" uri="" href="javascript:void(0)" style="background-color: grey;"> 结算 </a>';
            }

            $data[$i]->content = $data[$i]->content1.$data[$i]->content;

        }
        return $data->toArray();

    }

    //投注金额汇总统计
    function MoneyCount($where = '',$type) {
        $data = array();
//        $data['all_money'] = $this->sum('money');
//        $data['all_prize_money'] = $this->sum('prize_money');
//        $data['alraedy_prize_money'] = $this->where('settle_status','2')->sum('gain_money');
        $data['all_money'] = 0;
        $data['all_prize_money'] = 0;
        $data['alraedy_prize_money'] = 0;
        $all_money = $this->join('money_details','money_details.info_identity','=','money_buy_simplex.info_identity')->join('st_'.$type.'_competition','st_'.$type.'_competition.match_id','=','money_buy_simplex.match_id')->join('st_'.$type.'_league','st_'.$type.'_competition.lg_id','=','st_'.$type.'_league.lg_id')->select('money_buy_simplex.money','money_buy_simplex.prize_money','money_buy_simplex.gain_money')->where('money_buy_simplex.status','1')->get();
        for($a=0;$a<count($all_money);$a++){
            $data['all_money'] = $data['all_money']+$all_money[$a]->money;
        }
        $all_prize_money = $this->join('money_details','money_details.info_identity','=','money_buy_simplex.info_identity')->join('st_'.$type.'_competition','st_'.$type.'_competition.match_id','=','money_buy_simplex.match_id')->join('st_'.$type.'_league','st_'.$type.'_competition.lg_id','=','st_'.$type.'_league.lg_id')->select('money_buy_simplex.money','money_buy_simplex.prize_money','money_buy_simplex.gain_money')->where('money_buy_simplex.status','1')->get();
        for($a=0;$a<count($all_prize_money);$a++){
            $data['all_prize_money'] = $data['all_money']+$all_prize_money[$a]->prize_money;
        }
        $alraedy_prize_money = $this->join('money_details','money_details.info_identity','=','money_buy_simplex.info_identity')->join('st_'.$type.'_competition','st_'.$type.'_competition.match_id','=','money_buy_simplex.match_id')->join('st_'.$type.'_league','st_'.$type.'_competition.lg_id','=','st_'.$type.'_league.lg_id')->select('money_buy_simplex.money','money_buy_simplex.prize_money','money_buy_simplex.gain_money')->where('money_buy_simplex.settle_status','2')->where('money_buy_simplex.status','1')->get();
        for($a=0;$a<count($alraedy_prize_money);$a++){
            $data['alraedy_prize_money'] = $data['all_money']+$alraedy_prize_money[$a]->gain_money;
        }

        if (!empty($where) && is_array($where)) {
            $data['all_money'] = 0;
            $data['all_prize_money'] = 0;
            $data['alraedy_prize_money'] = 0;
            //$data['all_money'] = $this->where($where)->sum('money');
//            $data['all_prize_money'] = $this->where($where)->sum('prize_money');
//            $data['alraedy_prize_money'] = $this->where($where)->where('settle_status','2')->sum('gain_money');
            $all_money = $this->join('money_details','money_details.info_identity','=','money_buy_simplex.info_identity')->join('st_zq_competition','st_zq_competition.match_id','=','money_buy_simplex.match_id')->join('st_zq_league','st_zq_competition.lg_id','=','st_zq_league.lg_id')->select('money_buy_simplex.money','money_buy_simplex.prize_money','money_buy_simplex.gain_money')->where($where)->where('money_buy_simplex.status','1')->get();
            for($a=0;$a<count($all_money);$a++){
                $data['all_money'] = $data['all_money']+$all_money[$a]->money;
            }
            $all_prize_money = $this->join('money_details','money_details.info_identity','=','money_buy_simplex.info_identity')->join('st_'.$type.'_competition','st_'.$type.'_competition.match_id','=','money_buy_simplex.match_id')->join('st_'.$type.'_league','st_'.$type.'_competition.lg_id','=','st_'.$type.'_league.lg_id')->select('money_buy_simplex.money','money_buy_simplex.prize_money','money_buy_simplex.gain_money')->where($where)->where('money_buy_simplex.status','1')->get();
            for($a=0;$a<count($all_prize_money);$a++){
                $data['all_prize_money'] = $data['all_prize_money']+$all_prize_money[$a]->prize_money;
            }
            $alraedy_prize_money = $this->join('money_details','money_details.info_identity','=','money_buy_simplex.info_identity')->join('st_'.$type.'_competition','st_'.$type.'_competition.match_id','=','money_buy_simplex.match_id')->join('st_'.$type.'_league','st_'.$type.'_competition.lg_id','=','st_'.$type.'_league.lg_id')->select('money_buy_simplex.money','money_buy_simplex.prize_money','money_buy_simplex.gain_money')->where('money_buy_simplex.settle_status','2')->where($where)->where('money_buy_simplex.status','1')->get();
            for($a=0;$a<count($alraedy_prize_money);$a++){
                $data['alraedy_prize_money'] = $data['alraedy_prize_money']+$alraedy_prize_money[$a]->gain_money;
            }
        }

        return $data;
    }

    //修改赛事下的所有单式下注 3199308
    function updatesimplex($ssid,$code){
        $newapp = new \App\Models\MoneyBuyMatch();
        $data = $newapp->allsimplexorder($ssid,$code);
        if($data){
            for ($i=0; $i < count($data); $i++) { 
                $the = array(
                    'status' => 2,
                    'game_status' => 3,
                    'game_code' =>$code,
                );
                $res = $this->where('order_id',$data[$i]['order_id'])->update($the);
            }
            if($res<0){
                return -7012000302; //操作失败
            }
            return $res;
        }else{
            return -111;
        }
    }



}