<?php

namespace common\models;

use common\helpers\Cache;
use Yii;

/**
 * This is the model class for table "{{%COUNTRIES}}".
 *
 * @property string ID
 * @property string NAME 名字
 * @property string CODE 简码
 * @property string LOCAL_CURRENCY_ID 货币ID
 * @property string DEFAULT_LANGUAGE_ID 语言ID
 * @property string LANGUAGE_KEY 语言KEY
 * @property int ACTIVE 状态
 * @property int CREATED_AT 创建时间
 * @property int UPDATED_AT 更新时间
 */
class Countries extends \common\components\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%COUNTRIES}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'NAME' => '名字',
            'CODE' => '简码',
            'LOCAL_CURRENCY_ID' => '货币ID',
            'DEFAULT_LANGUAGE_ID' => '语言ID',
            'ACTIVE' => '状态：1正常 0异常',
            'LANGUAGE_KEY' => '语言KEY',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '更新时间',
        ];
    }

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

        return $data;
    }

    /**
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getAllData()
    {
        return static::find()->where('ACTIVE=1')->orderBy('ACTIVE DESC, NAME ASC')->asArray()->all();
    }

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

    /**
     * 通过ID获取
     * @param string $id
     * @return array|null
     */
    public static function getById(string $id)
    {
        return self::findOneAsArray('ID=:ID', [':ID' => $id]);
    }

    /**
     * 通过ID获取
     * @param string $id
     * @return string
     */
    public static function getNameById(string $id): string
    {
        return self::findOneAsArray('ID=:ID', [':ID' => $id])['NAME'];
    }

    /**
     * 通过ID获取
     * @param string $id
     * @return string
     */
    public static function getCodeById(string $id): string
    {
        return self::findOneAsArray('ID=:ID', [':ID' => $id])['CODE'];
    }

    /**
     * 通过ID查询币种.
     * @param string $id
     * @return int|mixed
     */
    public static function getCurrency(string $id)
    {
        $record = self::findOneAsArray('ID=:ID', [':ID' => $id]);
        return $record['LOCAL_CURRENCY_ID'] ?? 0;
    }

    public static function getCountry(string $id)
    {
        $record = self::findOneAsArray('ID=:ID', [':ID' => $id]);
        return $record['NAME'] ?? '';
    }
}
