UserLabelRelationServices.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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 setUserLable($uids, array $labels)
  50. {
  51. if (!is_array($uids)) $uids = [$uids];
  52. $re = $this->dao->delete([['uid', 'in', $uids]]);
  53. if (!count($labels)) return true;
  54. if ($re === false) {
  55. throw new AdminException(400667);
  56. }
  57. /** @var UserServices $userServices */
  58. $userServices = app()->make(UserServices::class);
  59. $data = [];
  60. foreach ($uids as $uid) {
  61. foreach ($labels as $label) {
  62. $data[] = ['uid' => $uid, 'label_id' => $label];
  63. }
  64. $userServices->update(['uid' => $uid], ['label_ids' => implode(',', $labels)]);
  65. }
  66. if ($data) {
  67. if (!$this->dao->saveAll($data))
  68. throw new AdminException(400668);
  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. }