ArticleCategoryDao.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\common\dao\article;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\article\ArticleCategory;
  5. use app\common\model\BaseModel;
  6. use think\Collection;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\Model;
  11. /**
  12. * Class ArticleCategoryDao
  13. * @package app\common\dao\article
  14. * @author zfy
  15. * @day 2020-04-20
  16. */
  17. class ArticleCategoryDao extends BaseDao
  18. {
  19. /**
  20. * @return BaseModel
  21. * @author zfy
  22. * @day 2020-03-30
  23. */
  24. protected function getModel(): string
  25. {
  26. return ArticleCategory::class;
  27. }
  28. /**
  29. * @param int $mer_id
  30. * @return array
  31. * @author zfy
  32. * @day 2020-04-20
  33. */
  34. public function getAllOptions($mer_id = 0)
  35. {
  36. return ArticleCategory::getDB()->where('mer_id', $mer_id)->order('sort DESC')->column('pid,title', $this->getPk());
  37. }
  38. /**
  39. * @param int $mer_id
  40. * @return Collection
  41. * @throws DataNotFoundException
  42. * @throws DbException
  43. * @throws ModelNotFoundException
  44. * @author zfy
  45. * @day 2020-04-20
  46. */
  47. public function getAll($mer_id = 0,$status = null)
  48. {
  49. return ArticleCategory::getDB()->where('mer_id', $mer_id)->when($status,function($query)use($status){
  50. $query->where('status',$status);
  51. })->order('sort DESC')->select();
  52. }
  53. /**
  54. * @param array $where
  55. * @return \think\db\BaseQuery
  56. * @author zfy
  57. * @day 2020/9/18
  58. */
  59. public function search(array $where)
  60. {
  61. return ArticleCategory::getDB()->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  62. $query->where('status', $where['status']);
  63. })->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
  64. $query->where('pid', $where['pid']);
  65. })->order('sort DESC, article_category_id DESC');
  66. }
  67. /**
  68. * @param int $merId
  69. * @param $field
  70. * @param $value
  71. * @param null $except
  72. * @return bool
  73. * @author zfy
  74. * @day 2020-04-20
  75. */
  76. public function merFieldExists(int $merId, $field, $value, $except = null)
  77. {
  78. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  79. $query->where($field, '<>', $except);
  80. })->where('mer_id', $merId)->where($field, $value)->count() > 0;
  81. }
  82. /**
  83. * @param int $merId
  84. * @param int $id
  85. * @param null $except
  86. * @return bool
  87. * @author zfy
  88. * @day 2020-04-20
  89. */
  90. public function merExists(int $merId, int $id, $except = null)
  91. {
  92. return $this->merFieldExists($merId, $this->getPk(), $id, $except);
  93. }
  94. /**
  95. * @param int $id
  96. * @param int $merId
  97. * @return array|Model|null
  98. * @throws DataNotFoundException
  99. * @throws DbException
  100. * @throws ModelNotFoundException
  101. * @author zfy
  102. * @day 2020-04-15
  103. */
  104. public function get( $id, $merId = 0)
  105. {
  106. return ($this->getModel())::getDB()->where('mer_id', 0)->find($id);
  107. }
  108. /**
  109. * @param int $id
  110. * @param int $merId
  111. * @return int
  112. * @throws DbException
  113. * @author zfy
  114. * @day 2020-04-20
  115. */
  116. public function delete(int $id, $merId = 0)
  117. {
  118. return ($this->getModel())::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->delete();
  119. }
  120. }