ArticleCategoryDao.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. * @param int $mer_id
  39. * @return array
  40. * @author xaboy
  41. * @day 2020-04-20
  42. */
  43. public function getAllOptions($mer_id = 0)
  44. {
  45. return ArticleCategory::getDB()->where('mer_id', $mer_id)->order('sort DESC')->column('pid,title', $this->getPk());
  46. }
  47. /**
  48. * @param int $mer_id
  49. * @return Collection
  50. * @throws DataNotFoundException
  51. * @throws DbException
  52. * @throws ModelNotFoundException
  53. * @author xaboy
  54. * @day 2020-04-20
  55. */
  56. public function getAll($mer_id = 0,$status = null)
  57. {
  58. return ArticleCategory::getDB()->where('mer_id', $mer_id)->when($status,function($query)use($status){
  59. $query->where('status',$status);
  60. })->order('sort DESC')->select();
  61. }
  62. /**
  63. * @param array $where
  64. * @return \think\db\BaseQuery
  65. * @author xaboy
  66. * @day 2020/9/18
  67. */
  68. public function search(array $where)
  69. {
  70. return ArticleCategory::getDB()->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  71. $query->where('status', $where['status']);
  72. })->when(isset($where['pid']) && $where['pid'] !== '', function ($query) use ($where) {
  73. $query->where('pid', $where['pid']);
  74. })->order('sort DESC, article_category_id DESC');
  75. }
  76. /**
  77. * @param int $merId
  78. * @param $field
  79. * @param $value
  80. * @param null $except
  81. * @return bool
  82. * @author xaboy
  83. * @day 2020-04-20
  84. */
  85. public function merFieldExists(int $merId, $field, $value, $except = null)
  86. {
  87. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  88. $query->where($field, '<>', $except);
  89. })->where('mer_id', $merId)->where($field, $value)->count() > 0;
  90. }
  91. /**
  92. * @param int $merId
  93. * @param int $id
  94. * @param null $except
  95. * @return bool
  96. * @author xaboy
  97. * @day 2020-04-20
  98. */
  99. public function merExists(int $merId, int $id, $except = null)
  100. {
  101. return $this->merFieldExists($merId, $this->getPk(), $id, $except);
  102. }
  103. /**
  104. * @param int $id
  105. * @param int $merId
  106. * @return array|Model|null
  107. * @throws DataNotFoundException
  108. * @throws DbException
  109. * @throws ModelNotFoundException
  110. * @author xaboy
  111. * @day 2020-04-15
  112. */
  113. public function get( $id, $merId = 0)
  114. {
  115. return ($this->getModel())::getDB()->where('mer_id', 0)->find($id);
  116. }
  117. /**
  118. * @param int $id
  119. * @param int $merId
  120. * @return int
  121. * @throws DbException
  122. * @author xaboy
  123. * @day 2020-04-20
  124. */
  125. public function delete(int $id, $merId = 0)
  126. {
  127. return ($this->getModel())::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->delete();
  128. }
  129. }