<?php

namespace common\models;

use common\helpers\Cache;
use Yii;

/**
 * This is the model class for table "{{%REG_TYPE}}".
 *
 * @property string $ID
 * @property string $TYPE_NAME 类型名称
 * @property int $IS_PACT 是否启用协议
 * @property string $MONTH_LIMIT_AMOUNT 达到月提现金额升级应注册类型
 * @property string $YEAR_LIMIT_AMOUNT 达到年提现金额升级应注册类型
 * @property string $CREATE_ADMIN 创建管理员
 * @property string $UPDATE_ADMIN 修改管管理
 * @property string $REMARK 备注
 * @property int $SORT 排序
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 更新时间
 */
class RegType extends \common\components\ActiveRecord {
    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%REG_TYPE}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['TYPE_NAME', 'CREATED_AT'], 'required'],
            [['IS_PACT', 'SORT', 'CREATED_AT', 'UPDATED_AT'], 'integer'],
            [['MONTH_LIMIT_AMOUNT', 'YEAR_LIMIT_AMOUNT'], 'number'],
            [['ID', 'CREATE_ADMIN', 'UPDATE_ADMIN'], 'string', 'max' => 32],
            [['TYPE_NAME'], 'string', 'max' => 48],
            [['REMARK'], 'string', 'max' => 4000],
            [['TYPE_NAME'], 'unique'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'ID' => 'ID',
            'TYPE_NAME' => '类型名称',
            'IS_PACT' => '是否启用协议',
            'MONTH_LIMIT_AMOUNT' => '达到月提现金额升级应注册类型',
            'YEAR_LIMIT_AMOUNT' => '达到年提现金额升级应注册类型',
            'CREATE_ADMIN' => '创建管理员',
            'UPDATE_ADMIN' => '修改管理员',
            'REMARK' => '备注',
            'SORT' => '排序',
            'CREATED_AT' => '创建时间',
            'UPDATED_AT' => '更新时间',
        ];
    }

    /**
     * 从缓存获取信息
     * @return array|mixed|\yii\db\ActiveRecord[]
     */
    public static function getFromCache(){
        $data = Yii::$app->cache->get(Cache::REG_TYPE_CONFIG_KEY);
        if(!$data){
            // 获取信息
            $data = self::getTypes();
            Yii::$app->cache->set(Cache::REG_TYPE_CONFIG_KEY, $data);
        }
        // i18n转换
        foreach ($data as &$item) {
            $item['TYPE_NAME'] = Yii::t('ctx', $item['LANGUAGE_KEY']);
        }
        return $data;
    }

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

    /**
     * 获取注册类型
     * @return array|null
     */
    public static function getTypes() {
        return self::find()->indexBy('ID')->orderBy('SORT ASC')->asArray()->all();
    }

    /**
     * 是否满足修改应注册类型
     * @param $uid
     * @param $shouldRegType
     * @param $year
     * @param $month
     */
    public static function chkRegType($uid, $shouldRegType, $year, $month) {
        $status = [Withdraw::STATUS_APPLIED, Withdraw::STATUS_AUDITED, Withdraw::STATUS_WAIT_PAID, Withdraw::STATUS_PAID_FALSE, Withdraw::STATUS_PAID];
        $statusStr = implode("','", $status);
        $statusStr = "'" . $statusStr . "'";
        $withdrawMonth = Withdraw::find()->where('USER_ID=:USER_ID AND WITHDRAW_MONTH=:WITHDRAW_MONTH AND WITHDRAW_YEAR=:WITHDRAW_YEAR AND AUDIT_STATUS IN (' . $statusStr . ')', [':USER_ID' => $uid, ':WITHDRAW_MONTH' => $month, ':WITHDRAW_YEAR' => $year])->sum('AMOUNT');
        $withdrawYear = Withdraw::find()->where('USER_ID=:USER_ID AND WITHDRAW_YEAR=:WITHDRAW_YEAR AND AUDIT_STATUS IN (' . $statusStr . ')', [':USER_ID' => $uid, ':WITHDRAW_YEAR' => $year])->sum('AMOUNT');
        $regTypes = array_reverse(self::getTypes());
        $toType = $shouldRegType;
        foreach ($regTypes as $value) {
            if ($withdrawMonth >= $value['MONTH_LIMIT_AMOUNT'] || $withdrawYear >= $value['YEAR_LIMIT_AMOUNT']) {
                if ($value['SORT'] != 1) $toType = $value['ID'];
                break;
            }
        }
        if ($toType != $shouldRegType) {
            UserInfo::updateAll(['SHOULD_REG_TYPE' => $toType], 'USER_ID=:USER_ID', [':USER_ID' => $uid]);
        }
    }
}
