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

namespace frontend\server;

use common\models\House;
use common\models\ListNav;
use common\models\Live;
use common\models\News;
use common\models\PushNews;
use common\models\Vr;
use Yii;
use common\models\CategoryCity;
use common\models\HousesPrice;
use common\models\CategoryHousetype;
use common\models\Characteristic;
use common\models\PushRotation;
use common\models\PushActivity;
use common\models\PushChoice;
use common\models\PushHousesale;
use common\models\CityDetails;
use common\models\PushCity;
use common\models\PushNewscolumn;
use common\models\NewKnowledgelabel;
use yii\helpers\ArrayHelper;

class IndexServer
{


    //区域
    public function CityList()
    {
        if (Yii::$app->hostserver->hostId != 0) {
            $model = new \common\models\CategoryCity();
            $model->pid = Yii::$app->hostserver->hostId;
            $model->state = 1;
            $rows = $model->getList([]);
        } else {
            $model = new PushCity();
            $rows = $model->Homegetlist();
        }
        if (!empty($rows)) {
            return $rows;
        }
    }

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

    //户型
    public function HouseType()
    {
        $typeModel = new CategoryHousetype();
        $typeList = $typeModel->getList([], ['id', 'huxing_name']);
        if ($typeList != null) {
            return $typeList;
        }
    }

    //主题
    public function Subject()
    {
        $sub = new Characteristic();
        $input['page'] = 1;
        $input['limit'] = 15;
        $subList = $sub->getList($input, ['id', 'name']);
        if ($subList != null) {
            return $subList;
        }
    }

    //轮播图

    public function Lunbo($limit)
    {
        $model = new PushRotation();
        if (Yii::$app->hostserver->hostId != 0) {
            $model->city = Yii::$app->hostserver->hostId;
        }
//        $rows = $model->HomeGetListP();
        $rows = $model->getRoationChart( $limit);
        if ($rows != null) {
            $url = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['push_lb'];
            foreach ($rows as &$val) {
                $val['img'] = $url . $val['img'];
            }
            return $rows;
        }
    }

    //热门活动
    public function Hotactivity()
    {
        $model = new PushActivity();
        if (Yii::$app->hostserver->hostId != 0) {
            $model->city = Yii::$app->hostserver->hostId;
        }
        $rows = $model->HomeGetList();
        if ($rows != null) {
            $url = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['hot_activity'];
            foreach ($rows as &$val) {
                $val['img'] = $url . $val['img'];
            }
//                    p($rows);
            return $rows;
        }
    }

    //本周精选
    public function Choice()
    {
        $model = new PushChoice();
        if (Yii::$app->hostserver->hostId != 0) {
            $model->city = Yii::$app->hostserver->hostId;
        }
        $rows = $model->HomeGetList();
        if ($rows != null) {
            $url = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['choice'];
            foreach ($rows as &$val) {
                $val['img'] = $url . $val['img'];
            }
            return $rows;
        }
    }

