refactor(douyu): 统一活动查询与直播商品映射
- 收口斗鱼活动、商品、绑定信息和兑换资格查询 - 工作台改为复用统一客户端、快照与实时商品目录 - 补充活动查询与商品映射测试
This commit is contained in:
@@ -10,8 +10,7 @@ from fastapi import HTTPException
|
||||
from sqlalchemy import or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.douyu import DouyuActivityClient, DouyuActivityError
|
||||
from core.douyu.cookie_utils import cookie_value
|
||||
from core.douyu import DouyuActivityError
|
||||
|
||||
from ...models import (
|
||||
Account,
|
||||
@@ -23,6 +22,7 @@ from ...models import (
|
||||
PlatformFulfillmentTask,
|
||||
User,
|
||||
)
|
||||
from ..douyu_activity_query import activity_client, query_exchange_eligibility
|
||||
from ..douyu_batch_launcher import start_douyu_batch
|
||||
from ..douyu_service import douyu_config_value, ensure_douyu_config
|
||||
from ..external_order_source import (
|
||||
@@ -103,9 +103,11 @@ def _append_event(
|
||||
|
||||
|
||||
def _load_task(db: Session, task_id: int) -> PlatformFulfillmentTask:
|
||||
task = db.query(PlatformFulfillmentTask).filter(
|
||||
PlatformFulfillmentTask.id == int(task_id)
|
||||
).first()
|
||||
task = (
|
||||
db.query(PlatformFulfillmentTask)
|
||||
.filter(PlatformFulfillmentTask.id == int(task_id))
|
||||
.first()
|
||||
)
|
||||
if task is None:
|
||||
raise HTTPException(status_code=404, detail="直播账号履约任务不存在")
|
||||
return task
|
||||
@@ -339,9 +341,7 @@ def _task_out(
|
||||
"areaName": str(bind_result.get("area_name") or ""),
|
||||
"platName": str(bind_result.get("plat_name") or ""),
|
||||
"bindPhase": str(bind_result.get("bind_phase") or ""),
|
||||
"bindReadyForConfirm": bool(
|
||||
bind_result.get("bind_ready_for_confirm")
|
||||
),
|
||||
"bindReadyForConfirm": bool(bind_result.get("bind_ready_for_confirm")),
|
||||
"bindConfirmed": bool(bind_result.get("bind_confirmed")),
|
||||
}
|
||||
if bind_task
|
||||
@@ -474,12 +474,12 @@ def upsert_mapping(
|
||||
detail={"commodityId": row.commodity_id, "enabled": row.enabled},
|
||||
)
|
||||
db.commit()
|
||||
return next(item for item in list_mappings(db, keyword=sku_key) if item["id"] == row.id)
|
||||
return next(
|
||||
item for item in list_mappings(db, keyword=sku_key) if item["id"] == row.id
|
||||
)
|
||||
|
||||
|
||||
def allocate_task(
|
||||
db: Session, *, task_id: int, current: User
|
||||
) -> dict[str, Any]:
|
||||
def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]:
|
||||
task = _load_task(db, task_id)
|
||||
if task.execution_status in {"completed", "cancelled"}:
|
||||
raise HTTPException(status_code=409, detail="已结束任务不能重新分配账号")
|
||||
@@ -579,9 +579,7 @@ def release_task_account(
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
|
||||
async def start_binding(
|
||||
db: Session, *, task_id: int, current: User
|
||||
) -> dict[str, Any]:
|
||||
async def start_binding(db: Session, *, task_id: int, current: User) -> dict[str, Any]:
|
||||
task = _load_task(db, task_id)
|
||||
if not task.account_id or not task.lease_id:
|
||||
raise HTTPException(status_code=409, detail="请先分配账号")
|
||||
@@ -728,21 +726,17 @@ def refresh_exchange_eligibility(
|
||||
raise HTTPException(status_code=409, detail="账号 CK 不可用")
|
||||
config = ensure_douyu_config(db)
|
||||
try:
|
||||
client = DouyuActivityClient(cookie=str(account.cookie))
|
||||
ctn = client.acf_ccn(refresh_subscribe=False)
|
||||
user_info = client.elite_user_info(
|
||||
cookie = str(account.cookie)
|
||||
client = activity_client(cookie)
|
||||
eligibility = query_exchange_eligibility(
|
||||
client,
|
||||
cookie=cookie,
|
||||
manual_id=str(douyu_config_value("manual_id", config.manual_id)),
|
||||
rid=str(douyu_config_value("rid", config.rid)),
|
||||
uid=str(account.uid or ""),
|
||||
)
|
||||
account.elite_opened = bool(user_info.get("manual_type"))
|
||||
uid = account.uid or cookie_value(str(account.cookie), "acf_uid")
|
||||
points_result = client.query_points(uid=uid, ctn=ctn)
|
||||
account.points = int(points_result.get("points") or 0)
|
||||
result = client.query_limited_goods(
|
||||
manual_id=str(douyu_config_value("manual_id", config.manual_id)),
|
||||
rid=str(douyu_config_value("rid", config.rid)),
|
||||
)
|
||||
limited = result.get("limited_goods") or []
|
||||
account.elite_opened = bool(eligibility.elite_user_info.get("manual_type"))
|
||||
account.points = eligibility.points
|
||||
except DouyuActivityError as exc:
|
||||
task.execution_status = "manual_required"
|
||||
task.exchange_status = "check_failed"
|
||||
@@ -760,13 +754,7 @@ def refresh_exchange_eligibility(
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
limited_ids = {
|
||||
str(item.get("commodityId") or item.get("commodity_id") or "")
|
||||
for item in limited
|
||||
if isinstance(item, dict)
|
||||
}
|
||||
limited_ids.discard("")
|
||||
if task.commodity_id in limited_ids:
|
||||
if task.commodity_id in eligibility.limited_ids:
|
||||
task.exchange_status = "limited"
|
||||
task.issue_code = "commodity_limit_reached"
|
||||
task.last_error = "该账号已达到此商品兑换上限"
|
||||
@@ -795,7 +783,7 @@ def refresh_exchange_eligibility(
|
||||
actor_id=current.id,
|
||||
detail={
|
||||
"exchangeStatus": task.exchange_status,
|
||||
"limitedCount": len(limited_ids),
|
||||
"limitedCount": len(eligibility.limited_ids),
|
||||
},
|
||||
)
|
||||
db.commit()
|
||||
|
||||
Reference in New Issue
Block a user