refactor(business): 编辑出参取校验返回值省重查,submit_change 批量预载

- update_receipt 取 _ensure_shipment_of_business 返回值组装出参
  (shipment_id 已赋为 payload 新值,与 get_with_refs 重查同源),
  省一次三表 join;update_shipment 同理取锁定行的 business_no
- 删 business_repository.count_items(无任何调用的死代码,删除保护
  实际走 has_docs)
- submit_change 校验段定义与岗位各一条 IN 批量预载(逐条查询在批次
  扩大时是写侧 N+1);岗位字典键 casefold 归一保持与单条版 ci 校对
  等价的命中语义;旧值留痕保持逐条(窗口函数优化留待 M3 一并设计)

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
liuwenxinandClaude Code committed 2026-10-15 09:32:35 +08:00
1 parent 5ef48abf47
commit 409e26614f
6 files changed
+57 -19

No files matched your search

+20 -3
View File
@@ -218,11 +218,28 @@ def submit_change(
if payload.change_type == 2 and effective < date.today():
raise BadRequestError("指定生效日期不得早于今日")
# 先全量校验、再写入:任何一项不合法整批拒绝(不留半批脏数据)
# 先全量校验、再写入:任何一项不合法整批拒绝(不留半批脏数据)。
# 定义与岗位各一条 IN 批量查询预载(逐条查询在批次扩大时是写侧 N+1);
# 岗位预载收集全部非空维度值 —— 集合可能略大于实际所需,但省去按
# 维度类型先分类的第二轮遍历,无害多取由校验逻辑自行过滤
codes = list({item.param_code for item in payload.items})
definitions = param_repository.list_definitions_by_codes(session, codes)
dimension_values = list(
{item.dimension_value for item in payload.items if item.dimension_value}
)
# 预载字典键归一为 casefold:单条版 get_by_code 走 SQL ci 校对(大小写
# 变形可命中),批量版改字典查找后须自行等价归一,否则变形大小写会
# 被误判「不是已存在的岗位」;岗位编码均为 ascii 小写蛇形,casefold 足够
roles = {
code.casefold(): role
for code, role in business_role_repository.list_by_codes(
session, dimension_values
).items()
}
normalized: list[tuple[ParamDefinition, str, str]] = []
seen: set[tuple[str, str]] = set()
for item in payload.items:
definition = param_repository.get_definition_by_code(session, item.param_code)
definition = definitions.get(item.param_code)
if definition is None:
# 用户输入先行查库给 400;未注册编码出现在库里属数据与注册表漂移
raise BadRequestError(f"参数 {item.param_code} 未在系统中定义")
@@ -234,7 +251,7 @@ def submit_change(
if spec.dimension_type == "NONE" and dimension != "":
raise BadRequestError(f"参数 {item.param_code} 不带维度,维度值须为空")
if spec.dimension_type == "BUSINESS_ROLE" and dimension:
role = business_role_repository.get_by_code(session, dimension)
role = roles.get(dimension.casefold())
if role is None:
raise BadRequestError(
f"参数 {item.param_code} 的维度值 {dimension!r} 不是已存在的岗位"
+6 -3
View File
@@ -284,8 +284,11 @@ def update_receipt(
if receipt.settlement_status == SETTLEMENT_DONE:
raise BadRequestError("已核算回款禁止修改")
business = _ensure_business_writable(session, receipt.business_id, "修改")
# 返回值不取用:DTO 组装以 get_with_refs 重查为准(shipment 变更后同源)
_ensure_shipment_of_business(session, payload.shipment_id, receipt.business_id)
# 校验返回的 (shipment_no, ship_date) 即新批次 DTO 所需 —— shipment_id
# 已被赋为 payload 新值,与重查 get_with_refs 同源,省一次三表 join
shipment_no, ship_date = _ensure_shipment_of_business(
session, payload.shipment_id, receipt.business_id
)
receipt.receipt_date = payload.receipt_date
receipt.shipment_id = payload.shipment_id
@@ -293,7 +296,7 @@ def update_receipt(
receipt.is_barter = payload.is_barter
receipt.remark = payload.remark
_recalc_totals(session, business)
out = _to_out(receipt_repository.get_with_refs(session, receipt_id))
out = _to_out((receipt, business.business_no, shipment_no, ship_date))
batch_no = receipt.receipt_batch_no
session.commit()
# commit 后不再触碰 ORM 属性(audit_service 约定):批次号已固化
+2 -1
View File
@@ -317,7 +317,8 @@ def update_shipment(
total = shipment_repository.sum_item_amount(session, shipment_id)
shipment.total_amount = total if total is not None else Decimal("0")
_recalc_totals(session, business)
out = _to_out(shipment_repository.get_with_business_no(session, shipment_id))
# business_id 创建时定死不可改挂,锁定的 business.business_no 与重查同源
out = _to_out((shipment, business.business_no))
shipment_no = shipment.shipment_no
session.commit()
# commit 后不再触碰 ORM 属性(audit_service 约定):单号已固化