feat(douyu): 支持管理员取消作废协作单

This commit is contained in:
yml2213
2026-11-06 15:42:19 +08:00
parent 973042288f
commit afcbff2207
3 changed files with 233 additions and 7 deletions
+64 -1
View File
@@ -908,6 +908,60 @@ def _assert_ready_for_recharge(order: DouyuExternalOrder) -> None:
)
def _restore_void_status(
db: Session,
current: User,
order: DouyuExternalOrder,
*,
target: str,
reason: str,
commit: bool,
) -> DouyuExternalOrder:
"""超级管理员取消作废,并把订单转为指定状态。"""
if current.role != "super_admin":
raise CollabError(
"仅超级管理员可取消作废",
status_code=403,
code="admin_required",
)
if order.archived_at is not None:
raise CollabError("已归档的订单不能改状态", code="order_archived")
target_index = STAGE_INDEX.get(target)
if target_index is not None and target_index >= STAGE_INDEX[STATUS_TO_RECHARGE]:
_assert_ready_for_recharge(order)
old_void_reason = order.void_reason or ""
# 取消作废等于重新进入状态机:先清空旧流程时间,再按目标状态补当前时间。
for field in STATUS_TIMESTAMP_FIELDS.values():
setattr(order, field, None)
if target_index is not None:
for name, field in STATUS_TIMESTAMP_FIELDS.items():
if STAGE_INDEX[name] <= target_index:
setattr(order, field, _now())
order.status = target
order.void_reason = ""
order.updated_by = current.id
order.version = int(order.version or 0) + 1
record_audit(
db,
current,
action="douyu:collab:void_restore",
target=_status_audit_target(order),
detail={
"from": STATUS_VOID,
"to": target,
"previous_void_reason": old_void_reason,
"reason": _text(reason, max_length=256),
},
)
if commit:
db.commit()
db.refresh(order)
return order
def set_status(
db: Session,
current: User,
@@ -926,7 +980,16 @@ def set_status(
if order.archived_at is not None:
raise CollabError("已归档的订单不能改状态", code="order_archived")
if order.status == STATUS_VOID:
raise CollabError("已作废的订单不能改状态", code="order_void")
if target == STATUS_VOID:
raise CollabError("已作废的订单不能改状态", code="order_void")
return _restore_void_status(
db,
current,
order,
target=target,
reason=reason,
commit=commit,
)
old = order.status
now = _now()