<?php

namespace App\Http\Controllers\Api;

use  Illuminate\Routing\Controller as Controller;
use  App\Lib\Settlement\SettlementAuto;
use Illuminate\Support\Facades\DB;
use App\Lib\Biz\Sport\Common;


class SettlementAutoController extends Controller
{
    public function doAddNotie()
    {
        if (!$this->safecheck()) {
            Common::SET_NOMAM_RECORD('自动结算验证失败(doaddnotice) ', array_merge($_REQUEST, ['ip' => $_SERVER['REMOTE_ADDR']]));
            exit();
        }

        $obj = new SettlementAuto();
        $obj->doAddNotie();
        return 'ok';
    }

    public function doAutoSett()
    {
        if (!$this->safecheck()) {
            Common::SET_NOMAM_RECORD('自动结算验证失败(doAutoSett) ', array_merge($_REQUEST, ['ip' => $_SERVER['REMOTE_ADDR']]));
            exit();
        }

        $obj = new SettlementAuto();
        $obj->doAutoSett($_REQUEST['token']);
        return 'ok';
    }


    private function safecheck()
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        $ipreg = "/(^127\.0\.0\.1$)|(^172\.18\.0\.\d{1,3}$)|(^172\.17\.0\.\d{1,3}$)|(^103\.108\.41\.11\d$)/";
        $token = $_REQUEST['token'];

        $ret = preg_match($ipreg, $ip);
        if (!$ret) {
            return false;
        }

        $us = DB::table('system_user')->where(['token' => $token])->first();
        if (!$us) {
            return false;
        }

        return true;
    }


}