StoreBrandDao.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\product\brand;
  12. use app\dao\BaseDao;
  13. use app\model\product\brand\StoreBrand;
  14. /**
  15. * Class StoreBrandDao
  16. * @package app\dao\product\product
  17. */
  18. class StoreBrandDao extends BaseDao
  19. {
  20. /**
  21. * 设置模型
  22. * @return string
  23. */
  24. protected function setModel(): string
  25. {
  26. return StoreBrand::class;
  27. }
  28. /**
  29. * 获取品牌列表
  30. * @param array $where
  31. * @param array $with
  32. * @param array $field
  33. * @param int $page
  34. * @param int $limit
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getList(array $where, array $with = [], array $field = ['*'], int $page = 0, int $limit = 0)
  41. {
  42. return $this->search($where)->field($field)
  43. ->when(in_array('product', $with), function ($query) use ($with) {
  44. $with = array_merge(array_diff($with, ['product']), ['product' => function ($que) {
  45. $que->field(['pid', 'brand_id', "count(`brand_id`) as brand_num"])->where('pid', 0)->where('is_del', 0)->group('brand_id');
  46. }]);
  47. $query->with($with);
  48. })->when($page && $limit, function ($query) use($page, $limit) {
  49. $query->page($page, $limit);
  50. })->order('sort desc,id desc')->select()->toArray();
  51. }
  52. /**
  53. * @param int $id
  54. * @param string $field
  55. * @return array
  56. * @throws \think\db\exception\DataNotFoundException
  57. * @throws \think\db\exception\DbException
  58. * @throws \think\db\exception\ModelNotFoundException
  59. */
  60. public function getAllById(int $id, string $field = 'id')
  61. {
  62. return $this->getModel()->where(function ($query) use ($id) {
  63. $query->where(function ($q) use ($id) {
  64. $q->where('id', $id)->whereOr('pid', $id);
  65. });
  66. })->where('is_show', 1)->field($field)->select()->toArray();
  67. }
  68. }