<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/25
 * Time: 14:10
 */

namespace App\Lib\Settlement;
set_time_limit(600);
ini_set('memory_limit', '256M');

use Illuminate\Support\Facades\DB;
use App\Lib\Settlement\SettlementBase;

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


/**
 * 自动结算相关功能
 */
class SettlementAuto
{
    public function doAddNotie()
    {
        $model = new ComendnoticeModel();
        $ret = $model->InsertEndGame();
        return $ret;
    }


    public function doAutoSett($token = '')
    {
        $ccmodel = new ComendnoticeModel();
        $model = $ccmodel->getNeedSettelent();
        if (empty($model)) {
            return 0;
        }
        Common::SET_NOMAM_RECORD('开始自动结算! ', ['id' => $model->id, 'match_id' => $model->match_id]);

        $model->pcount = 1;
        $model->start_time = date('Y-m-d H:i:s', time());
        $model->status = 1;
        $model->save();

        $mbmm = (new MoneyBuyMatchModel())->countByMatch($model->match_id,$model->game_code);
        $count = $mbmm['count'];
        if (!$count) {
            $model->status = 4;
            $model->save();
            $this->cgStatus($model->game_code, $model->match_id);
            Common::SET_NOMAM_RECORD('结束自动结算(无match记录)! ', ['id' => $model->id, 'match_id' => $model->match_id]);
            return 0;
        }

        $winret = $this->SubmitSettelement('w', ['noticeid' => $model->id, 'token' => $token]);
        if ($winret) {
            $dataComm = [
                'match_id' => $model->match_id,
                'game_code' => $model->game_code,
                'change_status' => 1,
                'settype' => 2,
                'is_manaue' => 0,
                'order_ids' => '',
                'token' => $token
            ];
            if ($mbmm['bet1'] > 0) {
                $this->SubmitSettelement('s', array_merge($dataComm, ['bettype' => 1]));
            }
            if ($mbmm['bet2'] > 0) {
                $this->SubmitSettelement('s', array_merge($dataComm, ['bettype' => 2]));
            }
            return true;
        }
        return false;
    }


    //提交结算
    public function SubmitSettelement($type, $datas)
    {
        $url = config('sconstant.url');
        $wsurls = ['w' => '/WinFail', 's' => '/Settelement'];

        //$token = 'oclatv15689731035d84a12f550df';
        //$datas = array_merge(['token' => $token], $datas);

        $fullurl = $url . $wsurls[$type];
        $ret = request_post($fullurl, $datas);
        if (!empty($ret)) {
            $retarr = json_decode($ret, true);
            Common::SET_NOMAM_RECORD('自动结算返回： ', ['type' => $type, 'req' => $datas, 'rt' => $retarr]);
            if (isset($retarr['status']) && $retarr['status'] == 1) {
                return true;
            }
        }
        return false;
    }

    function cgStatus($game_code, $match_id)
    {
        $table1 = "st_" . $game_code . "_result";
        $table2 = "st_" . $game_code . "_competition";

        DB::update("update $table1 set  status=3  where   match_id=$match_id ");
        DB::update("update $table2 set  status=3  where   id=$match_id ");

        return true;
    }


}