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

namespace common\models;


class PushmRotation  extends Common
{

        public function rules()
        {
            return [
                [['title','url'],'required','message'=>'{attribute}不能为空'],
                ['url', 'url', 'defaultScheme' => 'http','message'=>'请输入正确的地址'],
                ['title','string','max'=>50],
                ['img','string','max'=>50],
                ['hid','number','message'=>'请选择楼盘'],
                ['sort','number','message'=>'排序只能是数字'],
                ['city','default','value'=>0],
                ['city','number'],
                ['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_pushm_rotation.id','pfg_pushm_rotation.hid',
                'pfg_pushm_rotation.img','pfg_pushm_rotation.url','pfg_pushm_rotation.title','pfg_pushm_rotation.create_at',
                'pfg_pushm_rotation.is_show','pfg_pushm_rotation.sort','pfg_pushm_rotation.expiration_date']);
            $query->andWhere(['pfg_pushm_rotation.del'=>$this->setDel]);
            $query->leftJoin('pfg_house','pfg_pushm_rotation.hid=pfg_house.id');
            $query->leftJoin('pfg_category_city','pfg_pushm_rotation.city=pfg_category_city.id');
            if(!empty($input['city']))
            {
                $query->andFilterWhere(['pfg_pushm_rotation.city'=>$input['city']]);
            }
            if (!empty($input['name'])) {
                $query->andWhere(['like','pfg_house.name',$input['name']]);
            }
            if(!empty($input['page']))
            {
                $query->limit = $input['limit'];
                $query->offset = ($input['page']-1 )* $input['limit'];
            }
            return $query->orderBy(['pfg_pushm_rotation.is_show'=>SORT_ASC,'pfg_pushm_rotation.sort'=>SORT_DESC,'pfg_pushm_rotation.create_at'=>SORT_DESC])->asArray()->all();
        }

        public function WhereColumn($query)
        {

        }

        public function Total($input)
        {
            $query = self::find();
            $query->andWhere(['pfg_pushm_rotation.del'=>$this->setDel]);
            if(!empty($input['city']))
            {
                $query->andFilterWhere(['pfg_pushm_rotation.city'=>$input['city']]);
            }
            if (!empty($input['name'])) {
                $query->andWhere(['like','pfg_house.name',$input['name']]);
            }
            $query->leftJoin('pfg_house','pfg_pushm_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 = 5;
                return $query->all();
        }

        /**不分地区读取全部*/
        public function GetAllList()
        {
                $query = self::find();
                $query->select(['img','url','title','expiration_date']);
                $query->andWhere(['del'=>$this->setDel]);
                $query->andWhere(['is_show'=>1]);
                $query->orderBy(['sort'=>SORT_ASC]);
                $query->limit = 5;
                return $query->all();
        }
	
	/**
	 * 前台获取图片
	 * @param $type
	 * @param $limit
	 * @return array|\yii\db\ActiveRecord[]
	 */
	public function getRoationChart( $limit)
	{
		$query = self::find();
		$query->andWhere(['del' => $this->setDel]);
		$query->select(['title', 'url', 'img']);
		$query->limit = $limit;
		$query->orderBy(['sort' => SORT_DESC, 'id' => SORT_DESC]);
		$data = $query->asArray()->all();
		
		if (!empty($data)) {
			foreach ($data as &$val) {
				$val['img'] = \Yii::$app->params['httpImg']['host'] . \Yii::$app->params['httpImg']['push_lb'] . $val['img'];
			}
			return $data;
		}
	}

}