62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
"""在途调拨策略硬门禁。
|
||
|
||
用途:一期策略禁止部分数量调拨、数量容差与 ERP 自动重试,
|
||
强制默认 10 分钟确认有效期。
|
||
依赖:app.core.errors.AppError、standard decimal。
|
||
逻辑:任何策略变体触碰三项禁令之一即抛 StrategyViolationError,
|
||
阻止发布/执行(fail-closed)。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from dataclasses import dataclass
|
||
from decimal import Decimal
|
||
|
||
from app.core.errors import AppError
|
||
|
||
STRATEGY_VIOLATION_CODE = "TRANSFER.STRATEGY_VIOLATION"
|
||
DEFAULT_CONFIRM_TTL_SECONDS = 600 # 10 分钟
|
||
|
||
|
||
@dataclass(frozen=True)
|
||
class TransferStrategyPolicy:
|
||
"""调拨策略冻结快照;三项禁令默认关闭。"""
|
||
|
||
allow_partial: bool = False
|
||
quantity_tolerance: Decimal = Decimal("0")
|
||
allow_erp_auto_retry: bool = False
|
||
confirm_ttl_seconds: int = DEFAULT_CONFIRM_TTL_SECONDS
|
||
|
||
|
||
class StrategyViolationError(AppError):
|
||
"""策略触碰一期禁令。"""
|
||
|
||
def __init__(self, message: str, *, reason: str):
|
||
super().__init__(
|
||
code=STRATEGY_VIOLATION_CODE,
|
||
message=message,
|
||
http_status=409,
|
||
details={"reason": reason},
|
||
)
|
||
|
||
|
||
def validate_strategy(strategy: TransferStrategyPolicy) -> None:
|
||
"""校验策略是否违反一期硬门禁。
|
||
|
||
Args:
|
||
strategy: 待校验策略。
|
||
Raises:
|
||
StrategyViolationError: 开启部分调拨、非零容差或 ERP 自动重试。
|
||
"""
|
||
if strategy.allow_partial:
|
||
raise StrategyViolationError("partial transfer is not supported in phase 1", reason="allow_partial")
|
||
if strategy.quantity_tolerance != Decimal("0"):
|
||
raise StrategyViolationError("quantity tolerance is not supported in phase 1", reason="quantity_tolerance")
|
||
if strategy.allow_erp_auto_retry:
|
||
raise StrategyViolationError("ERP auto retry is forbidden in phase 1", reason="allow_erp_auto_retry")
|
||
|
||
|
||
def default_strategy() -> TransferStrategyPolicy:
|
||
"""返回一期默认策略(全部禁令关闭,确认有效期 600 秒)。"""
|
||
return TransferStrategyPolicy()
|