StoreBrandDao.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\store;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\StoreBrand as model;
  14. use crmeb\traits\CategoresDao;
  15. class StoreBrandDao extends BaseDao
  16. {
  17. use CategoresDao;
  18. protected function getModel(): string
  19. {
  20. return model::class;
  21. }
  22. /**
  23. * 获取所有品牌信息,包括品牌分类和品牌详情。
  24. * 此方法专门设计用于获取品牌列表,列表中包括一个特殊的项代表“其他”品牌。
  25. *
  26. * @return array 返回一个包含所有品牌信息的数组,其中最后一个元素是特殊的“其他”品牌项。
  27. */
  28. public function getAll()
  29. {
  30. // 根据品牌分类中的"is_show"字段为1来筛选品牌分类
  31. $query = $this->getModel()::hasWhere('brandCategory',function($query){
  32. $query->where('is_show',1);
  33. });
  34. // 筛选品牌表中"is_show"字段为1的品牌
  35. $query->where('StoreBrand.is_show',1);
  36. // 按品牌的排序和创建时间降序排列,并获取所有符合条件的品牌数据
  37. $list = $query->order('StoreBrand.sort DESC,StoreBrand.create_time DESC')->select()->toArray();
  38. // 向品牌列表中添加一个特殊的“其他”品牌项
  39. array_push($list,[
  40. "brand_id" => 0,
  41. "brand_category_id" => 0,
  42. "brand_name" => "其他",
  43. "sort" => 999,
  44. "pic" => "",
  45. "is_show" => 1,
  46. "create_time" => "",
  47. ]);
  48. // 返回处理后的品牌列表
  49. return $list;
  50. }
  51. /**
  52. * 检查指定字段是否存在特定值。
  53. *
  54. * 该方法用于查询数据库中指定字段的值是否已存在。它支持排除特定值的查询,
  55. * 这使得可以在检查存在性时忽略特定的记录。
  56. *
  57. * @param string $field 要查询的字段名。
  58. * @param mixed $value 要查询的字段值。
  59. * @param mixed $except 可选参数,用于指定需要排除的值。
  60. * @return bool 如果找到匹配的记录,则返回true;否则返回false。
  61. */
  62. public function merFieldExists($field, $value, $except = null)
  63. {
  64. // 从模型中获取数据库实例。
  65. return ($this->getModel())::getDB()
  66. // 当$except有值时,添加一个不等于($field, '<>')的查询条件。
  67. ->when($except, function ($query, $except) use ($field) {
  68. $query->where($field, '<>', $except);
  69. })
  70. // 添加等于($field, $value)的查询条件。
  71. ->where($field, $value)
  72. // 统计符合条件的记录数,如果大于0,则表示存在。
  73. ->count() > 0;
  74. }
  75. /**
  76. * 根据条件搜索品牌信息
  77. *
  78. * 本函数用于根据传入的条件数组搜索品牌的数据库记录。条件包括品牌分类ID、品牌名称和品牌ID列表。
  79. * 查询结果将按照排序和创建时间倒序返回。
  80. *
  81. * @param array $where 搜索条件数组,包含品牌分类ID、品牌名称和品牌ID列表等条件。
  82. * @return \think\db\Query 查询结果的查询对象,可用于进一步的查询操作或获取结果。
  83. */
  84. public function search(array $where)
  85. {
  86. // 获取数据库查询对象
  87. $query = $this->getModel()::getDB();
  88. // 如果条件数组中包含品牌分类ID,并且该ID不为空,则添加到查询条件中
  89. if(isset($where['brand_category_id']) && $where['brand_category_id'])
  90. $query->where('brand_category_id',$where['brand_category_id']);
  91. // 如果条件数组中包含品牌名称,并且该名称不为空,则添加到查询条件中,使用LIKE进行模糊匹配
  92. if(isset($where['brand_name']) && $where['brand_name'])
  93. $query->where('brand_name','like','%'.$where['brand_name'].'%');
  94. // 如果条件数组中包含品牌ID列表,并且该列表不为空,则添加到查询条件中,查询包含在列表中的品牌
  95. if((isset($where['ids']) && $where['ids']))
  96. $query->where($this->getPk(),'in',$where['ids']);
  97. // 对查询结果按照排序和创建时间进行倒序排序
  98. return $query->order('sort DESC,create_time desc');
  99. }
  100. }