<?php

namespace common\models;

use Yii;
use common\libs\logging\operate\valueType\Config as ValueTypeConfig;

/**
 * This is the model class for table "{{%USER_WALLET}}".
 *
 * @property string $ID
 * @property string $USER_ID
 * @property string $CASH
 * @property integer $UPDATED_AT
 * @property integer $CLEAR_BY_CLOSED_AT
 * @property User $user
 */
class UserWallet extends \common\components\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%USER_WALLET}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['USER_ID'], 'required'],
            [['CASH'], 'number'],
            [['UPDATED_AT', 'CLEAR_BY_CLOSED_AT'], 'integer'],
            [['ID','USER_ID'], 'string', 'max' => 32],
            [['USER_ID'], 'unique'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'USER_ID' => '用户id',
            'CASH' => '现金',
            'UPDATED_AT' => '修改时间',
            'CLEAR_BY_CLOSED_AT' => '清空关停会员钱包时间',
        ];
    }

    /**
     * 操作日志记录条件
     * @return array
     */
    public function attrLabelsWithLogType(){
        return [
            'USER_ID' => '用户id',
            'CLEAR_BY_CLOSED_AT' => [
                'label' => '清空关停会员钱包时间',
                'type' => ValueTypeConfig::DATE_TIME_TYPE,
            ],
        ];
    }

    /**
     * 获取一名会员的余额
     * @param $userId
     * @return array|null
     */
    public static function getCashByUserId($userId){
        $data = UserWallet::findOneAsArray('USER_ID=:USER_ID', [':USER_ID'=>$userId], 'USER_ID,CASH');
        if(!$data){
            $data = [
                'USER_ID' => $userId,
                'CASH' => 0,
            ];
        }
        return $data;
    }
}
