<?php

namespace common\models;

use common\helpers\Cache;
use Yii;

/**
 * This is the model class for table "{{%WITHDRAW_LEVEL}}".
 *
 * @property string $ID
 * @property string $TYPE 注册类型
 * @property string $MIN_AMOUNT 最小金额
 * @property string $MAX_AMOUNT 最大金额
 * @property string $TAX_PERCENT 税金比率
 * @property int $CREATED_AT 创建时间
 */
class WithdrawLevel extends \common\components\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%WITHDRAW_LEVEL}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['TYPE', 'CREATED_AT'], 'required'],
            [['MIN_AMOUNT', 'MAX_AMOUNT', 'TAX_PERCENT'], 'number'],
            [['CREATED_AT'], 'integer'],
            [['ID', 'TYPE'], 'string', 'max' => 32],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'TYPE' => '注册类型',
            'MIN_AMOUNT' => '最小金额',
            'MAX_AMOUNT' => '最大金额',
            'TAX_PERCENT' => '税金比率',
            'CREATED_AT' => '创建时间',
        ];
    }

    /**
     * 从缓存获取信息
     * @return array|mixed|\yii\db\ActiveRecord[]
     */
    public static function getFromCache(){
        $data = Yii::$app->cache->get(Cache::WITHDRAW_LEVEL_CONFIG_KEY);
        if(!$data){
            // 获取信息
            $data = static::find()->where('1=1')->orderBy('MIN_AMOUNT ASC')->asArray()->all();
            Yii::$app->cache->set(Cache::WITHDRAW_LEVEL_CONFIG_KEY, $data);
        }
        return $data;
    }

    /**
     *  更新缓存
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function updateToCache(){
        // 获取配置
        $data = static::find()->where('1=1')->orderBy('MIN_AMOUNT ASC')->asArray()->all();
        Yii::$app->cache->set(Cache::WITHDRAW_LEVEL_CONFIG_KEY, $data);
        return $data;
    }
}
