<?php


namespace common\models;


class PushDiscount extends Common
{

    public function rules()
    {
        return [
            ['hid', 'unique', 'targetClass' => 'common\models\PushDiscount', 'message' => '该楼盘已存在', 'filter' => function ($query) {
                return $query->andWhere(['type' => $this->type]);
            }],
            ['url', 'url', 'defaultScheme' => 'http', 'message' => '请输入正确的链接地址'],
            [['type', 'sort', 'is_show', 'hid'], 'number'],
            ['sort', 'number', 'max' => 10000],
            [['is_show'], 'in', 'range' => [1, 2]],
            [['title', 'subject'], 'string', 'max' => 255],
        ];
    }

    public function attributeLabels()
    {
        return [
            'hid' => '楼盘',
            'title' => '标题',
            'subject' => '内容',
            'url' => '跳转链接'
        ];
    }

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

    /**
     * 后台统计条数
     * @param $post
     * @return int|string
     */
    public function countList($post)
    {
        $query = self::find();
        $query->andWhere(['pfg_push_discount.type' => $post['type']]);
        $query->leftJoin('pfg_house', 'pfg_push_discount.hid = pfg_house.id');
        return $query->count();
    }

    /**
     * PC前台列表页查询数据
     * @param $type
     * @param $limit
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getHomeList($type, $limit)
    {
        $query = self::find();
        $query->andWhere(['pfg_push_discount.type' => $type]);
        $query->select(['pfg_house.id', 'pfg_house.name', 'pfg_house.sale_price', 'pfg_house_detail.price_unit', 'pfg_push_discount.url', 'pfg_push_discount.title', 'pfg_push_discount.subject']);
        $query->leftJoin('pfg_house', 'pfg_push_discount.hid = pfg_house.id');
        $query->leftJoin('pfg_house_detail', 'pfg_house.id = pfg_house_detail.hid');
        $query->orderBy(['pfg_push_discount.sort' => SORT_DESC, 'pfg_push_discount.id' => SORT_DESC]);
        $query->limit = $limit;
        return $query->asArray()->all();
    }


}