<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/2/26/026
 * Time: 14:54
 */

namespace common\models;
//use yii\db\ActiveRecord;
//use yii\behaviors\TimestampBehavior;
//use Yii;
class UserRole extends Common
{

//        public function behaviors()
//        {
//            return [
//                [
//                    'class' => TimestampBehavior::className(),
//                    'attributes' => [
//                        # 创建之前
//                        ActiveRecord::EVENT_BEFORE_INSERT => ['create_at', 'update_at'],
//                        # 修改之前
//                        ActiveRecord::EVENT_BEFORE_UPDATE => ['update_at']
//                    ],
//                    #设置默认值
//                    'value' => $_SERVER['REQUEST_TIME']
//                ]
//            ];
//        }

        public function rules()
        {
            return [
                    ['name','required','message'=>'部门名称不能为空'],
                    ['name', 'unique', 'targetClass' => 'common\models\UserRole','on'=>['add'],'message'=>'该部门名称已存在'],
                    ['name','string','min'=>2,'max'=>20,'message'=>'{attribute}长度必须在{min}到{max}之间'],
//                    ['describe','required','message'=>'部门描述不能为空'],
                    ['describe','string','max'=>200,'message'=>'{attribute}长度必须在{min}到{max}之间'],
                    ['state','in','range'=>[1,2]],
                   ];
        }


        public function Check($input,$save = true)
        {
               $this->load($input,'');
               if(!$this->validate()) return $this->errors;
               return $this;
        }

        public function FindById($id)
        {
            return self::findOne($id);
        }


        public function getList($input)
        {
            $query = self::find();
            if(!empty($input['page']))
            {
                $query->offset = ($input['page'] -1) * $input['limit'];
                $query->limit =  $input['limit'];
            }
            $row['count'] = $query->count();
            $row['data']  = $query->orderBy('create_at')->asArray()->all();
            return $row;
        }

}