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

namespace home\modules\customer\models;

use Yii;

/**
 * 客户类别表 ({{%customer_level}})
 *
 * 客户类别模型
 *
 *@property integer  $level_id     (客户等级ID)
 *@property string   $name         (客户等级名称)
 *@property integer  $sort         (排序)
 *@property smallint $status       (状态：0 禁用 1开启 99逻辑删除 默认开启)
 *@property integer  $user_id      (创建人，关联表qc_user)
 *@property integer  $create_time  (创建时间)
 *@property integer  $update_time  (更新时间)
 *@property string   $ip           (创建人IP)
 *
 * @author zealxd http://blog.u-bo.com
 * @since 0.1
 */
class Level extends \yii\db\ActiveRecord
{
    const STATUS_ENABLE  = 1;  //启用
    const STATUS_DISABLE = 0;  //禁用

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%customer_level}}';
    }

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

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'level_id'    => 'ID',
            'name'        => '等级名称',
            'sort'        => '排序',
            'status'      => '状态',
            'user_id'     => '用户id',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
            'ip'          => 'ip地址',
        ];
    }

    /**
     * 查询资源
     *
     * @param type $map  查询条件
     * @param type $order   排序
     * @param type $field 查询字段 默认全部（*）
     * @return type 返回值
     */
     public static function getCustomerLevel($map = [], $order = ['level_id' => SORT_ASC], $field = "*")
    {
        $customerLevelList = static::find()
                ->select($field)
                ->where($map)
                ->orderBy($order)
                ->asArray()
                ->all();
        return $customerLevelList;
    }

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

    /**
     * 公共搜索所需信息
     */
    public function search()
    {
        return [
			'keywords' => [
				'name' => '等级名称',
			],
			'status' => self::getStatusList(),
        ];
    }

    /**
     * 插入更新前置操作
     */
    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;
        }
    }


}
