UserLabelRelationServices.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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;
  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('清空用户标签失败');
  54. }
  55. $data = [];
  56. foreach ($uids as $uid) {
  57. foreach ($labels as $label) {
  58. $data[] = ['uid' => $uid, 'label_id' => $label];
  59. }
  60. }
  61. if ($data) {
  62. if (!$this->dao->saveAll($data))
  63. throw new AdminException('设置标签失败');
  64. }
  65. return true;
  66. }
  67. /**
  68. * 取消用户标签
  69. * @param int $uid
  70. * @param array $labels
  71. * @return mixed
  72. */
  73. public function unUserLabel(int $uid, array $labels)
  74. {
  75. if (!count($labels)) {
  76. return true;
  77. }
  78. $this->dao->delete([
  79. ['uid', '=', $uid],
  80. ['label_id', 'in', $labels],
  81. ]);
  82. return true;
  83. }
  84. /**
  85. * 获取用户标签
  86. * @param array $uids
  87. * @return array
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\DbException
  90. * @throws \think\db\exception\ModelNotFoundException
  91. */
  92. public function getUserLabelList(array $uids)
  93. {
  94. return $this->dao->getLabelList($uids);
  95. }
  96. }