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

    function getinfo($list = 10, $page, $where = '')
    {
        if (is_array ($where) && count ($where) > 0) {
            $data = $this
                ->join('money_details','money_details.info_identity','=','money_buy_str.info_identity')
                ->select('money_buy_str.id','money_buy_str.account_name','money_buy_str.account_identity','money_buy_str.order_id','money_buy_str.str_type','money_buy_str.money','money_buy_str.prize_money','money_buy_str.status','money_buy_str.money_time','money_buy_str.settle_status','money_buy_str.game_status','money_buy_str.gain_money','money_details.money_cash','money_buy_str.batch_id')
                ->where($where)
                ->orderby('money_buy_str.money_time','desc')
                ->paginate ($list);
        } else {
            $data = $this
                ->join('money_details','money_details.info_identity','=','money_buy_str.info_identity')
                ->select('money_buy_str.id','money_buy_str.account_name','money_buy_str.account_identity','money_buy_str.order_id','money_buy_str.str_type','money_buy_str.money','money_buy_str.prize_money','money_buy_str.status','money_buy_str.money_time','money_buy_str.settle_status','money_buy_str.game_status','money_buy_str.gain_money','money_details.money_cash','money_buy_str.batch_id')
                ->orderby('money_buy_str.money_time','desc')
                ->paginate ($list);
        }
        if (!$data < 0) {
            return -2021052003; //
        }

        //足球赛事
        $zqmatch =  \App\Models\SportsSoccer::where('status','<>','4')->where('status','<>','6')->get();
        //篮球赛事
        $lqmatch =  \App\Models\SportsBasket::where('status','<>','4')->where('status','<>','6')->get();
        //网球赛事
        $wqmatch =  \App\Models\SportsTennis::where('status','<>','4')->where('status','<>','6')->get();
        //棒球赛事
        $bqmatch =  \App\Models\SportsBase::where('status','<>','4')->where('status','<>','6')->get();

        //反水
        $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','2')->get();
        for($c=0;$c<count($content);$c++) {
            if ($content[$c]->game_code == 'zq') {
                for ($d = 0; $d < count($zqmatch); $d++) {
                    if ($content[$c]->match_id == $zqmatch[$d]->id) {
                        $content[$c]->match_status = $zqmatch[$d]->status;
                    }
                }
            }
            if ($content[$c]->game_code == 'lq') {
                for ($d = 0; $d < count($lqmatch); $d++) {
                    if ($content[$c]->match_id == $zqmatch[$d]->id) {
                        $content[$c]->match_status = $zqmatch[$d]->status;
                    }
                }
            }
            if ($content[$c]->game_code == 'wq') {
                for ($d = 0; $d < count($wqmatch); $d++) {
                    if ($content[$c]->match_id == $zqmatch[$d]->id) {
                        $content[$c]->match_status = $zqmatch[$d]->status;
                    }
                }
            }
            if ($content[$c]->game_code == 'bq') {
                for ($d = 0; $d < count($bqmatch); $d++) {
                    if ($content[$c]->match_id == $zqmatch[$d]->id) {
                        $content[$c]->match_status = $zqmatch[$d]->status;
                    }
                }
            }
        }

        for($i=0;$i<count($data);$i++){
            if($data[$i]->status==1 && $data[$i]->settle_status==1){
                $data[$i]->order_status = '投注'.'<br><a class="layui-btn layui-btn-sm invalid" lay-event="detail" pid="id" uri="/admin/SoccerStringNoteList/invalid/?id=" href="javascript:invalid(\'/admin/SoccerStringNoteList/invalid/?id='.$data[$i]->id.'\');"> 作废 </a>';
            }else if($data[$i]->status==1 && $data[$i]->settle_status==2){
                $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 if($data[$i]->game_status==1){
                $data[$i]->game_status = '中奖';
            }else if($data[$i]->game_status==2){
                $data[$i]->game_status = '未中奖';
            }

            //$str_type = str_replace('*', '串', $data[$i]->str_type);
            $mun = explode('串',$data[$i]->str_type);
            $data[$i]->detail_content = $data[$i]->settle_status.'：'.$mun[0].' [<a class="detail" lay-event="detail" pid="id" uri="/admin/SoccerStringNoteList/detail/?id=" href="/admin/SoccerStringNoteList/detail/?id='.$data[$i]->id.'"> 详情 </a>]';
            $data[$i]->str_type = $data[$i]->str_type.'<br>(<span>'.$data[$i]->account_name.'</span>)';

            //反水
            $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]->money_time = '下注：'.$data[$i]->money_time;
            //用户投注前账户金额
            $data[$i]->available_cash = $data[$i]->money_cash+$data[$i]->money;
            $data[$i]->account_money = $data[$i]->available_cash.' <span>'.$data[$i]->money.' </span> '.$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;
                    }
                }
            }


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

            if($data[$i]->status == 1){
                if($data[$i]->settle_status == '未结算'){
                    $match_status = array();
                    for($j=0;$j<count($content);$j++){
                        if($data[$i]->batch_id == $content[$j]->batch_id){
                            $match_status[] = $content[$j]->match_status;
                        }
                    }
                    if(in_array('0',$match_status) || in_array(1,$match_status)){
                        $data[$i]->settle_status = $data[$i]->settle_status.'<br><a class="layui-btn layui-btn-sm resettlement settlement" lay-event="detail" pid="id" lay-filter = "resettlement" uri="" href="javascript:void(0)" style="background-color: grey;"> 结算 </a>';
                    }else{
                        $data[$i]->settle_status = $data[$i]->settle_status.'<br><a class="layui-btn layui-btn-sm settlement" lay-event="detail" pid="id" lay-filter = "resettlement" uri="/admin/SoccerStringNoteList/settlement/?id=" href="javascript:settlement(\'/admin/SoccerStringNoteList/settlement/?id='.$data[$i]->id.'\');"> 结算 </a>';
                    }

                }else if($data[$i]->settle_status == '已结算'){
                    $data[$i]->settle_status = $data[$i]->settle_status.'<br><a class="layui-btn layui-btn-sm settlement" lay-event="detail" pid="id" lay-filter = "resettlement" uri="/admin/SoccerStringNoteList/resettlement/?id=" href="javascript:resettlement(\'/admin/SoccerStringNoteList/resettlement/?id='.$data[$i]->id.'\');">重新结算 </a>';
                }
            }else{
                $data[$i]->settle_status = $data[$i]->settle_status.'<br><a class="layui-btn layui-btn-sm resettlement settlement" lay-event="detail" pid="id" lay-filter = "resettlement" uri="" href="javascript:void(0)" style="background-color: grey;"> 结算 </a>';
            }

        }
        return $data->toArray();

    }

    //投注金额汇总统计
    function MoneyCount($where = '') {
        $data = array();
        $data['all_money'] = $this->where('status','1')->sum('money');
        $data['all_prize_money'] = $this->where('status','1')->sum('prize_money');
        $data['alraedy_prize_money'] = $this->where('settle_status','2')->where('status','1')->sum('gain_money');
        if (!empty($where) && is_array($where)) {
            $data['all_money'] = $this->where($where)->where('status','1')->sum('money');
            $data['all_prize_money'] = $this->where($where)->where('status','1')->sum('prize_money');
            $data['alraedy_prize_money'] = $this->where($where)->where('settle_status','2')->where('status','1')->sum('gain_money');
        }

        return $data;
    }

    //获取串关注单详情信息
    function getdetailinfo($id) {
        $res = $this->where('id',$id)->first();
        $data = \App\Models\MoneyBuyMatch::where('batch_id',$res->batch_id)->where('bet_type','2')->get();
        $game_type = \App\Models\StGameType::get();
        $odds_code = \App\Models\Matchcode::get();
        for($i=0;$i<count($data);$i++){

            //当父级玩法代码为first_last_ball/half_full/capot/two_sides时，不显示赔率条件
            if($data[$i]->p_code == 'first_last_ball' || $data[$i]->p_code == 'half_full' || $data[$i]->p_code == 'capot' || $data[$i]->p_code == 'two_sides' || $data[$i]->p_code == 'total_goal'){
                $data[$i]->condition = '';
            }
            
            $league = DB::table('st_'.$data[$i]->game_code.'_league')->join('st_'.$data[$i]->game_code.'_competition','st_'.$data[$i]->game_code.'_competition.lg_id','=','st_'.$data[$i]->game_code.'_league.lg_id')->where('st_'.$data[$i]->game_code.'_league.lg_id',$data[$i]->lg_id)->where('st_'.$data[$i]->game_code.'_competition.match_id',$data[$i]->match_id)->first();
            if(!empty($league)){
                //联赛名称
                $data[$i]->league = $league->name_chinese;
                //赛事开赛时间
                $data[$i]->time = $league->match_date.' '.$league->match_time;
            }

            if(!empty(strstr($data[$i]->odds_code,'home',true))){
                $data[$i]->team = $data[$i]->home_team;
            }
            if(!empty(strstr($data[$i]->odds_code,'guest',true))){
                $data[$i]->team = $data[$i]->guest_team;
            }

            if($data[$i]->p_code == 'half_full'){
                $string = str_replace('home',$data[$i]->home_team,$data[$i]->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'];
            }

            for($j=0;$j<count($odds_code);$j++){
                if($data[$i]->game_code == $odds_code[$j]->game_code && $data[$i]->odds_code == $odds_code[$j]->odds_code){
                    $data[$i]->odds_code = $odds_code[$j]->odds_name;
                }
                if($data[$i]->game_code == $odds_code[$j]->game_code && $data[$i]->p_code == $odds_code[$j]->odds_code){
                    $data[$i]->p_code = $odds_code[$j]->odds_name;
                }
            }

            $data[$i]->home_guest = $data[$i]->home_team.' VS '.$data[$i]->guest_team;
            $data[$i]->content = $data[$i]->p_code.'<br><span>'.$data[$i]->odds_code.$data[$i]->condition.'<br>'.$data[$i]->team.' @'.$data[$i]->odds.'</span>';

            for($j=0;$j<count($game_type);$j++){
                if($data[$i]->game_code == $game_type[$j]->game_code){
                    $data[$i]->game_type = $game_type[$j]->game_name;
                }
            }

            if($league->status == 0){
                $data[$i]->status = '未开始';
            }else if($league->status == 1){
                $data[$i]->status = '正在进行';
            }else if($league->status == 2){
                $data[$i]->status = '已结束';
            }else if($league->status == 3){
                $data[$i]->status = '已结算';
            }else if($league->status == 4){
                $data[$i]->status = '已作废';
            }else if($league->status == 5){
                $data[$i]->status = '未结算';
            }else if($league->status == 6){
                $data[$i]->status = '已取消';
            }

            if($data[$i]->result == -1){
                $data[$i]->result = '输';
            }else if($data[$i]->result == 0){
                $data[$i]->result = '未处理';
            }else if($data[$i]->result == 1){
                $data[$i]->result = '赢';
            }else if($data[$i]->result == 2){
                $data[$i]->result = '平';
            }else if($data[$i]->result == 3){
                $data[$i]->result = '赢半平半';
            }else if($data[$i]->result == 4){
                $data[$i]->result = '输半平半';
            }
        }
        return $data->toArray();
    }

    //串式撤单修改状态
    function updatestatus($data){
        if($data){
            for ($i=0; $i < count($data); $i++) { 
                $the = array(
                    'status' => 2,
                );
                $res = $this->where('order_id',$data[$i])->update($the);
            }
            if (!$res) {
                return -4010000102; //更新失败
            }
            return $res;
        }
    }


}