<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use app\utils\RedisKeyEnum;
use app\utils\RedisUtil;
/**
 * 选手
 */
class Player extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];



    //给选手投票
    public function record(){
        $this->error('接口作废');
        if(!$this->apiLimit('操作太快了,休息一下吧'));

        $player_id = input('player_id','');
        $vote      = input('vote',1);
        if(!$player_id){
            $this->error();
        }

        //登录用户票数的检查
        $user_id = $this->auth->id;
        //今天,投了几票
        $today_record   = RedisUtil::getInstance(RedisKeyEnum::VOTE_RECORD.date('Y-m-d').':'.$user_id)->get();
        //今天，用户答对的次数，也就是答题获得的票数
        $question_vote  = RedisUtil::getInstance(RedisKeyEnum::EAXM_RIGHT.date('Y-m-d').':'.$user_id)->get();
        //今天，用户答题的次数
        $today_question = RedisUtil::getInstance(RedisKeyEnum::EAXM_TIMES.date('Y-m-d').':'.$user_id)->get();
        //今天,免费的的票
        $gift_votes = config('site.gift_votes_user_eday');
        //今天,免费的答题次数
        $exam_times = config('site.exam_times_user_eday');

        //免费票 + 答对次数 - 已投票次数 < vote
        $times = $gift_votes + $question_vote - $today_record;
        if($times < 0){$times = 0;}

        //票数不够
        if($times < $vote){
            $msg = '投票失败,您今日剩余投票次数: '.$times.'次';

            //剩余投票次数，剩余答题次数
            $result = [
                'uservote' => $times,
                'userquestion' => $exam_times - $today_question,
            ];

            $this->success($msg,$result);
        }


        Db::startTrans();

        //检查选手
        $player_info = Db::name('vote_player')->field('id,subject_id,votes')->where(['id'=>$player_id])->lock(true)->find();
        if(!$player_info){
            Db::rollback();
            $this->error('不存在的选手');
        }

        //给选手加票
        $update_data = [
            'votes' => $player_info['votes'] + $vote,
        ];
        $update_rs = Db::name('vote_player')->where('id',$player_id)->update($update_data);
        if($update_rs === false){
            Db::rollback();
            $this->error('投票失败');
        }

        //日志
        $data = [
            'user_id' => $this->auth->id,
            'subject_id' => $player_info['subject_id'],
            'player_id' => $player_id,
            'vote' => $vote,
            'createdate' => strtotime(date('Y-m-d')),
            'createtime' => time(),
        ];
        $log_id = Db::name('vote_record')->insertGetId($data);
        if(!$log_id){
            Db::rollback();
            $this->error('投票失败');
        }

        Db::commit();

        //今日投票次数，自增一次
        $today_record = RedisUtil::getInstance(RedisKeyEnum::VOTE_RECORD.date('Y-m-d').':'.$this->auth->id)->incrby_expire($vote,86400);


        //免费票 + 答对次数 - 已投票次数
        $times = $gift_votes + $question_vote - $today_record;
        if($times < 0){$times = 0;}

        //
        $msg = '投票成功,您今日剩余投票次数: '.$times.'次';

        //剩余投票次数，剩余答题次数
        $result = [
            'uservote' => $times,
            'userquestion' => $exam_times - $today_question,
        ];

        $this->success($msg,$result);

    }






}
