<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/4/20
 * Time: 下午3:21
 */

namespace frontend\server;

use common\fm\HouseHandle;
use common\fm\Opencc;
use common\models\CategoryCity;
use common\models\House;
use common\models\News;
use common\models\HouseNumber;
use common\models\Search;
use common\models\Video;
use Yii;
use common\models\HouseAlbum;
use common\models\Characteristic;
use common\models\HouseType;
use common\models\HouseFacilities;
use common\models\HouseNews;
use common\models\CategoryLabel;
use common\models\PushHousesale;
use common\models\HousePermit;
use common\models\HousePriceRecord;
use common\models\HouseComment;
use common\models\HouseCommentLikes;
use common\models\HouseCommentreply;

class HouseServer
{

    /*
     * 搜索楼盘名字
     * */
    public function SearchHouseName($input)
    {
        if (!empty($input['title'])) {
            (new Search())->SaveSearchPC($input['title']);
            $query = new House();
            $arr = (new \common\api\HouseServer())->HouseNameConvert($input['title']);

            if (!empty($arr['pinyin'])) {
                $query->pinyin = $arr['pinyin'];
            }
            if (!empty($arr['zhongwen'])) {
                $query->name = $arr['zhongwen'];
            }
            $result = $query->HouseName();
            return $result;
        }
    }

    public function Search($input)
    {
		
        $query = new House();
        //搜索条件
        if (!empty($input['zhuti']) && $input['zhuti'] != 0) {
            $query->characteristic = $input['zhuti'];
        }
	    if (!empty($input['characteristic']) && $input['characteristic'] != 0) {
		    $query->characteristic = $input['characteristic'];
	    }

        if (!empty($input['price']) && $input['price'] != 0) {
            $query->price = $input['price'];
        }

        if (!empty($input['type']) && $input['type'] != 0) {
            $query->type = $input['type'];
        }

//        if(!empty($input['pricesort']) && $input['pricesort'] != 0)
//        {
//            $query->pricesort = $input['pricesort'];
//        }

        if (!empty($input['pinyin'])) {
            $query->pinyin = $input['pinyin'];
        } else {
            if (!empty($input['name'])) {
                $query->name = $input['name'];
            }
        }

        //VR看房
        if (!empty($input['vrread']) && $input['vrread'] != 0) {
            $idList = array_column((new \common\models\Vr())->getColumnHid(), 'hid');
            $query->id = $idList;
        }
        if (!empty($input['city']) && $input['city'] != 0) {
            $query->city = $input['city'];
        }

        //处理价格区间
        if (!empty($query->price)) {
            $price = \common\models\HousesPrice::find();
            $priceModel = $price->andWhere(['id' => $query->price])->one();
            if ($priceModel != null) {
                $query->price = $priceModel->price;
            }
        }

        //户型
        if (!empty($query->type)) {
            $type = new \common\models\HouseType();
            $type->type_id = $query->type;
            $typeModel = $type->getList(['hid']);
            if (!empty($typeModel)) {
                $query->id = array_unique(array_column($typeModel, 'hid'));
            } else {
                $query->id = 0;  //处理 ID没有的情况下,不查询该条件
            }


        }

//        if (!empty($input['name'])) {
//            $arr = (new \common\api\HouseServer())->HouseNameConvert($input['name']);
//            if (!empty($arr['pinyin'])) {
//                $query->pinyin = $arr['pinyin'];
//            }
//            if (!empty($arr['zhongwen'])) {
//                $query->name = $arr['zhongwen'];
//            }
//            $query->city = '';
//        }

        $data = $query->QgetList(['pfg_house_detail.video_url', 'pfg_house_detail.preferential', 'pfg_house_detail.panorama', 'pfg_house.is_tel', 'pfg_house.area', 'pfg_category_city.city_name', 'pfg_house.id', 'pfg_house.city', 'pfg_house.price', 'pfg_house.name', 'pfg_house_detail.address', 'pfg_house_detail.main_units', 'pfg_house_detail.price_unit', 'pfg_house.thumb', 'pfg_house.sale_price', 'pfg_house.characteristic', 'pfg_house_detail.longitude_latitude'], $input);

        if ($data['data'] != null) {
            foreach ($data['data'] as &$val) {

                $val['characteristic'] = \common\fm\HouseHandle::ChangeCharacteristic($val['characteristic']);

                $val['citytel'] = \common\fm\HouseHandle::ChangeCityTel($val['city']);

                if (empty($val['is_tel'])) {
                    if (!empty($val['area']) && \common\fm\HouseHandle::ChangeCityTel($val['area']) != Yii::$app->params['default_dialtel']) {

                        $val['citytel'] = \common\fm\HouseHandle::ChangeCityTel($val['area']);
                    } else {
                        $val['citytel'] = \common\fm\HouseHandle::ChangeCityTel($val['city']);
                    }

                } else {
                    $val['citytel'] = $val['is_tel'];
                }

                //查询是否单独设置楼盘号码
                if (!empty(HouseNumber::SearchNumber($val['id']))) {
                    $val['citytel'] = HouseNumber::SearchNumber($val['id']);
                }

                //                if(empty($val['is_tel']))
//                {
//                    $val['citytel'] = \common\fm\HouseHandle::ChangeCityTel($val['city']);
//
//                }
//                else
//                {
//                    $val['citytel'] = $val['is_tel'];
//                }
//                $val['video'] = $this->Video($val['id']);
            }
            return $data;
        }
    }

