<?php

namespace App\Http\Controllers\Api;

use App\Http\Model\StSettlementMiddleDetail;
use App\Lib\Settlement\Adapter\RulePlus;
use App\Lib\Settlement\SettlementOrder;
use App\Lib\Settlement\Adapter\ZqRule;


use App\Lib\Settlement\SettlementWinFail;
use  Illuminate\Routing\Controller as Controller;
use function MongoDB\BSON\toJSON;
use Illuminate\Support\Facades\DB;


class SettlementController extends Controller
{
    use RulePlus;
    public function index()
    {
        $dd = new ZqRule();
        dd($dd->bodan(1,2,1));
    }

    public function dd()
    {
        $obj = new SettlementOrder();
        $ret = $obj->stringComputing(1);
        var_dump($ret);
    }

    public function debug()
    {

        $rule = new ZqRule();
        $model = DB::table('money_buy_match')->where(['id' => 320])->first();
        $reco = DB::table('st_zq_result')->where(['id' => 1317823])->get();

        print_r([$model,$reco]);
        $ret = $rule->tema_ball($model, $reco, []);
        //corner_concede_home($model, $reco, []);
        print_r($ret);


        exit;









        $cond = '+2.5/3';
        $cond = '+3/3.5';

        $ret = $this->zq_letball(3,$cond,'xxx');
        print_r($ret);
        exit ;




        /*
        $rule = new ZqRule();
        $model = DB::table('money_buy_match')->where(['id' => 320])->first();
        $reco = DB::table('st_zq_result')->where(['id' => 1317823])->get();
        print_r([$model,$reco]);
        $ret = $rule->first_ball_guest($model, $reco, []);
        print_r($ret);

        exit ;
        */



        $noticeid = 3592444;
        $matchid = 3217130;
        $order_ids = $this->getIds(1, $matchid,'zq');


        $winorfalse = new  SettlementWinFail();
        $ret1 = $winorfalse->doRun($noticeid);
        print_r(['ret1' => $ret1]);

        if ($ret1['status'] == 1) {
            $setOrder = new SettlementOrder();
            $ret2 = $setOrder->reSettlement($order_ids, 1, 2, 'zq', $matchid);
            print_r(['ret2' => $ret2]);
        }
        exit;




        $model = DB::table('st_zq_result')->where(['id' => 1317823])->first();
        echo json_decode($model->corner_ball);


        exit;



    }


    private function getIds($type, $match_id, $game_code='')
    {
        $return  = [] ;
        if ($type == 1) {
            $ret = DB::table('money_buy_simplex')->where(['match_id' => $match_id])->get();
            if ($ret){
                foreach ($ret as $val){
                    $return[] = $val->order_id;
                }
            }
            return $return;
        }else{
            $ret = DB::table('money_buy_str')->leftJoin('money_buy_match','money_buy_match.batch_id','=','money_buy_str.batch_id')->where(['money_buy_match.match_id'=>$match_id,'money_buy_match.game_code'=>$game_code])->get();
            if ($ret){
                foreach ($ret as $val){
                    $return[] = $val->order_id;
                }
            }
            return $return ;
        }
    }

}