states.py
4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""库存补货流程共用的状态常量和小工具(service.py 与 single_service.py 共用,避免循环导入)。"""
from __future__ import annotations
import datetime as dt
import re
# 后台「强制终止」能停的状态(建单 / 截图 / 提交中、建单异常的 DMS 结果可能未知,不能强停)
TERMINABLE = {"scheduled": ("scanning", "scan_retry", "refreshing", "awaiting_confirm"),
"single": ("scanning", "awaiting_confirm"),
"multi": ("scanning", "refreshing", "awaiting_confirm")}
# 单据类型:scheduled 定时补货 / single 单次补货(会话里一个零件,「订」「确认提交」在会话里回)/
# multi 批量补货(会话里一次发多个零件:复用单次补货的查询链路,结果做成单据页链接,建单 / 提交在页面上点)
PAGE_KINDS = ("scheduled", "multi") # 在单据页上确认 / 建单 / 提交的
CHAT_KINDS = ("single", "multi") # 会话里发起、消息只发回所属会话的
BUSY = ("scanning", "scan_retry", "refreshing", "creating", "screenshotting", "submitting")
OPEN = BUSY + ("awaiting_confirm", "create_unknown", "awaiting_submit") # 还没走完的单
TERMINAL = ("done", "cancelled", "error", "superseded", "expired", "terminated")
STAGE_CN = {"scanning": "取数中", "scan_retry": "取数失败等待重试", "refreshing": "刷新取数中",
"awaiting_confirm": "待确认", "creating": "建单中", "create_unknown": "建单异常", "screenshotting": "截图中",
"awaiting_submit": "待确认提交", "submitting": "提交中", "done": "已完成",
"cancelled": "不订", "error": "出错", "superseded": "已作废", "expired": "已过期",
"terminated": "已终止"}
KIND_CN = {"scheduled": "定时补货", "single": "单次补货", "multi": "批量补货"}
VIA_CN = {"chat": "企微群里回复", "order_page": "单据页按钮", "console": "后台"}
TRIGGER_CN = {"schedule": "定时推送", "console": "手动补充", "chat": "企微对话"}
PUSH_WAIT_S = 15 # 到点时手动补货还在跑:每隔这么久看一次,跑完了再开始定时推送
ETA_SECONDS = 180 # 取数 / 建单消息里说的「大概要等多久」
CONFIRM_GRACE = 3 # 有效期判定的网络余量(秒)
RECONCILE_WAIT_S = 120 # 建单结果未知后,至少等这么久才允许再确认(防 DMS 在网关报错之后才落库)
RECONCILE_LOOKBACK_MIN = 10 # 核实时从补货单创建前多少分钟开始翻 DMS 采购单列表
UNKNOWN_REMIND_EVERY_MIN = 120 # 单次补货建单异常:第一次提醒之后,每隔这么久最多再提醒一次
SCHED_UNKNOWN_STATES = ("awaiting_confirm", "superseded", "expired") # 定时补货建单结果未知、可自动核实的状态
SCHED_RECONCILE_RETRY_MIN = 5 # 定时补货自动核实失败(取数端不在线 / 查询失败)后隔多久再试
SCHED_RECONCILE_DAYS = 2 # 定时补货自动核实只看最近几天的单
SUBMIT_EXACT = ("提交", "确认提交")
def explicit_submit(text: str) -> bool:
"""群里的话是不是明确要「提交」:去掉空格和标点后必须正好是「提交」或「确认提交」。
「提交吧」「可以,提交」「没问题提交」一律不算(会回复提示),大模型误调用也提交不了。"""
t = re.sub(r"[\s,,。.!!~~、;;::]", "", text or "")
return t in SUBMIT_EXACT
def _money(v) -> float:
try:
return float(v or 0)
except (TypeError, ValueError):
return 0.0
def eta_text(seconds: float) -> str:
"""秒数 → 「还要等多久」:45 秒 / 90 秒都说「大概 1 分钟」,150 秒说「大概 2 分半」。"""
m = max(float(seconds or 0), 0) / 60
if m <= 1.5:
return "大概 1 分钟"
whole, rest = int(m), m - int(m)
if rest < 0.25:
return f"大概 {whole} 分钟"
if rest < 0.75:
return f"大概 {whole} 分半"
return f"大概 {whole + 1} 分钟"
def at(day: dt.date, hhmm: str) -> dt.datetime:
h, m = (int(x) for x in hhmm.split(":")[:2])
return dt.datetime.combine(day, dt.time(h, m))
def window_of(day: dt.date, cut: str) -> tuple[dt.datetime, dt.datetime]:
"""出库窗口:前一天 cut(含)~ 当天 cut(不含)。"""
end = at(day, cut)
return end - dt.timedelta(days=1), end
def _ts(v: dt.datetime | None) -> str:
return v.strftime("%Y-%m-%d %H:%M:%S") if v else ""
def _bool(v) -> bool:
return bool(v) and v not in ("0", "false", "False")