<?php
/**
 * Created by PhpStorm.
 * User: scstf
 * Date: 2018/9/28
 * Time: 20:05
 */

namespace App\Models;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;

class MoneyBuyMatch extends Model
{
    protected $table = 'money_buy_match';
    public $timestamps = false;

    public function getByAttrs($where, $orderArray = [])
    {
        if (empty($orderArray)) {
            $datas = $this->where($where)->get();
        } else {
            $datas = $this->where($where)->orderby($orderArray['orderby'], $orderArray['order'])->get();
        }
        return $datas;
    }

    //赛事下所有投注状态的单式下单
    function simsettleorder($ssid='', $code=''){
        $where = array();
        $where[] = array('money_buy_match.match_id', '=', $ssid);
        $where[] = array('money_buy_match.bet_type', '=', 1);
        $where[] = array('money_buy_match.game_code', '=', $code);
        $where[] = array('money_buy_simplex.status', '=', 1);

        $select = ['money_buy_simplex.order_id', 'money_buy_simplex.money', 'money_buy_simplex.account_identity', 'money_buy_simplex.info_identity', 'money_buy_simplex.match_id','st_odds_code.odds_name'];
        $data = $this->join('money_buy_simplex', 'money_buy_match.order_id', '=', 'money_buy_simplex.order_id')->join('st_odds_code', 'money_buy_match.odds_code', '=', 'st_odds_code.odds_code')->select($select)->where($where)->get()->toArray();

        return $data;

    }

    //赛事下所有投注状态下的串式下单
    function strsettleorder($ssid=''){
        $where = array();
        $where[] = array('money_buy_match.match_id', '=', $ssid);
        $where[] = array('money_buy_match.bet_type', '=', 2);
        $where[] = array('money_buy_str.status', '=', 1);

        $select = ['money_buy_str.order_id'];
        $data = $this->join('money_buy_str', 'money_buy_match.order_id', '=', 'money_buy_str.order_id')->join('st_odds_code', 'money_buy_match.odds_code', '=', 'st_odds_code.odds_code')->select($select)->where($where)->distinct('money_buy_match.order_id')->get()->toArray();

        return $data;
    }

    //赛事下所有下注单式下单
    function allsimplexorder($ssid='', $code='',$select=[],$match_ids = [])
    {

        //单式下单
        $where = array();
        $where[] = array('money_buy_match.match_id', '=', $ssid);
        $where[] = array('money_buy_match.bet_type', '=', 1);
        $where[] = array('money_buy_match.game_code', '=', $code);

        if(empty($select)){
            //一个赛事所有注单
            $select = ['money_buy_simplex.order_id', 'money_buy_simplex.money', 'money_buy_simplex.account_identity', 'money_buy_simplex.info_identity', 'money_buy_simplex.match_id'];
            $data = $this->join('money_buy_simplex', 'money_buy_match.order_id', '=', 'money_buy_simplex.order_id')->select($select)->where($where)->get()->toArray();
        }else if(!empty($match_ids) and !empty($select)){
            //多赛事所有注单
            $typeWhere = [
                ['money_buy_match.bet_type', '=', 1],
                ['money_buy_match.game_code', '=', $code],
                ['money_buy_simplex.status', '=', 1],
            ];
            $data = $this->join('money_buy_simplex', 'money_buy_match.order_id', '=', 'money_buy_simplex.order_id')->select($select)->whereIn('money_buy_match.match_id',$match_ids)->where($typeWhere)->get();
        }else{
            $data = $this->join('money_buy_simplex', 'money_buy_match.order_id', '=', 'money_buy_simplex.order_id')->join('st_odds_code', 'money_buy_match.odds_code', '=', 'st_odds_code.odds_code')->select($select)->where($where)->get();
        }
        return $data;
    }

    //赛事下所有下注串式下单
    function allstrorder($ssid='',$game_code = '',$select=[])
    {
        //串式下单
        $where = array();
        $where[] = array('money_buy_match.match_id', '=', $ssid);
        $where[] = array('money_buy_match.bet_type', '=', 2);
        if(!empty($game_code)){
            $where[] = array('money_buy_match.game_code', '=', $game_code);
        }
        if(empty($select)){
            $select = ['money_buy_str.order_id'];
            $data = $this->join('money_buy_str', 'money_buy_match.order_id', '=', 'money_buy_str.order_id')->select($select)->where($where)->distinct('money_buy_match.order_id')->get()->toArray();
        }else{
            $data = $this->join('money_buy_str', 'money_buy_match.order_id', '=', 'money_buy_str.order_id')->join('st_odds_code', 'money_buy_match.odds_code', '=', 'st_odds_code.odds_code')->select($select)->where($where)->distinct('money_buy_match.order_id')->get();
        }
        return $data;
    }

    //修改串式下注状态
    function updatast($match_id)
    {
        $the = array(
            'updated_at' => date('Y-m-d H:i:s'),
            'result' => 2,
        );
        $where = array(
            'match_id' => $match_id,
            'bet_type' => 2,
        );

        $res = $this->where($where)->update($the);

        if (!$res) {
            return -4010000102; //更新失败
        }
        return $res;
    }

    //某个赛事的单式和串式条数统计
    public function countByMatch($matchID, $game_code)
    {
        $matchID = intval($matchID);

        $sqla = "select count(id) as mcount from  money_buy_match where match_id=$matchID and game_code='$game_code' and  bet_type=1 and   order_id in( select order_id from  money_buy_simplex where match_id=$matchID and  game_code='$game_code' and is_manual=0 )";
        $sqlb = "select count(id) as mcount from  money_buy_match where match_id=$matchID and game_code='$game_code' and  bet_type=2 ";

        $reta = DB::select($sqla);
        $retb = DB::select($sqlb);

        $ret1 = $ret2 = 0;
        if ($reta && isset($reta['0']->mcount)) {
            $ret1 = intval($reta['0']->mcount);
        }

        if ($retb && isset($retb['0']->mcount)) {
            $ret2 = intval($retb['0']->mcount);
        }

        return ['bet1' => $ret1, 'bet2' => $ret2, 'count' => $ret1 + $ret2];
    }


}