UserLabelRelationServices.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 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;
  13. use app\services\BaseServices;
  14. use app\dao\user\UserLabelRelationDao;
  15. use crmeb\exceptions\AdminException;
  16. /**
  17. *
  18. * Class UserLabelRelationServices
  19. * @package app\services\user
  20. * @method getColumn(array $where, string $field, string $key = '') 获取某个字段数组
  21. * @method saveAll(array $data) 批量保存数据
  22. */
  23. class UserLabelRelationServices extends BaseServices
  24. {
  25. /**
  26. * UserLabelRelationServices constructor.
  27. * @param UserLabelRelationDao $dao
  28. */
  29. public function __construct(UserLabelRelationDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取某个用户标签ids
  35. * @param int $uid
  36. * @return array
  37. */
  38. public function getUserLabels(int $uid)
  39. {
  40. return $this->dao->getColumn(['uid' => $uid], 'label_id', '');
  41. }
  42. /**
  43. * 用户设置标签
  44. * @param $uids
  45. * @param array $labels
  46. * @return bool
  47. * @throws \Exception
  48. */
  49. public function setUserLabel($uids, array $labels, $label_type = 0)
  50. {
  51. if (!is_array($uids)) $uids = [$uids];
  52. if (!count($labels)) {
  53. $this->dao->delete([['uid', 'in', $uids]]);
  54. return true;
  55. }
  56. if ($label_type == 0 || $label_type == 1) {
  57. if ($label_type == 0) $this->dao->delete([['uid', 'in', $uids]]);
  58. $data = [];
  59. foreach ($uids as $uid) {
  60. foreach ($labels as $label) {
  61. $data[] = ['uid' => $uid, 'label_id' => $label];
  62. }
  63. }
  64. if ($data) {
  65. if (!$this->dao->saveAll($data)) throw new AdminException('设置标签失败');
  66. }
  67. } else {
  68. $this->dao->delete([['uid', 'in', $uids], ['label_id', 'in', $labels]]);
  69. }
  70. return true;
  71. }
  72. /**
  73. * 取消用户标签
  74. * @param int $uid
  75. * @param array $labels
  76. * @return mixed
  77. */
  78. public function unUserLabel(int $uid, array $labels)
  79. {
  80. if (!count($labels)) {
  81. return true;
  82. }
  83. $this->dao->delete([
  84. ['uid', '=', $uid],
  85. ['label_id', 'in', $labels],
  86. ]);
  87. return true;
  88. }
  89. /**
  90. * 获取用户标签
  91. * @param array $uids
  92. * @return array
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public function getUserLabelList(array $uids)
  98. {
  99. return $this->dao->getLabelList($uids);
  100. }
  101. }