<?php

namespace common\models;

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

/**
 * This is the model class for table "{{%EMPLOY_LEVEL}}".
 *
 * @property string $ID
 * @property string $LEVEL_NAME 级别名称
 * @property int $LOCATION_NUM 区数量
 * @property string $LOCATION_PERF 单区业绩
 * @property string $NEW_PERF 新增业绩
 * @property string $MIN_EMPLOY_LEVEL 几个区域内分别有几个最低什么聘级的会员
 * @property string $YC_PERCENT_ARR 育成津贴比例集合
 * @property string $CF_BASE_NUM 车房奖基数
 * @property string $CF_PERCENT 车房奖系数
 * @property string $LX_BASE_NUM 领袖奖奖基数
 * @property string $LX_PERCENT 领袖奖系数
 * @property int $SORT 排序
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 更新时间
 * @property string $CREATE_ADMIN 创建人
 * @property string $UPDATE_ADMIN 更新人
 * @property int $ICON_TYPE 图标类型
 * @property int $ICON_NUM 图标个数
 * @property int $RX_PERCENT 图标个数
 * @property string $OTHER_DEPART_PERF 去除最大部门，其它部门累计推荐业绩
 * @property int $MIN_EMPLOY_NUM 最小聘级的数量
 * @property string $LEVEL_SCORE 级别分数
 * @property string $UPGRADE_SCORE 升级分数
 */
class EmployLevel extends \common\components\ActiveRecord
{

    const NO_LEVEL_ID = '67BE1C6C21F81B32E055736AECE8644D';
    const JX_ZR_LEVEL_ID = 'E121497617216708615';
    const SHX_ZJ_LEVEL_ID = '67BE82019BE524CAE055736AECE8644D';

