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

namespace common\models;
use yii\data\Pagination;
class NewsKnowledge extends Common
{
        public function rules()
        {
            return [
                ['thumb','required','message'=>'不能为空','on'=>'add'],
                [['subject','abstract','source','content','label_id'],'required','message'=>'{attribute}不能为空'],
                [['label_id','sort','clicks'],'number'],
                ['author','string','max'=>30],
                ['accountability','number'],
                [['thumb_size','is_show','del'],'in','range'=>[1,2]],
                ['short_subject','string','max'=>50],
                [['thumb_size','del','is_show'],'default','value'=>1],
                ['thumb','string','max'=>200],
				['keywords','string','max'=>255],
            ];
        }

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

        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 WhereColumn()
        {
            $query = static::find();
            $query->andWhere(['pfg_news_knowledge.del'=>$this->setDel]);
            $query->andFilterWhere(['pfg_news_knowledge.label_id'=>$this->label_id]);
            $query->andFilterWhere(['pfg_news_knowledge.id'=>$this->id]);
            $query->andFilterWhere(['like','pfg_news_knowledge.subject',$this->subject]);
            return $query;
        }

        public function getListTotal()
        {
            $query = $this->WhereColumn();
            return $query->count();
        }

        public function getList($page,$select = null,$sort)
        {
            $query = $this->WhereColumn();
            $query->select($select);
            switch ($sort)
            {
                case 1:
                    $query->orderBy(['pfg_news_knowledge.sort'=>SORT_DESC]);
                    break;
                case 2:
                    $query->orderBy(['pfg_news_knowledge.sort'=>SORT_ASC]);
                    break;
                case 3:
                    $query->orderBy(['pfg_news_knowledge.create_at'=>SORT_DESC]);
                    break;
                default:
                    break;
            }
            $query->leftJoin('pfg_new_knowledgelabel','pfg_news_knowledge.label_id = pfg_new_knowledgelabel.id');
            if(!empty($page['page']))
            {
                $query->offset = ($page['page'] - 1) * $page['limit'];
                $query->limit = $page['limit'];
            }
            return $query->asArray()->all();
        }

      public function getTitle($page,$select=null,$sort)
        {
            $query = self::find();
            $query->select($select);
            $query->andWhere(['del'=>$this->setDel]);
            $query->andWhere(['is_show'=>1]);
            if(isset($page['keywords'])){
                $query->andFilterWhere(['or',
['like','subject',$page['keywords']],]);
            }
            $query->andFilterWhere(['label_id'=>$this->label_id]);
            switch ($sort)
            {
                case 1:
                    $query->orderBy(['sort'=>SORT_DESC]);
                    break;
                case 2:
                    $query->orderBy(['sort'=>SORT_ASC]);
                    break;
                default:
                    $query->orderBy(['create_at'=>SORT_DESC]);
                    break;
            }
            $pages = new Pagination(['totalCount' =>$query->count(),'pageSize'=>8,  'pageSizeParam' => false, 'validatePage' => false, ]);
            $query->offset($pages->offset);
            $query->limit($pages->limit);
            $arr = [];
            $arr['page'] = $pages;
            $arr['data'] = $query->asArray()->all();
            return $arr;
        }
        public function getMtitle($page,$select=null,$sort)
        {
            $query = self::find();
            $query->select($select);
            $query->andWhere(['pfg_news_knowledge.del'=>$this->setDel]);
            $query->andWhere(['pfg_news_knowledge.is_show'=>1]);
            $query->leftJoin('pfg_new_knowledgelabel','pfg_news_knowledge.label_id=pfg_new_knowledgelabel.id');
            if(isset($page['name'])){
                $query->andFilterWhere(['or',
['like','pfg_news_knowledge.subject',$page['name']],]);
            }
            $query->andFilterWhere(['pfg_news_knowledge.label_id'=>$this->label_id]);
            
            switch ($sort)
            {
                case 1:
                    $query->orderBy(['pfg_news_knowledge.sort'=>SORT_DESC]);
                    break;
                case 2:
                    $query->orderBy(['pfg_news_knowledge.sort'=>SORT_ASC]);
                    break;
                default:
                    $query->orderBy(['pfg_news_knowledge.create_at'=>SORT_DESC]);
                    break;
            }
            if(!empty($page['page']))
            {
                $query->offset = ($page['page'] - 1) * $page['limit'];
                $query->limit = $page['limit'];
            }
            else
            {
                $query->limit = 10;
            }
            return $query->asArray()->all();
        }

        /*上一篇和下一篇*/
        public function getPage($input)
        {
            if(is_numeric($input['nid']))//'<=','pfg_house.create_at'
            {
                $info = self::find()->andWhere([$input['mark'],'id',$input['nid']])->andWhere(['del'=>1])->andWhere(['is_show'=>1])->one();
                if(!empty($info))
                {
                    $info->clicks =  ++$info->clicks;
                    $info->true_click =  ++$info->true_click;
                    $info->save(false);
                }
                return $info;
            }
        }

        /*
         * 详情
         * */
        public function NewsKnowledgeDetails($input)
        {
            if(is_numeric($input['nid']))
            {
                // $ip = new \mobile\server\IpServer();
                // $ipaddress = $ip->add(['nid'=>$input['nid'],'type'=>2]);
                $info = self::find()->andWhere(['id'=>$input['nid']])->andWhere(['del'=>1])->andWhere(['is_show'=>1])->one();
                // if($ipaddress === true)
                // {

                    if(!empty($info))
                    {
                        $info->clicks =  ++$info->clicks;
                        $info->true_click =  ++$info->true_click;
                        $info->save(false);
                    }
                // }
                return $info;
            }
        }
        /*热度关键词*/
        public function HotKeyword($select=null)
        {
            $query = self::find();
            $query->select($select);
            $query->andWhere(['del'=>$this->setDel]);
            $query->andWhere(['is_show'=>1]);
            return $query->asArray()->all();
        }
        public function RelevantArticle($select=null,$limit=10)
        {
            $query = self::find();
            $query->select($select);
            $query->andWhere(['del'=>$this->setDel]);
            $query->andWhere(['is_show'=>1]);
            $query->limit = $limit;
            return $query->asArray()->all();
        }
}