    public function HotHouse()
    {
        $model = new PushHousesale();
        $model->type = 1;
        if (Yii::$app->hostserver->hostId != 0) {
            $model->city = Yii::$app->hostserver->hostId;
        }
        $rows = $model->Homegetlist(4);
        if ($rows != null) {
            $zhuti = new \frontend\server\HouseServer();
            foreach ($rows as &$val) {
                $val['characteristic'] = $zhuti->Subject(json_decode($val['characteristic']), 2);
                $val['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $val['thumb'];
            }
            return $rows;
        }
    }

    public function CityInfo()
    {
        $model = new \common\models\PushSojourncity();
        $rows = $model->Homegetlist(10);
        if ($rows != null) {
            return $rows;
        }
    }

    /*
     *
     * */
    public function Characteristic()
    {
        $model = new \common\models\PushCharacteristic();
        $rows = $model->Homegetlist();
        if (!empty($rows)) {
            $url = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['characteristic'];
            foreach ($rows as &$val) {
                $val['img'] = $url . $val['img'];
            }
            return $rows;
        }
    }

    /*
     * 资讯
     * */
    public function newsColumn()
    {
//                $query = PushNewscolumn::find();
//                $query->select(['pfg_category_news.news_name','pfg_push_newscolumn.id','pfg_push_newscolumn.short','pfg_push_newscolumn.cid']);
//                $query->andWhere(['pfg_push_newscolumn.del'=>1]);
//                $query->andWhere(['pfg_push_newscolumn.is_show'=>1]);
//                $query->leftJoin('pfg_category_news','pfg_push_newscolumn.cid=pfg_category_news.id');
//                $query->limit = 3;
//                $data = $query->orderBy(['pfg_push_newscolumn.sort'=>SORT_ASC])->asArray()->all();
//                if(!empty($data))
//                {
//
//                    $id = array_column($data,'id');
//                    $new =  PushNews::find();
//                    $new->select(['pfg_push_news.cid','pfg_news.id','pfg_news.subject','pfg_news.short_subject','pfg_news.thumb','pfg_news.open_time']);
//                    $new->andWhere(['pfg_push_news.del'=>1]);
//                    $new->andWhere(['pfg_push_news.is_show'=>1]);
//                    $new->andWhere(['pfg_push_news.type'=>4]);
//                    $new->andFilterWhere(['pfg_push_news.cid'=>$id]);
//                    $new->leftJoin('pfg_news','pfg_push_news.nid=pfg_news.id');
////                    $new->limit = 10;
//                    $a = $new->orderBy(['pfg_push_news.sort'=>SORT_ASC])->asArray()->all();
//                    $arr = [];
//                    foreach ($a as $val)
//                    {
//                        $arr[$val['cid']][] = $val;
//                    }
//
//                    foreach ($data as &$val)
//                    {
//                        if(isset($arr[$val['id']]))
//                        {
//                            $val['data'] = $arr[$val['id']];
//                        }
//
//                    }
//                    return $data;
//                }

        //2020.7.2改版
        $hostId = Yii::$app->hostserver->hostId;

        $model = new News();

        $data = [];

        if ($hostId == 60) {
            $data[0] = $model->getNews(1, 2);
            $data[1] = $model->getNews(13, 4);
            $data[2] = $model->getNews(18, 7);
            $data[3] = $model->getNews(14, 6);
            $data[4] = $model->getNews(3, 4);
        } else {
            $data[0] = $model->getNews(14, 2);
            $data[1] = $model->getNews(15, 4);
            $data[2] = $model->getNews(1, 7);
            $data[3] = $model->getNews(13, 6);
            $data[4] = $model->getNews(3, 4);
        }

        return $data;
    }

    /*
     * 价格走势
     * */
    public function Pricetrend()
    {
        $model = new \common\models\PushCityprice();
        if (Yii::$app->hostserver->hostId != 0) {

            $model->city_id = \common\models\CategoryCity::find()->select(['id'])->andWhere(['pid' => Yii::$app->hostserver->hostId])->andWhere(['del' => 1])->column();

        }

        return $model->Homegetlist(20);
    }

    /**
     * 购房攻略
     */
    public function Goufang($type)
    {
        $model = new \common\models\PushNews();
        $model->type = $type;
        return $model->Homegetlist(8);
    }

    /**
     * 别墅专区
     */
    public function Villa()
    {
        $model = new \common\models\House();
        $model->del = 1;
        if (Yii::$app->hostserver->hostId != 0) {
            $model->city = Yii::$app->hostserver->hostId;
        }
        return $model->getVilla('pfg_house.id,pfg_house.name,pfg_house_detail.address,pfg_house_detail.longitude_latitude,pfg_house_detail.main_units,pfg_house_detail.price_unit,pfg_house.sale_price,pfg_house.thumb,pfg_category_city.city_name', 10);
    }

    //精选新房
    public function RecommendCat()
    {

        if (Yii::$app->hostserver->hostId == 0) {
            $model = new \common\models\PushRecommend();
            $model->city = Yii::$app->hostserver->hostId;
            // $model->state =1;
            return $model->getList(Yii::$app->request->get());
        } else {
            $model = new \common\models\CategoryCity();
            return $model->getSonCity(Yii::$app->hostserver->hostId);
        }

    }

    /**知识库 20181010 eit*/
    public function getKnowledgeBase()
    {
        $model = new NewKnowledgelabel();
        $model->pid = 0;
        $result = $model->getList([], 'id,pid,name', 2);
        $result = ArrayHelper::toArray($result);
        foreach ($result as $k => $v) {
            $model->pid = $v['id'];
            $arr = $model->getList([], 'id,pid,name', 1);
            $arr = ArrayHelper::toArray($arr);
            $result[$k]['child'] = $arr;
        }
        return $result;
    }

    //精选新房 根据地区获取1条地区
    public function RecommendCatOne()
    {
        $model = new \common\models\PushRecommend();
        $city = Yii::$app->hostserver->hostId;
        return $model->getCatOne($city);
    }

    public function getHomeHotHouse($limit = null)
    {
        return House::find()->select(['id', 'name'])->where(['del' => 1, 'is_view' => 1])->limit($limit)->orderBy(['is_push' => SORT_DESC, 'sort' => SORT_DESC, 'create_at' => SORT_DESC])->all();
    }

    public function getPushCity($limit)
    {
        $data = (new \common\models\ListNav())->getPcPushCityList(1, $limit);
        return $data;
    }

    public function getPushAerial($limit)
    {
        $list_id = 11;
        $data = (new \common\models\ListHouse())->getIndexPushHouse($list_id, $limit);
        foreach ($data as &$val) {
            if ($val['video_img']) {
                $val['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['house_video'] . $val['video_img'] . '/same';
            } else {
                $val['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['house'] . $val['thumb'] . '/same';
            }
        }
        return $data;
    }

    //2020.10.21v2改版首页VR全景看房楼盘
    public function getVrHouse($limit)
    {
        $data = (new \common\models\ListHouse())->getIndexPushHouse(10, $limit);
        $vrModel = new Vr();
        foreach ($data as &$val) {
            $row = $vrModel->getListAndVr(['hid' => $val['id']]);
            $val['thumb'] = Yii::$app->params['httpImg']['host'] . Yii::$app->params['httpImg']['video'] . $row[0]['img'];
//            $val['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $val['thumb'] . '/same';
        }
        return $data;
    }

    //2020.10.25v2改版自动或直播回放
    public function getVoluntarilylive($limit)
    {
        $data = (new Live())->getLiveHouseInfo($limit);
//        foreach ($data as &$val) {
//            $val['thumb'] = Yii::$app->params['httpImg']['hosts'] . Yii::$app->params['httpImg']['houses'] . $val['thumb'] . '/same';
//        }
        return $data;
    }

    //2020.10.25v2改版获取所有地区名称列表
    public function getAllCityList()
    {
        $model = new CategoryCity();
        $model->pid = 68;
        $model->state = 1;
        $data = $model->getList([], ['id', 'city_name']);
        return $data;
    }

    //2020.10.25v2改版获取推送资讯栏目
    public function getPushNews($limit)
    {
        $data = (new ListNav())->getPcListNews(2, $limit);
        return $data;
    }

    //2020.10.25v2改版获取推送热搜楼盘词
    public function getPushTitle($list_id, $limit)
    {
        $data = (new ListNav())->getPcPushTitle($list_id, $limit);
        return $data;
    }

}