News.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/3/4
  6. * Time: 上午9:42
  7. */
  8. namespace common\models;
  9. use yii\data\Pagination;
  10. class News extends Common
  11. {
  12. public function rules()
  13. {
  14. return [
  15. ['thumb', 'required', 'message' => '{attribute}不能为空', 'on' => ['add', 'edit']],
  16. [['subject', 'abstract', 'source', 'content', 'category'], 'required', 'message' => '{attribute}不能为空'],
  17. [['category', 'sort', 'city', 'clicks'], 'number'],
  18. ['author', 'string', 'max' => 30],
  19. ['open_time', 'date', 'format' => 'yyyy-mm-dd', 'message' => '时间格式为:1990-01-01'],
  20. ['thumb_size', 'in', 'range' => [1, 2]],
  21. ['short_subject', 'string', 'max' => 50],
  22. ['hid', 'number'],
  23. ];
  24. }
  25. public function attributeLabels()
  26. {
  27. return [
  28. 'city' => '区域',
  29. 'thumb' => '缩略图',
  30. 'subject' => '资讯标题',
  31. 'abstract' => '简单描述',
  32. 'author' => '作者',
  33. 'clicks' => '点击数',
  34. 'content' => '资讯内容',
  35. 'source' => '来源',
  36. 'open_time' => '添加时间',
  37. 'thumb_size' => '大图'
  38. ];
  39. }
  40. public function FindById($id)
  41. {
  42. // return self::findOne($id);
  43. return self::find()->andWhere(['del' => 1])->andWhere(['state' => 1])->andWhere(['id' => $id])->one();
  44. }
  45. public function FindId($id)
  46. {
  47. return self::find()->andWhere(['del' => 1])->andWhere(['id' => $id])->one();
  48. }
  49. public function FindByIdSort($id)
  50. {
  51. return self::findOne($id);
  52. }
  53. public function Authenticator($input)
  54. {
  55. $this->load($input, '');
  56. if ($this->validate()) return $this;
  57. return $this->errors;
  58. }
  59. public function getList($page)
  60. {
  61. $query = self::find();
  62. $query = $this->getListWhere($query);
  63. if (!empty($page['page'])) {
  64. $query->offset = ($page['page'] - 1) * $page['limit'];
  65. $query->limit = $page['limit'];
  66. }
  67. return $query->orderBy(['pfg_news.create_at' => SORT_DESC])->asArray()->all();
  68. }
  69. //前台数据展示
  70. public function HomeList($get = null)
  71. {
  72. $query = self::find();
  73. $query->andWhere(['pfg_news.del' => $this->setDel]);
  74. $query->andWhere(['pfg_news.state' => 1]);
  75. $query->andWhere(['pfg_category_news.state' => 1]);
  76. $query->andFilterWhere(['pfg_news.category' => $this->category]);
  77. $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']);
  78. if (!empty($get['name'])) {
  79. $query->andWhere(['like', 'pfg_news.subject', $get['name']]);
  80. }
  81. $query->leftJoin('pfg_category_city', 'pfg_news.city = pfg_category_city.id');
  82. $query->leftJoin('pfg_category_news', 'pfg_news.category = pfg_category_news.id');
  83. $pages = new Pagination(['totalCount' => $this->PaginationTotal($get), 'pageSize' => 8, 'pageSizeParam' => false, 'validatePage' => false,]);
  84. if (!empty($get['cid'])) {
  85. $pages = new Pagination(['totalCount' => $this->PaginationTotal($get), 'pageSize' => 10, 'pageSizeParam' => false, 'validatePage' => false,]);
  86. }
  87. $query->offset($pages->offset);
  88. $query->limit($pages->limit);
  89. $arr = [];
  90. $arr['page'] = $pages;
  91. $arr['data'] = $query->orderBy(['pfg_news.create_at' => SORT_DESC])->asArray()->all();
  92. return $arr;
  93. }
  94. //移动端
  95. public function Mhomelist($input)
  96. {
  97. $query = self::find();
  98. $query->andWhere(['pfg_news.del' => $this->setDel]);
  99. $query->andWhere(['pfg_news.state' => 1]);
  100. $query->andFilterWhere(['pfg_news.category' => $this->category]);
  101. $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']);
  102. $query->leftJoin('pfg_category_city', 'pfg_news.city=pfg_category_city.id');
  103. if (!empty($input['page'])) {
  104. $query->offset = ($input['page'] - 1) * $input['limit'];
  105. $query->limit = $input['limit'];
  106. } else {
  107. $query->limit = 10;
  108. }
  109. return $query->orderBy(['pfg_news.create_at' => SORT_DESC])->asArray()->all();
  110. }
  111. //前台数据总数
  112. public function PaginationTotal($get = null)
  113. {
  114. $query = self::find();
  115. $query->andWhere(['pfg_news.del' => $this->setDel]);
  116. $query->andWhere(['pfg_news.state' => 1]);
  117. $query->andFilterWhere(['pfg_news.category' => $this->category]);
  118. if (!empty($get['name'])) {
  119. $query->andWhere(['like', 'pfg_news.subject', $get['name']]);
  120. }
  121. $query->leftJoin('pfg_category_city', 'pfg_news.city = pfg_category_city.id');
  122. $query->leftJoin('pfg_category_news', 'pfg_news.category = pfg_category_news.id');
  123. return $query->count();
  124. }
  125. public function getListWhere($query)
  126. {
  127. $query->select(['pfg_category_city.city_name', 'pfg_news.id', 'pfg_news.subject', 'pfg_news.state', 'pfg_news.clicks',
  128. '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']);
  129. $query->leftJoin('pfg_category_city', 'pfg_news.city = pfg_category_city.id');
  130. $query->leftJoin('pfg_category_news', 'pfg_news.category = pfg_category_news.id');
  131. $query->leftJoin('pfg_house', 'pfg_house.id = pfg_news.hid');
  132. $query->andWhere(['pfg_news.del' => $this->setDel]);
  133. $query->andFilterWhere(['like', 'pfg_news.subject', $this->subject]);
  134. $query->andFilterWhere(['pfg_news.state' => $this->state]);
  135. $query->andFilterWhere(['pfg_news.id' => $this->id]);
  136. $query->andFilterWhere(['pfg_news.category' => $this->category]);
  137. $query->andFilterWhere(['pfg_news.city' => $this->city]);
  138. $query->andFilterWhere(['pfg_news.author' => $this->author]);
  139. // $query->andFilterWhere(['pfg_news.hid'=>$this->hid]);
  140. return $query;
  141. }
  142. //排序点击量
  143. public function Clicks($limit)
  144. {
  145. $query = self::find();
  146. $query->select(['subject', 'id', 'clicks']);
  147. $query->andWhere(['del' => $this->setDel]);
  148. $query->andWhere(['state' => 1]);
  149. $query->andFilterWhere(['category' => $this->category]);
  150. $query->limit = $limit;
  151. return $query->orderBy(['pfg_news.clicks' => SORT_DESC])->asArray()->all();
  152. }
  153. //资讯类型
  154. // public fu
  155. public function Total()
  156. {
  157. $query = self::find();
  158. $query = $this->getListWhere($query);
  159. return $query->count();
  160. }
  161. public function HouseNewsList($page)
  162. {
  163. $query = self::find();
  164. $query->select(['subject', 'clicks', 'id', 'state']);
  165. $query = $this->WhereColumn($query);
  166. if (!empty($page['page'])) {
  167. $query->offset = ($page['page'] - 1) * $page['limit'];
  168. $query->limit = $page['limit'];
  169. }
  170. return $query->orderBy(['create_at' => SORT_DESC])->asArray()->all();
  171. }
  172. private function WhereColumn($query)
  173. {
  174. $query->andFilterWhere(['like', 'subject', $this->subject]);
  175. $query->andFilterWhere(['hid' => $this->hid]);
  176. $query->andFilterWhere(['category' => $this->category]);
  177. return $query;
  178. }
  179. public function HouseNewsTotal()
  180. {
  181. $query = self::find();
  182. $query = $this->WhereColumn($query);
  183. return $query->count();
  184. }
  185. //项目动态的资讯
  186. public function GetDynamic($limit)
  187. {
  188. $query = self::find();
  189. $query->andWhere(['pfg_news.del' => $this->setDel]);
  190. $query->andWhere(['pfg_news.state' => 1]);
  191. $query->andFilterWhere(['pfg_news.category' => 1]);
  192. $query->select(['pfg_news.id', 'pfg_news.subject', 'pfg_news.abstract', 'pfg_news.open_time']);
  193. $query->limit = $limit;
  194. $query->orderBy(['pfg_news.create_at' => SORT_DESC]);
  195. return $query->asArray()->all();
  196. }
  197. /**
  198. * 根据资讯栏目获取最新资讯
  199. * @param $category
  200. * @param $limit
  201. * @return array|\yii\db\ActiveRecord[]
  202. */
  203. public function getNews($category, $limit)
  204. {
  205. $query = self::find();
  206. $query->andWhere(['pfg_news.category' => $category]);
  207. $query->andWhere(['pfg_news.state' => 1]);
  208. $query->andWhere(['pfg_news.del' => 1]);
  209. $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']);
  210. $query->leftJoin('pfg_category_news', 'pfg_news.category = pfg_category_news.id');
  211. $query->leftJoin('pfg_category_city', 'pfg_news.city = pfg_category_city.id');
  212. $query->orderBy(['pfg_news.create_at' => SORT_DESC]);
  213. $query->limit = $limit;
  214. return $query->asArray()->all();
  215. }
  216. /*
  217. * 获取楼盘相关资讯
  218. * */
  219. public function AllHouseNews()
  220. {
  221. $query = self::find();
  222. $query->select(['pfg_news.*', 'pfg_category_news.news_name']);
  223. $query->andWhere(['pfg_news.hid' => $this->hid]);
  224. $query->andWhere(['pfg_news.del' => 1]);
  225. $query->andWhere(['pfg_news.state' => 1]);
  226. if (!empty($cid)) {
  227. $query->andWhere(['pfg_category_news.id' => $cid]);
  228. }
  229. $query->innerJoin('pfg_category_news', 'pfg_category_news.id = pfg_news.category');
  230. return $query->orderBy(['pfg_news.create_at' => SORT_DESC])->asArray()->all();
  231. }
  232. /**
  233. * 获取楼盘关联资讯,HouseNews表以及News表一同获取
  234. * */
  235. public function getNewsListHouse($nids,$limit = null)
  236. {
  237. $select = [
  238. 'pfg_news.*',
  239. 'pfg_category_news.news_name'
  240. ];
  241. $query = self::find();
  242. $query->select($select);
  243. $query->where(['pfg_news.del'=>1]);
  244. $query->where(['pfg_news.state'=>1]);
  245. $query->andFilterWhere(['pfg_news.hid'=>$this->hid]);
  246. if(!empty($nids)){
  247. $query->orFilterWhere(['pfg_news.id'=>$nids]);
  248. }
  249. if(!empty($limit)){
  250. $query->limit($limit);
  251. }
  252. $query->leftJoin('pfg_category_news','pfg_news.category = pfg_category_news.id');
  253. return $query->orderBy(['pfg_news.create_at'=>SORT_DESC])->asArray()->all();
  254. }
  255. //2020-11-18
  256. public function getNewsByCategory($category,$limit = null){
  257. $query = self::find();
  258. $query->select("*");
  259. $query->where(['category'=>$category,'state'=>1,'del'=>1]);
  260. if(!empty($limit)){
  261. $query->limit($limit);
  262. }
  263. return $query->orderBy(['create_at'=>SORT_DESC])->asArray()->all();
  264. }
  265. public function getHouseNewByHid($hid, $limit = null)
  266. {
  267. $query = self::find();
  268. $query->select(['subject','id']);
  269. $query->where(['del'=>1,'state'=>1,'hid'=>$hid]);
  270. if(!empty($limit)){
  271. $query->limit($limit);
  272. }
  273. return $query->orderBy(['create_at'=>SORT_DESC])->asArray()->all();
  274. }
  275. /**
  276. * 传入楼盘名称,查找标题中含有楼盘名称的资讯
  277. * @param $name 楼盘名称
  278. * @param $limit 条数
  279. * @return array|\yii\db\ActiveRecord[]
  280. */
  281. public function getHouseNews($name, $limit = null)
  282. {
  283. $query = self::find();
  284. $query->andWhere(['like', 'subject', $name]);
  285. $query->andWhere(['state' => 1]);
  286. $query->andWhere(['del' => 1]);
  287. $query->select(['id', 'open_time', 'thumb', 'subject', 'abstract', 'true_click', 'category', 'create_at']);
  288. if (!empty($limit)) {
  289. $query->limit = $limit;
  290. }
  291. return $query->orderBy(['create_at' => SORT_DESC])->asArray()->all();
  292. }
  293. }