<?php
/*
  Created by PhpStorm.
  User: xiaofeng
  Date: 2018/6/10
  Time: 下午3:15
 */

namespace frontend\controllers;

use backend\base\Help;
use common\models\House;
use common\models\SpecialTopic;
use frontend\base\CommonController;
use Yii;
use frontend\server\ZtServer;

class ZtController extends CommonController
{
    // 元旦专题
    public function actionNewyear()
    {
        return $this->render('newyear');
    }

    // 新年专题
    public function actionNewyearzt()
    {
        return $this->render('newyearzt');
    }

    // 碧桂园鼎龙湾专题
    public function actionBgydlw()
    {
        return $this->render('bgydlw');
    }

    /*金九银十专题*/
    public function actionJjys()
    {
        return $this->render('jjys');
    }

    /*国庆专题*/
    public function actionNationalzt()
    {
        return $this->render('nationalzt');
    }

    /*融创高隆湾 3天2晚游*/
    public function actionRcglw()
    {
        return $this->render('rcglw');
    }

    // 春节专题
    public function actionChunjiezt()
    {
        $ztmodel = new \common\models\SpecialTopic();
        $row = $ztmodel::findOne(7);
        $model = new \common\models\House();
        $cmodel = new \common\models\CategoryCity();
        $city = '';
        $house = '';
        $imgUrl = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['push_lb'];
        if (!empty(json_decode($row->hids, true))) {
            $hid = json_decode($row->hids, true);
            $query = $model::find();
            $query->andWhere(['pfg_house.id' => $hid]);
            $query->joinWith(['housedetail']);
            $query->with(['cityName']);
            $query->orderBy(['pfg_house.sort' => SORT_DESC]);
            $house = $query->asArray()->all();

            $pcity = array_column($house, 'province');
            $cquey = $cmodel::find();
            $cquey->select(['id', 'city_name']);
            $cquey->andWhere(['id' => $pcity]);
            $cquey->orderBy(['sort' => SORT_DESC]);
            $city = array_column($cquey->asArray()->all(), 'city_name', 'id');

        }
        empty($row->pc_img) ? '' : $row->pc_img = $imgUrl . $row->pc_img;

        return $this->render('chunjiezt', ['city' => $city, 'house' => $house, 'model' => $row]);
    }


    /**
     * 线上售楼处专题
     * @return string
     */
    public function actionSellhousezt()
    {
        $row = SpecialTopic::findOne(8);
        if (!empty($row)) {
            $hid = json_decode($row['hids'], true);
            $house = House::find()->select(['pfg_house.id', 'pfg_house.name', 'pfg_house_detail.address', 'pfg_house.thumb',
                'pfg_house_detail.preferential_zt', 'pfg_house.sale_price', 'pfg_house_detail.price_unit', 'pfg_category_city.pid'])
                ->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid')
                ->leftJoin('pfg_category_city', 'pfg_house.city = pfg_category_city.id')
                ->andWhere(['pfg_house.id' => $hid])
                ->orderBy(['pfg_house.sort' => SORT_DESC])->asArray()->all();
        }
        return $this->render('sellhousezt', ['house' => $house, 'row' => $row]);
    }

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

