发布(0.4.6): 修复预售兑换记录判定与详情展示并新增工作台强制发放

This commit is contained in:
yml
2026-11-27 14:50:45 +08:00
parent 5006367217
commit e25e35994d
22 changed files with 1118 additions and 38 deletions
+98 -1
View File
@@ -23,6 +23,7 @@
from __future__ import annotations
from datetime import UTC, datetime, timedelta
from typing import Any
from sqlalchemy import or_
from sqlalchemy.orm import Session
@@ -194,6 +195,96 @@ def _account_display_label(account: Account) -> str:
)
def _occupying_platform_task_for_lease(
db: Session,
account_id: int,
lease: AccountLease,
) -> PlatformFulfillmentTask | None:
task = (
db.query(PlatformFulfillmentTask)
.filter(PlatformFulfillmentTask.lease_id == int(lease.id))
.order_by(PlatformFulfillmentTask.id.desc())
.first()
)
if task is not None:
return task
return (
db.query(PlatformFulfillmentTask)
.filter(
PlatformFulfillmentTask.account_id == int(account_id),
PlatformFulfillmentTask.execution_status.notin_(("completed", "cancelled")),
)
.order_by(PlatformFulfillmentTask.id.desc())
.first()
)
def workbench_force_goods_inspection(
db: Session,
account: Account,
*,
lease: AccountLease | None = None,
) -> dict[str, Any]:
"""评估工作台强制锁单/兑换的占用与角色安全条件。
只有斗鱼号凭据正常、已获取当前绑定角色(非空且非未绑定),且与履约单
目标角色一致时才允许超管/运营二次确认后强制发放。
"""
active_lease = lease or active_account_lease(db, account.id)
label = _account_display_label(account)
if active_lease is None or active_lease.purpose != "platform_fulfillment":
return {
"occupied": False,
"can_force": True,
"reason": "",
"account_id": int(account.id),
"account_name": label,
"account_role": str(account.game_name or "").strip(),
"platform_task_id": None,
"platform_order_id": "",
"customer_role_name": "",
"sku_name": "",
"lease_batch_id": "",
}
pf_task = _occupying_platform_task_for_lease(db, account.id, active_lease)
current_role = str(account.game_name or "").strip()
customer_role = str(pf_task.customer_role_name or "").strip() if pf_task else ""
order_label = (
str(pf_task.platform_order_id or pf_task.source_order_id or f"#{pf_task.id}")
if pf_task
else (active_lease.batch_id or "-")
)
reason = ""
if not str(account.cookie or "").strip() or str(account.ck_check_status or "") == "invalid":
reason = f"账号 {label} 登录凭据异常,禁止强制发放"
elif str(account.role_bind_status or "") == "unbound" or not current_role:
reason = (
f"账号 {label} 当前未获取到已绑定的游戏角色,"
"请先点击「一键获取游戏名」确认角色正常后再强制发放"
)
elif customer_role and not role_names_match(current_role, customer_role):
reason = (
f"账号 {label} 当前绑定角色({current_role})与履约单 {order_label} "
f"目标角色({customer_role})不一致,禁止强制发放"
)
return {
"occupied": True,
"can_force": not bool(reason),
"reason": reason,
"account_id": int(account.id),
"account_name": label,
"account_role": current_role,
"platform_task_id": int(pf_task.id) if pf_task is not None else None,
"platform_order_id": order_label,
"customer_role_name": customer_role,
"sku_name": str(pf_task.sku_name or "").strip() if pf_task else "",
"lease_batch_id": str(active_lease.batch_id or ""),
}
def workbench_task_occupation_error(
db: Session,
account: Account,
@@ -202,13 +293,15 @@ def workbench_task_occupation_error(
handbook_scope: str,
platform_task_id: int | None,
allow_auto_link: bool = False,
force_occupied: bool = False,
) -> str | None:
"""检查工作台无归属任务是否会误改已占用账号的角色或库存。
仅拦截未绑定 ``platform_task_id`` 的精英宝典写操作:
- 绑码/换绑(``get_bind_qr`` / ``confirm_bind``):若未命中可自动归属的
待绑定履约单,禁止在任意活动租约(平台履约、预售台账等)或活跃预售单期间执行;
- 锁单/支付/兑换:账号处于平台履约租约期间,只能由履约订单带 ``platform_task_id`` 发起。
- 锁单/支付/兑换:账号处于平台履约租约期间,默认要求从履约订单内发起;
超管/运营显式 ``force_occupied=True`` 时,必须通过角色非空且一致的严格校验。
"""
if handbook_scope != "elite" or platform_task_id is not None:
return None
@@ -265,6 +358,9 @@ def workbench_task_occupation_error(
and lease is not None
and lease.purpose == "platform_fulfillment"
):
if force_occupied:
inspection = workbench_force_goods_inspection(db, account, lease=lease)
return inspection["reason"] or None
return (
f"账号 {label} 当前由平台履约订单占用"
f"(批次 {lease.batch_id or '-'}),请从履约订单内发起锁单或兑换"
@@ -286,5 +382,6 @@ __all__ = [
"normalize_role_name",
"role_names_match",
"source_created_within_lease",
"workbench_force_goods_inspection",
"workbench_task_occupation_error",
]