<?php
/**
 * Created by PhpStorm.
 * User: xiaofeng
 * Date: 2018/3/4
 * Time: 上午9:42
 */

namespace common\models;
use yii\data\Pagination;
class NewsRenovation extends Common
{
    public function rules()
    {
        return [
            [['type','title','source','describe'],'required','message'=>'{attribute}不能为空'],
            ['title','string','max'=>100],
            ['author','string','max'=>10],
            ['source','string','max'=>50],
            ['describe','string','max'=>255],
            ['click','number'],
            ['type','number'],
            ['hid','number','message'=>'请输入正确的楼盘名称'],
            ['click_true','number'],
            ['is_show','in','range'=>[1,2]],
            ['del','in','range'=>[1,2]],
        ];
    }

    public function attributeLabels()
    {
        return [
            'hid'=>'楼盘名称',
            'img'=>'缩略图',
            'title'=>'标题',
            'describe'=>'描述',
            'author'=>'作者',
            'click'=>'点击数',
            'source'=>'来源',
            'type'=>'装修风格',
        ];
    }

    public function Clickadd($id)
    {
        $query = self::findOne($id);
        $query->click = ++$query->click;
        $query->click_true = ++$query->click_true;
        return $query->save();
    }

    public function FindById($id)
    {
        return self::findOne($id);
    }

    public function Authenticator($input)
    {
        $this->load($input,'');
        if($this->validate()) return $this;
        return $this->errors;
    }

    public function getList($page)
    {
        $query = self::find();
//        'count(pfg_news_renovationcontent.nid) as count',
        $query->select(['pfg_house.name','pfg_news_renovation.hid','pfg_news_renovation.title','pfg_news_renovation.is_show','pfg_news_renovation.create_at','pfg_news_renovation.id','pfg_news_renovation.click','pfg_news_renovation.click_true']);
        $query->andWhere(['pfg_news_renovation.del'=>$this->setDel]);
        $query->andFilterWhere(['like','pfg_news_renovation.title',$this->title]);
        if(!empty($page['page']))
        {
            $query->offset = ($page['page'] - 1) * $page['limit'];
            $query->limit = $page['limit'];
        }
        $query->leftJoin('pfg_house','pfg_news_renovation.hid = pfg_house.id');
//        $query->leftJoin('pfg_news_renovationcontent','pfg_news_renovation.id = pfg_news_renovationcontent.nid');
        return $query->orderBy(['pfg_news_renovation.create_at'=>SORT_DESC])->asArray()->all();
    }



    public function getListTotal()
    {
        $query = self::find();
        $query->andWhere(['pfg_news_renovation.del'=>$this->setDel]);
        $query->andFilterWhere(['like','pfg_news_renovation.title',$this->title]);
        return $query->count();
    }

    public function PcgetList($page)
    {
        $query = self::find();
        $query->andWhere(['del'=>$this->setDel]);
        $query->andWhere(['is_show'=>1]);
        $query->select(['title','id','img','click','type']);
        if(is_object($page))
        {
            $query->offset($page->offset);
            $query->limit($page->limit);
        }
        $query->orderBy(['create_at'=>SORT_DESC,'click'=>SORT_DESC,'sort'=>SORT_DESC]);
        return $query->asArray()->all();
    }

    public function PcgetListTotal()
    {
        $query = self::find();
        $query->andWhere(['del'=>$this->setDel]);
        $query->andWhere(['is_show'=>1]);
        return $query->count();
    }

    public function RandList($limit,$sele = null)
    {
        $query = self::find();
        $query->andWhere(['del'=>$this->setDel]);
        $query->andWhere(['is_show'=>1]);
        $query->select($sele);
        $query->limit = $limit;
        $query->orderBy('RAND()');
        return $query->asArray()->all();
    }

    public function MgetList($page)
    {
        $query = self::find();
        $query->andWhere(['del'=>$this->setDel]);
        $query->andWhere(['is_show'=>1]);
        $query->select(['title','id','img','click','type']);
        if(!empty($page['page']))
        {
            $query->offset = ($page['page'] - 1) * $page['limit'];
            $query->limit = $page['limit'];
        }
        $query->orderBy(['create_at'=>SORT_DESC,'click'=>SORT_DESC,'sort'=>SORT_DESC]);
        return $query->asArray()->all();
    }




}