<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/3/4
 * Time: 上午9:25
 * 楼盘预售证号
 */

namespace common\models;

class HousePermit extends Common
{
        public function rules()
        {
            return [
                ['permit','required','message'=>'{attribute}不能为空'],
                ['hid','required','message'=>'请刷新重试'],
                ['time','string','max'=>30],
                ['building_num','string','max'=>200],

            ];
        }

        public function attributeLabels()
        {
            return [
                'permit'=>'预售证号',
                'time'=>'发证时间',
                'building_num'=>'楼栋数',
            ];
        }

        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 getList($input)
        {
            $query = self::find();
            $query->andWhere(['del'=>$this->setDel]);
            if(!empty($input['hid']))
            {
                $query->andWhere(['hid'=>$input['hid']]);
            }
            if(!empty($input['page']))
            {
                $query->limit = $input['limit'];
                $query->offset = ($input['page']-1 )* $input['limit'];
            }
            return $query->orderBy(['create_at'=>SORT_DESC])->asArray()->all();
        }

        public function Total()
        {
            $query = self::find();
            $query->andWhere(['del'=>$this->setDel]);
            $query->andFilterWhere(['hid'=>$this->hid]);
            return $query->count();
        }

        public function FindNewest()
        {
            $query = self::find();
            $query->select(['permit','building_num','time']);
            $query->andWhere(['hid'=>$this->hid]);
            $query->andWhere(['del'=>$this->setDel]);
            return $query->orderBy(['create_at'=>SORT_DESC])->asArray()->one();
        }

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

}