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

namespace common\models;

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

//        public $Setdel = 1;

//        public $item = [1=>'不推荐',2=>'推荐'];

//        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 [

                ['type_name','required','message'=>'不能为空'],
                ['sort','number'],
                ['is_recommend','in','range'=>[1,2]],
                ['del','in','range'=>[1,2],'on'=>'del'],

            ];
        }

        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 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(['sort'=>SORT_DESC,'create_at'=>SORT_DESC])->asArray()->all();
        }

        public function WhereFocus($query)
        {
            $query->andFilterWhere(['type_name'=>$this->type_name]);
            $query->andWhere(['del'=>$this->setDel]);
            return $query;
        }

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


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

}