feat(shipping-notice): 增强发货通知与审计功能,支持敏感数据控制与哈希链校验
- 更新发货通知工具 API,剥离不可信的操作人身份字段,确保审计安全 - 引入审计事件的哈希链校验机制,支持数据完整性验证 - 增强订单抽取与审计功能,支持敏感快照的访问控制 - 更新相关模型与服务,确保新功能的完整性与可追溯性 - 完善文档与代码注释,反映新的业务逻辑与审计要求
This commit is contained in:
1 parent
95cdfe66b0
commit
d0fe084999
225 files changed
+25923
-6653
No files matched your search
@@ -3,7 +3,7 @@ services/shipping_notice/query_service.py
|
||||
|
||||
用途:发货通知读模型 — 任务/批次查询、排序与序列化投影(含版本链、待办分类与任务中心字段)。
|
||||
依赖:models.ShippingBatch、ShippingCase、ShippingNoticeCandidate、ShippingTask、ShippingTaskEvent、
|
||||
ShippingOrderReviewCase、ShippingOrderExtractionRun、state_machine、动态订单字段编辑器。
|
||||
ShippingReservation、ShippingOrderReviewCase、ShippingOrderExtractionRun、state_machine、动态订单字段编辑器。
|
||||
逻辑:workspace_shipping_notice 与 shipping_notice_tools 的读路径统一经本模块;
|
||||
tools.py 通过 TaskProjection / BatchProjection 委托序列化与任务解析,写路径逐步下沉;
|
||||
SUMMARY 经 _enrich_task_center_fields 附带 todo_category / can_handle / ui_state / recent_event;
|
||||
@@ -11,7 +11,7 @@ services/shipping_notice/query_service.py
|
||||
客户关键字 SQL 同时过滤 Review Case(文件名/Case 号/PO)与 Task(客户快照/来源文件名);
|
||||
待办 todo_category 下推为与状态机同序的 SQL CASE;list 用 UNION ALL + COUNT/OFFSET/LIMIT,
|
||||
仅水合当前页(禁止 500 截断冒充总数);
|
||||
DETAIL 另投影 parse_validation / process_field_gate / source_candidate 供持久只读展示;
|
||||
DETAIL 另投影 parse_validation / process_field_gate / source_candidate 与履约时间线(Reservation/Artifact);
|
||||
订单行仅从任务锁定 Extraction 的 FieldValue 投影,未抽取任务返回空行集。
|
||||
"""
|
||||
|
||||
@@ -22,7 +22,7 @@ from enum import Enum
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import asc, case, desc, exists, func, literal, select, union_all
|
||||
from sqlalchemy import String, asc, case, cast, desc, exists, func, literal, select, union_all
|
||||
|
||||
from app import db
|
||||
from app.models import (
|
||||
@@ -38,6 +38,7 @@ from app.models import (
|
||||
ShippingOrderExtractionRun,
|
||||
ShippingOrderLine,
|
||||
ShippingOrderReviewCase,
|
||||
ShippingReservation,
|
||||
ShippingTask,
|
||||
ShippingTaskEvent,
|
||||
ShippingUploadAttempt,
|
||||
@@ -833,8 +834,10 @@ def task_todo_category(task: ShippingTask) -> str:
|
||||
"""
|
||||
由 checkpoint_kind + 复核检查点推导待办分类(服务端唯一口径)。
|
||||
|
||||
核心逻辑:complete→done;wait_manual_upload→manual_upload;
|
||||
must_human 且属复核入口→review,其余 must_human→exception;auto_continue→in_progress。
|
||||
核心逻辑:wait_manual_upload→manual_upload(含 Excel 导出后待登记);
|
||||
complete→done;must_human 且属复核入口→review,其余 must_human→exception;
|
||||
auto_continue→in_progress。
|
||||
筛选 manual_upload 时 SQL 侧亦覆盖 waiting_upload / 有导出文件的任务。
|
||||
|
||||
Returns:
|
||||
review | exception | manual_upload | in_progress | done
|
||||
@@ -845,10 +848,10 @@ def task_todo_category(task: ShippingTask) -> str:
|
||||
|
||||
def _todo_category_from_ui(checkpoint: str | None, kind: CheckpointKind) -> str:
|
||||
"""将 checkpoint_kind(及复核入口集合)映射为 todo_category。"""
|
||||
if kind == CheckpointKind.COMPLETE:
|
||||
return "done"
|
||||
if kind == CheckpointKind.WAIT_MANUAL_UPLOAD:
|
||||
return "manual_upload"
|
||||
if kind == CheckpointKind.COMPLETE:
|
||||
return "done"
|
||||
if kind == CheckpointKind.MUST_HUMAN:
|
||||
if str(checkpoint or "") in _REVIEW_TODO_CHECKPOINTS:
|
||||
return "review"
|
||||
@@ -982,7 +985,8 @@ def _enrich_task_center_fields(
|
||||
为 SUMMARY/DETAIL 投影补充任务中心字段(就地写入并返回 data)。
|
||||
|
||||
核心逻辑:todo_category / created_by 始终写入;viewer 给定时写入 can_handle;
|
||||
recent_event / artifact_summary / ui_state 懒加载同源状态,避免任务中心再推导。
|
||||
recent_event / artifact_summary / ui_state 懒加载同源状态,避免任务中心再推导;
|
||||
DETAIL 另附 workflow_item(含 Workflow Event)供时间线与最后活动展示。
|
||||
|
||||
Args:
|
||||
task: 源任务。
|
||||
@@ -1008,6 +1012,25 @@ def _enrich_task_center_fields(
|
||||
return data
|
||||
|
||||
|
||||
def _attach_workflow_item_projection(task: ShippingTask, data: dict[str, Any]) -> dict[str, Any]:
|
||||
"""
|
||||
为任务详情附带绑定的 WorkflowItem 投影(含可见 Workflow Event)。
|
||||
|
||||
核心逻辑:按 current_task_no 定位 Item;未绑定时 workflow_item=None,不阻断详情。
|
||||
"""
|
||||
try:
|
||||
from app.services.shipping_notice.workflow_ledger_service import find_item_by_current_task_no
|
||||
from app.services.shipping_notice.workflow_progress_projection import serialize_workflow_item
|
||||
|
||||
item = find_item_by_current_task_no(task_no=str(task.task_no or ''))
|
||||
data['workflow_item'] = (
|
||||
serialize_workflow_item(item, include_events=True) if item is not None else None
|
||||
)
|
||||
except Exception:
|
||||
data['workflow_item'] = None
|
||||
return data
|
||||
|
||||
|
||||
def serialize_task_summary(
|
||||
task: ShippingTask,
|
||||
*,
|
||||
@@ -1035,7 +1058,7 @@ def serialize_task_detail(
|
||||
is_admin: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
工作台任务详情用的 DETAIL 投影(含 lines、events 与任务中心 enrich)。
|
||||
工作台任务详情用的 DETAIL 投影(含 lines、events、WorkflowItem 与任务中心 enrich)。
|
||||
|
||||
Args:
|
||||
task: 源任务。
|
||||
@@ -1043,9 +1066,10 @@ def serialize_task_detail(
|
||||
is_admin: 是否管理员视图。
|
||||
"""
|
||||
data = serialize_task(task, TaskProjection.DETAIL)
|
||||
return _enrich_task_center_fields(
|
||||
_enrich_task_center_fields(
|
||||
task, data, viewer_user_id=viewer_user_id, is_admin=is_admin,
|
||||
)
|
||||
return _attach_workflow_item_projection(task, data)
|
||||
|
||||
|
||||
def get_batch_for_user(batch_no: str, user_id: int) -> ShippingBatch | None:
|
||||
@@ -1368,15 +1392,22 @@ def _todo_category_sql_filter(allowed: frozenset[str]):
|
||||
核心逻辑:与 task_todo_category 对齐的保守 OR;精确分类由 _task_todo_category_sql_expr 完成。
|
||||
"""
|
||||
clauses = []
|
||||
if "done" in allowed:
|
||||
clauses.append(ShippingTask.process_status.in_(("uploaded", "closed", "rejected")))
|
||||
if "manual_upload" in allowed:
|
||||
# wait_manual_upload:waiting_upload / 已有导出文件且未 uploaded。
|
||||
clauses.append(
|
||||
db.or_(
|
||||
ShippingTask.process_status.in_(("waiting_upload", "upload_failed")),
|
||||
ShippingTask.export_file_path.isnot(None),
|
||||
db.and_(
|
||||
ShippingTask.export_file_path.isnot(None),
|
||||
ShippingTask.process_status.notin_(("uploaded", "closed", "rejected")),
|
||||
),
|
||||
)
|
||||
)
|
||||
if "done" in allowed:
|
||||
# 已上传/关闭/驳回终态(不含待人工上传登记)。
|
||||
clauses.append(
|
||||
ShippingTask.process_status.in_(("uploaded", "closed", "rejected"))
|
||||
)
|
||||
if "review" in allowed:
|
||||
clauses.append(
|
||||
db.or_(
|
||||
@@ -1419,8 +1450,14 @@ def _task_has_dynamic_order_lines_sql():
|
||||
|
||||
|
||||
def _sql_json_falsy(column):
|
||||
"""JSON 列在 Python 中视为假值的 SQL 近似(None / [] / {})。"""
|
||||
return db.or_(column.is_(None), column == [], column == {})
|
||||
"""
|
||||
JSON 列在 Python 中视为假值的 SQL 近似(None / [] / {})。
|
||||
|
||||
核心逻辑:Postgres `json` 无等号运算符(`json = json` 会 UndefinedFunction);
|
||||
转为 text 再与紧凑字面量比较,兼容 SQLite 测试库。
|
||||
"""
|
||||
as_text = cast(column, String)
|
||||
return db.or_(column.is_(None), as_text == '[]', as_text == '{}')
|
||||
|
||||
|
||||
def _sql_str_blank(column):
|
||||
@@ -1511,15 +1548,11 @@ def _task_todo_category_sql_expr():
|
||||
),
|
||||
literal("exception"),
|
||||
),
|
||||
(reason == PH_UPLOAD_RESULT_UNKNOWN, literal("exception")),
|
||||
(reason == PH_UPLOAD_RESULT_UNKNOWN, literal("manual_upload")),
|
||||
(reason == PH_RPA_PENDING, literal("exception")),
|
||||
(
|
||||
db.or_(
|
||||
reason == PH_UPLOAD_RPA_PENDING,
|
||||
ShippingTask.process_status == "waiting_upload",
|
||||
),
|
||||
literal("exception"),
|
||||
),
|
||||
(reason == PH_UPLOAD_RPA_PENDING, literal("exception")),
|
||||
# Excel 后待人工上传登记。
|
||||
(ShippingTask.process_status == "waiting_upload", literal("manual_upload")),
|
||||
else_=literal("in_progress"),
|
||||
)
|
||||
|
||||
@@ -1601,8 +1634,17 @@ def _todo_tasks_base_query(
|
||||
)
|
||||
)
|
||||
elif normalized_bucket == "done":
|
||||
# Excel 就绪(matched+export / waiting_upload)与历史上传完成均归 done。
|
||||
q = q.filter(
|
||||
ShippingTask.process_status.in_(("uploaded", "closed", "rejected"))
|
||||
db.or_(
|
||||
ShippingTask.process_status.in_(
|
||||
("uploaded", "closed", "rejected", "waiting_upload")
|
||||
),
|
||||
db.and_(
|
||||
ShippingTask.process_status == "matched",
|
||||
ShippingTask.export_file_path.isnot(None),
|
||||
),
|
||||
)
|
||||
)
|
||||
return q.filter(_task_todo_category_sql_expr().in_(tuple(sorted(allowed))))
|
||||
|
||||
@@ -1790,7 +1832,17 @@ def count_my_todos(user_id: int, *, is_admin: bool = False) -> dict[str, int]:
|
||||
processing = active.filter(category_expr == "in_progress").count()
|
||||
done = (
|
||||
_visible_tasks_query_for_todos(user_id, is_admin=is_admin)
|
||||
.filter(ShippingTask.process_status.in_(("uploaded", "closed", "rejected")))
|
||||
.filter(
|
||||
db.or_(
|
||||
ShippingTask.process_status.in_(
|
||||
("uploaded", "closed", "rejected", "waiting_upload")
|
||||
),
|
||||
db.and_(
|
||||
ShippingTask.process_status == "matched",
|
||||
ShippingTask.export_file_path.isnot(None),
|
||||
),
|
||||
)
|
||||
)
|
||||
.filter(category_expr == "done")
|
||||
.count()
|
||||
)
|
||||
|
||||
Reference in new issue
Block a user