<?php

namespace App\Http\Controllers\Api;

use Illuminate\Routing\Controller as BaseController;
use App\Http\Response\Response;
use Illuminate\Http\Request as Req;
use Illuminate\Support\Facades\DB;
use App\Lib\Biz\Sport\Common as commonFunction;
use App\Http\Model\StZqMatch as StMatchModel;
use App\Models\SportsNoteList as SportsNoteListModel;
use \App\Models\System_user;

/**
 * 定时处理任务
 * Tank
 * 2019/10/31
 */
class CrontabController extends BaseController{
    //是否启用 数据写入记录
    protected $isRecord;

    public function __construct()
    {
        $this->isRecord = config('record.isRecord');
    }

    /**
     * 定时执行 滚球投注危险球审核
     * 每分钟执行一次1
     */
    public function HandleOrder(Req $data){
        //验证token
        $user = new System_user;
        if (empty($data->token)) return Response::generate('未获取到token', Response::TOKEN_ERR);
        $token = $user::where(['token' => $data->token])->value('token');
        if (empty($token)) return Response::generate('token验证不通过', Response::TOKEN_ERR);

        try {
            //开启事务
            DB::beginTransaction();
            $time =60*5;
            //处理指定时间内的滚球待审核订单
            SportsNoteListModel::getOrderData($time);
            //提交事务
            DB::commit();
            //写请求数据 日志记录
            if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('zq', [], 'HandleOrder', Response::success());
            return Response::success();
        } catch (\Exception $e) {
            //回滚事务
            DB::rollBack();
            //写请求数据 日志记录
            if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('zq', [], 'HandleOrder', $e->getMessage());
            return $e->getMessage();
        }

    }

    /**
     * 定时执行 处理到时间应该开始而未开始的赛事
     * 每分钟执行一次
     */
    public function HandleMatch(Req $data){
         //验证token
        $user = new System_user;
        if (empty($data->token)) return Response::generate('未获取到token', Response::TOKEN_ERR);
        $token = $user::where(['token' => $data->token])->value('token');
        if (empty($token)) return Response::generate('token验证不通过', Response::TOKEN_ERR);

        try {
            //开启事务
            DB::beginTransaction();
            $time = 60*60*24;//60 秒
            //处理指定时间内的赛事
            StMatchModel::HandleMatch($time);
            //提交事务
            DB::commit();
            //写请求数据 日志记录
            if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('all', [], 'HandleMatch', Response::success());
            return Response::success();
        } catch (\Exception $e) {
            //回滚事务
            DB::rollBack();
            //写请求数据 日志记录
            if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('all', [], 'HandleMatch', $e->getMessage());
            return $e->getMessage();
        }
    }

    /**
     * 定时执行 处理时间段内赛事已取消的注单1
     */
    public function HandleOrderInvalid(Req $data){
        //验证token
        $user = new System_user;
        if (empty($data->token)) return Response::generate('未获取到token', Response::TOKEN_ERR);
        $token = $user::where(['token' => $data->token])->value('token');
        if (empty($token)) return Response::generate('token验证不通过', Response::TOKEN_ERR);

        try {
            //开启事务
            DB::beginTransaction();
            $time = 60*5;//5分钟
            //处理指定时间内已取消赛事的注单
            SportsNoteListModel::HandleOrderInvalid($time);
            //提交事务
            DB::commit();
            //写请求数据 日志记录
            if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('all', [], 'HandleOrderInvalid', Response::success());
            return Response::success();
        } catch (\Exception $e) {
            //回滚事务
            DB::rollBack();
            //写请求数据 日志记录
            if ($this->isRecord) commonFunction::SET_SPORTS_RECORD('all', [], 'HandleOrderInvalid', $e->getMessage());
            return $e->getMessage();
        }
    }
}
