<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/3/25
 * Time: 上午10:06
 */

namespace common\models;
class HouseType extends Common
{


    public function rules()
    {
        return [
            [['hid', 'type_id'], 'required', 'message' => '{attribute}不能为空'],
            [['sort', 'hid', 'hot'], 'number'],
            [['img'], 'string', 'max' => 50],
            ['title', 'each', 'rule' => ['string', 'max' => 50], 'message' => '最多输入50个字符'],        //验证
            ['area', 'each', 'rule' => ['string', 'max' => 50], 'message' => '最多输入50个字符'],        //验证
            ['indoor_info', 'each', 'rule' => ['string', 'max' => 50], 'message' => '最多输入50个字符'],        //验证
            [['del', 'hot'], 'in', 'range' => [1, 2]],
            [['del', 'hot'], 'default', 'value' => 1]
        ];
    }

    public function attributeLabels()
    {
        return [
            'type_id' => '所属户型',
            'title' => '户型描述',
            'area' => '建筑面积',
            'indoor_info' => '厅卫厨',
        ];
    }

    public function Authenticator($input)
    {
        $this->load($input, '');
        if (!$this->validate()) return $this->errors;
        return $this;
    }

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

    //随机返回
    public function RandList($select = null, $limit)
    {
        $query = $this->QueryFind();
        $query = $this->QueryFindWhere($query);
        if ($select != null) {
            $query->select($select);
        }
        $query->limit = $limit;
        return $query->orderBy('RAND()')->asArray()->all();
    }

    //分组
    public function TypeGroup()
    {
	    $query = self::find();
	    $query->andWhere(['pfg_category_housetype.state' => 1]);
	    $query->andWhere(['pfg_house_type.del' => $this->setDel]);
	    $query->andFilterWhere(['pfg_house_type.hid' => $this->hid]);
        $query->select(['pfg_category_housetype.huxing_name', 'pfg_house_type.type_id', 'pfg_house_type.hid', 'pfg_house_type.state', "count('type_id') as num"]);
        $query->leftJoin('pfg_category_housetype', 'pfg_house_type.type_id = pfg_category_housetype.id');
        $query->orderBy(['pfg_category_housetype.sort' => SORT_ASC]);
        return $query->groupBy('type_id')->asArray()->all();
    }

    public function getList($select = null)
    {
        $query = self::find();
        $query->select($select);
        $query = $this->QueryFindWhere($query);
//            $query->andWhere(['del'=>$this->setDel]);
//            $query->andFilterWhere(['hid'=>$this->hid]);
//            $query->andFilterWhere(['type_id'=>$this->type_id]);
        return $query->asArray()->all();
    }

    public function QgetList($limit = null)
    {
        $query = $this->QueryFind();
        $query = $this->QueryFindWhere($query);
        $query->limit($limit);
        return $query->asArray()->all();
    }


    //楼盘分组查询统计
    public function HidTotal()
    {
        $query = self::find();
        $query->select(["count('hid') as num", 'hid']);
        $query->andFilterWhere(['hid' => $this->hid]);
        $query->andWhere(['del' => $this->setDel]);
        $query->andWhere(['NOT', ['img' => '']]);
        return $query->groupBy('hid')->asArray()->all();
    }

    //移动端调用
    public function MgetList($limit, $select = null)
    {
        $query = $this->QueryFind();
        $query->select($select);
        $query = $this->QueryFindWhere($query);
        $query->limit($limit);
        return $query->asArray()->all();
    }

    public function getListByHid($select = null)
    {
        $query = $this->QueryFind();
        $query->select($select);
        $query = $this->QueryFindWhere($query);
        return $query->asArray()->all();
    }

    protected function QueryFind()
    {
        return self::find();
    }

    protected function QueryFindWhere($query)
    {
        $query->andWhere(['del' => $this->setDel]);
        $query->andFilterWhere(['hid' => $this->hid]);
        $query->andFilterWhere(['type_id' => $this->type_id]);
        return $query;
    }

    public function getHouseTypeName()
    {
        return $this->hasOne(CategoryHousetype::className(), ['id' => 'type_id']);
    }

}