<?php
/**
 * Created by PhpStorm.
 * User: Jonlin
 * Date: 2019/4/9
 * Time: 9:18
 */

namespace App\Models;

use Illuminate\Support\Facades\DB;
use App\Lib\Biz\Sport\Common as commonFunction;
use App\Models\MoneyBuyMatchb;
use App\Http\Response\Response;
use App\Http\Model\StZqResult as StZqResultModel;

use App\Http\Model\StZqMatch as ZqMatchModel;
use App\Http\Model\StLqMatch as LqMatchModel;
use App\Http\Model\StWqMatch as WqMatchModel;
use App\Http\Model\StBqMatch as BqMatchModel;


class SportsNoteList extends BaseModel
{
    protected $table = "money_buy_simplex";
    public $timestamps = false;

    public function matchdatas()
    {
        return $this->hasMany(MoneyBuyMatch::class, 'order_id', 'order_id');
    }

    /**
     * 获取单式注单 数据
     * 测试 使用
     */
    function getinfo($list = 10, $page, $where = '', $type){
        $orderData = $this
            ->join('money_details', 'money_details.info_identity', '=', 'money_buy_simplex.info_identity')
            ->join('st_' . $type . '_competition', 'st_' . $type . '_competition.id', '=', 'money_buy_simplex.match_id')
            ->join('money_buy_match','money_buy_match.order_id','=','money_buy_simplex.order_id')
            ->join('st_' . $type . '_league', 'money_buy_simplex.lg_id', '=', 'st_' . $type . '_league.id')
            ->select('money_buy_simplex.id', 'money_buy_simplex.account_name','money_buy_match.is_rolling','money_buy_match.roll_time','money_buy_match.home_score','money_buy_match.guest_score', 'money_buy_simplex.is_champion', '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','money_buy_simplex.use_mark', 'st_' . $type . '_competition.home_team', 'st_' . $type . '_competition.guest_team', 'st_' . $type . '_competition.match_date', 'st_' . $type . '_competition.match_time', 'money_buy_simplex.lg_id', 'st_' . $type . '_competition.status as match_status', 'money_details.money_cash', 'money_buy_simplex.batch_id', 'st_' . $type . '_league.name_chinese as league', 'st_' . $type . '_league.last_time')
            ->whereIn('money_buy_simplex.roll_ratify',array(0,1,-1))
            ->where($where)
            //->distinct('money_buy_match.order_id')
            ->orderby('money_buy_simplex.money_time', 'desc')
            ->paginate($list);
        if (count($orderData) < 1) {
            return -2021052003; //
        }
        //获取各球类 控制器名
        if($type == 'zq'){
            //足球赛事
            $match_c_name = 'sportsfoot';
            //足球联赛
            $leage_c_name = 'ZqChampionsLeague';
            //球类名称
            $gameName = '足球';
        }
        if($type == 'lq'){
            //篮球赛事
            $match_c_name = 'Sportsbk';
            //篮球联赛
            $leage_c_name = 'LqChampionsLeague';
            //球类名称
            $gameName = '篮球';
        }
        if($type == 'wq'){
            //网球赛事
            $match_c_name = 'Sportswq';
            //网球联赛
            $leage_c_name = 'WqChampionsLeague';
            //球类名称
            $gameName = '网球';
        }
        if($type == 'bq'){
            //棒球赛事
            $match_c_name = 'Sportsbq';
            //棒球联赛
            $leage_c_name = 'BqChampionsLeague';
            //球类名称
            $gameName = '棒球';
        }

        //获取注单 id
        $order_ids = [];
        //重组 注单数据
        foreach($orderData as $k=>$v){
            //所有注单 id
            $order_ids[] = $v->order_id;
        }

        //获取 注单 反水 金额变动记录
        $select_fs = ['trade_id','money','money_cash'];
        $water_return_money = \App\Models\Money_details::SELECT($select_fs)->where('trade_type', '7')->whereIn('trade_id',$order_ids)->get();

        //获取 注单 作废 金额变动记录
        $select_zf = ['trade_id','money','money_cash'];
        $invalid_money = \App\Models\Money_details::SELECT($select_zf)->whereIn('trade_type', [3,4,25])->whereIn('trade_id',$order_ids)->get();

        //获取注单 输赢 结果
        $select_play = ['order_id','result','matchresult','is_rolling','condition','odds','p_code','odds_code','bet_money','home_team','guest_team','home_score','guest_score'];
        $content = \App\Models\MoneyBuyMatch::SELECT($select_play)->where('bet_type', '1')->whereIn('order_id',$order_ids)->get();

        //获取 注单 玩法
        $select_code = ['odds_code','odds_name'];
        $result = \App\Models\Matchcode::SELECT($select_code)->where('game_code', $type)->get();

        //拼接注单 玩法
        foreach($content as $k=>$v){
            foreach($result as $kk=>$vv){
                if($v->odds_code == $vv->odds_code){
                    //匹配球队
                    $teamType_home = commonFunction::checkstr($vv->odds_name,'主队');
                    $teamType_guest = commonFunction::checkstr($vv->odds_name,'客队');
                    $teamName = '';
                    //主队名
                    if($teamType_home == 1){
                        $teamName = $v->home_team;
                    }else
                    //客队名
                    if($teamType_guest == 1){
                        $teamName = $v->guest_team;
                    }
                    $v->odds_name = $vv->odds_name;
                    //不是独赢玩法投注
                    if($v->p_code != 'C'){
                        //如果是滚球投注 追加当前比分
                        if($v->is_rolling == 1){
                            $v->content = $vv->odds_name . '<br>' . $v->condition .'-'. $teamName.'('.$v->home_score.'-'.$v->guest_score.')<br>@' . $v->odds . ' ￥' . $v->bet_money . '<br>';
                        }else{
                            $v->content = $vv->odds_name . '<br>' . $v->condition .'-'. $teamName.'<br>@' . $v->odds . ' ￥' . $v->bet_money . '<br>';
                        }
                    }else{
                        //独赢玩法
                        //如果不是1或2,则为和局
                        if($v->condition != 1 and $v->condition != 2 ){
                            $v->condition = '和局';
                        }else{
                            $v->condition = '';
                        }
                        //如果是滚球投注 追加当前比分
                        if($v->is_rolling == 1){
                            $v->content = $vv->odds_name . '<br>' . $v->condition .'-'. $teamName.'('.$v->home_score.'-'.$v->guest_score.')<br>@' . $v->odds . ' ￥' . $v->bet_money . '<br>';
                        }else{
                            $v->content = $vv->odds_name . '<br>' . $v->condition .'-'. $teamName.'<br>@' . $v->odds . ' ￥' . $v->bet_money . '<br>';
                        }
                    }
                }
            }
        }

        //生成最终数据
        foreach($orderData as $k=>$v){
            //普通赛事投注 赛事id+订单id
            if($v->is_champion == 0){
                $v->match_id_order = '<a href="/admin/'.$match_c_name.'/outcome?home_team=' . $v->match_id . '">' . $v->match_id . '</a> (<span>' . $v->account_name . '</span>)<br>' . $v->order_id;
                $v->home_guest = $v->home_team . ' VS ' . $v->guest_team;
                $v->time = '开赛：' . $v->match_date . ' ' . $v->match_time;
                //下注时间/开赛时间
                $v->money_match_time = '下注：' . $v->money_time . $v->time;

            }
            //冠军联赛投注 联赛id+订单id  
            else{
                $v->lg_id_order = '<a href="/admin/'.$leage_c_name.'/index?name_chinese=' . $v->league . '">' . $v->lg_id . '</a> (<span>' . $v->account_name . '</span>)<br>' . $v->order_id;
                $v->home_guest = "冠军联赛投注";
                $v->time = '结束：' . $v->last_time;
                //下注时间/结束时间
                $v->money_match_time = '下注：' . $v->money_time . $v->time;

            }
           //money_match_time

            //追加输赢结果 /投注详情
            $contentData = $this->getOrderC($content,$v,$gameName);
            $v->content = $contentData['content'];
            
            //未作废/撤单 数据
            if($v->status < 2){
                //反水
                $v->water_return = commonFunction::filter_by_value($water_return_money,'trade_id',$v->order_id)['money'];
                //根据输赢 拼接结果金额  
                //默认 结果为0 
                $v->result = 0;//$v->water_return;
                if($contentData['result'] == '赢'){
                    //反水+预期中奖金额+投注金额
                    $v->result = $v->water_return + $v->money + $v->prize_money;
                }
                if($contentData['result'] == '赢半平半'){
                    //投注金额+投注金额的1/2*赔率+返水
                    $v->result = $v->water_return + ($v->prize_money/2) + $v->money;
                }
                if($contentData['result'] == '输半平半'){
                    //投注金额的1/2+返水
                    $v->result = $v->water_return + ($v->money/2);
                }
                if($contentData['result'] == '平'){
                    //投注金额
                    $v->result = $v->money;
                }
                if($contentData['result'] == '输'){
                    //投注金额
                    $v->result = $v->water_return;
                }

                //用户投注前账户金额
                $v->available_cash = number_format(($v->money_cash + $v->money),2);
                //用户投注后账户金额
                $v->frozen_cash = commonFunction::filter_by_value($water_return_money,'trade_id',$v->order_id)['money_cash'];
           
                //投注前后账户金额
                $v->account_money = $v->available_cash . '<br><span>' . number_format($v->money,2) . '</span><br>' . $v->frozen_cash;

            }else{//已作废/撤单
                //反水
                $v->water_return = 0;
                //如果注单已作废 结果金额 = 投注金额
                $v->result = $v->money;

                //作废资金变动
                $money_data = commonFunction::filter_by_value($invalid_money,'trade_id',$v->order_id);

                //用户作废后账户金额
                $v->frozen_cash = $money_data['money_cash'];
             
                //用户作废前账户金额
                 $v->available_cash = number_format(($v->frozen_cash - $v->money),2);
                //作废前后账户金额
                $v->account_money = $v->available_cash . '<br><span>' . number_format($v->money,2) . '</span><br>' . $v->frozen_cash;

            }

            //===结算/订单状态===
            if ($v->settle_status == 1) {
                $settle_status = '未结算';
            } else{
                $settle_status = '已结算';
            }
            if($v->status==1 && $v->settle_status == 1){
                $v->order_status = '投注<br>'.$settle_status;
            }
            else if ($v->status == 1 && $v->settle_status != 1) {
                //$v->order_status = '投注<br>'.$settle_status;
                $v->order_status = '<span>'.$contentData['result'].'</span>';

            } else if ($v->status == 2) {
                $v->order_status = '作废<br>'.$v->use_mark;
            } else if ($v->status == 3) {
                $v->order_status = '撤单<br>'.$settle_status;
            }



            //区别是否是 冠军投注
            if($v->is_champion == 1){
                $function = 'setResult_v2';
            }else{
                $function = 'setScore_v2';
            }
            //获取 投注玩法名称
            if(!empty($contentData['odds_name'])){
                $odds_name = $contentData['odds_name'];
            }else{
                $odds_name = '未知玩法';
            }

            //根据球类/玩法名称 获取需设置比分项 
            $ScoreTitle = $this->getScoreTitle($odds_name,$type);

            //操作 按钮
            //投注/未结算 则有结算/作废 按钮  
            if($v->status==1 and $v->settle_status ==1){
                $v->game_status = '
                <button style="margin-left: 0;" onclick="'.$function.'(\'' . $v->order_id . '\',\''.$v->match_id.'\',\''.$type.'\',\''.$ScoreTitle.'\',\''.$v->league.'\',\''.$v->home_team.'\',\''.$v->guest_team.'\')"  class="layui-btn layui-btn-sm settlement">手动结算</button>
                <br>
                <a class="layui-btn layui-btn-sm invalid" style="background-color:red" lay-event="detail" pid="id" uri="/admin/SoccerNoteList/invalid/?id=" href="javascript:invalid(\'/admin/SoccerNoteList/invalid/?id=' . $v->id . '\',\''.$v->order_id.'\',\''.$v->league.'\');"> 作废 </a>';
            }
            //投注/已结算 则有取消结算/作废 按钮
            if($v->status==1 and $v->settle_status >1){
                $v->game_status = '
                <button style="margin-left: 0;" onclick="no_cancel_end(\'' . $v->order_id . '\')"  class="layui-btn layui-btn-sm settlement">取消结算</button>
                <br>
                <a class="layui-btn layui-btn-sm invalid" style="background-color:red" lay-event="detail" pid="id" uri="/admin/SoccerNoteList/invalid/?id=" href="javascript:invalid(\'/admin/SoccerNoteList/invalid/?id=' . $v->id . '\',\''.$v->order_id.'\',\''.$v->league.'\');"> 作废 </a>';
            }
            //作废  取消作废
            if($v->status==2){
                $v->game_status = '
                <button style="margin-left: 0;" onclick="no_cancel_invalid(\'' . $v->order_id . '\')"  class="layui-btn layui-btn-sm settlement">取消作废</button>';
            }

        }
        return $orderData->toArray();
    }
    /**
     * 根据球类/玩法名称 获取需设置比分项 
     */
    public function getScoreTitle($oddsName,$gameCode){
        //足球
        if($gameCode == 'zq'){
            //上半场
            $ScoreTitle_h = commonFunction::checkstr($oddsName,'上半场');
            //全场
            $ScoreTitle_f = commonFunction::checkstr($oddsName,'全场');
            //角球
            $ScoreTitle_c = commonFunction::checkstr($oddsName,'角球');
            //有上半场 无角球 为上半场比分
            if($ScoreTitle_h == 1 and $ScoreTitle_c == 0){
                $ScoreTitle = '上半';
            }
            //有上半场 有角球 为角球上半场比分
            if($ScoreTitle_h == 1 and $ScoreTitle_c == 1){
                $ScoreTitle = '角球上半';
            }
            //有全场 无角球 为全场比分
            if($ScoreTitle_f == 1 and $ScoreTitle_c == 0){
                $ScoreTitle = '全场';
            }
            //有全场 有角球 为角球全场比分
            if($ScoreTitle_f == 1 and $ScoreTitle_c == 1){
                $ScoreTitle = '角球全场';
            }
        }
        //篮球
        if($gameCode == 'lq'){

            //[{"home_team":0,"guest_team":0,"home_score":0,"guest_score":0,"all_goal":0,"first_score":0,"last_score":0,"match_winer":0,"update_time":0,"match_id":0,"u_guest_score":0,"u_home_score":0,"penaltycard":{"home":0,"guest":0,"home_half":0,"guest_half":0},"cornerball":{"home":0,"guest":0,"home_half":0,"guest_half":0}}]

            //得分最后一位数
            $ScoreTitle_dfNum = commonFunction::checkstr($oddsName,'得分最后一位数');
            //上半场
            $ScoreTitle_h = commonFunction::checkstr($oddsName,'上半场');
            //全场
            $ScoreTitle_f = commonFunction::checkstr($oddsName,'全场');
            //下半场
            $ScoreTitle_s = commonFunction::checkstr($oddsName,'下半场');
            //第一节
            $ScoreTitle_1 = commonFunction::checkstr($oddsName,'一节');
            //第二节
            $ScoreTitle_2 = commonFunction::checkstr($oddsName,'二节');
            //第三节
            $ScoreTitle_3 = commonFunction::checkstr($oddsName,'三节');
            //第四节
            $ScoreTitle_4 = commonFunction::checkstr($oddsName,'四节');

            //有上半场 为上半场比分
            if($ScoreTitle_h == 1){
                $ScoreTitle = '上半';
            }
            //有下半场 为下半场比分
            if($ScoreTitle_s == 1){
                $ScoreTitle = '下半';
            }
            //有全场 为全场比分
            if($ScoreTitle_f == 1 || $ScoreTitle_dfNum == 1){
                $ScoreTitle = '全场';
            }

            //有第一节 为第一节比分
            if($ScoreTitle_1 == 1){
                $ScoreTitle = '第一节';
            }
            //有第二节 为第二节比分
            if($ScoreTitle_2 == 1){
                $ScoreTitle = '第二节';
            }
            //有第三节 为第三节比分
            if($ScoreTitle_3 == 1){
                $ScoreTitle = '第三节';
            }
            //有第四节 为第四节比分
            if($ScoreTitle_4 == 1){
                $ScoreTitle = '第四节';
            }
        }

        //棒球
        if($gameCode == 'bq'){
            //全场
            $ScoreTitle_f = commonFunction::checkstr($oddsName,'全场');

            //前五局
            $ScoreTitle_five = commonFunction::checkstr($oddsName,'前五局');

            //有全场
            if($ScoreTitle_f == 1){
                $ScoreTitle = '全场';
            }
            //前五局
            if($ScoreTitle_five == 1){
                $ScoreTitle = '前五局';
            }
        }

        if($gameCode == 'wq'){
             //让盘
             $ScoreTitle_rp = commonFunction::checkstr($oddsName, '让盘');
             //让局
             $ScoreTitle_rj = commonFunction::checkstr($oddsName, '让局');
             //冠军
             $ScoreTitle_gj = commonFunction::checkstr($oddsName, '冠军');
             //总局数
             $ScoreTitle_all_j = commonFunction::checkstr($oddsName, '总局数');
             //波胆
             $ScoreTitle_bd = commonFunction::checkstr($oddsName, '波胆');
             //一盘
             $ScoreTitle_1 = commonFunction::checkstr($oddsName, '第一盘');
             //二盘
             $ScoreTitle_2 = commonFunction::checkstr($oddsName, '第二盘');
             //三盘
             $ScoreTitle_3 = commonFunction::checkstr($oddsName, '第三盘');
              //四盘
              $ScoreTitle_4 = commonFunction::checkstr($oddsName, '第四盘');
              //五盘
              $ScoreTitle_5 = commonFunction::checkstr($oddsName, '第五盘');

             //全场 波胆 
             if ($ScoreTitle_bd == 1) {
                 $ScoreTitle = '全场';
             }
             //只有总局数 全场
             if($ScoreTitle_all_j == 1 and $ScoreTitle_1 == 0 and $ScoreTitle_2 == 0 and $ScoreTitle_3 == 0 ){
                $ScoreTitle = '全场';
             }
             //只有让盘 全场
             if($ScoreTitle_rp == 1 and $ScoreTitle_1 == 0 and $ScoreTitle_2 == 0 and $ScoreTitle_3 == 0 ){
                $ScoreTitle = '全场';
             }
             //只有让局 全场
             if($ScoreTitle_rj == 1 and $ScoreTitle_1 == 0 and $ScoreTitle_2 == 0 and $ScoreTitle_3 == 0 ){
                $ScoreTitle = '全场';
             }
             //只有冠军 全场
             if($ScoreTitle_gj == 1 and $ScoreTitle_1 == 0 and $ScoreTitle_2 == 0 and $ScoreTitle_3 == 0 ){
                $ScoreTitle = '冠军';
             }
             //一盘
             if($ScoreTitle_1 == 1){
                $ScoreTitle = '第一盘';
             }
             //二盘
             if($ScoreTitle_2 == 1){
                $ScoreTitle = '第二盘';
             }
             //三盘
             if($ScoreTitle_3 == 1){
                $ScoreTitle = '第三盘';
             }
              //四盘
              if($ScoreTitle_4 == 1){
                $ScoreTitle = '第四盘';
             }
             //五盘
             if($ScoreTitle_5 == 1){
                $ScoreTitle = '第五盘';
             }
        }
        if(empty($ScoreTitle)){
            $ScoreTitle = '未知';
        }

        return $ScoreTitle;
    }

