<?php

namespace common\models;

use common\helpers\Cache;
use Yii;

/**
 * This is the model class for table "{{%REGION}}".
 *
 * @property string $ID
 * @property int $REGION_CODE 编码
 * @property int $DEEP 深度
 * @property string $REGION_NAME 名称
 * @property int $PID 父级id
 * @property string COUNTRY_ID 国家
 * @property string $AREA_CODE 区号
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 更新时间
 * @property string $UPDATER 更新人
 * @property string $ADM_NAME 操作人
 */
class Region extends \common\components\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%REGION}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['REGION_CODE', 'DEEP', 'PID', 'CREATED_AT', 'UPDATED_AT'], 'integer'],
            [['ID'], 'string', 'max' => 32],
            [['REGION_NAME'], 'string', 'max' => 128],
            [['AREA_CODE'], 'string', 'max' => 6],
            [['UPDATER', 'ADM_NAME'], 'string', 'max' => 16],
            [['REGION_CODE'], 'unique'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'REGION_CODE' => '编码',
            'DEEP' => '深度',
            'REGION_NAME' => '名称',
            'PID' => '父级id',
            'COUNTRY_ID' => '国家',
            'AREA_CODE' => '区号',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '更新时间',
            'UPDATER' => '更新人',
            'ADM_NAME' => '操作人',
        ];
    }

    /**
     * 获取中文的地区名称
     * @param $regionCode
     * @return string
     */
    public static function getCnName($regionCode){
//        self::updateToCache();
        if (!$regionCode) return '';
        $allData = self::getFromCache();
        return $allData[$regionCode]['REGION_NAME'] ?? '';
    }

    /**
     * 获取全部配置，把育成津贴奖金比例解成数组
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getAllData(){
        return static::find()->where('STATUS=1')->orderBy('REGION_NAME ASC')->indexBy('REGION_CODE')->asArray()->all();
    }

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

    public static function getByCountryId($countyId)
    {
        $result = [];
        $regions = self::getFromCache();
        foreach ($regions as $region) {
            if ($region['COUNTRY_ID'] == $countyId) {
                $result[] = ['value' => $region['REGION_CODE'], 'label' => $region['REGION_NAME'],];
            }
        }

        return $result;
    }

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

    /**
     * 根据省编码获取仓库
     * @param $regionCode
     * @return string
     */
    public static function getWarehouseByCode($regionCode){
        //'540000','710000','810000','820000'
        $ZoningArr = [
            '01' => ['10100','10200','10300','10400','10500','10600','10700','10800','10900',
                '11000','11100','11200','11300','11400','11500','11600','11700','11800',
                '11900','12000','12100','12200','12300','12400','12500','12600','12700',
                '12800','12900','13000','13100','13200','13300','13400','13500','13600','19900',

                '10000', '10001', '10002', '10003', '10004', '10005', '10006',
                ]
        ];
        $zoning = '';
        foreach ($ZoningArr as $key=>$val){
            if(in_array($regionCode,$val)){
                $zoning = $key;
                break;
            }
        }
        return $zoning;
    }
}
