feat(douyu): 支持批量导出用户名
This commit is contained in:
@@ -106,6 +106,7 @@ POINTS_THRESHOLD_MAX = 1_000_000
|
||||
MAX_IMAGES = 20
|
||||
CK_EXPORT_BATCH_MAX = 50
|
||||
CK_EXPORT_ACTION = "douyu:collab:ck_export"
|
||||
USERNAME_EXPORT_BATCH_MAX = 500
|
||||
|
||||
VERDICT_SUCCESS = "success"
|
||||
VERDICT_FAILED = "failed"
|
||||
@@ -2084,3 +2085,75 @@ def export_cks(
|
||||
"skipped": skipped,
|
||||
"items": items,
|
||||
}
|
||||
|
||||
|
||||
def export_usernames(
|
||||
db: Session,
|
||||
order_ids: Sequence[int],
|
||||
) -> dict[str, Any]:
|
||||
"""导出勾选订单关联的斗鱼用户名(去重、保持选择顺序)。
|
||||
|
||||
用户名不是凭据,能看列表的人本来就能看到,所以这里不额外要求 CK 导出权限,
|
||||
也不写审计;没有用户名的行会显式跳过。
|
||||
"""
|
||||
requested = list(dict.fromkeys(int(item) for item in order_ids if item))
|
||||
if not requested:
|
||||
raise CollabError("请先勾选要导出的行", code="username_export_empty")
|
||||
if len(requested) > USERNAME_EXPORT_BATCH_MAX:
|
||||
raise CollabError(
|
||||
f"一次最多导出 {USERNAME_EXPORT_BATCH_MAX} 行,请分批操作",
|
||||
code="username_export_too_large",
|
||||
)
|
||||
|
||||
rows = (
|
||||
db.query(DouyuExternalOrder).filter(DouyuExternalOrder.id.in_(requested)).all()
|
||||
)
|
||||
by_id = {int(order.id): order for order in rows}
|
||||
items: list[dict[str, Any]] = []
|
||||
skipped: list[dict[str, Any]] = []
|
||||
usernames: list[str] = []
|
||||
seen: set[str] = set()
|
||||
for order_id in requested:
|
||||
order = by_id.get(order_id)
|
||||
if order is None:
|
||||
skipped.append(
|
||||
{
|
||||
"id": order_id,
|
||||
"order_no": "",
|
||||
"reason": "协作单不存在或已删除",
|
||||
}
|
||||
)
|
||||
continue
|
||||
username = str(order.account_username or "").strip()
|
||||
if not username:
|
||||
account = account_by_id(db, order.account_id) or account_by_username(
|
||||
db, order.account_username
|
||||
)
|
||||
username = str(account.username if account else "").strip()
|
||||
if not username:
|
||||
skipped.append(
|
||||
{
|
||||
"id": int(order.id),
|
||||
"order_no": order.order_no,
|
||||
"reason": "这单还没填斗鱼用户名",
|
||||
}
|
||||
)
|
||||
continue
|
||||
items.append(
|
||||
{
|
||||
"id": int(order.id),
|
||||
"order_no": order.order_no,
|
||||
"account_username": username,
|
||||
}
|
||||
)
|
||||
if username not in seen:
|
||||
seen.add(username)
|
||||
usernames.append(username)
|
||||
return {
|
||||
"total": len(requested),
|
||||
"exported": len(items),
|
||||
"unique": len(usernames),
|
||||
"skipped": skipped,
|
||||
"items": items,
|
||||
"usernames": usernames,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user