<?php

namespace App\Http\Controllers\Api;

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

use Illuminate\Http\Request as Req;

use App\Lib\Settlement\SettlementWinFail;
use  Illuminate\Routing\Controller as Controller;
use Illuminate\Support\Facades\DB;
use App\Lib\Biz\Sport\WagentProxy;

use App\Models\Comendnotice as ComendnoticeModel;
use App\Models\MoneyBuyMatch as MoneyBuyMatchModel;
use App\Lib\Biz\Sport\Common;


class SettlementController extends Controller
{
    use RulePlus;

    public function AutoSett()
    {


    }

    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(Req $req)
    {


        $model = new ComendnoticeModel();
        $need = $model->getNeedSettelent() ;
        var_dump($need);
        exit ;



        $auto = new SettlementAuto();
        $ret =$auto->doAutoSett('oclatv15689731035d84a12f550df');
        var_dump($ret);
        //return;
        //print_r($_SERVER);
        exit;

        $model = new MoneyBuyMatchModel();
        $ret = $model->countByMatch(758);
        print_r($ret);

        exit;


        /*
        $model = new  \App\Models\StoddsCode();
        $aa =$model->getTypeDatas('zq');
        print_r($aa);
        return ;
         */


        $order_id = isset($req->order_id) ? $req->order_id : 0;
        $model = new  \App\Models\SportsNoteList();
        $ret = $model->getorder($order_id);

        if ($ret) {
            $ret = $ret->toArray();
        }

        print_r($ret);
        exit;

        return responseToJson(1, '成功', $ret);


        return;
        $match = DB::table('money_buy_match')->where(['batch_id' => '20190916094717519851520'])->first();
        $result = DB::table('st_zq_result')->where(['match_id' => '2115'])->get();
        //$result = DB::table('st_wq_result')->where(['id' => '20'])->get();
        $rule = new  ZqRule();
        $resultb = $rule->GunqiuWeixian($match, $result, []);

        print_r($match);
        print_r($result);
        print_r($resultb);
        return;


        //print_r([$match, $result]);
        // return ;

        //$resultb = $rule->bq_getResultBySn($result, 1,1);
        //print_r($resultb);
        //return;

        $resultb = $rule->wq_scoreDatas($result);
        print_r($resultb);


        exit;


        return;
        $datas = DB::table("dc_navs")->select("id", "name", "href", "type")->get();
        $r = trans('menu');
        foreach ($datas as $val) {
            $tname = trim($val->name);
            if (isset($r[$tname])) {
                if ($tname != $r[$tname]) {
                    DB::table("dc_navs")->where("id", $val->id)->update(["name" => $r[$tname]]);
                }
            }
        }

        echo "ok";
        return;

        $match = DB::table('money_buy_match')->where(['batch_id' => '15671599105'])->first();
        $result = DB::table('st_zq_result')->where(['match_id' => '1'])->get();
        $rule = new  ZqRule();

        //print_r([$match,$result]);exit ;

        $result = $rule->B($match, $result, []);
        print_r($result);


        exit;


        ///$config = ['name' => 'test1agent', 'key' => 'abcdefg', 'secret' => 'asdfasdfasd', 'domain' => 'http://sports.bocai108.com', 'cachefile' => ''];
        //$config = ['name' => 'test1agent', 'key' => 'abcdefg', 'secret' => 'asdfasdfasd', 'domain' => 'http://www.kaiyou-dev.com'];
        $config = ['name' => 'test1agent', 'key' => 'abcdefg', 'secret' => 'asdfasdfasd'];
        $proxy = new WagentProxy();
        $proxy->Init($config);

        $ret1 = $proxy->todaycr();
        print_r($ret1);


        //$ret2 = $proxy->caie('onmygod');
        //print_r($ret2);


        exit;


        $ret1 = DB::table('mtest')->get();
        print_r($ret1);

        $new = DB::table('mtest')->insert(['id' => rand(10000, 20000), 'cont' => json_encode(['name' => rand(100, 200), 'age' => rand(10, 30)])]);


        echo "asfa";
        exit;


        /*
        $pdo = DB::getPdo();
        $mict = microtime(true);
        $sql = [
            "update  logfile   set   url='$mict', device_number='3610373'  where id=3610373",
            "update  logfile   set   url='$mict', device_number='3610374'  where id=3610374",
            "update  logfile   set   url='$mict', device_number='3610375'  where id=3610375"
        ];
        $sqlStr = implode(";",$sql);
        $ret = $pdo->exec($sqlStr);
        var_dump($ret);

        exit ;
        */


        /*
            $id = 3 ;
            $obj = new  SettlementWinFail();
            $ret = $obj->doRun($id);
            print_r($ret);
            echo "cost:" . (microtime(true) - floatval($_SERVER['REQUEST_TIME_FLOAT'])) .' 秒!';
            exit;
        */


        $oida = $this->getIds(1, 455, 'zq');
        $swf = new SettlementOrder();
        $ret = $swf->reSettlement($oida, 1, 2, 'zq', 455);
        var_dump($ret);
        echo "<hr/>";
        echo "cost:" . (microtime(true) - floatval($_SERVER['REQUEST_TIME_FLOAT'])) . ' 秒!';

        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 = 900;
        $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;
        }
    }

}