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

namespace common\models;


class PushmCity  extends Common
{

    public function rules()
    {
        return [
            ['city_id', 'unique', 'targetClass' => 'common\models\PushmCity','on'=>['add'],'message'=>'该区域已存在','filter'=>function($query){
                $query->andWhere(['del'=>$this->setDel]);
                return $query->andWhere(['type'=>$this->type]);
            }],
            ['city_id','number','message'=>'请选择区域'],
            ['type','number'],
            ['abroad','number'],
            ['url','string','max'=>255],
            ['title','string','max'=>50],
//                ['city_id','each','rule'=>['integer'],'message'=>'请输入正确的区域信息'],
            ['sort','number','message'=>'排序只能是数字'],
            ['sort','number','max'=>10000],
        ];
    }

    public function attributeLabels()
    {
        return [
            'sort'=>'排序',
            'title'=>'标题',
        ];
    }

    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_pushm_city.url','pfg_pushm_city.title','pfg_pushm_city.id','pfg_category_city.city_name','pfg_pushm_city.create_at','pfg_pushm_city.is_show','pfg_pushm_city.sort','pfg_pushm_city.img']);
        $query->andWhere(['pfg_pushm_city.del'=>$this->setDel]);
        if(!empty($input['city_name']))
        {
            $query->andWhere(['like','pfg_category_city.city_name',$input['city_name']]);
        }
        if(!empty($input['city_id']))
        {
            $query->andWhere(['pfg_pushm_city.city_id'=>$input['city_id']]);
        }
        $query->andFilterWhere(['pfg_pushm_city.type'=>$this->type]);
        $query->leftJoin('pfg_category_city','pfg_pushm_city.city_id=pfg_category_city.id');
        if(!empty($input['page']))
        {
            $query->limit = $input['limit'];
            $query->offset = ($input['page']-1 )* $input['limit'];
        }
        return $query->orderBy(['pfg_pushm_city.sort'=>SORT_ASC])->asArray()->all();
    }

    public function WhereColumn($query)
    {

    }

    public function Total()
    {
        $query = self::find();
        $query->andWhere(['pfg_pushm_city.del'=>$this->setDel]);
        $query->andFilterWhere(['pfg_pushm_city.type'=>$this->type]);
        $query->leftJoin('pfg_category_city','pfg_pushm_city.city_id=pfg_category_city.id');
        return $query->count();
    }


    public function Homegetlist($limit)
    {
        $query = self::find();
        $query->select(['pfg_category_city.id','pfg_category_city.city_name','pfg_pushm_city.img','pfg_pushm_city.url','pfg_pushm_city.title','pfg_pushm_city.abroad']);
        $query->andWhere(['pfg_pushm_city.del'=>$this->setDel]);
        $query->andWhere(['pfg_pushm_city.is_show'=>1]);
        $query->andFilterWhere(['pfg_pushm_city.type'=>$this->type]);
        $query->leftJoin('pfg_category_city','pfg_pushm_city.city_id=pfg_category_city.id');
        $query->orderBy(['pfg_pushm_city.sort'=>SORT_DESC]);
        $query->limit = $limit;
        return $query->asArray()->all();
    }

    /*
     * 返回区域的随机电话号码
     * */
    public function RandFind()
    {
        $query = self::find();
        $query->andWhere(['pfg_pushm_city.del'=>$this->setDel]);
        $query->andWhere(['pfg_pushm_city.is_show'=>1]);
        $query->andFilterWhere(['pfg_pushm_city.type'=>$this->type]);
        return $query->orderBy('RAND()')->one();
    }

    public function Homelist($limit)
    {
        $query = self::find();
        $query->select(['pfg_category_city.id','pfg_category_city.city_name','pfg_pushm_city.img','pfg_pushm_city.url','pfg_pushm_city.title','pfg_pushm_city.abroad']);
        $query->andWhere(['pfg_pushm_city.del'=>$this->setDel]);
        $query->andWhere(['pfg_pushm_city.is_show'=>1]);
        $query->andFilterWhere(['pfg_pushm_city.type'=>$this->type]);
        $query->andFilterWhere(['pfg_pushm_city.abroad'=>$this->abroad]);
        $query->leftJoin('pfg_category_city','pfg_pushm_city.city_id=pfg_category_city.id');
        $query->orderBy(['pfg_pushm_city.sort'=>SORT_DESC]);
        $query->limit = $limit;
        return $query->asArray()->all();
    }
}