This commit is contained in:
jlj 2025-06-10 16:53:50 +08:00
parent 54ad21b952
commit 0d494d1976

View File

@ -2191,37 +2191,42 @@ def add_to_public_pool(request):
data = json.loads(request.body) data = json.loads(request.body)
# 获取必要参数 # 获取必要参数
creator_id = data.get('creator_id') creator_ids = data.get('creator_ids') # 支持多个达人ID
category = data.get('category') category = data.get('category')
remark = data.get('remark') remark = data.get('remark')
if not creator_id: if not creator_ids:
return JsonResponse({ return JsonResponse({
'code': 400, 'code': 400,
'message': '缺少必要参数: creator_id', 'message': '缺少必要参数: creator_ids',
'data': None 'data': None
}, json_dumps_params={'ensure_ascii': False}) }, json_dumps_params={'ensure_ascii': False})
result_data = []
success_count = 0
failed_count = 0
failed_ids = []
existed_creators = []
for creator_id in creator_ids:
# 查询达人信息 # 查询达人信息
try: try:
creator = CreatorProfile.objects.get(id=creator_id) creator = CreatorProfile.objects.get(id=creator_id)
except CreatorProfile.DoesNotExist: except CreatorProfile.DoesNotExist:
return JsonResponse({ failed_count += 1
'code': 404, failed_ids.append(creator_id)
'message': f'找不到ID为 {creator_id} 的达人', continue
'data': None
}, json_dumps_params={'ensure_ascii': False})
# 检查是否已存在于公有库 # 检查是否已存在于公有库
exists = PublicCreatorPool.objects.filter(creator=creator).exists() exists = PublicCreatorPool.objects.filter(creator=creator).exists()
if exists: if exists:
# 如果已存在,则更新信息 # 如果已存在,不进行更新,只记录已存在状态
public_creator = PublicCreatorPool.objects.get(creator=creator) public_creator = PublicCreatorPool.objects.get(creator=creator)
public_creator.category = category if category else public_creator.category existed_creators.append({
public_creator.remark = remark if remark else public_creator.remark 'id': creator.id,
public_creator.save() 'name': creator.name
})
action = "更新" continue
else: else:
# 创建新的公有库达人 # 创建新的公有库达人
public_creator = PublicCreatorPool.objects.create( public_creator = PublicCreatorPool.objects.create(
@ -2230,12 +2235,8 @@ def add_to_public_pool(request):
remark=remark remark=remark
) )
action = "添加" success_count += 1
result_data.append({
return JsonResponse({
'code': 200,
'message': f'成功{action}达人到公有库',
'data': {
'creator': { 'creator': {
'id': creator.id, 'id': creator.id,
'name': creator.name 'name': creator.name
@ -2245,6 +2246,28 @@ def add_to_public_pool(request):
'category': public_creator.category, 'category': public_creator.category,
'remark': public_creator.remark 'remark': public_creator.remark
} }
})
# 生成响应消息
message_parts = []
if success_count > 0:
message_parts.append(f'成功添加{success_count}个达人到公有库')
if len(existed_creators) > 0:
message_parts.append(f'{len(existed_creators)}个达人已存在于公有库')
if failed_count > 0:
message_parts.append(f'{failed_count}个达人不存在')
message = ''.join(message_parts)
return JsonResponse({
'code': 200,
'message': message,
'data': {
'success_count': success_count,
'failed_count': failed_count,
'failed_ids': failed_ids,
'existed_creators': existed_creators,
'results': result_data
} }
}, json_dumps_params={'ensure_ascii': False}) }, json_dumps_params={'ensure_ascii': False})