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

namespace common\models;

use yii\data\Pagination;

class News extends Common
{
    public function rules()
    {
        return [
            ['thumb', 'required', 'message' => '{attribute}不能为空', 'on' => ['add', 'edit']],
            [['subject', 'abstract', 'source', 'content', 'category'], 'required', 'message' => '{attribute}不能为空'],
            [['category', 'sort', 'city', 'clicks'], 'number'],
            ['author', 'string', 'max' => 30],
            ['open_time', 'date', 'format' => 'yyyy-mm-dd', 'message' => '时间格式为:1990-01-01'],
            ['thumb_size', 'in', 'range' => [1, 2]],
            ['short_subject', 'string', 'max' => 50],
            ['hid', 'number'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'city' => '区域',
            'thumb' => '缩略图',
            'subject' => '资讯标题',
            'abstract' => '简单描述',
            'author' => '作者',
            'clicks' => '点击数',
            'content' => '资讯内容',
            'source' => '来源',
            'open_time' => '添加时间',
            'thumb_size' => '大图'
        ];
    }

    public function FindById($id)
    {
        // return self::findOne($id);
        return self::find()->andWhere(['del' => 1])->andWhere(['state' => 1])->andWhere(['id' => $id])->one();
    }

    public function FindId($id)
    {
        return self::find()->andWhere(['del' => 1])->andWhere(['id' => $id])->one();
    }

    public function FindByIdSort($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();

        $query = $this->getListWhere($query);
        if (!empty($page['page'])) {
            $query->offset = ($page['page'] - 1) * $page['limit'];
            $query->limit = $page['limit'];
        }
        return $query->orderBy(['pfg_news.create_at' => SORT_DESC])->asArray()->all();
    }

    //前台数据展示
    public function HomeList($get = null)
    {
        $query = self::find();
        $query->andWhere(['pfg_news.del' => $this->setDel]);
        $query->andWhere(['pfg_news.state' => 1]);
        $query->andWhere(['pfg_category_news.state' => 1]);
        $query->andFilterWhere(['pfg_news.category' => $this->category]);
        $query->select(['pfg_category_news.news_name', 'pfg_category_city.city_name', 'pfg_news.id', 'pfg_news.subject', 'pfg_news.thumb', 'pfg_news.abstract', 'pfg_news.open_time']);
        if (!empty($get['name'])) {
            $query->andWhere(['like', 'pfg_news.subject', $get['name']]);
        }
        $query->leftJoin('pfg_category_city', 'pfg_news.city = pfg_category_city.id');
        $query->leftJoin('pfg_category_news', 'pfg_news.category = pfg_category_news.id');
        $pages = new Pagination(['totalCount' => $this->PaginationTotal($get), 'pageSize' => 8, 'pageSizeParam' => false, 'validatePage' => false,]);
        if (!empty($get['cid'])) {
            $pages = new Pagination(['totalCount' => $this->PaginationTotal($get), 'pageSize' => 10, 'pageSizeParam' => false, 'validatePage' => false,]);
        }
        $query->offset($pages->offset);
        $query->limit($pages->limit);
        $arr = [];
        $arr['page'] = $pages;
        $arr['data'] = $query->orderBy(['pfg_news.create_at' => SORT_DESC])->asArray()->all();
        return $arr;
    }

    //移动端
    public function Mhomelist($input)
    {
        $query = self::find();
        $query->andWhere(['pfg_news.del' => $this->setDel]);
        $query->andWhere(['pfg_news.state' => 1]);
        $query->andFilterWhere(['pfg_news.category' => $this->category]);
        $query->select(['pfg_news.id', 'pfg_news.subject', 'pfg_news.thumb', 'pfg_news.abstract', 'pfg_news.open_time', 'pfg_news.thumb_size', 'pfg_news.source', 'pfg_news.clicks', 'pfg_category_city.city_name']);
        $query->leftJoin('pfg_category_city', 'pfg_news.city=pfg_category_city.id');
        if (!empty($input['page'])) {
            $query->offset = ($input['page'] - 1) * $input['limit'];
            $query->limit = $input['limit'];
        } else {
            $query->limit = 10;
        }
        return $query->orderBy(['pfg_news.create_at' => SORT_DESC])->asArray()->all();
    }

    //前台数据总数
    public function PaginationTotal($get = null)
    {
        $query = self::find();
        $query->andWhere(['pfg_news.del' => $this->setDel]);
        $query->andWhere(['pfg_news.state' => 1]);
        $query->andFilterWhere(['pfg_news.category' => $this->category]);
        if (!empty($get['name'])) {
            $query->andWhere(['like', 'pfg_news.subject', $get['name']]);
        }
        $query->leftJoin('pfg_category_city', 'pfg_news.city = pfg_category_city.id');
        $query->leftJoin('pfg_category_news', 'pfg_news.category = pfg_category_news.id');
        return $query->count();
    }


    public function getListWhere($query)
    {
        $query->select(['pfg_category_city.city_name', 'pfg_news.id', 'pfg_news.subject', 'pfg_news.state', 'pfg_news.clicks',
            'pfg_news.create_at', 'pfg_news.category', 'pfg_category_news.news_name', 'pfg_news.true_click', 'pfg_news.author', 'pfg_house.name as house_name']);
        $query->leftJoin('pfg_category_city', 'pfg_news.city = pfg_category_city.id');
        $query->leftJoin('pfg_category_news', 'pfg_news.category = pfg_category_news.id');
        $query->leftJoin('pfg_house', 'pfg_house.id = pfg_news.hid');
        $query->andWhere(['pfg_news.del' => $this->setDel]);
        $query->andFilterWhere(['like', 'pfg_news.subject', $this->subject]);
        $query->andFilterWhere(['pfg_news.state' => $this->state]);
        $query->andFilterWhere(['pfg_news.id' => $this->id]);
        $query->andFilterWhere(['pfg_news.category' => $this->category]);
        $query->andFilterWhere(['pfg_news.city' => $this->city]);
        $query->andFilterWhere(['pfg_news.author' => $this->author]);
//            $query->andFilterWhere(['pfg_news.hid'=>$this->hid]);
        return $query;
    }

    //排序点击量
    public function Clicks($limit)
    {
        $query = self::find();
        $query->select(['subject', 'id', 'clicks']);
        $query->andWhere(['del' => $this->setDel]);
        $query->andWhere(['state' => 1]);
        $query->andFilterWhere(['category' => $this->category]);
        $query->limit = $limit;
        return $query->orderBy(['pfg_news.clicks' => SORT_DESC])->asArray()->all();
    }

    //资讯类型
//        public fu


    public function Total()
    {
        $query = self::find();
        $query = $this->getListWhere($query);
        return $query->count();
    }

    public function HouseNewsList($page)
    {
        $query = self::find();
        $query->select(['subject', 'clicks', 'id', 'state']);
        $query = $this->WhereColumn($query);
        if (!empty($page['page'])) {
            $query->offset = ($page['page'] - 1) * $page['limit'];
            $query->limit = $page['limit'];
        }
        return $query->orderBy(['create_at' => SORT_DESC])->asArray()->all();
    }

    private function WhereColumn($query)
    {
        $query->andFilterWhere(['like', 'subject', $this->subject]);
        $query->andFilterWhere(['hid' => $this->hid]);
        $query->andFilterWhere(['category' => $this->category]);
        return $query;
    }

    public function HouseNewsTotal()
    {
        $query = self::find();
        $query = $this->WhereColumn($query);
        return $query->count();
    }

    //项目动态的资讯
    public function GetDynamic($limit)
    {
        $query = self::find();
        $query->andWhere(['pfg_news.del' => $this->setDel]);
        $query->andWhere(['pfg_news.state' => 1]);
        $query->andFilterWhere(['pfg_news.category' => 1]);
        $query->select(['pfg_news.id', 'pfg_news.subject', 'pfg_news.abstract', 'pfg_news.open_time']);
        $query->limit = $limit;
        $query->orderBy(['pfg_news.create_at' => SORT_DESC]);
        return $query->asArray()->all();
    }

    /**
     * 根据资讯栏目获取最新资讯
     * @param $category
     * @param $limit
     * @return array|\yii\db\ActiveRecord[]
     */
    public function getNews($category, $limit)
    {
        $query = self::find();
        $query->andWhere(['pfg_news.category' => $category]);
        $query->andWhere(['pfg_news.state' => 1]);
        $query->andWhere(['pfg_news.del' => 1]);
        $query->select(['pfg_news.id', 'pfg_news.category', 'pfg_news.subject', 'pfg_news.short_subject', 'pfg_news.thumb', 'pfg_category_news.news_name', 'pfg_category_city.city_name']);
        $query->leftJoin('pfg_category_news', 'pfg_news.category = pfg_category_news.id');
        $query->leftJoin('pfg_category_city', 'pfg_news.city = pfg_category_city.id');
        $query->orderBy(['pfg_news.create_at' => SORT_DESC]);
        $query->limit = $limit;
        return $query->asArray()->all();
    }

    /*
     * 获取楼盘相关资讯
     * */
    public function AllHouseNews()
    {
        $query = self::find();
        $query->select(['pfg_news.*', 'pfg_category_news.news_name']);
        $query->andWhere(['pfg_news.hid' => $this->hid]);
        $query->andWhere(['pfg_news.del' => 1]);
        $query->andWhere(['pfg_news.state' => 1]);
        if (!empty($cid)) {
            $query->andWhere(['pfg_category_news.id' => $cid]);
        }
        $query->innerJoin('pfg_category_news', 'pfg_category_news.id = pfg_news.category');
        return $query->orderBy(['pfg_news.create_at' => SORT_DESC])->asArray()->all();
    }

    /**
     * 获取楼盘关联资讯,HouseNews表以及News表一同获取
     * */
    public function getNewsListHouse($nids,$limit = null)
    {
        $select = [
            'pfg_news.*',
            'pfg_category_news.news_name'
        ];
        $query = self::find();
        $query->select($select);
        $query->where(['pfg_news.del'=>1]);
        $query->andFilterWhere(['pfg_news.hid'=>$this->hid]);
        if(!empty($nids)){
            $query->orFilterWhere(['pfg_news.id'=>$nids]);
        }
        if(!empty($limit)){
            $query->limit($limit);
        }
        $query->leftJoin('pfg_category_news','pfg_news.category = pfg_category_news.id');
        return $query->orderBy(['pfg_news.create_at'=>SORT_DESC])->asArray()->all();
    }
    
	//2020-11-18
	public function getNewsByCategory($category,$limit = null){
		$query = self::find();
		$query->select("*");
		$query->where(['category'=>$category,'state'=>1,'del'=>1]);
		if(!empty($limit)){
			$query->limit($limit);
		}
		return $query->orderBy(['create_at'=>SORT_DESC])->asArray()->all();
	}
	public function getHouseNewByHid($hid, $limit = null)
	{
		$query = self::find();
		$query->select(['subject','id']);
		$query->where(['del'=>1,'state'=>1,'hid'=>$hid]);
		if(!empty($limit)){
			$query->limit($limit);
		}
		return $query->orderBy(['create_at'=>SORT_DESC])->asArray()->all();
	}
	
	/**
	 * 传入楼盘名称,查找标题中含有楼盘名称的资讯
	 * @param $name 楼盘名称
	 * @param $limit 条数
	 * @return array|\yii\db\ActiveRecord[]
	 */
	public function getHouseNews($name, $limit = null)
	{
		$query = self::find();
		$query->andWhere(['like', 'subject', $name]);
		$query->andWhere(['state' => 1]);
		$query->andWhere(['del' => 1]);
		$query->select(['id', 'open_time', 'thumb', 'subject', 'abstract', 'true_click', 'category', 'create_at']);
		if (!empty($limit)) {
			$query->limit = $limit;
		}
		return $query->orderBy(['create_at' => SORT_DESC])->asArray()->all();
	}
	
}