UserLabelRelationServices.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. declare (strict_types=1);
  12. namespace app\services\user\label;
  13. use app\services\BaseServices;
  14. use app\dao\user\label\UserLabelRelationDao;
  15. use crmeb\exceptions\AdminException;
  16. /**
  17. * 用户关联标签
  18. * Class UserLabelRelationServices
  19. * @package app\services\user\label
  20. * @mixin UserLabelRelationDao
  21. */
  22. class UserLabelRelationServices extends BaseServices
  23. {
  24. /**
  25. * UserLabelRelationServices constructor.
  26. * @param UserLabelRelationDao $dao
  27. */
  28. public function __construct(UserLabelRelationDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 获取某个用户标签ids
  34. * @param int $uid
  35. * @param int $store_id
  36. * @return array
  37. */
  38. public function getUserLabels(int $uid, int $type = 0, int $relation_id = 0)
  39. {
  40. return $this->dao->getColumn(['uid' => $uid, 'type' => $type, 'relation_id' => $relation_id], 'label_id', '');
  41. }
  42. /**
  43. * 用户设置标签
  44. * @param $uids
  45. * @param array $labels
  46. * @param int $type
  47. * @param int $relation_id
  48. * @param bool $group
  49. * @return bool
  50. */
  51. public function setUserLable($uids, array $labels, int $type = 0, int $relation_id = 0, bool $group = false)
  52. {
  53. if (!$uids) {
  54. return true;
  55. }
  56. if (!is_array($uids)) {
  57. $uids = [$uids];
  58. }
  59. //增加标签
  60. $data = [];
  61. foreach ($uids as $uid) {
  62. //用户已经存在的标签
  63. $user_label_ids = $this->dao->getColumn([
  64. ['uid', '=', $uid],
  65. ['type', '=', $type],
  66. ['relation_id', '=', $relation_id]
  67. ], 'label_id');
  68. //删除未选中标签
  69. if ($group) {
  70. $del_labels = array_diff($user_label_ids, $labels);
  71. $this->dao->delete([
  72. ['uid', '=', $uid],
  73. ['label_id', 'in', $del_labels],
  74. ['type', '=', $type],
  75. ['relation_id', '=', $relation_id]
  76. ]);
  77. }
  78. $add_labels = array_diff($labels, $user_label_ids);
  79. if ($add_labels) {
  80. foreach ($add_labels as $label) {
  81. $label = (int)$label;
  82. if (!$label) continue;
  83. $data[] = ['uid' => $uid, 'label_id' => $label, 'type' => $type, 'relation_id' => $relation_id];
  84. }
  85. }
  86. }
  87. if ($data) {
  88. if (!$this->dao->saveAll($data))
  89. throw new AdminException('设置标签失败');
  90. }
  91. return true;
  92. }
  93. /**
  94. * 取消用户标签
  95. * @param int $uid
  96. * @param array $labels
  97. * @param int $type
  98. * @param int $relation_id
  99. * @return bool
  100. */
  101. public function unUserLabel(int $uid, array $labels = [], int $type = 0, int $relation_id = 0)
  102. {
  103. $where = [
  104. ['uid', '=', $uid],
  105. ['type', '=', $type],
  106. ['relation_id', '=', $relation_id]
  107. ];
  108. //不传入 清空用户所有标签
  109. if (count($labels)) {
  110. $where[] = ['label_id', 'in', $labels];
  111. }
  112. $this->dao->delete($where);
  113. return true;
  114. }
  115. /**
  116. * 获取用户标签
  117. * @param array $uids
  118. * @param int $type
  119. * @param int $relation_id
  120. * @return array
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public function getUserLabelList(array $uids, int $type = 0, int $relation_id = 0)
  126. {
  127. return $this->dao->getLabelList($uids, $type, $relation_id);
  128. }
  129. }