CommentService.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2020-01-23 11:40
  7. */
  8. namespace common\services;
  9. use backend\models\search\CommentSearch;
  10. use common\models\Comment;
  11. class CommentService extends Service implements CommentServiceInterface
  12. {
  13. public function getSearchModel(array $options=[])
  14. {
  15. return new CommentSearch();
  16. }
  17. public function getModel($id, array $options = [])
  18. {
  19. return Comment::findOne($id);
  20. }
  21. public function newModel(array $options = [])
  22. {
  23. return new Comment();
  24. }
  25. /**
  26. * @param int $limit
  27. * @return array|\yii\db\ActiveRecord[]
  28. */
  29. public function getRecentComments($limit = 10)
  30. {
  31. return $this->newModel()->find()->orderBy('created_at desc')->with('article')->limit($limit)->all();
  32. }
  33. public function getCommentCountByPeriod($startAt=null, $endAt=null)
  34. {
  35. $model = Comment::find();
  36. if( $startAt != null && $endAt != null ){
  37. $model->andWhere(["between", "created_at", $startAt, $endAt]);
  38. }else if ($startAt != null){
  39. $model->andwhere([">", "created_at", $startAt]);
  40. } else if($endAt != null){
  41. $model->andWhere(["<", "created_at", $endAt]);
  42. }
  43. return $model->count('id');
  44. }
  45. }