<?php

namespace app\common\controller;

use app\common\library\Auth;
use think\Config;
use think\Cookie;
use think\Db;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Validate;

/**
 * 前台控制器基类
 */
class Commons
{
    public function create_trade_no($prefix = '')
    {
        //return $prefix . date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
        $code = array(
            'A',
            'B',
            'C',
            'D',
            'E',
            'F',
            'G',
            'H',
            'I',
            'J',
            'L',
            'M',
            'N',
            'O',
            'P',
            'Q',
            'R',
            'S',
            'T',
            'U',
            'V',
            'W',
            'X',
            'Y',
            'Z'
        );

        return $osn = $code[mt_rand(0, count($code))] . strtoupper(dechex(date('m'))) . date('d') . substr(
                time(),
                -5
            ) . substr(
                microtime(),
                2,
                5
            ) . sprintf('%02d', rand(0, 99));
    }

    public function get_score($row)
    {
        $type = $row['ti_type'];
        $answers = $row['answer_list'];
        $answerArr = json_decode($answers, true);
        $questionList = $this->get_question($type);
        $c = $questionList['c'];
        $groupData = [];
        // dd($answerArr);
        foreach ($answerArr as $key => $item) {
            $dic = $c[$key][$item];
            if(!isset($groupData[$dic])){
                $groupData[$dic] = 0;
            }else{
                $groupData[$dic] += 1;
            }
            
        }
        // dd($groupData);
        //var_dump($groupData);
        $tpl = '';
        //第一组
        if (isset($groupData['E']) && isset($groupData['I']) && $groupData['E'] > $groupData['I']) {
            $tpl .= 'E';
        } else {
            $tpl .= 'I';
        }
        //第二组
        if (isset($groupData['S']) && isset($groupData['N']) && $groupData['S'] > $groupData['N']) {
            $tpl .= 'S';
        } else {
            $tpl .= 'N';
        }
        //第三组
        if (isset($groupData['T']) && isset($groupData['F']) && $groupData['T'] > $groupData['F']) {
            $tpl .= 'T';
        } else {
            $tpl .= 'F';
        }
        //第四组
        if (isset($groupData['J']) && isset($groupData['P']) && $groupData['J'] > $groupData['P']) {
            $tpl .= 'J';
        } else {
            $tpl .= 'P';
        }
        return $tpl;
    }

    public function get_question($type)
    {
        return config('mbti' . $type);
    }
}