<?php
namespace wechat\server;

use common\models\PushmTimelimitsgroup;
use Yii;

class TimeLimitsGroupServer{

        public function GroupHouselist($input)
        {
            $query = new PushmTimelimitsgroup();
			$data = $query->Homegetlist($input);
            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['thumb'] = Yii::$app->params['httpImg']['hosts'].Yii::$app->params['httpImg']['houses'].$val['thumb'];
                    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 (isset($cityTel[$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'=>$data];
            }
        }



        public function Details()
        {
            $input = Yii::$app->request->get();
            if(is_numeric($input['hid']))
            {
                $arr = [];
                //所有信息,后期优化显示字段
                $model = new House();
                $model->id = $input['hid'];
                $select = ['pfg_house.*','pfg_house_detail.*','pfg_category_city.city_name'];
                $arr['house'] = $model->Qdetails($select);

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


                if($arr['house'] != null)
                {
                    //特色主题
                    $sub = new IndexServer();
                    $arr['characteristic'] = $sub->Subject(json_decode($arr['house']['characteristic']),3);

                    //楼盘相册
                    $queryAlbum = new  HouseAlbum();
                    $queryAlbum->hid = $input['hid'];
                    $arr['albumCount'] = $queryAlbum->Total();

                    //楼盘户型
                    $queryType = new  \common\models\HouseType();
                    $queryType->hid = $input['hid'];
                    $arr['type'] =  $queryType->MgetList(3,['area','title','img','indoor_info']);
                    //电话
                    $arr['house']['tel'] = $sub->RandTel(  $arr['house']['city']);

                    //预售证书
                    $certificate = new \common\models\HousePermit();
                    $certificate->hid = $input['hid'];
                    $arr['certificate'] = $certificate->FindNewest();
                    //楼盘状态
                    $arr['house']['state'] =Yii::$app->params['HouseSalesStatus'][$arr['house']['state']];
                    //新闻
                    $arr['news'] = $this->NewsDynamic($input);
                }
                return $arr;
            }
        }



        //楼盘详情页数据
        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();

                $sub = new IndexServer();
                $result['house']['characteristic'] = $sub->Subject(json_decode($result['house']['characteristic']),3);
                $result['house']['tel'] = $sub->RandTel($result['house']['city']);
                $result['house']['architecture_type'] = $this->ArchitectureType(json_decode($result['house']['architecture_type']));

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

                return $result;
            }
        }

        /*
         * 建筑类别
         * */
        public function ArchitectureType($id)
        {
            if(!empty($id))
            {
                $model = new CategoryLabel();

                $model->id = $id;
                $model->type = 2;
                $rows =  $model->getList([],['name']);
                if(!empty($rows))
                {
                    return array_column($rows,'name');
                }
            }
                    return [];
        }

}