    /**
     * 拼接 订单详情 字段内容
     */
    public function getOrderC($content,$order,$gameName){
        $content_str = commonFunction::filter_by_value($content,'order_id',$order->order_id);

         //===投注详情===
         $content1 = '';
         //如果是滚球投注 追加投注时赛事进行时间
         if (!empty($order->roll_time) and $order->is_rolling == 1) {
            $content1 = '[' . $order->roll_time . ']';
        }
        //  else if (!empty($order->last_time)) {
        //     $datetime = explode(' ', $order->last_time);
        //     $content1 = '[' . $datetime[1] . ']';
        // }

        // 当前赛事 投注 结果  0未处理  1 赢 -1输  2平 3赢半平半  4输半平半
        if ($content_str['result'] == 0) {
            $content_str['result'] = '未处理';
        }
        if ($content_str['result'] == 1) {
            $content_str['result'] = '赢';
        }
        if ($content_str['result'] == -1) {
            $content_str['result'] = '输';
        }
        if ($content_str['result'] == 2) {
            $content_str['result'] = '平';
        }
        if ($content_str['result'] == 3) {
            $content_str['result'] = '赢半平半';
        }
        if ($content_str['result'] == 4) {
            $content_str['result'] = '输半平半';
        }

        //当前投注所属球类及盘口
        if($order->is_rolling == 0){//未知 盘口
            $typeName = $gameName.'未知盘口';
        }
        if($order->is_rolling == 1){//滚球
            $typeName = $gameName.'滚球';
        }
        if($order->is_rolling == 2){//即将
            $typeName = $gameName.'即将';
        }
        if($order->is_rolling == 3){//今日
            $typeName = $gameName.'今日';
        }
        if($order->is_rolling == 4){//早盘
            $typeName = $gameName.'早盘';
        }
        if($order->is_rolling == 5){//串场
            $typeName = $gameName.'串场';
        }
        if($order->is_rolling == 6){//冠军
            $typeName = $gameName.'冠军';
        }
        if($order->is_rolling == 7){//首页投注 只显示球类
            $typeName = $gameName.'首页';
        }

        //普通赛事注单 详情
        if($order->is_champion == 0){
            if ($content_str['result'] == '未处理' || $order->status > 1) {
                if(!empty($content_str['content'])){
                    $content_str['content'] = $typeName.'<br>'.$content1.$content_str['content'];
                }else{
                    $content_str['content'] =  $typeName.'<br>'.$content1.'未知玩法';
                }
            }
            else if($order->status == 1) {
//                $content_str['content'] = $content1.$content_str['content']. '<span>' . $content_str['result'] . '</span>(' . $content_str['matchresult'] . ')<br>';
                $content_str['content'] =  $typeName.'<br>'.$content1.$content_str['content']. '<span>' .  '</span>(' . $content_str['matchresult'] . ')<br>';
            }
        }

        //冠军联赛注单 详情
        else{
            if ($content_str['result'] == '未处理' || $order->status > 1) {
                $content_str['content'] =  $typeName.'<br>'.$content_str['home_team'] . '<br>' . $order->content . $content_str['odds_code'] . '<br>' . $content_str['condition'] . ' ' . $order->team . '@' . $content_str['odds'] . ' ￥' . $content_str['bet_money'] . '<br>';
            } else {
                $content_str['content'] =  $typeName.'<br>'.$content_str['home_team'] . '<br>' . $order->content . $content_str['odds_code'] . '<br>' . $content_str['condition'] . ' ' . $order->team . '@' . $content_str['odds'] . ' ￥' . $content_str['bet_money'] . '<br><span>' . $content_str['result'] . '</span>(' . $content_str['matchresult'] . ')<br>';
            }
        }
    
        return $content_str;
    }


