<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/7
 * Time: 20:21
 */

namespace App\Lib\Settlement;

//后台结算，输赢结算接口
/*
 * 返回  status=1 成功 其它失败
 * Array
(
    [status] => 1
    [msg] => succes
    [data] => Array
        (
            [cost] => 0.035570859909058
        )
)
 *
 * */


class SwInterface
{
    private static $Instance = null;
    static $urlBase = '';
    static $token = 'oclatv15689731035d84a12f550df';

    public function debug()
    {
        return ['url' => self::$urlBase, 'token' => self::$token];
    }

    public static function getInstance($cache = true)
    {
        if ($cache && self::$Instance) {
            return self::$Instance;
        }
        self::$Instance = new self();
        self::$urlBase = config('sconstant.url');
        if (session('adminInfo.token')) {
            self::$token = session('adminInfo.token');
        }
        return self::$Instance;
    }

    //按赛事计算输赢  $noticeId 比赛结束通知表commendnotice表的ID值
    public function MatchWinFail($noticeId)
    {
        $url = self::$urlBase . '/WinFail';
        $data = [
            'token' => self::$token,
            'noticeid' => $noticeId,
        ];
        $ret = json_decode($this->request_post($url, $data), true);
        return $ret;
    }

    //手动计算某个订单输赢
    public function WinFailHandOrder($order_id)
    {
        $url = self::$urlBase . '/DoWinFailOneOrder';
        $data = [
            'token' => self::$token,
            'order_id' => $order_id,
        ];
        $ret = json_decode($this->request_post($url, $data), true);
        return $ret;
    }

    //普通 计算某个订单输赢   $bet_type 1单式  2串式
    public function WinFailNomalOrder($order_id, $bet_type)
    {
        $url = self::$urlBase . '/WinfailoneNomal';
        $data = [
            'token' => self::$token,
            'order_id' => $order_id,
            'bet_type' => $bet_type,
        ];
        $ret = json_decode($this->request_post($url, $data), true);
        return $ret;
    }

    //结算处理接口  注意只能同一赛事的订单(区分单式串式) $order_ids为空表示单式全部或串式全部  $change_statuc全部时是否更新状态 $is_manurl是否手动结果(单条单式才动结算时为1)
    public function Setelement($game_code, $match_id, $bettype, $order_ids = '', $change_statuc = 0, $is_manurl = 0)
    {
        $url = self::$urlBase . '/Settelement';
        $data = [
            'token' => self::$token,
            'game_code' => $game_code,
            'match_id' => $match_id,
            'change_status' => $change_statuc,
            'is_manual' => $is_manurl,
            'settype' => 2,
            'bettype' => $bettype,
            'order_ids' => $order_ids,
        ];
        $ret = json_decode($this->request_post($url, $data), true);
        return $ret;
    }


    //撤销一个串式订单
    public function UnsetOneStringOrder($game_code, $match_id, $order_id)
    {
        $url = self::$urlBase . '/UnsetOneStringOrder';
        $data = [
            'token' => self::$token,
            'game_code' => $game_code,
            'match_id' => $match_id,
            'order_id' => $order_id
        ];
        $ret = json_decode($this->request_post($url, $data), true);
        return $ret;
    }


    //把一个串式  撤销订单改为重下单状态
    public function ResStringOneOrder($game_code, $match_id, $order_id)
    {
        $url = self::$urlBase . '/ResStringOneOrder';
        $data = [
            'token' => self::$token,
            'game_code' => $game_code,
            'match_id' => $match_id,
            'order_id' => $order_id
        ];
        $ret = json_decode($this->request_post($url, $data), true);
        return $ret;
    }

    //取消一个订单
    public function CancelOneOrder($game_code, $match_id, $order_id)
    {
        $url = self::$urlBase . '/UnsetOneOrder';
        $data = [
            'token' => self::$token,
            'game_code' => $game_code,
            'match_id' => $match_id,
            'order_id' => $order_id
        ];
        $ret = json_decode($this->request_post($url, $data), true);
        return $ret;
    }


    public function request_post($url = '', $param = '')
    {
        if (empty($url) || empty($param)) {
            return false;
        }

        $postUrl = $url;
        $curlPost = $param;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $data = curl_exec($ch);//运行curl
        curl_close($ch);

        return $data;
    }

}