<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/5/21
 * Time: 下午5:18
 */

namespace common\models;


class PushRotation extends Common
{

    public function rules()
    {
        return [
            [['hid', 'title', 'url'], 'required', 'message' => '{attribute}不能为空'],
            ['url', 'url', 'defaultScheme' => 'http', 'message' => '请输入正确的地址'],
            ['title', 'string', 'max' => 50],
            ['city', 'number'],
            ['hid', 'number', 'message' => '请选择楼盘'],
            ['sort', 'number', 'message' => '排序只能是数字'],
            ['sort', 'number', 'max' => 10000],
            ['expiration_date', 'string']
        ];
    }

    public function attributeLabels()
    {
        return [
            'hid' => '楼盘名称',
            'url' => '地址',
            'title' => '标题',
            'city' => '区域',
        ];
    }


    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_category_city.city_name', 'pfg_house.name', 'pfg_push_rotation.id', 'pfg_push_rotation.hid',
            'pfg_push_rotation.img', 'pfg_push_rotation.url', 'pfg_push_rotation.title', 'pfg_push_rotation.create_at',
            'pfg_push_rotation.is_show', 'pfg_push_rotation.sort', 'pfg_push_rotation.expiration_date', 'pfg_push_rotation.universal']);
        $query->andWhere(['pfg_push_rotation.del' => $this->setDel]);
        $query->leftJoin('pfg_house', 'pfg_push_rotation.hid=pfg_house.id');
        $query->leftJoin('pfg_category_city', 'pfg_push_rotation.city=pfg_category_city.id');
        if (!empty($input['title'])) {
            $query->andWhere(['like', 'pfg_push_rotation.title', $input['title']]);
        }
        if (!empty($input['city'])) {
            $query->andFilterWhere(['pfg_push_rotation.city' => $input['city']]);
        }
        if (!empty($input['page'])) {
            $query->limit = $input['limit'];
            $query->offset = ($input['page'] - 1) * $input['limit'];
        }
        return $query->orderBy(['pfg_push_rotation.is_show' => SORT_ASC, 'pfg_push_rotation.sort' => SORT_DESC, 'pfg_push_rotation.id' => SORT_DESC])->asArray()->all();
    }

    public function WhereColumn($query)
    {

    }

    public function Total($input)
    {
        $query = self::find();
        $query->andWhere(['pfg_push_rotation.del' => $this->setDel]);
        if (!empty($input['title'])) {
            $query->andWhere(['like', 'pfg_push_rotation.title', $input['title']]);
        }
        if (!empty($input['city'])) {
            $query->andFilterWhere(['pfg_push_rotation.city' => $input['city']]);
        }
        $query->leftJoin('pfg_house', 'pfg_push_rotation.hid=pfg_house.id');
        return $query->count();
    }


    public function HomeGetList()
    {
        $query = self::find();
        $query->select(['img', 'url', 'title', 'expiration_date']);
        $query->andWhere(['del' => $this->setDel]);
        $query->andFilterWhere(['city' => $this->city]);
        $query->andWhere(['is_show' => 1]);
        $query->orderBy(['sort' => SORT_ASC]);
        $query->limit = 8;
        return $query->all();
    }

    /*
     * 2020.7.20 lyy 增加公共轮播图获取
     * */
    public function HomeGetListP()
    {
        $p_list = $this->getPublicRotation();
        $query = self::find();
        $query->select(['id','img', 'url', 'title', 'expiration_date']);
        $query->andWhere(['del' => $this->setDel]);
        $query->andFilterWhere(['city' => $this->city]);
        $query->andWhere(['is_show' => 1]);
        if(!empty($p_list)){
            $query->andWhere(['not','id=:id']);
            $query->addParams([':id' => $p_list['id']]);
            $query->limit = 7;
        }else{
            $query->limit = 8;
        }
        $query->orderBy(['sort' => SORT_ASC]);
        $data_list = $query->asArray()->all();
        if(!empty($p_list)){
            array_unshift($data_list,$p_list);
        }
        return $data_list;
    }

    public function getRoationChart($limit)
    {
        $p_list = $this->getPublicRotation();
        $query = self::find();
        $query->select(['id','img', 'url', 'title', 'expiration_date']);
        $query->andWhere(['del' => $this->setDel]);
        $query->andFilterWhere(['city' => $this->city]);
        $query->andWhere(['is_show' => 1]);
        if(!empty($p_list)){
            $query->andWhere(['not','id=:id']);
            $query->addParams([':id' => $p_list['id']]);
        }
        $query->limit = $limit;
        $query->orderBy(['sort' => SORT_ASC]);
        $data_list = $query->asArray()->all();
        if(!empty($p_list)){
            array_unshift($data_list,$p_list);
        }
        return $data_list;
    }

    public function getPublicRotation()
    {
        $query = self::find();
        $query->select(['id','img', 'url', 'title', 'expiration_date']);
        $query->andWhere(['del' => $this->setDel]);
        $query->andWhere(['universal' => 1]);
        $query->andWhere(['is_show' => 1]);
        $query->orderBy(['sort' => SORT_ASC]);
        $query->limit = 1;
        return $query->asArray()->one();
    }
}