    CONST EMP_LEVEL_SORT = [
        'NO_LEVEL' => 0,//无聘级
        'JX_ZR_LEVEL' => 1,//见习主任
        'ZR_LEVEL' => 2,//主任
        'GJ_ZR_LEVEL' => 3,//高级主任
        'JL_LEVEL' => 4,//经理
        'GJ_JL_LEVEL' => 5,//高级经理
        'ZJ_LEVEL' => 6,//总监
        'GJ_ZJ_LEVEL' => 7,//高级总监
        'SHX_ZJ_LEVEL' => 8,//首席总监
    ];

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%EMPLOY_LEVEL}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['LEVEL_NAME', 'CREATED_AT', 'CREATE_ADMIN'], 'required'],
            [['LOCATION_NUM', 'SORT', 'CREATED_AT', 'UPDATED_AT', 'ICON_TYPE', 'ICON_NUM', 'MIN_EMPLOY_NUM'], 'integer'],
            [['LOCATION_PERF', 'NEW_PERF', 'CF_BASE_NUM', 'CF_PERCENT', 'LX_BASE_NUM', 'LX_PERCENT', 'RX_PERCENT', 'OTHER_DEPART_PERF', 'LEVEL_SCORE', 'UPGRADE_SCORE'], 'number'],
            [['ID', 'MIN_EMPLOY_LEVEL', 'CREATE_ADMIN', 'UPDATE_ADMIN'], 'string', 'max' => 32],
            [['LEVEL_NAME'], 'string', 'max' => 20],
            [['YC_PERCENT_ARR'], 'string', 'max' => 4000],
            [['LEVEL_NAME'], 'unique'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'LEVEL_NAME' => '级别名称',
            'LOCATION_NUM' => '区数量',
            'LOCATION_PERF' => '单区业绩',
            'NEW_PERF' => '新增业绩',
            'MIN_EMPLOY_LEVEL' => '几个区域内分别有几个最低什么聘级的会员',
            'YC_PERCENT_ARR' => '育成津贴比例集合',
            'CF_BASE_NUM' => '车房奖基数',
            'CF_PERCENT' => '车房奖系数',
            'LX_BASE_NUM' => '领袖奖奖基数',
            'LX_PERCENT' => '领袖奖系数',
            'SORT' => '排序',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '更新时间',
            'CREATE_ADMIN' => '创建人',
            'UPDATE_ADMIN' => '更新人',
            'ICON_TYPE' => '图标类型',
            'ICON_NUM' => '图标个数',
            'RX_PERCENT' => '荣衔奖比例',
            'OTHER_DEPART_PERF' => '去除最大部门，其它部门累计推荐业绩',
            'LEVEL_SCORE' => '级别分数',
            'UPGRADE_SCORE' => '升级分数',
        ];
    }

    /**
     * 获取全部级别以数字索引的方式展现
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getAllDataWithNumIndex(){
        $allData = static::find()->where('1=1')->orderBy('SORT ASC, CREATED_AT ASC')->indexBy('SORT')->asArray()->all();
        foreach($allData as $key=>$data){
            if($data['YC_PERCENT_ARR']){
                $allData[$key]['YC_PERCENT_ARR'] = Json::decode($data['YC_PERCENT_ARR']);
            } else{
                $allData[$key]['YC_PERCENT_ARR'] = [0,0,0,0,0,0,0,0,0,0];
            }
        }
        return $allData;
    }

    /**
     * 获取全部配置，把育成津贴奖金比例解成数组
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getAllData(){
        $allData = static::find()->where('1=1')->orderBy('SORT ASC, CREATED_AT ASC')->indexBy('ID')->asArray()->all();
        foreach($allData as $key=>$data){
            if($data['YC_PERCENT_ARR']){
                $allData[$key]['YC_PERCENT_ARR'] = Json::decode($data['YC_PERCENT_ARR']);
            } else{
                $allData[$key]['YC_PERCENT_ARR'] = [0,0,0,0,0,0,0,0,0,0];
            }
        }
        return $allData;
    }

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

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

    /**
     * 通过排序获取级别
     * @param int $sort
     * @return mixed
     */
    public static function getLevelFromSort(int $sort){
        static $empLevels;
        if(!$empLevels){
            $empLevels = self::getFromCache();
            $empLevels = array_column($empLevels, null, 'sort');
        }
        return $empLevels[$sort];
    }

    /**
     * 通过排序获取ID
     * @param int $sort
     * @return mixed
     */
    public static function getIdFromSort(int $sort){
        $level = self::getLevelFromSort($sort);
        return $level['ID'];
    }

    /**
     * 获取默认级别
     * @return mixed
     */
    public static function getDefaultLevelId(){
        return self::NO_LEVEL_ID;
    }

    /**
     * 获取聘级的排序
     * @param $id
     * @return mixed
     */
    public static function getSortById($id){
        $empLevels = self::getFromCache();
        return $empLevels[$id]['SORT']??0;
    }

    /**
     * 是否合格高级经理及以上级别
     * @param $id
     * @return bool
     */
    public static function chkSenior($id) {
        if (self::getSortById($id) >= 2) return true;
        return false;
    }

    /**
     * 通过ID获取聘级名称
     * @param $levelId
     * @return mixed'
     */
    public static function getNameById($levelId){
        $data = self::findOneAsArray('ID=:ID', [':ID'=>$levelId]);
        return $data['LEVEL_NAME'];
    }

    /**
     * 奖金参数
     */
    public static function getBonusConfig(){
        $allData = self::getAllData();
        $data = [];
        foreach ($allData as $value) {
            $data[$value['ID']]['label'] = $value['LEVEL_NAME'];
            $data[$value['ID']]['value'] = '【育成津贴】按代数拿奖比例(%)'.implode(",",$value['YC_PERCENT_ARR']).'；【车房养老奖】基数'.$value['CF_BASE_NUM'].',分工系数'.$value['CF_PERCENT'].'；【领袖分红奖】基数'.$value['LX_BASE_NUM'].',分工系数'.$value['LX_PERCENT'];
        }
        return $data;
    }
}
