123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- <?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->where(['pfg_news.state'=>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();
- }
-
- }
|