ArticleService.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace addons\TinyBlog\services;
  3. use Yii;
  4. use common\enums\StatusEnum;
  5. use addons\TinyBlog\common\models\Article;
  6. use addons\TinyBlog\common\enums\ArticlePositionEnum;
  7. /**
  8. * Class ArticleService
  9. * @package addons\TinyBlog\services
  10. * @author jianyan74 <751393839@qq.com>
  11. */
  12. class ArticleService
  13. {
  14. /**
  15. * 上一篇
  16. *
  17. * @param $id
  18. * @return array|\yii\db\ActiveRecord|null
  19. */
  20. public function getPrev($id)
  21. {
  22. return Article::find()
  23. ->select(['id', 'merchant_id', 'cate_id', 'title', 'description', 'cover', 'author', 'view', 'created_at'])
  24. ->where(['<', 'id', $id])
  25. ->andWhere(['status' => StatusEnum::ENABLED])
  26. ->andWhere(['merchant_id' => Yii::$app->services->merchant->getId()])
  27. ->select(['id', 'title'])
  28. ->orderBy('id desc')
  29. ->one();
  30. }
  31. /**
  32. * 下一篇
  33. *
  34. * @param $id
  35. * @return array|\yii\db\ActiveRecord|null
  36. */
  37. public function getNext($id)
  38. {
  39. return Article::find()
  40. ->select(['id', 'merchant_id', 'cate_id', 'title', 'description', 'cover', 'author', 'view', 'created_at'])
  41. ->where(['>', 'id', $id])
  42. ->andWhere(['status' => StatusEnum::ENABLED])
  43. ->andWhere(['merchant_id' => Yii::$app->services->merchant->getId()])
  44. ->select(['id', 'title'])
  45. ->orderBy('id asc')
  46. ->one();
  47. }
  48. /**
  49. * @return array|\yii\db\ActiveRecord[]
  50. */
  51. public function hot($limit = 6)
  52. {
  53. return Article::find()
  54. ->select(['id', 'merchant_id', 'cate_id', 'title', 'description', 'cover', 'author', 'view', 'created_at'])
  55. ->where(['status' => StatusEnum::ENABLED])
  56. ->andWhere(ArticlePositionEnum::position(ArticlePositionEnum::HOT))// 推荐位 位运算查询
  57. ->orderBy('created_at desc')
  58. ->asArray()
  59. ->limit($limit)
  60. ->all();
  61. }
  62. /**
  63. * @return array|\yii\db\ActiveRecord[]
  64. */
  65. public function newest($limit = 6)
  66. {
  67. return Article::find()
  68. ->select(['id', 'merchant_id', 'cate_id', 'title', 'description', 'cover', 'author', 'view', 'created_at'])
  69. ->where(['status' => StatusEnum::ENABLED])
  70. ->orderBy('created_at desc')
  71. ->asArray()
  72. ->limit($limit)
  73. ->all();
  74. }
  75. /**
  76. * @return array|\yii\db\ActiveRecord[]
  77. */
  78. public function recommend($limit = 8)
  79. {
  80. return Article::find()
  81. ->select(['id', 'merchant_id', 'cate_id', 'title', 'description', 'cover', 'author', 'view', 'created_at'])
  82. ->where(['status' => StatusEnum::ENABLED])
  83. ->orderBy('rand()')
  84. ->asArray()
  85. ->limit($limit)
  86. ->all();
  87. }
  88. }
粤ICP备19079148号