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

namespace common\models;

use yii\data\Pagination;

class House extends Common
{

    public $typeHouse;  //户型搜索条件

    public function rules()
    {
//            apartment  保留户型
//            return [
//                [['name','pinyin','state'],'required','message'=>'{attribute}不能为空'],
//                ['name', 'unique', 'targetClass' => 'common\models\House','on'=>'add','message'=>'楼盘名称已经存在'],
//                ['state','in','range'=>[1,2,3,4]],
//                [['sort','sale_price'],'number'],
//                [['name'],'trim'],
////                [['type','city','price','characteristic'],'string','max'=>100],
//                [['city','price','alias_name'],'string','max'=>100],
//                ['type','each','rule'=>['integer'],'message'=>'aaaa'],        //验证
//                ['characteristic','each','rule'=>['integer'],'message'=>'aaaa'],
//                ['architecture_type','each','rule'=>['integer'],'message'=>'aaaa'],
//                ['thumb','string','links','max'=>200],
//                ['del','in','range'=>[1,2]],
//                ['is_push','in','range'=>[1,2]],
//                ['is_push','default','value'=>1],
//            ];
        return [
            [['name', 'pinyin', 'state', 'city'], 'required', 'message' => '{attribute}不能为空'],
            ['name', 'unique', 'targetClass' => 'common\models\House', 'on' => ['add', 'edit'], 'message' => '楼盘名称已经存在', 'filter' => function ($query) {
                return $query->andWhere(['del' => $this->setDel]);
            }],
            ['state', 'in', 'range' => [1, 2, 3, 4]],
            [['sort', 'sale_price', 'province', 'area'], 'number'],
            [['name'], 'trim'],
//                [['type','city','price','characteristic'],'string','max'=>100],
            [['city', 'price', 'alias_name'], 'string', 'max' => 100],
            ['type', 'each', 'rule' => ['integer'], 'message' => '物业类型必填'],        //验证
            ['characteristic', 'each', 'rule' => ['integer'], 'message' => '特色主题必填'],
            ['architecture_type', 'each', 'rule' => ['integer'], 'message' => '建筑类别必填'],
            ['thumb', 'string', 'max' => 200],
            ['del', 'in', 'range' => [1, 2]],
            ['is_push', 'in', 'range' => [1, 2]],
            ['is_push', 'default', 'value' => 1],
            ['sale_price', 'default', 'value' => 0],
            ['is_view', 'default', 'value' => 2],
            ['is_tel', 'string', 'max' => 20],
            ['links', 'string'],
            ['prohibit_city', 'string', 'max' => 255],
            ['prohibit_city', 'default', 'value' => ''],
            ['customer_service', 'string']
        ];
    }

    public function attributeLabels()
    {
        return [
            'name' => '楼盘名称',
            'pinyin' => '楼盘拼音不能为空',
            'type' => '物业类型',
            'city' => '区域',
            'price' => '价格区间',
            'characteristic' => '特色主题',
            'thumb' => '图片',
            'state' => '销售状态',
            'sale_price' => '均价',
            'architecture_type' => '建筑类别',
            'alias_name' => '楼盘别名',
            'is_push' => '主推楼盘',
            'is_tel' => '电话',
            'prohibit_city' => '禁售区域',
        ];
    }


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

