- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明 Made-with: Cursor
170 lines
4.9 KiB
PHP
170 lines
4.9 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | Author: ScottPan Team
|
|
// +----------------------------------------------------------------------
|
|
|
|
declare (strict_types=1);
|
|
|
|
namespace app\services\user\label;
|
|
|
|
use app\services\BaseServices;
|
|
use app\dao\user\label\UserLabelRelationDao;
|
|
use crmeb\exceptions\AdminException;
|
|
use think\annotation\Inject;
|
|
|
|
/**
|
|
* 用户关联标签
|
|
* Class UserLabelRelationServices
|
|
* @package app\services\user\label
|
|
* @mixin UserLabelRelationDao
|
|
*/
|
|
class UserLabelRelationServices extends BaseServices
|
|
{
|
|
|
|
/**
|
|
* @var UserLabelRelationDao
|
|
*/
|
|
#[Inject]
|
|
protected UserLabelRelationDao $dao;
|
|
|
|
/**
|
|
* 获取某个用户标签ids
|
|
* @param int $uid
|
|
* @param int $type
|
|
* @param int $relation_id
|
|
* @return array
|
|
*/
|
|
public function getUserLabels(int $uid, int $type = 0, int $relation_id = 0)
|
|
{
|
|
return $this->dao->getColumn(['uid' => $uid, 'type' => $type, 'relation_id' => $relation_id], 'label_id', '');
|
|
}
|
|
|
|
/**
|
|
* 用户设置标签
|
|
* @param $uids
|
|
* @param array $labels
|
|
* @param int $type
|
|
* @param int $relation_id
|
|
* @param bool $group
|
|
* @return bool
|
|
*/
|
|
public function setUserLable($uids, array $labels, int $type = 0, int $relation_id = 0, bool $group = false, $label_type = 0)
|
|
{
|
|
if (!$uids) {
|
|
return true;
|
|
}
|
|
if (!is_array($uids)) {
|
|
$uids = [$uids];
|
|
}
|
|
//增加标签
|
|
$data = [];
|
|
foreach ($uids as $uid) {
|
|
if ($label_type == 0) {
|
|
//用户已经存在的标签
|
|
$user_label_ids = $this->dao->delete([
|
|
['uid', '=', $uid],
|
|
['type', '=', $type],
|
|
['relation_id', '=', $relation_id]
|
|
]);
|
|
if ($labels) {
|
|
foreach ($labels as $label) {
|
|
$label = (int)$label;
|
|
if (!$label) continue;
|
|
$data[] = ['uid' => $uid, 'label_id' => $label, 'type' => $type, 'relation_id' => $relation_id];
|
|
}
|
|
}
|
|
} elseif ($label_type == 1) {
|
|
foreach ($labels as $label) {
|
|
$label = (int)$label;
|
|
if (!$label) continue;
|
|
$data[] = ['uid' => $uid, 'label_id' => $label, 'type' => $type, 'relation_id' => $relation_id];
|
|
}
|
|
} else {
|
|
$this->dao->delete([
|
|
['uid', '=', $uid],
|
|
['label_id', 'in', $labels],
|
|
['type', '=', $type],
|
|
['relation_id', '=', $relation_id]
|
|
]);
|
|
}
|
|
}
|
|
if ($data) {
|
|
if (!$this->dao->saveAll($data))
|
|
throw new AdminException('设置标签失败');
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 取消用户标签
|
|
* @param int $uid
|
|
* @param array $labels
|
|
* @param int $type
|
|
* @param int $relation_id
|
|
* @return bool
|
|
*/
|
|
public function unUserLabel(int $uid, array $labels = [], int $type = 0, int $relation_id = 0)
|
|
{
|
|
$where = [
|
|
['uid', '=', $uid],
|
|
['type', '=', $type],
|
|
['relation_id', '=', $relation_id]
|
|
];
|
|
//不传入 清空用户所有标签
|
|
if (count($labels)) {
|
|
$where[] = ['label_id', 'in', $labels];
|
|
}
|
|
$this->dao->delete($where);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 获取用户标签
|
|
* @param array $uids
|
|
* @param int $type
|
|
* @param int $relation_id
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function getUserLabelList(array $uids, int $type = 0, int $relation_id = 0)
|
|
{
|
|
return $this->dao->getLabelList($uids, $type, $relation_id);
|
|
}
|
|
|
|
/**
|
|
* 批量获取用户标签关系映射
|
|
* @param array $uids 用户ID数组
|
|
* @param int $type 类型
|
|
* @param int $relation_id 关联ID
|
|
* @return array 返回格式:[uid => [label_id1, label_id2, ...]]
|
|
*/
|
|
public function getUserLabelsMap(array $uids, int $type = 0, int $relation_id = 0): array
|
|
{
|
|
if (empty($uids)) {
|
|
return [];
|
|
}
|
|
$where = [
|
|
'uid' => $uids,
|
|
'type' => $type,
|
|
'relation_id' => $relation_id
|
|
];
|
|
|
|
|
|
$relations = $this->dao->search($where)->field(['uid', 'label_id'])->select()->toArray();
|
|
|
|
$userLabelsMap = [];
|
|
foreach ($uids as $uid) {
|
|
$userLabelsMap[$uid] = [];
|
|
}
|
|
|
|
foreach ($relations as $relation) {
|
|
$userLabelsMap[$relation['uid']][] = $relation['label_id'];
|
|
}
|
|
|
|
return $userLabelsMap;
|
|
}
|
|
}
|