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

namespace qcbase\models;

use Yii;
use yii\db\ActiveRecord;

/**
* This is the model class for table "qc_payment_method".
* 
* 合同付款方式控制器
*
* @property integer $method_id  (付款方式ID)
* @property string $name  (合同名称)
* @property integer $user_id  (创建人，关联表qc_user)
* @property integer $create_time  (创建时间)
* @property integer $update_time  (更新时间)
* @property integer $sort  (排序)
* @property smallint $status  (状态：0 禁用 1开启 99逻辑删除 默认开启)
* @property string $ip  (创建人IP)
*
* @author zealxd http://blog.u-bo.com
* @since 0.1
*/
class PaymentMethod extends ActiveRecord
{
    const STATUS_ENABLE = 1;//启用
    const STATUS_DISABLE = 0;//禁用

    /**
    * @inheritdoc
    */
    public static function tableName()
    {
    return '{{%payment_method}}';
    }
    
    /**
    * @inheritdoc
    */
    public function rules()
    {
    return [
            [['name', 'status'], 'required'],
            [['user_id', 'create_time', 'update_time', 'sort', 'status'], 'integer'],
            [['name'], 'string', 'max' => 100],
            [['ip'], 'string', 'max' => 15]
        ];
    }

    /**
    * @inheritdoc
    */
    public function attributeLabels()
    {
        return [
            'method_id' => 'ID',
            'name' => '付款类型',
            'user_id' => '创建人ID',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'sort' => '排序',
            'status' => '状态',
            'ip' => 'Ip',
        ];
    }

    /**
     * @return type返回状态
     */
    public static function getStatusList()
    {
        return [
            self::STATUS_ENABLE => '启用',
            self::STATUS_DISABLE => '禁用',
        ];
    }

    /**
     * 公共搜索所需信息 
     */
    public function search()
    {
        return [
            'name' => '付款方式',
        ];
    }

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

}
