<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/3/4
 * Time: 上午9:30
 */

namespace common\models;

//use yii\db\ActiveRecord;
//use yii\behaviors\TimestampBehavior;
class Characteristic extends Common
{

        public function rules()
        {
            return [
                ['name','required','message'=>'不能为空'],
                ['name', 'unique', 'targetClass' => 'common\models\Characteristic','on'=>['add'],'message'=>'该特色主题已存在','filter'=>function($query){
                    return $query->andWhere(['del'=>$this->setDel]);
                }],
//                ['name', 'unique', 'targetClass' => 'common\models\Characteristic','message'=>'该名称已经存在'],
                ['name','string','max'=>50,'message'=>'请输入最大100位的值'],
                [['del','is_show'],'in','range'=>[1,2]],

            ];
        }

        public function getList($input,$select = null)
        {
            $query = $this->FindQuery();
            $query->select($select);
            $query = $this->WhereFocus($query);
            if(!empty($input['page']))
            {
                $query->limit = $input['limit'];
                $query->offset = ($input['page']-1 )* $input['limit'];
            }
            return $query->orderBy(['create_at'=>SORT_DESC])->asArray()->all();
        }
    public function getAllList($input,$select = null)
    {
        $query = $this->FindQuery();
        $query->select($select);
        $query->andFilterWhere(['name'=>$this->name]);
        $query->andWhere(['del'=>$this->setDel]);
        $query->andFilterWhere(['id'=>$this->id]);
        if(!empty($input['page']))
        {
            $query->limit = $input['limit'];
            $query->offset = ($input['page']-1 )* $input['limit'];
        }
        return $query->orderBy(['create_at'=>SORT_DESC])->asArray()->all();
    }


        public function Total()
        {
            $query = $this->FindQuery();
            return $this->WhereFocus($query)->count();
        }

//        public function ManyId()
//        {
//            $query = $this->FindQuery();
//            $query = $this->WhereFocus($query);
//            return $query->asArray()->all();
//        }

        private function WhereFocus($query)
        {
            $query->andFilterWhere(['name'=>$this->name]);
            $query->andWhere(['del'=>$this->setDel]);
            $query->andWhere(['is_show'=>1]);
            $query->andFilterWhere(['id'=>$this->id]);
            return $query;
        }


        private function FindQuery()
        {
            return self::find();
        }

        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 getListAll()
        {
            return self::find()->select(['id','name'])->andWhere(['del'=>1,'is_show'=>1])->asArray()->all();
        }

}