chore(order-delivery-reply): 前端页面调整、细节修改、优化逻辑等
This commit is contained in:
1 parent
7fb36b301c
commit
36868f875c
36 files changed
+1577
-376
No files matched your search
@@ -36,7 +36,7 @@ from app.services.integrations.wecom.notify import send_wecom_event_message
|
||||
# 企微事件开关名;与 settings.WECOM_NOTIFY_EVENTS 默认值条目一致(同 contract_payment_warning 命名风格)。
|
||||
WECOM_EVENT_TYPE = "order_delivery_reply_notify"
|
||||
# 工作台跳转路由(前端第 8 步落地,带上该行查询参数直开行)。
|
||||
WORKSPACE_JUMP_PATH = "/workspace/order-delivery-reply"
|
||||
JUMP_PATH = "/apps/order-delivery-reply" # 免布局嵌入路由:企微内打开不带工作台侧栏
|
||||
|
||||
|
||||
def _load_line(order_no: str, line_no: int) -> OrderDeliveryErpLine:
|
||||
@@ -94,7 +94,7 @@ def _send_line_notification(line: OrderDeliveryErpLine, recipient: User) -> dict
|
||||
user=recipient,
|
||||
title="订单交期回复",
|
||||
content=f"您有订单号:项次{line.order_no}:{line.line_no}的订单需要填写/确认/查看交期。",
|
||||
jump_path=WORKSPACE_JUMP_PATH,
|
||||
jump_path=JUMP_PATH,
|
||||
jump_params={"order_no": line.order_no, "line_no": line.line_no},
|
||||
)
|
||||
|
||||
|
||||
@@ -76,6 +76,21 @@ def _locked_subquery():
|
||||
)
|
||||
|
||||
|
||||
def _reply_person_subquery(like: str):
|
||||
"""该订单行的交期回复人员(姓名/工号,存于行状态快照)命中关键词的 EXISTS 子查询。"""
|
||||
return (
|
||||
select(OrderDeliveryLineState.line_id)
|
||||
.where(
|
||||
OrderDeliveryLineState.line_id == OrderDeliveryErpLine.id,
|
||||
or_(
|
||||
OrderDeliveryLineState.notified_user_name.ilike(like),
|
||||
OrderDeliveryLineState.notified_employee_id.ilike(like),
|
||||
),
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
|
||||
|
||||
def _base_query() -> Select:
|
||||
"""列表基础查询:只取未标 erp_missing 的已同步行(默认列表不展示已不在同步范围的行)。"""
|
||||
return select(OrderDeliveryErpLine).where(OrderDeliveryErpLine.erp_missing.is_(False))
|
||||
@@ -86,7 +101,8 @@ def _apply_filters(stmt: Select, params: dict[str, Any]) -> Select:
|
||||
应用列表筛选(AND 语义)与状态筛选下推。
|
||||
|
||||
核心逻辑:keyword 关键词合并搜索——订单单号/客户订单/客户编号/客户简称/公司全名/
|
||||
产品编号/品名/客户产品编号命中任一字段即返回(OR,工作台单搜索框口径);
|
||||
产品编号/品名/客户产品编号/业务员(姓名、工号)/交期回复人员(姓名、工号,
|
||||
2026-09-23 增补,按行状态快照 EXISTS)命中任一字段即返回(OR,工作台单搜索框口径);
|
||||
order_date 精确单日;status=replied → 已锁定,
|
||||
status=not_fully_replied → 未锁定(未回复∪部分回复)。
|
||||
"""
|
||||
@@ -103,6 +119,9 @@ def _apply_filters(stmt: Select, params: dict[str, Any]) -> Select:
|
||||
OrderDeliveryErpLine.part_no.ilike(like),
|
||||
OrderDeliveryErpLine.part_name.ilike(like),
|
||||
OrderDeliveryErpLine.customer_part_no.ilike(like),
|
||||
OrderDeliveryErpLine.salesperson_name.ilike(like),
|
||||
OrderDeliveryErpLine.salesperson_code.ilike(like),
|
||||
_reply_person_subquery(like),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -8,9 +8,11 @@ backend/app/services/order_delivery_reply/reminder_service.py
|
||||
逻辑:[出货提醒] 已锁定(已回复)行的批次 planned_date 落在 [今天, 今天+N] 时,按业务员聚合
|
||||
发一条企微;同一 (行, 交期) 只提醒一次(audit_events action=ship_remind 去重),发送失败
|
||||
不落成功记录,次日扫描自动补发。
|
||||
[未回复提醒] 未锁定行的快照首次入库(created_at)距今 ≥ N 天时再次提醒生管(收件人 =
|
||||
排程任务 notify_user_ids_json),同一行每 N 天最多提醒一次(action=unreplied_remind 去重),
|
||||
多行聚合成一条通知避免刷屏。提醒只读业务数据 + 写审计,绝不改动批次/锁定状态。
|
||||
[未回复提醒] 未锁定行的订单日期(order_date,取 ERP oea02)距今 ≥ N 天时再次提醒生管
|
||||
(收件人 = 任务参数 unreplied_remind_user_ids,留空回退任务通知人),同一行每 N 天最多
|
||||
提醒一次(action=unreplied_remind 去重),多行聚合成一条通知避免刷屏。
|
||||
提醒只读业务数据 + 写审计,绝不改动批次/锁定状态。
|
||||
(2026-09-23 修订:超期锚点由快照首次入库 created_at 改为订单日期,按业务确认以订单日期计超期。)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -33,7 +35,7 @@ from app.services.integrations.wecom.notify import send_wecom_event_message
|
||||
|
||||
# 企微事件开关名:与行级通知共用同一事件(WECOM_NOTIFY_EVENTS 已包含,无需新增配置)。
|
||||
WECOM_EVENT_TYPE = "order_delivery_reply_notify"
|
||||
WORKSPACE_JUMP_PATH = "/workspace/order-delivery-reply"
|
||||
JUMP_PATH = "/apps/order-delivery-reply" # 免布局嵌入路由:企微内打开不带工作台侧栏
|
||||
|
||||
# 通知正文最多列出的订单条数;超出的折叠为「…等 N 笔」,避免超出企微卡片描述上限。
|
||||
MAX_LISTED_ORDERS = 8
|
||||
@@ -125,7 +127,7 @@ def _send_remind_card(user: User, content: str, jump_params: dict[str, Any] | No
|
||||
user=user,
|
||||
title="订单交期回复",
|
||||
content=content,
|
||||
jump_path=WORKSPACE_JUMP_PATH,
|
||||
jump_path=JUMP_PATH,
|
||||
jump_params=jump_params,
|
||||
)
|
||||
return {"ok": bool(result.get("ok")), "trace_id": result.get("trace_id"), "reason": result.get("reason")}
|
||||
@@ -222,7 +224,8 @@ def run_ship_reminders(advance_days: int) -> dict[str, Any]:
|
||||
content = (
|
||||
f"您有 {len(bucket['entries'])} 笔订单将在 {advance_days} 天内交货,请及时安排出货:\n{lines_text}"
|
||||
)
|
||||
result = _send_remind_card(user, content)
|
||||
# 卡片带 status=replied:业务员点开直达「已回复」视图(出货提醒行必然已回复锁定)。
|
||||
result = _send_remind_card(user, content, jump_params={"status": "replied"})
|
||||
if result["ok"]:
|
||||
notified_persons += 1
|
||||
notified_lines += len(bucket["line_ids"])
|
||||
@@ -258,10 +261,11 @@ def run_ship_reminders(advance_days: int) -> dict[str, Any]:
|
||||
|
||||
def run_unreplied_reminders(unreplied_days: int, notify_user_ids: list[int]) -> dict[str, Any]:
|
||||
"""
|
||||
未回复再提醒:超 N 天仍未回复的订单行,聚合提醒生管(收件人 = 排程任务通知人配置)。
|
||||
未回复再提醒:订单日期距今超 N 天仍未回复的订单行,聚合提醒生管(收件人 = 排程任务通知人配置)。
|
||||
|
||||
核心逻辑:[STEP 1] 候选 = 未锁定且未标 erp_missing、快照首次入库(created_at)距今
|
||||
≥ unreplied_days 的行;[STEP 2] 同行距上次提醒 ≥ unreplied_days 才再次提醒
|
||||
核心逻辑:[STEP 1] 候选 = 未锁定且未标 erp_missing、订单日期(order_date)距今
|
||||
≥ unreplied_days 的行(订单日期为空不参与提醒);
|
||||
[STEP 2] 同行距上次提醒 ≥ unreplied_days 才再次提醒
|
||||
(audit action=unreplied_remind 的 max(created_at) 控制,未提醒过则立即提醒);
|
||||
[STEP 3] 汇总为一
|
||||
条企微发给每个配置的生管收件人;无收件人时跳过并如实返回。
|
||||
@@ -269,7 +273,7 @@ def run_unreplied_reminders(unreplied_days: int, notify_user_ids: list[int]) ->
|
||||
{"unreplied_days", "due_lines", "notified_recipients", "failed_recipients", "skipped_no_recipient"}。
|
||||
"""
|
||||
today = _business_today()
|
||||
threshold_dt = datetime.utcnow() - timedelta(days=max(0, int(unreplied_days)))
|
||||
threshold_date = today - timedelta(days=max(0, int(unreplied_days)))
|
||||
|
||||
locked_subq = (
|
||||
select(OrderDeliveryLineState.line_id)
|
||||
@@ -283,9 +287,12 @@ def run_unreplied_reminders(unreplied_days: int, notify_user_ids: list[int]) ->
|
||||
.where(
|
||||
OrderDeliveryErpLine.erp_missing.is_(False),
|
||||
~locked_subq,
|
||||
OrderDeliveryErpLine.created_at <= threshold_dt,
|
||||
# 超期锚点 = ERP 订单日期(oea02,2026-09-23 由 created_at 改来,按业务确认口径):
|
||||
# 订单日期距今 ≥ N 天仍未锁定即入选;订单日期为空无法判定,不参与提醒。
|
||||
OrderDeliveryErpLine.order_date.is_not(None),
|
||||
OrderDeliveryErpLine.order_date <= threshold_date,
|
||||
)
|
||||
.order_by(OrderDeliveryErpLine.created_at.asc())
|
||||
.order_by(OrderDeliveryErpLine.order_date.asc())
|
||||
).all()
|
||||
)
|
||||
if not due_lines:
|
||||
|
||||
@@ -4,7 +4,8 @@ backend/app/services/order_delivery_reply/reply_service.py
|
||||
用途:订单交期回复的写路径——单行/联合保存交期批次(未锁定行整表替换)。
|
||||
依赖:models/order_delivery_reply(快照/批次/状态/审计)、extensions.db、core.errors。
|
||||
逻辑:保存与通知是两步(需求 §5):本服务只写批次与 save 审计,不通知、不锁定;
|
||||
已锁定行拒绝写入(409);批次校验(交期必填、数量>0、备注≤200)在此层完成。
|
||||
已锁定行拒绝写入(409);批次校验(交期必填、数量>0、备注≤200、各批次合计必须
|
||||
等于订单数量——2026-09-23 由「超额仅提醒」修订,BATCH_QTY_TOTAL_MISMATCH)在此层完成。
|
||||
后续通知/锁定在 notify_service 中与企微发送同事务实现,禁止在本文件锁定。
|
||||
"""
|
||||
|
||||
@@ -136,9 +137,10 @@ def save_batches(order_no: str, line_no: int, raw_batches: Any, operator_user_id
|
||||
AI/排程不触发本函数(无自动回复语义)。
|
||||
|
||||
核心逻辑:[STEP 1] 校验批次结构(_parse_batches);
|
||||
[STEP 2] get_line_for_write 确认行存在且未锁定;
|
||||
[STEP 2] get_line_for_write 确认行存在且未锁定;数量守恒校验:各批次合计必须
|
||||
等于订单数量(2026-09-23 由「超额仅提醒」修订),不等抛 422 BATCH_QTY_TOTAL_MISMATCH;
|
||||
[STEP 3] 事务内删除旧批次、按序号写入新批次;
|
||||
[STEP 4] 写 action=save 审计(含批次快照,超额不拦截只留痕,展示层标「超」)。
|
||||
[STEP 4] 写 action=save 审计(含批次快照)。
|
||||
保存后行仍为部分回复;通知与锁定走 notify 路径,本函数不触碰 line_states。
|
||||
|
||||
Args:
|
||||
@@ -151,6 +153,16 @@ def save_batches(order_no: str, line_no: int, raw_batches: Any, operator_user_id
|
||||
normalized = _parse_batches(raw_batches)
|
||||
line, _state = get_line_for_write(order_no, line_no)
|
||||
|
||||
# [STEP 2] 数量守恒:各批次合计必须等于订单数量(保存口径 2026-09-23 修订,原允许超额仅提醒);
|
||||
# 校验在任何写操作之前,失败时不落批次也不落审计。
|
||||
total_qty = sum((batch["qty"] for batch in normalized), Decimal(0))
|
||||
order_qty = line.qty if line.qty is not None else Decimal(0)
|
||||
if total_qty != order_qty:
|
||||
raise ValidationError(
|
||||
f"各批次数量合计({total_qty})必须等于订单数量({order_qty}),请调整后重新保存",
|
||||
code="ORDER_DELIVERY_REPLY.BATCH_QTY_TOTAL_MISMATCH",
|
||||
)
|
||||
|
||||
# [STEP 3] 整表替换:未锁定行的旧批次全部作废,以本次提交为准。
|
||||
db.session.query(OrderDeliveryBatch).filter(OrderDeliveryBatch.line_id == line.id).delete()
|
||||
for seq, batch in enumerate(normalized, start=1):
|
||||
@@ -165,7 +177,6 @@ def save_batches(order_no: str, line_no: int, raw_batches: Any, operator_user_id
|
||||
)
|
||||
)
|
||||
|
||||
total_qty = sum((batch["qty"] for batch in normalized), Decimal(0))
|
||||
db.session.add(
|
||||
OrderDeliveryAuditEvent(
|
||||
line_id=line.id,
|
||||
|
||||
@@ -53,6 +53,44 @@ ERP_LINE_COLUMNS = (
|
||||
|
||||
_FETCH_SIZE = 500
|
||||
|
||||
_thick_init_lock = threading.Lock()
|
||||
_thick_initialized = False
|
||||
|
||||
|
||||
def _ensure_thick_client(oracledb) -> None:
|
||||
"""按 ORACLE_CLIENT_LIB_DIR(或 backend/oracle_client 约定)初始化 thick 模式;幂等且失败不掩埋。"""
|
||||
global _thick_initialized
|
||||
with _thick_init_lock:
|
||||
if _thick_initialized or not getattr(oracledb, "is_thin_mode", lambda: True)():
|
||||
_thick_initialized = True
|
||||
return
|
||||
candidates = []
|
||||
env_dir = (os.getenv("ORACLE_CLIENT_LIB_DIR") or "").strip()
|
||||
if env_dir:
|
||||
candidates.append(os.path.normpath(env_dir))
|
||||
backend_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
candidates.append(os.path.join(backend_dir, "oracle_client"))
|
||||
marker = {"oci.dll", "libclntsh.so", "libclntsh.dylib"}
|
||||
lib_dir = None
|
||||
for root in candidates:
|
||||
if not root or not os.path.isdir(root):
|
||||
continue
|
||||
if marker & {n.lower() for n in os.listdir(root)}:
|
||||
lib_dir = root
|
||||
break
|
||||
sub = [d for d in os.listdir(root) if d.startswith("instantclient_") and os.path.isdir(os.path.join(root, d))]
|
||||
if sub:
|
||||
lib_dir = os.path.join(root, sub[0])
|
||||
break
|
||||
if lib_dir:
|
||||
os.environ["PATH"] = lib_dir + os.pathsep + os.environ.get("PATH", "")
|
||||
if hasattr(os, "add_dll_directory"):
|
||||
os.add_dll_directory(lib_dir)
|
||||
oracledb.init_oracle_client(lib_dir=lib_dir)
|
||||
else:
|
||||
oracledb.init_oracle_client()
|
||||
_thick_initialized = True
|
||||
|
||||
# 进程内互斥:同一 worker 同时只跑一个同步(多 worker 由 sync_runs running 行兜底,一期单机足够)。
|
||||
_sync_lock = threading.Lock()
|
||||
|
||||
@@ -82,8 +120,6 @@ class OrderDeliverySyncService:
|
||||
"""
|
||||
import oracledb
|
||||
|
||||
from app.services.text2sql import Text2SQLService
|
||||
|
||||
user = (
|
||||
os.getenv("ERP_DB_USER")
|
||||
or os.getenv("ERP_USERNAME")
|
||||
@@ -104,8 +140,10 @@ class OrderDeliverySyncService:
|
||||
try:
|
||||
return oracledb.connect(user=user, password=password, dsn=dsn)
|
||||
except oracledb.DatabaseError:
|
||||
# thin 模式连不上时按 Text2SQL 既有路径切 thick(本地 Instant Client)。
|
||||
Text2SQLService._init_oracle_client(oracledb)
|
||||
# thin 模式连不上(topprod 为 11g 必走 thick):用 ORACLE_CLIENT_LIB_DIR 自初始化,
|
||||
# 不再依赖 services/text2sql——worker 进程 sys.path 差异曾致
|
||||
# "No module named app.services.text2sql"(2026-09-22 排程同步失败根因)。
|
||||
_ensure_thick_client(oracledb)
|
||||
return oracledb.connect(user=user, password=password, dsn=dsn)
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in new issue
Block a user