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

namespace common\models;
use Yii;
//use yii\db\ActiveRecord;
//use yii\behaviors\TimestampBehavior;
//use yii\captcha\Captcha;
class User extends Common
{
		public $order = 'status ASC';
        public $repassword; //确认密码
        public $verifycode;
//        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 [
                    ['user','required','message'=>'不能为空'],
                    ['password','required','message'=>'密码不能为空'],
                    ['password','match','pattern'=>Yii::$app->params['match']['password'],'message'=>'请输入正确的密码'],
                    ['repassword', 'compare', 'compareAttribute' => 'password','on'=>['Signup','savepwd']],
                    ['user', 'unique', 'targetClass' => 'common\models\User','on'=>['Signup']],
                    ['repassword','required','message'=>'确认密码不能为空','on'=>['Signup']],
                    ['name','required','message'=>'姓名不能为空','on'=>['Signup']],
                    ['verifycode', 'captcha','captchaAction'=>'/public/captcha','on'=>['login'],'message'=>'请输入正确的验证码!'],
                    ['name','string','max'=>6],
                    ['status','in','range'=>[1,2]],

           ];
        }

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

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

        public function findUserName()
        {
            $query = self::find();
            $query = $this->Setonditions($query);
            return $query->one();
        }

        public function getList($input,$field = null )
        {
            $query = self::find();
            $query->select($field);
            $query = $this->Setonditions($query);
            $query->orderBy($this->order);
            if(Yii::$app->session['user_info']['user'] != 'admin'){
                $query->andFilterWhere(['not',"user = 'admin'"]);
            }
            if(!empty($input))
            {
                $query->offset = ($input['page'] -1) * $input['limit'];
                $query->limit = $input['limit'];
            }
            return $query->orderBy(['status'=>SORT_ASC])->asArray()->all();
        }

        public function Setonditions($query)
        {
            $query->andFilterWhere(['user'=>$this->user]);
            $query->andFilterWhere(['uid'=>$this->uid]);
            return $query;
        }


        /*
         * 返回数据条数
         * */
        public function Total()
        {
            $query =  self::find();
            $query = $this->Setonditions($query);
            return $query->count();
        }

        public function setPassword($pwd)
        {
            return md5(md5($pwd));
        }

        public function getNews()
        {
            return $this->hasMany(News::className(),['author'=>'name']);
        }


}