<?php

namespace common\models;


class HouseNumber extends Common
{

    public function rules()
    {
        return [
            ['hids', 'required', 'message' => '楼盘ID不能为空'],
            ['number', 'required', 'message' => '楼盘号码不能为空'],
            ['explain', 'required', 'message' => '号码说明不能为空'],
            [['hids', 'number', 'explain'], 'string',],
        ];
    }

    public function attributeLabels()
    {
        return [
            'hids' => '楼盘ID',
            'number' => '楼盘号码',
            'explain' => '号码说明',
        ];
    }

    /**
     * 后台查询数据
     * @param $page
     * @return mixed
     */
    public function getList($page)
    {
        $query = self::find();
        $data['count'] = $query->count();
        if (!empty($page['page'])) {
            $query->offset = ($page['page'] - 1) * $page['limit'];
            $query->limit = $page['limit'];
        }
        $data['data'] = $query->asArray()->all();
        return $data;
    }

    /**
     * 根据楼盘ID查询号码
     * @param $hid
     */
    public static function SearchNumber($hid)
    {
        $query = self::find();
        $query->andWhere(['is_show' => 1]);
        $query->andWhere(['like', 'hids', $hid]);
        $query->select(['number']);

        $data = $query->asArray()->one();
        if (!empty($data)) {
            $data['number'] = explode(',', $data['number']);
            $rand = array_rand($data['number'], 1);
            return $data['number'][$rand];
        }
        return '';
    }


}