<?php


namespace common\models;

use common\fm\HouseHandle;
use common\fm\Opencc;
use common\models\Common;

class PushHothouse extends Common
{

    public function rules()
    {
        return [
            ['hid', 'unique', 'targetClass' => 'common\models\china\PushHothouse', 'on' => ['add'], 'message' => '该楼盘已存在', 'filter' => function ($query) {
                return $query->andWhere(['type' => $this->type]);
            }],
            ['hid', 'number', 'message' => '请选择楼盘'],
            ['city', 'number'],
            ['sort', 'number', 'message' => '排序只能是数字'],
            ['sort', 'number', 'max' => 10000],
            ['type', 'number'],
            [['state'], 'number'],
            [['state'], 'default', 'value' => 1],
            [['state'], 'in', 'range' => [1, 2]],
        ];
    }

    public function attributeLabels()
    {
        return [
            'hid' => '楼盘名称',
            'city' => '区域',
        ];
    }

    public function FindById($id)
    {
        return self::findOne($id);
    }

    /**
     * 后台查询数据
     * @param $input
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getList($input)
    {
        $query = self::find();
        $query->select(['pfg_house.name', 'pfg_push_hothouse.id', 'pfg_push_hothouse.hid', 'pfg_push_hothouse.create_at', 'pfg_push_hothouse.state', 'pfg_push_hothouse.sort']);
        if (!empty($input['name'])) {
            $query->andWhere(['like', 'pfg_house.name', $input['name']]);
        }
        $query->andWhere(['pfg_push_hothouse.type' => $input['type']]);
        $query->leftJoin('pfg_house', 'pfg_push_hothouse.hid = pfg_house.id');
        if (!empty($input['page'])) {
            $query->limit = $input['limit'];
            $query->offset = ($input['page'] - 1) * $input['limit'];
        }
        return $query->orderBy(['pfg_push_hothouse.sort' => SORT_DESC, 'pfg_push_hothouse.id' => SORT_DESC])->asArray()->all();
    }

    /**
     * 后台查询数据统计条数
     * @param $input
     * @return int|string
     */
    public function Total($input)
    {
        $query = self::find();
        if (!empty($input['name'])) {
            $query->andWhere(['like', 'pfg_house.name', $input['name']]);
        }
        $query->andWhere(['pfg_push_hothouse.type' => $input['type']]);
        $query->leftJoin('pfg_house', 'pfg_push_hothouse.hid = pfg_house.id');
        return $query->count();
    }

    /**
     * 获取推送的热门楼盘
     * @param $type 区分类型
     * @param $labellimit 返回标签的数量
     * @param $sort 排序
     * @param $limit
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getHotHouse($type, $limit, $sort = '')
    {
        $query = self::find();
        $query->andWhere(['pfg_push_hothouse.state' => 1]);
        $query->andWhere(['pfg_push_hothouse.type' => $type]);
        $query->select(['pfg_house.id', 'pfg_house.name', 'pfg_house.thumb', 'pfg_house.sale_price',
            'pfg_house.city', 'pfg_house_detail.price_unit', 'pfg_house.characteristic', 'pfg_category_city.city_name','pfg_house_detail.preferential']);
        $query->leftJoin('pfg_house', 'pfg_push_hothouse.hid = pfg_house.id');
        $query->leftJoin('pfg_category_city', 'pfg_house.city = pfg_category_city.id');
        $query->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid');
        $query->limit = $limit;
        $query->orderBy(['pfg_push_hothouse.sort' => SORT_DESC, 'pfg_push_hothouse.id' => SORT_DESC]);
        if (!empty($sort) && $sort == 'rand') {
            $query->orderBy('RAND()');
        }

        $data = $query->asArray()->all();
        if (!empty($data)) {
            foreach ($data as &$val) {
                $val['name'] = Opencc::Convert($val['name']);
                $val['city_name'] = Opencc::Convert($val['city_name']);
                $val['price_unit'] = Opencc::Convert($val['price_unit']);
                $val['thumb'] = \Yii::$app->params['httpImg']['hosts'] . \Yii::$app->params['httpImg']['houses'] . $val['thumb'] . '/same';
                $val['tel'] = HouseHandle::RandTel($val['city']);
                $val['label'] = HouseHandle::Subject(json_decode($val['characteristic']), 3);
                if (!empty($val['label'])) {
                    foreach ($val['label'] as &$v) {
                        $v = Opencc::Convert($v);
                    }
                }
            }
            return $data;
        }
        return '';
    }


    public function HomeListRand($limit, $type)
    {
        $query = self::find();
        $query->select(['pfg_house.name', 'pfg_house.thumb', 'pfg_house.id', 'pfg_house.characteristic']);
        $query->andWhere(['pfg_push_hothouse.state' => 1]);
        $query->andWhere(['pfg_push_hothouse.type' => $type]);
        $query->leftJoin('pfg_house', 'pfg_push_hothouse.hid = pfg_house.id');
        $query->limit = $limit;
        $query->orderBy('RAND()');
        return $query->asArray()->all();
    }
}