    public function Details()
    {
        $input = Yii::$app->request->get();
        $arr = [];
        $seo = [];
        $seo['province'] = '海南省';
        $model = new House();
        $model->id = $input['hid'];
        $select = ['pfg_house.*', 'pfg_house_detail.*', 'pfg_category_city.city_name', 'pfg_category_city.pid as city_pid'];
        $arr['house'] = $model->Qdetails($select);
        if (empty($arr['house'])) return false;

        //楼盘点击量
        $click = new \common\api\HouseServer();
        $click->HouseClick($input['hid']);

        if (!empty($arr['house'])) {
            $seo['city'] = $arr['house']['city_name'];
            if (!empty($arr['house']['city_pid'])) {
                $cityModel = new CategoryCity();
                $city = $cityModel->FindById($arr['house']['city_pid']);
                $seo['province'] = $city['city_name'];
            }

            $arr['seo'] = $seo;
            $arr['characteristic'] = $this->Subject(json_decode($arr['house']['characteristic']), 3);

            $queryAlbum = new HouseAlbum();

            $arr['album'] = $queryAlbum->AlbumGroup(['hid' => $input['hid']]);

            $arr['album_find'] = $queryAlbum->Groupfind(['hid' => $input['hid']]);

            //直播视频
            $arr['house']['live'] = (new \common\models\Live())->GetOneLiveByHouse($input['hid']);

            $perModel = new HousePermit();
            $perModel->hid = $input['hid'];
            $arr['permit'] = $perModel->FindNewest();

            //户型图
            $queryType = new HouseType();
            $queryType->hid = $input['hid'];
            $arr['type'] = $queryType->RandList(['area', 'indoor_info', 'title', 'type_id', 'img','hid'], 4);

            //户型分类
            $arr['type_grouy'] = $queryType->TypeGroup();

            if (empty($arr['house']['is_tel'])) {
                //新增三级联动区域的电话获取修改
                if (!empty($arr['house']['area']) && $this->RandTel($arr['house']['area']) != Yii::$app->params['default_dialtel']) {
                    $arr['house']['tel'] = $this->RandTel($arr['house']['area']);
                } else {
                    $arr['house']['tel'] = $this->RandTel($arr['house']['city']);
                }
            } else {
                $arr['house']['tel'] = $arr['house']['is_tel'];
            }

            $arr['house']['state'] = Yii::$app->params['HouseSalesStatus'][$arr['house']['state']];

            $arr['news'] = (new News())->getHouseNews($arr['house']['name'], 2);

            //建筑类别
            $architecture_type = new CategoryLabel();
            $architecture_type->type = 2;
            $arr['architecture_type'] = array_column($architecture_type->getList([], ['id', 'name']), 'name', 'id');
            $arr['house']['architecture_type'] = json_decode($arr['house']['architecture_type'], true);

            $vr3d = $this->Vr3d($arr['house']['id']);
            $vrcount = \common\models\Vr::find()->where(['hid' => $arr['house']['id'], 'state' => 1, 'type' => 1])->count();
            if(!empty($arr['house']['panorama'])){
                $vrcount += 1;
            }
            if($vrcount > 0){
                array_unshift($arr['type_grouy'], ['huxing_name' => 'VR看房', 'num' => $vrcount, 'hid' => $arr['house']['id'], 'type_id' => 'vr']);
            }

            if (!empty($vr3d)) {
                $arr['house']['panorama'] = $vr3d['path'];
                $arr['house']['panorama_img'] = $vr3d['img'];
            } else {
                if ($arr['house']['panorama_img']) {
                    $arr['house']['panorama_img'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['house_video'] . $arr['house']['panorama_img'] . '/same';
                } else {
                    $arr['house']['panorama_img'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $arr['house']['thumb'] . '/same';
                }
            }

            if (!empty($arr['house']['video_url'])) {
                if (!empty($arr['house']['video_img'])) {
                    $arr['house']['video_img'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['house_video'] . $arr['house']['video_img'] . '/same';
                } else {
                    $arr['house']['video_img'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $arr['house']['thumb'] . '/same';
                }
            }

        } else {
            return false;
        }
        return $arr;
    }

    public function Vr3d($hid)
    {
        $data = \common\models\Vr::find()->where(['hid' => $hid, 'state' => 1, 'type' => 1])->select(['img', 'name', 'path'])->orderBy(['sort' => SORT_DESC])->asArray()->one();
        if (!empty($data)) {
            $data['img'] = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['video'] . $data['img'];
        }
        return $data;
    }

    public function NewsDynamic($input)
    {
        $model = new HouseNews();
        $model->hid = $input['hid'];
        $houseNewsInfo = $model->getHidList();
        $nids = array_column($houseNewsInfo, 'nid');
        $newsModel = new News();
        $newsModel->hid = $input['hid'];
        $result = $newsModel->getNewsListHouse($nids, 2);
        if ($result != null) {
            return $result;
        }
    }

    public function GetInput($input)
    {
        $Arr = [];
        $Arr['title'] = $input['name'];
        $Arr['ip'] = Yii::$app->request->userIP;
        return $Arr;
    }


    //Video楼盘视频
    public function Video($Id)
    {
        $data = Video::find()->select(['video_url', 'thumb'])
            ->andWhere(['del' => 1])->andWhere(['state' => 1])->andWhere(['hid' => $Id])
            ->orderBy(['create_at' => SORT_DESC])->asArray()->one();

        return $data;
    }

    /****Live楼盘直播视频****/
    public function GetLiveInfo($hid)
    {
        $data = \common\models\Live::find()
            ->select('id,hid,video_url,img')
            ->andWhere(['hid' => $hid])
            ->asArray()->one();
        return $data;
    }

    //楼盘详情页数据
    public function ParamsDetails()
    {
        $input = Yii::$app->request->get();
        if (is_numeric($input['hid'])) {
            $result = [];
            $model = new House();
            $model->id = $input['hid'];
            $result['house'] = $model->HousesAll();


            $result['house']['characteristic'] = \common\fm\HouseHandle::ChangeCharacteristic($result['house']['characteristic']);
//                $this->Subject(json_decode($result['house']['characteristic']),4);
            if (empty($result['house']['is_tel'])) {
                if (!empty($result['house']['area']) && HouseHandle::ChangeCityTel($result['house']['area']) != Yii::$app->params['default_dialtel']) {
                    $result['house']['tel'] = \common\fm\HouseHandle::ChangeCityTel($result['house']['area']);
                } else {
                    $result['house']['tel'] = \common\fm\HouseHandle::ChangeCityTel($result['house']['city']);
                }


            } else {
                $result['house']['tel'] = $result['house']['is_tel'];
            }

            //查询是否单独设置楼盘号码
            if (!empty(HouseNumber::SearchNumber($result['house']['id']))) {
                $result['house']['tel'] = HouseNumber::SearchNumber($result['house']['id']);
            }

            $result['house']['architecture_type'] = $this->ArchitectureType(json_decode($result['house']['architecture_type']), 2);

            //预售证号
            $zheng = new HousePermit();
            $result['house']['zhengshu'] = $zheng->getList($input);

            //价格历史
            $price = new HousePriceRecord();
            $price->hid = $input['hid'];
            $result['house']['price_record'] = $price->getList([]);

            $peitao = new HouseFacilities();
            $peitao->hid = $input['hid'];
            $pei = $peitao->InfoList();
            if (!empty($pei)) {
                $peiArr = [];
                foreach ($pei as $key => $val) {
                    $peiArr[$val['name']]['name'][] = $val['pname'];
                }
                $result['peitao'] = $peiArr;
            }
            return $result;
        }
    }

    //获取楼盘相册逻辑
    public function Album()
    {
        $input = Yii::$app->request->get();
        $queryAlbum = new  HouseAlbum();
        $arr['album'] = $queryAlbum->AlbumGroup(['hid' => $input['hid']]);
        foreach ($arr['album'] as $key => $val) {
            $arr['data'][$val['album_id']] = $queryAlbum::find()->andWhere(['album_id' => $val['album_id']])->andWhere(['hid' => $input['hid']])->andWhere(['del' => 1])->orderBy(['sort' => SORT_DESC, 'id' => SORT_DESC])->limit(3)->asArray()->all();
            $arr['album_id'][$val['album_id']] = $val['name'];
        }
        return $arr;
    }

    //获取楼盘相册信息
    public function AlbumType()
    {
        $input = Yii::$app->request->get();
        $queryAlbum = new  HouseAlbum();

        $arr['album'] = $queryAlbum->AlbumGroup(['hid' => $input['hid']]);
        $queryAlbum->hid = $input['hid'];
        $queryAlbum->album_id = Yii::$app->request->get('album_id', $arr['album'][0]['album_id']);
        $arr['data'] = $queryAlbum->getList();

        if (!empty($arr['data'])) {
            foreach ($arr['data'] as &$val) {
                $val['img'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['housealbums'] . $val['img'] . Yii::$app->params['pfgwatermark'];
            }
        }
        $arr['selectalbumtype'] = Yii::$app->request->get('album_id', $arr['album'][0]['album_id']);
        return $arr;
    }

    //获取楼盘分类的所有相册信息
    public function AlbumAll()
    {
        $input = Yii::$app->request->post();
        $queryAlbum = new  HouseAlbum();
        $queryAlbum->hid = $input['hid'];
        $queryAlbum->album_id = $input['album_id'];
        $rows = $queryAlbum->getList();
        if ($rows != null) {
            foreach ($rows as &$val) {
                $val['img'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['housealbums'] . $val['img'] . Yii::$app->params['pfgwatermark'];
            }
            return $rows;
        }

    }

    /*相册获取*/
    public function GetAlbumAll($input)
    {
        $queryAlbum = new  HouseAlbum();
        $queryAlbum->hid = $input['hid'];
        $queryAlbum->album_id = $input['album_id'];
        $rows = $queryAlbum->getList();
        if ($rows != null) {
            foreach ($rows as &$val) {
                $val['img'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['housealbums'] . $val['img'] . Yii::$app->params['pfgwatermark'];
            }
            return $rows;
        }

    }

    //楼盘户型分组
    public function Type()
    {
        $input = Yii::$app->request->get();
        $typeModel = new HouseType();
        $typeModel->hid = $input['hid'];
        $groupType = $typeModel->TypeGroup();

        $vrcount = \common\models\Vr::find()->where(['hid' => $input['hid'], 'state' => 1, 'type' => 1])->count();
        $houseModel = new House();
        $houseModel->id = $input['hid'];
        $house720 = $houseModel->HousesAll();
        if(!empty($house720['panorama'])){
            $vrcount+=1;
        }
        if($vrcount > 0 || !empty($house720['panorama'])){
            array_unshift($groupType, ['huxing_name' => 'VR看房', 'num' => $vrcount, 'hid' => $input['hid'], 'type_id' => 'vr']);
        }

        return $groupType;
    }
	
	/**
	 * 楼盘户型数据
	 * @param $input
	 * @param bool $tr
	 * @return mixed
	 */
	public function TypeAll($input, $tr = true)
	{
		if ($input['type_id'] == 'vr') {
			$input['type_id'] = '';
			$list = (new \common\models\Vr())->getListAndVr($input);
			$houseModel = new House();
			$houseModel->id = $input['hid'];
			$house720 = $houseModel->HousesAll();
			if(!empty($house720['panorama'])){
				array_unshift($list, ['title' => '720VR全景看房', 'area' => '全景','indoor_info'=>'全景', 'hid' => $input['hid'], 'type_id' => 'vr','panorama'=>$house720['panorama'],'panorama_img'=>$house720['panorama_img'],'thumb'=>$house720['thumb']]);
			}
		}elseif($input['type_id'] == 'all'){
			$typeModel = new HouseType();
			$typeModel->hid = $input['hid'];
			//            $typeModel->type_id = $input['type_id'];
			if ($tr == true) {
				$list = $typeModel->QgetList(4);
			} else {
				$list = $typeModel->getList();
			}
			$input['type_id'] = '';
			$list_vr = (new \common\models\Vr())->getListAndVr($input);
			
			$houseModel = new House();
			$houseModel->id = $input['hid'];
			$house720 = $houseModel->HousesAll();
			
			if(!empty($house720['panorama'])){
				array_unshift($list_vr, ['title' => '720VR全景看房', 'area' => '全景','indoor_info'=>'全景', 'hid' => $input['hid'], 'type_id' => 'vr','panorama'=>$house720['panorama'],'panorama_img'=>$house720['panorama_img'],'thumb'=>$house720['thumb']]);
			}
			$list = array_merge($list_vr,$list);
		}else{
			$typeModel = new HouseType();
			$typeModel->hid = $input['hid'];
			$typeModel->type_id = $input['type_id'];
			if ($tr == true) {
				$list = $typeModel->QgetList(4);
			} else {
				$list = $typeModel->getList();
			}
		}
		if ($list != null) {
			foreach ($list as &$val) {
				$val['title'] = Opencc::Convert($val['title']);
				$val['area'] = Opencc::Convert($val['area']);
				$val['indoor_info'] = Opencc::Convert($val['indoor_info']);
				if (!empty($val['path'])) {
					$val['img'] = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['video'] . $val['img'];
					$val['type_id'] = 'vr';
				} elseif(!empty($val['panorama'])){
					if ($val['panorama_img']) {
						$val['img'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['house_video'] . $val['panorama_img'] . '/same';
					} else {
						$val['img'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $val['thumb'] . '/same';
					}
				}else {
					$val['img'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['housetypes'] . $val['img'] . '/same';
				}
			}
		}
		return $list;
	}

    //楼盘公共信息
    public function PublicDetails()
    {
        $input = Yii::$app->request->get();
        $houseModel = new House();
        $houseModel->id = $input['hid'];
        $houseRow = $houseModel->OneDetails(['id', 'name', 'state', 'city', 'characteristic', 'thumb', 'is_tel']);
        if ($houseRow != null) {
//            $houseRow['city'] = '';
            if (!empty($houseRow['city'])) {
                $cityModel = new \common\models\CategoryCity();
                $houseRow['seo'] = $cityModel->FatherSon($houseRow['city']);
                if (empty($houseRow['seo']['city'])) {
                    $houseRow['seo']['city'] = '';
                }
            }
            //猜你喜欢
            $houseModel->id = '';
            $houseModel->city = $houseRow['city'];
            $houseRow['randhouse'] = $houseModel->randList(4);

            $houseRow['state'] = Yii::$app->params['HouseSalesStatus'][$houseRow['state']];
            $houseRow['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $houseRow['thumb'];
            //特色主题
            $subject = \common\fm\HouseHandle::ChangeCharacteristic($houseRow['characteristic']);
//                $this->Subject(json_decode($houseRow['characteristic'],true));
            $houseRow['characteristic'] = $subject;

            //随机电话
            if (empty($houseRow['is_tel'])) {
                $houseRow['tel'] = $this->RandTel($houseRow['city']);
            } else {
                $houseRow['tel'] = $houseRow['is_tel'];
            }

            return $houseRow;
        }
    }

    //楼盘随机电话
    public function RandTel($city)
    {
        if (!empty($city)) {
            $tel = new \common\models\CategoryTelCity();
            $tel->cid = $city;
            $telModel = $tel->CityTelOne();
            if (!empty($telModel['tel'])) {
                $telArr = explode(',', $telModel['tel']);
                $randNum = array_rand($telArr, 1);
                return $telArr[$randNum];
            }
        }
        return Yii::$app->params['default_dialtel'];
//        $tel = new \common\models\CategoryTelCity();
//        $tel->cid = $city;
//        $telModel = $tel->CityTelOne();
//        if(!empty($telModel['tel']))
//        {
//            $telArr = explode(',',$telModel['tel']);
//            $randNum =  array_rand($telArr,1);
//            return $telArr[$randNum];
//        }
//            return Yii::$app->params['default_dialtel'];
    }

    //特色主题 超过3个随机返回3个元素
    public function Subject($id, $num = 3)
    {
        if (empty($id) || !is_array($id)) return [];
        $char = new Characteristic();
        $char->id = $id;
//        if(is_array($id) && !empty($id))
//        {
//            $char->id = $id;
//        }
        $charMode = array_column($char->getList([], ['id', 'name']), 'name', 'id');
        if ($charMode != null) {
            $count = count($charMode);
            $arr = [];
            if ($count >= $num) {
                $rand = array_rand($charMode, $num);
                foreach ($rand as $key => $val) {
                    $arr[$key] = $charMode[$val];
                }
            } else {
                $arr = $charMode;
            }
            return $arr;
        }
    }

    public function ArchitectureType($id, $type = null)
    {
        if (!empty($id)) {
            $model = new CategoryLabel();

            $model->id = $id;
            $model->type = $type;
            $rows = $model->getList([], ['name']);

            if (!empty($rows)) {
                return array_column($rows, 'name');
            }
        }
        return [];

    }


    //楼盘资讯
    public function HouseNewsList()
    {
        $input = Yii::$app->request->get();
        if (is_numeric($input['hid'])) {
            $model = new HouseNews();
            $model->hid = $input['hid'];
            $houseNewsInfo = $model->getHidList();
            $nids = array_column($houseNewsInfo, 'nid');
            $newsModel = new News();
            $newsModel->hid = $input['hid'];
            $result = $newsModel->getNewsListHouse($nids);


            foreach ($result as &$val) {
                $time_int = strtotime($val['open_time']);

                $val['d'] = date('d', $time_int);
                $val['ym'] = date('Y-m', $time_int);
                $val['thumb'] = json_decode($val['thumb'], true);
            }
            return $result;
        }
    }

    /*
     * 递归区域
     * */
    public function RecursionCity()
    {

    }

    public function distinguishHouse($input)
    {

        switch ($input['type']) {
            case 'tuijian':
                $model = new PushHousesale();
                $model->type = 3;
                $result = $model->Homegetlist(4);

                break;
            case 'price':
                $model = new House();
                $model->is_push = 2;
                $model->sale_price = $input['content'];
                $a['sale_price'] = $input['content'];
                $a['page'] = $input['id'];
                $a['limit'] = 4;
                $result = $model->HomegetList(4, $a);
                if (empty($result)) {
                    $_model = clone $model;
                    $model->is_push = 1;
                    $result = $model->HomegetList(4, $a);
                }
                break;
            case 'quyu':
                $model = new House();
                $model->is_push = 2;
                $model->city = $input['content'];
                $a['page'] = $input['id'];
                $a['limit'] = 4;
                $result = $model->HomegetList(4, $a);
                if (empty($result)) {
                    $_model = clone $model;
                    $model->is_push = 1;
                    $result = $model->HomegetList(4, $a);
                }

                break;
        }

        if (!empty($result)) {
            foreach ($result as &$val) {
                $val['price_zj'] = HouseHandle::priceUnit($val['price_unit']);
                $val['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $val['thumb'];
            }
            return $result;
        }

    }

    /*
     * 地图搜索,后期优化搜索代码
     * */
    public function MapSearch($input)
    {
        $query = new House();
        //搜索条件
        if (!empty($input)) {
            foreach ($input as $a => $b) {
                if ($a == 'zhuti') {
                    $a = 'characteristic';
                }
                if (isset($query->$a)) {
                    $query->$a = $b;
                }
            }
        }


        //处理价格区间
        if (!empty($query->price)) {
            $price = \common\models\HousesPrice::find();
            $priceModel = $price->andWhere(['id' => $query->price])->one();
            if ($priceModel != null) {
                $query->price = $priceModel->price;
            }
        }

        //户型
        if (!empty($query->type)) {
            $type = \common\models\HouseType::find();
            $typeModel = $type->andWhere(['type_id' => $query->type, 'del' => 1])->asArray()->all();
            $query->id = array_unique(array_column($typeModel, 'hid'));
        }


        $data = $query->MapSearch(['pfg_category_city.city_name', 'pfg_house.id', 'pfg_house.city', 'pfg_house.price', 'pfg_house.name', 'pfg_house_detail.address', 'pfg_house_detail.main_units', 'pfg_house_detail.price_unit', 'pfg_house.thumb', 'pfg_house.sale_price', 'pfg_house.characteristic', 'pfg_house_detail.longitude_latitude']);

        if ($data != null) {
//            $characteristic = new \common\models\Characteristic();
//            $charList = $characteristic->getList([]);
//            $characteristicId = array_column($charList,'name','id');
//            $chara = array_column($data,'characteristic','id');
//            $charaArrs = [];
//            //处理特色主题
//            foreach ($chara as $key=>$val)
//            {
//                $arr = json_decode($val,true);
//                if(is_array($arr) && !empty($arr))
//                {
//                    foreach ($arr as $k=>$v)
//                    {
//                        if(isset($characteristicId[$v]))
//                        {
//                            $charaArrs[$key][$k] = $characteristicId[$v];
//                        }
//                    }
//                }
//            }
//
//            $Tel = new \common\models\CategoryTelCity();
//            $telRow = $Tel->QgetAll();
//
//            $cityTel = [];
//            foreach ($telRow as &$v)
//            {
//                $cityTel[$v['cid']] = explode(',',$v['tel']);
//            }
//
//            $randArr = [];
            foreach ($data as &$val) {
                $val['characteristic'] = \common\fm\HouseHandle::ChangeCharacteristic($val['characteristic']);
//                if(isset($charaArrs[$val['id']]))
//                {
//                    //随机选择3个特色主题
//                    if(count($charaArrs[$val['id']]) > 3)
//                    {
//                        $ranChar = array_rand($charaArrs[$val['id']],3);
//                        $randArr[] = $charaArrs[$val['id']][$ranChar[0]];
//                        $randArr[] = $charaArrs[$val['id']][$ranChar[1]];
//                        $randArr[] = $charaArrs[$val['id']][$ranChar[2]];
//                        $val['characteristic'] = $randArr;
//                        $randArr= [];
//                    }
//                    else
//                    {
//                        $val['characteristic'] = $charaArrs[$val['id']];
//                    }
//
//                }

                if (!empty($cityTel[$val['city']][0])) {
                    $val['citytel'] = \common\fm\HouseHandle::ChangeCityTel($val['city']);
//                    $rand =  array_rand($cityTel[$val['city']],1);
//                    $val['citytel'] =$cityTel[$val['city']][$rand];

                } else {
                    $val['citytel'] = Yii::$app->params['default_dialtel'];
                }
            }
            return $data;

        }

    }

    public function getHouseRemarkList($input, $page)
    {
        $houseComment = new HouseComment();
        $houseCommentLikes = new HouseCommentLikes();
        $houseCommentReply = new HouseCommentreply();
        $houseComment->hid = $input['hid'];
        $houseCommentList = $houseComment->getHouseList($page);
        if ($houseCommentList != null) {
            foreach ($houseCommentList as &$value) {
                $value['allremark'] = floor(($value['diduan'] + $value['jiaotong'] + $value['peitao'] + $value['huanjing'] + $value["xingjiabi"]) / 5);
                $value['mobile'] = preg_replace("/(\d{3})\d{4}(\d{2})/", "\$1******\$3", $value['mobile']);

                $houseCommentLikes->hid = $value['hid'];
                $houseCommentLikes->hcid = $value['id'];
                $value['count_likes'] = $houseCommentLikes->Total();
                $value['reply'] = $houseCommentReply->getCommentReply($value['id']);
            }
            return $houseCommentList;
        }
    }
	
	public function getHouseListByCity($city, $limit = null)
	{
		$cityList = (new CategoryCity())->getTongSonList($city);
		$model = new House();
		$model->city = $cityList;
		$data = $model->getCityHouseList(6);
		foreach ($data as &$val) {
			$val['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $val['thumb'] . '/same';
			$val['tel'] = $this->RandTel($val['city']);
			$news = (new News())->getHouseNewByHid($val['id'], 1);
			if (!empty($news)) {
				$val['news_name'] = $news[0]['subject'];
				$val['news_id'] = $news[0]['id'];
			}
		}
		return $data;
	}
}