    public function getList($page)
    {
        $query = self::find();
        $query->andWhere(['pfg_house.del' => $this->setDel,'pfg_category_city.del'=>1]);
        if (!empty($page['id'])) {
            $query->andFilterWhere(['pfg_house.id' => $page['id']]);

        }

        if (!empty($page['city'])) {
            $query->andFilterWhere(['pfg_house.city' => $page['city']]);
        }


        if (!empty($page['start_time'])) {
            $query->andFilterWhere(['>=', 'pfg_house.create_at', strtotime($page['start_time'] . ' 00:00:00')]);
        }

        if (!empty($page['end_time'])) {
            $query->andFilterWhere(['<=', 'pfg_house.create_at', strtotime($page['end_time'] . ' 23:59:59')]);
        }

        if (!empty($page['name'])) {
            $query->andFilterWhere(['like', 'pfg_house.name', trim($page['name'], " ")]);
        }

        if (!empty($page['pinyin'])) {
            $query->andFilterWhere(['like', 'pfg_house.pinyin', $page['pinyin']]);
        }

        if (!empty($page['characteristic'])) {
            $query->andFilterWhere(['like', 'pfg_house.characteristic', $page['characteristic']]);
        }

        if (!empty($page['price'])) {
            $query->andFilterWhere(['pfg_house.price' => $page['price']]);
        }

        if (!empty($page['is_view'])) {
            $query->andWhere(['pfg_house.is_view' => $page['is_view']]);
        }

        if (!empty($page['price_unit']) && $page['price_unit'] == 'price_unit') {
            $query->andWhere(['NOT', ['pfg_house.sale_price' => '']]);
            $query->andWhere(['pfg_house_detail.price_unit' => '']);
        }

        if (!empty($page['price_unit']) && $page['price_unit'] == 'is_permit') {
            $s = HousePermit::find()->select(['hid'])->groupBy('hid')->asArray()->all();

            $query->andWhere(['not in', 'pfg_house.id', array_column($s, 'hid')]);
        }

        if (!empty($page['price_unit']) && $page['price_unit'] == 'discount') {
            $query->andWhere(['pfg_house_detail.preferential' => '']);
        }

        if (!empty($page['price_unit']) && $page['price_unit'] == 'discounts') {
            $query->andWhere(['NOT', ['pfg_house_detail.preferential' => '']]);
        }

        if (!empty($page['sort'])) {

            if ($page['sort'] == 'sort') {
                $query->orderBy(['pfg_house.sort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
            }
            if ($page['sort'] == 'map') {
                $query->orderBy(['pfg_house.is_push'=>SORT_DESC,'pfg_house.sort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
            }
            if ($page['sort'] == 'regionsort') {
                $query->orderBy(['pfg_house_detail.regionsort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
            }

            if ($page['sort'] == 'allsort') {
                $query->orderBy(['pfg_house_detail.allsort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
            }

        }

        if (!empty($page['is_push'])) {
            $query->andWhere(['pfg_house.is_push' => $page['is_push']]);
            $query->orderBy(['pfg_house.create_at' => SORT_DESC]);
        }


        $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->leftJoin('pfg_user_house', 'pfg_house.id=pfg_user_house.hid');
        $query->leftJoin('pfg_user', 'pfg_user_house.uid=pfg_user.uid');
        $query->select(['pfg_house.create_at', 'pfg_user.name as user_name', 'pfg_category_city.city_name', 'pfg_house.id', 'pfg_house.name', 'pfg_house.sort', 'pfg_house.click_num', 'pfg_house.is_view', 'pfg_house.sale_price', 'pfg_house.thumb', 'pfg_house.is_push', 'pfg_house_detail.evaluating', 'pfg_house_detail.longitude_latitude', 'pfg_house.characteristic', 'pfg_house_detail.main_units', 'pfg_house_detail.price_unit', 'pfg_house.sale_price', 'pfg_house_detail.allsort', 'pfg_house_detail.regionsort']);
        if (!empty($page['page'])) {
            $query->offset = ($page['page'] - 1) * $page['limit'];
            $query->limit = $page['limit'];
        }
        return $query->asArray()->all();
    }


    public function getListTotal($page)
    {
        $query = self::find();
        $query->andWhere(['pfg_house.del' => $this->setDel]);
        if (!empty($page['id'])) {
            $query->andFilterWhere(['pfg_house.id' => $page['id']]);

        } else if (!empty($page['city'])) {
            $query->andFilterWhere(['pfg_house.city' => $page['city']]);
        } else if (!empty($page['name'])) {
            $query->andFilterWhere(['like', 'pfg_house.name', trim($page['name'], "")]);
        }

        if (!empty($page['characteristic'])) {
            $query->andFilterWhere(['like', 'pfg_house.characteristic', $page['characteristic']]);
        }

        if (!empty($page['price'])) {
            $query->andFilterWhere(['pfg_house.price' => $page['price']]);
        }

        if (!empty($page['is_view'])) {
            $query->andWhere(['pfg_house.is_view' => $page['is_view']]);
        }

        if (!empty($page['start_time'])) {
            $query->andFilterWhere(['>=', 'pfg_house.create_at', strtotime($page['start_time'] . ' 00:00:00')]);
        }

        if (!empty($page['end_time'])) {
            $query->andFilterWhere(['<=', 'pfg_house.create_at', strtotime($page['end_time'] . ' 23:59:59')]);
        }


        if (!empty($page['price_unit']) && $page['price_unit'] == 'price_unit') {
            $query->andWhere(['NOT', ['pfg_house.sale_price' => '']]);
            $query->andWhere(['pfg_house_detail.price_unit' => '']);
        }

        if (!empty($page['price_unit']) && $page['price_unit'] == 'discount') {
            $query->andWhere(['pfg_house_detail.preferential' => '']);
        }

        if (!empty($page['price_unit']) && $page['price_unit'] == 'discounts') {
            $query->andWhere(['NOT', ['pfg_house_detail.preferential' => '']]);
        }


        if (!empty($page['is_push'])) {
            $query->andWhere(['pfg_house.is_push' => $page['is_push']]);
        }

        if (!empty($page['price_unit']) && $page['price_unit'] == 'is_permit') {
            $s = HousePermit::find()->select(['hid'])->groupBy('hid')->asArray()->all();
//                $a =  self::find();
//                $a->select(['pfg_house_permit.hid']);
//                $a->leftJoin('pfg_house_permit','pfg_house.id = pfg_house_permit.hid');
//                $a->asArray();
//                $s = $a->all();

            $query->andWhere(['not in', 'pfg_house.id', array_column($s, 'hid')]);
        }

        $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->leftJoin('pfg_user_house', 'pfg_house.id=pfg_user_house.hid');
        $query->leftJoin('pfg_user', 'pfg_user_house.uid=pfg_user.uid');
        return $query->count();
    }


    public function HousesAll()
    {
        $query = self::find();
        $query->andWhere(['pfg_house.id' => $this->id]);
        $query->andWhere(['pfg_house.del' => $this->setDel]);
        $query->select(['pfg_house.*', 'pfg_house_detail.*']);
        $query->leftJoin('pfg_house_detail', 'pfg_house.id=pfg_house_detail.hid');
        return $query->asArray()->one();
    }

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

    public function total()
    {
        $query = $this->QueryFind();
        $query = $this->WhereColumn($query);
        return $query->count();
    }

    //楼盘和楼盘附表信息
    public function QgetList($arr = null, $sort = null)
    {

        $query = $this->QueryFind();
        $query->select($arr);
        $query = $this->LeftJoin_where($query);
        $query = $this->LeftJoin_house_detail($query);
        $query = $this->LeftJoin_house_type($query);
        $query = $this->LeftJoin_house_city($query);
        $pages = new Pagination(['totalCount' => $this->PaginationTotal(), 'pageSize' => 8, 'pageSizeParam' => false, 'validatePage' => false,]);
        $query->offset($pages->offset)->limit($pages->limit);

//
//            if(!isset($sort['py']) && $_SERVER['HTTP_HOST'] == $_SERVER['SERVER_NAME']){
//
//                    $query->orderBy(['pfg_house_detail.allsort'=>SORT_DESC,'pfg_house_detail.hid'=>SORT_DESC]);
//
//
//                }
//                else{
//                    $query->orderBy(['pfg_house.sort'=>SORT_DESC]);
////                    $query->orderBy(['pfg_house_detail.regionsort'=>SORT_DESC,'pfg_house_detail.hid'=>SORT_DESC]);
//
//                 }

//            if(isset($sort['son']) && $sort['son'] == 'son'){
//
//                $query->orderBy(['pfg_house.sort'=>SORT_DESC]);
//
//            }

        //            if (!empty($sort['renqi'])){
//                $query->orderBy(['pfg_house.is_push'=>SORT_DESC]);
//
//            } else if(isset($sort['pricesort'])) {
//
//                switch ($sort['pricesort'])
//                {
//                    case 1:
//                        $query->orderBy(['pfg_house.sale_price'=>SORT_ASC]);
//                        break;
//                    case 2:
//                        $query->orderBy(['pfg_house.sale_price'=>SORT_DESC]);
//                        break;
//                }
//
//            }

        if (\Yii::$app->hostserver->hostId == 0) {

            $query->orderBy(['pfg_house_detail.allsort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);

            if (!empty($sort['py']) && is_array($sort['city'])) {

                $query->orderBy(['pfg_house_detail.regionsort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);

            } else if (!is_array($sort['city'])) {

                $query->orderBy(['pfg_house.sort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
            }

        } else {

            $query->orderBy(['pfg_house_detail.regionsort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);

            if (!is_array($sort['city'])) {

                $query->orderBy(['pfg_house.sort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
            }
        }

        if (!empty($sort['renqi'])) {

            $query->orderBy(['pfg_house.click_num' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
        }

        $models = $query->asArray()->all();

        $data['page'] = $pages;
        $data['data'] = $models;

        return $data;
    }

    public function MapSearch($arr)
    {
        $query = $this->QueryFind();
        $query->select($arr);
        $query = $this->LeftJoin_where($query);
        $query = $this->LeftJoin_house_detail($query);
        $query = $this->LeftJoin_house_type($query);
        $query = $this->LeftJoin_house_city($query);
        return $query->asArray()->all();
    }

    //总数
    public function PcHouseListTotal()
    {
        $query = $this->QueryFind();
        $query = $this->LeftJoin_where($query);
        $query = $this->LeftJoin_house_detail($query);
        $query = $this->LeftJoin_house_type($query);
        $query = $this->LeftJoin_house_city($query);
        return $query->count();
    }

    public function PaginationTotal()
    {
        $query = $this->QueryFind();
        $query = $this->LeftJoin_where($query);
        $query = $this->LeftJoin_house_detail($query);
        $query = $this->LeftJoin_house_type($query);
        $query = $this->LeftJoin_house_city($query);
        return $query->count();
    }

    //pc端楼盘列表
    public function PcHouseList($input, $select = null, $sort = null)
    {
        $query = $this->QueryFind();
        $query->select($select);
        $query = $this->LeftJoin_where($query);
        $query = $this->LeftJoin_house_detail($query);
        $query = $this->LeftJoin_house_type($query);
        $query = $this->LeftJoin_house_city($query);
        if (!empty($input['hao'])) {
            $query = $this->InnerJoin_Mansion($query);
        }

        if (!isset($sort['py']) && $_SERVER['HTTP_HOST'] == $_SERVER['SERVER_NAME']) {
            if (\Yii::$app->hostserver->hostId != 0) {
                $query->orderBy(['pfg_house_detail.regionsort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
            } else {
                $query->orderBy(['pfg_house_detail.allsort' => SORT_DESC, 'pfg_house_detail.hid' => SORT_DESC]);
            }

        } else {
            if ($sort != null) {
                switch ($sort) {
                    case '1':
                        $query->orderBy(['pfg_house.sale_price' => SORT_ASC]);
                        break;
                    case '2':
                        $query->orderBy(['pfg_house.sale_price' => SORT_DESC]);
                        break;
                }
            }
            if (\Yii::$app->hostserver->hostId != 0) {
                $query->orderBy(['pfg_house_detail.regionsort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
            } else {
                $query->orderBy(['pfg_house.sort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
            }
        }

        if (!empty($input['city'])) {

            $query->orderBy(['pfg_house.sort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
        }

        //加入豪宅排序
        if (!empty($input['hao'])) {
            if (!empty($input['sort'])) {
                if ($input['sort'] != null) {
                    switch ($input['sort']) {
                        case '1':
                            $query->orderBy(['pfg_house.sale_price' => SORT_ASC, 'pfg_house.id' => SORT_DESC]);
                            break;
                        case '2':
                            $query->orderBy(['pfg_house.sale_price' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
                            break;
                    }
                }
            } else {

                $query->orderBy(['pfg_house.sort' => SORT_DESC, 'pfg_house.id' => SORT_DESC]);
            }
        }

        if (!empty($input['page']) && !empty($input['limit']) && is_numeric($input['page'])) {
            $query->offset = ($input['page'] - 1) * $input['limit'];
            $query->limit = $input['limit'];
        } else {
            $query->limit = 10;
        }

        return $query->asArray()->all();
    }

    //前端调用
    public function Qdetails($sele = null)
    {
        $query = $this->QueryFind();
        $query->select($sele);
        $query->andWhere(['pfg_house.del' => $this->setDel]);
        $query->andWhere(['pfg_house.is_view' => 1]);//上线打开
        $query->andFilterWhere(['pfg_house.id' => $this->id]);
        $query->andFilterWhere(['pfg_category_city.state' => 1]);
        $query->andFilterWhere(['pfg_house.city' => $this->city]);
        $query->andFilterWhere(['pfg_house.price' => $this->price]);

        $query->andFilterWhere(['like', 'pfg_house.characteristic', $this->characteristic]);
        $query->andFilterWhere(['or like', 'pfg_house.name', $this->name]);
        $query->andFilterWhere(['or like', 'pfg_house.pinyin', $this->pinyin]);
        $query->andFilterWhere(['pfg_house_type.type_id' => $this->typeHouse]);
        $query = $this->LeftJoin_house_detail($query);
        $query = $this->LeftJoin_Category_city($query);
        return $query->asArray()->one();
    }

    //
    public function HomegetList($limit, $input = null)
    {
        $query = $this->QueryFind();
        //处理价格是待定的状态
        if (isset($input['sale_price'])) {
            $query->andWhere(['pfg_house.sale_price' => $input['sale_price']]);
        }
        $query->select(['pfg_category_city.city_name', 'pfg_house.name', 'pfg_house.city', 'pfg_house.sale_price', 'pfg_house_detail.price_unit', 'pfg_house.id', 'pfg_house.thumb', 'pfg_house_detail.main_units', 'pfg_house.characteristic']);
        $query = $this->LeftJoin_where($query);
        $query = $this->LeftJoin_house_detail($query);
        $query = $this->LeftJoin_Category_city($query);
        if (!empty($input['page'])) {
            $query->offset = ($input['page'] - 1) * $input['limit'];
            $query->limit = $input['limit'];
        } else {
            $query->limit = $limit;
        }

        return $query->orderBy(['pfg_house.sort' => SORT_DESC, 'pfg_house.id' => SORT_DESC])->asArray()->all();
    }

    /*
     * 传入条件 随机返回数据
     * */
    public function randList($limit, $input = null)
    {
        $query = $this->QueryFind();
        //处理价格是待定的状态
//            if(isset($input['price'])&&!empty($input['price'])){
//                $query->andWhere(['pfg_house.price'=>$input['price']]);
//            }

        $query->select(['pfg_category_city.id as city', 'pfg_category_city.city_name', 'pfg_house.name', 'pfg_house.sale_price', 'pfg_house_detail.price_unit', 'pfg_house.id', 'pfg_house.thumb', 'pfg_house_detail.address', 'pfg_house.characteristic', 'pfg_house.state']);
        $query = $this->LeftJoin_where($query);
        $query = $this->LeftJoin_house_detail($query);
        $query = $this->LeftJoin_Category_city($query);
        if (!empty($input['house_id'])) {
            $query->andWhere(['<>', 'pfg_house.id', $input['house_id']]);
        }
        $query->limit = $limit;
        return $query->orderBy('RAND()')->asArray()->all();
    }

    /*
    * 传入条件 随机返回主推楼盘
    * */
    public function ispush($limit, $input = null)
    {
        $query = $this->QueryFind();
        //处理价格是待定的状态
//            if(isset($input['price'])&&!empty($input['price'])){
//                $query->andWhere(['pfg_house.price'=>$input['price']]);
//            }

        $query->select(['pfg_category_city.id as city', 'pfg_category_city.city_name', 'pfg_house.name', 'pfg_house.sale_price', 'pfg_house_detail.price_unit', 'pfg_house.id', 'pfg_house.thumb', 'pfg_house_detail.address', 'pfg_house.characteristic']);
        $query = $this->LeftJoin_where($query);
        $query = $this->LeftJoin_house_detail($query);
        $query = $this->LeftJoin_Category_city($query);
        if (!empty($input['page'])) {
            $query->offset = ($input['page'] - 1) * 5;
            $query->limit = 5;
        } else {
            $query->limit = $limit;
        }
        return $query->orderBy('RAND()')->asArray()->all();
    }


    /*
   * 传入条件 随机返回主推楼盘
   * */
    public function ispushcount()
    {
        $query = $this->QueryFind();
        $query = $this->LeftJoin_where($query);
        $query = $this->LeftJoin_house_detail($query);
        $query = $this->LeftJoin_Category_city($query);
        return $query->count();
    }





    //        /**
//         * 主推楼盘,
//         */
//        public function PushList($limit,$input = null)
//        {
//            $query = $this->QueryFind();
//            //处理价格是待定的状态
//            if(isset($input['price'])){
//                $query->andWhere(['pfg_house.sale_price'=>$input['price']]);
//            }
//
//            $query->select(['pfg_category_city.id as city','pfg_category_city.city_name','pfg_house.name','pfg_house.sale_price','pfg_house_detail.price_unit','pfg_house.id','pfg_house.thumb','pfg_house_detail.address','pfg_house.characteristic']);
//            $query = $this->LeftJoin_where($query);
//            $query = $this->LeftJoin_house_detail($query);
//            $query = $this->LeftJoin_Category_city($query);
//            $query->limit = $limit;
//            return $query->orderBy(['pfg_houseis_pu'])->asArray()->all();
//        }

    public function OneDetails($sele = null)
    {
        return self::find()->select($sele)->andWhere(['del' => $this->setDel])->andFilterWhere(['id' => $this->id])->asArray()->one();
    }

    public function HouseName()
    {
        $query = $this->QueryFind();
        $query->andWhere(['pfg_house.del' => $this->setDel]);
        $query->andWhere(['pfg_house.is_view' => 1]);  //上线打开
        $query->andWhere(['pfg_category_city.state' => 1]);
        $query->andFilterWhere(['pfg_house.is_push' => $this->is_push]);
        $query->andFilterWhere(['like', 'pfg_house.name', $this->name]);
        $query->andFilterWhere(['like', 'pfg_house.pinyin', $this->pinyin . '%', false]);

        //限制海南IP的操作
        $query->andWhere(['pfg_house.prohibit_city' => '']);
        $query = $this->LeftJoin_Category_city($query);
        $query = $this->LeftJoin_house_detail($query);
        $query->select(['pfg_house.name', 'pfg_house.id', 'pfg_house.sale_price', 'pfg_category_city.city_name', 'pfg_house_detail.price_unit']);
        return $query->orderBy(['pfg_house.sort' => SORT_DESC])->asArray()->all();
    }


    public function CityHouse()
    {
        $query = $this->QueryFind();
        $query->andWhere(['pfg_house.del' => $this->setDel]);
        $query->andWhere(['pfg_house.is_view' => 1]);  //上线打开
        $query->andFilterWhere(['like', 'pfg_house.name', $this->name]);
        $query->andFilterWhere(['like', 'pfg_house.city', $this->city]);
        $query->andFilterWhere(['like', 'pfg_house.pinyin', $this->pinyin . '%', false]);
        $query = $this->LeftJoin_Category_city($query);
        $query = $this->LeftJoin_house_detail($query);
        $query->select(['pfg_house.name', 'pfg_house.id', 'pfg_house.sale_price', 'pfg_category_city.city_name', 'pfg_house_detail.price_unit', 'pfg_house.thumb', 'pfg_house_detail.longitude_latitude', 'pfg_house_detail.main_units', 'pfg_house_detail.address']);
        return $query->orderBy(['pfg_house.sort' => SORT_DESC])->asArray()->all();
    }

    //手机端 首页热门楼盘调用
    public function HotList($limit = 5)
    {
        $query = $this->QueryFind();
        $query->select(['pfg_category_city.id as cid', 'pfg_category_city.city_name', 'pfg_house.name', 'pfg_house.sale_price', 'pfg_house_detail.price_unit', 'pfg_house.characteristic', 'pfg_house.id', 'pfg_house.thumb']);
        $query = $this->LeftJoin_where($query);
        $query = $this->LeftJoin_Category_city($query);
        $query = $this->LeftJoin_house_detail($query);
        $query->limit = $limit;
        return $query->orderBy(['pfg_house.sort' => SORT_DESC, 'id' => SORT_DESC])->asArray()->all();
    }

    protected function LeftJoin_where($query)
    {
        $query->andWhere(['pfg_house.del' => $this->setDel]);
        $query->andWhere(['pfg_house.is_view' => 1]);//上线打开
        $query->andFilterWhere(['pfg_house.id' => $this->id]);
        $query->andFilterWhere(['pfg_category_city.state' => 1]);
        $query->andFilterWhere(['pfg_category_city.abroad' => 1]); // 国内
        $query->andFilterWhere(['pfg_house.city' => $this->city]);
        $query->andFilterWhere(['pfg_house.price' => $this->price]);
        $query->andFilterWhere(['pfg_house.is_push' => $this->is_push]);
        $query->andFilterWhere(['pfg_house.sale_price' => $this->sale_price]);
        $query->andFilterWhere(['like', 'pfg_house.characteristic', $this->characteristic]);

        if (!empty(\Yii::$app->request->get('name'))) {
            $city = CategoryCity::find()->andWhere(['del' => 1])->andWhere(['like', 'city_name', \Yii::$app->request->get('name')])->select(['id'])->column();

            $query->andFilterWhere(['or like', 'pfg_house.name', $this->name]);
            $query->orFilterWhere(['pfg_house.city' => $city]);
        }

        $query->andFilterWhere(['or like', 'pfg_house.pinyin', $this->pinyin]);
        $query->andFilterWhere(['pfg_house_type.type_id' => $this->typeHouse]);

        if ((!empty($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] == '/house/vr') || (!empty($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] == '/house/vr/')) {
            $query->andWhere(['NOT', ['pfg_house_detail.panorama' => '']]);
        }

        $query->andWhere(['pfg_house.prohibit_city' => '']); //限制无法搜索
        return $query;
    }

    protected function LeftJoin_Category_city($query)
    {
        return $query->leftJoin('pfg_category_city', 'pfg_house.city=pfg_category_city.id');
    }

    //豪宅
    protected function InnerJoin_Mansion($query)
    {
        $query->andWhere(['pfg_pushm_mansion.is_show' => 1]);
        $query->innerJoin('pfg_pushm_mansion', 'pfg_house.id = pfg_pushm_mansion.hid');
        return $query->leftJoin('pfg_developers', 'pfg_pushm_mansion.did = pfg_developers.id');
    }

    //豪宅总数
    public function Count_Mansion()
    {
        $query = self::find();
        $query->andWhere(['pfg_pushm_mansion.is_show' => 1]);
        $query->innerJoin('pfg_pushm_mansion', 'pfg_house.id = pfg_pushm_mansion.hid');
        return $query->count();
    }

    protected function LeftJoin_house_detail($query)
    {
        return $query->leftJoin('pfg_house_detail', 'pfg_house.id=pfg_house_detail.hid');
    }

    protected function LeftJoin_house_city($query)
    {
        return $query->leftJoin('pfg_category_city', 'pfg_house.city=pfg_category_city.id');
    }

    protected function LeftJoin_house_type($query)
    {
        return $query;
//            ->leftJoin('pfg_house_type','pfg_house.id=pfg_house_type.hid');
    }


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

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

    //获取别墅
    public function getVilla($select = null, $limit)
    {
        $query = self::find();
        $query->select($select);
        $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->andFilterWhere(['or', ['like', 'pfg_house.type', 18]]);
        $query->limit = $limit;
        return $query->orderBy(['pfg_house.sort' => SORT_DESC])->asArray()->all();
    }

    /*
     * 检测楼盘是否正常显示
     */
    public function HouseIsshow($id)
    {
        return static::find()->andWhere(['id' => $id])->andWhere(['del' => 1])->andWhere(['is_view' => 1])->exists();
    }


    public function GetOneHouse($id)
    {
        $query = self::find();
        $query->andWhere(['pfg_house.del' => 1, 'pfg_house.is_view' => 1, 'pfg_house.id' => $id]);
        $query->select(['pfg_house.id', 'pfg_house.name', 'pfg_house.sale_price', 'pfg_house.characteristic',
            'pfg_house_detail.address', 'pfg_house_detail.price_unit', 'pfg_house_detail.main_units',
            'pfg_house.thumb', 'pfg_house.city', 'pfg_house_detail.longitude_latitude',
            'pfg_category_city.city_name']);
        $query->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid');
        $query->leftJoin('pfg_category_city', 'pfg_house.city = pfg_category_city.id');
        return $query->asArray()->one();
    }


    public function AddNewsHouse()
    {
        return self::find()->andWhere(['del' => 1, 'is_view' => 1])->select(['id', 'name'])->orderBy(['sort' => SORT_DESC, 'id' => SORT_DESC])->asArray()->all();
    }

    public static function FindByHid($id)
    {
        return self::find()->andWhere(['id' => $id, 'del' => 1, 'is_view' => 1])->one();
    }

    public function getCityName()
    {
        return $this->hasOne(CategoryCity::className(), ['id' => 'city'])->select(['pfg_category_city.id', 'pfg_category_city.city_name']);
    }

    public function getHousedetail()
    {
        return $this->hasOne(HouseDetail::className(), ['hid' => 'id']);
    }

    public function getCityNameByProvince()
    {
        return $this->hasOne(CategoryCity::className(), ['id' => 'province'])->select(['id', 'city_name'])->alias('p');
    }

    public function getHouseTypesGroup()
    {
        return $this->hasMany(HouseType::className(), ['hid' => 'id'])
            ->andWhere(['pfg_house_type.del' => 1, 'pfg_house_type.state' => 1])->groupBy(['type_id'])
            ->joinWith(['houseTypeName' => function ($q) {
                $q->select(['pfg_category_housetype.id', 'pfg_category_housetype.huxing_name']);
            }])->asArray();
    }
	
	public function getCityHouseList($limit = null)
	{
		$selct = [
			'pfg_house.name',
			'pfg_house.id',
			'pfg_house.city',
			'pfg_house.sale_price',
			'pfg_category_city.city_name',
			'pfg_house_detail.price_unit',
			'pfg_house.thumb',
			'pfg_house_detail.preferential',
			'pfg_house_detail.main_units',
			'pfg_house_detail.address'
		];
		$query = $this->QueryFind();
		$query->andWhere(['pfg_house.del' => $this->setDel]);
		$query->andWhere(['pfg_house.is_view' => 1]);  //上线打开
		$query->andFilterWhere(['pfg_house.city' => $this->city]);
		$query = $this->LeftJoin_Category_city($query);
		$query = $this->LeftJoin_house_detail($query);
		$query->select($selct);
		if (!empty($limit)) {
			$query->limit($limit);
		}
		return $query->orderBy(['pfg_house.sort' => SORT_DESC, 'id' => SORT_DESC])->asArray()->all();
	}
}