UserLabelRelationServices.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 int $uid
  45. * @param array $labels
  46. */
  47. public function setUserLable($uids, array $labels)
  48. {
  49. if (!is_array($uids)) $uids = [$uids];
  50. $re = $this->dao->delete([['uid', 'in', $uids]]);
  51. if (!count($labels)) return true;
  52. if ($re === false) {
  53. throw new AdminException(400667);
  54. }
  55. /** @var UserServices $userServices */
  56. $userServices = app()->make(UserServices::class);
  57. $data = [];
  58. foreach ($uids as $uid) {
  59. foreach ($labels as $label) {
  60. $data[] = ['uid' => $uid, 'label_id' => $label];
  61. }
  62. $userServices->update(['uid' => $uid], ['label_ids' => implode(',', $labels)]);
  63. }
  64. if ($data) {
  65. if (!$this->dao->saveAll($data))
  66. throw new AdminException(400668);
  67. }
  68. return true;
  69. }
  70. /**
  71. * 取消用户标签
  72. * @param int $uid
  73. * @param array $labels
  74. * @return mixed
  75. */
  76. public function unUserLabel(int $uid, array $labels)
  77. {
  78. if (!count($labels)) {
  79. return true;
  80. }
  81. $this->dao->delete([
  82. ['uid', '=', $uid],
  83. ['label_id', 'in', $labels],
  84. ]);
  85. return true;
  86. }
  87. /**
  88. * 获取用户标签
  89. * @param array $uids
  90. * @return array
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\DbException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. */
  95. public function getUserLabelList(array $uids)
  96. {
  97. return $this->dao->getLabelList($uids);
  98. }
  99. }