SystemConfigClassifyDao.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\system\config;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\system\config\SystemConfigClassify;
  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 SystemConfigClassifyDao
  22. * @package app\common\dao\system\config
  23. * @author xaboy
  24. * @day 2020-03-27
  25. */
  26. class SystemConfigClassifyDao extends BaseDao
  27. {
  28. /**
  29. * @return BaseModel
  30. * @author xaboy
  31. * @day 2020-03-30
  32. */
  33. protected function getModel(): string
  34. {
  35. return SystemConfigClassify::class;
  36. }
  37. /**
  38. * @return array
  39. * @author xaboy
  40. * @day 2020-03-27
  41. */
  42. public function getOptions()
  43. {
  44. return SystemConfigClassify::column('pid,classify_name', 'config_classify_id');
  45. }
  46. /**
  47. * @return array
  48. * @author xaboy
  49. * @day 2020-04-22
  50. */
  51. public function getTopOptions()
  52. {
  53. return SystemConfigClassify::getDB()->where('pid', 0)->column('classify_name', 'config_classify_id');
  54. }
  55. public function search(array $where)
  56. {
  57. return SystemConfigClassify::getDB()->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  58. $query->where('status', $where['status']);
  59. })->when(isset($where['classify_name']) && $where['classify_name'] !== '', function ($query) use ($where) {
  60. $query->where('classify_name', 'LIKE', "%{$where['classify_name']}%");
  61. });
  62. }
  63. /**
  64. * @return Collection
  65. * @throws DataNotFoundException
  66. * @throws DbException
  67. * @throws ModelNotFoundException
  68. * @author xaboy
  69. * @day 2020-03-31
  70. */
  71. public function all()
  72. {
  73. return SystemConfigClassify::getDB()->select();
  74. }
  75. /**
  76. * @return int
  77. * @author xaboy
  78. * @day 2020-03-31
  79. */
  80. public function count()
  81. {
  82. return SystemConfigClassify::getDB()->count();
  83. }
  84. /**
  85. * @param int $pid
  86. * @param string $field
  87. * @return Collection
  88. * @throws DataNotFoundException
  89. * @throws DbException
  90. * @throws ModelNotFoundException
  91. * @author xaboy
  92. * @day 2020-03-27
  93. */
  94. public function children(int $pid, string $field = 'config_classify_id,classify_name')
  95. {
  96. return SystemConfigClassify::getDB()->where('pid', $pid)->field($field)->select();
  97. }
  98. /**
  99. * @param int $id
  100. * @return bool
  101. * @author xaboy
  102. * @day 2020-03-27
  103. */
  104. public function existsChild(int $id): bool
  105. {
  106. return $this->fieldExists('pid', $id);
  107. }
  108. /**
  109. * @param $key
  110. * @param int|null $except
  111. * @return bool
  112. * @author xaboy
  113. * @day 2020-03-27
  114. */
  115. public function keyExists($key, ?int $except = null): bool
  116. {
  117. return $this->fieldExists('classify_key', $key, $except);
  118. }
  119. /**
  120. * @param int $pid
  121. * @param int|null $except
  122. * @return bool
  123. * @author xaboy
  124. * @day 2020-03-27
  125. */
  126. public function pidExists(int $pid, ?int $except = null): bool
  127. {
  128. return $this->fieldExists('config_classify_id', $pid, $except);
  129. }
  130. /**
  131. * @param string $key
  132. * @return mixed
  133. * @author xaboy
  134. * @day 2020-04-22
  135. */
  136. public function keyById(string $key)
  137. {
  138. return SystemConfigClassify::getDB()->where('classify_key', $key)->value('config_classify_id');
  139. }
  140. /**
  141. * @param string $key
  142. * @return array|Model|null
  143. * @throws DataNotFoundException
  144. * @throws DbException
  145. * @throws ModelNotFoundException
  146. * @author xaboy
  147. * @day 2020-04-22
  148. */
  149. public function keyByData(string $key)
  150. {
  151. return SystemConfigClassify::getDB()->where('classify_key', $key)->find();
  152. }
  153. public function getOption()
  154. {
  155. return SystemConfigClassify::column('classify_name,pid,config_classify_id');
  156. }
  157. }