from __future__ import annotations from dataclasses import dataclass from decimal import Decimal from typing import Optional @dataclass(frozen=True) class ThreeStatus: order_status: str delivery_status: str pay_status: str def _to_decimal(v) -> Decimal: try: if v is None: return Decimal("0") if isinstance(v, Decimal): return v return Decimal(str(v)) except Exception: return Decimal("0") def derive_three_status_from_legacy( legacy_status: Optional[str], *, paid_amount=None, ) -> ThreeStatus: """ 阶段A兼容:从旧 status 推导三字段。 约束:旧 status 本身混合了支付/配送/订单生命周期,因此只能尽力推断; 若业务侧已有更精确来源(例如配送任务表/结算表),后续阶段再增强。 """ s = (legacy_status or "").strip() or "pending_payment" paid = _to_decimal(paid_amount) # 默认:仅保留新主状态模型 order_status = "pending_confirm" if paid > 0 else "pending_payment" delivery_status = "pending_dispatch" pay_status = "paid" if paid > 0 else "pending_payment" if s == "cancelled": order_status = "cancelled" elif s == "completed": order_status = "completed" delivery_status = "delivered" elif s == "pending_payment": order_status = "pending_payment" pay_status = "pending_payment" delivery_status = "pending_dispatch" elif s == "delivering": order_status = "pending_confirm" delivery_status = "delivering" elif s == "pending_confirm": order_status = "pending_confirm" delivery_status = "delivered" elif s == "pending_review_cancel": order_status = "pending_confirm" delivery_status = "pending_dispatch" pass elif s == "refunding": pay_status = "refunding" return ThreeStatus(order_status=order_status, delivery_status=delivery_status, pay_status=pay_status) def derive_legacy_status_from_three( *, order_status: Optional[str], delivery_status: Optional[str], pay_status: Optional[str], ) -> str: """ 阶段A兼容:从三字段反推旧 status(用于旧前端/旧筛选参数兼容)。 """ o = (order_status or "").strip() d = (delivery_status or "").strip() p = (pay_status or "").strip() if o == "cancelled": return "cancelled" if p == "refunding": return "refunding" if o == "completed": return "completed" if o == "pending_payment": return "pending_payment" if d == "pending_dispatch": return "pending_confirm" if d == "delivering": return "delivering" if d == "delivered": return "pending_confirm" return "pending_payment"