order_status_compat.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from __future__ import annotations
  2. from dataclasses import dataclass
  3. from decimal import Decimal
  4. from typing import Optional
  5. @dataclass(frozen=True)
  6. class ThreeStatus:
  7. order_status: str
  8. delivery_status: str
  9. pay_status: str
  10. def _to_decimal(v) -> Decimal:
  11. try:
  12. if v is None:
  13. return Decimal("0")
  14. if isinstance(v, Decimal):
  15. return v
  16. return Decimal(str(v))
  17. except Exception:
  18. return Decimal("0")
  19. def derive_three_status_from_legacy(
  20. legacy_status: Optional[str],
  21. *,
  22. paid_amount=None,
  23. ) -> ThreeStatus:
  24. """
  25. 阶段A兼容:从旧 status 推导三字段。
  26. 约束:旧 status 本身混合了支付/配送/订单生命周期,因此只能尽力推断;
  27. 若业务侧已有更精确来源(例如配送任务表/结算表),后续阶段再增强。
  28. """
  29. s = (legacy_status or "").strip() or "pending_payment"
  30. paid = _to_decimal(paid_amount)
  31. # 默认:仅保留新主状态模型
  32. order_status = "pending_confirm" if paid > 0 else "pending_payment"
  33. delivery_status = "pending_dispatch"
  34. pay_status = "paid" if paid > 0 else "pending_payment"
  35. if s == "cancelled":
  36. order_status = "cancelled"
  37. elif s == "completed":
  38. order_status = "completed"
  39. delivery_status = "delivered"
  40. elif s == "pending_payment":
  41. order_status = "pending_payment"
  42. pay_status = "pending_payment"
  43. delivery_status = "pending_dispatch"
  44. elif s == "delivering":
  45. order_status = "pending_confirm"
  46. delivery_status = "delivering"
  47. elif s == "pending_confirm":
  48. order_status = "pending_confirm"
  49. delivery_status = "delivered"
  50. elif s == "pending_review_cancel":
  51. order_status = "pending_confirm"
  52. delivery_status = "pending_dispatch"
  53. pass
  54. elif s == "refunding":
  55. pay_status = "refunding"
  56. return ThreeStatus(order_status=order_status, delivery_status=delivery_status, pay_status=pay_status)
  57. def derive_legacy_status_from_three(
  58. *,
  59. order_status: Optional[str],
  60. delivery_status: Optional[str],
  61. pay_status: Optional[str],
  62. ) -> str:
  63. """
  64. 阶段A兼容:从三字段反推旧 status(用于旧前端/旧筛选参数兼容)。
  65. """
  66. o = (order_status or "").strip()
  67. d = (delivery_status or "").strip()
  68. p = (pay_status or "").strip()
  69. if o == "cancelled":
  70. return "cancelled"
  71. if p == "refunding":
  72. return "refunding"
  73. if o == "completed":
  74. return "completed"
  75. if o == "pending_payment":
  76. return "pending_payment"
  77. if d == "pending_dispatch":
  78. return "pending_confirm"
  79. if d == "delivering":
  80. return "delivering"
  81. if d == "delivered":
  82. return "pending_confirm"
  83. return "pending_payment"