<?php
namespace app\api\controller;
use app\common\controller\Authentication;
use app\common\model\QuestionModel;
use app\common\model\RoleModel;
use app\common\model\RoleListModel;
use \think\Db;

class Role extends Authentication{
    
    private static $M_role,$M_rolelist;
    public function __construct(){
        parent::__construct();
        $this->init();
    }
    private function init(){
        self::$M_role = new RoleModel();
        self::$M_rolelist = new RoleListModel();
    }
    private function update_account_status($account_id){
        Db::execute("update jr_account_role set current = 0 where account_id = " . $account_id);
    }
    // 保存当前用户角色
    public function add(){
        $post = input();
        $result['status'] = -1;
        $where['account_id'] = $post['account_id'];
        $where['grade'] = $post['grade'];
        $info = self::$M_role->get_role_by_where($where);

        // 将所有年级置为非当前
        $this->update_account_status($post['account_id']);
        
        if( ! $info ){
            $result = ['account_id'=>$post['account_id'],'grade'=>$post['grade'],'score' => 0,'lv' => 0];
            $result['status'] = $post['status'];
            $result['current'] = 1;
            $role = new RoleModel($result);
            $role->save();
            $result['id'] = $role->id;
        }else{
            if(count($info) > 0){
                $where['id'] = $info[0]->id;
            }
            $info = self::$M_role->get( $where );
            $info['current'] = 1;
            $info->save();
            $result = $info;
        }
        return json($result);
    }
    // 返回某个用户的所有段位
    public function get_list(){
        $where['account_id'] = input('account_id');
        $where['account_role_id'] = input('account_role_id');
        $where['status'] = 1;
        $list = self::$M_rolelist->get_role_list_by_where($where);
        return json($list);
    }
    // 新增单题结果
    public function add_list(){
        $post = input();
        $result = [];
        // 获取用户所选年级
        $result['id'] = self::$M_rolelist->add( $post );
        // 更新年级总分
        $role = self::$M_role->get_role_by_id(['id'=>$post['account_role_id']]);
        if($post['status']){
            $role->lv += 1;
            $role->score += $post['score'];
        }
        $role->save();
        return json($role);
    }
    // 重新闯关
    public function reset_pass(){
        $where['id'] = input('id');
        $role = self::$M_role->get_role_by_id($where);
        $role->current = 0;
        $role->status = input('status');
        $role->save();
        //copy
        $data = input();
        unset($data['id']);
        $data['status'] = 'pass';
        $data['lv'] = 0;
        $data['score'] = 0;
        $info = new RoleModel($data);
        $info->save();

        return json($info);
    }
    // 根据ID返回具体某个年级
    public function get_role(){
        $where['id'] = input('role_id');
        $info = self::$M_role->get_role_by_id($where);
        return json($info);
    }
}
