32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
# 加车结果。HTTP 客户端把商城回包收成这三种。
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
KIND_OK = "ok"
|
|
KIND_BUSINESS = "business"
|
|
KIND_SYSTEM = "system"
|
|
|
|
|
|
# 一次加车的结果:成功、业务失败(库存/中包装)或系统失败(登录、返回异常)。
|
|
@dataclass
|
|
class AddOutcome:
|
|
kind: str
|
|
reason: str = ""
|
|
|
|
|
|
# 按商城回文区分成功、库存/中包装等业务失败,以及看不懂的系统失败。
|
|
def classify_mall_message(text: str) -> AddOutcome:
|
|
msg = (text or "").strip()
|
|
if not msg:
|
|
return AddOutcome(kind=KIND_SYSTEM, reason="加购失败")
|
|
if any(token in msg for token in ("成功", "已加入")):
|
|
return AddOutcome(kind=KIND_OK)
|
|
if any(token in msg for token in ("中包装", "倍数", "起订")):
|
|
reason = msg if "中包装" in msg else f"中包装加不进:{msg}"
|
|
return AddOutcome(kind=KIND_BUSINESS, reason=reason)
|
|
if any(token in msg for token in ("库存", "无货", "缺货")):
|
|
return AddOutcome(kind=KIND_BUSINESS, reason="库存不足")
|
|
return AddOutcome(kind=KIND_BUSINESS, reason=msg)
|