    /**
     * 获取单式注单 数据
     * 因异常 暂停使用
     */
    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.id', '=', 'money_buy_simplex.match_id')
            ->join('money_buy_match','money_buy_match.order_id','=','money_buy_simplex.order_id')
            ->join('st_' . $type . '_league', 'money_buy_simplex.lg_id', '=', 'st_' . $type . '_league.id')
            ->select('money_buy_simplex.id', 'money_buy_simplex.account_name', 'money_buy_simplex.is_champion', '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','money_buy_simplex.use_mark', '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')
            ->whereIn('money_buy_simplex.roll_ratify',array(0,1,-1))
            ->where($where)
            //->distinct('money_buy_match.order_id')
            ->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', $type)->get();
        //$result = \App\Model\Matchcode::get();


        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/SoccerNoteList/invalid/?id=" href="javascript:invalid(\'/admin/SoccerNoteList/invalid/?id=' . $data[$i]->id . '\',\''.$data[$i]->order_id.'\',\''.$data[$i]->name_chinese.'\');"> 作废 </a>';
             }
            else if ($data[$i]->status == 1 && $data[$i]->settle_status != 1) {
                // $data[$i]->order_status = '投注'.'<br>';
            } 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{
                $data[$i]->settle_status = '已结算';
            }
            if ($data[$i]->game_status == 0) {
                $data[$i]->game_status = '未处理';
            } else {
                $data[$i]->game_status = '已处理';
            }
            */
             /*
                if ($data[$i]->settle_status == 1) {
                    $data[$i]->settle_status = '未结算';
                } else{
                    $data[$i]->settle_status = '已结算';
                }
                */
            if ($data[$i]->settle_status == 1) {
                $settle_status = '未结算';
            } else{
                $settle_status = '已结算';
            }
            if($data[$i]->status==1 && $data[$i]->settle_status == 1){
                $data[$i]->order_status = '投注<br>'.$settle_status;
            }
            else if ($data[$i]->status == 1 && $data[$i]->settle_status != 1) {
                // $data[$i]->order_status = '投注'.'<br>';
                $data[$i]->order_status = '投注<br>'.$settle_status;

            } else if ($data[$i]->status == 2) {
                $data[$i]->order_status = '作废<br>'.$data[$i]->use_mark;
                // $data[$i]->order_status = '作废<br>'.$settle_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>'.$settle_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]->game_status == 0) {
                $data[$i]->game_status = '未处理';
            } else {
                $data[$i]->game_status = '已处理';
            }

            $data[$i]->league = $data[$i]->name_chinese;

