ArticleDao.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\dao\article;
  12. use app\dao\BaseDao;
  13. use app\model\article\Article;
  14. use think\exception\ValidateException;
  15. /**
  16. * 文章dao
  17. * Class ArticleDao
  18. * @package app\dao\article
  19. */
  20. class ArticleDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return Article::class;
  29. }
  30. /**
  31. * 文章搜索
  32. * @param array $where
  33. * @param bool $search
  34. * @return \crmeb\basic\BaseModel
  35. * @throws \ReflectionException
  36. * @author 吴汐
  37. * @email 442384644@qq.com
  38. * @date 2023/03/20
  39. */
  40. public function search(array $where = [], bool $search = false)
  41. {
  42. return parent::search($where, $search)->when(isset($where['ids']) && count($where['ids']), function ($query) use ($where) {
  43. $query->whereNotIn('id', $where['ids']);
  44. });
  45. }
  46. /**
  47. * 获取文章列表
  48. * @param array $where
  49. * @param int $page
  50. * @param int $limit
  51. * @return mixed
  52. * @throws \ReflectionException
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function getList(array $where, int $page, int $limit)
  58. {
  59. return $this->search($where)->with(['content', 'storeInfo', 'cateName'])->page($page, $limit)->order('sort desc,id desc')->select()->toArray();
  60. }
  61. /**
  62. * 获取一条数据
  63. * @param $id
  64. * @return mixed
  65. * @throws \ReflectionException
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function read($id)
  71. {
  72. $data = $this->search()->with(['content', 'storeInfo', 'cateName'])->find($id);
  73. if (!$data) throw new ValidateException('文章不存在');
  74. $data['store_info'] = $data['storeInfo'];
  75. return $data;
  76. }
  77. /**
  78. * 新闻分类下的文章
  79. * @param $new_id
  80. * @return mixed
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public function articleLists($new_id)
  86. {
  87. return $this->getModel()->where('hide', 0)->where('id', 'in', $new_id)->select();
  88. }
  89. /**
  90. * 图文详情
  91. * @param $new_id
  92. * @return mixed
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public function articleContentList($new_id)
  98. {
  99. return $this->getModel()->where('hide', 0)->where('id', 'in', $new_id)->with('content')->select();
  100. }
  101. }