<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/5/21
 * Time: 下午5:18
 */

namespace common\models;


class PushChoice  extends Common
{

        public function rules()
        {
            return [
                [['url'],'required','message'=>'{attribute}不能为空'],
                ['url', 'url', 'defaultScheme' => 'http','message'=>'请输入正确的地址'],
                ['title','string','max'=>50],
                ['city','number'],
                ['hid','number','message'=>'请选择楼盘'],
                ['sort','number','message'=>'排序只能是数字'],
                ['sort','number','max'=>10000],
                ['expiration_date','string'],
            ];
        }

        public function attributeLabels()
        {
            return [
                'hid'=>'楼盘名称',
                'url'=>'地址',
                'city'=>'区域',
            ];
        }


        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)
        {
            $query = self::find();
            $query->select(['pfg_house.name','pfg_push_choice.id','pfg_push_choice.hid','pfg_push_choice.img','pfg_push_choice.url',
                'pfg_push_choice.title','pfg_push_choice.create_at','pfg_push_choice.is_show','pfg_push_choice.expiration_date']);
            $query->andWhere(['pfg_push_choice.del'=>$this->setDel]);
            $query->leftJoin('pfg_house','pfg_push_choice.hid=pfg_house.id');
            if(!empty($input['title']))
            {
                $query->andWhere(['like','pfg_push_choice.title',$input['title']]);
            }
			if(!empty($input['city']))
	        {
	            $query->andWhere(['pfg_push_choice.city'=>$input['city']]);
	        }
            if(!empty($input['page']))
            {
                $query->limit = $input['limit'];
                $query->offset = ($input['page']-1 )* $input['limit'];
            }
            return $query->orderBy(['pfg_push_choice.is_show'=>SORT_ASC,'pfg_push_choice.create_at'=>SORT_DESC])->asArray()->all();
        }

        public function WhereColumn($query)
        {

        }

        public function Total($input)
        {
            $query = self::find();
            if(!empty($input['title']))
            {
                $query->andWhere(['like','pfg_push_choice.title',$input['title']]);
            }
			if(!empty($input['city']))
	        {
	            $query->andWhere(['pfg_push_choice.city'=>$input['city']]);
	        }
            $query->andWhere(['pfg_push_choice.del'=>$this->setDel]);
            $query->leftJoin('pfg_house','pfg_push_choice.hid=pfg_house.id');
            return $query->count();
        }

        public function HomeGetList()
        {
            $query = self::find();
            $query->select(['img','url','title','expiration_date']);
            $query->andFilterWhere(['city'=>$this->city]);
            $query->andWhere(['del'=>$this->setDel]);
            $query->andWhere(['is_show'=>1]);
            $query->orderBy(['sort'=>SORT_ASC]);
            $query->limit = 2;
            return $query->all();
        }


}