- 将 RPA 管理 API 扩展至出站报表配置,新增出站相关的回调接口。 - 重构 freight_rpa_report_config_service,支持入站与出站报表配置的管理。 - 更新 ORM 模型,增加出站报表相关的持久化模型。 - 统一命名规范,确保在手、入站与出站的概念一致性。
111 lines
3.6 KiB
Python
111 lines
3.6 KiB
Python
"""
|
||
scripts/freight_rpa/outbound/outbound_excel_empty.py
|
||
|
||
用途:出站 Excel「有表头无明细」判空(真相源,对齐入站 Excel 判空)。
|
||
依赖:openpyxl(运行期可选;缺失时不强制 empty)。
|
||
逻辑:outbound_rpa_worker 落盘校验后调用;标题行(如 Outbound Details)
|
||
不得当成表头,否则真表头会被误判为明细导致假 success。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from pathlib import Path
|
||
|
||
|
||
class NoOutboundDataError(RuntimeError):
|
||
"""出站报表可打开且有表头,但无业务明细行(合法 empty / no_outbound_data)。"""
|
||
|
||
|
||
def _normalize_row_joined(row: tuple | list) -> str:
|
||
"""
|
||
将一行单元格规范为小写比较串。
|
||
|
||
核心逻辑:换行压成空格,去空白后用 | 拼接。
|
||
"""
|
||
parts: list[str] = []
|
||
for cell in row:
|
||
if cell is None:
|
||
continue
|
||
text = ' '.join(str(cell).replace('\n', ' ').replace('\r', ' ').split()).strip()
|
||
if text:
|
||
parts.append(text.lower())
|
||
return ' | '.join(parts)
|
||
|
||
|
||
def is_outbound_excel_header_row(joined: str) -> bool:
|
||
"""
|
||
判断是否为出站列标题行(非报表标题行)。
|
||
|
||
核心逻辑:要求 sku/quantity 与 outbound/shipment/dispatch 等组合特征;
|
||
单独含 outbound(如「Outbound Details」标题)不得命中。
|
||
"""
|
||
if not joined:
|
||
return False
|
||
has_sku = 'sku' in joined or 'part number' in joined or 'item' in joined
|
||
has_qty = 'quantity' in joined or 'qty' in joined
|
||
has_move = (
|
||
'outbound' in joined
|
||
or 'shipment' in joined
|
||
or 'dispatch' in joined
|
||
or 'shipped' in joined
|
||
or 'ship to' in joined
|
||
)
|
||
if has_sku and has_qty and has_move:
|
||
return True
|
||
if 'warehouse' in joined and has_move and (has_sku or has_qty):
|
||
return True
|
||
if 'supplier id' in joined and 'aws sku' in joined and has_move:
|
||
return True
|
||
return False
|
||
|
||
|
||
def outbound_excel_has_detail_rows(path: Path) -> bool | None:
|
||
"""
|
||
判断出站 Excel 列标题之后是否有明细行。
|
||
|
||
核心逻辑:先定位严格表头行,再扫描其后非空行;无法识别表头时返回 None
|
||
(不强制 empty,由结构校验兜底)。
|
||
|
||
Returns:
|
||
True: 有明细;False: 有表头无明细;None: 未识别表头形态。
|
||
"""
|
||
try:
|
||
from openpyxl import load_workbook
|
||
except ImportError:
|
||
return None
|
||
try:
|
||
workbook = load_workbook(path, read_only=True, data_only=True)
|
||
except Exception:
|
||
return None
|
||
try:
|
||
worksheet = workbook.active
|
||
if worksheet is None:
|
||
return None
|
||
header_idx: int | None = None
|
||
for idx, row in enumerate(worksheet.iter_rows(max_row=120, values_only=True), start=1):
|
||
joined = _normalize_row_joined(row or ())
|
||
if not joined:
|
||
continue
|
||
if header_idx is None:
|
||
if is_outbound_excel_header_row(joined):
|
||
header_idx = idx
|
||
continue
|
||
if idx > header_idx:
|
||
return True
|
||
if header_idx is None:
|
||
return None
|
||
return False
|
||
finally:
|
||
workbook.close()
|
||
|
||
|
||
def enforce_outbound_excel_not_empty(path: Path) -> None:
|
||
"""
|
||
有表头无明细时记为无出站(NoOutboundDataError → empty / no_outbound_data)。
|
||
|
||
Raises:
|
||
NoOutboundDataError: 可识别表头但无业务行;message 固定为 no_outbound_data。
|
||
"""
|
||
if outbound_excel_has_detail_rows(path) is False:
|
||
raise NoOutboundDataError('no_outbound_data')
|