feat(huya): 增加协作单礼包字典与名称编辑

This commit is contained in:
yml2213
2026-11-07 16:54:01 +08:00
parent afcbff2207
commit efa8bf2a46
12 changed files with 1183 additions and 44 deletions
+82
View File
@@ -37,6 +37,9 @@ from ...schemas import (
HuyaCollabCkPreviewOut,
HuyaCollabCkPreviewRequest,
HuyaCollabFrequentGiftOut,
HuyaCollabGiftCatalogCreate,
HuyaCollabGiftCatalogOut,
HuyaCollabGiftCatalogUpdate,
HuyaCollabMetricOut,
HuyaCollabShopCreate,
HuyaCollabShopOut,
@@ -97,6 +100,11 @@ def _publish_change(current: User, *, scope: str, action: str) -> None:
collab_events.publish(scope=scope, action=action, actor_id=int(current.id))
def _gift_out(db: Session, gift) -> dict:
names = collab.user_names(db, [gift.created_by, gift.updated_by])
return collab.serialize_gift(gift, users=names)
# --------------------------------------------------------------------------
# 字面量路径必须声明在 /{order_id} 之前,否则 "summary" 会被当成 id
# --------------------------------------------------------------------------
@@ -239,6 +247,80 @@ def delete_collab_shop(
return {"success": True, "name": snapshot["name"]}
@router.get("/collab-gifts", response_model=list[HuyaCollabGiftCatalogOut])
def list_collab_gifts(
db: Session = Depends(get_db),
_current: User = Depends(require_permission("huya:collab:view")),
):
"""全部礼包字典项(含停用的:管理弹窗要看到;新建时前端按 is_active 过滤)。"""
gifts = collab.list_gift_records(db)
names = collab.user_names(
db,
[gift.created_by for gift in gifts] + [gift.updated_by for gift in gifts],
)
return [collab.serialize_gift(gift, users=names) for gift in gifts]
@router.post("/collab-gifts", response_model=HuyaCollabGiftCatalogOut)
def create_collab_gift(
req: HuyaCollabGiftCatalogCreate,
db: Session = Depends(get_db),
current: User = Depends(require_permission("huya:collab:manage")),
):
"""新增标准礼包(重名 409;写操作仅管理员)。"""
try:
gift = collab.create_gift(
db,
current,
name=req.name,
points=req.points,
sort_order=req.sort_order,
remark=req.remark,
)
except collab.CollabError as exc:
raise _http_error(exc) from exc
_publish_change(current, scope="gifts", action="gift.create")
return _gift_out(db, gift)
@router.put("/collab-gifts/{gift_id}", response_model=HuyaCollabGiftCatalogOut)
def update_collab_gift(
gift_id: int,
req: HuyaCollabGiftCatalogUpdate,
db: Session = Depends(get_db),
current: User = Depends(require_permission("huya:collab:manage")),
):
"""改礼包字典;改名会同步所有历史协作单上的礼包名。"""
try:
gift = collab.get_gift(db, gift_id)
collab.update_gift(db, current, gift, req.model_dump(exclude_unset=True))
except collab.CollabError as exc:
raise _http_error(exc) from exc
_publish_change(current, scope="all", action="gift.update")
return _gift_out(db, gift)
@router.delete("/collab-gifts/{gift_id}")
def delete_collab_gift(
gift_id: int,
db: Session = Depends(get_db),
current: User = Depends(require_permission("huya:collab:manage")),
):
"""删除未被历史订单引用的字典项;仍在使用时改为停用。"""
try:
gift = collab.get_gift(db, gift_id)
result = collab.delete_gift(db, current, gift)
except collab.CollabError as exc:
raise _http_error(exc) from exc
_publish_change(current, scope="gifts", action="gift.delete")
return {
"success": True,
"id": gift_id,
"name": result["name"],
"points": result["points"],
}
@router.get("/external-orders", response_model=HuyaExternalOrderListOut)
def list_external_orders(
shop: str = Query("", max_length=64),