<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "{{%OPEN_BANK}}".
 *
 * @property string $ID
 * @property string $BANK_NAME 银行名称
 * @property string $BANK_CODE 银行代码
 * @property int $LIST_ORDER 排序
 * @property int $CREATED_AT 创建时间
 * @property int $UPDATED_AT 更新时间
 * @property string $ADM_NAME 操作人
 * @property string $MIN_CHARGE 最低手续费
 * @property string $MAX_CHARGE 最高手续费
 * @property string $CHARGE_PROP 手续费比例
 * @property string $UPDATER 更新人
 * @property int $STATUS 状态
 */
class OpenBank extends \common\components\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%OPEN_BANK}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['BANK_NAME', 'BANK_CODE', 'ADM_NAME'], 'required'],
            [['LIST_ORDER', 'CREATED_AT', 'UPDATED_AT', 'STATUS'], 'integer'],
            [['MIN_CHARGE', 'MAX_CHARGE', 'CHARGE_PROP'], 'number'],
            [['ID', 'BANK_CODE'], 'string', 'max' => 32],
            [['BANK_NAME'], 'string', 'max' => 64],
            [['ADM_NAME', 'UPDATER'], 'string', 'max' => 16],
            [['BANK_CODE'], 'unique'],
            [['ID'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'ID' => 'ID',
            'BANK_NAME' => Yii::t('app', 'bankName'),'银行名称',
            'BANK_CODE' => Yii::t('app', 'bankCode'),'银行代码',
            'LIST_ORDER' => Yii::t('app', 'sort'),'排序',
            'CREATED_AT' => Yii::t('app', 'createAt'),'创建时间',
            'UPDATED_AT' => Yii::t('app', 'updateAt'),'更新时间',
            'ADM_NAME' => Yii::t('app', 'admin'),'操作人',
            'MIN_CHARGE' => Yii::t('app', 'minCharge'),'最低手续费',
            'MAX_CHARGE' => Yii::t('app', 'maxCharge'),'最高手续费',
            'CHARGE_PROP' => Yii::t('app', 'chargeRate'),'手续费比例',
            'UPDATER' => Yii::t('app', 'updater'),'更新人',
            'STATUS' => Yii::t('app', 'status'),'状态',
        ];
    }

    public static function getCnName($code){
        $data = static::findOneAsArray('BANK_CODE=:BANK_CODE', [':BANK_CODE' => $code], 'BANK_NAME');
        return $data ? $data['BANK_NAME'] : '';
    }

    /**
     * 获取全部银行
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getAllOpenBank(){
        return self::find()->where('1=1')->indexBy('BANK_CODE')->orderBy('CREATED_AT ASC')->asArray()->all();
    }
}