            if ($data[$i]->is_champion == 0) {
                if ($data[$i]->game_code == 'zq') {
                    $data[$i]->match_id_order = '<a href="/admin/sportsfoot/outcome?home_team=' . $data[$i]->match_id . '">' . $data[$i]->match_id . '</a> (<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;
                }
                if ($data[$i]->game_code == 'lq') {
                    $data[$i]->match_id_order = '<a href="/admin/Sportsbk/outcome?home_team=' . $data[$i]->match_id . '">' . $data[$i]->match_id . '</a> (<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;
                }
                if ($data[$i]->game_code == 'wq') {
                    $data[$i]->match_id_order = '<a href="/admin/Sportswq/outcome?home_team=' . $data[$i]->match_id . '">' . $data[$i]->match_id . '</a> (<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;
                }
                if ($data[$i]->game_code == 'bq') {
                    $data[$i]->match_id_order = '<a href="/admin/Sportsbq/outcome?home_team=' . $data[$i]->match_id . '">' . $data[$i]->match_id . '</a> (<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;
                }

                $data[$i]->home_guest = $data[$i]->home_team . ' VS ' . $data[$i]->guest_team;
                $data[$i]->time = '开赛：' . $data[$i]->match_date . ' ' . $data[$i]->match_time;
            } else {
                $data[$i]->lg_id_order = $data[$i]->match_id . '(<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;

                $data[$i]->home_guest = '冠军联赛投注';
                $data[$i]->time = '结束：' . $data[$i]->last_time;

            }
            $data[$i]->money_match_time = '下注：' . $data[$i]->money_time . $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;
            }
           
            //如果注单已作废 结果金额 = 投注金额
            if($data[$i]->status == 2){
                $data[$i]->result = $data[$i]->money;
            }else{
                //此时结果金额
                $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;
//                    }
//                }
                $data[$i]->account_money = $data[$i]->available_cash;
            }
            $data[$i]->str = 0;
            for ($b = 0; $b < count($content); $b++) {
                $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 && $data[$i]->is_champion == 0) {

                    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>(' . $content[$b]->matchresult . ')<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>(' . $content[$b]->matchresult . ')<br>';
                        }
                    }
                }
                //处理冠军联赛玩法详情
                if ($data[$i]->match_id == $content[$b]->lg_id && $data[$i]->batch_id == $content[$b]->batch_id && $data[$i]->is_champion == 1) {
                    $content[$b]->condition = '';

                    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 = $content[$b]->home_team.'<br>'.$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 = $content[$b]->home_team.'<br>'.$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>('.$content[$b]->matchresult.')<br>';
                            }
                        }
                    }
                    */
                    if ($data[$i]->str1 == 0) {
                        if ($content[$b]->result == '未处理') {
                            $data[$i]->content = $content[$b]->home_team . '<br>' . $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 = $content[$b]->home_team . '<br>' . $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>(' . $content[$b]->matchresult . ')<br>';
                        }
                    }
                }
            }
            // if($data[$i]->status == 1){
            // if($data[$i]->match_status == 0 || $data[$i]->match_status == 1){
            //     $data[$i]->game_status = $data[$i]->settle_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 == 3 || $data[$i]->match_status == 5){
            //         if($data[$i]->settle_status == '已结算'){
            //             $data[$i]->game_status = $data[$i]->settle_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]->settle_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>';
            //         }
            //     }
            // }else{
            //     $data[$i]->game_status = $data[$i]->settle_status.'<br><a class="layui-btn layui-btn-sm audit" lay-event="detail" pid="id" uri="" href="javascript:void(0)" style="background-color: grey;"> 结2算 </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.id', '=', 'money_buy_simplex.match_id')->join('st_' . $type . '_league', 'st_' . $type . '_competition.lg_id', '=', 'st_' . $type . '_league.id')->select('money_buy_simplex.money', 'money_buy_simplex.prize_money', 'money_buy_simplex.gain_money')->whereIn('money_buy_simplex.roll_ratify',array(0,1))->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.id', '=', 'money_buy_simplex.match_id')->join('st_' . $type . '_league', 'st_' . $type . '_competition.lg_id', '=', 'st_' . $type . '_league.id')->select('money_buy_simplex.money', 'money_buy_simplex.prize_money', 'money_buy_simplex.gain_money')->whereIn('money_buy_simplex.roll_ratify',array(0,1))->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.id', '=', 'money_buy_simplex.match_id')->join('st_' . $type . '_league', 'st_' . $type . '_competition.lg_id', '=', 'st_' . $type . '_league.id')->select('money_buy_simplex.money', 'money_buy_simplex.prize_money', 'money_buy_simplex.gain_money')->where('money_buy_simplex.settle_status', '2')->whereIn('money_buy_simplex.roll_ratify',array(0,1))->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.id', '=', 'money_buy_simplex.match_id')->join('st_zq_league', 'st_zq_competition.lg_id', '=', 'st_zq_league.id')->select('money_buy_simplex.money', 'money_buy_simplex.prize_money', 'money_buy_simplex.gain_money')->whereIn('money_buy_simplex.roll_ratify',array(0,1))->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.id', '=', 'money_buy_simplex.match_id')->join('st_' . $type . '_league', 'st_' . $type . '_competition.lg_id', '=', 'st_' . $type . '_league.id')->select('money_buy_simplex.money', 'money_buy_simplex.prize_money', 'money_buy_simplex.gain_money')->whereIn('money_buy_simplex.roll_ratify',array(0,1))->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.id', '=', 'money_buy_simplex.match_id')->join('st_' . $type . '_league', 'st_' . $type . '_competition.lg_id', '=', 'st_' . $type . '_league.id')->select('money_buy_simplex.money', 'money_buy_simplex.prize_money', 'money_buy_simplex.gain_money')->whereIn('money_buy_simplex.roll_ratify',array(0,1))->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;
        }
    }


    function info($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.id', '=', 'money_buy_simplex.match_id')
            // ->join('st_'.$type.'_league','st_'.$type.'_competition.lg_id','=','st_'.$type.'_league.id')
            ->join('st_' . $type . '_league', 'money_buy_simplex.lg_id', '=', 'st_' . $type . '_league.id')
            ->select('money_buy_simplex.id', 'money_buy_simplex.account_name', 'money_buy_simplex.is_champion', 'money_buy_simplex.account_identity', 'money_buy_simplex.order_id','money_buy_simplex.use_mark', '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')
            ->whereIn('money_buy_simplex.roll_ratify',array(0,1))
            ->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', $type)->get();
        //$result = \App\Model\Matchcode::get();


        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/SoccerNoteList/invalid/?id=" href="javascript:invalid(\'/admin/SoccerNoteList/invalid/?id=' . $data[$i]->id . '\',\''.$data[$i]->order_id.'\',\''.$data[$i]->name_chinese.'\');"> 作废 </a>';
            } else if ($data[$i]->status == 1 && $data[$i]->settle_status != 1) {
                $data[$i]->order_status = '投注';
            } else if ($data[$i]->status == 2) {
                $data[$i]->order_status = '作废';//
            } else if ($data[$i]->status == 3) {
                $data[$i]->order_status = '撤单';
            }
            // 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 = '已处理';
            }
            */
            if ($data[$i]->settle_status == 1) {
                $settle_status = '未结算';
            } else{
                $settle_status = '已结算';
            }
            if($data[$i]->status==1 && $data[$i]->settle_status == 1){
                $data[$i]->order_status = '投注<br>'.$settle_status;
            }
            else if ($data[$i]->status == 1 && $data[$i]->settle_status != 1) {
                // $data[$i]->order_status = '投注'.'<br>';
                $data[$i]->order_status = '投注<br>'.$settle_status;

            } else if ($data[$i]->status == 2) {
                $data[$i]->order_status = '作废<br>'.$data[$i]->use_mark;
                // $data[$i]->order_status = '作废<br>'.$settle_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>'.$settle_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]->game_status == 0) {
                $data[$i]->game_status = '未处理';
            } else {
                $data[$i]->game_status = '已处理';
            }

            $data[$i]->league = $data[$i]->name_chinese;

            if ($data[$i]->is_champion == 0) {
                if ($data[$i]->game_code == 'zq') {
                    $data[$i]->match_id_order = '<a href="/admin/sportsfoot/outcome?home_team=' . $data[$i]->match_id . '">' . $data[$i]->match_id . '</a> (<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;
                }
                if ($data[$i]->game_code == 'lq') {
                    $data[$i]->match_id_order = '<a href="/admin/Sportsbk/outcome?home_team=' . $data[$i]->match_id . '">' . $data[$i]->match_id . '</a> (<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;
                }
                if ($data[$i]->game_code == 'wq') {
                    $data[$i]->match_id_order = '<a href="/admin/Sportswq/outcome?home_team=' . $data[$i]->match_id . '">' . $data[$i]->match_id . '</a> (<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;
                }
                if ($data[$i]->game_code == 'bq') {
                    $data[$i]->match_id_order = '<a href="/admin/Sportsbq/outcome?home_team=' . $data[$i]->match_id . '">' . $data[$i]->match_id . '</a> (<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;
                }

                $data[$i]->home_guest = $data[$i]->home_team . ' VS ' . $data[$i]->guest_team;
                $data[$i]->time = '开赛：' . $data[$i]->match_date . ' ' . $data[$i]->match_time;

            } else {
                $data[$i]->lg_id_order = $data[$i]->match_id . '(<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;

                $data[$i]->home_guest = '冠军联赛投注';
                $data[$i]->time = '结束：' . $data[$i]->last_time;

            }
            $data[$i]->money_match_time = '下注：' . $data[$i]->money_time . $data[$i]->time;

            /*1
            $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;
            }
            //如果是作废/撤单状态 结果金额 为投注金额
            if ($data[$i]->status == 2 || $data[$i]->status == 3){
                //此时结果金额
                $data[$i]->result = $data[$i]->money;
            }else{
                //此时结果金额
                $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;
//                    }
//                }
                $data[$i]->account_money = $data[$i]->available_cash;
            }
            $data[$i]->str = 0;
            for ($b = 0; $b < count($content); $b++) {
                $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 && $data[$i]->is_champion == 0) {
                    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>(' . $content[$b]->matchresult . ')<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>(' . $content[$b]->matchresult . ')<br>';
                        }
                    }
                }
                //处理冠军联赛玩法详情
                if ($data[$i]->match_id == $content[$b]->lg_id && $data[$i]->batch_id == $content[$b]->batch_id && $data[$i]->is_champion == 1) {

                    $content[$b]->condition = '';

                    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>('.$content[$b]->matchresult.')<br>';
                            }
                        }
                    }
                    */
                    if ($data[$i]->str1 == 0) {
                        if ($content[$b]->result == '未处理') {
                            $data[$i]->content = $content[$b]->home_team . '<br>' . $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 = $content[$b]->home_team . '<br>' . $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>(' . $content[$b]->matchresult . ')<br>';
                        }
                    }
                }
            }
            $data[$i]->game_status = $data[$i]->settle_status;
            $data[$i]->content = $data[$i]->content1 . $data[$i]->content;
        }
        return $data->toArray();
    }

    //查询单订单是否存在
    function onlyorder($orderid)
    {
        $order = $this->where('order_id', $orderid)->count();
        return $order;
    }

    //查询单订单是否存在
    function getorder($orderid)
    {
        $order = $this->with("matchdatas")->where('order_id', $orderid)->first();
        return $order;
    }


    function checkinfo($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.id', '=', 'money_buy_simplex.match_id')
            ->join('money_buy_match','money_buy_match.order_id','=','money_buy_simplex.order_id')
            ->join('st_' . $type . '_league', 'money_buy_simplex.lg_id', '=', 'st_' . $type . '_league.id')
            ->select('money_buy_simplex.id', 'money_buy_simplex.account_name', 'money_buy_simplex.is_champion', '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', 'money_buy_simplex.roll_ratify', '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')
            ->whereIn('money_buy_simplex.roll_ratify',array(-1,2))
            ->where($where)
            //->distinct('money_buy_match.order_id')
            ->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', $type)->get();
        //$result = \App\Model\Matchcode::get();


        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/SoccerNoteList/invalid/?id=" href="javascript:invalid(\'/admin/SoccerNoteList/invalid/?id=' . $data[$i]->id . '\',\''.$data[$i]->order_id.'\',\''.$data[$i]->name_chinese.'\');"> 作废 </a>';
             }
            if ($data[$i]->status == 1 && $data[$i]->settle_status != 1) {
                $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 = '已处理';
            }

            if ($data[$i]->roll_ratify == 2) {
                $data[$i]->roll_ratify = '
                <a lay-event="detail" pid="id" value="有效" uri="/admin/CheckRolling/check/?id=" href="javascript:check(\'/admin/CheckRolling/check/?id=' . $data[$i]->id . '\');"> 有效 </a>
                <br>
                <a lay-event="detail" pid="id" style="margin-top:5px;" uri="/admin/CheckRolling/nocheck/?id=" href="javascript:goalNocheck(\'/admin/CheckRolling/nocheck/?id=' . $data[$i]->id . '&type=1\');"> 进球无效 </a>
                <br>
                <a lay-event="detail" pid="id" style="margin-top:5px;" uri="/admin/CheckRolling/nocheck/?id=" href="javascript:redCardNocheck(\'/admin/CheckRolling/nocheck/?id=' . $data[$i]->id . '&type=2\');"> 红卡无效 </a>
                <br>                
                <a lay-event="detail" pid="id" style="margin-top:5px;" uri="/admin/CheckRolling/nocheck/?id=" href="javascript:nocheck(\'/admin/CheckRolling/nocheck/?id=' . $data[$i]->id . '&type=0\');"> 无效 </a>
                ';

                // $data[$i]->roll_ratify = '<a class="layui-btn layui-btn-sm lay-btn-diy" lay-event="detail" pid="id" uri="/admin/CheckRolling/check/?id=" href="javascript:check(\'/admin/CheckRolling/check/?id=' . $data[$i]->id . '\');"> 通过 </a><br><a class="layui-btn layui-btn-sm invalid" lay-event="detail" pid="id" style="margin-top:5px;" uri="/admin/CheckRolling/nocheck/?id=" href="javascript:nocheck(\'/admin/CheckRolling/nocheck/?id=' . $data[$i]->id . '\');"> 拒绝 </a>';
            }
            if ($data[$i]->roll_ratify == -1) {
                $data[$i]->roll_ratify = '未通过审核';
            }


            if ($data[$i]->game_code == 'zq') {
                $data[$i]->match_id_order = '<a href="/admin/sportsfoot/outcome?home_team=' . $data[$i]->match_id . '">' . $data[$i]->match_id . '</a> (<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;
            }
            if ($data[$i]->game_code == 'lq') {
                $data[$i]->match_id_order = '<a href="/admin/Sportsbk/outcome?home_team=' . $data[$i]->match_id . '">' . $data[$i]->match_id . '</a> (<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;
            }
            if ($data[$i]->game_code == 'wq') {
                $data[$i]->match_id_order = '<a href="/admin/Sportswq/outcome?home_team=' . $data[$i]->match_id . '">' . $data[$i]->match_id . '</a> (<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;
            }
            if ($data[$i]->game_code == 'bq') {
                $data[$i]->match_id_order = '<a href="/admin/Sportsbq/outcome?home_team=' . $data[$i]->match_id . '">' . $data[$i]->match_id . '</a> (<span>' . $data[$i]->account_name . '</span>)<br>' . $data[$i]->order_id;
            }

            $data[$i]->league = $data[$i]->name_chinese;


            if ($data[$i]->is_champion == 0) {
                $data[$i]->home_guest = $data[$i]->home_team . ' VS ' . $data[$i]->guest_team;
                $data[$i]->time = '开赛：' . $data[$i]->match_date . ' ' . $data[$i]->match_time;
            } else {
                $data[$i]->home_guest = '冠军联赛投注';
                $data[$i]->time = '结束：' . $data[$i]->last_time;

            }
            $data[$i]->money_match_time = '下注：' . $data[$i]->money_time . $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;
//                    }
//                }
                $data[$i]->account_money = $data[$i]->available_cash;
            }
            $data[$i]->str = 0;
            for ($b = 0; $b < count($content); $b++) {
                $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 && $data[$i]->is_champion == 0) {

                    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;
                            $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>';

                            /*
                            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>(' . $content[$b]->matchresult . ')<br>';
                            }
                            */
                        }
                    }
                    if ($data[$i]->str1 == 0) {
                        $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>';
                        /*
                        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>(' . $content[$b]->matchresult . ')<br>';
                        }
                        */
                    }
                }
                //处理冠军联赛玩法详情
                if ($data[$i]->match_id == $content[$b]->lg_id && $data[$i]->batch_id == $content[$b]->batch_id && $data[$i]->is_champion == 1) {
                    $content[$b]->condition = '';

                    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 = $content[$b]->home_team.'<br>'.$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 = $content[$b]->home_team.'<br>'.$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>('.$content[$b]->matchresult.')<br>';
                            }
                        }
                    }
                    */
                    if ($data[$i]->str1 == 0) {
                        if ($content[$b]->result == '未处理') {
                            $data[$i]->content = $content[$b]->home_team . '<br>' . $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 = $content[$b]->home_team . '<br>' . $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>(' . $content[$b]->matchresult . ')<br>';
                        }
                    }
                }
            }
            // if($data[$i]->status == 1){
            // if($data[$i]->match_status == 0 || $data[$i]->match_status == 1){
            //     $data[$i]->game_status = $data[$i]->settle_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 == 3 || $data[$i]->match_status == 5){
            //         if($data[$i]->settle_status == '已结算'){
            //             $data[$i]->game_status = $data[$i]->settle_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]->settle_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>';
            //         }
            //     }
            // }else{
            //     $data[$i]->game_status = $data[$i]->settle_status.'<br><a class="layui-btn layui-btn-sm audit" lay-event="detail" pid="id" uri="" href="javascript:void(0)" style="background-color: grey;"> 结2算 </a>';
            // }

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

        }
        return $data->toArray();

    }

    /**
     * 作废赛事下注单处理
     * $matchid int 赛事id
     * $type str 球类代码 zq lq wq bq
     * $orders_sim obj 待处理单式注单数据 非必须
     * $orders_str obj 待处理串式注单数据 非必须
     * $type_handleType int 默认0 通过赛事id处理 1根据注单处理
     */
    function delorder($matchid,$type,$orders_sim=[],$orders_str=[],$type_handleType=0){
        //该赛事的单式注单
        $account_identitys = array();
        if(empty($orders_sim) and $type_handleType == 0){
            //无注单时，按整个赛事查询
            $orders = $this->where(['match_id'=>$matchid,'game_code'=>$type])->get();
        }else{
            //有注单时 按注单处理
            $orders = $orders_sim;
        }
        //去除数组的空值
        foreach( $orders as $k=>$v){
            if( !$v )
                unset( $orders[$k] );
        }
        //数组去重
        foreach( $orders as $k=>$v){
            $order = $orders;
            unset( $order[$k] );
            foreach( $order as $kk=>$vv){
                if( $v['order_id'] ==  $vv['order_id'])
                    unset( $orders[$k] );
            }
        }

        $order_ids = array();
        if(!empty($orders)){
            foreach( $orders as $k=>$v){
                $account_identitys[] = $v['account_identity'];
                $order_ids[] = $v['order_id'];
            }

            //用戶账户金额
            $account_money = \App\Models\Account_detailed::wherein('account_identity',$account_identitys)->get();
            //反水
            $water_return_money = \App\Models\Money_details::wherein('trade_id',$order_ids)->where('trade_type', '7')->get();

            //订单反水
            foreach($water_return_money as $k=>$v){
                foreach($orders as $kk=>$vv){
                    $vv['return_money'] = 0;
                    if($v['trade_id'] == $vv['order_id']){
                        $vv['return_money'] = $v['money'];
                    }
                }
            }

            //用户余额
            foreach($account_money as $k=>$v){
                $account_money = 0;
                foreach($orders as $kk=>$vv){
                    if($v['account_identity'] == $vv['account_identity']){
                        if($account_money == 0){
                            $vv['available_cash'] = $v['available_cash']+$vv['money']-$vv['return_money']-$vv['gain_money'];
                            //当前注单退款后的余额
                            $account_money = $vv['available_cash'];
                        }else{//其他 获取 上个注单处理后的余额
                            $vv['available_cash'] = $account_money+$vv['money']-$vv['return_money']-$vv['gain_money'];
                            $account_money = $vv['available_cash'];
                        }
                    }
                }
            }

            foreach($orders as $k=>$v){
                $models = new \App\Models\Money_details();
                $models->info_identity = UUID();
                $models->trade_id = $v['order_id'];
                $models->account_name = $v['account_name'];
                $models->account_identity = $v['account_identity'];
                $change_money = $v['money'] - $v['return_money'] - $v['gain_money'];
                $models->money = abs($change_money);
                $models->money_time = date("Y-m-d H:i:s", time());
                if ($change_money > 0) {
                    $models->money_type = '1';
                } else {
                    $models->money_type = '2';
                }
                $models->money_cash = $v['available_cash'];
                $models->trade_type = '3';
                $models->trade_desc = '赛事作废单式注单回款'.$change_money.'元，订单号'.$v['order_id'];
                $models->status = '1';
                try {
                    DB::beginTransaction();//开启事务
                    $this->where('order_id', $v['order_id'])->update(['status' => '2']);//'use_mark'=>'通过设置赛事比分作废'
                    \App\Models\Account_detailed::where('account_identity', $v['account_identity'])->update(['available_cash' => $v['available_cash'], 'cash' => $v['available_cash']]);
                    $models->save();

                    DB::commit();//提交
                } catch (Exception $e) {
                    DB::rollback();//回滚
                }
            }

        }

        //该赛事相关的的串关注单做平局处理
        if(empty($orders_str) and $type_handleType == 0){
            //无注单时，按整个赛事处理
            \App\Models\MoneyBuyMatch::where('match_id',$matchid)->where('game_code',$type)->where('bet_type',2)->update(['result'=>2]);
        }else{
            //有串式注单时 按注单id处理
            $order_ids_str = array_column($orders_str,'order_id');
            \App\Models\MoneyBuyMatch::where('match_id',$matchid)->where('game_code',$type)->whereIn('order_id',$order_ids_str)->where('bet_type',2)->update(['result'=>2]);
        }
    }

    /**
     * 改变注单状态下注单处理
     * $matchid int 赛事id
     * $orders_sim obj 待处理单式注单数据 非必须
     * $orders_str obj 待处理串式注单数据 非必须
     * $type  str 球类代码 zq lq wq bq
     */
    function settleorder($matchid,$orders_sim=[],$orders_str=[],$type){
        //该赛事的单式注单
        $account_identitys = array();
        $orders = array();
        if(!empty($orders_sim)){
            $orders = $orders_sim;
        }
        //该赛事的串式注单
        $order = array();
        if(!empty($orders_str)){
            $order = $orders_str;
        }
        $orders = array_merge($orders,$order);
        //去除数组的空值  去除串式已结算的注单
        foreach( $orders as $k=>$v){
            if( !$v )
                unset( $orders[$k] );
            if( $v['str_type'] && $v['settle_status'] ==2 )
                unset( $orders[$k] );
        }
        //数组去重
        foreach( $orders as $k=>$v){
            $order = $orders;
            unset( $order[$k] );
            foreach( $order as $kk=>$vv){
                if( $v['order_id'] ==  $vv['order_id'])
                    unset( $orders[$k] );
            }
        }
        //print_r($orders);exit;

        $order_ids = array();
        if(!empty($orders)){
            foreach( $orders as $k=>$v){
                $account_identitys[] = $v['account_identity'];
                $order_ids[] = $v['order_id'];
            }

            //用戶账户金额
            $account_money = \App\Models\Account_detailed::wherein('account_identity',$account_identitys)->get();
            //反水
            $water_return_money = \App\Models\Money_details::wherein('trade_id',$order_ids)->where('trade_type', '7')->get();

            //订单反水
            foreach($water_return_money as $k=>$v){
                foreach($orders as $kk=>$vv){
                    $vv['return_money'] = 0;
                    if($v['trade_id'] == $vv['order_id']){
                        $vv['return_money'] = $v['money'];
                    }
                }
            }
            //用户余额
            foreach($account_money as $k=>$v){
                $account_money = 0;
                foreach($orders as $kk=>$vv){
                    if($v['account_identity'] == $vv['account_identity']){
                        if($vv['status'] == 1 && $vv['settle_status'] == 2 && $vv['result'] != -1 && $vv['result'] != 0) {
                            if ($account_money == 0) {
                                $vv['available_cash'] = $v['available_cash'] - $vv['gain_money'];
                                //当前注单状态改变后的余额
                                $account_money = $vv['available_cash'];
                            } else {//其他 获取 上个注单处理后的余额
                                $vv['available_cash'] = $account_money - $vv['gain_money'];
                                $account_money = $vv['available_cash'];
                            }
                        }
                        if($vv['status'] == 2 || $vv['status'] == 3){
                            if ($account_money == 0) {
                                $vv['available_cash'] = $v['available_cash'] - $vv['money'] + $vv['return_money'];
                                //当前注单状态改变后的余额
                                $account_money = $vv['available_cash'];
                            } else {//其他 获取 上个注单处理后的余额
                                $vv['available_cash'] = $account_money - $vv['money'] + $vv['return_money'];
                                $account_money = $vv['available_cash'];
                            }
                        }
                    }
                }
            }

            //修改注单状态
            foreach($orders as $k=>$v){
                $models = new \App\Models\Money_details();
                $models->info_identity = UUID();
                $models->trade_id = $v['order_id'];
                $models->account_name = $v['account_name'];
                $models->account_identity = $v['account_identity'];
                $str = 0;
                $change_money = 0;
                if($v['status'] == 1 && $v['settle_status'] == 2 && $v['gain_money'] != 0){
                    $str = 1;
                    $change_money = 0 - $v['gain_money'];
                    $models->trade_type = '26';
                    $models->trade_desc = '已结算注单改为未结算，扣除中奖金额'.abs($change_money).'元，订单号'.$v['order_id'];
                }
                if($v['status'] == 2 || $v['status'] == 3){
                    $str = 1;
                    $change_money = $v['return_money'] - $v['money'];
                    $models->trade_type = '1';
                    $models->trade_desc = '取消作废注单，扣除投注金额'.abs($change_money).'元，订单号'.$v['order_id'];
                }
                $models->money = abs($change_money);
                $models->money_time = date("Y-m-d H:i:s", time());
                if ($change_money > 0) {
                    $models->money_type = '1';
                } else {
                    $models->money_type = '2';
                }
                $models->money_cash = $v['available_cash'];
                $models->status = '1';

                if($str == 1){
                    try {
                        DB::beginTransaction();//开启事务
                        if($v['str_type']){//串式注单
                            \App\Models\MoneyBuyStr::where('order_id', $v['order_id'])->update(['status' => '1','settle_status' => '1','game_status' => '0','gain_money' => '0','use_mark'=>'']);
                        }else{//单式注单
                            $this->where('order_id', $v['order_id'])->update(['status' => '1','settle_status' => '1','game_status' => '0','gain_money' => '0','use_mark'=>'']);//'use_mark'=>'通过设置赛事比分作废'
                        }
                        \App\Models\SettlementMiddleDetail::where('order_id', $v['order_id'])->update(['money' => '0']);
                        \App\Models\MoneyBuyMatch::where('order_id', $v['order_id'])->where('match_id', $matchid)->update(['result' => 0, 'status' => 0]);
                        \App\Models\Account_detailed::where('account_identity', $v['account_identity'])->update(['available_cash' => $v['available_cash'], 'cash' => $v['available_cash']]);
                        $models->save();

                        DB::commit();//提交
                    } catch (Exception $e) {
                        DB::rollback();//回滚
                    }
                }else{
                    try {
                        DB::beginTransaction();//开启事务
                        if($v['str_type']){//串式注单
                            \App\Models\MoneyBuyStr::where('order_id', $v['order_id'])->update(['status' => '1','settle_status' => '1','game_status' => '0','use_mark'=>'']);
                        }else{//单式注单
                            $this->where('order_id', $v['order_id'])->update(['status' => '1','settle_status' => '1','game_status' => '0','use_mark'=>'']);
                        }
                        \App\Models\MoneyBuyMatch::where('order_id', $v['order_id'])->where('match_id', $matchid)->update(['result' => 0, 'status' => 0]);
                        DB::commit();//提交
                    } catch (Exception $e) {
                        DB::rollback();//回滚
                    }
                }
            }
        }
    }


    /**
     * 滚球投注 危险球自动审核
     * $match_id  int 赛事id
     * $warn_data  arr 危险球数据
     */
    public static function WarnHandle($match_id=0,$warn_data=[]){

        if(!empty($match_id) and !empty($warn_data)){
            $db = new \App\Models\Setinfo();
            //获取设定需审核时间 秒
            $handle_time =  $db->getInfo(1003)['infocontent']?:90;
            //获取赛事下 待审核 滚球投注
            $where = [
                ['match_id','=',$match_id],
                ['roll_ratify','=',2]
            ];
            $order_data = self::where($where)->SELECT('id','account_name','account_identity','order_id','money_time','roll_ratify','money','gain_money')->get()->toArray();
            if(!empty($order_data)){
                //获取审核不通过订单 订单号
                $order_ids_n = [];
                //获取审核不通过订单
                $order_data_n = [];
                //获取审核通过订单 订单号
                $order_ids_y = [];
                //获取审核未通过订单 投注人
                $account_identitys = [];
                foreach($order_data as $k=>$v){
                    //投注时间 时间戳
                    $order_time = strtotime($v['money_time']);
                    foreach($warn_data as $kk=>$vv){
                        //危险球时间 时间戳
                        $warn_time = strtotime($vv['find_time']);
                        if($order_time < $warn_time and ($order_time+$handle_time) > $warn_time){
                            $order_ids_n[] = $v['order_id'];
                            $account_identitys[] = $v['account_identity'];
                            $order_data_n[] = $v;
                            unset($order_data[$k]);
                        }
                    }
                }

                if(!empty($order_data)){
                    foreach($order_data as $k=>$v){
                        $order_ids_y[] = $v['order_id'];
                    }
                }

                if(!empty($order_ids_n)){
                    //在审核时间内订单 审核不通过
                    self::HandleMoney_v2($account_identitys,$order_ids_n,$order_data_n);
                }
                if(!empty($order_ids_y)){
                    //在审核时间外订单 审核通过
                    $ret_y = self::whereIn('order_id', $order_ids_y)->update(['roll_ratify' => 1,'use_mark'=>trans('handleOrder.handle_order_auto_pass')]);
                    //如果更新数量小于订单数量 则返回异常
                    if($ret_y < count($order_ids_y))  throw new \Exception( Response::generate('',Response::HANDLE_ORDER_Y_ERR));

                }

            }
           
        }
    }

    /**
     * 滚球投注 获取指定时间内未审核订单
     */
    public static function __getOrderData($time=60){

        $db = new \App\Models\Setinfo();
        //获取设定需审核时间 秒
        $handle_time =  $db->getInfo(1003)['infocontent']?:90;

        //获取需查询 时间条件 时间戳
        //时间区间 开始时间
        $time_unx_s = time() - $time - $handle_time;//-150 秒
        //时间区间 截止时间
        $time_unx_e = time() - $handle_time;//-90 秒

        //拼接查询条件
        $where = [
            ['money_time','>',date('Y-m-d H:i:s',$time_unx_s)],
            ['money_time','<',date('Y-m-d H:i:s',$time_unx_e)],
            ['roll_ratify','=',2]
        ];
        //获取 待处理订单数据
        $order_data = self::where($where)->SELECT('id','match_id','account_name','account_identity','order_id','money_time','roll_ratify','money','gain_money')->get()->toArray();
        
        if(!empty($order_data)){
           
            //获取订单所属赛事id
            $match_ids = [];
            foreach($order_data as $k=>$v){
                $match_ids[] = $v['match_id'];
            }

            //去重 获取赛事危险球数据
            $warn_json_data = StZqResultModel::whereIn('match_id',$match_ids)->SELECT('match_id','warn_more')->get()->toArray();

            //获取审核不通过订单 订单号
            $order_ids_n = [];
            //获取审核不通过订单
            $order_data_n = [];
            //获取审核通过订单 订单号
            $order_ids_y = [];
            //获取审核未通过订单 投注人
            $account_identitys = [];
            foreach($order_data as $k=>$v){
                //获取每个订单的危险球数据
                $warn_data = commonFunction::filter_by_value($warn_json_data,'match_id',$v['match_id']);
                $warn_data_arr = json_decode($warn_data['warn_more'],true);
                //投注时间 时间戳
                $order_time = strtotime($v['money_time']);
                foreach($warn_data_arr as $kk=>$vv){
                    //危险球时间 时间戳
                    $warn_time = strtotime($vv['timei']);
                    if($order_time < $warn_time and ($order_time+$handle_time) > $warn_time){
                        $order_ids_n[] = $v['order_id'];
                        $account_identitys[] = $v['account_identity'];
                        $order_data_n[] = $v;
                        unset($order_data[$k]);
                    }
                }
            }

            if(!empty($order_data)){
                foreach($order_data as $k=>$v){
                    $order_ids_y[] = $v['order_id'];
                }
            }

            if(!empty($order_ids_n)){
                //在审核时间内订单 审核不通过
                self::HandleMoney_v2($account_identitys,$order_ids_n,$order_data_n);
            }
            if(!empty($order_ids_y)){
                //在审核时间外订单 审核通过
                $ret_y = self::whereIn('order_id', $order_ids_y)->update(['roll_ratify' => 1,'use_mark'=>trans('handleOrder.handle_order_auto_pass')]);
                //如果更新数量小于订单数量 则返回异常
                if($ret_y < count($order_ids_y))  throw new \Exception( Response::generate('',Response::HANDLE_ORDER_Y_ERR));

            }

        }
      
    }
    public static function getOrderData($time=60){

        $db = new \App\Models\Setinfo();
        //获取设定需审核时间 秒
        $handle_time =  $db->getInfo(1003)['infocontent']?:90;

        //获取需查询 时间条件 时间戳
        //时间区间 截止时间
        $time_unx_e = time() - (int)$handle_time;//-90 秒
        //时间区间 开始时间
        $time_unx_s = $time_unx_e - $time;//-150 秒

        //拼接查询条件
        $where = [
            ['money_time','>',date('Y-m-d H:i:s',$time_unx_s)],//查询开始时间 大于 当前时间-审核延迟时间-执行间隔时间（默认60秒）
            ['money_time','<',date('Y-m-d H:i:s',$time_unx_e)],//查询结束时间 小于 当前时间-审核延迟时间
            ['roll_ratify','=',2],//查询  时间段内未审核 滚球投注 注单
            ['money_buy_simplex.status','=',1],//查询 时间段内注单 投注状态
        ];
        //获取 待处理订单数据
        $order_data = self::join('money_buy_match','money_buy_match.order_id','=','money_buy_simplex.order_id')
            // ->where(['money_buy_simplex.order_id'=>'S20191025111201242070491'])
            ->where($where)
            ->SELECT('money_buy_simplex.id','money_buy_simplex.match_id','money_buy_simplex.account_name','money_buy_simplex.account_identity','money_buy_simplex.order_id','money_buy_simplex.money_time','money_buy_simplex.roll_ratify','money_buy_simplex.money','money_buy_simplex.gain_money','money_buy_match.p_code')
            ->get()
            ->toArray();
        if(!empty($order_data)){
           
            //获取订单所属赛事id
            $match_ids = [];
            foreach($order_data as $k=>$v){
                $match_ids[] = $v['match_id'];
            }

            //去重 获取赛事危险球数据
            $warn_json_data = StZqResultModel::whereIn('match_id',$match_ids)->SELECT('match_id','warn_more')->get()->toArray();

            //获取审核不通过订单 订单号
            $order_ids_n = [];
            //获取审核不通过订单
            $order_data_n = [];
            //获取审核通过订单 订单号
            $order_ids_y = [];
            //获取审核未通过订单 投注人
            $account_identitys = [];
            foreach($order_data as $k=>$v){
                //获取每个订单的危险球数据
                $warn_data = commonFunction::filter_by_value($warn_json_data,'match_id',$v['match_id']);
                $warn_data_arr = json_decode($warn_data['warn_more'],true);
                //去除危险球默认数据
                foreach($warn_data_arr as $key=>$val){
                    if($val['rtype'] == 0){
                        unset($warn_data_arr[$key]);
                    }
                }
                sort($warn_data_arr);

                //当危险球为空，则无需审核，全部通过
                if(!empty($warn_data_arr)){
                    //投注时间 时间戳
                    $order_time = strtotime($v['money_time']);
                    foreach($warn_data_arr as $kk=>$vv){
                        //危险球时间 时间戳
                        $warn_time = strtotime($vv['timei']);
                        //危险球类型 可能的值：1 进球无效 2红卡无效 3无效（用于角球）
                        $warn_type = $vv['warn_type'];
                        //审核危险球类型
                        $v['use_mark'] = '未知类型';
                        //确认危险球审核条件
                        if($order_time < $warn_time and ($order_time+$handle_time) > $warn_time){
                            //根据投注 玩法 确定 危险球审核类型
                            if($v['p_code'] == 'CB' and $warn_type== 3){
                                $v['use_mark'] = '进球无效';
                            }else{
                                if($warn_type == 1){
                                    $v['use_mark'] = '进球无效';
                                }
                                if($warn_type == 2){
                                    $v['use_mark'] = '红卡无效';
                                }
                            }

                            $order_ids_n[] = $v['order_id'];
                            $account_identitys[] = $v['account_identity'];
                            $order_data_n[] = $v;
                            unset($order_data[$k]);
                        }
                    }
                }
            }
                

            if(!empty($order_data)){
                foreach($order_data as $k=>$v){
                    $order_ids_y[] = $v['order_id'];
                }
            }

            if(!empty($order_ids_n)){
                //在审核时间内订单 审核不通过
                self::HandleMoney_v2($account_identitys,$order_ids_n,$order_data_n);
            }
            if(!empty($order_ids_y)){
                //在审核时间外订单 审核通过
                $ret_y = self::whereIn('order_id', $order_ids_y)->update(['roll_ratify' => 1,'use_mark'=>trans('handleOrder.handle_order_auto_pass')]);
                //如果更新数量小于订单数量 则返回异常
                if($ret_y < count($order_ids_y))  throw new \Exception( Response::generate('',Response::HANDLE_ORDER_Y_ERR));

            }

        }
      
    }

    /**
     * 滚球自动审核 账户资金处理
     * v1 
     * 当用户下同时只有一个订单退款时 可用
     * 
     */
     public static function HandleMoney($account_identitys=[],$order_ids=[],$orders=[]){
         //用戶账户金额
        $account_money = \App\Models\Account_detailed::whereIn('account_identity',$account_identitys)->SELECT('account_identity','available_cash')->get()->toArray();
		//反水
        $water_return_money = \App\Models\Money_details::whereIn('trade_id',$order_ids)->where('trade_type', '7')->SELECT('trade_id','money')->get()->toArray();

		if(!empty($orders)){
			for($i=0;$i<count($orders);$i++){
				for($a=0;$a<count($account_money);$a++){
					if($orders[$i]['account_identity'] == $account_money[$a]['account_identity']){
						$available_cash = $account_money[$a]['available_cash'];
					}
				}
				for($b=0;$b<count($water_return_money);$b++){
					if($orders[$i]['order_id'] == $water_return_money[$b]['trade_id']){
						$water_return = $water_return_money[$b]['money'];
					} else {
						$water_return = 0;
					}
                }
                //需退回金额 = 投注金额-反水金额
                $money = abs($orders[$i]['money'] - $water_return);
                //用户剩余金额
				$new_available_cash = $available_cash + $money;

				$models = new \App\Models\Money_details();
				$models->info_identity = UUID();
				$models->trade_id = $orders[$i]['order_id'];
				$models->account_name = $orders[$i]['account_name'];
				$models->account_identity = $orders[$i]['account_identity'];
				$models->money = $money;
				$models->money_time = date("Y-m-d H:i:s", time());
				if ($new_available_cash > $available_cash) {
					$models->money_type = '1';
				} else {
					$models->money_type = '2';
				}
				$models->money_cash = $new_available_cash;
				$models->trade_type = '25';
				$models->trade_desc = trans('handleOrder.handle_order_auto_fail').',退回投注资金：￥'.$money.';订单号:'.$orders[$i]['order_id'].'';
                $models->status = '1';
                
                //更新订单数据
                $ret_order = self::where('id', $orders[$i]['id'])->update(['roll_ratify' => '-1','status' => '2','use_mark'=>trans('handleOrder.handle_order_auto_fail')]);
                if($ret_order < 1)  throw new \Exception( Response::generate('',Response::UP_ORDER_ERR));

                //更新用户数据
                $ret_user = \App\Models\Account_detailed::where('account_identity', $orders[$i]['account_identity'])->update(['available_cash' => $new_available_cash, 'cash' => $new_available_cash]);
                if($ret_user < 1)  throw new \Exception( Response::generate('',Response::UP_USER_ERR));

                //更新用户资金数据
                if(!$models->save()) throw new \Exception( Response::generate('',Response::UP_MONEY_ERR));
				
			}
        }
        
    }

    /**
     * 滚球自动审核 账户资金处理
     * v2 
     * 处理多用户下 多个注单退款时的 余额计算
     */
    public static function HandleMoney_v2($account_identitys=[],$order_ids=[],$orders=[],$type = 0){
       
        //获取  用户 初始余额
        $account_money = \App\Models\Account_detailed::whereIn('account_identity',$account_identitys)->SELECT('account_identity','available_cash')->get()->toArray();
        //获取 指定注单 反水金额
        $water_return_money = \App\Models\Money_details::whereIn('trade_id',$order_ids)->where('trade_type', '7')->SELECT('trade_id','money')->get()->toArray();

        if(!empty($account_money) and !empty($orders) and !empty($water_return_money)){
            //拼接每个注单 反水
            foreach($orders as $k=>$v){
                $v['fanshui'] = self::getFanshui($v['order_id'],$water_return_money);
                $orders[$k] = $v;

            }

            foreach($account_money as $k=>$v){
                //默认 余额 0
                $account_identity = 0;
                foreach($orders as $kk=>$vv){
                    //计算当前余额
                    if($v['account_identity'] == $vv['account_identity']){
                        //第一个注单 获取用户初始余额
                        if($account_identity == 0){
                            //当前注单余额
                            $vv['available_cash'] = $v['available_cash'];
                            //当前注单退款后的余额
                            $account_identity = $v['available_cash']+$vv['money']-$vv['fanshui'];
                        }else{//其他 获取 上个注单处理后的余额
                            $vv['available_cash'] = $account_identity;
                            $account_identity = $account_identity+$vv['money']-$vv['fanshui'];
                        }
                        $orders[$kk] = $vv;
                    }
                }
            }

            //滚球注单审核 处理
            if($type == 0){
                foreach($orders as $k=>$v){
                    //需退回金额 = 投注金额-反水金额
                    $money = abs($v['money'] - $v['fanshui']);
                    //用户剩余金额
                    $new_available_cash = $v['available_cash'] + $money;
                    $models = new \App\Models\Money_details();
                    $models->info_identity = UUID();
                    $models->trade_id = $v['order_id'];
                    $models->account_name = $v['account_name'];
                    $models->account_identity = $v['account_identity'];
                    $models->money = $money;
                    $models->money_time = date("Y-m-d H:i:s", time());
                    $models->money_type = '1';
                    
                    $models->money_cash = $new_available_cash;
                    $models->trade_type = '25';
                    $models->trade_desc = trans('handleOrder.handle_order_auto_fail').',退回投注资金：￥'.$money.';订单号:'.$v['order_id'].'';
                    $models->status = '1';
                
                    //更新订单数据
                    $ret_order = self::where('id', $v['id'])->update(['roll_ratify' => '-1','status' => '2','use_mark'=>$v['use_mark']]);//trans('handleOrder.handle_order_auto_fail')
                    if($ret_order < 1)  throw new \Exception( Response::generate('',Response::UP_ORDER_ERR));

                    //更新用户数据
                    $ret_user = \App\Models\Account_detailed::where('account_identity', $v['account_identity'])->update(['available_cash' => $new_available_cash, 'cash' => $new_available_cash]);
                    if($ret_user < 1)  throw new \Exception( Response::generate('',Response::UP_USER_ERR));

                    //更新用户资金数据
                    if(!$models->save()) throw new \Exception( Response::generate('',Response::UP_MONEY_ERR));

                }
            }
            //已取消赛事注单相关处理
            else{
                foreach($orders as $k=>$v){
                    //需退回金额 = 投注金额-反水金额
                    $money = abs($v['money'] - $v['fanshui']);
                    //用户剩余金额
                    $new_available_cash = $v['available_cash'] + $money;
                    $models = new \App\Models\Money_details();
                    $models->info_identity = UUID();
                    $models->trade_id = $v['order_id'];//交易id
                    $models->account_name = $v['account_name'];//用户名
                    $models->account_identity = $v['account_identity'];//用户uuid
                    $models->money = $money;//变动金额
                    $models->money_time = date("Y-m-d H:i:s", time());//资金发生时间
                    $models->money_type = '1';//1.增加 2.减少
                    
                    $models->money_cash = $new_available_cash;//剩余金额
                    $models->trade_type = '3';//交易类型 3撤单
                    $models->trade_desc = '因赛事取消,退回投注资金：￥'.$money.';订单号:'.$v['order_id'].'';//交易描述
                    $models->status = '1';//1成功 2失败
                
                    //更新订单数据
                    $ret_order = self::where('id', $v['id'])->update(['status' => '2','use_mark'=>'']);//因赛事取消，作废注单
                    if($ret_order < 1)  throw new \Exception( Response::generate('',Response::UP_ORDER_ERR));

                    //更新用户数据
                    $ret_user = \App\Models\Account_detailed::where('account_identity', $v['account_identity'])->update(['available_cash' => $new_available_cash, 'cash' => $new_available_cash]);
                    if($ret_user < 1)  throw new \Exception( Response::generate('',Response::UP_USER_ERR));

                    //更新用户资金数据
                    if(!$models->save()) throw new \Exception( Response::generate('',Response::UP_MONEY_ERR));
                }
            }

        }
   }

   /**
    * 获取注单 反水金额
    * $order_id 订单id str
    * $fanshuiData 反水数据 arr
    */
    public static function getFanshui($order_id='',$fanshuiData=[]){
        foreach($fanshuiData as $k=>$v){
            if($v['trade_id'] == $order_id){
                $fanshui = $v['money'];
            }
        }

        return $fanshui;

    }

    /**
     * 定时处理 已取消赛事相关注单
     * $time 查询时间 默认60秒
     */
    public static function HandleOrderInvalid($time = 60){
        //获取指定时间内已取消赛事
        //拼接查询条件
        $time_unx_max = time() - $time ;//查询最大时间 时间戳 5分钟前
        $time_unx_min = $time_unx_max - $time;//查询最小时间 时间戳 10分钟前

        $where = [
            ['utime','>',date('Y-m-d H:i:s',$time_unx_min)],
            ['utime','<',date('Y-m-d H:i:s',$time_unx_max)],
            ['status','=',6]
        ];
        //足球
        $ZqMatchData = ZqMatchModel::where($where)->SELECT('id')->get()->toArray();
        //篮球
        $LqMatchData = LqMatchModel::where($where)->SELECT('id')->get()->toArray();
        //网球
        $WqMatchData = WqMatchModel::where($where)->SELECT('id')->get()->toArray();
        //棒球
        $BqMatchData = BqMatchModel::where($where)->SELECT('id')->get()->toArray();
        
        //===取消注单处理===
		$moneyBuyMatch = new \App\Models\MoneyBuyMatch();

        //获取赛事下所有单式注单
        $select = ['money_buy_simplex.id','money_buy_simplex.match_id','money_buy_simplex.account_name','money_buy_simplex.account_identity','money_buy_simplex.order_id','money_buy_simplex.money_time','money_buy_simplex.roll_ratify','money_buy_simplex.money','money_buy_simplex.gain_money','money_buy_match.p_code'];
        //足球 单式注单
        $zq_match_order_sim = [];
        if(!empty($ZqMatchData)){
            $zqMatch_ids = array_column($ZqMatchData,'id');
            $zq_match_order_sim = $moneyBuyMatch->allsimplexorder('','zq',$select,$zqMatch_ids)->toArray();
            //将所有赛事下串式注单 作废 （设置为 平）
            \App\Models\MoneyBuyMatch::whereIn('match_id',$zqMatch_ids)->where('game_code','zq')->where('bet_type',2)->update(['result'=>2]);
        }
        //篮球 单式注单
        $lq_match_order_sim = [];
        if(!empty($LqMatchData)){
            $lqMatch_ids = array_column($LqMatchData,'id');
            $lq_match_order_sim = $moneyBuyMatch->allsimplexorder('','lq',$select,$lqMatch_ids)->toArray();
            //将所有赛事下串式注单 作废 （设置为 平）
            \App\Models\MoneyBuyMatch::whereIn('match_id',$lqMatch_ids)->where('game_code','lq')->where('bet_type',2)->update(['result'=>2]);
        }
        //网球 单式注单
        $wq_match_order_sim = [];
        if(!empty($WqMatchData)){
            $wqMatch_ids = array_column($WqMatchData,'id');
            $wq_match_order_sim = $moneyBuyMatch->allsimplexorder('','wq',$select,$wqMatch_ids)->toArray();
            //将所有赛事下串式注单 作废 （设置为 平）
            \App\Models\MoneyBuyMatch::whereIn('match_id',$wqMatch_ids)->where('game_code','wq')->where('bet_type',2)->update(['result'=>2]);
        }
        //棒球 单式注单
        $bq_match_order_sim = [];
        if(!empty($BqMatchData)){
            $bqMatch_ids = array_column($BqMatchData,'id');
            $bq_match_order_sim = $moneyBuyMatch->allsimplexorder('','bq',$select,$bqMatch_ids)->toArray();
            //将所有赛事下串式注单 作废 （设置为 平）
            \App\Models\MoneyBuyMatch::whereIn('match_id',$bqMatch_ids)->where('game_code','bq')->where('bet_type',2)->update(['result'=>2]);
        }

        //合并单式注单
        $order_sim = array_merge($zq_match_order_sim,$lq_match_order_sim,$wq_match_order_sim,$bq_match_order_sim);
        // dd($order_sim);
        
        //如果有单式注单
        if(!empty($order_sim)){
            //用户uuid
            $account_identitys = array_column($order_sim,'account_identity');
            //注单id
            $order_ids = array_column($order_sim,'order_id');
           
            //注单作废处理
            self::HandleMoney_v2($account_identitys,$order_ids,$order_sim,1);
        }
    }

    //获取单式注单id,match_id
    public function getsim($game_code){
        $sim = $this->select('id','match_id')->where('game_code',$game_code)->get();
        return $sim;
    }

    //取消结算 扣除中奖金额
    public function no_cancel_end($order_id){
        //获取注单数据
        $orderData = $this->where('order_id',$order_id)->first();
        if(empty($orderData)) throw new \Exception('未找到该注单');
        //投注用户
        $account_name = $orderData->account_name;
        //用户uuid = 
        $account_identity = $orderData->account_identity;
        //获取注单中奖金额
        $gain_money = $orderData->gain_money;
        //获取注单状态
        $status = $orderData->status;
        //获取结算状态
        $settle_status = $orderData->settle_status;

        //如果注单状态不是投注-已结算
        if($status != 1 and $settle_status < 2) throw new \Exception('注单状态错误');

        //获取用户账户余额
        $account_money = \App\Models\Account_detailed::where('account_identity',$account_identity)->SELECT('account_identity','available_cash')->first()->available_cash;
    
        //===处理用户资金相关===
         //需扣除金额 = 中奖金额
         $money = $gain_money;
         //用户剩余金额
         $new_available_cash = $account_money - $money;
         $models = new \App\Models\Money_details();
         $models->info_identity = UUID();
         $models->trade_id = $order_id;//交易id
         $models->account_name = $account_name;//用户名
         $models->account_identity = $account_identity;//用户uuid
         $models->money = $money;//变动金额
         $models->money_time = date("Y-m-d H:i:s", time());//资金发生时间
         $models->money_type = '2';//1.增加 2.减少
         
         $models->money_cash = $new_available_cash;//剩余金额
         $models->trade_type = '26';//交易类型 26 已结算改为未结算 扣除中奖金额
         $models->trade_desc = '已结算改为未结算，扣除中奖金额：￥'.$money.';订单号:'.$order_id.'';//交易描述
         $models->status = '1';//1成功 2失败

         //新增资金流水
         if(!$models->save()) throw new \Exception('新增资金流水失败');

         //更新用户资金
         $ret_user = \App\Models\Account_detailed::where('account_identity',$account_identity)->update(['available_cash' => $new_available_cash, 'cash' => $new_available_cash]);
         if($ret_user < 1)  throw new \Exception('更新用户资金失败');
     
         //更新订单数据
         $order_up = [
             'gain_money' =>0,//中奖金额 0
             'settle_status' =>1,//结算状态 未结算
             'game_status' =>0,//开奖状态 未开奖
             'remark' =>'',//处理备注
         ];
         $ret_order = self::where('order_id',$order_id)->update($order_up);//将注单已结算改为未结算
         if($ret_order < 1)  throw new \Exception('注单数据更新失败');

         //更新投注详情数据
         $match_up = [
             'status'=>0,//结算状态 0 未结算
             'result'=>0,//结果状态 0 未处理
             'utime'=>date("Y-m-d H:i:s", time()),//更新时间
             'matchresult'=>'',//结算结果 默认空

         ];
         $ret_money_buy_match = \App\Models\MoneyBuyMatch::where('order_id',$order_id)->update($match_up);
         if($ret_money_buy_match < 1)  throw new \Exception('注单详情投注数据更新失败');
         //===end===

         return 1;
    }

    //取消作废 扣除投注金额 追加反水金额
    public function no_cancel_invalid($order_id){
         //获取注单数据
         $orderData = $this->where('order_id',$order_id)->first();
         if(empty($orderData)) throw new \Exception('未找到该注单');
         //投注用户
         $account_name = $orderData->account_name;
         //用户uuid = 
         $account_identity = $orderData->account_identity;
         
         //获取注单状态
         $status = $orderData->status;
         //获取投注金额
         $order_money = $orderData->money;
        
         //如果注单状态不是作废
         if($status < 2) throw new \Exception('注单状态错误');
 
         //获取用户账户余额
         $account_money = \App\Models\Account_detailed::where('account_identity',$account_identity)->SELECT('account_identity','available_cash')->first()->available_cash;
     
         //===处理用户资金相关===
         //===扣除投注金额===
          //需扣除金额 = 投注金额
          $money = $order_money;
          //用户剩余金额
          $new_available_cash = $account_money - $money;
          $models = new \App\Models\Money_details();
          $models->info_identity = UUID();
          $models->trade_id = $order_id;//交易id
          $models->account_name = $account_name;//用户名
          $models->account_identity = $account_identity;//用户uuid
          $models->money = $money;//变动金额
          $models->money_time = date("Y-m-d H:i:s", time());//资金发生时间
          $models->money_type = '2';//1.增加 2.减少
          
          $models->money_cash = $new_available_cash;//剩余金额
          $models->trade_type = '1';//交易类型 1 已作废改为未结算，扣除投注金额
          $models->trade_desc = '已作废改为未结算，扣除投注金额：￥'.$money.';订单号:'.$order_id.'';//交易描述
          $models->status = '1';//1成功 2失败
 
          //新增资金流水
          if(!$models->save()) throw new \Exception('新增资金流水失败');
 
          //更新用户资金
          $ret_user = \App\Models\Account_detailed::where('account_identity',$account_identity)->update(['available_cash' => $new_available_cash, 'cash' => $new_available_cash]);
          if($ret_user < 1)  throw new \Exception('更新用户资金失败');
      
          //更新订单数据
          $order_up = [
              'status'=>1,//已作废改为投注
              'gain_money' =>0,//中奖金额 0
              'settle_status' =>1,//结算状态 未结算
              'game_status' =>0,//开奖状态 未开奖
              'remark' =>'',//处理备注
              'use_mark' => '',//作废备注为空
          ];
          $ret_order = self::where('order_id',$order_id)->update($order_up);//将注单已作废改为未结算
          if($ret_order < 1)  throw new \Exception('注单数据更新失败');
 
          //更新投注详情数据
          $match_up = [
              'status'=>0,//结算状态 0 未结算
              'result'=>0,//结果状态 0 未处理
              'utime'=>date("Y-m-d H:i:s", time()),//更新时间
              'matchresult'=>'',//结算结果 默认空
          ];
          $ret_money_buy_match = \App\Models\MoneyBuyMatch::where('order_id',$order_id)->update($match_up);
          if($ret_money_buy_match < 1)  throw new \Exception('注单详情投注数据更新失败');
          //===end===

          //追加用户投注反水
          //获取反水比例
          $water_return =  \App\Models\Setinfo::where('infotype',1001)->first()->infocontent;
          //反水金额 = 投注金额*反水比例
          $water_return_money = $water_return*$order_money;

            //需追加金额 = 反水金额
            $money = $water_return_money;
            //用户剩余金额
            $new_available_cash = $new_available_cash + $money;
            $models = new \App\Models\Money_details();
            $models->info_identity = UUID();
            $models->trade_id = $order_id;//交易id
            $models->account_name = $account_name;//用户名
            $models->account_identity = $account_identity;//用户uuid
            $models->money = $money;//变动金额
            $models->money_time = date("Y-m-d H:i:s", time());//资金发生时间
            $models->money_type = '1';//1.增加 2.减少
            
            $models->money_cash = $new_available_cash;//剩余金额
            $models->trade_type = '1';//交易类型 1 已作废改为未结算，扣除投注金额
            $models->trade_desc = '已作废改为未结算，追加反水金额：￥'.$money.';订单号:'.$order_id.'';//交易描述
            $models->status = '1';//1成功 2失败
   
            //新增资金流水
            if(!$models->save()) throw new \Exception('新增资金流水失败');
   
            //更新用户资金
            $ret_user = \App\Models\Account_detailed::where('account_identity',$account_identity)->update(['available_cash' => $new_available_cash, 'cash' => $new_available_cash]);
            if($ret_user < 1)  throw new \Exception('更新用户资金失败');
        
          return 1;
    }
}