<?php

namespace common\models;

class PushHouserecommend  extends Common
{

        public function rules()
        {
            return [
                ['hid', 'unique', 'targetClass' => 'common\models\PushHouserecommend','on'=>['add'],'message'=>'该楼盘已存在','filter'=>function($query){
                            $query->andWhere(['del'=>$this->setDel]);
                    return  $query->andWhere(['cat_id'=>$this->cat_id]);
                }],
                ['hid','number','message'=>'请选择楼盘'],
                ['city','number'],
                ['sort','number','message'=>'排序只能是数字'],
                ['sort','number','max'=>10000],
                ['cat_id','number'],
            ];
        }

        public function attributeLabels()
        {
            return [
                'hid'=>'楼盘名称',
                '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_house.thumb','pfg_push_houserecommend.id','pfg_push_houserecommend.hid','pfg_house.sale_price','pfg_house.characteristic','pfg_house_detail.price_unit','pfg_house_detail.main_units','pfg_push_houserecommend.cat_id','pfg_push_houserecommend.create_at','pfg_category_city.city_name']);
            $query->andWhere(['pfg_push_houserecommend.del'=>$this->setDel]);
            // $query->andWhere(['pfg_push_houserecommend.cat_id'=>$input['cat_id']]);
            if(isset($input['city'])){
                $query->andFilterWhere(['pfg_category_city.id'=>$input['city']]);
            }
            if(!empty($input['house_name']))
            {
                $query->andFilterWhere(['like', 'pfg_house.name', trim($input['house_name']," ")]);
            }
            $query->leftJoin('pfg_house','pfg_push_houserecommend.hid=pfg_house.id');
            $query->leftJoin('pfg_house_detail','pfg_push_houserecommend.hid=pfg_house_detail.hid');
            $query->leftJoin('pfg_category_city','pfg_house.city=pfg_category_city.id');
            $query->limit = 7;
            return $query->orderBy(['pfg_push_houserecommend.sort'=>SORT_DESC])->asArray()->all();
        }

        public function getHouseList($input)
        {
            $query = self::find();
            $query->select(['pfg_house.name','pfg_house.thumb','pfg_push_houserecommend.id','pfg_push_houserecommend.sort','pfg_push_houserecommend.hid','pfg_house.sale_price','pfg_house.characteristic','pfg_house_detail.price_unit','pfg_house_detail.main_units','pfg_push_houserecommend.cat_id','pfg_push_houserecommend.create_at','pfg_category_city.city_name']);
            $query->andWhere(['pfg_push_houserecommend.del'=>$this->setDel]);
            $query->andWhere(['pfg_push_houserecommend.city'=>$input['cat_id']]);
            if(isset($input['city'])){
                $query->andFilterWhere(['pfg_push_houserecommend.city'=>$input['city']]);
            }
            if(!empty($input['house_name']))
            {
                $query->andFilterWhere(['like', 'pfg_house.name', trim($input['house_name']," ")]);
            }
            $query->leftJoin('pfg_house','pfg_push_houserecommend.hid=pfg_house.id');
            $query->leftJoin('pfg_house_detail','pfg_push_houserecommend.hid=pfg_house_detail.hid');
            $query->leftJoin('pfg_category_city','pfg_house.city=pfg_category_city.id');
            if(!empty($input['page']))
            {
                $query->offset = ($input['page'] - 1) * $input['limit'];
                $query->limit = $input['limit'];
            }
            return $query->orderBy(['pfg_push_houserecommend.sort'=>SORT_DESC])->asArray()->all();
        }

        public function WhereColumn($query)
        {

        }

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


        public function Homegetlist($limit)
        {
            $query = self::find();
            $query->select(['pfg_category_city.city_name','pfg_house.name','pfg_house.sale_price','pfg_house_detail.price_unit','pfg_house.characteristic','pfg_house_detail.main_units','pfg_house.id','pfg_house.thumb']);
            $query->andWhere(['pfg_push_houserecommend.del'=>$this->setDel]);
            $query->andWhere(['pfg_push_houserecommend.is_show'=>1]);
            $query->andWhere(['pfg_push_houserecommend.cat_id'=>$this->cat_id]);
            $query->andFilterWhere(['pfg_push_houserecommend.city'=>$this->city]);
            $query->leftJoin('pfg_house','pfg_push_houserecommend.hid=pfg_house.id');
            $query->leftJoin('pfg_category_city','pfg_house.city=pfg_category_city.id');
            $query->leftJoin('pfg_house_detail','pfg_house.id=pfg_house_detail.hid');
            $query->orderBy(['pfg_push_houserecommend.sort'=>SORT_ASC]);
            $query->limit = $limit;
            return $query->asArray()->all();
        }

}