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

namespace common\models;


class PushBottom extends Common
{

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

    public function attributeLabels()
    {
        return [
            'url' => '地址',
            'title' => '标题',
            'explain' => '活动说明',
            'button_color' => '按钮颜色',
            'buttonfont_color' => '按钮字体颜色',
            'font_color' => '字体颜色',
        ];
    }


    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_house.name', 'pfg_category_city.city_name', 'pfg_push_bottom.id', 'pfg_push_bottom.title', 'pfg_push_bottom.img', 'pfg_push_bottom.url',
            'pfg_push_bottom.explain', 'pfg_push_bottom.button_color', 'pfg_push_bottom.buttonfont_color', 'pfg_push_bottom.img_small',
            'pfg_push_bottom.font_color', 'pfg_push_bottom.create_at', 'pfg_push_bottom.sort', 'pfg_push_bottom.is_show', 'pfg_push_bottom.img_show', 'pfg_push_bottom.state', 'pfg_push_bottom.universal']);

        $query->leftJoin('pfg_house', 'pfg_push_bottom.hid=pfg_house.id');
        $query->leftJoin('pfg_category_city', 'pfg_push_bottom.city_pid=pfg_category_city.id');
        if (!empty($input['title'])) {
            $query->andWhere(['like', 'pfg_push_bottom.title', $input['title']]);
        }
        if (!empty($input['city'])) {
            $query->andWhere(['pfg_push_bottom.city_pid' => $input['city']]);
        }
        if (!empty($input['page'])) {
            $query->limit = $input['limit'];
            $query->offset = ($input['page'] - 1) * $input['limit'];
        }
        return $query->orderBy(['pfg_push_bottom.is_show' => SORT_ASC, 'pfg_push_bottom.sort' => SORT_DESC, 'pfg_push_bottom.create_at' => SORT_DESC])->asArray()->all();
    }

    public function WhereColumn($query)
    {

    }

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


    public static function HomeGetList()
    {
        $hostId = \Yii::$app->hostserver->hostId;
        $query = self::find();
        $query->select(['pfg_push_bottom.state', 'input', 'img', 'hid', 'url', 'explain', 'button_color', 'buttonfont_color', 'font_color', 'pfg_category_city.city_name']);
        $query->leftJoin('pfg_category_city', 'pfg_push_bottom.city_pid=pfg_category_city.id');
        $query->andWhere(['is_show' => 1]);
        if ($hostId != 0) {
            $query->andWhere(['pfg_push_bottom.city_pid' => $hostId]);
        }
        $query->limit = 5;
        return $query->orderBy(['pfg_push_bottom.sort' => SORT_DESC])->asArray()->all();
    }

    /*
     * 2020.7.20 lyy 增加公共地步横幅获取
     * */
    public static function HomeGetListP()
    {
        $p_list = self::getPublicBottom();
        $hostId = \Yii::$app->hostserver->hostId;
        $query = self::find();
        $query->select(['pfg_push_bottom.state','pfg_push_bottom.id', 'input', 'img', 'hid', 'url', 'explain', 'button_color', 'buttonfont_color', 'font_color', 'pfg_category_city.city_name']);
        $query->leftJoin('pfg_category_city', 'pfg_push_bottom.city_pid=pfg_category_city.id');
        $query->andWhere(['is_show' => 1]);
        if(!empty($p_list)){
            $query->andWhere(['not','pfg_push_bottom.id=:id']);
            $query->addParams([':id' => $p_list['id']]);
            $query->limit = 4;
        }else{
            $query->limit = 5;
        }
        if ($hostId != 0) {
            $query->andWhere(['pfg_push_bottom.city_pid' => $hostId]);
        }
        $data_list = $query->orderBy(['pfg_push_bottom.sort' => SORT_DESC])->asArray()->all();
        if(!empty($p_list)){
            array_unshift($data_list,$p_list);
        }
        return $data_list;
    }

    public static function getPublicBottom()
    {
        $query = self::find();
        $query->select(['pfg_push_bottom.state','pfg_push_bottom.id', 'input', 'img', 'hid', 'url', 'explain', 'button_color', 'buttonfont_color', 'font_color', 'pfg_category_city.city_name']);
        $query->leftJoin('pfg_category_city', 'pfg_push_bottom.city_pid=pfg_category_city.id');
        $query->andWhere(['is_show' => 1]);
        $query->andWhere(['universal' => 1]);
        $query->limit = 1;
        $query->orderBy(['pfg_push_bottom.sort' => SORT_DESC]);
        return $query->asArray()->one();
    }


    public static function Getimgsmall()
    {
        $hostId = \Yii::$app->hostserver->hostId;

        $query = self::find();
        $query->select(['img_small', 'url']);
        if ($hostId == 0) {
            $query->andWhere(['img_show' => 1]);
            return $query->one();
        } elseif ($hostId != 0) {
            $query->andWhere(['pfg_push_bottom.city_pid' => $hostId]);
            $query->orderBy(['pfg_push_bottom.sort' => SORT_DESC]);
            return $query->one();
        }
    }

}