ArticleCategoryDao.php 2.9 KB

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