<?php
/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace home\modules\receivable\models;

use Yii;
use yii\db\ActiveRecord;
use home\modules\receivable\models\Bank;

/**
 * 收款账号表 ({%bank_account})
 *
 * @property integer $account_id  (银行账号id)
 * @property integer $bank_id  (银行id 关联表qc_receivable_bank)
 * @property string $no  (银行账号)
 * @property smallint $category  (分类： 1收款)
 * @property integer $sort  (排序  默认0)
 * @property smallint $status  (状态：0 禁用 1开启 99逻辑删除 默认开启)
 * @property text $remark  (备注)
 * @property integer $create_time  (创建时间)
 * @property integer $update_time  (修改时间)
 * @property integer $user_id  (创建人id)
 * @property string $ip  (创建人ip)
 *
 * @author Strief http://blog.u-bo.com
 * @since 0.1
 */
class Account extends \yii\db\ActiveRecord
{
    /**
    * @inheritdoc
    */
    public static function tableName()
    {
        return '{{%bank_account}}';
    }
    
    /**
    * @inheritdoc
    */
    public function rules()
    {
        return [
            [['bank_id', 'category', 'sort', 'status', 'create_time', 'update_time', 'user_id'], 'integer'],
            [['no'], 'required'],
            [['remark'], 'string'],
            [['no'], 'string', 'max' => 100],
            [['ip'], 'string', 'max' => 15]
        ];
    }

    /**
    * @inheritdoc
    */
    public function attributeLabels()
    {
        return [
            'account_id' => 'ID',
            'bank_id' => '所属银行',
            'no' => '银行账号',
            'category' => '分类',
            'sort' => '排序',
            'status' => '状态',
            'remark' => '备注',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'user_id' => '创建人ID',
            'ip' => '创建人Ip',
        ];
    }

    /**
     * 关联获取银行信息
     */
    public function getBank()
    {
        return $this->hasOne(Bank::className(), ['bank_id' => 'bank_id']);
    }

    /**
     * 查询收款银行列表
     *
     * @return array   $bankLists 返回查询结果
     */
    public static function getAccount($map = [], $order = ['sort' => SORT_DESC], $field = "*")
    {
        $map['status'] = 1;
        $accountLists = static::find()
                ->select($field)
                ->where($map)
                ->orderBy($order)
                ->asArray()
                 ->all();
        return $accountLists;
    }

    /**
     * 收款账号下拉框
     *
     * @return array $date 下拉框数据
     */
    public static function getAccountList($map = [], $cur = '')
    {
        $accountList = static::getAccount($map, ['sort' => SORT_DESC], 'account_id, no');
        $str = '';
        foreach ($accountList as $item) {
            //$data[$item['no']] = $item['no'];
            $isSelect = $item['no'] == $cur ? "selected='selected'" : '';
            $str .= "<option value='" . $item['no'] . "'" . $isSelect . ">" . $item['no'] . "</option>";
        }
        return $str;
    }

    /**
     * 插入更新前置操作
     */
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            //判断是否是插入操作
            if ($this->isNewRecord) {
                $this->user_id =  Yii::$app->user->identity->user_id;
                $this->ip = Yii::$app->getRequest()->getUserIP();
                $this->create_time = time();
            }
            $this->update_time = time();
            return true;
        } else {
            return false;
        }
    }
}