CategoryDao.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\dao\other;
  12. use app\dao\BaseDao;
  13. use app\model\other\Category;
  14. /**
  15. * 分类
  16. * Class CategoryDao
  17. * @package app\dao\other
  18. */
  19. class CategoryDao extends BaseDao
  20. {
  21. /**
  22. * 设置模型
  23. * @return string
  24. */
  25. protected function setModel(): string
  26. {
  27. return Category::class;
  28. }
  29. public function search(array $where = [])
  30. {
  31. return parent::search($where)->when(isset($where['name']) && $where['name'], function ($query) use ($where) {
  32. $query->where(function ($q) use ($where) {
  33. $q->whereOr('id|name', 'like', '%' . $where['name'] . '%')
  34. ->when(!empty($where['product_label']), function ($query) use ($where) {
  35. $query->whereOr('id', 'IN', function ($l) use ($where) {
  36. $l->name('store_product_label')->whereLike('id|label_name', '%' . $where['name'] . '%')->field('label_cate');
  37. });
  38. });
  39. });
  40. })->when(!empty($where['nowName']), function ($query) use ($where) {
  41. $query->where('name', $where['nowName']);
  42. });
  43. }
  44. /**
  45. * 获取分类
  46. * @param array $where
  47. * @param int $page
  48. * @param int $limit
  49. * @param array|string[] $field
  50. * @param array $with
  51. * @return array
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\DbException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. */
  56. public function getCateList(array $where, int $page = 0, int $limit = 0, array $field = ['*'], array $with = [])
  57. {
  58. $other = false;
  59. if (isset($where['other'])) {
  60. $other = true;
  61. unset($where['other']);
  62. }
  63. return $this->search($where)->field($field)->when(count($with), function ($query) use ($with) {
  64. $query->with($with);
  65. })->when($page && $limit, function ($query) use ($page, $limit) {
  66. $query->page($page, $limit);
  67. })->when($other, function ($query) {
  68. $query->where('other', '<>', '');
  69. })->order('sort DESC,id DESC')->select()->toArray();
  70. }
  71. /**
  72. * 获取全部标签分类
  73. * @return array
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public function getAll(array $where = [], array $with = [])
  79. {
  80. return $this->search($where)->when(count($with), function ($query) use ($with) {
  81. $query->with($with);
  82. })->order('sort DESC,id DESC')->select()->toArray();
  83. }
  84. /**
  85. * 添加修改选择上级分类列表
  86. * @param array $where
  87. * @param string $field
  88. * @return array
  89. */
  90. public function getMenus(array $where, string $field = 'id,pid,name')
  91. {
  92. return $this->search($where)->order('sort desc,id desc')->column($field);
  93. }
  94. }