<?php

namespace common\models;

use common\helpers\Cache;
use Yii;

/**
 * This is the model class for table "{{%CURRENCIES_CONVERSIONS}}".
 *
 * @property string ID
 * @property int PERIOD_NUM 期数
 * @property int FROM_CURRENCY_ID 源
 * @property int TO_CURRENCY_ID 目标
 * @property double PRODUCT_RATE 商品汇率
 * @property double BONUSES_RATE 奖金汇率
 * @property int ACTIVE 状态
 * @property int CREATED_AT 创建时间
 * @property int UPDATED_AT 更新时间
 * @property string CREATED_BY 创建人
 * @property string UPDATED_BY 更新人
 */
class CurrencyConversions extends \common\components\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%CURRENCIES_CONVERSIONS}}';
    }

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

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'PERIOD_NUM' => '期数',
            'FROM_CURRENCY_ID' => '源币种',
            'TO_CURRENCY_ID' => '目标币种',
            'PRODUCT_RATE' => '商品汇率',
            'BONUSES_RATE' => '奖金汇率',
            'ACTIVE' => '状态：1正常 0异常',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '更新时间',
            'CREATED_BY' => '创建人',
            'UPDATED_BY' => '更新人',
        ];
    }

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

        foreach ($records as &$record) {
            $record['FROM_CURRENCY'] = Currency::getNameById($record['FROM_CURRENCY_ID']);
            $record['TO_CURRENCY'] = Currency::getNameById($record['TO_CURRENCY_ID']);
        }

        return $records;
    }

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

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

    /**
     * 查询汇率.
     * @param int $fromCurrencyId
     * @param int $toCurrencyId
     * @return mixed.
     */
    public static function getOne(int $fromCurrencyId, int $toCurrencyId): ?array
    {
        return self::findOneAsArray('FROM_CURRENCY_ID=:FROM_CURRENCY_ID AND TO_CURRENCY_ID=:TO_CURRENCY_ID AND ACTIVE=1',
            [
                ':FROM_CURRENCY_ID' => $fromCurrencyId,
                ':TO_CURRENCY_ID' => $toCurrencyId,
            ]);
    }

    /**
     * 查询汇率.
     * @param int $fromCurrencyId
     * @param int $toCurrencyId
     * @param string $rateType
     * @return mixed.
     */
    public static function getRate(int $fromCurrencyId, int $toCurrencyId, string $rateType = 'product'): ?array
    {
        $data = self::findOneAsArray('FROM_CURRENCY_ID=:FROM_CURRENCY_ID AND TO_CURRENCY_ID=:TO_CURRENCY_ID AND ACTIVE=1',
            [
                ':FROM_CURRENCY_ID' => $fromCurrencyId,
                ':TO_CURRENCY_ID' => $toCurrencyId,
            ]);

        return $rateType == 'product' ? ($data['PRODUCT_RATE'] ?? 0.0) : ($data['BONUSES_RATE'] ?? 0.0);
    }

    /**
     * 查询汇率.
     * @param int $toCurrencyId
     * @param string $rateType
     * @return mixed.
     */
    public static function getToUSDRate(int $toCurrencyId, string $rateType = 'product')
    {
        $data = self::findOneAsArray('FROM_CURRENCY_ID=:FROM_CURRENCY_ID AND TO_CURRENCY_ID=:TO_CURRENCY_ID AND ACTIVE=1',
            [
                ':FROM_CURRENCY_ID' => Currency::USD,
                ':TO_CURRENCY_ID' => $toCurrencyId,
            ]);

        return $rateType == 'product' ? ($data['PRODUCT_RATE'] ?? 0.0) : ($data['BONUSES_RATE'] ?? 0.0);
    }

    public static function deleteOne(int $fromCurrencyId, int $toCurrencyId): bool
    {
        return self::deleteAll('FROM_CURRENCY_ID=:FROM_CURRENCY_ID AND TO_CURRENCY_ID=:TO_CURRENCY_ID',
            [
                ':FROM_CURRENCY_ID' => $fromCurrencyId,
                ':TO_CURRENCY_ID' => $toCurrencyId,
            ]);
    }
}
