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


class Account extends Authentication{
    
    private static $M_account,$M_role,$M_rolelist;
    public function __construct(){
        parent::__construct();
        $this->init();
    }
    public function test(){
        exit('test');
    }

    private function init(){
        self::$M_account = new AccountModel();
        self::$M_role = new RoleModel();
        self::$M_rolelist = new RoleListModel();
    }
    // 检测用户是否是首次登陆并存储用户
    public function add_or_get(){
        $post = input();
        $result['account_id'] = -1;
        $info = self::$M_account->get_by_openid($post['openid']);
        // var_dump($info);
        if( !$info ){
            $data = [
                        'openid'=>$post['openid'],
                        'nickname'=>$post['nickName'],
                        'avatarurl'=>$post['avatarUrl'],
                        'wx_data'=>json_encode($post)
                    ];
            $account = new AccountModel($data);
            $account->save();

            $result['account_id'] = $account->id;
            $info['lead_created'] = null;
        }else{
            $result['account_id'] = $info['id'];
        }
        // 取出当前用户所在段位
        if($result['account_id'] > -1){
            $result['role'] = $this->__get_current($result['account_id']);
            $result['lead_created'] = $info['lead_created'];
            // $where['account_id'] = $result['account_id'];
            // $role = self::$M_role->get_role_by_where($where);
            // if($role){
            //     $result['role'] = $role[0];
            // }
        }
        return json($result);
    }
    // 更新用户信息
    public function update(){
        $post = input();
        if($post['openid']){
            $account = AccountModel::get(['openid' => $post['openid']]);
            if($post['mobile']){
                $account->mobile = $post['mobile'];
                $account->lead_created = now();
                $account->save();
            }
            $account->account_id = $account->id;
        }
        return json(array_merge($post,$account->toArray()));
    }
    // 换算当前用户的OPENID
    public function get_openID(){
        $code = input('code');
        $api = "https://api.weixin.qq.com/sns/jscode2session?appid=".APPID."&secret=".SECRET."&js_code=".$code."&grant_type=authorization_code";
        $data = json_decode(\Requests::get($api)->body);
        \think\Log::record('get_openID');
        \think\Log::record($data);
        return json($data);
    }
    // 根据Account获取用户所有学科的能力图谱
    public function get_power(){
        $where['account_id'] = input('account_id');
        $where['id'] = input('role_id');
        $my = $list = self::$M_role->get_by_account_to_role_list($where);
        if($list){
            $list = $list->role_list;
        }
        $filter = [];
        // 将序列化答题数据统一放入处理数据组中
        foreach($list as $info){    
            $info = $info->toArray();
            $answer_list = objectToArray(json_decode($info['answer_data']));
            if(is_array($answer_list)){
                foreach($answer_list as $answer){
                    if($answer['question_id'] > 0){
                        array_push($filter,$answer);
                    }
                }
            }
        }
        // 按学科进行分组 
        if($filter){
            $list = [];
            foreach($filter as $info){
                $list[$info['subject']][] = $info;
            }
        }
        // 最终统计成功率
        $result = [];
        if($list){
            $key = 0;
            foreach($list as $key1 => $info){
                $result[$key]['subject'] = $key1;
                $yes = 0;
                foreach($info as $key2 => $item){
                    if($item['result'] == 'yes'){
                        ++ $yes;
                    }
                }
                $result[$key]['yes_count'] = $yes;
                $result[$key]['count'] = count($info);
                $result[$key]['accuracy'] = round(($yes / count($info)) * 100,0);
                ++ $key;
            }
            // 将数据直接打成charts快速格式
            if($result){
                $return_subject = [];
                $return_accuracy = [];
                foreach($result as $info){
                    array_push($return_subject,$info['subject']);
                    array_push($return_accuracy,sqrt($info['accuracy']) * 10);
                }
                $result = [];
                $result['subject'] = $return_subject;
                $result['accuracy'] = $return_accuracy;
                $result['info'] = $my;
                // $result['accuracy'] = array_map(function($item){
                //     if($item == 0)return 0;
                //     return $item;
                // },$return_accuracy);
            }
        }
        
        return json($result);
    }
    // 获取排名
    public function rank(){
        $world_rank = Cache::get('world_rank_'.input('account_id'));
        if(input('grade')){
            $grade_rank = Cache::get('grade_rank_' . input('grade') . '_' . input('account_id'));
        }
        
        //if( ! $grade_rank ){
            $grade_rank = [];
            if(input('grade') && input('grade') != 'null'){
                $grade_rank = self::$M_role->rank(['grade' => input('grade')],input('account_id'));
                Cache::set('grade_rank_' . input('grade') . '_' . input('account_id'),$grade_rank,3600);
            }
        //}
        //if( ! $world_rank ){
            $world_rank = self::$M_role->rank(null,input('account_id'));
            Cache::set('world_rank_' . input('account_id'),$world_rank,3600);
        //}
        return json(['grade_rank'=>$grade_rank,'world_rank'=>$world_rank]);
    }
    // 获取当前进行中的角色
    public function __get_current($account_id){
        $where['account_id'] = $account_id;
        $where['current'] = 1;
        $info = self::$M_role->get_role_by_where($where);
        return get_info_list($info);
    }
     // 获取当前进行中的角色
     public function get_current(){
        $result = [];
        if(input('account_id')){
            $result = $this->__get_current( input('account_id') ); 
        }
        else if(input('openid')){
            $info = self::$M_account->get_by_openid(input('openid'));
            if($info){
                $result = $this->__get_current($info->id);
            }
        }
        return json($result);
    }
    // 获取用户详细
    public function get_account_by_openid(){
        $info = self::$M_account->get_by_openid(input('openid'));
        return json($info);
    }
}
