<?php

namespace common\models;

use common\helpers\Cache;
use common\helpers\LoggerTool;
use Yii;

/**
 * This is the model class for table "{{%ELITE_LEVEL}}".
 *
 * @property string $ID
 * @property string $LEVEL_NAME 级别名称
 * @property int $ICON_TYPE 图标类型
 * @property int $ICON_NUM 图标个数
 * @property int $RX_PERCENT 图标个数
 * @property string $MIN_LEVEL_ID 上级ID
 * @property string $LEVEL_SCORE 级别分数
 * @property int $SORT 排序
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 更新时间
 * @property string $CREATE_ADMIN 创建人
 * @property string $UPDATE_ADMIN 更新人
 */
class EliteLevel extends \common\components\ActiveRecord
{
    const NO_LEVEL_ID = 'N0RI23JKCP4OBLZ6GAXYWH9D1QS8NA7V';

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%ELITE_LEVEL}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['LEVEL_NAME', 'CREATED_AT', 'CREATE_ADMIN'], 'required'],
            [['SORT', 'CREATED_AT', 'UPDATED_AT'], 'integer'],
            [['ID', 'CREATE_ADMIN', 'UPDATE_ADMIN'], 'string', 'max' => 32],
            [['LEVEL_NAME'], 'string', 'max' => 20],
            [['LEVEL_NAME'], 'unique'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'LEVEL_NAME' => '级别名称',
            'SORT' => '排序',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '更新时间',
            'CREATE_ADMIN' => '创建人',
            'UPDATE_ADMIN' => '更新人',
        ];
    }

    /**
     * 获取全部级别以数字索引的方式展现
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getAllDataWithNumIndex()
    {
        return static::find()->where('1=1')->orderBy('SORT ASC, CREATED_AT ASC')->indexBy('SORT')->asArray()->all();
    }

    /**
     * 获取全部配置
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getAllData()
    {
        return static::find()->where('1=1')->orderBy('SORT ASC, CREATED_AT ASC')->indexBy('ID')->asArray()->all();
    }

    // 获取级别配置一维度数组,id为键,级别等级为值
    public static function getIdConvertLevelSort()
    {
        $ret = [];
        $allData = static::getAllData();
        foreach($allData as $data) {
            $ret[$data['ID']] = $data['SORT'];
        }
        return $ret;
    }

    /**
     * 从缓存获取信息
     * @return array|mixed|\yii\db\ActiveRecord[]
     */
    public static function getIdConvertLevelSortCache()
    {
        $key = Cache::ELITE_LEVEL_CONFIG_KEY . ':idsort';
        $data = Yii::$app->cache->get($key);
        if(!$data){
            // 获取信息
            $data = self::getIdConvertLevelSort();
            Yii::$app->cache->set($key, $data);
        }

        return $data;
    }

    /**
     * 从缓存获取信息
     * @return array|mixed|\yii\db\ActiveRecord[]
     */
    public static function getFromCache()
    {
        $data = Yii::$app->cache->get(Cache::ELITE_LEVEL_CONFIG_KEY);
        if(!$data){
            // 获取信息
            $data = self::getAllData();
            Yii::$app->cache->set(Cache::ELITE_LEVEL_CONFIG_KEY, $data);
        }
        // i18n转换
//        foreach ($data as &$item) {
//            $item['LEVEL_NAME'] = Yii::t('ctx', $item['LANGUAGE_KEY']);
//        }
        return $data;
    }

    /**
     *  更新缓存
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function updateToCache()
    {
        // 获取配置
        $data = self::getAllData();
        Yii::$app->cache->set(Cache::ELITE_LEVEL_CONFIG_KEY, $data);
        return $data;
    }

    /**
     * 通过排序获取级别
     * @param int $sort
     * @return mixed
     */
    public static function getLevelFromSort(int $sort)
    {
        static $crownLevels;
        if(!$crownLevels){
            $crownLevels = self::getFromCache();
            $crownLevels = array_column($crownLevels, null, 'sort');
        }
        return $crownLevels[$sort];
    }

    /**
     * 通过排序获取ID
     * @param int $sort
     * @return mixed
     */
    public static function getIdFromSort(int $sort)
    {
        $level = self::getLevelFromSort($sort);
        return $level['ID'];
    }

    /**
     * 获取默认级别
     * @return mixed
     */
    public static function getDefaultLevelId()
    {
        return self::NO_LEVEL_ID;
    }

    /**
     * 获取星级的排序
     * @param $id
     * @return mixed
     */
    public static function getSortById($id)
    {
        $crownLevels = self::getFromCache();
        return $crownLevels[$id]['SORT'] ?? 0;
    }

    /**
     * 通过ID获取星级名称
     * @param $levelId
     * @return mixed'
     */
    public static function getNameById($levelId)
    {
        return self::findOneAsArray('ID = :ID', [':ID' => $levelId])['LEVEL_NAME'] ?? '';
    }

}
