<?php
/**
 * Created by PhpStorm.
 * User: 201901
 * Date: 2020/7/7
 * Time: 9:19
 */

namespace common\models;


class HomeDiscount extends Common
{

    public function rules()
    {
        return [
            ['hid', 'required', 'message' => '{attribute}不能为空'],
            ['hid', 'unique', 'targetClass' => 'common\models\HomeDiscount', 'on' => ['add', 'edit'], 'message' => '该楼盘已存在'],
            ['title', 'string', 'max' => 255],
            ['sort', 'number', 'message' => '排序只能是数字'],
            ['sort', 'number', 'max' => 10000],
        ];
    }

    public function attributeLabels()
    {
        return [
            'hid' => '楼盘',
        ];
    }


    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->select(['pfg_home_discount.id', 'pfg_home_discount.create_at', 'pfg_home_discount.is_show', 'pfg_home_discount.sort', 'pfg_home_discount.title', 'pfg_house.name']);
        $query->innerJoin('pfg_house', 'pfg_home_discount.hid = pfg_house.id');
        if (!empty($input['page'])) {
            $query->limit = $input['limit'];
            $query->offset = ($input['page'] - 1) * $input['limit'];
        }
        return $query->orderBy(['pfg_home_discount.is_show' => SORT_ASC, 'pfg_home_discount.sort' => SORT_DESC, 'pfg_home_discount.id' => SORT_DESC])->asArray()->all();
    }

    public function Total($input)
    {
        $query = self::find();
        $query->innerJoin('pfg_house', 'pfg_home_discount.hid = pfg_house.id');
        return $query->count();
    }

    public static function getDiscount($limit)
    {
        $query = self::find();
        $query->andWhere(['pfg_home_discount.is_show' => 1]);
        $query->select(['pfg_home_discount.hid', 'pfg_home_discount.title', 'pfg_house.name']);
        $query->innerJoin('pfg_house', 'pfg_home_discount.hid = pfg_house.id');
        $query->limit = $limit;
        return $query->orderBy(['pfg_home_discount.sort' => SORT_DESC, 'pfg_home_discount.id' => SORT_DESC])->asArray()->all();
    }


}