ArticleCategoryDao.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\article;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\article\ArticleCategory;
  14. use app\common\model\BaseModel;
  15. use think\Collection;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\Model;
  20. /**
  21. * Class ArticleCategoryDao
  22. * @package app\common\dao\article
  23. * @author xaboy
  24. * @day 2020-04-20
  25. */
  26. class ArticleCategoryDao extends BaseDao
  27. {
  28. /**
  29. * @return BaseModel
  30. * @author xaboy
  31. * @day 2020-03-30
  32. */
  33. protected function getModel(): string
  34. {
  35. return ArticleCategory::class;
  36. }
  37. /**
  38. * 获取文章分类集合
  39. * @param int $mer_id
  40. * @return array
  41. * @author xaboy
  42. * @day 2020-04-20
  43. */
  44. public function getAllOptions($mer_id = 0)
  45. {
  46. return ArticleCategory::getDB()->where('mer_id', $mer_id)->order('sort DESC')->column('pid,title', $this->getPk());
  47. }
  48. /**
  49. * 获取文章分类
  50. * @param int $mer_id
  51. * @return Collection
  52. * @throws DataNotFoundException
  53. * @throws DbException
  54. * @throws ModelNotFoundException
  55. * @author xaboy
  56. * @day 2020-04-20
  57. */
  58. public function getAll($mer_id = 0,$status = null)
  59. {
  60. return ArticleCategory::getDB()->where('mer_id', $mer_id)->when($status,function($query)use($status){
  61. $query->where('status',$status);
  62. })->order('sort DESC')->select();
  63. }
  64. /**
  65. * 查询文章分类
  66. * @param array $where
  67. * @return \think\db\BaseQuery
  68. * @author xaboy
  69. * @day 2020/9/18
  70. */
  71. public function search(array $where)
  72. {
  73. return ArticleCategory::getDB()->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  74. $query->where('status', $where['status']);
  75. })->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
  76. $query->where('pid', $where['pid']);
  77. })->order('sort DESC, article_category_id DESC');
  78. }
  79. /**
  80. * 查询指定字段的数据是否存在
  81. * @param int $merId
  82. * @param $field
  83. * @param $value
  84. * @param null $except
  85. * @return bool
  86. * @author xaboy
  87. * @day 2020-04-20
  88. */
  89. public function merFieldExists(int $merId, $field, $value, $except = null)
  90. {
  91. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  92. $query->where($field, '<>', $except);
  93. })->where('mer_id', $merId)->where($field, $value)->count() > 0;
  94. }
  95. /**
  96. * 查询主键是否存在
  97. * @param int $merId
  98. * @param int $id
  99. * @param null $except
  100. * @return bool
  101. * @author xaboy
  102. * @day 2020-04-20
  103. */
  104. public function merExists(int $merId, int $id, $except = null)
  105. {
  106. return $this->merFieldExists($merId, $this->getPk(), $id, $except);
  107. }
  108. /**
  109. * 文章分类详情
  110. * @param int $id
  111. * @param int $merId
  112. * @return array|Model|null
  113. * @throws DataNotFoundException
  114. * @throws DbException
  115. * @throws ModelNotFoundException
  116. * @author xaboy
  117. * @day 2020-04-15
  118. */
  119. public function get( $id, $merId = 0)
  120. {
  121. return ($this->getModel())::getDB()->where('mer_id', 0)->find($id);
  122. }
  123. /**
  124. * 删除文章分类
  125. * @param int $id
  126. * @param int $merId
  127. * @return int
  128. * @throws DbException
  129. * @author xaboy
  130. * @day 2020-04-20
  131. */
  132. public function delete(int $id, $merId = 0)
  133. {
  134. return ($this->getModel())::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->delete();
  135. }
  136. }