<?php

namespace common\models;

use common\helpers\Cache;
use Yii;

/**
 * This is the model class for table "{{%SMS_TEMPLATE}}".
 *
 * @property string $ID
 * @property string $TITLE 标题
 * @property string $DESCRIPTION 描述
 * @property string $FIELD 标题英文字段
 * @property string $DEC_LEVEL 报单级别
 * @property string $EMP_LEVEL 聘级
 * @property string $CONTENT 模板内容
 * @property int $CREATED_AT 创建时间
 */
class SmsTemplate extends \common\components\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%SMS_TEMPLATE}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['TITLE', 'DESCRIPTION', 'FIELD', 'CONTENT', 'CREATED_AT'], 'required'],
            [['CREATED_AT'], 'integer'],
            [['ID', 'TITLE', 'DEC_LEVEL', 'EMP_LEVEL'], 'string', 'max' => 32],
            [['DESCRIPTION', 'FIELD', 'CONTENT'], 'string', 'max' => 255],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'TITLE' => '标题',
            'DESCRIPTION' => '描述',
            'FIELD' => '标题英文字段',
            'DEC_LEVEL' => '报单级别',
            'EMP_LEVEL' => '聘级',
            'CONTENT' => '模板内容',
            'CREATED_AT' => '创建时间',
        ];
    }

    /**
     * 通过报单级别的索引来获取短信模板
     * @return array
     */
    public static function getTemplateByDecLevel(){
        $allDecLevelData = static::find()->select('DEC_LEVEL')->where('DEC_LEVEL IS NOT NULL')->asArray()->groupBy('DEC_LEVEL')->all();
        $result = [];
        foreach($allDecLevelData as $levelData){
            $result[$levelData['DEC_LEVEL']] = [];
        }
        $allData = static::find()->where('DEC_LEVEL IS NOT NULL')->asArray()->all();
        foreach ($allData as $data){
            if(isset($result[$data['DEC_LEVEL']])){
                $result[$data['DEC_LEVEL']][$data['FIELD']] = $data;
            }
        }
        return $result;
    }

    /**
     * 通过聘级的索引来获取短信模板
     * @return array
     */
    public static function getTemplateByEmpLevel(){
        $allEmpLevelData = static::find()->select('EMP_LEVEL')->where('EMP_LEVEL IS NOT NULL')->asArray()->groupBy('EMP_LEVEL')->all();
        $result = [];
        foreach($allEmpLevelData as $levelData){
            $result[$levelData['EMP_LEVEL']] = [];
        }
        $allData = static::find()->where('EMP_LEVEL IS NOT NULL')->asArray()->all();
        foreach ($allData as $data){
            if(isset($result[$data['EMP_LEVEL']])){
                $result[$data['EMP_LEVEL']][] = $data;
            }
        }
        return $result;
    }

    /**
     * 从缓存获取报单级别的短信模板
     * @return array|mixed
     */
    public static function getDecFromCache(){
        $data = Yii::$app->cache->get(Cache::SMS_TEMPLATE_DEC_KEY);
        if(!$data){
            // 获取信息
            $data = self::getTemplateByDecLevel();
            Yii::$app->cache->set(Cache::SMS_TEMPLATE_DEC_KEY, $data);
        }
        return $data;
    }

    /**
     * 从缓存获取报单级别的短信模板
     * @return array|mixed
     */
    public static function getEmpFromCache(){
        $data = Yii::$app->cache->get(Cache::SMS_TEMPLATE_EMP_KEY);
        if(!$data){
            // 获取信息
            $data = self::getTemplateByEmpLevel();
            Yii::$app->cache->set(Cache::SMS_TEMPLATE_EMP_KEY, $data);
        }
        return $data;
    }

    /**
     * 更新报单级别的短信模板
     * @return array
     */
    public static function updateDecToCache(){
        // 获取配置
        $data = self::getTemplateByDecLevel();
        Yii::$app->cache->set(Cache::SMS_TEMPLATE_DEC_KEY, $data);
        return $data;
    }

    /**
     * 更新聘级的短信模板
     * @return array
     */
    public static function updateEmpToCache(){
        // 获取配置
        $data = self::getTemplateByEmpLevel();
        Yii::$app->cache->set(Cache::SMS_TEMPLATE_EMP_KEY, $data);
        return $data;
    }

}
