UserLabelDao.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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\user;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\user\UserLabel;
  15. use think\db\BaseQuery;
  16. /**
  17. * Class UserLabelDao
  18. * @package app\common\dao\user
  19. * @author xaboy
  20. * @day 2020-05-07
  21. */
  22. class UserLabelDao extends BaseDao
  23. {
  24. /**
  25. * @return BaseModel
  26. * @author xaboy
  27. * @day 2020-03-30
  28. */
  29. protected function getModel(): string
  30. {
  31. return UserLabel::class;
  32. }
  33. /**
  34. * 根据条件搜索用户标签。
  35. *
  36. * 该方法提供了搜索用户标签的灵活性,根据传入的条件决定搜索的范围和条件。
  37. * 主要处理两种情况:1) 是否搜索所有标签;2) 是否指定商家ID进行搜索。
  38. *
  39. * @param array $where 搜索条件数组,可能包含 'all' 和 'mer_id' 键。
  40. * 'all' 表示是否搜索所有标签,'mer_id' 表示商家ID。
  41. * @return \think\db\Query 用户标签查询结果。
  42. */
  43. public function search(array $where = [])
  44. {
  45. // 获取用户标签的数据库查询对象
  46. $db = UserLabel::getDB();
  47. // 如果没有指定搜索所有标签或指定的值为空,则添加类型为0的条件
  48. $db->when(!isset($where['all']) || $where['all'] === '', function ($query) use ($where) {
  49. // 当 'all' 不设置或为空时,查询类型为0的标签
  50. $query->where('type', 0);
  51. });
  52. // 如果指定了商家ID且不为空,则添加商家ID的条件;否则,添加商家ID为0的条件
  53. $db->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  54. // 当 'mer_id' 设定且不为空时,查询指定商家ID的标签
  55. $query->where('mer_id', $where['mer_id']);
  56. }, function ($query) {
  57. // 当 'mer_id' 未设定或为空时,查询商家ID为0的标签
  58. $query->where('mer_id', 0);
  59. });
  60. // 返回处理后的查询对象
  61. return $db;
  62. }
  63. /**
  64. * 获取所有标签选项
  65. *
  66. * 本函数用于查询并返回所有类型为0的用户标签的名称和ID。这些标签属于特定的商家(mer_id)。
  67. * 主要用于在前端展示或供其他功能使用这些标签选项。
  68. *
  69. * @param int $merId 商家ID,默认为0,表示查询所有商家的标签。
  70. * @return array 返回一个数组,数组的键为标签ID,值为标签名称。
  71. */
  72. public function allOptions($merId = 0)
  73. {
  74. // 使用UserLabel模型的getDB方法获取数据库操作对象,并设置查询条件为商家ID和类型为0,然后返回标签名称和ID的列。
  75. return UserLabel::getDB()->where('mer_id', $merId)->where('type', 0)->column('label_name', 'label_id');
  76. }
  77. /**
  78. * 根据标签ID数组和商家ID获取标签名称列表
  79. *
  80. * 本函数通过查询用户标签数据库表,筛选出指定商家ID和标签ID数组内的标签名称。
  81. * 主要用于在系统中根据用户或商品等的标签ID,获取对应的标签名称,以便于展示或进一步处理。
  82. *
  83. * @param array $ids 标签ID数组,指定需要查询的标签ID列表
  84. * @param int $merId 商家ID,用于指定查询哪个商家的标签,默认为0,表示查询所有商家的标签
  85. * @return array 返回一个包含标签名称的数组,每个标签名称对应输入的标签ID
  86. */
  87. public function labels(array $ids, $merId = 0)
  88. {
  89. // 通过UserLabel类的静态方法getDB获取数据库操作对象,并构造查询条件,筛选出指定商家ID和标签ID内的标签名称
  90. return UserLabel::getDB()->where('mer_id', $merId)->whereIn('label_id', $ids)->column('label_name');
  91. }
  92. /**
  93. * 检查特定标签ID是否存在
  94. *
  95. * 此方法用于确定数据库中是否存在具有指定标签ID、类型为0且可选的商户ID的记录。
  96. * 主要用于标签管理中对标签存在性的验证,以避免重复创建或操作已存在的标签。
  97. *
  98. * @param int $id 标签的ID,用于唯一标识一个标签。
  99. * @param int $mer_id 商户的ID,可选参数,用于指定特定商户下的标签。默认为0,表示系统标签。
  100. * @return bool 返回布尔值,表示标签是否存在。存在返回true,不存在返回false。
  101. */
  102. public function exists(int $id, $mer_id = 0)
  103. {
  104. // 使用existsWhere方法查询是否存在满足条件的标签记录。
  105. return $this->existsWhere(['label_id' => $id, 'type' => 0, 'mer_id' => $mer_id]);
  106. }
  107. /**
  108. * 检查指定标签名称是否存在
  109. *
  110. * 该方法用于查询指定条件下的标签名称是否存在,主要考虑了商家ID、标签类型和排除特定ID的情况。
  111. *
  112. * @param string $name 标签名称
  113. * @param int $mer_id 商家ID,用于限定查询范围
  114. * @param int $type 标签类型,用于进一步筛选标签
  115. * @param int|null $except 排除的标签ID,用于确保查询结果不包括特定标签
  116. * @return bool 如果存在符合条件的标签名称则返回true,否则返回false
  117. */
  118. public function existsName($name, $mer_id = 0, $type = 0, $except = null)
  119. {
  120. // 根据参数条件查询标签数据库记录,包括标签名称、商家ID和标签类型
  121. return UserLabel::where('label_name', $name)->where('mer_id', $mer_id)->where('type', $type)
  122. // 当$except不为空时,添加额外的条件以排除特定ID的标签
  123. ->when($except, function ($query, $except) {
  124. $query->where($this->getPk(), '<>', $except);
  125. })->count() > 0;
  126. }
  127. /**
  128. * 获取用户标签交集
  129. * 该方法用于根据给定的标签ID数组,商家ID和可选的标签类型,从数据库中查询并返回符合条件的标签ID集合。
  130. *
  131. * @param array $ids 标签ID数组,表示需要查询的标签范围。
  132. * @param int $merId 商家ID,表示查询指定商家的标签。
  133. * @param string|null $type 标签类型,可选参数,用于进一步筛选标签类型。
  134. * @return array 返回符合条件的标签ID集合。
  135. */
  136. public function intersection(array $ids, $merId, $type)
  137. {
  138. // 使用UserLabel的数据库连接,并构造查询条件
  139. return UserLabel::getDB()->whereIn('label_id', $ids)->where('mer_id', $merId)->when(!is_null($type), function ($query) use ($type) {
  140. // 如果指定了标签类型,则添加类型查询条件
  141. $query->where('type', $type);
  142. })->column('label_id');
  143. }
  144. }