<?php

namespace common\models;

use common\helpers\Cache;
use Yii;
use yii\helpers\Json;

/**
 * This is the model class for table "{{%OCR_API}}".
 *
 * @property string $ID
 * @property string $API_NAME 文件名称
 * @property string $DESCRIPTION 分类
 * @property string $CONFIG 备注
 * @property int $STATUS 文件路径
 * @property string $CREATE_ADMIN 创建管理员
 * @property string $UPDATE_ADMIN 更新管理员
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 更新时间
 */
class OcrApi extends \common\components\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%OCR_API}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['API_NAME', 'DESCRIPTION', 'CREATE_ADMIN', 'CREATED_AT'], 'required'],
            [['STATUS', 'CREATED_AT', 'UPDATED_AT'], 'integer'],
            [['ID', 'CREATE_ADMIN', 'UPDATE_ADMIN'], 'string', 'max' => 32],
            [['API_NAME'], 'string', 'max' => 48],
            [['DESCRIPTION'], 'string', 'max' => 255],
            [['CONFIG'], 'string', 'max' => 4000],
            [['API_NAME'], 'unique'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'API_NAME' => '文件名称',
            'DESCRIPTION' => '分类',
            'CONFIG' => '备注',
            'STATUS' => '文件路径',
            'CREATE_ADMIN' => '创建管理员',
            'UPDATE_ADMIN' => '更新管理员',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '更新时间',
        ];
    }

    /**
     * 获取全部数据
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getAllData(){
        $allData = static::find()->where('1=1')->orderBy('CREATED_AT ASC')->indexBy('API_NAME')->asArray()->all();
        foreach($allData as $key=>$data){
            if($data['CONFIG']){
                $allData[$key]['CONFIG'] = Json::decode($data['CONFIG']);
            } else{
                $allData[$key]['CONFIG'] = [];
            }
        }
        return $allData;
    }

    /**
     * 获取当前正在启用的接口
     * @return array
     */
    public static function getActiveData(){
        $data = static::find()->where('STATUS=:STATUS', [':STATUS' => 1])->orderBy('CREATED_AT ASC')->asArray()->one();
        if($data['CONFIG']){
            $data['CONFIG'] = Json::decode($data['CONFIG']);
        } else {
            $data['CONFIG'] = null;
        }
        return $data;
    }

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

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

    /**
     * 按id获取配置
     * @param $id
     * @return array
     */
    public static function getConfigById($id) {
        $smsApi = self::findOneAsArray('ID=:ID',[':ID'=>$id],'CONFIG');
        $config = json_decode($smsApi['CONFIG'],true);
        $data = [];
        foreach ($config as $key=>$value){
            $data[$key]['label'] = $key;
            $data[$key]['value'] = $value;
        }
        return $data;
    }

}
