<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/6/9
 * Time: 上午11:12
 */

namespace wechat\server;

use common\models\CategoryCity;
use common\models\MiniRotation;
use common\models\PushmRotation;
use common\models\PushmCharacteristic;
use common\models\PushmHouse;
use common\models\PushmRecommend;
use common\models\Characteristic;
use common\models\HousesPrice;
use Yii;

class IndexServer
{
        /*
         * 轮播图
         * */
        public function Lunbo()
        {
            $model = new MiniRotation();
			if(Yii::$app->hostserver->hostId != 0)
           {
               $model->city = Yii::$app->hostserver->hostId;
				$result=$model->HomeGetList();
                if(empty($result)){
                    return $model->GetAllList();
                }else{
					 return $model->HomeGetList();
				}
           }
          return $model->GetAllList();
        }

        /*
         * 搜索框-热门筛选
         * */
        public function shaixuan()
        {
            $model = new PushmHouse();
            $model->type = 2;
            $rows = $model->Homegetlist(10);
            if(!empty($rows))
            {
                return $rows;
            }
        }

        /*
         * 搜索框 - 价格
         * */
        public function PriceList()
        {
            $PriceModel = new HousesPrice();
            $PriceList = $PriceModel->getList([],['price','id']);
            if($PriceList != null)
            {
                return $PriceList;
            }
        }

        /*
         * 特色推荐
         * */
        public function tese()
        {
            $model = new  PushmCharacteristic();
            return $model->Homegetlist(3);
        }

        /*
         * 热销楼盘
         * */
        public function Hothouse($input)
        {
            $row = [];
            if(empty($input['city']))
            {
                $model = new PushmHouse();
                $model->type = $input['type'];
                $rows = $model->Homegetlist(6,$input['rand']);
             }
            else
            {
                $id = CategoryCity::find()->select(['id'])->andWhere(['pid'=>$input['city']])->asArray()->all();
                $city = array_column($id,'id');
                $h = new PushmHouse();
                $h->type = $input['type'];
                $rows = $h->CityGetHome($city);
            }
//            if(isset($input['city']) && $input['city'] == 0)
//            {
//                $model = new PushmHouse();
//                $model->type = $input['type'];
//                $rows = $model->Homegetlist(6,$input['rand']);
//            }
//            else if ( $input['city'] != 0)
//            {
//                $id = CategoryCity::find()->select(['id'])->andWhere(['pid'=>$input['city']])->asArray()->all();
//                $city = array_column($id,'id');
//                $h = new PushmHouse();
//                $h->type = $input['type'];
//                $rows = $h->CityGetHome($city);
//            }

            if(!empty($rows))
            {
                foreach ($rows as &$val)
                {
                    if(!empty($val['characteristic']))
                    {
//                        $val['characteristic'] = $this->Subject(json_decode($val['characteristic']),3);
                        $val['characteristic'] = \common\fm\HouseHandle::ChangeCharacteristic($val['characteristic']);
                    }

//                    $val['tel'] = $this->RandTel($val['cid']);
                    $val['tel'] = \common\fm\HouseHandle::ChangeCityTel($val['cid']);
                }
            }

            return $rows;
        }

        /*
         * 品房推荐
         * */
        public function tuijian()
        {
            $model = new PushmRecommend();
            return $model->Homegetlist(10);
        }


        /*
         * 热门地区
         * */
        public function hotCity()
        {
            $model = new \common\models\PushmCity();
            $model->type = 1;
            $rows = $model->Homegetlist(6);
            return $rows;
        }





        //资讯
        public function newslist()
        {
            $model = new \common\models\PushmNews();
            $model->type  = 1;
            return $model->Homegetlist(4);
        }

        //资讯栏目
        public function NewsColumn()
        {
            $model = new \common\models\PushmNewscolumn();
            return $model->Homegetlist(4);
        }


        //特色主题 超过3个随机返回3个元素
        public function Subject($id,$num = 3)
        {
            $char = new Characteristic();
            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 >= 3)
                    {
                        $rand = array_rand($charMode,$num);
                        foreach ($rand as $key=>$val)
                        {
                            $arr[$key] = $charMode[$val];
                        }
                    }
                    else
                    {
                        $arr = $charMode;
                    }
                    return $arr;
                }
            }
        }
        //楼盘随机电话
        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);
                return $telArr[$randNum];
            }
        }

}