    // 写字楼专题
    public function actionOffice()
    {
        $cityList = \common\models\PushplaceType::find()->where(['type' => 4, 'state' => 1])->limit(2)->orderBy(['sort' => SORT_DESC])->select('id,title')->asArray()->all();
        foreach ($cityList as &$val) {
            $val['house_list'] = \common\models\PushhouseType::find()
                ->select('pfg_pushhouse_type.*,pfg_house.name,pfg_house.city,pfg_house.sale_price,pfg_house.thumb,pfg_house_detail.price_unit,pfg_house_detail.address,pfg_house_detail.preferential,pfg_house_detail.preferential_price,pfg_house_detail.preferential_zt')
                ->where(['pfg_pushhouse_type.previd' => $val['id'], 'pfg_pushhouse_type.type' => 4])
                ->innerJoin('pfg_house', 'pfg_house.id = pfg_pushhouse_type.hid')
                ->innerJoin('pfg_house_detail', 'pfg_house_detail.hid = pfg_house.id')
                ->orderBy(['pfg_pushhouse_type.sort' => SORT_DESC])->asArray()->all();
            foreach ($val['house_list'] as &$value) {
                $value['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $value['thumb'] . '/same';
                $value['tel'] = $this->RandTel($value['city']);
            }
        }
        if (!empty($cityList)) {
            $cityList[0]['house_list'] = array_chunk($cityList[0]['house_list'], 3);
        }
        return $this->render('office', ['cityList' => $cityList]);
    }

    // 国庆中秋专题
    public function actionAutumnzt()
    {
        $server = new ZtServer();
        $data = $server->GuoQingZhongQiu();
        return $this->render('autumnzt',['data'=>$data]);
    }
    
	// 海南别墅专题
	public function actionHnbszt()
	{
		$row = new \common\models\PushplaceType();
		$city1 = $row->city1();
		$city2 = $row->city2();
		$hn_boutiquevilla = $row->boutiquevilla();
		$hn_city = \common\models\CategoryCity::find()->where(['pid'=>68])->select('id,city_name')->asArray()->all();
		$boutiquevillaList = [];
		foreach ($hn_boutiquevilla as $value){
			$boutiquevillaList[$value['id']] = (new \common\models\PushhouseType())->boutiquevillaList($value['id'],4);
		}
		$priceList = \common\models\HousePrice::find()->where(['del'=>1,'state'=>1])->orderBy(['sort'=>SORT_ASC])->asArray()->all();
		//别墅动态
		$villa_news = (new \common\models\PushnewsType())->getlist(4);
		foreach ($villa_news as &$value){
			$value['thumb'] = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['newsthumb'] . json_decode($value['thumb'],true)[0];
			$value['time'] = date('Y.m.d',$value['create_at']);
		}
		//获取最新资讯
		$new_news = \common\models\News::find()->select('id,subject,thumb,abstract,create_at')->where(['state'=>1,'del'=>1])->orderBy(['create_at'=>SORT_DESC,'id'=>SORT_DESC])->limit(4)->asArray()->all();
		foreach ($new_news as &$value){
			$value['thumb'] = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['newsthumb'] . json_decode($value['thumb'],true)[0];
			$value['time'] = date('Y.m.d',$value['create_at']);
		}
		return $this->render('hnbszt', [
			'city1' => $city1,
			'city2' => $city2,
			'hn_city'=>$hn_city,
			'priceList'=>$priceList,
			'hn_boutiquevilla'=>$hn_boutiquevilla,
			'boutiquevillaList'=>$boutiquevillaList,
			'villa_news'=>$villa_news,
			'new_news'=>$new_news
		]);
	}
	
	//获取指定地区别墅TOP8
	public function actionGetprovince()
	{
		$id = Yii::$app->request->post('city');
		if(is_numeric($id)){
			$data = \common\models\PushhouseType::find()
			                                    ->select(['pfg_house.id','pfg_house.name','pfg_house.sale_price','pfg_house.thumb','pfg_house_detail.price_unit','pfg_house_detail.preferential'])
			                                    ->where(['pfg_pushhouse_type.previd' => $id,'pfg_pushhouse_type.type'=>2])
			                                    ->innerJoin('pfg_house', 'pfg_house.id = pfg_pushhouse_type.hid')
			                                    ->innerJoin('pfg_house_detail','pfg_house_detail.hid = pfg_house.id')
			                                    ->limit(8)
			                                    ->orderBy(['pfg_pushhouse_type.sort'=>SORT_DESC])->asArray()->all();
			foreach ($data as &$value){
				$value['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['housesch'] . $value['thumb'] . '/same';
			}
			return Help::JsonData(Help::SUCCESS,'成功',0,$data);
		}else{
			return Help::JsonData(0,'成功',0,[]);
		}
	}
	
	//获取湾区列表
	public function actionGetgulfcitygl()
	{
		$id = Yii::$app->request->post('id');
		if(is_numeric($id)){
			$data = \common\models\PushhouseType::find()
			                                    ->select('pfg_pushhouse_type.*,pfg_house.name,pfg_house.city,pfg_house.sale_price,pfg_house.thumb,pfg_house_detail.price_unit')
			                                    ->where(['pfg_pushhouse_type.previd' => $id])
			                                    ->innerJoin('pfg_house', 'pfg_house.id = pfg_pushhouse_type.hid')
			                                    ->innerJoin('pfg_house_detail','pfg_house_detail.hid = pfg_house.id')
			                                    ->limit(6)
			                                    ->orderBy(['pfg_pushhouse_type.sort'=>SORT_DESC])->asArray()->all();
			foreach ($data as &$value){
				$value['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['housesch'] . $value['thumb'] . '/same';
				$value['tel'] = $this->RandTel($value['city']);
			}
			return Help::JsonData(Help::SUCCESS,'成功',0,$data);
		}else{
			return Help::JsonData(0,'成功',0,[]);
		}
	}
	
	
}